Skip to content

Parse subslice patterns #1509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions crates/ra_parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ 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);
p.bump();
atom_pat(p, recovery_set);
m.complete(p, RANGE_PAT);
if let Some(_) = atom_pat(p, recovery_set) {
m.complete(p, RANGE_PAT);
} else {
m.abandon(p);
p.err_recover("expected pattern", recovery_set);
}
}
// test marco_pat
// fn main() {
Expand Down Expand Up @@ -145,7 +149,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 +247,45 @@ 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) {
match p.current() {
T![..] => p.bump(),
_ => {
if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern");
break;
}
pattern(p)
while !p.at(EOF) && !p.at(T![']']) {
let la0 = p.nth(0);
let la1 = p.nth(1);
if la0 == T![ref]
|| la0 == T![mut]
|| la0 == T![box]
|| (la0 == IDENT
&& !(la1 == T![::] || la1 == T!['('] || la1 == T!['{'] || la1 == T![!]))
{
let m = p.start();
bind_pat(p, true);
if p.current() == T![..] {
p.bump();
m.complete(p, SUBSLICE_PAT);
} else {
m.abandon(p);
}
} else if la0 == T![..] {
p.bump();
} else {
if !p.at_ts(PATTERN_FIRST) {
p.error("expected a pattern");
break;
}
pattern(p);
}
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"