Skip to content
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

feat: Parse half-open ..= X patterns #13769

Merged
merged 1 commit into from
Dec 14, 2022
Merged
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
75 changes: 43 additions & 32 deletions crates/parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,50 @@ fn pattern_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
}

fn pattern_single_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
if let Some(lhs) = atom_pat(p, recovery_set) {
// test range_pat
// fn main() {
// match 92 {
// 0 ... 100 => (),
// 101 ..= 200 => (),
// 200 .. 301 => (),
// 302 .. => (),
// }
//
// match Some(10 as u8) {
// Some(0) | None => (),
// Some(1..) => ()
// }
//
// match () {
// S { a: 0 } => (),
// S { a: 1.. } => (),
// }
//
// match () {
// [0] => (),
// [1..] => (),
// }
//
// match (10 as u8, 5 as u8) {
// (0, _) => (),
// (1.., _) => ()
// }
// }
// test range_pat
// fn main() {
// match 92 {
// 0 ... 100 => (),
// 101 ..= 200 => (),
// 200 .. 301 => (),
// 302 .. => (),
// ..= 303 => (),
// }
//
// match Some(10 as u8) {
// Some(0) | None => (),
// Some(1..) => (),
// Some(..=2) => (),
// }
//
// match () {
// S { a: 0 } => (),
// S { a: 1.. } => (),
// S { a: ..=2 } => (),
// }
//
// match () {
// [0] => (),
// [1..] => (),
// [..=2] => (),
// }
//
// match (10 as u8, 5 as u8) {
// (0, _) => (),
// (1.., _) => (),
// (..=2, _) => (),
// }
// }

if p.at(T![..=]) {
let m = p.start();
p.bump(T![..=]);
atom_pat(p, recovery_set);
m.complete(p, RANGE_PAT);
return;
}

// FIXME: support half_open_range_patterns (`..=2`),
// exclusive_range_pattern (`..5`) with missing lhs
if let Some(lhs) = atom_pat(p, recovery_set) {
for range_op in [T![...], T![..=], T![..]] {
if p.at(range_op) {
let m = lhs.precede(p);
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl<'t> Parser<'t> {
Marker::new(pos)
}

/// Consume the next token if `kind` matches.
/// Consume the next token. Panics if the parser isn't currently at `kind`.
pub(crate) fn bump(&mut self, kind: SyntaxKind) {
assert!(self.eat(kind));
}
Expand Down
106 changes: 106 additions & 0 deletions crates/parser/test_data/parser/inline/ok/0058_range_pat.rast
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ SOURCE_FILE
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
RANGE_PAT
DOT2EQ "..="
WHITESPACE " "
LITERAL_PAT
LITERAL
INT_NUMBER "303"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
Expand Down Expand Up @@ -169,6 +184,28 @@ SOURCE_FILE
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
TUPLE_STRUCT_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "Some"
L_PAREN "("
RANGE_PAT
DOT2EQ "..="
LITERAL_PAT
LITERAL
INT_NUMBER "2"
R_PAREN ")"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
Expand Down Expand Up @@ -240,6 +277,36 @@ SOURCE_FILE
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
RECORD_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_PAT_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_PAT_FIELD
NAME_REF
IDENT "a"
COLON ":"
WHITESPACE " "
RANGE_PAT
DOT2EQ "..="
LITERAL_PAT
LITERAL
INT_NUMBER "2"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
Expand Down Expand Up @@ -285,6 +352,23 @@ SOURCE_FILE
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
SLICE_PAT
L_BRACK "["
RANGE_PAT
DOT2EQ "..="
LITERAL_PAT
LITERAL
INT_NUMBER "2"
R_BRACK "]"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
Expand Down Expand Up @@ -360,6 +444,28 @@ SOURCE_FILE
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
TUPLE_PAT
L_PAREN "("
RANGE_PAT
DOT2EQ "..="
LITERAL_PAT
LITERAL
INT_NUMBER "2"
COMMA ","
WHITESPACE " "
WILDCARD_PAT
UNDERSCORE "_"
R_PAREN ")"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n"
Expand Down
9 changes: 7 additions & 2 deletions crates/parser/test_data/parser/inline/ok/0058_range_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ fn main() {
101 ..= 200 => (),
200 .. 301 => (),
302 .. => (),
..= 303 => (),
}

match Some(10 as u8) {
Some(0) | None => (),
Some(1..) => ()
Some(1..) => (),
Some(..=2) => (),
}

match () {
S { a: 0 } => (),
S { a: 1.. } => (),
S { a: ..=2 } => (),
}

match () {
[0] => (),
[1..] => (),
[..=2] => (),
}

match (10 as u8, 5 as u8) {
(0, _) => (),
(1.., _) => ()
(1.., _) => (),
(..=2, _) => (),
}
}