Skip to content

Commit 3ddebf4

Browse files
authored
Remove special cases for built-in attributes (#163)
Parse all attribute using the general attribute syntax
1 parent 099b033 commit 3ddebf4

File tree

6 files changed

+71075
-74498
lines changed

6 files changed

+71075
-74498
lines changed

corpus/declarations.txt

+42-52
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ fn accumulate(self) -> Machine<{State::Accumulate}> {}
176176
name: (identifier)
177177
parameters: (parameters
178178
(attribute_item
179-
(attr_item
179+
(attribute
180180
(identifier)))
181181
(parameter
182182
pattern: (identifier)
183183
type: (primitive_type))
184184
(attribute_item
185-
(attr_item
185+
(attribute
186186
(identifier)))
187187
(parameter
188188
pattern: (identifier)
@@ -578,7 +578,7 @@ struct Inches(i32);
578578
(field_identifier)
579579
(primitive_type))
580580
(attribute_item
581-
(attr_item
581+
(attribute
582582
(identifier)))
583583
(field_declaration
584584
(field_identifier)
@@ -737,10 +737,10 @@ pub enum Node<T: Item> {
737737
(field_identifier)
738738
(primitive_type))))
739739
(attribute_item
740-
(attr_item
740+
(attribute
741741
(identifier)))
742742
(attribute_item
743-
(attr_item
743+
(attribute
744744
(identifier)))
745745
(enum_variant
746746
(identifier)
@@ -1030,50 +1030,44 @@ mod macos_only {}
10301030

10311031
(source_file
10321032
(attribute_item
1033-
(meta_item
1033+
(attribute
10341034
(identifier)))
10351035
(function_item
10361036
name: (identifier)
10371037
parameters: (parameters)
10381038
body: (block))
10391039
(attribute_item
1040-
(meta_item
1040+
(attribute
10411041
(identifier)
1042-
arguments: (meta_arguments
1043-
(meta_item
1044-
(identifier)))))
1042+
arguments: (token_tree
1043+
(identifier))))
10451044
(struct_item
10461045
name: (type_identifier))
10471046
(attribute_item
1048-
(meta_item
1047+
(attribute
10491048
(identifier)
1050-
arguments: (meta_arguments
1051-
(meta_item
1052-
(identifier))
1053-
(meta_item
1054-
(identifier)))))
1049+
arguments: (token_tree
1050+
(identifier)
1051+
(identifier))))
10551052
(struct_item
10561053
name: (type_identifier))
10571054
(attribute_item
1058-
(meta_item
1055+
(attribute
10591056
(identifier)
1060-
arguments: (meta_arguments
1061-
(meta_item
1062-
(identifier)
1063-
value: (string_literal)))))
1057+
arguments: (token_tree
1058+
(identifier)
1059+
(string_literal))))
10641060
(mod_item
10651061
name: (identifier)
10661062
body: (declaration_list))
10671063
(inner_attribute_item
1068-
(meta_item
1064+
(attribute
10691065
(identifier)
1070-
arguments: (meta_arguments
1071-
(meta_item
1072-
(scoped_identifier
1073-
path: (identifier)
1074-
name: (identifier))))))
1066+
arguments: (token_tree
1067+
(identifier)
1068+
(identifier))))
10751069
(attribute_item
1076-
(attr_item
1070+
(attribute
10771071
(scoped_identifier
10781072
path: (identifier)
10791073
name: (identifier))
@@ -1094,12 +1088,11 @@ mod macos_only {
10941088
name: (identifier)
10951089
body: (declaration_list
10961090
(inner_attribute_item
1097-
(meta_item
1091+
(attribute
10981092
(identifier)
1099-
arguments: (meta_arguments
1100-
(meta_item
1101-
(identifier)
1102-
value: (string_literal))))))))
1093+
arguments: (token_tree
1094+
(identifier)
1095+
(string_literal)))))))
11031096

11041097
================================================================================
11051098
Key-Value Attribute Expressions
@@ -1115,7 +1108,7 @@ fn baz() {}
11151108

11161109
(source_file
11171110
(attribute_item
1118-
(meta_item
1111+
(attribute
11191112
(identifier)
11201113
(macro_invocation
11211114
(identifier)
@@ -1126,7 +1119,7 @@ fn baz() {}
11261119
(parameters)
11271120
(block))
11281121
(attribute_item
1129-
(attr_item
1122+
(attribute
11301123
(identifier)
11311124
(scoped_identifier
11321125
(identifier)
@@ -1152,7 +1145,7 @@ foo(#[bar(some tokens are special in other contexts: $/';()*()+.)] x);
11521145
function: (identifier)
11531146
arguments: (arguments
11541147
(attribute_item
1155-
(attr_item
1148+
(attribute
11561149
(identifier)
11571150
arguments: (token_tree
11581151
(identifier)
@@ -1164,7 +1157,7 @@ foo(#[bar(some tokens are special in other contexts: $/';()*()+.)] x);
11641157
function: (identifier)
11651158
arguments: (arguments
11661159
(attribute_item
1167-
(attr_item
1160+
(attribute
11681161
(identifier)
11691162
arguments: (token_tree
11701163
(identifier)
@@ -1203,19 +1196,17 @@ pub enum Error {
12031196
(identifier)
12041197
(identifier)))
12051198
(attribute_item
1206-
(meta_item
1199+
(attribute
12071200
(identifier)
1208-
(meta_arguments
1209-
(meta_item
1210-
(identifier))
1211-
(meta_item
1212-
(identifier)))))
1201+
(token_tree
1202+
(identifier)
1203+
(identifier))))
12131204
(enum_item
12141205
(visibility_modifier)
12151206
(type_identifier)
12161207
(enum_variant_list
12171208
(attribute_item
1218-
(attr_item
1209+
(attribute
12191210
(identifier)
12201211
(token_tree
12211212
(string_literal)
@@ -1227,7 +1218,7 @@ pub enum Error {
12271218
(ordered_field_declaration_list
12281219
(type_identifier)))
12291220
(attribute_item
1230-
(attr_item
1221+
(attribute
12311222
(identifier)
12321223
(token_tree
12331224
(string_literal)
@@ -1270,18 +1261,17 @@ fn foo() {
12701261
arguments: (arguments
12711262
(identifier)
12721263
(attribute_item
1273-
(meta_item
1264+
(attribute
12741265
(identifier)
1275-
arguments: (meta_arguments
1276-
(meta_item
1277-
(identifier)
1278-
value: (string_literal)))))
1266+
arguments: (token_tree
1267+
(identifier)
1268+
(string_literal))))
12791269
(identifier))))
12801270
(let_declaration
12811271
pattern: (identifier)
12821272
value: (array_expression
12831273
(attribute_item
1284-
(attr_item
1274+
(attribute
12851275
(identifier)))
12861276
(integer_literal)
12871277
(integer_literal)
@@ -1290,7 +1280,7 @@ fn foo() {
12901280
pattern: (identifier)
12911281
value: (tuple_expression
12921282
(attribute_item
1293-
(attr_item
1283+
(attribute
12941284
(identifier)))
12951285
(integer_literal)
12961286
(integer_literal)

corpus/expressions.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ let msg = match x {
612612
value: (integer_literal))
613613
(match_arm
614614
(attribute_item
615-
(attr_item
615+
(attribute
616616
(identifier)))
617617
pattern: (match_pattern
618618
(integer_literal))

grammar.js

+3-86
Original file line numberDiff line numberDiff line change
@@ -36,54 +36,6 @@ const numeric_types = [
3636

3737
const primitive_types = numeric_types.concat(['bool', 'str', 'char'])
3838

39-
const built_in_attributes = [
40-
'cfg',
41-
'cfg_attr',
42-
'test',
43-
'ignore',
44-
'should_panic',
45-
'derive',
46-
'automatically_derived',
47-
'macro_export',
48-
'macro_use',
49-
'proc_macro',
50-
'proc_macro_derive',
51-
'proc_macro_attribute',
52-
'allow',
53-
'warn',
54-
'deny',
55-
'forbid',
56-
'deprecated',
57-
'must_use',
58-
'link',
59-
'link_name',
60-
'no_link',
61-
'repr',
62-
'crate_type',
63-
'no_main',
64-
'export_name',
65-
'link_section',
66-
'no_mangle',
67-
'used',
68-
'crate_name',
69-
'inline',
70-
'cold',
71-
'no_builtins',
72-
'target_feature',
73-
'track_caller',
74-
'doc',
75-
'no_std',
76-
'no_implicit_prelude',
77-
'path',
78-
'recursion_limit',
79-
'type_length_limit',
80-
'panic_handler',
81-
'global_allocator',
82-
'windows_subsystem',
83-
'feature',
84-
'non_exhaustive'
85-
]
86-
8739
module.exports = grammar({
8840
name: 'rust',
8941

@@ -259,61 +211,26 @@ module.exports = grammar({
259211
attribute_item: $ => seq(
260212
'#',
261213
'[',
262-
$._attr,
214+
$.attribute,
263215
']'
264216
),
265217

266218
inner_attribute_item: $ => seq(
267219
'#',
268220
'!',
269221
'[',
270-
$._attr,
222+
$.attribute,
271223
']'
272224
),
273225

274-
_attr: $ => choice(
275-
alias($.built_in_attr, $.meta_item),
276-
alias($.custom_attr, $.attr_item),
277-
),
278-
279-
custom_attr: $ => seq(
226+
attribute: $ => seq(
280227
$._path,
281228
optional(choice(
282229
seq('=', field('value', $._expression)),
283230
field('arguments', alias($.delim_token_tree, $.token_tree))
284231
))
285232
),
286233

287-
built_in_attr: $ => seq(
288-
$._built_in_attr_path,
289-
optional(choice(
290-
seq('=', field('value', $._expression)),
291-
field('arguments', $.meta_arguments)
292-
))
293-
),
294-
295-
_built_in_attr_path: $ => choice(
296-
...built_in_attributes.map(name => alias(name, $.identifier))
297-
),
298-
299-
meta_item: $ => seq(
300-
$._path,
301-
optional(choice(
302-
seq('=', field('value', $._expression)),
303-
field('arguments', $.meta_arguments)
304-
))
305-
),
306-
307-
meta_arguments: $ => seq(
308-
'(',
309-
sepBy(',', choice(
310-
$.meta_item,
311-
$._literal
312-
)),
313-
optional(','),
314-
')'
315-
),
316-
317234
mod_item: $ => seq(
318235
optional($.visibility_modifier),
319236
'mod',

0 commit comments

Comments
 (0)