blob: 028f6d2639f1e3bbfb86185155a811b3830c1f22 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
David Tolnayab919512017-12-30 23:31:51 -05002use proc_macro2::TokenStream;
3use token::{Paren, Brace, Bracket};
David Tolnay9c76bcb2017-12-26 23:14:59 -05004
5#[cfg(feature = "extra-traits")]
6use std::hash::{Hash, Hasher};
David Tolnayc43b44e2017-12-30 23:55:54 -05007#[cfg(feature = "extra-traits")]
8use tt::TokenStreamHelper;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07009
Alex Crichton62a0a592017-05-22 13:58:53 -070010ast_struct! {
David Tolnayab919512017-12-30 23:31:51 -050011 /// Represents a macro invocation. The Path indicates which macro is being
12 /// invoked, and the `TokenStream` contains the source of the macro
13 /// invocation.
David Tolnay9c76bcb2017-12-26 23:14:59 -050014 pub struct Macro #manual_extra_traits {
Alex Crichton62a0a592017-05-22 13:58:53 -070015 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -080016 pub bang_token: Token![!],
David Tolnayab919512017-12-30 23:31:51 -050017 pub delimiter: MacroDelimiter,
18 pub tts: TokenStream,
19 }
20}
21
22ast_enum! {
23 pub enum MacroDelimiter {
24 /// `macro!(...)`
25 Paren(Paren),
26 /// `macro!{...}`
27 Brace(Brace),
28 /// `macro![...]`
29 Bracket(Bracket),
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070031}
32
David Tolnay9c76bcb2017-12-26 23:14:59 -050033#[cfg(feature = "extra-traits")]
34impl Eq for Macro {}
35
36#[cfg(feature = "extra-traits")]
37impl PartialEq for Macro {
38 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -050039 self.path == other.path && self.bang_token == other.bang_token
David Tolnayab919512017-12-30 23:31:51 -050040 && self.delimiter == other.delimiter
41 && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
David Tolnay9c76bcb2017-12-26 23:14:59 -050042 }
43}
44
45#[cfg(feature = "extra-traits")]
46impl Hash for Macro {
47 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -050048 where
49 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -050050 {
51 self.path.hash(state);
52 self.bang_token.hash(state);
David Tolnayab919512017-12-30 23:31:51 -050053 self.delimiter.hash(state);
54 TokenStreamHelper(&self.tts).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -050055 }
56}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070057
David Tolnay84aa0752016-10-02 23:01:13 -070058#[cfg(feature = "parsing")]
59pub mod parsing {
60 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061
David Tolnayc5ab8c62017-12-26 16:43:39 -050062 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -070063
David Tolnaydecf28d2017-11-11 11:56:45 -080064 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -040065 named!(parse -> Self, do_parse!(
66 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -080067 bang: punct!(!) >>
David Tolnaye0824032017-12-27 15:25:56 -050068 body: call!(tt::delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -080069 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -040070 path: what,
71 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -050072 delimiter: body.0,
73 tts: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -040074 })
75 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -080076
77 fn description() -> Option<&'static str> {
78 Some("macro invocation")
79 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -070080 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070081}
David Tolnayf8cf9632016-10-02 23:15:25 -070082
83#[cfg(feature = "printing")]
84mod printing {
85 use super::*;
David Tolnay51382052017-12-27 13:46:21 -050086 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -070087
David Tolnaydecf28d2017-11-11 11:56:45 -080088 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -070089 fn to_tokens(&self, tokens: &mut Tokens) {
90 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -070091 self.bang_token.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -050092 match self.delimiter {
93 MacroDelimiter::Paren(ref paren) => {
94 paren.surround(tokens, |tokens| self.tts.to_tokens(tokens));
95 }
96 MacroDelimiter::Brace(ref brace) => {
97 brace.surround(tokens, |tokens| self.tts.to_tokens(tokens));
98 }
99 MacroDelimiter::Bracket(ref bracket) => {
100 bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens));
101 }
102 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700103 }
104 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700105}