@@ -31,10 +31,71 @@ module.exports = grammar({
31
31
$ . _declaration_statement ,
32
32
$ . _expression_statement ,
33
33
$ . _control_flow_statement ,
34
+ $ . expr_macro_rules ,
34
35
$ . macro_invocation ,
35
36
$ . empty_statement
36
37
) ,
37
38
39
+ expr_macro_rules : $ => {
40
+ const rules = seq (
41
+ repeat ( seq ( $ . macro_rule , ';' ) ) ,
42
+ optional ( $ . macro_rule )
43
+ )
44
+
45
+ return seq (
46
+ 'macro_rules!' ,
47
+ $ . identifier ,
48
+ choice (
49
+ seq ( '(' , rules , ')' , ';' ) ,
50
+ seq ( '{' , rules , '}' )
51
+ )
52
+ )
53
+ } ,
54
+
55
+ macro_rule : $ => seq (
56
+ $ . matcher ,
57
+ '=>' ,
58
+ $ . transcriber
59
+ ) ,
60
+
61
+ matcher : $ => choice (
62
+ seq ( '(' , repeat ( $ . _matcher ) , ')' ) ,
63
+ seq ( '[' , repeat ( $ . _matcher ) , ']' ) ,
64
+ seq ( '{' , repeat ( $ . _matcher ) , '}' )
65
+ ) ,
66
+
67
+ _matcher : $ => choice (
68
+ seq ( '(' , repeat ( $ . _matcher ) , ')' ) ,
69
+ seq ( '[' , repeat ( $ . _matcher ) , ']' ) ,
70
+ seq ( '{' , repeat ( $ . _matcher ) , '}' ) ,
71
+ seq ( $ . metavariable , ':' , $ . fragment_specifier ) ,
72
+ seq ( '$' , '(' , repeat ( $ . _matcher ) , ')' , optional ( / [ ^ + * ] + / ) , choice ( '+' , '*' ) ) ,
73
+ $ . non_special_token
74
+ ) ,
75
+
76
+ fragment_specifier : $ => choice (
77
+ 'ident' , 'path' , 'expr' , 'ty' , 'pat' , 'stmt' , 'block' , 'item' , 'meta' , 'tt'
78
+ ) ,
79
+
80
+ transcriber : $ => choice (
81
+ seq ( '(' , repeat ( $ . _transcriber ) , ')' ) ,
82
+ seq ( '[' , repeat ( $ . _transcriber ) , ']' ) ,
83
+ seq ( '{' , repeat ( $ . _transcriber ) , '}' )
84
+ ) ,
85
+
86
+ _transcriber : $ => prec . left ( - 1 , choice (
87
+ seq ( '(' , repeat ( $ . _transcriber ) , ')' ) ,
88
+ seq ( '[' , repeat ( $ . _transcriber ) , ']' ) ,
89
+ seq ( '{' , repeat ( $ . _transcriber ) , '}' ) ,
90
+ seq ( '$' , '(' , repeat ( $ . _transcriber ) , ')' , optional ( / [ ^ + * ] + / ) , choice ( '+' , '*' ) ) ,
91
+ $ . _statement ,
92
+ $ . _expression ,
93
+ seq ( $ . _expression , $ . non_special_token , $ . _expression ) ,
94
+ $ . non_special_token
95
+ ) ) ,
96
+
97
+ non_special_token : $ => / [ ^ \( \) \[ \] { } $ ] + / ,
98
+
38
99
macro_invocation : $ => prec . right ( seq (
39
100
$ . macro_name ,
40
101
$ . macro_arguments ,
@@ -81,7 +142,7 @@ module.exports = grammar({
81
142
$ . struct_item ,
82
143
$ . type_item ,
83
144
$ . function_item ,
84
- $ . impl_item ,
145
+ $ . impl_item
85
146
) ,
86
147
87
148
attribute_item : $ => seq (
@@ -313,10 +374,10 @@ module.exports = grammar({
313
374
314
375
mutable_specifier : $ => 'mut' ,
315
376
316
- _expression_statement : $ => seq (
377
+ _expression_statement : $ => prec . right ( seq (
317
378
$ . _expression ,
318
379
';'
319
- ) ,
380
+ ) ) ,
320
381
321
382
_expression : $ => choice (
322
383
$ . _no_struct_literal_expr ,
@@ -345,6 +406,7 @@ module.exports = grammar({
345
406
$ . break_expression ,
346
407
$ . continue_expression ,
347
408
$ . _index_expression ,
409
+ $ . metavariable ,
348
410
seq ( '(' , $ . _expression , ')' )
349
411
) ) ,
350
412
@@ -503,9 +565,9 @@ module.exports = grammar({
503
565
504
566
loop_label : $ => seq ( '\'' , $ . identifier ) ,
505
567
506
- break_expression : $ => seq ( 'break' , optional ( $ . loop_label ) ) ,
568
+ break_expression : $ => prec . left ( seq ( 'break' , optional ( $ . loop_label ) ) ) ,
507
569
508
- continue_expression : $ => seq ( 'continue' , optional ( $ . loop_label ) ) ,
570
+ continue_expression : $ => prec . left ( seq ( 'continue' , optional ( $ . loop_label ) ) ) ,
509
571
510
572
_index_expression : $ => seq ( $ . _expression , '[' , $ . _expression , ']' ) ,
511
573
@@ -636,6 +698,8 @@ module.exports = grammar({
636
698
637
699
super : $ => 'super' ,
638
700
701
+ metavariable : $ => / \$ [ a - z A - Z _ ] \w * / ,
702
+
639
703
empty_statement : $ => ';'
640
704
}
641
705
} )
0 commit comments