11
11
12
12
// https://doc.rust-lang.org/reference/expressions.html#expression-precedence
13
13
const PREC = {
14
- call : 15 ,
15
- field : 14 ,
16
- try : 13 ,
17
- unary : 12 ,
18
- cast : 11 ,
19
- multiplicative : 10 ,
20
- additive : 9 ,
21
- shift : 8 ,
22
- bitand : 7 ,
23
- bitxor : 6 ,
24
- bitor : 5 ,
25
- comparative : 4 ,
26
- and : 3 ,
27
- or : 2 ,
28
- range : 1 ,
14
+ call : 16 ,
15
+ field : 15 ,
16
+ try : 14 ,
17
+ unary : 13 ,
18
+ cast : 12 ,
19
+ multiplicative : 11 ,
20
+ additive : 10 ,
21
+ shift : 9 ,
22
+ bitand : 8 ,
23
+ bitxor : 7 ,
24
+ bitor : 6 ,
25
+ comparative : 5 ,
26
+ and : 4 ,
27
+ or : 3 ,
28
+ range : 2 ,
29
+ attribute : 1 ,
29
30
assign : 0 ,
30
31
closure : - 1 ,
31
32
} ;
@@ -95,8 +96,10 @@ module.exports = grammar({
95
96
$ . _field_identifier ,
96
97
$ . _non_special_token ,
97
98
$ . _declaration_statement ,
99
+ $ . _declaration_statement_without_attribute ,
98
100
$ . _reserved_identifier ,
99
101
$ . _expression_ending_with_block ,
102
+ $ . _expression_ending_with_block_without_attribute ,
100
103
] ,
101
104
102
105
conflicts : $ => [
@@ -132,12 +135,11 @@ module.exports = grammar({
132
135
prec ( 1 , $ . _expression_ending_with_block ) ,
133
136
) ,
134
137
135
- _declaration_statement : $ => choice (
138
+ _declaration_statement_without_attribute : $ => choice (
136
139
$ . const_item ,
137
140
$ . macro_invocation ,
138
141
$ . macro_definition ,
139
142
$ . empty_statement ,
140
- $ . attribute_item ,
141
143
$ . inner_attribute_item ,
142
144
$ . mod_item ,
143
145
$ . foreign_mod_item ,
@@ -156,6 +158,16 @@ module.exports = grammar({
156
158
$ . static_item ,
157
159
) ,
158
160
161
+ declaration_with_attribute : $ => seq (
162
+ field ( 'attributes' , $ . attributes ) ,
163
+ field ( 'declaration' , $ . _declaration_statement ) ,
164
+ ) ,
165
+
166
+ _declaration_statement : $ => choice (
167
+ $ . _declaration_statement_without_attribute ,
168
+ $ . declaration_with_attribute ,
169
+ ) ,
170
+
159
171
// Section - Macro definitions
160
172
161
173
macro_definition : $ => {
@@ -252,6 +264,10 @@ module.exports = grammar({
252
264
']' ,
253
265
) ,
254
266
267
+ attributes : $ => prec . left ( - 3 ,
268
+ repeat1 ( $ . attribute_item )
269
+ ) ,
270
+
255
271
inner_attribute_item : $ => seq (
256
272
'#' ,
257
273
'!' ,
@@ -332,12 +348,13 @@ module.exports = grammar({
332
348
333
349
enum_variant_list : $ => seq (
334
350
'{' ,
335
- sepBy ( ',' , seq ( repeat ( $ . attribute_item ) , $ . enum_variant ) ) ,
351
+ sepBy ( ',' , seq ( $ . enum_variant ) ) ,
336
352
optional ( ',' ) ,
337
353
'}' ,
338
354
) ,
339
355
340
356
enum_variant : $ => seq (
357
+ optional ( $ . attributes ) ,
341
358
optional ( $ . visibility_modifier ) ,
342
359
field ( 'name' , $ . identifier ) ,
343
360
field ( 'body' , optional ( choice (
@@ -352,7 +369,7 @@ module.exports = grammar({
352
369
353
370
field_declaration_list : $ => seq (
354
371
'{' ,
355
- sepBy ( ',' , seq ( repeat ( $ . attribute_item ) , $ . field_declaration ) ) ,
372
+ sepBy ( ',' , seq ( optional ( $ . attributes ) , $ . field_declaration ) ) ,
356
373
optional ( ',' ) ,
357
374
'}' ,
358
375
) ,
@@ -367,7 +384,7 @@ module.exports = grammar({
367
384
ordered_field_declaration_list : $ => seq (
368
385
'(' ,
369
386
sepBy ( ',' , seq (
370
- repeat ( $ . attribute_item ) ,
387
+ optional ( $ . attributes ) ,
371
388
optional ( $ . visibility_modifier ) ,
372
389
field ( 'type' , $ . _type ) ,
373
390
) ) ,
@@ -549,7 +566,7 @@ module.exports = grammar({
549
566
type_parameters : $ => prec ( 1 , seq (
550
567
'<' ,
551
568
sepBy1 ( ',' , seq (
552
- repeat ( $ . attribute_item ) ,
569
+ optional ( $ . attributes ) ,
553
570
choice (
554
571
$ . metavariable ,
555
572
$ . type_parameter ,
@@ -660,7 +677,7 @@ module.exports = grammar({
660
677
parameters : $ => seq (
661
678
'(' ,
662
679
sepBy ( ',' , seq (
663
- optional ( $ . attribute_item ) ,
680
+ optional ( $ . attributes ) ,
664
681
choice (
665
682
$ . parameter ,
666
683
$ . self_parameter ,
@@ -951,15 +968,27 @@ module.exports = grammar({
951
968
$ . closure_expression ,
952
969
$ . parenthesized_expression ,
953
970
$ . struct_expression ,
954
- $ . _expression_ending_with_block ,
971
+ $ . _expression_ending_with_block_without_attribute ,
955
972
) ,
956
973
957
974
_expression : $ => choice (
975
+ $ . _expression_without_attribute ,
976
+ $ . expression_with_attribute ,
977
+ ) ,
978
+
979
+ expression_with_attribute : $ => prec ( PREC . attribute ,
980
+ seq (
981
+ field ( 'attributes' , $ . attributes ) ,
982
+ field ( 'expression' , $ . _expression_without_attribute ) ,
983
+ )
984
+ ) ,
985
+
986
+ _expression_without_attribute : $ => choice (
958
987
$ . _expression_except_range ,
959
988
$ . range_expression ,
960
989
) ,
961
990
962
- _expression_ending_with_block : $ => choice (
991
+ _expression_ending_with_block_without_attribute : $ => choice (
963
992
$ . unsafe_block ,
964
993
$ . async_block ,
965
994
$ . gen_block ,
@@ -973,6 +1002,18 @@ module.exports = grammar({
973
1002
$ . const_block ,
974
1003
) ,
975
1004
1005
+ block_expression_with_attribute : $ => prec ( PREC . attribute ,
1006
+ seq (
1007
+ field ( 'attributes' , $ . attributes ) ,
1008
+ field ( 'expression' , $ . _expression_ending_with_block_without_attribute ) ,
1009
+ )
1010
+ ) ,
1011
+
1012
+ _expression_ending_with_block : $ => choice (
1013
+ $ . _expression_ending_with_block_without_attribute ,
1014
+ $ . block_expression_with_attribute ,
1015
+ ) ,
1016
+
976
1017
macro_invocation : $ => seq (
977
1018
field ( 'macro' , choice (
978
1019
$ . scoped_identifier ,
@@ -1113,22 +1154,22 @@ module.exports = grammar({
1113
1154
1114
1155
arguments : $ => seq (
1115
1156
'(' ,
1116
- sepBy ( ',' , seq ( repeat ( $ . attribute_item ) , $ . _expression ) ) ,
1157
+ sepBy ( ',' , $ . _expression ) ,
1117
1158
optional ( ',' ) ,
1118
1159
')' ,
1119
1160
) ,
1120
1161
1121
1162
array_expression : $ => seq (
1122
1163
'[' ,
1123
- repeat ( $ . attribute_item ) ,
1164
+ optional ( $ . attributes ) ,
1124
1165
choice (
1125
1166
seq (
1126
1167
$ . _expression ,
1127
1168
';' ,
1128
1169
field ( 'length' , $ . _expression ) ,
1129
1170
) ,
1130
1171
seq (
1131
- sepBy ( ',' , seq ( repeat ( $ . attribute_item ) , $ . _expression ) ) ,
1172
+ sepBy ( ',' , seq ( optional ( $ . attributes ) , $ . _expression ) ) ,
1132
1173
optional ( ',' ) ,
1133
1174
) ,
1134
1175
) ,
@@ -1143,7 +1184,7 @@ module.exports = grammar({
1143
1184
1144
1185
tuple_expression : $ => seq (
1145
1186
'(' ,
1146
- repeat ( $ . attribute_item ) ,
1187
+ optional ( $ . attributes ) ,
1147
1188
seq ( $ . _expression , ',' ) ,
1148
1189
repeat ( seq ( $ . _expression , ',' ) ) ,
1149
1190
optional ( $ . _expression ) ,
@@ -1173,12 +1214,12 @@ module.exports = grammar({
1173
1214
) ,
1174
1215
1175
1216
shorthand_field_initializer : $ => seq (
1176
- repeat ( $ . attribute_item ) ,
1217
+ optional ( $ . attributes ) ,
1177
1218
$ . identifier ,
1178
1219
) ,
1179
1220
1180
1221
field_initializer : $ => seq (
1181
- repeat ( $ . attribute_item ) ,
1222
+ optional ( $ . attributes ) ,
1182
1223
field ( 'field' , choice ( $ . _field_identifier , $ . integer_literal ) ) ,
1183
1224
':' ,
1184
1225
field ( 'value' , $ . _expression ) ,
@@ -1233,6 +1274,7 @@ module.exports = grammar({
1233
1274
1234
1275
match_block : $ => seq (
1235
1276
'{' ,
1277
+ repeat ( $ . inner_attribute_item ) ,
1236
1278
optional ( seq (
1237
1279
repeat ( $ . match_arm ) ,
1238
1280
alias ( $ . last_match_arm , $ . match_arm ) ,
@@ -1241,7 +1283,7 @@ module.exports = grammar({
1241
1283
) ,
1242
1284
1243
1285
match_arm : $ => prec . right ( seq (
1244
- repeat ( choice ( $ . attribute_item , $ . inner_attribute_item ) ) ,
1286
+ optional ( $ . attributes ) ,
1245
1287
field ( 'pattern' , $ . match_pattern ) ,
1246
1288
'=>' ,
1247
1289
choice (
@@ -1251,7 +1293,7 @@ module.exports = grammar({
1251
1293
) ) ,
1252
1294
1253
1295
last_match_arm : $ => seq (
1254
- repeat ( choice ( $ . attribute_item , $ . inner_attribute_item ) ) ,
1296
+ optional ( $ . attributes ) ,
1255
1297
field ( 'pattern' , $ . match_pattern ) ,
1256
1298
'=>' ,
1257
1299
field ( 'value' , $ . _expression ) ,
@@ -1358,13 +1400,13 @@ module.exports = grammar({
1358
1400
$ . block ,
1359
1401
) ,
1360
1402
1361
- block : $ => seq (
1403
+ block : $ => prec . right ( 3 , seq (
1362
1404
optional ( seq ( $ . label , ':' ) ) ,
1363
1405
'{' ,
1364
1406
repeat ( $ . _statement ) ,
1365
1407
optional ( $ . _expression ) ,
1366
1408
'}' ,
1367
- ) ,
1409
+ ) ) ,
1368
1410
1369
1411
// Section - Patterns
1370
1412
0 commit comments