Rewrite tokenization with `proc-macro2` tokens
This ended up being a bit larger of a commit than I intended! I imagine that
this'll be one of the larger of the commits working towards #142. The purpose of
this commit is to use an updated version of the `quote` crate which doesn't work
with strings but rather works with tokens form the `proc-macro2` crate. The
`proc-macro2` crate itself is based on the proposed API for `proc_macro` itself,
and will continue to mirror it. The hope is that we'll flip an easy switch
eventually to use compiler tokens, whereas for now we'll stick to string parsing
at the lowest layer.
The largest change here is the addition of span information to the AST. Building
on the previous PRs to refactor the AST this makes it relatively easy from a
user perspective to digest and use the AST still, it's just a few extra fields
on the side. The fallout from this was then quite large throughout the
`printing` feature of the crate. The `parsing`, `fold`, and `visit` features
then followed suit to get updated as well.
This commit also changes the the semantics of the AST somewhat as well.
Previously it was inferred what tokens should be printed, for example if you
have a closure argument `syn` would automatically not print the colon in `a: b`
if the type listed was "infer this type". Now the colon is a separate field and
must be in sync with the type listed as the colon/type will be printed
unconditionally (emitting no output if both are `None`).
diff --git a/synom/src/lib.rs b/synom/src/lib.rs
index 97c7833..0c90ece 100644
--- a/synom/src/lib.rs
+++ b/synom/src/lib.rs
@@ -23,12 +23,19 @@
extern crate unicode_xid;
+#[cfg(feature = "printing")]
+extern crate quote;
+
+#[cfg(feature = "parsing")]
#[doc(hidden)]
pub mod space;
+#[cfg(feature = "parsing")]
#[doc(hidden)]
pub mod helper;
+pub mod delimited;
+
/// The result of a parser.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IResult<I, O> {
@@ -49,9 +56,10 @@
///
/// use syn::Ty;
/// use syn::parse::ty;
+ /// use synom::delimited::Delimited;
///
/// // One or more Rust types separated by commas.
- /// named!(comma_separated_types -> Vec<Ty>,
+ /// named!(comma_separated_types -> Delimited<Ty, &str>,
/// separated_nonempty_list!(punct!(","), ty)
/// );
///
@@ -88,8 +96,9 @@
/// # #[macro_use] extern crate synom;
/// # use syn::Ty;
/// # use syn::parse::ty;
+/// # use synom::delimited::Delimited;
/// // One or more Rust types separated by commas.
-/// named!(pub comma_separated_types -> Vec<Ty>,
+/// named!(pub comma_separated_types -> Delimited<Ty, &str>,
/// separated_nonempty_list!(punct!(","), ty)
/// );
/// # fn main() {}
@@ -255,7 +264,11 @@
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
-/// use syn::parse::boolean;
+/// named!(boolean -> bool, alt!(
+/// keyword!("true") => { |_| true }
+/// |
+/// keyword!("false") => { |_| false }
+/// ));
///
/// // Parses a tuple of booleans like `(true, false, false)`, possibly with a
/// // dotdot indicating omitted values like `(true, true, .., true)`. Returns
@@ -278,7 +291,7 @@
/// // Allow trailing comma if there is no dotdot but there are elements.
/// cond!(!before.is_empty() && after.is_none(), option!(punct!(","))) >>
/// punct!(")") >>
-/// (before, after)
+/// (before.into_vec(), after)
/// ));
///
/// fn main() {
@@ -332,14 +345,18 @@
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
-/// use syn::parse::boolean;
-///
/// #[derive(Debug, PartialEq)]
/// struct VariadicBools {
/// data: Vec<bool>,
/// variadic: bool,
/// }
///
+/// named!(boolean -> bool, alt!(
+/// keyword!("true") => { |_| true }
+/// |
+/// keyword!("false") => { |_| false }
+/// ));
+///
/// // Parse one or more comma-separated booleans, possibly ending in "..." to
/// // indicate there may be more.
/// named!(variadic_bools -> VariadicBools, do_parse!(
@@ -358,7 +375,7 @@
/// // Gives `Some("...")` for variadic and `None` otherwise. Perfect!
/// variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
/// (VariadicBools {
-/// data: data,
+/// data: data.into_vec(),
/// variadic: variadic.is_some(),
/// })
/// ));
@@ -706,7 +723,7 @@
/// extern crate syn;
/// #[macro_use] extern crate synom;
///
-/// use syn::StrLit;
+/// use syn::Lit;
/// use syn::parse::string;
/// use synom::IResult;
///
@@ -714,7 +731,7 @@
/// named!(owned_string -> String,
/// map!(
/// terminated!(string, tag!("s")),
-/// |lit: StrLit| lit.value
+/// |lit: Lit| lit.to_string()
/// )
/// );
///
@@ -931,9 +948,10 @@
///
/// use syn::Ty;
/// use syn::parse::ty;
+/// use synom::delimited::Delimited;
///
/// // One or more Rust types separated by commas.
-/// named!(comma_separated_types -> Vec<Ty>,
+/// named!(comma_separated_types -> Delimited<Ty, &str>,
/// separated_nonempty_list!(punct!(","), ty)
/// );
///
@@ -949,7 +967,7 @@
#[macro_export]
macro_rules! separated_nonempty_list {
($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
- let mut res = ::std::vec::Vec::new();
+ let mut res = $crate::delimited::Delimited::new();
let mut input = $i;
// get the first element
@@ -959,19 +977,19 @@
if i.len() == input.len() {
$crate::IResult::Error
} else {
- res.push(o);
input = i;
+ res.push_first(o);
- while let $crate::IResult::Done(i2, _) = $sep!(input, $($args)*) {
+ while let $crate::IResult::Done(i2, s) = $sep!(input, $($args)*) {
if i2.len() == input.len() {
break;
}
if let $crate::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) {
+ res.push_next(o3, s);
if i3.len() == i2.len() {
break;
}
- res.push(o3);
input = i3;
} else {
break;