Skip to content

Commit afed274

Browse files
authored
Allow macro invocations in patterns (#143)
* Allow macro invocations in patterns * Regenerate parser files * Update corpus * Add additional test case for macro invocations in patterns Co-authored-by: ky <>
1 parent 9a6d980 commit afed274

File tree

6 files changed

+57030
-56973
lines changed

6 files changed

+57030
-56973
lines changed

corpus/expressions.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,11 @@ let msg = match x {
613613
(integer_literal))
614614
value: (string_literal))
615615
(match_arm
616-
pattern: (macro_invocation
617-
macro: (identifier)
618-
(token_tree
619-
(integer_literal)))
616+
pattern: (match_pattern
617+
(macro_invocation
618+
macro: (identifier)
619+
(token_tree
620+
(integer_literal))))
620621
value: (string_literal))
621622
(match_arm
622623
pattern: (match_pattern)

corpus/patterns.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ let ref mut x @ (A | B | C);
243243

244244
fn foo((1 | 2 | 3): u8) {}
245245

246+
if let x!() | y!() = () {}
247+
246248
// Discomment after box pattern land on master
247249
// let box (A | B | C);
248250

@@ -366,6 +368,17 @@ fn foo((1 | 2 | 3): u8) {}
366368
(integer_literal)))
367369
type: (primitive_type)))
368370
body: (block))
371+
(expression_statement
372+
(if_let_expression
373+
pattern: (or_pattern
374+
(macro_invocation
375+
macro: (identifier)
376+
(token_tree))
377+
(macro_invocation
378+
macro: (identifier)
379+
(token_tree)))
380+
value: (unit_expression)
381+
consequence: (block)))
369382
(line_comment)
370383
(line_comment)
371384
(line_comment)

grammar.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,10 +1216,7 @@ module.exports = grammar({
12161216

12171217
match_arm: $ => seq(
12181218
repeat($.attribute_item),
1219-
field('pattern', choice(
1220-
$.macro_invocation,
1221-
$.match_pattern
1222-
)),
1219+
field('pattern', $.match_pattern),
12231220
'=>',
12241221
choice(
12251222
seq(field('value', $._expression), ','),
@@ -1358,6 +1355,7 @@ module.exports = grammar({
13581355
$.range_pattern,
13591356
$.or_pattern,
13601357
$.const_block,
1358+
$.macro_invocation,
13611359
'_'
13621360
),
13631361

src/grammar.json

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@
768768
},
769769
{
770770
"type": "PATTERN",
771-
"value": "[/_\\-=->,;:::!=?.@*&#%^+<>|~]+"
771+
"value": "[\\/_\\-=->,;:::!=?.@*&#%^+<>|~]+"
772772
},
773773
{
774774
"type": "STRING",
@@ -7101,17 +7101,8 @@
71017101
"type": "FIELD",
71027102
"name": "pattern",
71037103
"content": {
7104-
"type": "CHOICE",
7105-
"members": [
7106-
{
7107-
"type": "SYMBOL",
7108-
"name": "macro_invocation"
7109-
},
7110-
{
7111-
"type": "SYMBOL",
7112-
"name": "match_pattern"
7113-
}
7114-
]
7104+
"type": "SYMBOL",
7105+
"name": "match_pattern"
71157106
}
71167107
},
71177108
{
@@ -7967,6 +7958,10 @@
79677958
"type": "SYMBOL",
79687959
"name": "const_block"
79697960
},
7961+
{
7962+
"type": "SYMBOL",
7963+
"name": "macro_invocation"
7964+
},
79707965
{
79717966
"type": "STRING",
79727967
"value": "_"

src/node-types.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@
343343
"type": "identifier",
344344
"named": true
345345
},
346+
{
347+
"type": "macro_invocation",
348+
"named": true
349+
},
346350
{
347351
"type": "mut_pattern",
348352
"named": true
@@ -2642,10 +2646,6 @@
26422646
"multiple": false,
26432647
"required": true,
26442648
"types": [
2645-
{
2646-
"type": "macro_invocation",
2647-
"named": true
2648-
},
26492649
{
26502650
"type": "match_pattern",
26512651
"named": true

0 commit comments

Comments
 (0)