David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | use super::*; |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 2 | use proc_macro2::TokenStream; |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 3 | #[cfg(feature = "parsing")] |
| 4 | use proc_macro2::{Delimiter, TokenTree}; |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 5 | use token::{Brace, Bracket, Paren}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 6 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 7 | #[cfg(feature = "parsing")] |
| 8 | use parse::{ParseStream, Result}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 9 | #[cfg(feature = "extra-traits")] |
| 10 | use std::hash::{Hash, Hasher}; |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 11 | #[cfg(feature = "extra-traits")] |
| 12 | use tt::TokenStreamHelper; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 13 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 14 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 15 | /// A macro invocation: `println!("{}", mac)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 16 | /// |
| 17 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 18 | /// feature.* |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 19 | pub struct Macro #manual_extra_traits { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 20 | pub path: Path, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 21 | pub bang_token: Token![!], |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 22 | pub delimiter: MacroDelimiter, |
| 23 | pub tts: TokenStream, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | ast_enum! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 28 | /// A grouping token that surrounds a macro body: `m!(...)` or `m!{...}` or `m![...]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 29 | /// |
| 30 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 31 | /// feature.* |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 32 | pub enum MacroDelimiter { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 33 | Paren(Paren), |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 34 | Brace(Brace), |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 35 | Bracket(Bracket), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 36 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 37 | } |
| 38 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 39 | #[cfg(feature = "extra-traits")] |
| 40 | impl Eq for Macro {} |
| 41 | |
| 42 | #[cfg(feature = "extra-traits")] |
| 43 | impl PartialEq for Macro { |
| 44 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 45 | self.path == other.path |
| 46 | && self.bang_token == other.bang_token |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 47 | && self.delimiter == other.delimiter |
| 48 | && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | #[cfg(feature = "extra-traits")] |
| 53 | impl Hash for Macro { |
| 54 | fn hash<H>(&self, state: &mut H) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 55 | where |
| 56 | H: Hasher, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 57 | { |
| 58 | self.path.hash(state); |
| 59 | self.bang_token.hash(state); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 60 | self.delimiter.hash(state); |
| 61 | TokenStreamHelper(&self.tts).hash(state); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 64 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 65 | #[cfg(feature = "parsing")] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 66 | pub fn parse_delimiter(input: ParseStream) -> Result<(MacroDelimiter, TokenStream)> { |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 67 | input.step(|cursor| { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 68 | if let Some((TokenTree::Group(g), rest)) = cursor.token_tree() { |
| 69 | let span = g.span(); |
| 70 | let delimiter = match g.delimiter() { |
| 71 | Delimiter::Parenthesis => MacroDelimiter::Paren(Paren(span)), |
| 72 | Delimiter::Brace => MacroDelimiter::Brace(Brace(span)), |
| 73 | Delimiter::Bracket => MacroDelimiter::Bracket(Bracket(span)), |
| 74 | Delimiter::None => { |
| 75 | return Err(cursor.error("expected delimiter")); |
| 76 | } |
| 77 | }; |
| 78 | Ok(((delimiter, g.stream().clone()), rest)) |
| 79 | } else { |
| 80 | Err(cursor.error("expected delimiter")) |
| 81 | } |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | #[cfg(feature = "parsing")] |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 86 | pub mod parsing { |
| 87 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 88 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 89 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 90 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 91 | impl Parse for Macro { |
| 92 | fn parse(input: ParseStream) -> Result<Self> { |
| 93 | let tts; |
| 94 | Ok(Macro { |
| 95 | path: input.call(Path::parse_mod_style)?, |
| 96 | bang_token: input.parse()?, |
| 97 | delimiter: { |
| 98 | let (delimiter, content) = parse_delimiter(input)?; |
| 99 | tts = content; |
| 100 | delimiter |
| 101 | }, |
| 102 | tts: tts, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 103 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 104 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 105 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 106 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 107 | |
| 108 | #[cfg(feature = "printing")] |
| 109 | mod printing { |
| 110 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 111 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 112 | use quote::ToTokens; |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 113 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 114 | impl ToTokens for Macro { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 115 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 116 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 117 | self.bang_token.to_tokens(tokens); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 118 | match self.delimiter { |
| 119 | MacroDelimiter::Paren(ref paren) => { |
| 120 | paren.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 121 | } |
| 122 | MacroDelimiter::Brace(ref brace) => { |
| 123 | brace.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 124 | } |
| 125 | MacroDelimiter::Bracket(ref bracket) => { |
| 126 | bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 127 | } |
| 128 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 131 | } |