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; |
| 3 | use token::{Paren, Brace, Bracket}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 4 | |
| 5 | #[cfg(feature = "extra-traits")] |
| 6 | use std::hash::{Hash, Hasher}; |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame^] | 7 | #[cfg(feature = "extra-traits")] |
| 8 | use tt::TokenStreamHelper; |
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! { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 11 | /// Represents a macro invocation. The Path indicates which macro is being |
| 12 | /// invoked, and the `TokenStream` contains the source of the macro |
| 13 | /// 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 | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 17 | pub delimiter: MacroDelimiter, |
| 18 | pub tts: TokenStream, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | ast_enum! { |
| 23 | pub enum MacroDelimiter { |
| 24 | /// `macro!(...)` |
| 25 | Paren(Paren), |
| 26 | /// `macro!{...}` |
| 27 | Brace(Brace), |
| 28 | /// `macro![...]` |
| 29 | Bracket(Bracket), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 31 | } |
| 32 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 33 | #[cfg(feature = "extra-traits")] |
| 34 | impl Eq for Macro {} |
| 35 | |
| 36 | #[cfg(feature = "extra-traits")] |
| 37 | impl PartialEq for Macro { |
| 38 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 39 | self.path == other.path && self.bang_token == other.bang_token |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 40 | && self.delimiter == other.delimiter |
| 41 | && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | #[cfg(feature = "extra-traits")] |
| 46 | impl Hash for Macro { |
| 47 | fn hash<H>(&self, state: &mut H) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 48 | where |
| 49 | H: Hasher, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 50 | { |
| 51 | self.path.hash(state); |
| 52 | self.bang_token.hash(state); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 53 | self.delimiter.hash(state); |
| 54 | TokenStreamHelper(&self.tts).hash(state); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 55 | } |
| 56 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 57 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 58 | #[cfg(feature = "parsing")] |
| 59 | pub mod parsing { |
| 60 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 61 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 62 | use synom::Synom; |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 63 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 64 | impl Synom for Macro { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 65 | named!(parse -> Self, do_parse!( |
| 66 | what: syn!(Path) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 67 | bang: punct!(!) >> |
David Tolnay | e082403 | 2017-12-27 15:25:56 -0500 | [diff] [blame] | 68 | body: call!(tt::delimited) >> |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 69 | (Macro { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 70 | path: what, |
| 71 | bang_token: bang, |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 72 | delimiter: body.0, |
| 73 | tts: body.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 74 | }) |
| 75 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 76 | |
| 77 | fn description() -> Option<&'static str> { |
| 78 | Some("macro invocation") |
| 79 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 80 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 81 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 82 | |
| 83 | #[cfg(feature = "printing")] |
| 84 | mod printing { |
| 85 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 86 | use quote::{ToTokens, Tokens}; |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 87 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 88 | impl ToTokens for Macro { |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 89 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 90 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 91 | self.bang_token.to_tokens(tokens); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 92 | match self.delimiter { |
| 93 | MacroDelimiter::Paren(ref paren) => { |
| 94 | paren.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 95 | } |
| 96 | MacroDelimiter::Brace(ref brace) => { |
| 97 | brace.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 98 | } |
| 99 | MacroDelimiter::Bracket(ref bracket) => { |
| 100 | bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 101 | } |
| 102 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 105 | } |