Skip to content

Commit 7598cc5

Browse files
committed
Attach outer attributes to the elements they annotate
Fixes tree-sitter#244.
1 parent 32c17ce commit 7598cc5

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
lines changed

grammar.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ module.exports = grammar({
138138
$.macro_invocation,
139139
$.macro_definition,
140140
$.empty_statement,
141-
$.attribute_item,
142141
$.inner_attribute_item,
143142
$.mod_item,
144143
$.foreign_mod_item,
@@ -270,6 +269,7 @@ module.exports = grammar({
270269
),
271270

272271
mod_item: $ => seq(
272+
repeat($.attribute_item),
273273
optional($.visibility_modifier),
274274
'mod',
275275
field('name', $.identifier),
@@ -280,6 +280,7 @@ module.exports = grammar({
280280
),
281281

282282
foreign_mod_item: $ => seq(
283+
repeat($.attribute_item),
283284
optional($.visibility_modifier),
284285
$.extern_modifier,
285286
choice(
@@ -295,6 +296,7 @@ module.exports = grammar({
295296
),
296297

297298
struct_item: $ => seq(
299+
repeat($.attribute_item),
298300
optional($.visibility_modifier),
299301
'struct',
300302
field('name', $._type_identifier),
@@ -314,6 +316,7 @@ module.exports = grammar({
314316
),
315317

316318
union_item: $ => seq(
319+
repeat($.attribute_item),
317320
optional($.visibility_modifier),
318321
'union',
319322
field('name', $._type_identifier),
@@ -323,6 +326,7 @@ module.exports = grammar({
323326
),
324327

325328
enum_item: $ => seq(
329+
repeat($.attribute_item),
326330
optional($.visibility_modifier),
327331
'enum',
328332
field('name', $._type_identifier),
@@ -377,6 +381,7 @@ module.exports = grammar({
377381
),
378382

379383
extern_crate_declaration: $ => seq(
384+
repeat($.attribute_item),
380385
optional($.visibility_modifier),
381386
'extern',
382387
$.crate,
@@ -389,6 +394,7 @@ module.exports = grammar({
389394
),
390395

391396
const_item: $ => seq(
397+
repeat($.attribute_item),
392398
optional($.visibility_modifier),
393399
'const',
394400
field('name', $.identifier),
@@ -404,6 +410,7 @@ module.exports = grammar({
404410
),
405411

406412
static_item: $ => seq(
413+
repeat($.attribute_item),
407414
optional($.visibility_modifier),
408415
'static',
409416

@@ -422,6 +429,7 @@ module.exports = grammar({
422429
),
423430

424431
type_item: $ => seq(
432+
repeat($.attribute_item),
425433
optional($.visibility_modifier),
426434
'type',
427435
field('name', $._type_identifier),
@@ -433,6 +441,7 @@ module.exports = grammar({
433441
),
434442

435443
function_item: $ => seq(
444+
repeat($.attribute_item),
436445
optional($.visibility_modifier),
437446
optional($.function_modifiers),
438447
'fn',
@@ -445,6 +454,7 @@ module.exports = grammar({
445454
),
446455

447456
function_signature_item: $ => seq(
457+
repeat($.attribute_item),
448458
optional($.visibility_modifier),
449459
optional($.function_modifiers),
450460
'fn',
@@ -487,6 +497,7 @@ module.exports = grammar({
487497
),
488498

489499
impl_item: $ => seq(
500+
repeat($.attribute_item),
490501
optional('unsafe'),
491502
'impl',
492503
field('type_parameters', optional($.type_parameters)),
@@ -505,6 +516,7 @@ module.exports = grammar({
505516
),
506517

507518
trait_item: $ => seq(
519+
repeat($.attribute_item),
508520
optional($.visibility_modifier),
509521
optional('unsafe'),
510522
'trait',
@@ -516,6 +528,7 @@ module.exports = grammar({
516528
),
517529

518530
associated_type: $ => seq(
531+
repeat($.attribute_item),
519532
'type',
520533
field('name', $._type_identifier),
521534
field('type_parameters', optional($.type_parameters)),
@@ -583,6 +596,7 @@ module.exports = grammar({
583596
),
584597

585598
let_declaration: $ => seq(
599+
repeat($.attribute_item),
586600
'let',
587601
optional($.mutable_specifier),
588602
field('pattern', $._pattern),
@@ -602,6 +616,7 @@ module.exports = grammar({
602616
),
603617

604618
use_declaration: $ => seq(
619+
repeat($.attribute_item),
605620
optional($.visibility_modifier),
606621
'use',
607622
field('argument', $._use_clause),

test/corpus/declarations.txt

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,41 +1110,41 @@ mod macos_only {}
11101110

11111111
#![allow(clippy::useless_transmute)]
11121112

1113-
#[clippy::cyclomatic_complexity = "100"]
1113+
#![clippy::cyclomatic_complexity = "100"]
11141114

11151115
--------------------------------------------------------------------------------
11161116

11171117
(source_file
1118-
(attribute_item
1119-
(attribute
1120-
(identifier)))
11211118
(function_item
1119+
(attribute_item
1120+
(attribute
1121+
(identifier)))
11221122
name: (identifier)
11231123
parameters: (parameters)
11241124
body: (block))
1125-
(attribute_item
1126-
(attribute
1127-
(identifier)
1128-
arguments: (token_tree
1129-
(identifier))))
11301125
(struct_item
1131-
name: (type_identifier))
1132-
(attribute_item
1133-
(attribute
1134-
(identifier)
1135-
arguments: (token_tree
1126+
(attribute_item
1127+
(attribute
11361128
(identifier)
1137-
(identifier))))
1138-
(struct_item
1129+
arguments: (token_tree
1130+
(identifier))))
11391131
name: (type_identifier))
1140-
(attribute_item
1141-
(attribute
1142-
(identifier)
1143-
arguments: (token_tree
1132+
(struct_item
1133+
(attribute_item
1134+
(attribute
11441135
(identifier)
1145-
(string_literal
1146-
(string_content)))))
1136+
arguments: (token_tree
1137+
(identifier)
1138+
(identifier))))
1139+
name: (type_identifier))
11471140
(mod_item
1141+
(attribute_item
1142+
(attribute
1143+
(identifier)
1144+
arguments: (token_tree
1145+
(identifier)
1146+
(string_literal
1147+
(string_content)))))
11481148
name: (identifier)
11491149
body: (declaration_list))
11501150
(inner_attribute_item
@@ -1153,7 +1153,7 @@ mod macos_only {}
11531153
arguments: (token_tree
11541154
(identifier)
11551155
(identifier))))
1156-
(attribute_item
1156+
(inner_attribute_item
11571157
(attribute
11581158
(scoped_identifier
11591159
path: (identifier)
@@ -1234,25 +1234,25 @@ fn baz() {}
12341234
--------------------------------------------------------------------------------
12351235

12361236
(source_file
1237-
(attribute_item
1238-
(attribute
1239-
(identifier)
1240-
(macro_invocation
1241-
(identifier)
1242-
(token_tree
1243-
(string_literal
1244-
(string_content))))))
12451237
(function_item
1238+
(attribute_item
1239+
(attribute
1240+
(identifier)
1241+
(macro_invocation
1242+
(identifier)
1243+
(token_tree
1244+
(string_literal
1245+
(string_content))))))
12461246
(identifier)
12471247
(parameters)
12481248
(block))
1249-
(attribute_item
1250-
(attribute
1251-
(identifier)
1252-
(scoped_identifier
1253-
(identifier)
1254-
(identifier))))
12551249
(function_item
1250+
(attribute_item
1251+
(attribute
1252+
(identifier)
1253+
(scoped_identifier
1254+
(identifier)
1255+
(identifier))))
12561256
(identifier)
12571257
(parameters)
12581258
(block)))
@@ -1323,13 +1323,13 @@ pub enum Error {
13231323
(scoped_identifier
13241324
(identifier)
13251325
(identifier)))
1326-
(attribute_item
1327-
(attribute
1328-
(identifier)
1329-
(token_tree
1330-
(identifier)
1331-
(identifier))))
13321326
(enum_item
1327+
(attribute_item
1328+
(attribute
1329+
(identifier)
1330+
(token_tree
1331+
(identifier)
1332+
(identifier))))
13331333
(visibility_modifier)
13341334
(type_identifier)
13351335
(enum_variant_list

0 commit comments

Comments
 (0)