Auto merge of #13769 - jonas-schievink:parse-half-open-range-pat, r=jonas-schievink

feat: Parse half-open `..= X` patterns

Closes https://github.com/rust-lang/rust-analyzer/issues/13739
This commit is contained in:
bors 2022-12-14 14:14:46 +00:00
commit fb4e9359d1
4 changed files with 157 additions and 35 deletions

View file

@ -62,7 +62,6 @@ fn pattern_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
} }
fn pattern_single_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 // test range_pat
// fn main() { // fn main() {
// match 92 { // match 92 {
@ -70,31 +69,43 @@ fn pattern_single_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
// 101 ..= 200 => (), // 101 ..= 200 => (),
// 200 .. 301 => (), // 200 .. 301 => (),
// 302 .. => (), // 302 .. => (),
// ..= 303 => (),
// } // }
// //
// match Some(10 as u8) { // match Some(10 as u8) {
// Some(0) | None => (), // Some(0) | None => (),
// Some(1..) => () // Some(1..) => (),
// Some(..=2) => (),
// } // }
// //
// match () { // match () {
// S { a: 0 } => (), // S { a: 0 } => (),
// S { a: 1.. } => (), // S { a: 1.. } => (),
// S { a: ..=2 } => (),
// } // }
// //
// match () { // match () {
// [0] => (), // [0] => (),
// [1..] => (), // [1..] => (),
// [..=2] => (),
// } // }
// //
// match (10 as u8, 5 as u8) { // match (10 as u8, 5 as u8) {
// (0, _) => (), // (0, _) => (),
// (1.., _) => () // (1.., _) => (),
// (..=2, _) => (),
// } // }
// } // }
// FIXME: support half_open_range_patterns (`..=2`), if p.at(T![..=]) {
// exclusive_range_pattern (`..5`) with missing lhs 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![..]] { for range_op in [T![...], T![..=], T![..]] {
if p.at(range_op) { if p.at(range_op) {
let m = lhs.precede(p); let m = lhs.precede(p);

View file

@ -162,7 +162,7 @@ impl<'t> Parser<'t> {
Marker::new(pos) 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) { pub(crate) fn bump(&mut self, kind: SyntaxKind) {
assert!(self.eat(kind)); assert!(self.eat(kind));
} }

View file

@ -94,6 +94,21 @@ SOURCE_FILE
R_PAREN ")" R_PAREN ")"
COMMA "," COMMA ","
WHITESPACE "\n " 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 "}" R_CURLY "}"
WHITESPACE "\n\n " WHITESPACE "\n\n "
EXPR_STMT EXPR_STMT
@ -169,6 +184,28 @@ SOURCE_FILE
TUPLE_EXPR TUPLE_EXPR
L_PAREN "(" L_PAREN "("
R_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 " WHITESPACE "\n "
R_CURLY "}" R_CURLY "}"
WHITESPACE "\n\n " WHITESPACE "\n\n "
@ -241,6 +278,36 @@ SOURCE_FILE
R_PAREN ")" R_PAREN ")"
COMMA "," COMMA ","
WHITESPACE "\n " 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 "}" R_CURLY "}"
WHITESPACE "\n\n " WHITESPACE "\n\n "
EXPR_STMT EXPR_STMT
@ -286,6 +353,23 @@ SOURCE_FILE
R_PAREN ")" R_PAREN ")"
COMMA "," COMMA ","
WHITESPACE "\n " 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 "}" R_CURLY "}"
WHITESPACE "\n\n " WHITESPACE "\n\n "
MATCH_EXPR MATCH_EXPR
@ -360,6 +444,28 @@ SOURCE_FILE
TUPLE_EXPR TUPLE_EXPR
L_PAREN "(" L_PAREN "("
R_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 " WHITESPACE "\n "
R_CURLY "}" R_CURLY "}"
WHITESPACE "\n" WHITESPACE "\n"

View file

@ -4,25 +4,30 @@ fn main() {
101 ..= 200 => (), 101 ..= 200 => (),
200 .. 301 => (), 200 .. 301 => (),
302 .. => (), 302 .. => (),
..= 303 => (),
} }
match Some(10 as u8) { match Some(10 as u8) {
Some(0) | None => (), Some(0) | None => (),
Some(1..) => () Some(1..) => (),
Some(..=2) => (),
} }
match () { match () {
S { a: 0 } => (), S { a: 0 } => (),
S { a: 1.. } => (), S { a: 1.. } => (),
S { a: ..=2 } => (),
} }
match () { match () {
[0] => (), [0] => (),
[1..] => (), [1..] => (),
[..=2] => (),
} }
match (10 as u8, 5 as u8) { match (10 as u8, 5 as u8) {
(0, _) => (), (0, _) => (),
(1.., _) => () (1.., _) => (),
(..=2, _) => (),
} }
} }