blob: 0623c063a3f33cbebd87ad14f54068753b274dd6 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
David Tolnay51382052017-12-27 13:46:21 -05003use proc_macro2::{Delimiter, TokenNode, TokenTree};
David Tolnay9c76bcb2017-12-26 23:14:59 -05004
5#[cfg(feature = "extra-traits")]
David Tolnayc6d84042017-12-27 02:14:17 -05006use proc_macro2::TokenStream;
7#[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
David Tolnay9c76bcb2017-12-26 23:14:59 -050044pub fn is_braced(tt: &TokenTree) -> bool {
45 match tt.kind {
46 TokenNode::Group(Delimiter::Brace, _) => true,
47 _ => false,
Alex Crichton62a0a592017-05-22 13:58:53 -070048 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070049}
50
Alex Crichtonccbb45d2017-05-23 10:58:24 -070051#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050052pub struct TokenTreeHelper<'a>(pub &'a TokenTree);
53
54#[cfg(feature = "extra-traits")]
55impl<'a> PartialEq for TokenTreeHelper<'a> {
56 fn eq(&self, other: &Self) -> bool {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070057 use proc_macro2::Spacing;
David Tolnayf4bbbd92016-09-23 14:41:55 -070058
Alex Crichtonccbb45d2017-05-23 10:58:24 -070059 match (&self.0.kind, &other.0.kind) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070060 (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061 match (d1, d2) {
David Tolnay51382052017-12-27 13:46:21 -050062 (Delimiter::Parenthesis, Delimiter::Parenthesis)
63 | (Delimiter::Brace, Delimiter::Brace)
64 | (Delimiter::Bracket, Delimiter::Bracket)
65 | (Delimiter::None, Delimiter::None) => {}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 _ => return false,
67 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070068
Alex Crichtonccbb45d2017-05-23 10:58:24 -070069 let s1 = s1.clone().into_iter();
70 let mut s2 = s2.clone().into_iter();
David Tolnayf4bbbd92016-09-23 14:41:55 -070071
Alex Crichtonccbb45d2017-05-23 10:58:24 -070072 for item1 in s1 {
73 let item2 = match s2.next() {
74 Some(item) => item,
75 None => return false,
76 };
David Tolnay9c76bcb2017-12-26 23:14:59 -050077 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) {
David Tolnay51382052017-12-27 13:46:21 -050078 return false;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070079 }
80 }
81 s2.next().is_none()
82 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070083 (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070084 o1 == o2 && match (k1, k2) {
David Tolnay51382052017-12-27 13:46:21 -050085 (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070086 _ => false,
87 }
88 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070089 (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070090 l1.to_string() == l2.to_string()
91 }
David Tolnay51382052017-12-27 13:46:21 -050092 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => s1.as_str() == s2.as_str(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070093 _ => false,
94 }
Alex Crichton62a0a592017-05-22 13:58:53 -070095 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070096}
97
Alex Crichtonccbb45d2017-05-23 10:58:24 -070098#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050099impl<'a> Hash for TokenTreeHelper<'a> {
100 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700101 use proc_macro2::Spacing;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700102
103 match self.0.kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700104 TokenNode::Group(delim, ref stream) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700105 0u8.hash(h);
106 match delim {
107 Delimiter::Parenthesis => 0u8.hash(h),
108 Delimiter::Brace => 1u8.hash(h),
109 Delimiter::Bracket => 2u8.hash(h),
110 Delimiter::None => 3u8.hash(h),
111 }
112
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500113 for item in stream.clone() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500114 TokenTreeHelper(&item).hash(h);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700115 }
116 0xffu8.hash(h); // terminator w/ a variant we don't normally hash
117 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700118 TokenNode::Op(op, kind) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700119 1u8.hash(h);
120 op.hash(h);
121 match kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700122 Spacing::Alone => 0u8.hash(h),
123 Spacing::Joint => 1u8.hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700124 }
125 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700126 TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
127 TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700128 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700129 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700130}
131
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700132#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500133pub struct TokenStreamHelper<'a>(pub &'a TokenStream);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500134
135#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500136impl<'a> PartialEq for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500137 fn eq(&self, other: &Self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -0500138 let left = self.0.clone().into_iter().collect::<Vec<_>>();
139 let right = other.0.clone().into_iter().collect::<Vec<_>>();
140 if left.len() != right.len() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500141 return false;
142 }
David Tolnay369f0c52017-12-27 01:50:45 -0500143 for (a, b) in left.into_iter().zip(right) {
144 if TokenTreeHelper(&a) != TokenTreeHelper(&b) {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500145 return false;
146 }
147 }
148 true
149 }
150}
151
152#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500153impl<'a> Hash for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500154 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay369f0c52017-12-27 01:50:45 -0500155 let tts = self.0.clone().into_iter().collect::<Vec<_>>();
156 tts.len().hash(state);
157 for tt in tts {
158 TokenTreeHelper(&tt).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500159 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 }
David Tolnay84aa0752016-10-02 23:01:13 -0700161}
162
163#[cfg(feature = "parsing")]
164pub mod parsing {
165 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700166
David Tolnayc5ab8c62017-12-26 16:43:39 -0500167 use synom::Synom;
David Tolnay84aa0752016-10-02 23:01:13 -0700168
David Tolnaydecf28d2017-11-11 11:56:45 -0800169 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400170 named!(parse -> Self, do_parse!(
171 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800172 bang: punct!(!) >>
David Tolnaye0824032017-12-27 15:25:56 -0500173 body: call!(tt::delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800174 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400175 path: what,
176 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500177 tokens: body,
Michael Layzell92639a52017-06-01 00:07:44 -0400178 })
179 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700180 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700181}
David Tolnayf8cf9632016-10-02 23:15:25 -0700182
183#[cfg(feature = "printing")]
184mod printing {
185 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500186 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -0700187
David Tolnaydecf28d2017-11-11 11:56:45 -0800188 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700189 fn to_tokens(&self, tokens: &mut Tokens) {
190 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700191 self.bang_token.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -0500192 self.tokens.to_tokens(tokens);
David Tolnayf8cf9632016-10-02 23:15:25 -0700193 }
194 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700195}