Ref exprs

This commit is contained in:
Aleksey Kladov 2018-07-31 12:51:00 +03:00
parent ff71cb1f6d
commit 66182ed5d5
5 changed files with 83 additions and 1 deletions

View File

@ -126,6 +126,7 @@ Grammar(
"TUPLE_EXPR", "TUPLE_EXPR",
"PATH_EXPR", "PATH_EXPR",
"CALL_EXPR", "CALL_EXPR",
"REF_EXPR",
"EXTERN_BLOCK", "EXTERN_BLOCK",
"ENUM_VARIANT", "ENUM_VARIANT",

View File

@ -26,7 +26,7 @@ pub(super) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
} }
pub(super) fn expr(p: &mut Parser) { pub(super) fn expr(p: &mut Parser) {
let mut lhs = atom_expr(p); let mut lhs = prefix_expr(p);
while let Some(m) = lhs { while let Some(m) = lhs {
match p.current() { match p.current() {
@ -36,6 +36,27 @@ pub(super) fn expr(p: &mut Parser) {
} }
} }
fn prefix_expr(p: &mut Parser) -> Option<CompletedMarker> {
match p.current() {
AMPERSAND => Some(ref_expr(p)),
_ => atom_expr(p)
}
}
// test ref_expr
// fn foo() {
// let _ = &1;
// let _ = &mut &f();
// }
fn ref_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(AMPERSAND));
let m = p.start();
p.bump();
p.eat(MUT_KW);
expr(p);
m.complete(p, REF_EXPR)
}
fn atom_expr(p: &mut Parser) -> Option<CompletedMarker> { fn atom_expr(p: &mut Parser) -> Option<CompletedMarker> {
match literal(p) { match literal(p) {
Some(m) => return Some(m), Some(m) => return Some(m),

View File

@ -117,6 +117,7 @@ pub enum SyntaxKind {
TUPLE_EXPR, TUPLE_EXPR,
PATH_EXPR, PATH_EXPR,
CALL_EXPR, CALL_EXPR,
REF_EXPR,
EXTERN_BLOCK, EXTERN_BLOCK,
ENUM_VARIANT, ENUM_VARIANT,
NAMED_FIELD, NAMED_FIELD,
@ -266,6 +267,7 @@ pub(crate) fn info(self) -> &'static SyntaxInfo {
TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" }, TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" },
PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" }, PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" },
CALL_EXPR => &SyntaxInfo { name: "CALL_EXPR" }, CALL_EXPR => &SyntaxInfo { name: "CALL_EXPR" },
REF_EXPR => &SyntaxInfo { name: "REF_EXPR" },
EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" },
ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" },
NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" },

View File

@ -0,0 +1,4 @@
fn foo() {
let _ = &1;
let _ = &mut &f();
}

View File

@ -0,0 +1,54 @@
FILE@[0; 52)
FN_ITEM@[0; 52)
FN_KW@[0; 2)
NAME@[2; 6)
WHITESPACE@[2; 3)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 9)
L_PAREN@[6; 7)
R_PAREN@[7; 8)
WHITESPACE@[8; 9)
BLOCK@[9; 52)
L_CURLY@[9; 10)
LET_STMT@[10; 31)
WHITESPACE@[10; 15)
LET_KW@[15; 18)
PLACEHOLDER_PAT@[18; 21)
WHITESPACE@[18; 19)
UNDERSCORE@[19; 20)
WHITESPACE@[20; 21)
EQ@[21; 22)
REF_EXPR@[22; 25)
WHITESPACE@[22; 23)
AMPERSAND@[23; 24)
LITERAL@[24; 25)
INT_NUMBER@[24; 25)
SEMI@[25; 26)
WHITESPACE@[26; 31)
LET_STMT@[31; 50)
LET_KW@[31; 34)
PLACEHOLDER_PAT@[34; 37)
WHITESPACE@[34; 35)
UNDERSCORE@[35; 36)
WHITESPACE@[36; 37)
EQ@[37; 38)
REF_EXPR@[38; 48)
WHITESPACE@[38; 39)
AMPERSAND@[39; 40)
MUT_KW@[40; 43)
REF_EXPR@[43; 48)
WHITESPACE@[43; 44)
AMPERSAND@[44; 45)
CALL_EXPR@[45; 48)
PATH_EXPR@[45; 46)
PATH@[45; 46)
PATH_SEGMENT@[45; 46)
NAME_REF@[45; 46)
IDENT@[45; 46) "f"
ARG_LIST@[46; 48)
L_PAREN@[46; 47)
R_PAREN@[47; 48)
SEMI@[48; 49)
WHITESPACE@[49; 50)
R_CURLY@[50; 51)
WHITESPACE@[51; 52)