From 60ba52b3e022aa88b011295b38fcf50fe440d1f9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 5 Aug 2018 18:24:56 +0300 Subject: [PATCH] for && array --- src/grammar.ron | 2 + src/grammar/expressions/atom.rs | 47 +++++++++++++++++ src/syntax_kinds/generated.rs | 4 ++ tests/data/parser/inline/0085_for_expr.rs | 3 ++ tests/data/parser/inline/0085_for_expr.txt | 34 ++++++++++++ tests/data/parser/inline/0086_array_expr.rs | 6 +++ tests/data/parser/inline/0086_array_expr.txt | 54 ++++++++++++++++++++ 7 files changed, 150 insertions(+) create mode 100644 tests/data/parser/inline/0085_for_expr.rs create mode 100644 tests/data/parser/inline/0085_for_expr.txt create mode 100644 tests/data/parser/inline/0086_array_expr.rs create mode 100644 tests/data/parser/inline/0086_array_expr.txt diff --git a/src/grammar.ron b/src/grammar.ron index df769bba6bd..ca37eb80be9 100644 --- a/src/grammar.ron +++ b/src/grammar.ron @@ -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", diff --git a/src/grammar/expressions/atom.rs b/src/grammar/expressions/atom.rs index 65b9e5ef0ce..e4f681c17a1 100644 --- a/src/grammar/expressions/atom.rs +++ b/src/grammar/expressions/atom.rs @@ -44,11 +44,13 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option 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) { diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs index 1bce3444304..3c507112617 100644 --- a/src/syntax_kinds/generated.rs +++ b/src/syntax_kinds/generated.rs @@ -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 @@ impl SyntaxKind { 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" }, diff --git a/tests/data/parser/inline/0085_for_expr.rs b/tests/data/parser/inline/0085_for_expr.rs new file mode 100644 index 00000000000..972197d2a17 --- /dev/null +++ b/tests/data/parser/inline/0085_for_expr.rs @@ -0,0 +1,3 @@ +fn foo() { + for x in [] {}; +} diff --git a/tests/data/parser/inline/0085_for_expr.txt b/tests/data/parser/inline/0085_for_expr.txt new file mode 100644 index 00000000000..33dc86ca023 --- /dev/null +++ b/tests/data/parser/inline/0085_for_expr.txt @@ -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) diff --git a/tests/data/parser/inline/0086_array_expr.rs b/tests/data/parser/inline/0086_array_expr.rs new file mode 100644 index 00000000000..4dc1999d140 --- /dev/null +++ b/tests/data/parser/inline/0086_array_expr.rs @@ -0,0 +1,6 @@ +fn foo() { + []; + [1]; + [1, 2,]; + [1; 2]; +} diff --git a/tests/data/parser/inline/0086_array_expr.txt b/tests/data/parser/inline/0086_array_expr.txt new file mode 100644 index 00000000000..40a9b018c79 --- /dev/null +++ b/tests/data/parser/inline/0086_array_expr.txt @@ -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)