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")] |
David Tolnay | d639f61 | 2019-06-09 03:36:01 -0700 | [diff] [blame] | 4 | use proc_macro2::{Delimiter, Span, 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")] |
David Tolnay | d639f61 | 2019-06-09 03:36:01 -0700 | [diff] [blame] | 8 | use parse::{Parse, ParseStream, Parser, 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 | d639f61 | 2019-06-09 03:36:01 -0700 | [diff] [blame] | 66 | fn delimiter_span(delimiter: &MacroDelimiter) -> Span { |
| 67 | match *delimiter { |
| 68 | MacroDelimiter::Paren(ref token) => token.span, |
| 69 | MacroDelimiter::Brace(ref token) => token.span, |
| 70 | MacroDelimiter::Bracket(ref token) => token.span, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl Macro { |
| 75 | /// Parse the tokens within the macro invocation's delimiters into a syntax |
| 76 | /// tree. |
| 77 | /// |
| 78 | /// This is equivalent to `syn::parse2::<T>(mac.tts)` except that it |
| 79 | /// produces a more useful span when `tts` is empty. |
| 80 | /// |
| 81 | /// # Example |
| 82 | /// |
| 83 | /// ```edition2018 |
| 84 | /// use syn::{parse_quote, Expr, ExprLit, Ident, Lit, LitStr, Macro, Token}; |
| 85 | /// use syn::ext::IdentExt; |
| 86 | /// use syn::parse::{Error, Parse, ParseStream, Result}; |
| 87 | /// use syn::punctuated::Punctuated; |
| 88 | /// |
| 89 | /// // The arguments expected by libcore's format_args macro, and as a |
| 90 | /// // result most other formatting and printing macros like println. |
| 91 | /// // |
| 92 | /// // println!("{} is {number:.prec$}", "x", prec=5, number=0.01) |
| 93 | /// struct FormatArgs { |
| 94 | /// format_string: Expr, |
| 95 | /// positional_args: Vec<Expr>, |
| 96 | /// named_args: Vec<(Ident, Expr)>, |
| 97 | /// } |
| 98 | /// |
| 99 | /// impl Parse for FormatArgs { |
| 100 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 101 | /// let format_string: Expr; |
| 102 | /// let mut positional_args = Vec::new(); |
| 103 | /// let mut named_args = Vec::new(); |
| 104 | /// |
| 105 | /// format_string = input.parse()?; |
| 106 | /// while !input.is_empty() { |
| 107 | /// input.parse::<Token![,]>()?; |
| 108 | /// if input.is_empty() { |
| 109 | /// break; |
| 110 | /// } |
| 111 | /// if input.peek(Ident::peek_any) && input.peek2(Token![=]) { |
| 112 | /// while !input.is_empty() { |
| 113 | /// let name: Ident = input.call(Ident::parse_any)?; |
| 114 | /// input.parse::<Token![=]>()?; |
| 115 | /// let value: Expr = input.parse()?; |
| 116 | /// named_args.push((name, value)); |
| 117 | /// if input.is_empty() { |
| 118 | /// break; |
| 119 | /// } |
| 120 | /// input.parse::<Token![,]>()?; |
| 121 | /// } |
| 122 | /// break; |
| 123 | /// } |
| 124 | /// positional_args.push(input.parse()?); |
| 125 | /// } |
| 126 | /// |
| 127 | /// Ok(FormatArgs { |
| 128 | /// format_string, |
| 129 | /// positional_args, |
| 130 | /// named_args, |
| 131 | /// }) |
| 132 | /// } |
| 133 | /// } |
| 134 | /// |
| 135 | /// // Extract the first argument, the format string literal, from an |
| 136 | /// // invocation of a formatting or printing macro. |
| 137 | /// fn get_format_string(m: &Macro) -> Result<LitStr> { |
| 138 | /// let args: FormatArgs = m.parse_body()?; |
| 139 | /// match args.format_string { |
| 140 | /// Expr::Lit(ExprLit { lit: Lit::Str(lit), .. }) => Ok(lit), |
| 141 | /// other => { |
| 142 | /// // First argument was not a string literal expression. |
| 143 | /// // Maybe something like: println!(concat!(...), ...) |
| 144 | /// Err(Error::new_spanned(other, "format string must be a string literal")) |
| 145 | /// } |
| 146 | /// } |
| 147 | /// } |
| 148 | /// |
| 149 | /// fn main() { |
| 150 | /// let invocation = parse_quote! { |
| 151 | /// println!("{:?}", Instant::now()) |
| 152 | /// }; |
| 153 | /// let lit = get_format_string(&invocation).unwrap(); |
| 154 | /// assert_eq!(lit.value(), "{:?}"); |
| 155 | /// } |
| 156 | /// ``` |
| 157 | #[cfg(feature = "parsing")] |
| 158 | pub fn parse_body<T: Parse>(&self) -> Result<T> { |
| 159 | self.parse_body_with(T::parse) |
| 160 | } |
| 161 | |
| 162 | /// Parse the tokens within the macro invocation's delimiters using the |
| 163 | /// given parser. |
| 164 | #[cfg(feature = "parsing")] |
| 165 | pub fn parse_body_with<F: Parser>(&self, parser: F) -> Result<F::Output> { |
| 166 | // TODO: see if we can get a group.span_close() span in here as the |
| 167 | // scope, rather than the span of the whole group. |
| 168 | let scope = delimiter_span(&self.delimiter); |
| 169 | private::parse_scoped(parser, scope, self.tts.clone()) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #[cfg(feature = "parsing")] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 174 | pub fn parse_delimiter(input: ParseStream) -> Result<(MacroDelimiter, TokenStream)> { |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 175 | input.step(|cursor| { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 176 | if let Some((TokenTree::Group(g), rest)) = cursor.token_tree() { |
| 177 | let span = g.span(); |
| 178 | let delimiter = match g.delimiter() { |
| 179 | Delimiter::Parenthesis => MacroDelimiter::Paren(Paren(span)), |
| 180 | Delimiter::Brace => MacroDelimiter::Brace(Brace(span)), |
| 181 | Delimiter::Bracket => MacroDelimiter::Bracket(Bracket(span)), |
| 182 | Delimiter::None => { |
| 183 | return Err(cursor.error("expected delimiter")); |
| 184 | } |
| 185 | }; |
| 186 | Ok(((delimiter, g.stream().clone()), rest)) |
| 187 | } else { |
| 188 | Err(cursor.error("expected delimiter")) |
| 189 | } |
| 190 | }) |
| 191 | } |
| 192 | |
| 193 | #[cfg(feature = "parsing")] |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 194 | pub mod parsing { |
| 195 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 196 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 197 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 198 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 199 | impl Parse for Macro { |
| 200 | fn parse(input: ParseStream) -> Result<Self> { |
| 201 | let tts; |
| 202 | Ok(Macro { |
| 203 | path: input.call(Path::parse_mod_style)?, |
| 204 | bang_token: input.parse()?, |
| 205 | delimiter: { |
| 206 | let (delimiter, content) = parse_delimiter(input)?; |
| 207 | tts = content; |
| 208 | delimiter |
| 209 | }, |
| 210 | tts: tts, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 211 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 212 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 213 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 214 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 215 | |
| 216 | #[cfg(feature = "printing")] |
| 217 | mod printing { |
| 218 | use super::*; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 219 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 220 | use quote::ToTokens; |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 221 | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 222 | impl ToTokens for Macro { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 223 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 224 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 225 | self.bang_token.to_tokens(tokens); |
David Tolnay | ab91951 | 2017-12-30 23:31:51 -0500 | [diff] [blame] | 226 | match self.delimiter { |
| 227 | MacroDelimiter::Paren(ref paren) => { |
| 228 | paren.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 229 | } |
| 230 | MacroDelimiter::Brace(ref brace) => { |
| 231 | brace.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 232 | } |
| 233 | MacroDelimiter::Bracket(ref bracket) => { |
| 234 | bracket.surround(tokens, |tokens| self.tts.to_tokens(tokens)); |
| 235 | } |
| 236 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
David Tolnay | f8cf963 | 2016-10-02 23:15:25 -0700 | [diff] [blame] | 239 | } |