blob: 5d4045b2e6d7dd6b52c967e677fbcb132ce597d5 [file] [log] [blame]
David Tolnaye83ef5a2018-01-11 15:18:36 -08001/// 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///
David Tolnay442cf762018-08-31 11:08:24 -07004/// [`quote!`]: https://docs.rs/quote/0.6/quote/index.html
David Tolnaye83ef5a2018-01-11 15:18:36 -08005///
David Tolnayd85a8812018-08-31 11:09:57 -07006/// The return type can be any syntax tree node that implements the [`Parse`]
David Tolnaye83ef5a2018-01-11 15:18:36 -08007/// trait.
8///
David Tolnayd85a8812018-08-31 11:09:57 -07009/// [`Parse`]: parse/trait.Parse.html
David Tolnaye83ef5a2018-01-11 15:18:36 -080010///
11/// ```
David Tolnaya1c98072018-09-06 08:58:10 -070012/// #[macro_use]
13/// extern crate quote;
14/// #[macro_use]
15/// extern crate syn;
16///
17/// use syn::Stmt;
David Tolnaye83ef5a2018-01-11 15:18:36 -080018///
19/// fn main() {
20/// let name = quote!(v);
21/// let ty = quote!(u8);
22///
23/// let stmt: Stmt = parse_quote! {
24/// let #name: #ty = Default::default();
25/// };
26///
27/// println!("{:#?}", stmt);
28/// }
29/// ```
30///
David Tolnay491680a2018-01-23 00:34:40 -080031/// *This macro is available if Syn is built with the `"parsing"` feature,
32/// although interpolation of syntax tree nodes into the quoted tokens is only
33/// supported if Syn is built with the `"printing"` feature as well.*
David Tolnaye83ef5a2018-01-11 15:18:36 -080034///
35/// # Example
36///
37/// The following helper function adds a bound `T: HeapSize` to every type
38/// parameter `T` in the input generics.
39///
40/// ```
David Tolnayf1a10262018-10-13 15:33:50 -070041#[cfg_attr(not(syn_can_call_macro_by_path), doc = " #[macro_use]")]
David Tolnaye614f282018-10-27 22:50:12 -070042#[cfg_attr(not(syn_can_call_macro_by_path), doc = " extern crate quote;")]
David Tolnaya1c98072018-09-06 08:58:10 -070043/// #[macro_use]
44/// extern crate syn;
45///
David Tolnay9b00f652018-09-01 10:31:02 -070046/// use syn::{Generics, GenericParam};
47///
David Tolnaye83ef5a2018-01-11 15:18:36 -080048/// // Add a bound `T: HeapSize` to every type parameter T.
49/// fn add_trait_bounds(mut generics: Generics) -> Generics {
50/// for param in &mut generics.params {
51/// if let GenericParam::Type(ref mut type_param) = *param {
52/// type_param.bounds.push(parse_quote!(HeapSize));
53/// }
54/// }
55/// generics
56/// }
57/// #
58/// # fn main() {}
59/// ```
60///
61/// # Special cases
62///
63/// This macro can parse the following additional types as a special case even
David Tolnayd85a8812018-08-31 11:09:57 -070064/// though they do not implement the `Parse` trait.
David Tolnaye83ef5a2018-01-11 15:18:36 -080065///
66/// - [`Attribute`] — parses one attribute, allowing either outer like `#[...]`
67/// or inner like `#![...]`
68/// - [`Punctuated<T, P>`] — parses zero or more `T` separated by punctuation
69/// `P` with optional trailing punctuation
70///
71/// [`Attribute`]: struct.Attribute.html
72/// [`Punctuated<T, P>`]: punctuated/struct.Punctuated.html
73///
74/// # Panics
75///
76/// Panics if the tokens fail to parse as the expected syntax tree type. The
77/// caller is responsible for ensuring that the input tokens are syntactically
78/// valid.
David Tolnaye0ba9202018-10-06 20:09:08 -070079#[macro_export(local_inner_macros)]
David Tolnaye83ef5a2018-01-11 15:18:36 -080080macro_rules! parse_quote {
81 ($($tt:tt)*) => {
David Tolnaye0ba9202018-10-06 20:09:08 -070082 $crate::parse_quote::parse($crate::export::From::from(quote_impl!($($tt)*)))
83 };
84}
85
86#[cfg(not(syn_can_call_macro_by_path))]
87#[doc(hidden)]
88#[macro_export]
89macro_rules! quote_impl {
90 ($($tt:tt)*) => {
91 // Require caller to have their own `#[macro_use] extern crate quote`.
92 quote!($($tt)*)
93 };
94}
95
96#[cfg(syn_can_call_macro_by_path)]
97#[doc(hidden)]
98#[macro_export]
99macro_rules! quote_impl {
100 ($($tt:tt)*) => {
101 $crate::export::quote::quote!($($tt)*)
David Tolnaye83ef5a2018-01-11 15:18:36 -0800102 };
103}
104
105////////////////////////////////////////////////////////////////////////////////
David Tolnay06faaca2018-09-01 20:25:04 -0700106// Can parse any type that implements Parse.
David Tolnaye83ef5a2018-01-11 15:18:36 -0800107
David Tolnay4ac232d2018-08-31 10:18:03 -0700108use parse::{Parse, ParseStream, Parser, Result};
David Tolnaye82a2b12018-08-30 16:31:10 -0700109use proc_macro2::TokenStream;
David Tolnaye83ef5a2018-01-11 15:18:36 -0800110
111// Not public API.
112#[doc(hidden)]
David Tolnay7d6a7b82018-01-23 00:42:22 -0800113pub fn parse<T: ParseQuote>(token_stream: TokenStream) -> T {
David Tolnaye83ef5a2018-01-11 15:18:36 -0800114 let parser = T::parse;
115 match parser.parse2(token_stream) {
116 Ok(t) => t,
David Tolnay40c06112018-08-30 16:14:50 -0700117 Err(err) => panic!("{}", err),
David Tolnaye83ef5a2018-01-11 15:18:36 -0800118 }
119}
120
121// Not public API.
122#[doc(hidden)]
123pub trait ParseQuote: Sized {
David Tolnay40c06112018-08-30 16:14:50 -0700124 fn parse(input: ParseStream) -> Result<Self>;
David Tolnaye83ef5a2018-01-11 15:18:36 -0800125}
126
David Tolnay40c06112018-08-30 16:14:50 -0700127impl<T: Parse> ParseQuote for T {
128 fn parse(input: ParseStream) -> Result<Self> {
129 <T as Parse>::parse(input)
David Tolnaye83ef5a2018-01-11 15:18:36 -0800130 }
131}
132
133////////////////////////////////////////////////////////////////////////////////
134// Any other types that we want `parse_quote!` to be able to parse.
135
David Tolnay70f30e92018-09-01 02:04:17 -0700136use punctuated::Punctuated;
David Tolnaye83ef5a2018-01-11 15:18:36 -0800137#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay40c06112018-08-30 16:14:50 -0700138use {attr, Attribute};
David Tolnaye83ef5a2018-01-11 15:18:36 -0800139
David Tolnaye83ef5a2018-01-11 15:18:36 -0800140#[cfg(any(feature = "full", feature = "derive"))]
141impl ParseQuote for Attribute {
David Tolnay40c06112018-08-30 16:14:50 -0700142 fn parse(input: ParseStream) -> Result<Self> {
143 if input.peek(Token![#]) && input.peek2(Token![!]) {
144 attr::parsing::single_parse_inner(input)
145 } else {
146 attr::parsing::single_parse_outer(input)
147 }
David Tolnaye83ef5a2018-01-11 15:18:36 -0800148 }
149}
David Tolnay024b5762018-08-31 11:11:53 -0700150
151impl<T: Parse, P: Parse> ParseQuote for Punctuated<T, P> {
152 fn parse(input: ParseStream) -> Result<Self> {
153 Self::parse_terminated(input)
154 }
155}