index expr

This commit is contained in:
Aleksey Kladov 2018-08-05 17:24:44 +03:00
parent 8cf962f1f5
commit 80fa861cd6
6 changed files with 54 additions and 1 deletions

View file

@ -153,6 +153,7 @@ Grammar(
// postfix
"CALL_EXPR",
"INDEX_EXPR",
"METHOD_CALL_EXPR",
"FIELD_EXPR",
"TRY_EXPR",

View file

@ -147,6 +147,7 @@ fn postfix_expr(p: &mut Parser, mut lhs: CompletedMarker) -> CompletedMarker {
loop {
lhs = match p.current() {
L_PAREN => call_expr(p, lhs),
L_BRACK => index_expr(p, lhs),
DOT if p.nth(1) == IDENT => if p.nth(2) == L_PAREN || p.nth(2) == COLONCOLON {
method_call_expr(p, lhs)
} else {
@ -172,6 +173,19 @@ fn call_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
m.complete(p, CALL_EXPR)
}
// test index_expr
// fn foo() {
// x[1][2];
// }
fn index_expr(p: &mut Parser, lhs: CompletedMarker) -> CompletedMarker {
assert!(p.at(L_BRACK));
let m = lhs.precede(p);
p.bump();
expr(p);
p.expect(R_BRACK);
m.complete(p, INDEX_EXPR)
}
// test method_call_expr
// fn foo() {
// x.foo();

View file

@ -141,6 +141,7 @@ pub enum SyntaxKind {
STRUCT_LIT,
STRUCT_LIT_FIELD,
CALL_EXPR,
INDEX_EXPR,
METHOD_CALL_EXPR,
FIELD_EXPR,
TRY_EXPR,
@ -367,6 +368,7 @@ impl SyntaxKind {
STRUCT_LIT => &SyntaxInfo { name: "STRUCT_LIT" },
STRUCT_LIT_FIELD => &SyntaxInfo { name: "STRUCT_LIT_FIELD" },
CALL_EXPR => &SyntaxInfo { name: "CALL_EXPR" },
INDEX_EXPR => &SyntaxInfo { name: "INDEX_EXPR" },
METHOD_CALL_EXPR => &SyntaxInfo { name: "METHOD_CALL_EXPR" },
FIELD_EXPR => &SyntaxInfo { name: "FIELD_EXPR" },
TRY_EXPR => &SyntaxInfo { name: "TRY_EXPR" },

View file

@ -71,7 +71,7 @@ impl RedNode {
match &self.children.read()[idx] {
Some(child) => return Some(child.into()),
None => (),
}
};
let green_children = self.green.children();
let start_offset = self.start_offset()
+ green_children[..idx]

View file

@ -0,0 +1,3 @@
fn foo() {
x[1][2];
}

View file

@ -0,0 +1,33 @@
FILE@[0; 26)
FN_ITEM@[0; 26)
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_EXPR@[9; 26)
L_CURLY@[9; 10)
EXPR_STMT@[10; 24)
INDEX_EXPR@[10; 22)
INDEX_EXPR@[10; 19)
PATH_EXPR@[10; 16)
PATH@[10; 16)
PATH_SEGMENT@[10; 16)
NAME_REF@[10; 16)
WHITESPACE@[10; 15)
IDENT@[15; 16) "x"
L_BRACK@[16; 17)
LITERAL@[17; 18)
INT_NUMBER@[17; 18) "1"
R_BRACK@[18; 19)
L_BRACK@[19; 20)
LITERAL@[20; 21)
INT_NUMBER@[20; 21) "2"
R_BRACK@[21; 22)
SEMI@[22; 23)
WHITESPACE@[23; 24)
R_CURLY@[24; 25)
WHITESPACE@[25; 26)