blob: a43c311cfb449dde1a05feb965f564b49a35cf45 [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")]
David Tolnayab919512017-12-30 23:31:51 -05006use proc_macro2::{TokenTree, TokenNode, Delimiter};
David Tolnayc6d84042017-12-27 02:14:17 -05007#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -05008use std::hash::{Hash, Hasher};
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
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050059pub struct TokenTreeHelper<'a>(pub &'a TokenTree);
60
61#[cfg(feature = "extra-traits")]
62impl<'a> PartialEq for TokenTreeHelper<'a> {
63 fn eq(&self, other: &Self) -> bool {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070064 use proc_macro2::Spacing;
David Tolnayf4bbbd92016-09-23 14:41:55 -070065
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 match (&self.0.kind, &other.0.kind) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070067 (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070068 match (d1, d2) {
David Tolnay51382052017-12-27 13:46:21 -050069 (Delimiter::Parenthesis, Delimiter::Parenthesis)
70 | (Delimiter::Brace, Delimiter::Brace)
71 | (Delimiter::Bracket, Delimiter::Bracket)
72 | (Delimiter::None, Delimiter::None) => {}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070073 _ => return false,
74 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070075
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 let s1 = s1.clone().into_iter();
77 let mut s2 = s2.clone().into_iter();
David Tolnayf4bbbd92016-09-23 14:41:55 -070078
Alex Crichtonccbb45d2017-05-23 10:58:24 -070079 for item1 in s1 {
80 let item2 = match s2.next() {
81 Some(item) => item,
82 None => return false,
83 };
David Tolnay9c76bcb2017-12-26 23:14:59 -050084 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) {
David Tolnay51382052017-12-27 13:46:21 -050085 return false;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070086 }
87 }
88 s2.next().is_none()
89 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070090 (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070091 o1 == o2 && match (k1, k2) {
David Tolnay51382052017-12-27 13:46:21 -050092 (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070093 _ => false,
94 }
95 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070096 (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070097 l1.to_string() == l2.to_string()
98 }
David Tolnay51382052017-12-27 13:46:21 -050099 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => s1.as_str() == s2.as_str(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 _ => false,
101 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700103}
104
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700105#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -0500106impl<'a> Hash for TokenTreeHelper<'a> {
107 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700108 use proc_macro2::Spacing;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700109
110 match self.0.kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700111 TokenNode::Group(delim, ref stream) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700112 0u8.hash(h);
113 match delim {
114 Delimiter::Parenthesis => 0u8.hash(h),
115 Delimiter::Brace => 1u8.hash(h),
116 Delimiter::Bracket => 2u8.hash(h),
117 Delimiter::None => 3u8.hash(h),
118 }
119
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500120 for item in stream.clone() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500121 TokenTreeHelper(&item).hash(h);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700122 }
123 0xffu8.hash(h); // terminator w/ a variant we don't normally hash
124 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700125 TokenNode::Op(op, kind) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 1u8.hash(h);
127 op.hash(h);
128 match kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700129 Spacing::Alone => 0u8.hash(h),
130 Spacing::Joint => 1u8.hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700131 }
132 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700133 TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
134 TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700135 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700137}
138
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700139#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500140pub struct TokenStreamHelper<'a>(pub &'a TokenStream);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500141
142#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500143impl<'a> PartialEq for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500144 fn eq(&self, other: &Self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -0500145 let left = self.0.clone().into_iter().collect::<Vec<_>>();
146 let right = other.0.clone().into_iter().collect::<Vec<_>>();
147 if left.len() != right.len() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500148 return false;
149 }
David Tolnay369f0c52017-12-27 01:50:45 -0500150 for (a, b) in left.into_iter().zip(right) {
151 if TokenTreeHelper(&a) != TokenTreeHelper(&b) {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500152 return false;
153 }
154 }
155 true
156 }
157}
158
159#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500160impl<'a> Hash for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500161 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay369f0c52017-12-27 01:50:45 -0500162 let tts = self.0.clone().into_iter().collect::<Vec<_>>();
163 tts.len().hash(state);
164 for tt in tts {
165 TokenTreeHelper(&tt).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500166 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700167 }
David Tolnay84aa0752016-10-02 23:01:13 -0700168}
169
170#[cfg(feature = "parsing")]
171pub mod parsing {
172 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700173
David Tolnayc5ab8c62017-12-26 16:43:39 -0500174 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700175
David Tolnaydecf28d2017-11-11 11:56:45 -0800176 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400177 named!(parse -> Self, do_parse!(
178 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800179 bang: punct!(!) >>
David Tolnaye0824032017-12-27 15:25:56 -0500180 body: call!(tt::delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800181 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400182 path: what,
183 bang_token: bang,
David Tolnayab919512017-12-30 23:31:51 -0500184 delimiter: body.0,
185 tts: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -0400186 })
187 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700188 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700189}
David Tolnayf8cf9632016-10-02 23:15:25 -0700190
191#[cfg(feature = "printing")]
192mod printing {
193 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500194 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -0700195
David Tolnaydecf28d2017-11-11 11:56:45 -0800196 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700197 fn to_tokens(&self, tokens: &mut Tokens) {
198 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700199 self.bang_token.to_tokens(tokens);
David Tolnayab919512017-12-30 23:31:51 -0500200 match self.delimiter {
201 MacroDelimiter::Paren(ref paren) => {
202 paren.surround(tokens, |tokens| self.tts.to_tokens(tokens));
203 }
204 MacroDelimiter::Brace(ref brace) => {
205 brace.surround(tokens, |tokens| self.tts.to_tokens(tokens));
206 }
207 MacroDelimiter::Bracket(ref bracket) => {
208 bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens));
209 }
210 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700211 }
212 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700213}