Skip to content

Commit d537982

Browse files
committed
Attach outer attributes to the elements they annotate
Closes tree-sitter#244.
1 parent 377ca96 commit d537982

File tree

1 file changed

+76
-34
lines changed

1 file changed

+76
-34
lines changed

grammar.js

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111

1212
// https://doc.rust-lang.org/reference/expressions.html#expression-precedence
1313
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,
2930
assign: 0,
3031
closure: -1,
3132
};
@@ -95,8 +96,10 @@ module.exports = grammar({
9596
$._field_identifier,
9697
$._non_special_token,
9798
$._declaration_statement,
99+
$._declaration_statement_without_attribute,
98100
$._reserved_identifier,
99101
$._expression_ending_with_block,
102+
$._expression_ending_with_block_without_attribute,
100103
],
101104

102105
conflicts: $ => [
@@ -132,12 +135,11 @@ module.exports = grammar({
132135
prec(1, $._expression_ending_with_block),
133136
),
134137

135-
_declaration_statement: $ => choice(
138+
_declaration_statement_without_attribute: $ => choice(
136139
$.const_item,
137140
$.macro_invocation,
138141
$.macro_definition,
139142
$.empty_statement,
140-
$.attribute_item,
141143
$.inner_attribute_item,
142144
$.mod_item,
143145
$.foreign_mod_item,
@@ -156,6 +158,16 @@ module.exports = grammar({
156158
$.static_item,
157159
),
158160

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+
159171
// Section - Macro definitions
160172

161173
macro_definition: $ => {
@@ -252,6 +264,10 @@ module.exports = grammar({
252264
']',
253265
),
254266

267+
attributes: $ => prec.left(-3,
268+
repeat1($.attribute_item)
269+
),
270+
255271
inner_attribute_item: $ => seq(
256272
'#',
257273
'!',
@@ -332,12 +348,13 @@ module.exports = grammar({
332348

333349
enum_variant_list: $ => seq(
334350
'{',
335-
sepBy(',', seq(repeat($.attribute_item), $.enum_variant)),
351+
sepBy(',', seq($.enum_variant)),
336352
optional(','),
337353
'}',
338354
),
339355

340356
enum_variant: $ => seq(
357+
optional($.attributes),
341358
optional($.visibility_modifier),
342359
field('name', $.identifier),
343360
field('body', optional(choice(
@@ -352,7 +369,7 @@ module.exports = grammar({
352369

353370
field_declaration_list: $ => seq(
354371
'{',
355-
sepBy(',', seq(repeat($.attribute_item), $.field_declaration)),
372+
sepBy(',', seq(optional($.attributes), $.field_declaration)),
356373
optional(','),
357374
'}',
358375
),
@@ -367,7 +384,7 @@ module.exports = grammar({
367384
ordered_field_declaration_list: $ => seq(
368385
'(',
369386
sepBy(',', seq(
370-
repeat($.attribute_item),
387+
optional($.attributes),
371388
optional($.visibility_modifier),
372389
field('type', $._type),
373390
)),
@@ -549,7 +566,7 @@ module.exports = grammar({
549566
type_parameters: $ => prec(1, seq(
550567
'<',
551568
sepBy1(',', seq(
552-
repeat($.attribute_item),
569+
optional($.attributes),
553570
choice(
554571
$.metavariable,
555572
$.type_parameter,
@@ -660,7 +677,7 @@ module.exports = grammar({
660677
parameters: $ => seq(
661678
'(',
662679
sepBy(',', seq(
663-
optional($.attribute_item),
680+
optional($.attributes),
664681
choice(
665682
$.parameter,
666683
$.self_parameter,
@@ -951,15 +968,27 @@ module.exports = grammar({
951968
$.closure_expression,
952969
$.parenthesized_expression,
953970
$.struct_expression,
954-
$._expression_ending_with_block,
971+
$._expression_ending_with_block_without_attribute,
955972
),
956973

957974
_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(
958987
$._expression_except_range,
959988
$.range_expression,
960989
),
961990

962-
_expression_ending_with_block: $ => choice(
991+
_expression_ending_with_block_without_attribute: $ => choice(
963992
$.unsafe_block,
964993
$.async_block,
965994
$.gen_block,
@@ -973,6 +1002,18 @@ module.exports = grammar({
9731002
$.const_block,
9741003
),
9751004

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+
9761017
macro_invocation: $ => seq(
9771018
field('macro', choice(
9781019
$.scoped_identifier,
@@ -1113,22 +1154,22 @@ module.exports = grammar({
11131154

11141155
arguments: $ => seq(
11151156
'(',
1116-
sepBy(',', seq(repeat($.attribute_item), $._expression)),
1157+
sepBy(',', $._expression),
11171158
optional(','),
11181159
')',
11191160
),
11201161

11211162
array_expression: $ => seq(
11221163
'[',
1123-
repeat($.attribute_item),
1164+
optional($.attributes),
11241165
choice(
11251166
seq(
11261167
$._expression,
11271168
';',
11281169
field('length', $._expression),
11291170
),
11301171
seq(
1131-
sepBy(',', seq(repeat($.attribute_item), $._expression)),
1172+
sepBy(',', seq(optional($.attributes), $._expression)),
11321173
optional(','),
11331174
),
11341175
),
@@ -1143,7 +1184,7 @@ module.exports = grammar({
11431184

11441185
tuple_expression: $ => seq(
11451186
'(',
1146-
repeat($.attribute_item),
1187+
optional($.attributes),
11471188
seq($._expression, ','),
11481189
repeat(seq($._expression, ',')),
11491190
optional($._expression),
@@ -1173,12 +1214,12 @@ module.exports = grammar({
11731214
),
11741215

11751216
shorthand_field_initializer: $ => seq(
1176-
repeat($.attribute_item),
1217+
optional($.attributes),
11771218
$.identifier,
11781219
),
11791220

11801221
field_initializer: $ => seq(
1181-
repeat($.attribute_item),
1222+
optional($.attributes),
11821223
field('field', choice($._field_identifier, $.integer_literal)),
11831224
':',
11841225
field('value', $._expression),
@@ -1233,6 +1274,7 @@ module.exports = grammar({
12331274

12341275
match_block: $ => seq(
12351276
'{',
1277+
repeat($.inner_attribute_item),
12361278
optional(seq(
12371279
repeat($.match_arm),
12381280
alias($.last_match_arm, $.match_arm),
@@ -1241,7 +1283,7 @@ module.exports = grammar({
12411283
),
12421284

12431285
match_arm: $ => prec.right(seq(
1244-
repeat(choice($.attribute_item, $.inner_attribute_item)),
1286+
optional($.attributes),
12451287
field('pattern', $.match_pattern),
12461288
'=>',
12471289
choice(
@@ -1251,7 +1293,7 @@ module.exports = grammar({
12511293
)),
12521294

12531295
last_match_arm: $ => seq(
1254-
repeat(choice($.attribute_item, $.inner_attribute_item)),
1296+
optional($.attributes),
12551297
field('pattern', $.match_pattern),
12561298
'=>',
12571299
field('value', $._expression),
@@ -1358,13 +1400,13 @@ module.exports = grammar({
13581400
$.block,
13591401
),
13601402

1361-
block: $ => seq(
1403+
block: $ => prec.right(3, seq(
13621404
optional(seq($.label, ':')),
13631405
'{',
13641406
repeat($._statement),
13651407
optional($._expression),
13661408
'}',
1367-
),
1409+
)),
13681410

13691411
// Section - Patterns
13701412

0 commit comments

Comments
 (0)