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