syn/
parse_quote.rs

1/// Quasi-quotation macro that accepts input like the [`quote!`] macro but uses
2/// type inference to figure out a return type for those tokens.
3///
4/// [`quote!`]: https://docs.rs/quote/1.0/quote/index.html
5///
6/// The return type can be any syntax tree node that implements the [`Parse`]
7/// trait.
8///
9/// [`Parse`]: crate::parse::Parse
10///
11/// ```
12/// use quote::quote;
13/// use syn::{parse_quote, Stmt};
14///
15/// fn main() {
16///     let name = quote!(v);
17///     let ty = quote!(u8);
18///
19///     let stmt: Stmt = parse_quote! {
20///         let #name: #ty = Default::default();
21///     };
22///
23///     println!("{:#?}", stmt);
24/// }
25/// ```
26///
27/// *This macro is available only if Syn is built with both the `"parsing"` and
28/// `"printing"` features.*
29///
30/// # Example
31///
32/// The following helper function adds a bound `T: HeapSize` to every type
33/// parameter `T` in the input generics.
34///
35/// ```
36/// use syn::{parse_quote, Generics, GenericParam};
37///
38/// // Add a bound `T: HeapSize` to every type parameter T.
39/// fn add_trait_bounds(mut generics: Generics) -> Generics {
40///     for param in &mut generics.params {
41///         if let GenericParam::Type(type_param) = param {
42///             type_param.bounds.push(parse_quote!(HeapSize));
43///         }
44///     }
45///     generics
46/// }
47/// ```
48///
49/// # Special cases
50///
51/// This macro can parse the following additional types as a special case even
52/// though they do not implement the `Parse` trait.
53///
54/// - [`Attribute`] — parses one attribute, allowing either outer like `#[...]`
55///   or inner like `#![...]`
56/// - [`Punctuated<T, P>`] — parses zero or more `T` separated by punctuation
57///   `P` with optional trailing punctuation
58/// - [`Vec<Stmt>`] — parses the same as `Block::parse_within`
59///
60/// [`Vec<Stmt>`]: Block::parse_within
61///
62/// # Panics
63///
64/// Panics if the tokens fail to parse as the expected syntax tree type. The
65/// caller is responsible for ensuring that the input tokens are syntactically
66/// valid.
67#[cfg_attr(docsrs, doc(cfg(all(feature = "parsing", feature = "printing"))))]
68#[macro_export]
69macro_rules! parse_quote {
70    ($($tt:tt)*) => {
71        $crate::__private::parse_quote($crate::__private::quote::quote!($($tt)*))
72    };
73}
74
75/// This macro is [`parse_quote!`] + [`quote_spanned!`][quote::quote_spanned].
76///
77/// Please refer to each of their documentation.
78///
79/// # Example
80///
81/// ```
82/// use quote::{quote, quote_spanned};
83/// use syn::spanned::Spanned;
84/// use syn::{parse_quote_spanned, ReturnType, Signature};
85///
86/// // Changes `fn()` to `fn() -> Pin<Box<dyn Future<Output = ()>>>`,
87/// // and `fn() -> T` to `fn() -> Pin<Box<dyn Future<Output = T>>>`,
88/// // without introducing any call_site() spans.
89/// fn make_ret_pinned_future(sig: &mut Signature) {
90///     let ret = match &sig.output {
91///         ReturnType::Default => quote_spanned!(sig.paren_token.span=> ()),
92///         ReturnType::Type(_, ret) => quote!(#ret),
93///     };
94///     sig.output = parse_quote_spanned! {ret.span()=>
95///         -> ::std::pin::Pin<::std::boxed::Box<dyn ::std::future::Future<Output = #ret>>>
96///     };
97/// }
98/// ```
99#[cfg_attr(docsrs, doc(cfg(all(feature = "parsing", feature = "printing"))))]
100#[macro_export]
101macro_rules! parse_quote_spanned {
102    ($span:expr=> $($tt:tt)*) => {
103        $crate::__private::parse_quote($crate::__private::quote::quote_spanned!($span=> $($tt)*))
104    };
105}
106
107////////////////////////////////////////////////////////////////////////////////
108// Can parse any type that implements Parse.
109
110use crate::error::Result;
111use crate::parse::{Parse, ParseStream, Parser};
112use proc_macro2::TokenStream;
113
114// Not public API.
115#[doc(hidden)]
116#[track_caller]
117pub fn parse<T: ParseQuote>(token_stream: TokenStream) -> T {
118    let parser = T::parse;
119    match parser.parse2(token_stream) {
120        Ok(t) => t,
121        Err(err) => panic!("{}", err),
122    }
123}
124
125#[doc(hidden)]
126pub trait ParseQuote: Sized {
127    fn parse(input: ParseStream) -> Result<Self>;
128}
129
130impl<T: Parse> ParseQuote for T {
131    fn parse(input: ParseStream) -> Result<Self> {
132        <T as Parse>::parse(input)
133    }
134}
135
136////////////////////////////////////////////////////////////////////////////////
137// Any other types that we want `parse_quote!` to be able to parse.
138
139use crate::punctuated::Punctuated;
140#[cfg(any(feature = "full", feature = "derive"))]
141use crate::{attr, Attribute, Field, FieldMutability, Ident, Type, Visibility};
142#[cfg(feature = "full")]
143use crate::{Block, Pat, Stmt};
144
145#[cfg(any(feature = "full", feature = "derive"))]
146impl ParseQuote for Attribute {
147    fn parse(input: ParseStream) -> Result<Self> {
148        if input.peek(Token![#]) && input.peek2(Token![!]) {
149            attr::parsing::single_parse_inner(input)
150        } else {
151            attr::parsing::single_parse_outer(input)
152        }
153    }
154}
155
156#[cfg(any(feature = "full", feature = "derive"))]
157impl ParseQuote for Vec<Attribute> {
158    fn parse(input: ParseStream) -> Result<Self> {
159        let mut attrs = Vec::new();
160        while !input.is_empty() {
161            attrs.push(ParseQuote::parse(input)?);
162        }
163        Ok(attrs)
164    }
165}
166
167#[cfg(any(feature = "full", feature = "derive"))]
168impl ParseQuote for Field {
169    fn parse(input: ParseStream) -> Result<Self> {
170        let attrs = input.call(Attribute::parse_outer)?;
171        let vis: Visibility = input.parse()?;
172
173        let ident: Option<Ident>;
174        let colon_token: Option<Token![:]>;
175        let is_named = input.peek(Ident) && input.peek2(Token![:]) && !input.peek2(Token![::]);
176        if is_named {
177            ident = Some(input.parse()?);
178            colon_token = Some(input.parse()?);
179        } else {
180            ident = None;
181            colon_token = None;
182        }
183
184        let ty: Type = input.parse()?;
185
186        Ok(Field {
187            attrs,
188            vis,
189            mutability: FieldMutability::None,
190            ident,
191            colon_token,
192            ty,
193        })
194    }
195}
196
197#[cfg(feature = "full")]
198impl ParseQuote for Pat {
199    fn parse(input: ParseStream) -> Result<Self> {
200        Pat::parse_multi_with_leading_vert(input)
201    }
202}
203
204#[cfg(feature = "full")]
205impl ParseQuote for Box<Pat> {
206    fn parse(input: ParseStream) -> Result<Self> {
207        <Pat as ParseQuote>::parse(input).map(Box::new)
208    }
209}
210
211impl<T: Parse, P: Parse> ParseQuote for Punctuated<T, P> {
212    fn parse(input: ParseStream) -> Result<Self> {
213        Self::parse_terminated(input)
214    }
215}
216
217#[cfg(feature = "full")]
218impl ParseQuote for Vec<Stmt> {
219    fn parse(input: ParseStream) -> Result<Self> {
220        Block::parse_within(input)
221    }
222}