Syn error experiment
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b1c7301
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/Cargo.lock
+/target
+**/*.rs.bk
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..6dd6f14
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,21 @@
+[package]
+name = "syn-error-experiment"
+version = "0.0.0"
+authors = ["David Tolnay <dtolnay@gmail.com>"]
+license = "MIT/Apache-2.0"
+description = "Syn error experiment"
+repository = "https://github.com/dtolnay/syn/issues/47"
+documentation = "https://docs.rs/syn-error-experiment"
+categories = ["development-tools::procedural-macro-helpers"]
+
+[features]
+default = ["proc-macro"]
+proc-macro = ["proc-macro2/proc-macro", "syn/proc-macro"]
+
+[dependencies]
+proc-macro2 = { version = "0.4", default-features = false }
+
+[dependencies.syn]
+version = "0.14"
+default-features = false
+features = ["parsing"]
diff --git a/LICENSE-APACHE b/LICENSE-APACHE
new file mode 100644
index 0000000..16fe87b
--- /dev/null
+++ b/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..31aa793
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,23 @@
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
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)))
+ })
+}