Skip to content

Commit

Permalink
Remove special cases for built-in attributes (#163)
Browse files Browse the repository at this point in the history
Parse all attribute using the general attribute syntax
  • Loading branch information
maxbrunsfeld authored Nov 3, 2022
1 parent 099b033 commit 3ddebf4
Show file tree
Hide file tree
Showing 6 changed files with 71,075 additions and 74,498 deletions.
94 changes: 42 additions & 52 deletions corpus/declarations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ fn accumulate(self) -> Machine<{State::Accumulate}> {}
name: (identifier)
parameters: (parameters
(attribute_item
(attr_item
(attribute
(identifier)))
(parameter
pattern: (identifier)
type: (primitive_type))
(attribute_item
(attr_item
(attribute
(identifier)))
(parameter
pattern: (identifier)
Expand Down Expand Up @@ -578,7 +578,7 @@ struct Inches(i32);
(field_identifier)
(primitive_type))
(attribute_item
(attr_item
(attribute
(identifier)))
(field_declaration
(field_identifier)
Expand Down Expand Up @@ -737,10 +737,10 @@ pub enum Node<T: Item> {
(field_identifier)
(primitive_type))))
(attribute_item
(attr_item
(attribute
(identifier)))
(attribute_item
(attr_item
(attribute
(identifier)))
(enum_variant
(identifier)
Expand Down Expand Up @@ -1030,50 +1030,44 @@ mod macos_only {}

(source_file
(attribute_item
(meta_item
(attribute
(identifier)))
(function_item
name: (identifier)
parameters: (parameters)
body: (block))
(attribute_item
(meta_item
(attribute
(identifier)
arguments: (meta_arguments
(meta_item
(identifier)))))
arguments: (token_tree
(identifier))))
(struct_item
name: (type_identifier))
(attribute_item
(meta_item
(attribute
(identifier)
arguments: (meta_arguments
(meta_item
(identifier))
(meta_item
(identifier)))))
arguments: (token_tree
(identifier)
(identifier))))
(struct_item
name: (type_identifier))
(attribute_item
(meta_item
(attribute
(identifier)
arguments: (meta_arguments
(meta_item
(identifier)
value: (string_literal)))))
arguments: (token_tree
(identifier)
(string_literal))))
(mod_item
name: (identifier)
body: (declaration_list))
(inner_attribute_item
(meta_item
(attribute
(identifier)
arguments: (meta_arguments
(meta_item
(scoped_identifier
path: (identifier)
name: (identifier))))))
arguments: (token_tree
(identifier)
(identifier))))
(attribute_item
(attr_item
(attribute
(scoped_identifier
path: (identifier)
name: (identifier))
Expand All @@ -1094,12 +1088,11 @@ mod macos_only {
name: (identifier)
body: (declaration_list
(inner_attribute_item
(meta_item
(attribute
(identifier)
arguments: (meta_arguments
(meta_item
(identifier)
value: (string_literal))))))))
arguments: (token_tree
(identifier)
(string_literal)))))))

================================================================================
Key-Value Attribute Expressions
Expand All @@ -1115,7 +1108,7 @@ fn baz() {}

(source_file
(attribute_item
(meta_item
(attribute
(identifier)
(macro_invocation
(identifier)
Expand All @@ -1126,7 +1119,7 @@ fn baz() {}
(parameters)
(block))
(attribute_item
(attr_item
(attribute
(identifier)
(scoped_identifier
(identifier)
Expand All @@ -1152,7 +1145,7 @@ foo(#[bar(some tokens are special in other contexts: $/';()*()+.)] x);
function: (identifier)
arguments: (arguments
(attribute_item
(attr_item
(attribute
(identifier)
arguments: (token_tree
(identifier)
Expand All @@ -1164,7 +1157,7 @@ foo(#[bar(some tokens are special in other contexts: $/';()*()+.)] x);
function: (identifier)
arguments: (arguments
(attribute_item
(attr_item
(attribute
(identifier)
arguments: (token_tree
(identifier)
Expand Down Expand Up @@ -1203,19 +1196,17 @@ pub enum Error {
(identifier)
(identifier)))
(attribute_item
(meta_item
(attribute
(identifier)
(meta_arguments
(meta_item
(identifier))
(meta_item
(identifier)))))
(token_tree
(identifier)
(identifier))))
(enum_item
(visibility_modifier)
(type_identifier)
(enum_variant_list
(attribute_item
(attr_item
(attribute
(identifier)
(token_tree
(string_literal)
Expand All @@ -1227,7 +1218,7 @@ pub enum Error {
(ordered_field_declaration_list
(type_identifier)))
(attribute_item
(attr_item
(attribute
(identifier)
(token_tree
(string_literal)
Expand Down Expand Up @@ -1270,18 +1261,17 @@ fn foo() {
arguments: (arguments
(identifier)
(attribute_item
(meta_item
(attribute
(identifier)
arguments: (meta_arguments
(meta_item
(identifier)
value: (string_literal)))))
arguments: (token_tree
(identifier)
(string_literal))))
(identifier))))
(let_declaration
pattern: (identifier)
value: (array_expression
(attribute_item
(attr_item
(attribute
(identifier)))
(integer_literal)
(integer_literal)
Expand All @@ -1290,7 +1280,7 @@ fn foo() {
pattern: (identifier)
value: (tuple_expression
(attribute_item
(attr_item
(attribute
(identifier)))
(integer_literal)
(integer_literal)
Expand Down
2 changes: 1 addition & 1 deletion corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ let msg = match x {
value: (integer_literal))
(match_arm
(attribute_item
(attr_item
(attribute
(identifier)))
pattern: (match_pattern
(integer_literal))
Expand Down
89 changes: 3 additions & 86 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,54 +36,6 @@ const numeric_types = [

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

const built_in_attributes = [
'cfg',
'cfg_attr',
'test',
'ignore',
'should_panic',
'derive',
'automatically_derived',
'macro_export',
'macro_use',
'proc_macro',
'proc_macro_derive',
'proc_macro_attribute',
'allow',
'warn',
'deny',
'forbid',
'deprecated',
'must_use',
'link',
'link_name',
'no_link',
'repr',
'crate_type',
'no_main',
'export_name',
'link_section',
'no_mangle',
'used',
'crate_name',
'inline',
'cold',
'no_builtins',
'target_feature',
'track_caller',
'doc',
'no_std',
'no_implicit_prelude',
'path',
'recursion_limit',
'type_length_limit',
'panic_handler',
'global_allocator',
'windows_subsystem',
'feature',
'non_exhaustive'
]

module.exports = grammar({
name: 'rust',

Expand Down Expand Up @@ -259,61 +211,26 @@ module.exports = grammar({
attribute_item: $ => seq(
'#',
'[',
$._attr,
$.attribute,
']'
),

inner_attribute_item: $ => seq(
'#',
'!',
'[',
$._attr,
$.attribute,
']'
),

_attr: $ => choice(
alias($.built_in_attr, $.meta_item),
alias($.custom_attr, $.attr_item),
),

custom_attr: $ => seq(
attribute: $ => seq(
$._path,
optional(choice(
seq('=', field('value', $._expression)),
field('arguments', alias($.delim_token_tree, $.token_tree))
))
),

built_in_attr: $ => seq(
$._built_in_attr_path,
optional(choice(
seq('=', field('value', $._expression)),
field('arguments', $.meta_arguments)
))
),

_built_in_attr_path: $ => choice(
...built_in_attributes.map(name => alias(name, $.identifier))
),

meta_item: $ => seq(
$._path,
optional(choice(
seq('=', field('value', $._expression)),
field('arguments', $.meta_arguments)
))
),

meta_arguments: $ => seq(
'(',
sepBy(',', choice(
$.meta_item,
$._literal
)),
optional(','),
')'
),

mod_item: $ => seq(
optional($.visibility_modifier),
'mod',
Expand Down
Loading

0 comments on commit 3ddebf4

Please sign in to comment.