David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 3 | use proc_macro2::{Delimiter, TokenNode, TokenTree}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 4 | |
| 5 | #[cfg(feature = "extra-traits")] |
David Tolnay | c6d8404 | 2017-12-27 02:14:17 -0500 | [diff] [blame] | 6 | use proc_macro2::TokenStream; |
| 7 | #[cfg(feature = "extra-traits")] |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 8 | use std::hash::{Hash, Hasher}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 9 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 10 | ast_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 Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 14 | pub struct Macro #manual_extra_traits { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 15 | pub path: Path, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 16 | pub bang_token: Token![!], |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 17 | pub tokens: TokenTree, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 19 | } |
| 20 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 21 | #[cfg(feature = "extra-traits")] |
| 22 | impl Eq for Macro {} |
| 23 | |
| 24 | #[cfg(feature = "extra-traits")] |
| 25 | impl PartialEq for Macro { |
| 26 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 27 | self.path == other.path && self.bang_token == other.bang_token |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 28 | && TokenTreeHelper(&self.tokens) == TokenTreeHelper(&other.tokens) |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
| 32 | #[cfg(feature = "extra-traits")] |
| 33 | impl Hash for Macro { |
| 34 | fn hash<H>(&self, state: &mut H) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 35 | where |
| 36 | H: Hasher, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 37 | { |
| 38 | self.path.hash(state); |
| 39 | self.bang_token.hash(state); |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 40 | TokenTreeHelper(&self.tokens).hash(state); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 41 | } |
| 42 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 43 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 44 | impl Macro { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 45 | pub fn is_braced(&self) -> bool { |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 46 | is_braced(&self.tokens) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 47 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 48 | } |
| 49 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 50 | pub fn is_braced(tt: &TokenTree) -> bool { |
| 51 | match tt.kind { |
| 52 | TokenNode::Group(Delimiter::Brace, _) => true, |
| 53 | _ => false, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 54 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 57 | #[cfg(feature = "extra-traits")] |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 58 | pub struct TokenTreeHelper<'a>(pub &'a TokenTree); |
| 59 | |
| 60 | #[cfg(feature = "extra-traits")] |
| 61 | impl<'a> PartialEq for TokenTreeHelper<'a> { |
| 62 | fn eq(&self, other: &Self) -> bool { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 63 | use proc_macro2::Spacing; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 64 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 65 | match (&self.0.kind, &other.0.kind) { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 66 | (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 67 | match (d1, d2) { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 68 | (Delimiter::Parenthesis, Delimiter::Parenthesis) |
| 69 | | (Delimiter::Brace, Delimiter::Brace) |
| 70 | | (Delimiter::Bracket, Delimiter::Bracket) |
| 71 | | (Delimiter::None, Delimiter::None) => {} |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 72 | _ => return false, |
| 73 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 74 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 75 | let s1 = s1.clone().into_iter(); |
| 76 | let mut s2 = s2.clone().into_iter(); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 77 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 78 | for item1 in s1 { |
| 79 | let item2 = match s2.next() { |
| 80 | Some(item) => item, |
| 81 | None => return false, |
| 82 | }; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 83 | if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 84 | return false; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | s2.next().is_none() |
| 88 | } |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 89 | (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 90 | o1 == o2 && match (k1, k2) { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 91 | (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 92 | _ => false, |
| 93 | } |
| 94 | } |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 95 | (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 96 | l1.to_string() == l2.to_string() |
| 97 | } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 98 | (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => s1.as_str() == s2.as_str(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 99 | _ => false, |
| 100 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 101 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 104 | #[cfg(feature = "extra-traits")] |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 105 | impl<'a> Hash for TokenTreeHelper<'a> { |
| 106 | fn hash<H: Hasher>(&self, h: &mut H) { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 107 | use proc_macro2::Spacing; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 108 | |
| 109 | match self.0.kind { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 110 | TokenNode::Group(delim, ref stream) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 111 | 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 Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 119 | for item in stream.clone() { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 120 | TokenTreeHelper(&item).hash(h); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 121 | } |
| 122 | 0xffu8.hash(h); // terminator w/ a variant we don't normally hash |
| 123 | } |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 124 | TokenNode::Op(op, kind) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 125 | 1u8.hash(h); |
| 126 | op.hash(h); |
| 127 | match kind { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 128 | Spacing::Alone => 0u8.hash(h), |
| 129 | Spacing::Joint => 1u8.hash(h), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 132 | TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h), |
| 133 | TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 134 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 135 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 138 | #[cfg(feature = "extra-traits")] |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 139 | pub struct TokenStreamHelper<'a>(pub &'a TokenStream); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 140 | |
| 141 | #[cfg(feature = "extra-traits")] |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 142 | impl<'a> PartialEq for TokenStreamHelper<'a> { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 143 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 144 | 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 Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 147 | return false; |
| 148 | } |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 149 | for (a, b) in left.into_iter().zip(right) { |
| 150 | if TokenTreeHelper(&a) != TokenTreeHelper(&b) { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | true |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[cfg(feature = "extra-traits")] |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 159 | impl<'a> Hash for TokenStreamHelper<'a> { |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 160 | fn hash<H: Hasher>(&self, state: &mut H) { |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 161 | 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 Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 165 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 166 | } |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #[cfg(feature = "parsing")] |
| 170 | pub mod parsing { |
| 171 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 172 | |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 173 | use proc_macro2::{TokenNode, TokenTree}; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 174 | use synom::Synom; |
| 175 | use cursor::Cursor; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 176 | use {parse_error, PResult}; |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 177 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 178 | impl Synom for Macro { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 179 | named!(parse -> Self, do_parse!( |
| 180 | what: syn!(Path) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 181 | bang: punct!(!) >> |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 182 | body: call!(parse_tt_delimited) >> |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 183 | (Macro { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 184 | path: what, |
| 185 | bang_token: bang, |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 186 | tokens: body, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 187 | }) |
| 188 | )); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 189 | } |
| 190 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 191 | pub fn parse_tt_delimited(input: Cursor) -> PResult<TokenTree> { |
| 192 | match input.token_tree() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 193 | Some(( |
| 194 | rest, |
| 195 | token @ TokenTree { |
| 196 | kind: TokenNode::Group(..), |
| 197 | .. |
| 198 | }, |
| 199 | )) => Ok((rest, token)), |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 200 | _ => parse_error(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 203 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 204 | |
| 205 | #[cfg(feature = "printing")] |
| 206 | mod printing { |
| 207 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame^] | 208 | use quote::{ToTokens, Tokens}; |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 209 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 210 | impl ToTokens for Macro { |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 211 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 212 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 213 | self.bang_token.to_tokens(tokens); |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 214 | self.tokens.to_tokens(tokens); |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 217 | } |