@@ -35,6 +35,54 @@ const numeric_types = [
3535
3636const 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+
3886module . exports = grammar ( {
3987 name : 'rust' ,
4088
@@ -202,18 +250,59 @@ module.exports = grammar({
202250 attribute_item : $ => seq (
203251 '#' ,
204252 '[' ,
205- $ . meta_item ,
253+ $ . _attr ,
206254 ']'
207255 ) ,
208256
209257 inner_attribute_item : $ => seq (
210258 '#' ,
211259 '!' ,
212260 '[' ,
213- $ . meta_item ,
261+ $ . _attr ,
214262 ']'
215263 ) ,
216264
265+ _attr : $ => choice (
266+ alias ( $ . built_in_attr , $ . meta_item ) ,
267+ alias ( $ . custom_attr , $ . attr_item ) ,
268+ ) ,
269+
270+ custom_attr : $ => seq (
271+ $ . _path ,
272+ optional ( choice (
273+ seq ( '=' , field ( 'value' , $ . _literal ) ) ,
274+ field ( 'arguments' , $ . delim_token_tree )
275+ ) )
276+ ) ,
277+
278+ delim_token_tree : $ => choice (
279+ seq ( '(' , repeat ( $ . _delim_tokens ) , ')' ) ,
280+ seq ( '[' , repeat ( $ . _delim_tokens ) , ']' ) ,
281+ seq ( '{' , repeat ( $ . _delim_tokens ) , '}' )
282+ ) ,
283+
284+ _delim_tokens : $ => choice (
285+ $ . _non_delim_token ,
286+ $ . delim_token_tree ,
287+ ) ,
288+
289+ _non_delim_token : $ => choice (
290+ $ . _non_special_token ,
291+ '$'
292+ ) ,
293+
294+ built_in_attr : $ => seq (
295+ $ . _built_in_attr_path ,
296+ optional ( choice (
297+ seq ( '=' , field ( 'value' , $ . _literal ) ) ,
298+ field ( 'arguments' , $ . meta_arguments )
299+ ) )
300+ ) ,
301+
302+ _built_in_attr_path : $ => choice (
303+ ...built_in_attributes . map ( name => alias ( name , $ . identifier ) )
304+ ) ,
305+
217306 meta_item : $ => seq (
218307 $ . _path ,
219308 optional ( choice (
0 commit comments