Syn error experiment
diff --git a/src/ast.rs b/src/ast.rs
new file mode 100644
index 0000000..2c51a80
--- /dev/null
+++ b/src/ast.rs
@@ -0,0 +1,102 @@
+use proc_macro2::Ident;
+
+use parse::{Parse, ParseStream, Result};
+use token;
+
+/// Things that can appear directly inside of a module or scope.
+#[derive(Debug)]
+pub enum Item {
+ Struct(ItemStruct),
+ Enum(ItemEnum),
+}
+
+/// A struct definition: `struct S { a: A, b: B }`.
+#[derive(Debug)]
+pub struct ItemStruct {
+ pub struct_token: Token![struct],
+ pub ident: Ident,
+ pub brace_token: token::Brace,
+ pub fields: Vec<Field>,
+}
+
+/// An enum definition: `enum E { A, B, C }`.
+#[derive(Debug)]
+pub struct ItemEnum {
+ pub enum_token: Token![enum],
+ pub ident: Ident,
+ pub brace_token: token::Brace,
+ pub variants: Vec<Variant>,
+}
+
+/// A named field of a braced struct.
+#[derive(Debug)]
+pub struct Field {
+ pub name: Ident,
+ pub colon_token: Token![:],
+ pub ty: Ident,
+ pub comma_token: Token![,],
+}
+
+/// An enum variant.
+#[derive(Debug)]
+pub struct Variant {
+ pub name: Ident,
+ pub comma_token: token::Comma,
+}
+
+impl Parse for Item {
+ fn parse(input: ParseStream) -> Result<Self> {
+ let lookahead = input.lookahead1();
+ if lookahead.peek(Token![struct]) {
+ input.parse().map(Item::Struct)
+ } else if lookahead.peek(Token![enum]) {
+ input.parse().map(Item::Enum)
+ } else {
+ Err(lookahead.error())
+ }
+ }
+}
+
+impl Parse for ItemStruct {
+ fn parse(input: ParseStream) -> Result<Self> {
+ let content;
+ Ok(ItemStruct {
+ struct_token: input.parse()?,
+ ident: input.parse()?,
+ brace_token: braced!(content in input),
+ fields: content.parse()?,
+ })
+ }
+}
+
+impl Parse for ItemEnum {
+ fn parse(input: ParseStream) -> Result<Self> {
+ let content;
+ Ok(ItemEnum {
+ enum_token: input.parse()?,
+ ident: input.parse()?,
+ brace_token: braced!(content in input),
+ variants: content.parse()?,
+ })
+ }
+}
+
+impl Parse for Field {
+ fn parse(input: ParseStream) -> Result<Self> {
+ Ok(Field {
+ name: input.parse()?,
+ colon_token: input.parse()?,
+ ty: input.parse()?,
+ comma_token: input.parse()?,
+ })
+ }
+}
+
+impl Parse for Variant {
+ fn parse(input: ParseStream) -> Result<Self> {
+ Ok(Variant {
+ name: input.parse()?,
+ comma_token: input.parse()?,
+ })
+ }
+}
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..92ac21c
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,86 @@
+use std;
+use std::fmt::{self, Display};
+use std::iter::FromIterator;
+
+use proc_macro2::{
+ Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
+};
+
+use syn::buffer::Cursor;
+
+/// The result of a Syn parser.
+pub type Result<T> = std::result::Result<T, Error>;
+
+/// Error returned when a Syn parser cannot parse the input tokens.
+#[derive(Debug)]
+pub struct Error {
+ span: Span,
+ message: String,
+}
+
+impl Error {
+ pub fn new<T: Display>(span: Span, message: T) -> Self {
+ Error {
+ span: span,
+ message: message.to_string(),
+ }
+ }
+
+ /// Render the error as an invocation of [`compile_error!`].
+ ///
+ /// The [`parse_macro_input!`] macro provides a convenient way to invoke
+ /// this method correctly in a procedural macro.
+ ///
+ /// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
+ /// [`parse_macro_input!`]: ../macro.parse_macro_input.html
+ pub fn into_compile_error(self) -> TokenStream {
+ // compile_error!($message)
+ TokenStream::from_iter(vec![
+ TokenTree::Ident(Ident::new("compile_error", self.span)),
+ TokenTree::Punct({
+ let mut punct = Punct::new('!', Spacing::Alone);
+ punct.set_span(self.span);
+ punct
+ }),
+ TokenTree::Group({
+ let mut group = Group::new(Delimiter::Brace, {
+ TokenStream::from_iter(vec![TokenTree::Literal({
+ let mut string = Literal::string(&self.message);
+ string.set_span(self.span);
+ string
+ })])
+ });
+ group.set_span(self.span);
+ group
+ }),
+ ])
+ }
+}
+
+// Not public API.
+#[doc(hidden)]
+pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error {
+ if cursor.eof() {
+ Error::new(scope, format!("unexpected end of input, {}", message))
+ } else {
+ Error::new(cursor.span(), message)
+ }
+}
+
+impl Display for Error {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+ formatter.write_str(&self.message)
+ }
+}
+
+impl std::error::Error for Error {
+ fn description(&self) -> &str {
+ "parse error"
+ }
+}
+
+impl From<LexError> for Error {
+ fn from(err: LexError) -> Self {
+ Error::new(Span::call_site(), format!("{:?}", err))
+ }
+}
diff --git a/src/export.rs b/src/export.rs
new file mode 100644
index 0000000..cceed14
--- /dev/null
+++ b/src/export.rs
@@ -0,0 +1,4 @@
+pub use std::result::Result::{Err, Ok};
+
+#[cfg(feature = "proc-macro")]
+pub use proc_macro::TokenStream;
diff --git a/src/group.rs b/src/group.rs
new file mode 100644
index 0000000..da8a7fc
--- /dev/null
+++ b/src/group.rs
@@ -0,0 +1,76 @@
+use proc_macro2::Delimiter;
+
+use parse::{ParseBuffer, Result};
+use token;
+
+pub struct Braces<'a> {
+ pub token: token::Brace,
+ pub content: ParseBuffer<'a>,
+}
+
+impl<'a> ParseBuffer<'a> {
+ // Not public API.
+ #[doc(hidden)]
+ pub fn parse_braces(&self) -> Result<Braces<'a>> {
+ self.step_cursor(|cursor| {
+ if let Some((content, span, rest)) = cursor.group(Delimiter::Brace) {
+ let braces = Braces {
+ token: token::Brace(span),
+ content: ParseBuffer::new(span, cursor.advance(content)),
+ };
+ Ok((braces, rest))
+ } else {
+ Err(cursor.error("expected curly braces"))
+ }
+ })
+ }
+}
+
+/// Parse a set of curly braces and expose their content to subsequent parsers.
+///
+/// ```rust
+/// # extern crate syn_error_experiment;
+/// #
+/// use syn_error_experiment::{braced, token, Ident, Token};
+/// use syn_error_experiment::parse::{Parse, ParseStream, Result};
+/// # use syn_error_experiment::Field;
+///
+/// // Parse a simplified struct syntax like:
+/// //
+/// // struct S {
+/// // a: A,
+/// // b: B,
+/// // }
+/// struct Struct {
+/// pub struct_token: Token![struct],
+/// pub ident: Ident,
+/// pub brace_token: token::Brace,
+/// pub fields: Vec<Field>,
+/// }
+///
+/// impl Parse for Struct {
+/// fn parse(input: ParseStream) -> Result<Self> {
+/// let content;
+/// Ok(Struct {
+/// struct_token: input.parse()?,
+/// ident: input.parse()?,
+/// brace_token: braced!(content in input),
+/// fields: content.parse()?,
+/// })
+/// }
+/// }
+/// ```
+#[macro_export]
+macro_rules! braced {
+ ($content:ident in $cursor:expr) => {
+ match $crate::parse::ParseBuffer::parse_braces(&$cursor) {
+ $crate::export::Ok(braces) => {
+ $content = braces.content;
+ braces.token
+ }
+ $crate::export::Err(error) => {
+ return $crate::export::Err(error);
+ }
+ }
+ };
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..b8807d5
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,102 @@
+#![doc(html_root_url = "https://docs.rs/syn-error-experiment/0.0.0")]
+
+extern crate proc_macro2;
+extern crate syn;
+
+#[cfg(feature = "proc-macro")]
+extern crate proc_macro;
+
+#[macro_use]
+pub mod token;
+
+#[macro_use]
+pub mod parse;
+
+#[macro_use]
+mod group;
+
+mod ast;
+mod error;
+mod lookahead;
+
+pub use ast::*;
+pub use proc_macro2::Ident;
+
+// Not public API.
+#[doc(hidden)]
+pub mod export;
+
+use std::str::FromStr;
+
+use proc_macro2::Span;
+use syn::buffer::TokenBuffer;
+
+use parse::{Parse, ParseBuffer, Result};
+
+/// 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 state = ParseBuffer::new(Span::call_site(), buf.begin());
+ T::parse(&state)
+}
+
+/// 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_error_experiment;
+/// #
+/// use proc_macro::TokenStream;
+/// use syn_error_experiment::parse_macro_input;
+/// use syn_error_experiment::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::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/lookahead.rs b/src/lookahead.rs
new file mode 100644
index 0000000..3b86c47
--- /dev/null
+++ b/src/lookahead.rs
@@ -0,0 +1,74 @@
+use std::cell::RefCell;
+
+use proc_macro2::Span;
+use syn::buffer::Cursor;
+
+use error;
+use parse::Error;
+use token::Token;
+
+/// Support for checking the next token in a stream to decide how to parse.
+///
+/// Use [`ParseStream::lookahead1`] to construct this object.
+///
+/// [`ParseStream::lookahead1`]: struct.ParseBuffer.html#method.lookahead1
+pub struct Lookahead1<'a> {
+ scope: Span,
+ cursor: Cursor<'a>,
+ comparisons: RefCell<Vec<String>>,
+}
+
+impl<'a> Lookahead1<'a> {
+ // Not public API.
+ #[doc(hidden)]
+ pub fn new(scope: Span, cursor: Cursor<'a>) -> Self {
+ Lookahead1 {
+ scope: scope,
+ cursor: cursor,
+ comparisons: RefCell::new(Vec::new()),
+ }
+ }
+
+ pub fn peek<T: Peek>(&self, token: T) -> bool {
+ let _ = token;
+ if T::Token::peek(self) {
+ return true;
+ }
+ self.comparisons.borrow_mut().push(T::Token::display());
+ false
+ }
+
+ pub fn error(self) -> Error {
+ let message = format!("expected one of {:?}", self.comparisons.borrow());
+ error::new_at(self.scope, self.cursor, message)
+ }
+}
+
+/// Types that can be parsed by looking at just one token.
+///
+/// This trait is sealed and cannot be implemented for types outside of Syn.
+pub trait Peek: private::Sealed {
+ // Not public API.
+ #[doc(hidden)]
+ type Token: Token;
+}
+
+impl<F: FnOnce(Span) -> T, T: Token> Peek for F {
+ type Token = T;
+}
+
+// Not public API.
+#[doc(hidden)]
+pub fn is_token(lookahead: &Lookahead1, repr: &'static str) -> bool {
+ if let Some((token, _rest)) = lookahead.cursor.token_tree() {
+ token.to_string() == repr
+ } else {
+ false
+ }
+}
+
+mod private {
+ use super::{Span, Token};
+ pub trait Sealed {}
+ impl<F, T: Token> Sealed for F where F: FnOnce(Span) -> T {}
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..e6f0840
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,11 @@
+extern crate syn_error_experiment;
+
+use syn_error_experiment::*;
+
+fn main() {
+ let input = "struct S { a: A, b: B, }";
+ println!("{:#?}", parse_str::<Item>(input).unwrap());
+
+ let input = "enum E { A, B, }";
+ println!("{:#?}", parse_str::<Item>(input).unwrap());
+}
diff --git a/src/parse.rs b/src/parse.rs
new file mode 100644
index 0000000..922da08
--- /dev/null
+++ b/src/parse.rs
@@ -0,0 +1,135 @@
+//! Parsing interface for parsing a token stream into a syntax tree node.
+
+use std::cell::Cell;
+use std::fmt::Display;
+use std::marker::PhantomData;
+use std::mem;
+use std::ops::Deref;
+
+use proc_macro2::{Ident, Span};
+use syn::buffer::Cursor;
+
+use error;
+
+pub use error::{Error, Result};
+pub use lookahead::{Lookahead1, Peek};
+
+/// Parsing interface implemented by all types that can be parsed in a default
+/// way from a token stream.
+pub trait Parse: Sized {
+ fn parse(input: ParseStream) -> Result<Self>;
+}
+
+/// Input to a Syn parser function.
+pub type ParseStream<'a> = &'a ParseBuffer<'a>;
+
+/// Cursor position within a buffered token stream.
+#[derive(Clone)]
+pub struct ParseBuffer<'a> {
+ scope: Span,
+ cell: Cell<Cursor<'static>>,
+ marker: PhantomData<Cursor<'a>>,
+}
+
+// Not public API.
+#[doc(hidden)]
+#[derive(Copy, Clone)]
+pub struct StepCursor<'c, 'a> {
+ scope: Span,
+ cursor: Cursor<'c>,
+ marker: PhantomData<fn(Cursor<'c>) -> Cursor<'a>>,
+}
+
+impl<'c, 'a> Deref for StepCursor<'c, 'a> {
+ type Target = Cursor<'c>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.cursor
+ }
+}
+
+impl<'c, 'a> StepCursor<'c, 'a> {
+ // Not public API.
+ #[doc(hidden)]
+ pub fn advance(self, other: Cursor<'c>) -> Cursor<'a> {
+ unsafe { mem::transmute::<Cursor<'c>, Cursor<'a>>(other) }
+ }
+
+ // Not public API.
+ #[doc(hidden)]
+ pub fn error<T: Display>(self, message: T) -> Error {
+ error::new_at(self.scope, self.cursor, message)
+ }
+}
+
+impl<'a> ParseBuffer<'a> {
+ // Not public API.
+ #[doc(hidden)]
+ pub fn new(scope: Span, cursor: Cursor<'a>) -> Self {
+ let extend = unsafe { mem::transmute::<Cursor<'a>, Cursor<'static>>(cursor) };
+ ParseBuffer {
+ scope: scope,
+ cell: Cell::new(extend),
+ marker: PhantomData,
+ }
+ }
+
+ pub fn cursor(&self) -> Cursor<'a> {
+ self.cell.get()
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.cursor().eof()
+ }
+
+ pub fn lookahead1(&self) -> Lookahead1<'a> {
+ Lookahead1::new(self.scope, self.cursor())
+ }
+
+ pub fn parse<T: Parse>(&self) -> Result<T> {
+ T::parse(self)
+ }
+
+ // Not public API.
+ #[doc(hidden)]
+ pub fn step_cursor<F, R>(&self, function: F) -> Result<R>
+ where
+ F: for<'c> FnOnce(StepCursor<'c, 'a>) -> Result<(R, Cursor<'c>)>,
+ {
+ match function(StepCursor {
+ scope: self.scope,
+ cursor: self.cell.get(),
+ marker: PhantomData,
+ }) {
+ Ok((ret, cursor)) => {
+ self.cell.set(cursor);
+ Ok(ret)
+ }
+ Err(err) => Err(err),
+ }
+ }
+}
+
+impl Parse for Ident {
+ fn parse(input: ParseStream) -> Result<Self> {
+ input.step_cursor(|cursor| {
+ if let Some((ident, rest)) = cursor.ident() {
+ Ok((ident, rest))
+ } else {
+ Err(cursor.error("expected identifier"))
+ }
+ })
+ }
+}
+
+// In reality the impl would be for Punctuated.
+impl<T: Parse> Parse for Vec<T> {
+ fn parse(input: ParseStream) -> Result<Self> {
+ let mut vec = Vec::new();
+ while !input.is_empty() {
+ let t = input.parse::<T>()?;
+ vec.push(t);
+ }
+ Ok(vec)
+ }
+}
diff --git a/src/token.rs b/src/token.rs
new file mode 100644
index 0000000..31002d5
--- /dev/null
+++ b/src/token.rs
@@ -0,0 +1,118 @@
+//! Tokens representing Rust punctuation, keywords, and delimiters.
+
+use proc_macro2::Span;
+
+use parse::{Lookahead1, Parse, ParseStream, Result};
+
+/// Marker trait for types that represent single tokens.
+///
+/// This trait is sealed and cannot be implemented for types outside of Syn.
+pub trait Token: private::Sealed {
+ // Not public API.
+ #[doc(hidden)]
+ fn peek(lookahead: &Lookahead1) -> bool;
+
+ // Not public API.
+ #[doc(hidden)]
+ fn display() -> String;
+}
+
+mod private {
+ pub trait Sealed {}
+}
+
+/// A type-macro that expands to the name of the Rust type representation of a
+/// given token.
+#[macro_export]
+#[cfg_attr(rustfmt, rustfmt_skip)]
+macro_rules! Token {
+ (struct) => { $crate::token::Struct };
+ (enum) => { $crate::token::Enum };
+ (:) => { $crate::token::Colon };
+ (,) => { $crate::token::Comma };
+}
+
+macro_rules! define_token {
+ ($token:tt $name:ident #[$doc:meta]) => {
+ #[$doc]
+ #[derive(Debug)]
+ pub struct $name(pub Span);
+
+ impl Token for $name {
+ fn peek(lookahead: &Lookahead1) -> bool {
+ ::lookahead::is_token(lookahead, $token)
+ }
+
+ fn display() -> String {
+ concat!("`", $token, "`").to_owned()
+ }
+ }
+
+ impl private::Sealed for $name {}
+ };
+}
+
+macro_rules! define_keywords {
+ ($($token:tt $name:ident #[$doc:meta])*) => {
+ $(
+ define_token!($token $name #[$doc]);
+
+ impl Parse for $name {
+ fn parse(input: ParseStream) -> Result<Self> {
+ parse_keyword(input, $token).map($name)
+ }
+ }
+ )*
+ };
+}
+
+macro_rules! define_punctuation {
+ ($($token:tt $name:ident #[$doc:meta])*) => {
+ $(
+ define_token!($token $name #[$doc]);
+
+ impl Parse for $name {
+ fn parse(input: ParseStream) -> Result<Self> {
+ parse_punctuation(input, $token).map($name)
+ }
+ }
+ )*
+ };
+}
+
+define_keywords! {
+ "struct" Struct /// `struct`
+ "enum" Enum /// `enum`
+}
+
+define_punctuation! {
+ ":" Colon /// `:`
+ "," Comma /// `,`
+}
+
+/// `{...}`
+#[derive(Debug)]
+pub struct Brace(pub Span);
+
+fn parse_keyword(input: ParseStream, token: &str) -> Result<Span> {
+ input.step_cursor(|cursor| {
+ if let Some((ident, rest)) = cursor.ident() {
+ if ident == token {
+ return Ok((ident.span(), rest));
+ }
+ }
+ Err(cursor.error(format!("expected `{}`", token)))
+ })
+}
+
+fn parse_punctuation(input: ParseStream, token: &str) -> Result<Span> {
+ input.step_cursor(|cursor| {
+ // TODO: support multi-character punctuation
+ if let Some((punct, rest)) = cursor.punct() {
+ if punct.as_char() == token.chars().next().unwrap() {
+ return Ok((punct.span(), rest));
+ }
+ }
+ Err(cursor.error(format!("expected `{}`", token)))
+ })
+}