Skip to content

Commit 25c50cb

Browse files
committed
Attach outer attributes to the elements they annotate
Fixes tree-sitter#244.
1 parent e86119b commit 25c50cb

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
@@ -137,7 +137,6 @@ module.exports = grammar({
137137
$.macro_invocation,
138138
$.macro_definition,
139139
$.empty_statement,
140-
$.attribute_item,
141140
$.inner_attribute_item,
142141
$.mod_item,
143142
$.foreign_mod_item,
@@ -269,6 +268,7 @@ module.exports = grammar({
269268
),
270269

271270
mod_item: $ => seq(
271+
repeat($.attribute_item),
272272
optional($.visibility_modifier),
273273
'mod',
274274
field('name', $.identifier),
@@ -279,6 +279,7 @@ module.exports = grammar({
279279
),
280280

281281
foreign_mod_item: $ => seq(
282+
repeat($.attribute_item),
282283
optional($.visibility_modifier),
283284
$.extern_modifier,
284285
choice(
@@ -294,6 +295,7 @@ module.exports = grammar({
294295
),
295296

296297
struct_item: $ => seq(
298+
repeat($.attribute_item),
297299
optional($.visibility_modifier),
298300
'struct',
299301
field('name', $._type_identifier),
@@ -313,6 +315,7 @@ module.exports = grammar({
313315
),
314316

315317
union_item: $ => seq(
318+
repeat($.attribute_item),
316319
optional($.visibility_modifier),
317320
'union',
318321
field('name', $._type_identifier),
@@ -322,6 +325,7 @@ module.exports = grammar({
322325
),
323326

324327
enum_item: $ => seq(
328+
repeat($.attribute_item),
325329
optional($.visibility_modifier),
326330
'enum',
327331
field('name', $._type_identifier),
@@ -376,6 +380,7 @@ module.exports = grammar({
376380
),
377381

378382
extern_crate_declaration: $ => seq(
383+
repeat($.attribute_item),
379384
optional($.visibility_modifier),
380385
'extern',
381386
$.crate,
@@ -388,6 +393,7 @@ module.exports = grammar({
388393
),
389394

390395
const_item: $ => seq(
396+
repeat($.attribute_item),
391397
optional($.visibility_modifier),
392398
'const',
393399
field('name', $.identifier),
@@ -403,6 +409,7 @@ module.exports = grammar({
403409
),
404410

405411
static_item: $ => seq(
412+
repeat($.attribute_item),
406413
optional($.visibility_modifier),
407414
'static',
408415

@@ -421,6 +428,7 @@ module.exports = grammar({
421428
),
422429

423430
type_item: $ => seq(
431+
repeat($.attribute_item),
424432
optional($.visibility_modifier),
425433
'type',
426434
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',
@@ -489,6 +499,7 @@ module.exports = grammar({
489499
),
490500

491501
impl_item: $ => seq(
502+
repeat($.attribute_item),
492503
optional('unsafe'),
493504
'impl',
494505
field('type_parameters', optional($.type_parameters)),
@@ -507,6 +518,7 @@ module.exports = grammar({
507518
),
508519

509520
trait_item: $ => seq(
521+
repeat($.attribute_item),
510522
optional($.visibility_modifier),
511523
optional('unsafe'),
512524
'trait',
@@ -518,6 +530,7 @@ module.exports = grammar({
518530
),
519531

520532
associated_type: $ => seq(
533+
repeat($.attribute_item),
521534
'type',
522535
field('name', $._type_identifier),
523536
field('type_parameters', optional($.type_parameters)),
@@ -598,6 +611,7 @@ module.exports = grammar({
598611
)),
599612

600613
let_declaration: $ => seq(
614+
repeat($.attribute_item),
601615
'let',
602616
optional($.mutable_specifier),
603617
field('pattern', $._pattern),
@@ -617,6 +631,7 @@ module.exports = grammar({
617631
),
618632

619633
use_declaration: $ => seq(
634+
repeat($.attribute_item),
620635
optional($.visibility_modifier),
621636
'use',
622637
field('argument', $._use_clause),

test/corpus/declarations.txt

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

12351235
#![allow(clippy::useless_transmute)]
12361236

1237-
#[clippy::cyclomatic_complexity = "100"]
1237+
#![clippy::cyclomatic_complexity = "100"]
12381238

12391239
--------------------------------------------------------------------------------
12401240

12411241
(source_file
1242-
(attribute_item
1243-
(attribute
1244-
(identifier)))
12451242
(function_item
1243+
(attribute_item
1244+
(attribute
1245+
(identifier)))
12461246
name: (identifier)
12471247
parameters: (parameters)
12481248
body: (block))
1249-
(attribute_item
1250-
(attribute
1251-
(identifier)
1252-
arguments: (token_tree
1253-
(identifier))))
12541249
(struct_item
1255-
name: (type_identifier))
1256-
(attribute_item
1257-
(attribute
1258-
(identifier)
1259-
arguments: (token_tree
1250+
(attribute_item
1251+
(attribute
12601252
(identifier)
1261-
(identifier))))
1262-
(struct_item
1253+
arguments: (token_tree
1254+
(identifier))))
12631255
name: (type_identifier))
1264-
(attribute_item
1265-
(attribute
1266-
(identifier)
1267-
arguments: (token_tree
1256+
(struct_item
1257+
(attribute_item
1258+
(attribute
12681259
(identifier)
1269-
(string_literal
1270-
(string_content)))))
1260+
arguments: (token_tree
1261+
(identifier)
1262+
(identifier))))
1263+
name: (type_identifier))
12711264
(mod_item
1265+
(attribute_item
1266+
(attribute
1267+
(identifier)
1268+
arguments: (token_tree
1269+
(identifier)
1270+
(string_literal
1271+
(string_content)))))
12721272
name: (identifier)
12731273
body: (declaration_list))
12741274
(inner_attribute_item
@@ -1277,7 +1277,7 @@ mod macos_only {}
12771277
arguments: (token_tree
12781278
(identifier)
12791279
(identifier))))
1280-
(attribute_item
1280+
(inner_attribute_item
12811281
(attribute
12821282
(scoped_identifier
12831283
path: (identifier)
@@ -1358,25 +1358,25 @@ fn baz() {}
13581358
--------------------------------------------------------------------------------
13591359

13601360
(source_file
1361-
(attribute_item
1362-
(attribute
1363-
(identifier)
1364-
(macro_invocation
1365-
(identifier)
1366-
(token_tree
1367-
(string_literal
1368-
(string_content))))))
13691361
(function_item
1362+
(attribute_item
1363+
(attribute
1364+
(identifier)
1365+
(macro_invocation
1366+
(identifier)
1367+
(token_tree
1368+
(string_literal
1369+
(string_content))))))
13701370
(identifier)
13711371
(parameters)
13721372
(block))
1373-
(attribute_item
1374-
(attribute
1375-
(identifier)
1376-
(scoped_identifier
1377-
(identifier)
1378-
(identifier))))
13791373
(function_item
1374+
(attribute_item
1375+
(attribute
1376+
(identifier)
1377+
(scoped_identifier
1378+
(identifier)
1379+
(identifier))))
13801380
(identifier)
13811381
(parameters)
13821382
(block)))
@@ -1447,13 +1447,13 @@ pub enum Error {
14471447
(scoped_identifier
14481448
(identifier)
14491449
(identifier)))
1450-
(attribute_item
1451-
(attribute
1452-
(identifier)
1453-
(token_tree
1454-
(identifier)
1455-
(identifier))))
14561450
(enum_item
1451+
(attribute_item
1452+
(attribute
1453+
(identifier)
1454+
(token_tree
1455+
(identifier)
1456+
(identifier))))
14571457
(visibility_modifier)
14581458
(type_identifier)
14591459
(enum_variant_list

0 commit comments

Comments
 (0)