blob: 12c4e0d5579de8fe40bf5d9e50267a592be648f5 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
David Tolnay369f0c52017-12-27 01:50:45 -05003use proc_macro2::{TokenNode, TokenTree, TokenStream, Delimiter};
David Tolnay9c76bcb2017-12-26 23:14:59 -05004
5#[cfg(feature = "extra-traits")]
6use std::hash::{Hash, Hasher};
Alex Crichtonccbb45d2017-05-23 10:58:24 -07007
Alex Crichton62a0a592017-05-22 13:58:53 -07008ast_struct! {
9 /// Represents a macro invocation. The Path indicates which macro
10 /// is being invoked, and the vector of token-trees contains the source
11 /// of the macro invocation.
David Tolnay9c76bcb2017-12-26 23:14:59 -050012 pub struct Macro #manual_extra_traits {
Alex Crichton62a0a592017-05-22 13:58:53 -070013 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -080014 pub bang_token: Token![!],
David Tolnay369f0c52017-12-27 01:50:45 -050015 pub tokens: TokenTree,
Alex Crichton62a0a592017-05-22 13:58:53 -070016 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070017}
18
David Tolnay9c76bcb2017-12-26 23:14:59 -050019#[cfg(feature = "extra-traits")]
20impl Eq for Macro {}
21
22#[cfg(feature = "extra-traits")]
23impl PartialEq for Macro {
24 fn eq(&self, other: &Self) -> bool {
25 self.path == other.path
26 && self.bang_token == other.bang_token
David Tolnay369f0c52017-12-27 01:50:45 -050027 && TokenTreeHelper(&self.tokens) == TokenTreeHelper(&other.tokens)
David Tolnay9c76bcb2017-12-26 23:14:59 -050028 }
29}
30
31#[cfg(feature = "extra-traits")]
32impl Hash for Macro {
33 fn hash<H>(&self, state: &mut H)
34 where H: Hasher
35 {
36 self.path.hash(state);
37 self.bang_token.hash(state);
David Tolnay369f0c52017-12-27 01:50:45 -050038 TokenTreeHelper(&self.tokens).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -050039 }
40}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070041
David Tolnaydecf28d2017-11-11 11:56:45 -080042impl Macro {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070043 pub fn is_braced(&self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -050044 is_braced(&self.tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -070045 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070046}
47
David Tolnay9c76bcb2017-12-26 23:14:59 -050048pub fn is_braced(tt: &TokenTree) -> bool {
49 match tt.kind {
50 TokenNode::Group(Delimiter::Brace, _) => true,
51 _ => false,
Alex Crichton62a0a592017-05-22 13:58:53 -070052 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070053}
54
Alex Crichtonccbb45d2017-05-23 10:58:24 -070055#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050056pub struct TokenTreeHelper<'a>(pub &'a TokenTree);
57
58#[cfg(feature = "extra-traits")]
59impl<'a> PartialEq for TokenTreeHelper<'a> {
60 fn eq(&self, other: &Self) -> bool {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070061 use proc_macro2::Spacing;
David Tolnayf4bbbd92016-09-23 14:41:55 -070062
Alex Crichtonccbb45d2017-05-23 10:58:24 -070063 match (&self.0.kind, &other.0.kind) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070064 (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 match (d1, d2) {
66 (Delimiter::Parenthesis, Delimiter::Parenthesis) |
67 (Delimiter::Brace, Delimiter::Brace) |
David Tolnaybb4ca9f2017-12-26 12:28:58 -050068 (Delimiter::Bracket, Delimiter::Bracket) |
Alex Crichtonccbb45d2017-05-23 10:58:24 -070069 (Delimiter::None, Delimiter::None) => {}
70 _ => return false,
71 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070072
Alex Crichtonccbb45d2017-05-23 10:58:24 -070073 let s1 = s1.clone().into_iter();
74 let mut s2 = s2.clone().into_iter();
David Tolnayf4bbbd92016-09-23 14:41:55 -070075
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 for item1 in s1 {
77 let item2 = match s2.next() {
78 Some(item) => item,
79 None => return false,
80 };
David Tolnay9c76bcb2017-12-26 23:14:59 -050081 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070082 return false
83 }
84 }
85 s2.next().is_none()
86 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070087 (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070088 o1 == o2 && match (k1, k2) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070089 (Spacing::Alone, Spacing::Alone) |
90 (Spacing::Joint, Spacing::Joint) => true,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070091 _ => false,
92 }
93 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070094 (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070095 l1.to_string() == l2.to_string()
96 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070097 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070098 s1.as_str() == s2.as_str()
99 }
100 _ => 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
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700174 use proc_macro2::{TokenNode, TokenTree};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500175 use synom::Synom;
176 use cursor::Cursor;
177 use {PResult, parse_error};
David Tolnay84aa0752016-10-02 23:01:13 -0700178
David Tolnaydecf28d2017-11-11 11:56:45 -0800179 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400180 named!(parse -> Self, do_parse!(
181 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800182 bang: punct!(!) >>
David Tolnay9c76bcb2017-12-26 23:14:59 -0500183 body: call!(parse_tt_delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800184 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400185 path: what,
186 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500187 tokens: body,
Michael Layzell92639a52017-06-01 00:07:44 -0400188 })
189 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700190 }
191
David Tolnay9c76bcb2017-12-26 23:14:59 -0500192 pub fn parse_tt_delimited(input: Cursor) -> PResult<TokenTree> {
193 match input.token_tree() {
194 Some((rest, token @ TokenTree { kind: TokenNode::Group(..), .. })) => {
195 Ok((rest, token))
Alex Crichton954046c2017-05-30 21:49:42 -0700196 }
David Tolnay9c76bcb2017-12-26 23:14:59 -0500197 _ => parse_error(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700198 }
199 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700200}
David Tolnayf8cf9632016-10-02 23:15:25 -0700201
202#[cfg(feature = "printing")]
203mod printing {
204 use super::*;
205 use quote::{Tokens, ToTokens};
206
David Tolnaydecf28d2017-11-11 11:56:45 -0800207 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700208 fn to_tokens(&self, tokens: &mut Tokens) {
209 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700210 self.bang_token.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -0500211 self.tokens.to_tokens(tokens);
David Tolnayf8cf9632016-10-02 23:15:25 -0700212 }
213 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700214}