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