David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 1 | #![doc(html_root_url = "https://docs.rs/syn-error-experiment/0.0.0")] |
| 2 | |
| 3 | extern crate proc_macro2; |
| 4 | extern crate syn; |
| 5 | |
| 6 | #[cfg(feature = "proc-macro")] |
| 7 | extern crate proc_macro; |
| 8 | |
| 9 | #[macro_use] |
| 10 | pub mod token; |
| 11 | |
| 12 | #[macro_use] |
| 13 | pub mod parse; |
| 14 | |
| 15 | #[macro_use] |
| 16 | mod group; |
| 17 | |
| 18 | mod ast; |
| 19 | mod error; |
| 20 | mod lookahead; |
David Tolnay | 2b687b0 | 2018-08-24 13:36:36 -0400 | [diff] [blame^] | 21 | mod span; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 22 | |
| 23 | pub use ast::*; |
| 24 | pub use proc_macro2::Ident; |
| 25 | |
| 26 | // Not public API. |
| 27 | #[doc(hidden)] |
| 28 | pub mod export; |
| 29 | |
| 30 | use std::str::FromStr; |
| 31 | |
| 32 | use proc_macro2::Span; |
| 33 | use syn::buffer::TokenBuffer; |
| 34 | |
| 35 | use parse::{Parse, ParseBuffer, Result}; |
| 36 | |
| 37 | /// Parse tokens of source code into the chosen syntax tree node. |
| 38 | #[cfg(feature = "proc-macro")] |
| 39 | pub fn parse<T: Parse>(input: proc_macro::TokenStream) -> Result<T> { |
| 40 | parse2(proc_macro2::TokenStream::from(input)) |
| 41 | } |
| 42 | |
| 43 | /// Parse a proc-macro2 token stream into the chosen syntax tree node. |
| 44 | pub fn parse2<T: Parse>(input: proc_macro2::TokenStream) -> Result<T> { |
| 45 | let buf = TokenBuffer::new2(input); |
| 46 | let state = ParseBuffer::new(Span::call_site(), buf.begin()); |
| 47 | T::parse(&state) |
| 48 | } |
| 49 | |
| 50 | /// Parse a string of Rust code into the chosen syntax tree node. |
| 51 | pub fn parse_str<T: Parse>(input: &str) -> Result<T> { |
| 52 | let tokens = proc_macro2::TokenStream::from_str(input)?; |
| 53 | parse2(tokens) |
| 54 | } |
| 55 | |
| 56 | /// Parse the input TokenStream of a macro, triggering a compile error if the |
| 57 | /// tokens fail to parse. |
| 58 | /// |
| 59 | /// # Intended usage |
| 60 | /// |
| 61 | /// ```rust |
| 62 | /// # extern crate proc_macro; |
| 63 | /// # extern crate syn_error_experiment; |
| 64 | /// # |
| 65 | /// use proc_macro::TokenStream; |
| 66 | /// use syn_error_experiment::parse_macro_input; |
| 67 | /// use syn_error_experiment::parse::{Parse, ParseStream, Result}; |
| 68 | /// |
| 69 | /// struct MyMacroInput { |
| 70 | /// /* ... */ |
| 71 | /// } |
| 72 | /// |
| 73 | /// impl Parse for MyMacroInput { |
| 74 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 75 | /// /* ... */ |
| 76 | /// # Ok(MyMacroInput {}) |
| 77 | /// } |
| 78 | /// } |
| 79 | /// |
| 80 | /// # const IGNORE: &str = stringify! { |
| 81 | /// #[proc_macro] |
| 82 | /// # }; |
| 83 | /// pub fn my_macro(tokens: TokenStream) -> TokenStream { |
| 84 | /// let input = parse_macro_input!(tokens as MyMacroInput); |
| 85 | /// |
| 86 | /// /* ... */ |
| 87 | /// # "".parse().unwrap() |
| 88 | /// } |
| 89 | /// # |
| 90 | /// # fn main() {} |
| 91 | /// ``` |
| 92 | #[cfg(feature = "proc-macro")] |
| 93 | #[macro_export] |
| 94 | macro_rules! parse_macro_input { |
| 95 | ($tokenstream:ident as $ty:ty) => { |
| 96 | match $crate::parse::<$ty>($tokenstream) { |
| 97 | $crate::export::Ok(data) => data, |
| 98 | $crate::export::Err(err) => { |
| 99 | return $crate::export::TokenStream::from(err.into_compile_error()); |
| 100 | } |
| 101 | }; |
| 102 | }; |
| 103 | } |