From 83a93f4bd3cb07f19044b27d954df746e9fb809e Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Sun, 16 Apr 2023 02:30:59 +0900 Subject: [PATCH] Parse inline const pattern in nested patterns --- crates/parser/src/grammar/patterns.rs | 11 +- .../inline/ok/0156_const_block_pat.rast | 120 ++++++++++++++++++ .../parser/inline/ok/0156_const_block_pat.rs | 8 ++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/crates/parser/src/grammar/patterns.rs b/crates/parser/src/grammar/patterns.rs index baa93aa083f..4801732101f 100644 --- a/crates/parser/src/grammar/patterns.rs +++ b/crates/parser/src/grammar/patterns.rs @@ -5,6 +5,7 @@ T![box], T![ref], T![mut], + T![const], T!['('], T!['['], T![&], @@ -17,7 +18,7 @@ /// 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![-]])); + expressions::LITERAL_FIRST.union(paths::PATH_FIRST).union(TokenSet::new(&[T![-], T![const]])); pub(crate) fn pattern(p: &mut Parser<'_>) { pattern_r(p, PAT_RECOVERY_SET); @@ -499,6 +500,14 @@ fn box_pat(p: &mut Parser<'_>) -> CompletedMarker { // fn main() { // let const { 15 } = (); // let const { foo(); bar() } = (); +// +// match 42 { +// const { 0 } .. const { 1 } => (), +// .. const { 0 } => (), +// const { 2 } .. => (), +// } +// +// let (const { () },) = (); // } fn const_block_pat(p: &mut Parser<'_>) -> CompletedMarker { assert!(p.at(T![const])); diff --git a/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rast b/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rast index 59de2b9f163..593867a7b12 100644 --- a/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rast +++ b/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rast @@ -74,6 +74,126 @@ SOURCE_FILE L_PAREN "(" R_PAREN ")" SEMICOLON ";" + WHITESPACE "\n\n " + EXPR_STMT + MATCH_EXPR + MATCH_KW "match" + WHITESPACE " " + LITERAL + INT_NUMBER "42" + WHITESPACE " " + MATCH_ARM_LIST + L_CURLY "{" + WHITESPACE "\n " + MATCH_ARM + RANGE_PAT + CONST_BLOCK_PAT + CONST_KW "const" + WHITESPACE " " + BLOCK_EXPR + STMT_LIST + L_CURLY "{" + WHITESPACE " " + LITERAL + INT_NUMBER "0" + WHITESPACE " " + R_CURLY "}" + WHITESPACE " " + DOT2 ".." + WHITESPACE " " + CONST_BLOCK_PAT + CONST_KW "const" + WHITESPACE " " + BLOCK_EXPR + STMT_LIST + L_CURLY "{" + WHITESPACE " " + LITERAL + INT_NUMBER "1" + WHITESPACE " " + R_CURLY "}" + WHITESPACE " " + FAT_ARROW "=>" + WHITESPACE " " + TUPLE_EXPR + L_PAREN "(" + R_PAREN ")" + COMMA "," + WHITESPACE "\n " + MATCH_ARM + RANGE_PAT + DOT2 ".." + WHITESPACE " " + CONST_BLOCK_PAT + CONST_KW "const" + WHITESPACE " " + BLOCK_EXPR + STMT_LIST + L_CURLY "{" + WHITESPACE " " + LITERAL + INT_NUMBER "0" + WHITESPACE " " + R_CURLY "}" + WHITESPACE " " + FAT_ARROW "=>" + WHITESPACE " " + TUPLE_EXPR + L_PAREN "(" + R_PAREN ")" + COMMA "," + WHITESPACE "\n " + MATCH_ARM + RANGE_PAT + CONST_BLOCK_PAT + CONST_KW "const" + WHITESPACE " " + BLOCK_EXPR + STMT_LIST + L_CURLY "{" + WHITESPACE " " + LITERAL + INT_NUMBER "2" + WHITESPACE " " + R_CURLY "}" + WHITESPACE " " + DOT2 ".." + WHITESPACE " " + FAT_ARROW "=>" + WHITESPACE " " + TUPLE_EXPR + L_PAREN "(" + R_PAREN ")" + COMMA "," + WHITESPACE "\n " + R_CURLY "}" + WHITESPACE "\n\n " + LET_STMT + LET_KW "let" + WHITESPACE " " + TUPLE_PAT + L_PAREN "(" + CONST_BLOCK_PAT + CONST_KW "const" + WHITESPACE " " + BLOCK_EXPR + STMT_LIST + L_CURLY "{" + WHITESPACE " " + TUPLE_EXPR + L_PAREN "(" + R_PAREN ")" + WHITESPACE " " + R_CURLY "}" + COMMA "," + R_PAREN ")" + WHITESPACE " " + EQ "=" + WHITESPACE " " + TUPLE_EXPR + L_PAREN "(" + R_PAREN ")" + SEMICOLON ";" WHITESPACE "\n" R_CURLY "}" WHITESPACE "\n" diff --git a/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rs b/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rs index dce9defac2f..6ecdee849b7 100644 --- a/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rs +++ b/crates/parser/test_data/parser/inline/ok/0156_const_block_pat.rs @@ -1,4 +1,12 @@ fn main() { let const { 15 } = (); let const { foo(); bar() } = (); + + match 42 { + const { 0 } .. const { 1 } => (), + .. const { 0 } => (), + const { 2 } .. => (), + } + + let (const { () },) = (); }