From e7b370226c9b3d045a805dae5b93231a0869bbca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Feb 2019 19:19:23 +0300 Subject: [PATCH 1/2] make macro-rules eq --- crates/ra_mbe/src/lib.rs | 20 ++++++++++---------- crates/ra_tt/src/lib.rs | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index b0983783147..29731b0502a 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -30,7 +30,7 @@ pub use crate::syntax_bridge::ast_to_token_tree; /// be very confusing is that AST has almost exactly the same shape as /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident` /// and `$()*` have special meaning (see `Var` and `Repeat` data structures) -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub struct MacroRules { pub(crate) rules: Vec, } @@ -44,13 +44,13 @@ impl MacroRules { } } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) struct Rule { pub(crate) lhs: Subtree, pub(crate) rhs: Subtree, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) enum TokenTree { Leaf(Leaf), Subtree(Subtree), @@ -58,7 +58,7 @@ pub(crate) enum TokenTree { } impl_froms!(TokenTree: Leaf, Subtree, Repeat); -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) enum Leaf { Literal(Literal), Punct(Punct), @@ -67,37 +67,37 @@ pub(crate) enum Leaf { } impl_froms!(Leaf: Literal, Punct, Ident, Var); -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) struct Subtree { pub(crate) delimiter: Delimiter, pub(crate) token_trees: Vec, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) struct Repeat { pub(crate) subtree: Subtree, pub(crate) kind: RepeatKind, pub(crate) separator: Option, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) enum RepeatKind { ZeroOrMore, OneOrMore, ZeroOrOne, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) struct Literal { pub(crate) text: SmolStr, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) struct Ident { pub(crate) text: SmolStr, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub(crate) struct Var { pub(crate) text: SmolStr, pub(crate) kind: Option, diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs index 043417abcce..df31f72f39d 100644 --- a/crates/ra_tt/src/lib.rs +++ b/crates/ra_tt/src/lib.rs @@ -39,7 +39,7 @@ pub struct Subtree { pub token_trees: Vec, } -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Delimiter { Parenthesis, Brace, From 2efdf41bdb178ebf1ff0f8e3b335f491c84d7fa3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Feb 2019 19:23:13 +0300 Subject: [PATCH 2/2] make macro a NameOwner --- crates/ra_syntax/src/ast/generated.rs | 1 + crates/ra_syntax/src/grammar.ron | 5 ++++- crates/ra_syntax/src/grammar/items.rs | 4 +++- crates/ra_syntax/tests/data/parser/err/0028_macro_2.0.txt | 3 ++- .../tests/data/parser/inline/ok/0062_mod_contents.txt | 3 ++- .../tests/data/parser/inline/ok/0096_no_semi_after_block.txt | 3 ++- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 256277609f6..d2b080743b7 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1908,6 +1908,7 @@ impl ToOwned for MacroCall { } +impl ast::NameOwner for MacroCall {} impl MacroCall { pub fn token_tree(&self) -> Option<&TokenTree> { super::child_opt(self) diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index d428bc5955d..2e4b2d776c2 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -545,7 +545,10 @@ Grammar( "Visibility": (), "Name": (), "NameRef": (), - "MacroCall": ( options: [ "TokenTree", "Path" ] ), + "MacroCall": ( + traits: [ "NameOwner" ], + options: [ "TokenTree", "Path" ], + ), "Attr": ( options: [ ["value", "TokenTree"] ] ), "TokenTree": (), "TypeParamList": ( diff --git a/crates/ra_syntax/src/grammar/items.rs b/crates/ra_syntax/src/grammar/items.rs index a61f260cf19..4b962c1f37f 100644 --- a/crates/ra_syntax/src/grammar/items.rs +++ b/crates/ra_syntax/src/grammar/items.rs @@ -347,7 +347,9 @@ fn macro_call(p: &mut Parser) -> BlockLike { pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { p.expect(EXCL); - p.eat(IDENT); + if p.at(IDENT) { + name(p); + } match p.current() { L_CURLY => { token_tree(p); diff --git a/crates/ra_syntax/tests/data/parser/err/0028_macro_2.0.txt b/crates/ra_syntax/tests/data/parser/err/0028_macro_2.0.txt index 12919fab7a8..440bd7f92ad 100644 --- a/crates/ra_syntax/tests/data/parser/err/0028_macro_2.0.txt +++ b/crates/ra_syntax/tests/data/parser/err/0028_macro_2.0.txt @@ -6,7 +6,8 @@ SOURCE_FILE@[0; 349) IDENT@[0; 5) "macro" err: `expected EXCL` WHITESPACE@[5; 6) - IDENT@[6; 21) "parse_use_trees" + NAME@[6; 21) + IDENT@[6; 21) "parse_use_trees" TOKEN_TREE@[21; 41) L_PAREN@[21; 22) DOLLAR@[22; 23) diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0062_mod_contents.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0062_mod_contents.txt index 62528ca47c9..6ccd0ffc305 100644 --- a/crates/ra_syntax/tests/data/parser/inline/ok/0062_mod_contents.txt +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0062_mod_contents.txt @@ -19,7 +19,8 @@ SOURCE_FILE@[0; 70) IDENT@[12; 23) "macro_rules" EXCL@[23; 24) WHITESPACE@[24; 25) - IDENT@[25; 28) "foo" + NAME@[25; 28) + IDENT@[25; 28) "foo" WHITESPACE@[28; 29) TOKEN_TREE@[29; 31) L_CURLY@[29; 30) diff --git a/crates/ra_syntax/tests/data/parser/inline/ok/0096_no_semi_after_block.txt b/crates/ra_syntax/tests/data/parser/inline/ok/0096_no_semi_after_block.txt index 63b2300916b..ac789651a5e 100644 --- a/crates/ra_syntax/tests/data/parser/inline/ok/0096_no_semi_after_block.txt +++ b/crates/ra_syntax/tests/data/parser/inline/ok/0096_no_semi_after_block.txt @@ -92,7 +92,8 @@ SOURCE_FILE@[0; 167) IDENT@[109; 120) "macro_rules" EXCL@[120; 121) WHITESPACE@[121; 122) - IDENT@[122; 126) "test" + NAME@[122; 126) + IDENT@[122; 126) "test" WHITESPACE@[126; 127) TOKEN_TREE@[127; 152) L_CURLY@[127; 128)