blob: e013f92e434aa233351aefcc409052802e9a4ad5 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
David Tolnay57292da2017-12-27 21:03:33 -05003use proc_macro2::TokenTree;
David Tolnay9c76bcb2017-12-26 23:14:59 -05004
5#[cfg(feature = "extra-traits")]
David Tolnay57292da2017-12-27 21:03:33 -05006use proc_macro2::{TokenStream, 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! {
11 /// Represents a macro invocation. The Path indicates which macro
12 /// is being invoked, and the vector of token-trees contains the source
13 /// of the macro 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 Tolnay369f0c52017-12-27 01:50:45 -050017 pub tokens: TokenTree,
Alex Crichton62a0a592017-05-22 13:58:53 -070018 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070019}
20
David Tolnay9c76bcb2017-12-26 23:14:59 -050021#[cfg(feature = "extra-traits")]
22impl Eq for Macro {}
23
24#[cfg(feature = "extra-traits")]
25impl PartialEq for Macro {
26 fn eq(&self, other: &Self) -> bool {
David Tolnay51382052017-12-27 13:46:21 -050027 self.path == other.path && self.bang_token == other.bang_token
David Tolnay369f0c52017-12-27 01:50:45 -050028 && TokenTreeHelper(&self.tokens) == TokenTreeHelper(&other.tokens)
David Tolnay9c76bcb2017-12-26 23:14:59 -050029 }
30}
31
32#[cfg(feature = "extra-traits")]
33impl Hash for Macro {
34 fn hash<H>(&self, state: &mut H)
David Tolnay51382052017-12-27 13:46:21 -050035 where
36 H: Hasher,
David Tolnay9c76bcb2017-12-26 23:14:59 -050037 {
38 self.path.hash(state);
39 self.bang_token.hash(state);
David Tolnay369f0c52017-12-27 01:50:45 -050040 TokenTreeHelper(&self.tokens).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -050041 }
42}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070043
Alex Crichtonccbb45d2017-05-23 10:58:24 -070044#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050045pub struct TokenTreeHelper<'a>(pub &'a TokenTree);
46
47#[cfg(feature = "extra-traits")]
48impl<'a> PartialEq for TokenTreeHelper<'a> {
49 fn eq(&self, other: &Self) -> bool {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070050 use proc_macro2::Spacing;
David Tolnayf4bbbd92016-09-23 14:41:55 -070051
Alex Crichtonccbb45d2017-05-23 10:58:24 -070052 match (&self.0.kind, &other.0.kind) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070053 (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070054 match (d1, d2) {
David Tolnay51382052017-12-27 13:46:21 -050055 (Delimiter::Parenthesis, Delimiter::Parenthesis)
56 | (Delimiter::Brace, Delimiter::Brace)
57 | (Delimiter::Bracket, Delimiter::Bracket)
58 | (Delimiter::None, Delimiter::None) => {}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070059 _ => return false,
60 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070061
Alex Crichtonccbb45d2017-05-23 10:58:24 -070062 let s1 = s1.clone().into_iter();
63 let mut s2 = s2.clone().into_iter();
David Tolnayf4bbbd92016-09-23 14:41:55 -070064
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 for item1 in s1 {
66 let item2 = match s2.next() {
67 Some(item) => item,
68 None => return false,
69 };
David Tolnay9c76bcb2017-12-26 23:14:59 -050070 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) {
David Tolnay51382052017-12-27 13:46:21 -050071 return false;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070072 }
73 }
74 s2.next().is_none()
75 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070076 (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070077 o1 == o2 && match (k1, k2) {
David Tolnay51382052017-12-27 13:46:21 -050078 (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070079 _ => false,
80 }
81 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070082 (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070083 l1.to_string() == l2.to_string()
84 }
David Tolnay51382052017-12-27 13:46:21 -050085 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => s1.as_str() == s2.as_str(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070086 _ => false,
87 }
Alex Crichton62a0a592017-05-22 13:58:53 -070088 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070089}
90
Alex Crichtonccbb45d2017-05-23 10:58:24 -070091#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050092impl<'a> Hash for TokenTreeHelper<'a> {
93 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070094 use proc_macro2::Spacing;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095
96 match self.0.kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070097 TokenNode::Group(delim, ref stream) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070098 0u8.hash(h);
99 match delim {
100 Delimiter::Parenthesis => 0u8.hash(h),
101 Delimiter::Brace => 1u8.hash(h),
102 Delimiter::Bracket => 2u8.hash(h),
103 Delimiter::None => 3u8.hash(h),
104 }
105
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500106 for item in stream.clone() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500107 TokenTreeHelper(&item).hash(h);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700108 }
109 0xffu8.hash(h); // terminator w/ a variant we don't normally hash
110 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700111 TokenNode::Op(op, kind) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700112 1u8.hash(h);
113 op.hash(h);
114 match kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700115 Spacing::Alone => 0u8.hash(h),
116 Spacing::Joint => 1u8.hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700117 }
118 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700119 TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
120 TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700121 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700122 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700123}
124
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700125#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500126pub struct TokenStreamHelper<'a>(pub &'a TokenStream);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500127
128#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500129impl<'a> PartialEq for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500130 fn eq(&self, other: &Self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -0500131 let left = self.0.clone().into_iter().collect::<Vec<_>>();
132 let right = other.0.clone().into_iter().collect::<Vec<_>>();
133 if left.len() != right.len() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500134 return false;
135 }
David Tolnay369f0c52017-12-27 01:50:45 -0500136 for (a, b) in left.into_iter().zip(right) {
137 if TokenTreeHelper(&a) != TokenTreeHelper(&b) {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500138 return false;
139 }
140 }
141 true
142 }
143}
144
145#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500146impl<'a> Hash for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500147 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay369f0c52017-12-27 01:50:45 -0500148 let tts = self.0.clone().into_iter().collect::<Vec<_>>();
149 tts.len().hash(state);
150 for tt in tts {
151 TokenTreeHelper(&tt).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500152 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 }
David Tolnay84aa0752016-10-02 23:01:13 -0700154}
155
156#[cfg(feature = "parsing")]
157pub mod parsing {
158 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700159
David Tolnayc5ab8c62017-12-26 16:43:39 -0500160 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700161
David Tolnaydecf28d2017-11-11 11:56:45 -0800162 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400163 named!(parse -> Self, do_parse!(
164 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800165 bang: punct!(!) >>
David Tolnaye0824032017-12-27 15:25:56 -0500166 body: call!(tt::delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800167 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400168 path: what,
169 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500170 tokens: body,
Michael Layzell92639a52017-06-01 00:07:44 -0400171 })
172 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700173 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700174}
David Tolnayf8cf9632016-10-02 23:15:25 -0700175
176#[cfg(feature = "printing")]
177mod printing {
178 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500179 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -0700180
David Tolnaydecf28d2017-11-11 11:56:45 -0800181 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700182 fn to_tokens(&self, tokens: &mut Tokens) {
183 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700184 self.bang_token.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -0500185 self.tokens.to_tokens(tokens);
David Tolnayf8cf9632016-10-02 23:15:25 -0700186 }
187 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700188}