blob: b2a6a196a8e45cf935f7eb2a575cfe3151b8e673 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
David Tolnay9c76bcb2017-12-26 23:14:59 -05003use proc_macro2::{TokenNode, TokenTree, Delimiter};
4
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![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070015 pub tokens: Vec<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
27 && SliceTokenTreeHelper(&self.tokens) == SliceTokenTreeHelper(&other.tokens)
28 }
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);
38 SliceTokenTreeHelper(&self.tokens).hash(state);
39 }
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 {
44 match self.tokens.last() {
David Tolnay9c76bcb2017-12-26 23:14:59 -050045 Some(t) => is_braced(t),
Alex Crichtonccbb45d2017-05-23 10:58:24 -070046 None => false,
47 }
Alex Crichton62a0a592017-05-22 13:58:53 -070048 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070049}
50
David Tolnay9c76bcb2017-12-26 23:14:59 -050051pub fn is_braced(tt: &TokenTree) -> bool {
52 match tt.kind {
53 TokenNode::Group(Delimiter::Brace, _) => true,
54 _ => false,
Alex Crichton62a0a592017-05-22 13:58:53 -070055 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070056}
57
Alex Crichtonccbb45d2017-05-23 10:58:24 -070058#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -050059pub struct TokenTreeHelper<'a>(pub &'a TokenTree);
60
61#[cfg(feature = "extra-traits")]
62impl<'a> PartialEq for TokenTreeHelper<'a> {
63 fn eq(&self, other: &Self) -> bool {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070064 use proc_macro2::Spacing;
David Tolnayf4bbbd92016-09-23 14:41:55 -070065
Alex Crichtonccbb45d2017-05-23 10:58:24 -070066 match (&self.0.kind, &other.0.kind) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070067 (&TokenNode::Group(d1, ref s1), &TokenNode::Group(d2, ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070068 match (d1, d2) {
69 (Delimiter::Parenthesis, Delimiter::Parenthesis) |
70 (Delimiter::Brace, Delimiter::Brace) |
David Tolnaybb4ca9f2017-12-26 12:28:58 -050071 (Delimiter::Bracket, Delimiter::Bracket) |
Alex Crichtonccbb45d2017-05-23 10:58:24 -070072 (Delimiter::None, Delimiter::None) => {}
73 _ => return false,
74 }
David Tolnayf4bbbd92016-09-23 14:41:55 -070075
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 let s1 = s1.clone().into_iter();
77 let mut s2 = s2.clone().into_iter();
David Tolnayf4bbbd92016-09-23 14:41:55 -070078
Alex Crichtonccbb45d2017-05-23 10:58:24 -070079 for item1 in s1 {
80 let item2 = match s2.next() {
81 Some(item) => item,
82 None => return false,
83 };
David Tolnay9c76bcb2017-12-26 23:14:59 -050084 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070085 return false
86 }
87 }
88 s2.next().is_none()
89 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070090 (&TokenNode::Op(o1, k1), &TokenNode::Op(o2, k2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070091 o1 == o2 && match (k1, k2) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070092 (Spacing::Alone, Spacing::Alone) |
93 (Spacing::Joint, Spacing::Joint) => true,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070094 _ => false,
95 }
96 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -070097 (&TokenNode::Literal(ref l1), &TokenNode::Literal(ref l2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070098 l1.to_string() == l2.to_string()
99 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700100 (&TokenNode::Term(ref s1), &TokenNode::Term(ref s2)) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700101 s1.as_str() == s2.as_str()
102 }
103 _ => false,
104 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700106}
107
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700108#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -0500109impl<'a> Hash for TokenTreeHelper<'a> {
110 fn hash<H: Hasher>(&self, h: &mut H) {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700111 use proc_macro2::Spacing;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700112
113 match self.0.kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700114 TokenNode::Group(delim, ref stream) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700115 0u8.hash(h);
116 match delim {
117 Delimiter::Parenthesis => 0u8.hash(h),
118 Delimiter::Brace => 1u8.hash(h),
119 Delimiter::Bracket => 2u8.hash(h),
120 Delimiter::None => 3u8.hash(h),
121 }
122
David Tolnaybb4ca9f2017-12-26 12:28:58 -0500123 for item in stream.clone() {
David Tolnay9c76bcb2017-12-26 23:14:59 -0500124 TokenTreeHelper(&item).hash(h);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700125 }
126 0xffu8.hash(h); // terminator w/ a variant we don't normally hash
127 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700128 TokenNode::Op(op, kind) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700129 1u8.hash(h);
130 op.hash(h);
131 match kind {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700132 Spacing::Alone => 0u8.hash(h),
133 Spacing::Joint => 1u8.hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700134 }
135 }
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700136 TokenNode::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
137 TokenNode::Term(ref word) => (3u8, word.as_str()).hash(h),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700138 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700140}
141
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700142#[cfg(feature = "extra-traits")]
David Tolnay9c76bcb2017-12-26 23:14:59 -0500143pub struct SliceTokenTreeHelper<'a>(pub &'a [TokenTree]);
144
145#[cfg(feature = "extra-traits")]
146impl<'a> PartialEq for SliceTokenTreeHelper<'a> {
147 fn eq(&self, other: &Self) -> bool {
148 if self.0.len() != other.0.len() {
149 return false;
150 }
151 for (a, b) in self.0.iter().zip(other.0) {
152 if TokenTreeHelper(a) != TokenTreeHelper(b) {
153 return false;
154 }
155 }
156 true
157 }
158}
159
160#[cfg(feature = "extra-traits")]
161impl<'a> Hash for SliceTokenTreeHelper<'a> {
162 fn hash<H: Hasher>(&self, state: &mut H) {
163 self.0.len().hash(state);
164 for tt in self.0 {
165 TokenTreeHelper(tt).hash(state);
166 }
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,
187 tokens: vec![body],
188 })
189 ));
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700190 }
191
David Tolnay9c76bcb2017-12-26 23:14:59 -0500192 pub fn parse_tt_list(input: Cursor) -> PResult<Vec<TokenTree>> {
193 Ok((Cursor::empty(), input.token_stream().into_iter().collect()))
194 }
Alex Crichton954046c2017-05-30 21:49:42 -0700195
David Tolnay9c76bcb2017-12-26 23:14:59 -0500196 pub fn parse_tt_delimited(input: Cursor) -> PResult<TokenTree> {
197 match input.token_tree() {
198 Some((rest, token @ TokenTree { kind: TokenNode::Group(..), .. })) => {
199 Ok((rest, token))
Alex Crichton954046c2017-05-30 21:49:42 -0700200 }
David Tolnay9c76bcb2017-12-26 23:14:59 -0500201 _ => parse_error(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700202 }
203 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700204}
David Tolnayf8cf9632016-10-02 23:15:25 -0700205
206#[cfg(feature = "printing")]
207mod printing {
208 use super::*;
209 use quote::{Tokens, ToTokens};
210
David Tolnaydecf28d2017-11-11 11:56:45 -0800211 impl ToTokens for Macro {
David Tolnayf8cf9632016-10-02 23:15:25 -0700212 fn to_tokens(&self, tokens: &mut Tokens) {
213 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700214 self.bang_token.to_tokens(tokens);
215 tokens.append_all(&self.tokens);
David Tolnayf8cf9632016-10-02 23:15:25 -0700216 }
217 }
David Tolnayf8cf9632016-10-02 23:15:25 -0700218}