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 8, 2019
1 parent a4316d6 commit f109128
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
45 changes: 33 additions & 12 deletions crates/ra_parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) {
// }
if p.at(T![...]) || p.at(T![..=]) || p.at(T![..]) {
let m = lhs.precede(p);
let dots = p.current();
p.bump();
atom_pat(p, recovery_set);
m.complete(p, RANGE_PAT);
if dots == T![..] && !p.at_ts(PATTERN_FIRST) {
m.complete(p, SUBSLICE_PAT);
} else {
atom_pat(p, recovery_set);
m.complete(p, RANGE_PAT);
}
}
// test marco_pat
// fn main() {
Expand Down Expand Up @@ -145,7 +150,21 @@ fn path_pat(p: &mut Parser) -> CompletedMarker {
fn tuple_pat_fields(p: &mut Parser) {
assert!(p.at(T!['(']));
p.bump();
pat_list(p, T![')']);
while !p.at(EOF) && !p.at(T![')']) {
match p.current() {
T![..] => p.bump(),
_ => {
if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern");
break;
}
pattern(p)
}
}
if !p.at(T![')']) {
p.expect(T![,]);
}
}
p.expect(T![')']);
}

Expand Down Expand Up @@ -229,28 +248,30 @@ fn tuple_pat(p: &mut Parser) -> CompletedMarker {
fn slice_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(T!['[']));
let m = p.start();
// test subslice_pat
// fn main() {
// let [a, b..] = [];
// let &[ref a.., ref b] = [];
// }
p.bump();
pat_list(p, T![']']);
p.expect(T![']']);
m.complete(p, SLICE_PAT)
}

fn pat_list(p: &mut Parser, ket: SyntaxKind) {
while !p.at(EOF) && !p.at(ket) {
while !p.at(EOF) && !p.at(T![']']) {
match p.current() {
T![..] => p.bump(),
_ => {
if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern");
break;
}
pattern(p)
pattern(p);
p.eat(T![..]);
}
}
if !p.at(ket) {
if !p.at(T![']']) {
p.expect(T![,]);
}
}
p.expect(T![']']);
m.complete(p, SLICE_PAT)
}

// test bind_pat
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
@@ -0,0 +1,4 @@
fn main() {
let [a, b..] = [];
let &[ref a.., ref b] = [];
}
69 changes: 69 additions & 0 deletions crates/ra_syntax/tests/data/parser/inline/ok/0137_subslice_pat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
SOURCE_FILE@[0; 69)
FN_DEF@[0; 68)
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
NAME@[3; 7)
IDENT@[3; 7) "main"
PARAM_LIST@[7; 9)
L_PAREN@[7; 8) "("
R_PAREN@[8; 9) ")"
WHITESPACE@[9; 10) " "
BLOCK@[10; 68)
L_CURLY@[10; 11) "{"
WHITESPACE@[11; 16) "\n "
LET_STMT@[16; 34)
LET_KW@[16; 19) "let"
WHITESPACE@[19; 20) " "
SLICE_PAT@[20; 28)
L_BRACK@[20; 21) "["
BIND_PAT@[21; 22)
NAME@[21; 22)
IDENT@[21; 22) "a"
COMMA@[22; 23) ","
WHITESPACE@[23; 24) " "
SUBSLICE_PAT@[24; 27)
BIND_PAT@[24; 25)
NAME@[24; 25)
IDENT@[24; 25) "b"
DOTDOT@[25; 27) ".."
R_BRACK@[27; 28) "]"
WHITESPACE@[28; 29) " "
EQ@[29; 30) "="
WHITESPACE@[30; 31) " "
ARRAY_EXPR@[31; 33)
L_BRACK@[31; 32) "["
R_BRACK@[32; 33) "]"
SEMI@[33; 34) ";"
WHITESPACE@[34; 39) "\n "
LET_STMT@[39; 66)
LET_KW@[39; 42) "let"
WHITESPACE@[42; 43) " "
REF_PAT@[43; 60)
AMP@[43; 44) "&"
SLICE_PAT@[44; 60)
L_BRACK@[44; 45) "["
SUBSLICE_PAT@[45; 52)
BIND_PAT@[45; 50)
REF_KW@[45; 48) "ref"
WHITESPACE@[48; 49) " "
NAME@[49; 50)
IDENT@[49; 50) "a"
DOTDOT@[50; 52) ".."
COMMA@[52; 53) ","
WHITESPACE@[53; 54) " "
BIND_PAT@[54; 59)
REF_KW@[54; 57) "ref"
WHITESPACE@[57; 58) " "
NAME@[58; 59)
IDENT@[58; 59) "b"
R_BRACK@[59; 60) "]"
WHITESPACE@[60; 61) " "
EQ@[61; 62) "="
WHITESPACE@[62; 63) " "
ARRAY_EXPR@[63; 65)
L_BRACK@[63; 64) "["
R_BRACK@[64; 65) "]"
SEMI@[65; 66) ";"
WHITESPACE@[66; 67) "\n"
R_CURLY@[67; 68) "}"
WHITESPACE@[68; 69) "\n"

0 comments on commit f109128

Please sign in to comment.