Merge next.rs into other modules
diff --git a/src/lib.rs b/src/lib.rs
index 51a7952..0604e67 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -583,9 +583,6 @@
pub mod export;
#[cfg(feature = "parsing")]
-pub mod next;
-
-#[cfg(feature = "parsing")]
mod lookahead;
#[cfg(feature = "parsing")]
@@ -596,9 +593,7 @@
////////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "parsing")]
-use proc_macro2::Span;
-#[cfg(feature = "parsing")]
-use synom::{Parser, Synom};
+use synom::Parser;
#[cfg(feature = "parsing")]
mod error;
@@ -663,7 +658,7 @@
feature = "proc-macro"
))]
pub fn parse<T: parse::Parse>(tokens: proc_macro::TokenStream) -> Result<T, Error> {
- parse2(tokens.into())
+ T::parse.parse(tokens)
}
/// Parse a proc-macro2 token stream into the chosen syntax tree node.
@@ -680,11 +675,7 @@
/// *This function is available if Syn is built with the `"parsing"` feature.*
#[cfg(feature = "parsing")]
pub fn parse2<T: parse::Parse>(tokens: proc_macro2::TokenStream) -> Result<T, Error> {
- let parser = T::parse;
- parser.parse2(tokens).map_err(|err| match T::description() {
- Some(s) => Error::new(Span::call_site(), format!("failed to parse {}: {}", s, err)),
- None => err,
- })
+ T::parse.parse2(tokens)
}
/// Parse a string of Rust code into the chosen syntax tree node.
@@ -717,13 +708,7 @@
/// ```
#[cfg(feature = "parsing")]
pub fn parse_str<T: parse::Parse>(s: &str) -> Result<T, Error> {
- match s.parse() {
- Ok(tts) => parse2(tts),
- Err(_) => Err(Error::new(
- Span::call_site(),
- "error while lexing input string",
- )),
- }
+ T::parse.parse_str(s)
}
// FIXME the name parse_file makes it sound like you might pass in a path to a
@@ -790,6 +775,55 @@
Ok(file)
}
+/// Parse the input TokenStream of a macro, triggering a compile error if the
+/// tokens fail to parse.
+///
+/// # Intended usage
+///
+/// ```rust
+/// # extern crate proc_macro;
+/// # extern crate syn;
+/// #
+/// use proc_macro::TokenStream;
+/// use syn::parse_macro_input;
+/// use syn::parse::{Parse, ParseStream, Result};
+///
+/// struct MyMacroInput {
+/// /* ... */
+/// }
+///
+/// impl Parse for MyMacroInput {
+/// fn parse(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 as MyMacroInput);
+///
+/// /* ... */
+/// # "".parse().unwrap()
+/// }
+/// #
+/// # fn main() {}
+/// ```
+#[cfg(feature = "proc-macro")]
+#[macro_export]
+macro_rules! parse_macro_input {
+ ($tokenstream:ident as $ty:ty) => {
+ match $crate::next::parse::<$ty>($tokenstream) {
+ $crate::export::Ok(data) => data,
+ $crate::export::Err(err) => {
+ return $crate::export::TokenStream::from(err.into_compile_error());
+ }
+ };
+ };
+}
+
#[cfg(all(
any(feature = "full", feature = "derive"),
feature = "printing"
diff --git a/src/next.rs b/src/next.rs
deleted file mode 100644
index e5b902c..0000000
--- a/src/next.rs
+++ /dev/null
@@ -1,82 +0,0 @@
-use std::cell::Cell;
-use std::rc::Rc;
-use std::str::FromStr;
-
-use buffer::TokenBuffer;
-#[cfg(feature = "proc-macro")]
-use proc_macro;
-use proc_macro2::{self, Span};
-
-use error::Result;
-use parse::{Parse, ParseBuffer};
-
-/// Parse tokens of source code into the chosen syntax tree node.
-#[cfg(feature = "proc-macro")]
-pub fn parse<T: Parse>(input: proc_macro::TokenStream) -> Result<T> {
- parse2(proc_macro2::TokenStream::from(input))
-}
-
-/// Parse a proc-macro2 token stream into the chosen syntax tree node.
-pub fn parse2<T: Parse>(input: proc_macro2::TokenStream) -> Result<T> {
- let buf = TokenBuffer::new2(input);
- let unexpected = Rc::new(Cell::new(None));
- let state = ParseBuffer::new(Span::call_site(), buf.begin(), unexpected);
- let node = T::parse(&state)?;
- state.check_unexpected()?;
- Ok(node)
-}
-
-/// Parse a string of Rust code into the chosen syntax tree node.
-pub fn parse_str<T: Parse>(input: &str) -> Result<T> {
- let tokens = proc_macro2::TokenStream::from_str(input)?;
- parse2(tokens)
-}
-
-/// Parse the input TokenStream of a macro, triggering a compile error if the
-/// tokens fail to parse.
-///
-/// # Intended usage
-///
-/// ```rust
-/// # extern crate proc_macro;
-/// # extern crate syn;
-/// #
-/// use proc_macro::TokenStream;
-/// use syn::parse_macro_input;
-/// use syn::parse::{Parse, ParseStream, Result};
-///
-/// struct MyMacroInput {
-/// /* ... */
-/// }
-///
-/// impl Parse for MyMacroInput {
-/// fn parse(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 as MyMacroInput);
-///
-/// /* ... */
-/// # "".parse().unwrap()
-/// }
-/// #
-/// # fn main() {}
-/// ```
-#[cfg(feature = "proc-macro")]
-#[macro_export]
-macro_rules! parse_macro_input {
- ($tokenstream:ident as $ty:ty) => {
- match $crate::next::parse::<$ty>($tokenstream) {
- $crate::export::Ok(data) => data,
- $crate::export::Err(err) => {
- return $crate::export::TokenStream::from(err.into_compile_error());
- }
- };
- };
-}
diff --git a/src/synom.rs b/src/synom.rs
index e39a534..817d4b3 100644
--- a/src/synom.rs
+++ b/src/synom.rs
@@ -152,13 +152,14 @@
use std::cell::Cell;
use std::rc::Rc;
+use std::str::FromStr;
#[cfg(all(
not(all(target_arch = "wasm32", target_os = "unknown")),
feature = "proc-macro"
))]
use proc_macro;
-use proc_macro2::{Delimiter, Group, Literal, Punct, Span, TokenStream, TokenTree};
+use proc_macro2::{self, Delimiter, Group, Literal, Punct, Span, TokenStream, TokenTree};
use error::parse_error;
pub use error::{Error, PResult};
@@ -291,7 +292,7 @@
feature = "proc-macro"
))]
fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> {
- self.parse2(tokens.into())
+ self.parse2(proc_macro2::TokenStream::from(tokens))
}
/// Parse a string of Rust code into the chosen syntax tree node.
@@ -301,13 +302,7 @@
/// Every span in the resulting syntax tree will be set to resolve at the
/// macro call site.
fn parse_str(self, s: &str) -> Result<Self::Output> {
- match s.parse() {
- Ok(tts) => self.parse2(tts),
- Err(_) => Err(Error::new(
- Span::call_site(),
- "error while lexing input string",
- )),
- }
+ self.parse2(proc_macro2::TokenStream::from_str(s)?)
}
}