Parse exclusive range pattern

This commit is contained in:
Ryo Yoshida 2023-04-16 01:53:51 +09:00
parent b218009f46
commit 89a1439de3
No known key found for this signature in database
GPG key ID: E25698A930586171
5 changed files with 116 additions and 38 deletions

View file

@ -106,7 +106,6 @@ stringify!(;
#[test]
fn range_patterns() {
// FIXME: rustc thinks there are three patterns here, not one.
check(
r#"
macro_rules! m {
@ -118,7 +117,7 @@ m!(.. .. ..);
macro_rules! m {
($($p:pat)*) => (stringify!($($p |)*);)
}
stringify!(.. .. .. |);
stringify!(.. | .. | .. |);
"#]],
);
}

View file

@ -15,6 +15,10 @@ pub(super) const PATTERN_FIRST: TokenSet =
const PAT_TOP_FIRST: TokenSet = PATTERN_FIRST.union(TokenSet::new(&[T![|]]));
/// Set of possible tokens at the start of a range pattern's end bound.
const RANGE_PAT_END_FIRST: TokenSet =
expressions::LITERAL_FIRST.union(paths::PATH_FIRST).union(TokenSet::new(&[T![-]]));
pub(crate) fn pattern(p: &mut Parser<'_>) {
pattern_r(p, PAT_RECOVERY_SET);
}
@ -105,6 +109,52 @@ fn pattern_single_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
return;
}
// test exclusive_range_pat
// fn main() {
// match 42 {
// ..0 => {}
// 1..2 => {}
// }
// }
// test dot_dot_pat
// fn main() {
// let .. = ();
// //
// // Tuples
// //
// let (a, ..) = ();
// let (a, ..,) = ();
// let Tuple(a, ..) = ();
// let Tuple(a, ..,) = ();
// let (.., ..) = ();
// let Tuple(.., ..) = ();
// let (.., a, ..) = ();
// let Tuple(.., a, ..) = ();
// //
// // Slices
// //
// let [..] = ();
// let [head, ..] = ();
// let [head, tail @ ..] = ();
// let [head, .., cons] = ();
// let [head, mid @ .., cons] = ();
// let [head, .., .., cons] = ();
// let [head, .., mid, tail @ ..] = ();
// let [head, .., mid, .., cons] = ();
// }
if p.at(T![..]) {
let m = p.start();
p.bump(T![..]);
if p.at_ts(RANGE_PAT_END_FIRST) {
atom_pat(p, recovery_set);
m.complete(p, RANGE_PAT);
} else {
m.complete(p, REST_PAT);
}
return;
}
if let Some(lhs) = atom_pat(p, recovery_set) {
for range_op in [T![...], T![..=], T![..]] {
if p.at(range_op) {
@ -173,7 +223,6 @@ fn atom_pat(p: &mut Parser<'_>, recovery_set: TokenSet) -> Option<CompletedMarke
_ if paths::is_path_start(p) => path_or_macro_pat(p),
_ if is_literal_pat_start(p) => literal_pat(p),
T![.] if p.at(T![..]) => rest_pat(p),
T![_] => wildcard_pat(p),
T![&] => ref_pat(p),
T!['('] => tuple_pat(p),
@ -334,39 +383,6 @@ fn wildcard_pat(p: &mut Parser<'_>) -> CompletedMarker {
m.complete(p, WILDCARD_PAT)
}
// test dot_dot_pat
// fn main() {
// let .. = ();
// //
// // Tuples
// //
// let (a, ..) = ();
// let (a, ..,) = ();
// let Tuple(a, ..) = ();
// let Tuple(a, ..,) = ();
// let (.., ..) = ();
// let Tuple(.., ..) = ();
// let (.., a, ..) = ();
// let Tuple(.., a, ..) = ();
// //
// // Slices
// //
// let [..] = ();
// let [head, ..] = ();
// let [head, tail @ ..] = ();
// let [head, .., cons] = ();
// let [head, mid @ .., cons] = ();
// let [head, .., .., cons] = ();
// let [head, .., mid, tail @ ..] = ();
// let [head, .., mid, .., cons] = ();
// }
fn rest_pat(p: &mut Parser<'_>) -> CompletedMarker {
assert!(p.at(T![..]));
let m = p.start();
p.bump(T![..]);
m.complete(p, REST_PAT)
}
// test ref_pat
// fn main() {
// let &a = ();

View file

@ -33,8 +33,7 @@ fn stmt() {
fn pat() {
check(PrefixEntryPoint::Pat, "x y", "x");
check(PrefixEntryPoint::Pat, "fn f() {}", "fn");
// FIXME: This one is wrong, we should consume only one pattern.
check(PrefixEntryPoint::Pat, ".. ..", ".. ..");
check(PrefixEntryPoint::Pat, ".. ..", "..");
}
#[test]

View file

@ -0,0 +1,58 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "main"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
MATCH_EXPR
MATCH_KW "match"
WHITESPACE " "
LITERAL
INT_NUMBER "42"
WHITESPACE " "
MATCH_ARM_LIST
L_CURLY "{"
WHITESPACE "\n "
MATCH_ARM
RANGE_PAT
DOT2 ".."
LITERAL_PAT
LITERAL
INT_NUMBER "0"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
R_CURLY "}"
WHITESPACE "\n "
MATCH_ARM
RANGE_PAT
LITERAL_PAT
LITERAL
INT_NUMBER "1"
DOT2 ".."
LITERAL_PAT
LITERAL
INT_NUMBER "2"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
R_CURLY "}"
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"

View file

@ -0,0 +1,6 @@
fn main() {
match 42 {
..0 => {}
1..2 => {}
}
}