Skip to content

Commit

Permalink
Parse subslice patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjakubowski committed Jul 7, 2019
1 parent fee552d commit 78963c9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
7 changes: 7 additions & 0 deletions crates/ra_parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const PAT_RECOVERY_SET: TokenSet =
fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
let la0 = p.nth(0);
let la1 = p.nth(1);
if la0 == IDENT && la1 == T![..] {
let m = p.start();
p.bump();
p.bump();
return Some(m.complete(p, SUBSLICE_PAT));
}
if la0 == T![ref]
|| la0 == T![mut]
|| la0 == T![box]
Expand Down Expand Up @@ -225,6 +231,7 @@ fn tuple_pat(p: &mut Parser) -> CompletedMarker {
// test slice_pat
// fn main() {
// let [a, b, ..] = [];
// let [a, b..] = [];
// }
fn slice_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(T!['[']));
Expand Down
2 changes: 2 additions & 0 deletions crates/ra_parser/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub enum SyntaxKind {
SLICE_PAT,
RANGE_PAT,
LITERAL_PAT,
SUBSLICE_PAT,
TUPLE_EXPR,
ARRAY_EXPR,
PAREN_EXPR,
Expand Down Expand Up @@ -632,6 +633,7 @@ impl SyntaxKind {
SLICE_PAT => &SyntaxInfo { name: "SLICE_PAT" },
RANGE_PAT => &SyntaxInfo { name: "RANGE_PAT" },
LITERAL_PAT => &SyntaxInfo { name: "LITERAL_PAT" },
SUBSLICE_PAT => &SyntaxInfo { name: "SUBSLICE_PAT" },
TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" },
ARRAY_EXPR => &SyntaxInfo { name: "ARRAY_EXPR" },
PAREN_EXPR => &SyntaxInfo { name: "PAREN_EXPR" },
Expand Down
3 changes: 2 additions & 1 deletion crates/ra_syntax/src/grammar.ron
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Grammar(
"SLICE_PAT",
"RANGE_PAT",
"LITERAL_PAT",
"SUBSLICE_PAT",

// atoms
"TUPLE_EXPR",
Expand Down Expand Up @@ -686,7 +687,7 @@ Grammar(
"LifetimeArg": (),

"MacroItems": (
traits: [ "ModuleItemOwner", "FnDefOwner" ],
traits: [ "ModuleItemOwner", "FnDefOwner" ],
),

"MacroStmts" : (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
let [a, b, ..] = [];
let [a, b..] = [];
}
35 changes: 29 additions & 6 deletions crates/ra_syntax/tests/data/parser/inline/ok/0024_slice_pat.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SOURCE_FILE@[0; 39)
FN_DEF@[0; 38)
SOURCE_FILE@[0; 62)
FN_DEF@[0; 61)
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
NAME@[3; 7)
Expand All @@ -8,7 +8,7 @@ SOURCE_FILE@[0; 39)
L_PAREN@[7; 8) "("
R_PAREN@[8; 9) ")"
WHITESPACE@[9; 10) " "
BLOCK@[10; 38)
BLOCK@[10; 61)
L_CURLY@[10; 11) "{"
WHITESPACE@[11; 16) "\n "
LET_STMT@[16; 36)
Expand All @@ -35,6 +35,29 @@ SOURCE_FILE@[0; 39)
L_BRACK@[33; 34) "["
R_BRACK@[34; 35) "]"
SEMI@[35; 36) ";"
WHITESPACE@[36; 37) "\n"
R_CURLY@[37; 38) "}"
WHITESPACE@[38; 39) "\n"
WHITESPACE@[36; 41) "\n "
LET_STMT@[41; 59)
LET_KW@[41; 44) "let"
WHITESPACE@[44; 45) " "
SLICE_PAT@[45; 53)
L_BRACK@[45; 46) "["
BIND_PAT@[46; 47)
NAME@[46; 47)
IDENT@[46; 47) "a"
COMMA@[47; 48) ","
WHITESPACE@[48; 49) " "
SUBSLICE_PAT@[49; 52)
IDENT@[49; 50) "b"
DOTDOT@[50; 52) ".."
R_BRACK@[52; 53) "]"
WHITESPACE@[53; 54) " "
EQ@[54; 55) "="
WHITESPACE@[55; 56) " "
ARRAY_EXPR@[56; 58)
L_BRACK@[56; 57) "["
R_BRACK@[57; 58) "]"
SEMI@[58; 59) ";"
WHITESPACE@[59; 60) "\n"
R_CURLY@[60; 61) "}"
WHITESPACE@[61; 62) "\n"

0 comments on commit 78963c9

Please sign in to comment.