Skip to content

Commit d1c4d88

Browse files
committed
Handle non-built-in attrs as attr-items
1 parent 0fa0b98 commit d1c4d88

File tree

5 files changed

+68135
-64636
lines changed

5 files changed

+68135
-64636
lines changed

grammar.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,54 @@ const numeric_types = [
3535

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

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

@@ -205,18 +253,59 @@ module.exports = grammar({
205253
attribute_item: $ => seq(
206254
'#',
207255
'[',
208-
$.meta_item,
256+
$._attr,
209257
']'
210258
),
211259

212260
inner_attribute_item: $ => seq(
213261
'#',
214262
'!',
215263
'[',
216-
$.meta_item,
264+
$._attr,
217265
']'
218266
),
219267

268+
_attr: $ => choice(
269+
alias($.built_in_attr, $.meta_item),
270+
alias($.custom_attr, $.attr_item),
271+
),
272+
273+
custom_attr: $ => seq(
274+
$._path,
275+
optional(choice(
276+
seq('=', field('value', $._literal)),
277+
field('arguments', $.delim_token_tree)
278+
))
279+
),
280+
281+
delim_token_tree: $ => choice(
282+
seq('(', repeat($._delim_tokens), ')'),
283+
seq('[', repeat($._delim_tokens), ']'),
284+
seq('{', repeat($._delim_tokens), '}')
285+
),
286+
287+
_delim_tokens: $ => choice(
288+
$._non_delim_token,
289+
$.delim_token_tree,
290+
),
291+
292+
_non_delim_token: $ => choice(
293+
$._non_special_token,
294+
'$'
295+
),
296+
297+
built_in_attr: $ => seq(
298+
$._built_in_attr_path,
299+
optional(choice(
300+
seq('=', field('value', $._literal)),
301+
field('arguments', $.meta_arguments)
302+
))
303+
),
304+
305+
_built_in_attr_path: $ => choice(
306+
...built_in_attributes.map(name => alias(name, $.identifier))
307+
),
308+
220309
meta_item: $ => seq(
221310
$._path,
222311
optional(choice(

0 commit comments

Comments
 (0)