Skip to content

Commit

Permalink
Merge pull request #18453 from ChayimFriedman2/leading-or
Browse files Browse the repository at this point in the history
Parse patterns with leading pipe properly in all places
  • Loading branch information
Veykril authored Oct 31, 2024
2 parents 86a850d + 210ead8 commit 0ba893e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 16 deletions.
4 changes: 2 additions & 2 deletions crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) mod entry {
}

pub(crate) fn pat_top(p: &mut Parser<'_>) {
patterns::pattern_top(p);
patterns::pattern(p);
}

pub(crate) fn ty(p: &mut Parser<'_>) {
Expand Down Expand Up @@ -117,7 +117,7 @@ pub(crate) mod entry {

pub(crate) fn pattern(p: &mut Parser<'_>) {
let m = p.start();
patterns::pattern_top(p);
patterns::pattern(p);
if p.at(EOF) {
m.abandon(p);
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ fn for_expr(p: &mut Parser<'_>, m: Option<Marker>) -> CompletedMarker {
fn let_expr(p: &mut Parser<'_>) -> CompletedMarker {
let m = p.start();
p.bump(T![let]);
patterns::pattern_top(p);
patterns::pattern(p);
p.expect(T![=]);
expr_let(p);
m.complete(p, LET_EXPR)
Expand Down
24 changes: 11 additions & 13 deletions crates/parser/src/grammar/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ const PAT_TOP_FIRST: TokenSet = PATTERN_FIRST.union(TokenSet::new(&[T![|]]));
const RANGE_PAT_END_FIRST: TokenSet =
expressions::LITERAL_FIRST.union(paths::PATH_FIRST).union(TokenSet::new(&[T![-], T![const]]));

pub(crate) fn pattern(p: &mut Parser<'_>) {
let m = p.start();
pattern_r(p, m, false, PAT_RECOVERY_SET);
}

/// Parses a pattern list separated by pipes `|`.
pub(super) fn pattern_top(p: &mut Parser<'_>) {
pattern_top_r(p, PAT_RECOVERY_SET);
pub(crate) fn pattern(p: &mut Parser<'_>) {
pattern_r(p, PAT_RECOVERY_SET);
}

pub(crate) fn pattern_single(p: &mut Parser<'_>) {
Expand All @@ -37,9 +32,7 @@ pub(crate) fn pattern_single(p: &mut Parser<'_>) {
/// Parses a pattern list separated by pipes `|`
/// using the given `recovery_set`.
pub(super) fn pattern_top_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
let m = p.start();
let has_leading_pipe = p.eat(T![|]);
pattern_r(p, m, has_leading_pipe, recovery_set);
pattern_r(p, recovery_set);
}

// test or_pattern
Expand All @@ -53,7 +46,10 @@ pub(super) fn pattern_top_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
// }
/// Parses a pattern list separated by pipes `|`, with no leading `|`,using the
/// given `recovery_set`.
fn pattern_r(p: &mut Parser<'_>, m: Marker, has_leading_pipe: bool, recovery_set: TokenSet) {
fn pattern_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
let m = p.start();
let has_leading_pipe = p.eat(T![|]);

pattern_single_r(p, recovery_set);

if !p.at(T![|]) && !has_leading_pipe {
Expand Down Expand Up @@ -319,6 +315,8 @@ fn record_pat_field(p: &mut Parser<'_>) {
IDENT | INT_NUMBER if p.nth(1) == T![:] => {
name_ref_or_index(p);
p.bump(T![:]);
// test record_field_pat_leading_or
// fn foo() { let R { a: | 1 | 2 } = 0; }
pattern(p);
}
// test_err record_pat_field_eq_recovery
Expand Down Expand Up @@ -438,7 +436,7 @@ fn tuple_pat(p: &mut Parser<'_>) -> CompletedMarker {
}
has_rest |= p.at(T![..]);

pattern_top(p);
pattern(p);
if !p.at(T![')']) {
has_comma = true;
p.expect(T![,]);
Expand All @@ -465,7 +463,7 @@ fn slice_pat(p: &mut Parser<'_>) -> CompletedMarker {

fn pat_list(p: &mut Parser<'_>, ket: SyntaxKind) {
while !p.at(EOF) && !p.at(ket) {
pattern_top(p);
pattern(p);
if !p.eat(T![,]) {
if p.at_ts(PAT_TOP_FIRST) {
p.error(format!("expected {:?}, got {:?}", T![,], p.current()));
Expand Down
4 changes: 4 additions & 0 deletions crates/parser/test_data/generated/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ mod ok {
run_and_expect_no_errors("test_data/parser/inline/ok/record_field_list.rs");
}
#[test]
fn record_field_pat_leading_or() {
run_and_expect_no_errors("test_data/parser/inline/ok/record_field_pat_leading_or.rs");
}
#[test]
fn record_lit() { run_and_expect_no_errors("test_data/parser/inline/ok/record_lit.rs"); }
#[test]
fn record_literal_field_with_attr() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "foo"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
LET_STMT
LET_KW "let"
WHITESPACE " "
RECORD_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "R"
WHITESPACE " "
RECORD_PAT_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_PAT_FIELD
NAME_REF
IDENT "a"
COLON ":"
WHITESPACE " "
OR_PAT
PIPE "|"
WHITESPACE " "
LITERAL_PAT
LITERAL
INT_NUMBER "1"
WHITESPACE " "
PIPE "|"
WHITESPACE " "
LITERAL_PAT
LITERAL
INT_NUMBER "2"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
EQ "="
WHITESPACE " "
LITERAL
INT_NUMBER "0"
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn foo() { let R { a: | 1 | 2 } = 0; }

0 comments on commit 0ba893e

Please sign in to comment.