for && array

This commit is contained in:
Aleksey Kladov 2018-08-05 18:24:56 +03:00
parent d44169ab1e
commit 60ba52b3e0
7 changed files with 150 additions and 0 deletions

View File

@ -140,12 +140,14 @@ Grammar(
// atoms
"TUPLE_EXPR",
"ARRAY_EXPR",
"PAREN_EXPR",
"PATH_EXPR",
"LAMBDA_EXPR",
"IF_EXPR",
"WHILE_EXPR",
"LOOP_EXPR",
"FOR_EXPR",
"BLOCK_EXPR",
"RETURN_EXPR",
"MATCH_EXPR",

View File

@ -44,11 +44,13 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<CompletedMark
let la = p.nth(1);
let done = match p.current() {
L_PAREN => tuple_expr(p),
L_BRACK => array_expr(p),
PIPE => lambda_expr(p),
MOVE_KW if la == PIPE => lambda_expr(p),
IF_KW => if_expr(p),
WHILE_KW => while_expr(p),
LOOP_KW => loop_expr(p),
FOR_KW => for_expr(p),
MATCH_KW => match_expr(p),
UNSAFE_KW if la == L_CURLY => block_expr(p),
L_CURLY => block_expr(p),
@ -86,6 +88,36 @@ fn tuple_expr(p: &mut Parser) -> CompletedMarker {
m.complete(p, if saw_expr && !saw_comma { PAREN_EXPR } else { TUPLE_EXPR })
}
// test array_expr
// fn foo() {
// [];
// [1];
// [1, 2,];
// [1; 2];
// }
fn array_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(L_BRACK));
let m = p.start();
p.bump();
if p.eat(R_BRACK) {
return m.complete(p, ARRAY_EXPR);
}
expr(p);
if p.eat(SEMI) {
expr(p);
p.expect(R_BRACK);
return m.complete(p, ARRAY_EXPR);
}
while !p.at(EOF) && !p.at(R_BRACK) {
p.expect(COMMA);
if !p.at(R_BRACK) {
expr(p);
}
}
p.expect(R_BRACK);
m.complete(p, ARRAY_EXPR)
}
// test lambda_expr
// fn foo() {
// || ();
@ -156,6 +188,21 @@ fn loop_expr(p: &mut Parser) -> CompletedMarker {
m.complete(p, LOOP_EXPR)
}
// test for_expr
// fn foo() {
// for x in [] {};
// }
fn for_expr(p: &mut Parser) -> CompletedMarker {
assert!(p.at(FOR_KW));
let m = p.start();
p.bump();
patterns::pattern(p);
p.expect(IN_KW);
expr_no_struct(p);
block(p);
m.complete(p, FOR_EXPR)
}
// test cond
// fn foo() { if let Some(_) = None {} }
fn cond(p: &mut Parser) {

View File

@ -130,12 +130,14 @@ pub enum SyntaxKind {
STRUCT_PAT,
TUPLE_PAT,
TUPLE_EXPR,
ARRAY_EXPR,
PAREN_EXPR,
PATH_EXPR,
LAMBDA_EXPR,
IF_EXPR,
WHILE_EXPR,
LOOP_EXPR,
FOR_EXPR,
BLOCK_EXPR,
RETURN_EXPR,
MATCH_EXPR,
@ -361,12 +363,14 @@ pub(crate) fn info(self) -> &'static SyntaxInfo {
STRUCT_PAT => &SyntaxInfo { name: "STRUCT_PAT" },
TUPLE_PAT => &SyntaxInfo { name: "TUPLE_PAT" },
TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" },
ARRAY_EXPR => &SyntaxInfo { name: "ARRAY_EXPR" },
PAREN_EXPR => &SyntaxInfo { name: "PAREN_EXPR" },
PATH_EXPR => &SyntaxInfo { name: "PATH_EXPR" },
LAMBDA_EXPR => &SyntaxInfo { name: "LAMBDA_EXPR" },
IF_EXPR => &SyntaxInfo { name: "IF_EXPR" },
WHILE_EXPR => &SyntaxInfo { name: "WHILE_EXPR" },
LOOP_EXPR => &SyntaxInfo { name: "LOOP_EXPR" },
FOR_EXPR => &SyntaxInfo { name: "FOR_EXPR" },
BLOCK_EXPR => &SyntaxInfo { name: "BLOCK_EXPR" },
RETURN_EXPR => &SyntaxInfo { name: "RETURN_EXPR" },
MATCH_EXPR => &SyntaxInfo { name: "MATCH_EXPR" },

View File

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

View File

@ -0,0 +1,34 @@
FILE@[0; 33)
FN_ITEM@[0; 33)
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; 33)
L_CURLY@[9; 10)
EXPR_STMT@[10; 31)
FOR_EXPR@[10; 29)
WHITESPACE@[10; 15)
FOR_KW@[15; 18)
BIND_PAT@[18; 21)
NAME@[18; 21)
WHITESPACE@[18; 19)
IDENT@[19; 20) "x"
WHITESPACE@[20; 21)
IN_KW@[21; 23)
ARRAY_EXPR@[23; 27)
WHITESPACE@[23; 24)
L_BRACK@[24; 25)
R_BRACK@[25; 26)
WHITESPACE@[26; 27)
BLOCK_EXPR@[27; 29)
L_CURLY@[27; 28)
R_CURLY@[28; 29)
SEMI@[29; 30)
WHITESPACE@[30; 31)
R_CURLY@[31; 32)
WHITESPACE@[32; 33)

View File

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

View File

@ -0,0 +1,54 @@
FILE@[0; 55)
FN_ITEM@[0; 55)
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; 55)
L_CURLY@[9; 10)
EXPR_STMT@[10; 23)
ARRAY_EXPR@[10; 17)
WHITESPACE@[10; 15)
L_BRACK@[15; 16)
R_BRACK@[16; 17)
SEMI@[17; 18)
WHITESPACE@[18; 23)
EXPR_STMT@[23; 32)
ARRAY_EXPR@[23; 26)
L_BRACK@[23; 24)
LITERAL@[24; 25)
INT_NUMBER@[24; 25) "1"
R_BRACK@[25; 26)
SEMI@[26; 27)
WHITESPACE@[27; 32)
EXPR_STMT@[32; 45)
ARRAY_EXPR@[32; 39)
L_BRACK@[32; 33)
LITERAL@[33; 34)
INT_NUMBER@[33; 34) "1"
COMMA@[34; 35)
LITERAL@[35; 37)
WHITESPACE@[35; 36)
INT_NUMBER@[36; 37) "2"
COMMA@[37; 38)
R_BRACK@[38; 39)
SEMI@[39; 40)
WHITESPACE@[40; 45)
EXPR_STMT@[45; 53)
ARRAY_EXPR@[45; 51)
L_BRACK@[45; 46)
LITERAL@[46; 47)
INT_NUMBER@[46; 47) "1"
SEMI@[47; 48)
LITERAL@[48; 50)
WHITESPACE@[48; 49)
INT_NUMBER@[49; 50) "2"
R_BRACK@[50; 51)
SEMI@[51; 52)
WHITESPACE@[52; 53)
R_CURLY@[53; 54)
WHITESPACE@[54; 55)