blob: f093a8cf367685011fc9b07cfbb6b5f3ef3aad9e [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 Tolnaydecf28d2017-11-11 11:56:45 -080044impl Macro {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070045 pub fn is_braced(&self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -050046 is_braced(&self.tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -070047 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070048}
49
David Tolnay9c76bcb2017-12-26 23:14:59 -050050pub fn is_braced(tt: &TokenTree) -> bool {
51 match tt.kind {
52 TokenNode::Group(Delimiter::Brace, _) => true,
53 _ => false,
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070055}
56
Alex Crichtonccbb45d2017-05-23 10:58:24 -070057#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050058pub struct TokenTreeHelper<'a>(pub &'a TokenTree);
59
60#[cfg(feature = "extra-traits")]
61impl<'a> PartialEq for TokenTreeHelper<'a> {
62 fn eq(&self, other: &Self) -> bool {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070063 use proc_macro2::Spacing;
David Tolnayf4bbbd92016-09-23 14:41:55 -070064
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 match (&self.0.kind, &other.0.kind) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070066 (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070067 match (d1, d2) {
David Tolnay51382052017-12-27 13:46:21 -050068 (Delimiter::Parenthesis, Delimiter::Parenthesis)
69 | (Delimiter::Brace, Delimiter::Brace)
70 | (Delimiter::Bracket, Delimiter::Bracket)
71 | (Delimiter::None, Delimiter::None) => {}
Alex Crichtonccbb45d2017-05-23 10:58:24 -070072 _ => return false,
73 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070074
Alex Crichtonccbb45d2017-05-23 10:58:24 -070075 let s1 = s1.clone().into_iter();
76 let mut s2 = s2.clone().into_iter();
David Tolnayf4bbbd92016-09-23 14:41:55 -070077
Alex Crichtonccbb45d2017-05-23 10:58:24 -070078 for item1 in s1 {
79 let item2 = match s2.next() {
80 Some(item) => item,
81 None => return false,
82 };
David Tolnay9c76bcb2017-12-26 23:14:59 -050083 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) {
David Tolnay51382052017-12-27 13:46:21 -050084 return false;
Alex Crichtonccbb45d2017-05-23 10:58:24 -070085 }
86 }
87 s2.next().is_none()
88 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070089 (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070090 o1 == o2 && match (k1, k2) {
David Tolnay51382052017-12-27 13:46:21 -050091 (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070092 _ => false,
93 }
94 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070095 (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070096 l1.to_string() == l2.to_string()
97 }
David Tolnay51382052017-12-27 13:46:21 -050098 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => s1.as_str() == s2.as_str(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070099 _ => false,
100 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700102}
103
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700104#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -0500105impl<'a> Hash for TokenTreeHelper<'a> {
106 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700107 use proc_macro2::Spacing;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700108
109 match self.0.kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700110 TokenNode::Group(delim, ref stream) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111 0u8.hash(h);
112 match delim {
113 Delimiter::Parenthesis => 0u8.hash(h),
114 Delimiter::Brace => 1u8.hash(h),
115 Delimiter::Bracket => 2u8.hash(h),
116 Delimiter::None => 3u8.hash(h),
117 }
118
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500119 for item in stream.clone() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500120 TokenTreeHelper(&item).hash(h);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700121 }
122 0xffu8.hash(h); // terminator w/ a variant we don't normally hash
123 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700124 TokenNode::Op(op, kind) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700125 1u8.hash(h);
126 op.hash(h);
127 match kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700128 Spacing::Alone => 0u8.hash(h),
129 Spacing::Joint => 1u8.hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700130 }
131 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700132 TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
133 TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700134 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700135 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700136}
137
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700138#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500139pub struct TokenStreamHelper<'a>(pub &'a TokenStream);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500140
141#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500142impl<'a> PartialEq for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500143 fn eq(&self, other: &Self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -0500144 let left = self.0.clone().into_iter().collect::<Vec<_>>();
145 let right = other.0.clone().into_iter().collect::<Vec<_>>();
146 if left.len() != right.len() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500147 return false;
148 }
David Tolnay369f0c52017-12-27 01:50:45 -0500149 for (a, b) in left.into_iter().zip(right) {
150 if TokenTreeHelper(&a) != TokenTreeHelper(&b) {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500151 return false;
152 }
153 }
154 true
155 }
156}
157
158#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500159impl<'a> Hash for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500160 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay369f0c52017-12-27 01:50:45 -0500161 let tts = self.0.clone().into_iter().collect::<Vec<_>>();
162 tts.len().hash(state);
163 for tt in tts {
164 TokenTreeHelper(&tt).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500165 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }
David Tolnay84aa0752016-10-02 23:01:13 -0700167}
168
169#[cfg(feature = "parsing")]
170pub mod parsing {
171 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700172
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700173 use proc_macro2::{TokenNode, TokenTree};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500174 use synom::Synom;
175 use cursor::Cursor;
David Tolnay51382052017-12-27 13:46:21 -0500176 use {parse_error, PResult};
David Tolnay84aa0752016-10-02 23:01:13 -0700177
David Tolnaydecf28d2017-11-11 11:56:45 -0800178 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400179 named!(parse -> Self, do_parse!(
180 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800181 bang: punct!(!) >>
David Tolnay9c76bcb2017-12-26 23:14:59 -0500182 body: call!(parse_tt_delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800183 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400184 path: what,
185 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500186 tokens: body,
Michael Layzell92639a52017-06-01 00:07:44 -0400187 })
188 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700189 }
190
David Tolnay9c76bcb2017-12-26 23:14:59 -0500191 pub fn parse_tt_delimited(input: Cursor) -> PResult<TokenTree> {
192 match input.token_tree() {
David Tolnay51382052017-12-27 13:46:21 -0500193 Some((
194 rest,
195 token @ TokenTree {
196 kind: TokenNode::Group(..),
197 ..
198 },
199 )) => Ok((rest, token)),
David Tolnay9c76bcb2017-12-26 23:14:59 -0500200 _ => parse_error(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700201 }
202 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700203}
David Tolnayf8cf9632016-10-02 23:15:25 -0700204
205#[cfg(feature = "printing")]
206mod printing {
207 use super::*;
David Tolnay51382052017-12-27 13:46:21 -0500208 use quote::{ToTokens, Tokens};
David Tolnayf8cf9632016-10-02 23:15:25 -0700209
David Tolnaydecf28d2017-11-11 11:56:45 -0800210 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700211 fn to_tokens(&self, tokens: &mut Tokens) {
212 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700213 self.bang_token.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -0500214 self.tokens.to_tokens(tokens);
David Tolnayf8cf9632016-10-02 23:15:25 -0700215 }
216 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700217}