790: make macro-rules eq r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-02-11 16:24:39 +00:00
commit 77ccac74f9
8 changed files with 25 additions and 16 deletions

View file

@ -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 /// 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` /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures) /// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub struct MacroRules { pub struct MacroRules {
pub(crate) rules: Vec<Rule>, pub(crate) rules: Vec<Rule>,
} }
@ -44,13 +44,13 @@ impl MacroRules {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Rule { pub(crate) struct Rule {
pub(crate) lhs: Subtree, pub(crate) lhs: Subtree,
pub(crate) rhs: Subtree, pub(crate) rhs: Subtree,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) enum TokenTree { pub(crate) enum TokenTree {
Leaf(Leaf), Leaf(Leaf),
Subtree(Subtree), Subtree(Subtree),
@ -58,7 +58,7 @@ pub(crate) enum TokenTree {
} }
impl_froms!(TokenTree: Leaf, Subtree, Repeat); impl_froms!(TokenTree: Leaf, Subtree, Repeat);
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) enum Leaf { pub(crate) enum Leaf {
Literal(Literal), Literal(Literal),
Punct(Punct), Punct(Punct),
@ -67,37 +67,37 @@ pub(crate) enum Leaf {
} }
impl_froms!(Leaf: Literal, Punct, Ident, Var); impl_froms!(Leaf: Literal, Punct, Ident, Var);
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Subtree { pub(crate) struct Subtree {
pub(crate) delimiter: Delimiter, pub(crate) delimiter: Delimiter,
pub(crate) token_trees: Vec<TokenTree>, pub(crate) token_trees: Vec<TokenTree>,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Repeat { pub(crate) struct Repeat {
pub(crate) subtree: Subtree, pub(crate) subtree: Subtree,
pub(crate) kind: RepeatKind, pub(crate) kind: RepeatKind,
pub(crate) separator: Option<char>, pub(crate) separator: Option<char>,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) enum RepeatKind { pub(crate) enum RepeatKind {
ZeroOrMore, ZeroOrMore,
OneOrMore, OneOrMore,
ZeroOrOne, ZeroOrOne,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Literal { pub(crate) struct Literal {
pub(crate) text: SmolStr, pub(crate) text: SmolStr,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Ident { pub(crate) struct Ident {
pub(crate) text: SmolStr, pub(crate) text: SmolStr,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct Var { pub(crate) struct Var {
pub(crate) text: SmolStr, pub(crate) text: SmolStr,
pub(crate) kind: Option<SmolStr>, pub(crate) kind: Option<SmolStr>,

View file

@ -1908,6 +1908,7 @@ impl ToOwned for MacroCall {
} }
impl ast::NameOwner for MacroCall {}
impl MacroCall { impl MacroCall {
pub fn token_tree(&self) -> Option<&TokenTree> { pub fn token_tree(&self) -> Option<&TokenTree> {
super::child_opt(self) super::child_opt(self)

View file

@ -545,7 +545,10 @@ Grammar(
"Visibility": (), "Visibility": (),
"Name": (), "Name": (),
"NameRef": (), "NameRef": (),
"MacroCall": ( options: [ "TokenTree", "Path" ] ), "MacroCall": (
traits: [ "NameOwner" ],
options: [ "TokenTree", "Path" ],
),
"Attr": ( options: [ ["value", "TokenTree"] ] ), "Attr": ( options: [ ["value", "TokenTree"] ] ),
"TokenTree": (), "TokenTree": (),
"TypeParamList": ( "TypeParamList": (

View file

@ -347,7 +347,9 @@ fn macro_call(p: &mut Parser) -> BlockLike {
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
p.expect(EXCL); p.expect(EXCL);
p.eat(IDENT); if p.at(IDENT) {
name(p);
}
match p.current() { match p.current() {
L_CURLY => { L_CURLY => {
token_tree(p); token_tree(p);

View file

@ -6,7 +6,8 @@ SOURCE_FILE@[0; 349)
IDENT@[0; 5) "macro" IDENT@[0; 5) "macro"
err: `expected EXCL` err: `expected EXCL`
WHITESPACE@[5; 6) WHITESPACE@[5; 6)
IDENT@[6; 21) "parse_use_trees" NAME@[6; 21)
IDENT@[6; 21) "parse_use_trees"
TOKEN_TREE@[21; 41) TOKEN_TREE@[21; 41)
L_PAREN@[21; 22) L_PAREN@[21; 22)
DOLLAR@[22; 23) DOLLAR@[22; 23)

View file

@ -19,7 +19,8 @@ SOURCE_FILE@[0; 70)
IDENT@[12; 23) "macro_rules" IDENT@[12; 23) "macro_rules"
EXCL@[23; 24) EXCL@[23; 24)
WHITESPACE@[24; 25) WHITESPACE@[24; 25)
IDENT@[25; 28) "foo" NAME@[25; 28)
IDENT@[25; 28) "foo"
WHITESPACE@[28; 29) WHITESPACE@[28; 29)
TOKEN_TREE@[29; 31) TOKEN_TREE@[29; 31)
L_CURLY@[29; 30) L_CURLY@[29; 30)

View file

@ -92,7 +92,8 @@ SOURCE_FILE@[0; 167)
IDENT@[109; 120) "macro_rules" IDENT@[109; 120) "macro_rules"
EXCL@[120; 121) EXCL@[120; 121)
WHITESPACE@[121; 122) WHITESPACE@[121; 122)
IDENT@[122; 126) "test" NAME@[122; 126)
IDENT@[122; 126) "test"
WHITESPACE@[126; 127) WHITESPACE@[126; 127)
TOKEN_TREE@[127; 152) TOKEN_TREE@[127; 152)
L_CURLY@[127; 128) L_CURLY@[127; 128)

View file

@ -39,7 +39,7 @@ pub struct Subtree {
pub token_trees: Vec<TokenTree>, pub token_trees: Vec<TokenTree>,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Delimiter { pub enum Delimiter {
Parenthesis, Parenthesis,
Brace, Brace,