Parse ..= X patterns

This commit is contained in:
Jonas Schievink 2022-12-13 17:32:25 +01:00
parent 4596847a88
commit 9e0aaf4cf3
4 changed files with 157 additions and 35 deletions

View File

@ -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, _) => (),
// }
// }
// FIXME: support half_open_range_patterns (`..=2`),
// exclusive_range_pattern (`..5`) with missing lhs
if p.at(T![..=]) {
let m = p.start();
p.bump(T![..=]);
atom_pat(p, recovery_set);
m.complete(p, RANGE_PAT);
return;
}
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);

View File

@ -162,7 +162,7 @@ pub(crate) fn start(&mut self) -> Marker {
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));
}

View File

@ -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 "
@ -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 "
@ -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 "
@ -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 "
@ -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"

View File

@ -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, _) => (),
}
}