blob: 1de6a2acd9ad86c87d27a8798b425ca22534f732 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
David Tolnayc6d84042017-12-27 02:14:17 -05003use proc_macro2::{TokenNode, TokenTree, Delimiter};
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 {
27 self.path == other.path
28 && self.bang_token == other.bang_token
David Tolnay369f0c52017-12-27 01:50:45 -050029 && TokenTreeHelper(&self.tokens) == TokenTreeHelper(&other.tokens)
David Tolnay9c76bcb2017-12-26 23:14:59 -050030 }
31}
32
33#[cfg(feature = "extra-traits")]
34impl Hash for Macro {
35 fn hash<H>(&self, state: &mut H)
36 where H: Hasher
37 {
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) {
68 (Delimiter::Parenthesis, Delimiter::Parenthesis) |
69 (Delimiter::Brace, Delimiter::Brace) |
David Tolnaybb4ca9f2017-12-26 12:28:58 -050070 (Delimiter::Bracket, Delimiter::Bracket) |
Alex Crichtonccbb45d2017-05-23 10:58:24 -070071 (Delimiter::None, Delimiter::None) => {}
72 _ => 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) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070084 return false
85 }
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) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070091 (Spacing::Alone, Spacing::Alone) |
92 (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 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070099 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 s1.as_str() == s2.as_str()
101 }
102 _ => false,
103 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700104 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700105}
106
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700107#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -0500108impl<'a> Hash for TokenTreeHelper<'a> {
109 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700110 use proc_macro2::Spacing;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700111
112 match self.0.kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700113 TokenNode::Group(delim, ref stream) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700114 0u8.hash(h);
115 match delim {
116 Delimiter::Parenthesis => 0u8.hash(h),
117 Delimiter::Brace => 1u8.hash(h),
118 Delimiter::Bracket => 2u8.hash(h),
119 Delimiter::None => 3u8.hash(h),
120 }
121
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500122 for item in stream.clone() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500123 TokenTreeHelper(&item).hash(h);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700124 }
125 0xffu8.hash(h); // terminator w/ a variant we don't normally hash
126 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700127 TokenNode::Op(op, kind) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700128 1u8.hash(h);
129 op.hash(h);
130 match kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700131 Spacing::Alone => 0u8.hash(h),
132 Spacing::Joint => 1u8.hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700133 }
134 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700135 TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
136 TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700137 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700139}
140
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700141#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500142pub struct TokenStreamHelper<'a>(pub &'a TokenStream);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500143
144#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500145impl<'a> PartialEq for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500146 fn eq(&self, other: &Self) -> bool {
David Tolnay369f0c52017-12-27 01:50:45 -0500147 let left = self.0.clone().into_iter().collect::<Vec<_>>();
148 let right = other.0.clone().into_iter().collect::<Vec<_>>();
149 if left.len() != right.len() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500150 return false;
151 }
David Tolnay369f0c52017-12-27 01:50:45 -0500152 for (a, b) in left.into_iter().zip(right) {
153 if TokenTreeHelper(&a) != TokenTreeHelper(&b) {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500154 return false;
155 }
156 }
157 true
158 }
159}
160
161#[cfg(feature = "extra-traits")]
David Tolnay369f0c52017-12-27 01:50:45 -0500162impl<'a> Hash for TokenStreamHelper<'a> {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500163 fn hash<H: Hasher>(&self, state: &mut H) {
David Tolnay369f0c52017-12-27 01:50:45 -0500164 let tts = self.0.clone().into_iter().collect::<Vec<_>>();
165 tts.len().hash(state);
166 for tt in tts {
167 TokenTreeHelper(&tt).hash(state);
David Tolnay9c76bcb2017-12-26 23:14:59 -0500168 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700169 }
David Tolnay84aa0752016-10-02 23:01:13 -0700170}
171
172#[cfg(feature = "parsing")]
173pub mod parsing {
174 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700175
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700176 use proc_macro2::{TokenNode, TokenTree};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500177 use synom::Synom;
178 use cursor::Cursor;
179 use {PResult, parse_error};
David Tolnay84aa0752016-10-02 23:01:13 -0700180
David Tolnaydecf28d2017-11-11 11:56:45 -0800181 impl Synom for Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400182 named!(parse -> Self, do_parse!(
183 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800184 bang: punct!(!) >>
David Tolnay9c76bcb2017-12-26 23:14:59 -0500185 body: call!(parse_tt_delimited) >>
David Tolnaydecf28d2017-11-11 11:56:45 -0800186 (Macro {
Michael Layzell92639a52017-06-01 00:07:44 -0400187 path: what,
188 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -0500189 tokens: body,
Michael Layzell92639a52017-06-01 00:07:44 -0400190 })
191 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700192 }
193
David Tolnay9c76bcb2017-12-26 23:14:59 -0500194 pub fn parse_tt_delimited(input: Cursor) -> PResult<TokenTree> {
195 match input.token_tree() {
196 Some((rest, token @ TokenTree { kind: TokenNode::Group(..), .. })) => {
197 Ok((rest, token))
Alex Crichton954046c2017-05-30 21:49:42 -0700198 }
David Tolnay9c76bcb2017-12-26 23:14:59 -0500199 _ => parse_error(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700200 }
201 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700202}
David Tolnayf8cf9632016-10-02 23:15:25 -0700203
204#[cfg(feature = "printing")]
205mod printing {
206 use super::*;
207 use quote::{Tokens, ToTokens};
208
David Tolnaydecf28d2017-11-11 11:56:45 -0800209 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700210 fn to_tokens(&self, tokens: &mut Tokens) {
211 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700212 self.bang_token.to_tokens(tokens);
David Tolnay369f0c52017-12-27 01:50:45 -0500213 self.tokens.to_tokens(tokens);
David Tolnayf8cf9632016-10-02 23:15:25 -0700214 }
215 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700216}