David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 10 | use proc_macro2::TokenStream; |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 11 | use token::{Brace, Bracket, Paren}; |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 12 | |
| 13 | #[cfg(feature = "extra-traits")] |
| 14 | use std::hash::{Hash, Hasher}; |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 15 | #[cfg(feature = "extra-traits")] |
| 16 | use tt::TokenStreamHelper; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 17 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 19 | /// A macro invocation: `println!("{}", mac)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame^] | 20 | /// |
| 21 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 22 | /// feature.* |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 23 | pub struct Macro #manual_extra_traits { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 24 | pub path: Path, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 25 | pub bang_token: Token![!], |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 26 | pub delimiter: MacroDelimiter, |
| 27 | pub tts: TokenStream, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | ast_enum! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 32 | /// 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^] | 33 | /// |
| 34 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 35 | /// feature.* |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 36 | pub enum MacroDelimiter { |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 37 | Paren(Paren), |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 38 | Brace(Brace), |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 39 | Bracket(Bracket), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 40 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 41 | } |
| 42 | |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 43 | #[cfg(feature = "extra-traits")] |
| 44 | impl Eq for Macro {} |
| 45 | |
| 46 | #[cfg(feature = "extra-traits")] |
| 47 | impl PartialEq for Macro { |
| 48 | fn eq(&self, other: &Self) -> bool { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 49 | self.path == other.path && self.bang_token == other.bang_token |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 50 | && self.delimiter == other.delimiter |
| 51 | && TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | #[cfg(feature = "extra-traits")] |
| 56 | impl Hash for Macro { |
| 57 | fn hash<H>(&self, state: &mut H) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 58 | where |
| 59 | H: Hasher, |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 60 | { |
| 61 | self.path.hash(state); |
| 62 | self.bang_token.hash(state); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 63 | self.delimiter.hash(state); |
| 64 | TokenStreamHelper(&self.tts).hash(state); |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 67 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 68 | #[cfg(feature = "parsing")] |
| 69 | pub mod parsing { |
| 70 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 71 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 72 | use synom::Synom; |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 73 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 74 | impl Synom for Macro { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 75 | named!(parse -> Self, do_parse!( |
| 76 | what: syn!(Path) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 77 | bang: punct!(!) >> |
David Tolnay | e082403 | 2017-12-27 15:25:56 -0500 | [diff] [blame] | 78 | body: call!(tt::delimited) >> |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 79 | (Macro { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 80 | path: what, |
| 81 | bang_token: bang, |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 82 | delimiter: body.0, |
| 83 | tts: body.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 84 | }) |
| 85 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 86 | |
| 87 | fn description() -> Option<&'static str> { |
| 88 | Some("macro invocation") |
| 89 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 90 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 91 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 92 | |
| 93 | #[cfg(feature = "printing")] |
| 94 | mod printing { |
| 95 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 96 | use quote::{ToTokens, Tokens}; |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 97 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 98 | impl ToTokens for Macro { |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 99 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 100 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 101 | self.bang_token.to_tokens(tokens); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 102 | match self.delimiter { |
| 103 | MacroDelimiter::Paren(ref paren) => { |
| 104 | paren.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 105 | } |
| 106 | MacroDelimiter::Brace(ref brace) => { |
| 107 | brace.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 108 | } |
| 109 | MacroDelimiter::Bracket(ref bracket) => { |
| 110 | bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 111 | } |
| 112 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 115 | } |