Skip to content

Commit 9cede24

Browse files
authored
Merge pull request #96 from ninevra/macro-invocation-dollar-sign
Parse macro invocations containing $
2 parents 3bc76de + 0da6724 commit 9cede24

File tree

4 files changed

+54298
-52599
lines changed

4 files changed

+54298
-52599
lines changed

corpus/macros.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ a!('\u{0}'..='\u{2}');
5151
a!('lifetime)
5252
default!(a);
5353
union!(a);
54+
a!($);
55+
a!($());
56+
a!($ a $);
57+
a!(${$([ a ])});
58+
a!($a $a:ident $($a);*);
5459

5560
--------------------------------------------------------------------------------
5661

@@ -101,7 +106,32 @@ union!(a);
101106
(macro_invocation
102107
(identifier)
103108
(token_tree
104-
(identifier)))))
109+
(identifier))))
110+
(expression_statement
111+
(macro_invocation
112+
(identifier)
113+
(token_tree)))
114+
(expression_statement
115+
(macro_invocation
116+
(identifier)
117+
(token_tree (token_tree))))
118+
(expression_statement
119+
(macro_invocation
120+
(identifier)
121+
(token_tree (identifier))))
122+
(expression_statement
123+
(macro_invocation
124+
(identifier)
125+
(token_tree (token_tree (token_tree (token_tree (identifier)))))))
126+
(expression_statement
127+
(macro_invocation
128+
(identifier)
129+
(token_tree
130+
(identifier)
131+
(identifier)
132+
(identifier)
133+
(token_tree
134+
(identifier))))))
105135

106136
================================================================================
107137
Macro invocation with comments

grammar.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ module.exports = grammar({
150150
$.token_tree_pattern,
151151
$.token_repetition_pattern,
152152
$.token_binding_pattern,
153+
$.metavariable,
153154
$._non_special_token
154155
),
155156

@@ -177,6 +178,7 @@ module.exports = grammar({
177178
_tokens: $ => choice(
178179
$.token_tree,
179180
$.token_repetition,
181+
$.metavariable,
180182
$._non_special_token
181183
),
182184

@@ -190,8 +192,11 @@ module.exports = grammar({
190192
'$', '(', repeat($._tokens), ')', optional(/[^+*?]+/), choice('+', '*', '?')
191193
),
192194

195+
// Matches non-delimiter tokens common to both macro invocations and
196+
// definitions. This is everything except $ and metavariables (which begin
197+
// with $).
193198
_non_special_token: $ => choice(
194-
$._literal, $.identifier, $.metavariable, $.mutable_specifier, $.self, $.super, $.crate,
199+
$._literal, $.identifier, $.mutable_specifier, $.self, $.super, $.crate,
195200
alias(choice(...primitive_types), $.primitive_type),
196201
/[/_\-=->,;:::!=?.@*&#%^+<>|~]+/,
197202
'\'',
@@ -891,7 +896,24 @@ module.exports = grammar({
891896
$._reserved_identifier,
892897
)),
893898
'!',
894-
$.token_tree
899+
alias($.delim_token_tree, $.token_tree)
900+
),
901+
902+
delim_token_tree: $ => choice(
903+
seq('(', repeat($._delim_tokens), ')'),
904+
seq('[', repeat($._delim_tokens), ']'),
905+
seq('{', repeat($._delim_tokens), '}')
906+
),
907+
908+
_delim_tokens: $ => choice(
909+
$._non_delim_token,
910+
alias($.delim_token_tree, $.token_tree),
911+
),
912+
913+
// Should match any token other than a delimiter.
914+
_non_delim_token: $ => choice(
915+
$._non_special_token,
916+
'$'
895917
),
896918

897919
scoped_identifier: $ => seq(

src/grammar.json

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@
308308
"type": "SYMBOL",
309309
"name": "token_binding_pattern"
310310
},
311+
{
312+
"type": "SYMBOL",
313+
"name": "metavariable"
314+
},
311315
{
312316
"type": "SYMBOL",
313317
"name": "_non_special_token"
@@ -529,6 +533,10 @@
529533
"type": "SYMBOL",
530534
"name": "token_repetition"
531535
},
536+
{
537+
"type": "SYMBOL",
538+
"name": "metavariable"
539+
},
532540
{
533541
"type": "SYMBOL",
534542
"name": "_non_special_token"
@@ -664,10 +672,6 @@
664672
"type": "SYMBOL",
665673
"name": "identifier"
666674
},
667-
{
668-
"type": "SYMBOL",
669-
"name": "metavariable"
670-
},
671675
{
672676
"type": "SYMBOL",
673677
"name": "mutable_specifier"
@@ -4927,9 +4931,110 @@
49274931
"type": "STRING",
49284932
"value": "!"
49294933
},
4934+
{
4935+
"type": "ALIAS",
4936+
"content": {
4937+
"type": "SYMBOL",
4938+
"name": "delim_token_tree"
4939+
},
4940+
"named": true,
4941+
"value": "token_tree"
4942+
}
4943+
]
4944+
},
4945+
"delim_token_tree": {
4946+
"type": "CHOICE",
4947+
"members": [
4948+
{
4949+
"type": "SEQ",
4950+
"members": [
4951+
{
4952+
"type": "STRING",
4953+
"value": "("
4954+
},
4955+
{
4956+
"type": "REPEAT",
4957+
"content": {
4958+
"type": "SYMBOL",
4959+
"name": "_delim_tokens"
4960+
}
4961+
},
4962+
{
4963+
"type": "STRING",
4964+
"value": ")"
4965+
}
4966+
]
4967+
},
4968+
{
4969+
"type": "SEQ",
4970+
"members": [
4971+
{
4972+
"type": "STRING",
4973+
"value": "["
4974+
},
4975+
{
4976+
"type": "REPEAT",
4977+
"content": {
4978+
"type": "SYMBOL",
4979+
"name": "_delim_tokens"
4980+
}
4981+
},
4982+
{
4983+
"type": "STRING",
4984+
"value": "]"
4985+
}
4986+
]
4987+
},
4988+
{
4989+
"type": "SEQ",
4990+
"members": [
4991+
{
4992+
"type": "STRING",
4993+
"value": "{"
4994+
},
4995+
{
4996+
"type": "REPEAT",
4997+
"content": {
4998+
"type": "SYMBOL",
4999+
"name": "_delim_tokens"
5000+
}
5001+
},
5002+
{
5003+
"type": "STRING",
5004+
"value": "}"
5005+
}
5006+
]
5007+
}
5008+
]
5009+
},
5010+
"_delim_tokens": {
5011+
"type": "CHOICE",
5012+
"members": [
5013+
{
5014+
"type": "SYMBOL",
5015+
"name": "_non_delim_token"
5016+
},
5017+
{
5018+
"type": "ALIAS",
5019+
"content": {
5020+
"type": "SYMBOL",
5021+
"name": "delim_token_tree"
5022+
},
5023+
"named": true,
5024+
"value": "token_tree"
5025+
}
5026+
]
5027+
},
5028+
"_non_delim_token": {
5029+
"type": "CHOICE",
5030+
"members": [
49305031
{
49315032
"type": "SYMBOL",
4932-
"name": "token_tree"
5033+
"name": "_non_special_token"
5034+
},
5035+
{
5036+
"type": "STRING",
5037+
"value": "$"
49335038
}
49345039
]
49355040
},

0 commit comments

Comments
 (0)