diff options
Diffstat (limited to 'src/parse_macro_input.rs')
-rw-r--r-- | src/parse_macro_input.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/parse_macro_input.rs b/src/parse_macro_input.rs index c8fc1cea..79c0de40 100644 --- a/src/parse_macro_input.rs +++ b/src/parse_macro_input.rs @@ -46,6 +46,42 @@ /// /// <br> /// +/// # Usage with Parser +/// +/// This macro can also be used with the [`Parser` trait] for types that have +/// multiple ways that they can be parsed. +/// +/// [`Parser` trait]: crate::rustdoc_workaround::parse_module::Parser +/// +/// ``` +/// # extern crate proc_macro; +/// # +/// # use proc_macro::TokenStream; +/// # use syn::{parse_macro_input, Result}; +/// # use syn::parse::ParseStream; +/// # +/// # struct MyMacroInput {} +/// # +/// impl MyMacroInput { +/// fn parse_alternate(input: ParseStream) -> Result<Self> { +/// /* ... */ +/// # Ok(MyMacroInput {}) +/// } +/// } +/// +/// # const IGNORE: &str = stringify! { +/// #[proc_macro] +/// # }; +/// pub fn my_macro(tokens: TokenStream) -> TokenStream { +/// let input = parse_macro_input!(tokens with MyMacroInput::parse_alternate); +/// +/// /* ... */ +/// # "".parse().unwrap() +/// } +/// ``` +/// +/// <br> +/// /// # Expansion /// /// `parse_macro_input!($variable as $Type)` expands to something like: @@ -77,6 +113,14 @@ macro_rules! parse_macro_input { } } }; + ($tokenstream:ident with $parser:path) => { + match $crate::parse::Parser::parse($parser, $tokenstream) { + $crate::export::Ok(data) => data, + $crate::export::Err(err) => { + return $crate::export::TokenStream::from(err.to_compile_error()); + } + } + }; ($tokenstream:ident) => { $crate::parse_macro_input!($tokenstream as _) }; |