@@ -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 (
0 commit comments