Vendor a simplified nom
diff --git a/Cargo.toml b/Cargo.toml
index 78d399f..14b06a8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,10 +11,9 @@
[features]
default = ["parsing", "printing"]
aster = []
-parsing = ["nom"]
+parsing = []
printing = ["quote"]
visit = []
[dependencies]
-nom = { version = "1.2.4", optional = true }
quote = { version = "0.1.2", optional = true }
diff --git a/README.md b/README.md
index d550609..554f58c 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@
Designed for fast compile time.
-- Compile time for `syn` (from scratch including all dependencies): **6 seconds**
+- Compile time for `syn` (from scratch including all dependencies): **4 seconds**
- Compile time for the `syntex`/`quasi`/`aster` stack: **60+ seconds**
```toml
diff --git a/src/attr.rs b/src/attr.rs
index 20bf628..2cf9825 100644
--- a/src/attr.rs
+++ b/src/attr.rs
@@ -32,7 +32,7 @@
use ident::parsing::ident;
use nom::multispace;
- named!(pub attribute<&str, Attribute>, alt_complete!(
+ named!(pub attribute -> Attribute, alt!(
do_parse!(
punct!("#") >>
punct!("[") >>
@@ -47,7 +47,7 @@
do_parse!(
punct!("///") >>
space: multispace >>
- content: take_until_s!("\n") >>
+ content: take_until!("\n") >>
(Attribute {
value: MetaItem::NameValue(
"doc".into(),
@@ -58,7 +58,7 @@
)
));
- named!(meta_item<&str, MetaItem>, alt_complete!(
+ named!(meta_item -> MetaItem, alt!(
do_parse!(
id: ident >>
punct!("(") >>
@@ -77,10 +77,10 @@
map!(ident, MetaItem::Word)
));
- named!(quoted<&str, String>, delimited!(
+ named!(quoted -> String, delimited!(
punct!("\""),
escaped_string,
- tag_s!("\"")
+ tag!("\"")
));
}
diff --git a/src/do_parse.rs b/src/do_parse.rs
deleted file mode 100644
index 13ae44d..0000000
--- a/src/do_parse.rs
+++ /dev/null
@@ -1,104 +0,0 @@
-// Copied from nom master:
-// https://github.com/Geal/nom/blob/a38188f333c29d00c32a3082bec5491d2eefa33f/src/sequence.rs#L591-L687
-// Will be released in nom 2.0.
-
-#![cfg(feature = "parsing")]
-
-#[macro_export]
-#[doc(hidden)]
-macro_rules! do_parse (
- ($i:expr, $($rest:tt)*) => (
- {
- do_parse_impl!($i, 0usize, $($rest)*)
- }
- );
-);
-
-/// Internal parser, do not use directly
-#[doc(hidden)]
-#[macro_export]
-macro_rules! do_parse_impl (
-
- ($i:expr, $consumed:expr, ( $($rest:expr),* )) => (
- ::nom::IResult::Done($i, ( $($rest),* ))
- );
-
- ($i:expr, $consumed:expr, $e:ident >> $($rest:tt)*) => (
- do_parse_impl!($i, $consumed, call!($e) >> $($rest)*);
- );
- ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => (
- {
- match $submac!($i, $($args)*) {
- ::nom::IResult::Error(e) => ::nom::IResult::Error(e),
- ::nom::IResult::Incomplete(::nom::Needed::Unknown) =>
- ::nom::IResult::Incomplete(::nom::Needed::Unknown),
- ::nom::IResult::Incomplete(::nom::Needed::Size(i)) =>
- ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)),
- ::nom::IResult::Done(i,_) => {
- do_parse_impl!(i,
- $consumed + (::nom::InputLength::input_len(&($i)) -
- ::nom::InputLength::input_len(&i)), $($rest)*)
- },
- }
- }
- );
-
- ($i:expr, $consumed:expr, $field:ident : $e:ident >> $($rest:tt)*) => (
- do_parse_impl!($i, $consumed, $field: call!($e) >> $($rest)*);
- );
-
- ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => (
- {
- match $submac!($i, $($args)*) {
- ::nom::IResult::Error(e) => ::nom::IResult::Error(e),
- ::nom::IResult::Incomplete(::nom::Needed::Unknown) =>
- ::nom::IResult::Incomplete(::nom::Needed::Unknown),
- ::nom::IResult::Incomplete(::nom::Needed::Size(i)) =>
- ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)),
- ::nom::IResult::Done(i,o) => {
- let $field = o;
- do_parse_impl!(i,
- $consumed + (::nom::InputLength::input_len(&($i)) -
- ::nom::InputLength::input_len(&i)), $($rest)*)
- },
- }
- }
- );
-
- // ending the chain
- ($i:expr, $consumed:expr, $e:ident >> ( $($rest:tt)* )) => (
- do_parse_impl!($i, $consumed, call!($e) >> ( $($rest)* ));
- );
-
- ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => (
- match $submac!($i, $($args)*) {
- ::nom::IResult::Error(e) => ::nom::IResult::Error(e),
- ::nom::IResult::Incomplete(::nom::Needed::Unknown) =>
- ::nom::IResult::Incomplete(::nom::Needed::Unknown),
- ::nom::IResult::Incomplete(::nom::Needed::Size(i)) =>
- ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)),
- ::nom::IResult::Done(i,_) => {
- ::nom::IResult::Done(i, ( $($rest)* ))
- },
- }
- );
-
- ($i:expr, $consumed:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => (
- do_parse_impl!($i, $consumed, $field: call!($e) >> ( $($rest)* ) );
- );
-
- ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => (
- match $submac!($i, $($args)*) {
- ::nom::IResult::Error(e) => ::nom::IResult::Error(e),
- ::nom::IResult::Incomplete(::nom::Needed::Unknown) =>
- ::nom::IResult::Incomplete(::nom::Needed::Unknown),
- ::nom::IResult::Incomplete(::nom::Needed::Size(i)) =>
- ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)),
- ::nom::IResult::Done(i,o) => {
- let $field = o;
- ::nom::IResult::Done(i, ( $($rest)* ))
- },
- }
- );
-
-);
diff --git a/src/escape.rs b/src/escape.rs
index cd97364..2cdb647 100644
--- a/src/escape.rs
+++ b/src/escape.rs
@@ -1,7 +1,7 @@
#![cfg(feature = "parsing")]
use std::{char, str};
-use nom::{self, IResult};
+use nom::IResult;
pub fn escaped_string(input: &str) -> IResult<&str, String> {
let mut s = String::new();
@@ -45,7 +45,7 @@
}
}
}
- IResult::Error(nom::Err::Position(nom::ErrorKind::Escaped, input))
+ IResult::Error
}
macro_rules! next_char {
diff --git a/src/generics.rs b/src/generics.rs
index 8fceaca..68968e1 100644
--- a/src/generics.rs
+++ b/src/generics.rs
@@ -80,8 +80,8 @@
use ty::parsing::{ty, poly_trait_ref};
use nom::multispace;
- named!(pub generics<&str, Generics>, do_parse!(
- bracketed: alt_complete!(
+ named!(pub generics -> Generics, do_parse!(
+ bracketed: alt!(
do_parse!(
punct!("<") >>
lifetimes: separated_list!(punct!(","), lifetime_def) >>
@@ -103,14 +103,14 @@
})
));
- named!(pub lifetime<&str, Lifetime>, preceded!(
+ named!(pub lifetime -> Lifetime, preceded!(
punct!("'"),
map!(ident, |id| Lifetime {
ident: format!("'{}", id).into(),
})
));
- named!(pub lifetime_def<&str, LifetimeDef>, do_parse!(
+ named!(pub lifetime_def -> LifetimeDef, do_parse!(
life: lifetime >>
bounds: opt_vec!(preceded!(
punct!(":"),
@@ -122,7 +122,7 @@
})
));
- named!(pub bound_lifetimes<&str, Vec<LifetimeDef> >, opt_vec!(do_parse!(
+ named!(pub bound_lifetimes -> Vec<LifetimeDef>, opt_vec!(do_parse!(
punct!("for") >>
punct!("<") >>
lifetimes: separated_list!(punct!(","), lifetime_def) >>
@@ -130,7 +130,7 @@
(lifetimes)
)));
- named!(ty_param<&str, TyParam>, do_parse!(
+ named!(ty_param -> TyParam, do_parse!(
id: ident >>
bounds: opt_vec!(preceded!(
punct!(":"),
@@ -147,7 +147,7 @@
})
));
- named!(pub ty_param_bound<&str, TyParamBound>, alt_complete!(
+ named!(pub ty_param_bound -> TyParamBound, alt!(
preceded!(punct!("?"), poly_trait_ref) => {
|poly| TyParamBound::Trait(poly, TraitBoundModifier::Maybe)
}
@@ -159,7 +159,7 @@
}
));
- named!(pub where_clause<&str, WhereClause>, alt_complete!(
+ named!(pub where_clause -> WhereClause, alt!(
do_parse!(
punct!("where") >>
multispace >>
@@ -171,7 +171,7 @@
epsilon!() => { |_| Default::default() }
));
- named!(where_predicate<&str, WherePredicate>, alt_complete!(
+ named!(where_predicate -> WherePredicate, alt!(
do_parse!(
ident: lifetime >>
punct!(":") >>
diff --git a/src/helper.rs b/src/helper.rs
index 952a356..90a2499 100644
--- a/src/helper.rs
+++ b/src/helper.rs
@@ -1,6 +1,6 @@
#![cfg(feature = "parsing")]
-use nom::{self, IResult};
+use nom::IResult;
macro_rules! punct {
($i:expr, $punct:expr) => {
@@ -16,35 +16,34 @@
let end = i + token.len();
IResult::Done(&input[end..], &input[i..end])
} else {
- IResult::Error(nom::Err::Position(nom::ErrorKind::TagStr, input))
+ IResult::Error
};
}
}
- IResult::Error(nom::Err::Position(nom::ErrorKind::TagStr, input))
+ IResult::Error
}
-macro_rules! option (
- ($i:expr, $submac:ident!( $($args:tt)* )) => ({
+macro_rules! option {
+ ($i:expr, $submac:ident!( $($args:tt)* )) => {
match $submac!($i, $($args)*) {
::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)),
- ::nom::IResult::Error(_) => ::nom::IResult::Done($i, None),
- ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, None),
+ ::nom::IResult::Error => ::nom::IResult::Done($i, None),
}
- });
- ($i:expr, $f:expr) => (
- option!($i, call!($f));
- );
-);
+ };
-macro_rules! opt_vec (
- ($i:expr, $submac:ident!( $($args:tt)* )) => ({
+ ($i:expr, $f:expr) => {
+ option!($i, call!($f));
+ };
+}
+
+macro_rules! opt_vec {
+ ($i:expr, $submac:ident!( $($args:tt)* )) => {
match $submac!($i, $($args)*) {
::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o),
- ::nom::IResult::Error(_) => ::nom::IResult::Done($i, Vec::new()),
- ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, Vec::new()),
+ ::nom::IResult::Error => ::nom::IResult::Done($i, Vec::new()),
}
- });
-);
+ };
+}
macro_rules! epsilon {
($i:expr,) => {
diff --git a/src/ident.rs b/src/ident.rs
index d504465..4bb8878 100644
--- a/src/ident.rs
+++ b/src/ident.rs
@@ -48,9 +48,9 @@
ch.is_alphanumeric() || ch == '_'
}
- named!(pub ident<&str, Ident>, preceded!(
+ named!(pub ident -> Ident, preceded!(
option!(multispace),
- map!(take_while1_s!(ident_ch), Into::into)
+ map!(take_while1!(ident_ch), Into::into)
));
}
diff --git a/src/item.rs b/src/item.rs
index df5abe4..3ed5f8a 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -62,10 +62,10 @@
use ty::parsing::ty;
use nom::multispace;
- named!(pub item<&str, Item>, do_parse!(
+ named!(pub item -> Item, do_parse!(
attrs: many0!(attribute) >>
vis: visibility >>
- which: alt_complete!(punct!("struct") | punct!("enum")) >>
+ which: alt!(punct!("struct") | punct!("enum")) >>
multispace >>
id: ident >>
generics: generics >>
@@ -90,7 +90,7 @@
(item)
));
- named!(struct_body<&str, VariantData>, alt_complete!(
+ named!(struct_body -> VariantData, alt!(
struct_like_body => { VariantData::Struct }
|
terminated!(tuple_like_body, punct!(";")) => { VariantData::Tuple }
@@ -98,7 +98,7 @@
punct!(";") => { |_| VariantData::Unit }
));
- named!(enum_body<&str, Vec<Variant> >, do_parse!(
+ named!(enum_body -> Vec<Variant>, do_parse!(
punct!("{") >>
variants: separated_list!(punct!(","), variant) >>
option!(punct!(",")) >>
@@ -106,10 +106,10 @@
(variants)
));
- named!(variant<&str, Variant>, do_parse!(
+ named!(variant -> Variant, do_parse!(
attrs: many0!(attribute) >>
id: ident >>
- data: alt_complete!(
+ data: alt!(
struct_like_body => { VariantData::Struct }
|
tuple_like_body => { VariantData::Tuple }
@@ -123,7 +123,7 @@
})
));
- named!(struct_like_body<&str, Vec<Field> >, do_parse!(
+ named!(struct_like_body -> Vec<Field>, do_parse!(
punct!("{") >>
fields: separated_list!(punct!(","), struct_field) >>
option!(punct!(",")) >>
@@ -131,7 +131,7 @@
(fields)
));
- named!(tuple_like_body<&str, Vec<Field> >, do_parse!(
+ named!(tuple_like_body -> Vec<Field>, do_parse!(
punct!("(") >>
fields: separated_list!(punct!(","), tuple_field) >>
option!(punct!(",")) >>
@@ -139,7 +139,7 @@
(fields)
));
- named!(struct_field<&str, Field>, do_parse!(
+ named!(struct_field -> Field, do_parse!(
attrs: many0!(attribute) >>
vis: visibility >>
id: ident >>
@@ -153,7 +153,7 @@
})
));
- named!(tuple_field<&str, Field>, do_parse!(
+ named!(tuple_field -> Field, do_parse!(
attrs: many0!(attribute) >>
vis: visibility >>
ty: ty >>
@@ -165,7 +165,7 @@
})
));
- named!(pub visibility<&str, Visibility>, alt_complete!(
+ named!(pub visibility -> Visibility, alt!(
do_parse!(
punct!("pub") >>
multispace >>
diff --git a/src/lib.rs b/src/lib.rs
index 6037fdf..2c1be2f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,12 +1,9 @@
-#[cfg(feature = "parsing")]
-#[macro_use]
-extern crate nom;
-
#[cfg(feature = "printing")]
extern crate quote;
+#[cfg(feature = "parsing")]
#[macro_use]
-mod do_parse;
+mod nom;
#[macro_use]
mod helper;
@@ -83,23 +80,23 @@
use nom;
pub fn parse_item(input: &str) -> Result<Item, String> {
- unwrap("item", item::parsing::item(input))
+ unwrap("item", item::parsing::item, input)
}
pub fn parse_type(input: &str) -> Result<Ty, String> {
- unwrap("type", ty::parsing::ty(input))
+ unwrap("type", ty::parsing::ty, input)
}
pub fn parse_path(input: &str) -> Result<Path, String> {
- unwrap("path", ty::parsing::path(input))
+ unwrap("path", ty::parsing::path, input)
}
pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
- unwrap("where clause", generics::parsing::where_clause(input))
+ unwrap("where clause", generics::parsing::where_clause, input)
}
- fn unwrap<T>(name: &'static str, ires: nom::IResult<&str, T>) -> Result<T, String> {
- return match ires {
+ fn unwrap<T>(name: &'static str, f: fn(&str) -> nom::IResult<&str, T>, input: &str) -> Result<T, String> {
+ match f(input) {
nom::IResult::Done(rest, t) => {
if rest.is_empty() {
Ok(t)
@@ -107,25 +104,7 @@
Err(format!("remaining tokens after {}: {:?}", name, rest))
}
}
- nom::IResult::Error(err) => Err(root_cause(err)),
- nom::IResult::Incomplete(_) => Err(format!("incomplete {}", name)),
- };
-
- fn root_cause(mut err: nom::Err<&str>) -> String {
- loop {
- match err {
- nom::Err::Code(kind) => {
- return format!("failed to parse {:?}", kind);
- }
- nom::Err::Position(kind, pos) => {
- return format!("failed to parse {:?}: {:?}", kind, pos);
- }
- nom::Err::Node(_, next) |
- nom::Err::NodePosition(_, _, next) => {
- err = *next;
- }
- }
- }
+ nom::IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)),
}
}
}
diff --git a/src/nom.rs b/src/nom.rs
new file mode 100644
index 0000000..a899a20
--- /dev/null
+++ b/src/nom.rs
@@ -0,0 +1,577 @@
+// Adapted from nom <https://github.com/Geal/nom> by removing the
+// IResult::Incomplete variant, which we don't use and which unfortunately more
+// than doubles the compilation time.
+
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub enum IResult<I, O> {
+ /// indicates a correct parsing, the first field containing the rest of the
+ /// unparsed data, the second field contains the parsed data
+ Done(I, O),
+ Error,
+}
+
+/// Recognizes spaces, tabs, carriage returns and line feeds
+pub fn multispace(input: &str) -> IResult<&str, &str> {
+ if input.is_empty() {
+ return IResult::Error;
+ }
+
+ for (idx, item) in input.char_indices() {
+ let chr = item;
+ if !(chr == ' ' || chr == '\t' || chr == '\r' || chr == '\n') {
+ if idx == 0 {
+ return IResult::Error;
+ } else {
+ return IResult::Done(&input[idx..], &input[0..idx]);
+ }
+ }
+ }
+ IResult::Done("", input)
+}
+
+/// Recognizes numerical characters: 0-9
+pub fn digit(input: &str) -> IResult<&str, &str> {
+ if input.is_empty() {
+ return IResult::Error;
+ }
+
+ for (idx, item) in input.char_indices() {
+ if !item.is_digit(10) {
+ if idx == 0 {
+ return IResult::Error;
+ } else {
+ return IResult::Done(&input[idx..], &input[0..idx]);
+ }
+ }
+ }
+ IResult::Done("", input)
+}
+
+macro_rules! named {
+ ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
+ fn $name(i: &str) -> $crate::nom::IResult<&str, $o> {
+ $submac!(i, $($args)*)
+ }
+ };
+
+ (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
+ pub fn $name(i: &str) -> $crate::nom::IResult<&str, $o> {
+ $submac!(i, $($args)*)
+ }
+ };
+}
+
+macro_rules! call {
+ ($i:expr, $fun:expr) => {
+ $fun($i)
+ };
+}
+
+macro_rules! map {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
+ map_impl!($i, $submac!($($args)*), call!($g));
+ };
+
+ ($i:expr, $f:expr, $g:expr) => {
+ map_impl!($i, call!($f), call!($g));
+ };
+}
+
+/// Internal parser, do not use directly
+macro_rules! map_impl {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i, o) => {
+ $crate::nom::IResult::Done(i, $submac2!(o, $($args2)*))
+ }
+ }
+ };
+}
+
+macro_rules! map_res {
+ ($i:expr, $f:expr, $g:expr) => {
+ map_res_impl!($i, call!($f), call!($g));
+ };
+}
+
+/// Internal parser, do not use directly
+macro_rules! map_res_impl {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i, o) => match $submac2!(o, $($args2)*) {
+ Ok(output) => $crate::nom::IResult::Done(i, output),
+ Err(_) => $crate::nom::IResult::Error,
+ }
+ }
+ };
+}
+
+macro_rules! not {
+ ($i:expr, $submac:ident!( $($args:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Done(_, _) => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, ""),
+ }
+ };
+}
+
+macro_rules! cond {
+ ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
+ if $cond {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Done(i,o) => $crate::nom::IResult::Done(i, ::std::option::Option::Some(o)),
+ $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, ::std::option::Option::None),
+ }
+ } else {
+ $crate::nom::IResult::Done($i, ::std::option::Option::None)
+ }
+ }
+}
+
+macro_rules! preceded {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
+ match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
+ $crate::nom::IResult::Done(remaining, (_, o)) => $crate::nom::IResult::Done(remaining, o),
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ }
+ };
+
+ ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
+ preceded!($i, $submac!($($args)*), call!($g));
+ };
+
+ ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
+ preceded!($i, call!($f), $submac!($($args)*));
+ };
+
+ ($i:expr, $f:expr, $g:expr) => {
+ preceded!($i, call!($f), call!($g));
+ };
+}
+
+macro_rules! terminated {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => {
+ match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) {
+ $crate::nom::IResult::Done(remaining, (o, _)) => $crate::nom::IResult::Done(remaining, o),
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ }
+ };
+
+ ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
+ terminated!($i, $submac!($($args)*), call!($g));
+ };
+
+ ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
+ terminated!($i, call!($f), $submac!($($args)*));
+ };
+
+ ($i:expr, $f:expr, $g:expr) => {
+ terminated!($i, call!($f), call!($g));
+ };
+}
+
+macro_rules! many0 {
+ ($i:expr, $submac:ident!( $($args:tt)* )) => {{
+ let ret;
+ let mut res = ::std::vec::Vec::new();
+ let mut input = $i;
+
+ loop {
+ if input.is_empty() {
+ ret = $crate::nom::IResult::Done(input, res);
+ break;
+ }
+
+ match $submac!(input, $($args)*) {
+ $crate::nom::IResult::Error => {
+ ret = $crate::nom::IResult::Done(input, res);
+ break;
+ }
+ $crate::nom::IResult::Done(i, o) => {
+ // loop trip must always consume (otherwise infinite loops)
+ if i == input {
+ ret = $crate::nom::IResult::Error;
+ break;
+ }
+
+ res.push(o);
+ input = i;
+ }
+ }
+ }
+
+ ret
+ }};
+
+ ($i:expr, $f:expr) => {
+ many0!($i, call!($f));
+ };
+}
+
+macro_rules! peek {
+ ($i:expr, $submac:ident!( $($args:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o),
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ }
+ };
+}
+
+macro_rules! take_while1 {
+ ($input:expr, $submac:ident!( $($args:tt)* )) => {{
+ let mut offset = $input.len();
+ for (o, c) in $input.char_indices() {
+ if !$submac!(c, $($args)*) {
+ offset = o;
+ break;
+ }
+ }
+ if offset == 0 {
+ $crate::nom::IResult::Error
+ } else if offset < $input.len() {
+ $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
+ } else {
+ $crate::nom::IResult::Done("", $input)
+ }
+ }};
+
+ ($input:expr, $f:expr) => {
+ take_while1!($input, call!($f));
+ };
+}
+
+macro_rules! take_until {
+ ($input:expr, $substr:expr) => {{
+ if $substr.len() > $input.len() {
+ $crate::nom::IResult::Error
+ } else {
+ let substr_vec: Vec<char> = $substr.chars().collect();
+ let mut window: Vec<char> = vec![];
+ let mut offset = $input.len();
+ let mut parsed = false;
+ for (o, c) in $input.char_indices() {
+ window.push(c);
+ if window.len() > substr_vec.len() {
+ window.remove(0);
+ }
+ if window == substr_vec {
+ parsed = true;
+ window.pop();
+ let window_len: usize = window.iter()
+ .map(|x| x.len_utf8())
+ .fold(0, |x, y| x + y);
+ offset = o - window_len;
+ break;
+ }
+ }
+ if parsed {
+ $crate::nom::IResult::Done(&$input[offset..], &$input[..offset])
+ } else {
+ $crate::nom::IResult::Error
+ }
+ }
+ }};
+}
+
+macro_rules! tag {
+ ($i:expr, $tag: expr) => {
+ if $tag.len() > $i.len() {
+ $crate::nom::IResult::Error
+ } else if $i.starts_with($tag) {
+ $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()])
+ } else {
+ $crate::nom::IResult::Error
+ }
+ };
+}
+
+macro_rules! switch {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i, o) => match o {
+ $(
+ $p => $subrule!(i, $($args2)*),
+ )*
+ _ => $crate::nom::IResult::Error,
+ }
+ }
+ };
+}
+
+macro_rules! value {
+ ($i:expr, $res:expr) => {
+ $crate::nom::IResult::Done($i, $res)
+ };
+}
+
+macro_rules! delimited {
+ ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => {
+ match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o)
+ }
+ };
+
+ ($i:expr, $f:expr, $($rest:tt)+) => {
+ delimited!($i, call!($f), $($rest)*);
+ };
+}
+
+macro_rules! separated_list {
+ ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
+ let mut res = ::std::vec::Vec::new();
+ let mut input = $i;
+
+ // get the first element
+ match $submac!(input, $($args2)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Done(input, ::std::vec::Vec::new()),
+ $crate::nom::IResult::Done(i,o) => {
+ if i.len() == input.len() {
+ $crate::nom::IResult::Error
+ } else {
+ res.push(o);
+ input = i;
+
+ // get the separator first
+ while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
+ if i2.len() == input.len() {
+ break;
+ }
+
+ // get the element next
+ if let $crate::nom::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) {
+ if i3.len() == i2.len() {
+ break;
+ }
+ res.push(o3);
+ input = i3;
+ } else {
+ break;
+ }
+ }
+ $crate::nom::IResult::Done(input, res)
+ }
+ }
+ }
+ }};
+
+ ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
+ separated_list!($i, $submac!($($args)*), call!($g));
+ };
+
+ ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
+ separated_list!($i, call!($f), $submac!($($args)*));
+ };
+
+ ($i:expr, $f:expr, $g:expr) => {
+ separated_list!($i, call!($f), call!($g));
+ };
+}
+
+macro_rules! separated_nonempty_list {
+ ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{
+ let mut res = ::std::vec::Vec::new();
+ let mut input = $i;
+
+ // get the first element
+ match $submac!(input, $($args2)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i,o) => {
+ if i.len() == input.len() {
+ $crate::nom::IResult::Error
+ } else {
+ res.push(o);
+ input = i;
+
+ while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) {
+ if i2.len() == input.len() {
+ break;
+ }
+
+ if let $crate::nom::IResult::Done(i3,o3) = $submac!(i2, $($args2)*) {
+ if i3.len() == i2.len() {
+ break;
+ }
+ res.push(o3);
+ input = i3;
+ } else {
+ break;
+ }
+ }
+ $crate::nom::IResult::Done(input, res)
+ }
+ }
+ }
+ }};
+
+ ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
+ separated_nonempty_list!($i, $submac!($($args)*), call!($g));
+ };
+
+ ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => {
+ separated_nonempty_list!($i, call!($f), $submac!($($args)*));
+ };
+
+ ($i:expr, $f:expr, $g:expr) => {
+ separated_nonempty_list!($i, call!($f), call!($g));
+ };
+}
+
+macro_rules! tuple {
+ ($i:expr, $($rest:tt)*) => {
+ tuple_parser!($i, (), $($rest)*)
+ };
+}
+
+/// Internal parser, do not use directly
+macro_rules! tuple_parser {
+ ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
+ tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*);
+ };
+
+ ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i,o) =>
+ tuple_parser!(i, (o), $($rest)*),
+ }
+ };
+
+ ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i,o) =>
+ tuple_parser!(i, ($($parsed)* , o), $($rest)*),
+ }
+ };
+
+ ($i:expr, ($($parsed:tt),*), $e:ident) => {
+ tuple_parser!($i, ($($parsed),*), call!($e));
+ };
+
+ ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
+ $submac!($i, $($args)*)
+ };
+
+ ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o))
+ }
+ };
+
+ ($i:expr, ($($parsed:expr),*)) => {
+ $crate::nom::IResult::Done($i, ($($parsed),*))
+ };
+}
+
+macro_rules! alt {
+ ($i:expr, $e:ident | $($rest:tt)*) => {
+ alt!($i, call!($e) | $($rest)*);
+ };
+
+ ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
+ match $subrule!($i, $($args)*) {
+ res @ $crate::nom::IResult::Done(_, _) => res,
+ _ => alt!($i, $($rest)*)
+ }
+ };
+
+ ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
+ match $subrule!($i, $($args)*) {
+ $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
+ $crate::nom::IResult::Error => alt!($i, $($rest)*)
+ }
+ };
+
+ ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
+ alt!($i, call!($e) => { $gen } | $($rest)*);
+ };
+
+ ($i:expr, $e:ident => { $gen:expr }) => {
+ alt!($i, call!($e) => { $gen });
+ };
+
+ ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
+ match $subrule!($i, $($args)*) {
+ $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)),
+ $crate::nom::IResult::Error => alt!($i)
+ }
+ };
+
+ ($i:expr, $e:ident) => {
+ alt!($i, call!($e));
+ };
+
+ ($i:expr, $subrule:ident!( $($args:tt)*)) => {
+ match $subrule!( $i, $($args)* ) {
+ $crate::nom::IResult::Done(i,o) => $crate::nom::IResult::Done(i,o),
+ $crate::nom::IResult::Error => alt!($i),
+ }
+ };
+
+ ($i:expr) => {
+ $crate::nom::IResult::Error
+ };
+}
+
+macro_rules! do_parse {
+ ($i:expr, ( $($rest:expr),* )) => {
+ $crate::nom::IResult::Done($i, ( $($rest),* ))
+ };
+
+ ($i:expr, $e:ident >> $($rest:tt)*) => {
+ do_parse!($i, call!($e) >> $($rest)*);
+ };
+
+ ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i, _) =>
+ do_parse!(i, $($rest)*),
+ }
+ };
+
+ ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
+ do_parse!($i, $field: call!($e) >> $($rest)*);
+ };
+
+ ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i,o) => {
+ let $field = o;
+ do_parse!(i, $($rest)*)
+ },
+ }
+ };
+
+ // ending the chain
+ ($i:expr, $e:ident >> ( $($rest:tt)* )) => {
+ do_parse!($i, call!($e) >> ( $($rest)* ));
+ };
+
+ ($i:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Done(i, _) => $crate::nom::IResult::Done(i, ($($rest)*)),
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ }
+ };
+
+ ($i:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => {
+ do_parse!($i, $field: call!($e) >> ( $($rest)* ) );
+ };
+
+ ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => {
+ match $submac!($i, $($args)*) {
+ $crate::nom::IResult::Error => $crate::nom::IResult::Error,
+ $crate::nom::IResult::Done(i,o) => {
+ let $field = o;
+ $crate::nom::IResult::Done(i, ($($rest)*))
+ },
+ }
+ };
+}
diff --git a/src/ty.rs b/src/ty.rs
index 09d7148..e87f740 100644
--- a/src/ty.rs
+++ b/src/ty.rs
@@ -188,7 +188,7 @@
use nom::{digit, multispace};
use std::str;
- named!(pub ty<&str, Ty>, alt_complete!(
+ named!(pub ty -> Ty, alt!(
ty_vec
|
ty_fixed_length_vec
@@ -212,14 +212,14 @@
ty_paren
));
- named!(ty_vec<&str, Ty>, do_parse!(
+ named!(ty_vec -> Ty, do_parse!(
punct!("[") >>
elem: ty >>
punct!("]") >>
(Ty::Vec(Box::new(elem)))
));
- named!(ty_fixed_length_vec<&str, Ty>, do_parse!(
+ named!(ty_fixed_length_vec -> Ty, do_parse!(
punct!("[") >>
elem: ty >>
punct!(";") >>
@@ -229,9 +229,9 @@
(Ty::FixedLengthVec(Box::new(elem), len))
));
- named!(ty_ptr<&str, Ty>, do_parse!(
+ named!(ty_ptr -> Ty, do_parse!(
punct!("*") >>
- mutability: alt_complete!(
+ mutability: alt!(
punct!("const") => { |_| Mutability::Immutable }
|
punct!("mut") => { |_| Mutability::Mutable }
@@ -243,7 +243,7 @@
})))
));
- named!(ty_rptr<&str, Ty>, do_parse!(
+ named!(ty_rptr -> Ty, do_parse!(
punct!("&") >>
life: option!(lifetime) >>
mutability: mutability >>
@@ -254,7 +254,7 @@
})))
));
- named!(ty_bare_fn<&str, Ty>, do_parse!(
+ named!(ty_bare_fn -> Ty, do_parse!(
punct!("fn") >>
multispace >>
lifetimes: opt_vec!(delimited!(
@@ -281,18 +281,18 @@
})))
));
- named!(ty_never<&str, Ty>, map!(punct!("!"), |_| Ty::Never));
+ named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
- named!(ty_tup<&str, Ty>, do_parse!(
+ named!(ty_tup -> Ty, do_parse!(
punct!("(") >>
elems: separated_list!(punct!(","), ty) >>
punct!(")") >>
(Ty::Tup(elems))
));
- named!(ty_path<&str, Ty>, map!(path, |p| Ty::Path(None, p)));
+ named!(ty_path -> Ty, map!(path, |p| Ty::Path(None, p)));
- named!(ty_qpath<&str, Ty>, do_parse!(
+ named!(ty_qpath -> Ty, do_parse!(
punct!("<") >>
this: map!(ty, Box::new) >>
path: option!(preceded!(
@@ -319,21 +319,21 @@
})
));
- named!(ty_impl_trait<&str, Ty>, do_parse!(
+ named!(ty_impl_trait -> Ty, do_parse!(
punct!("impl") >>
multispace >>
elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
(Ty::ImplTrait(elem))
));
- named!(ty_paren<&str, Ty>, do_parse!(
+ named!(ty_paren -> Ty, do_parse!(
punct!("(") >>
elem: ty >>
punct!(")") >>
(Ty::Paren(Box::new(elem)))
));
- named!(mutability<&str, Mutability>, alt_complete!(
+ named!(mutability -> Mutability, alt!(
do_parse!(
punct!("mut") >>
multispace >>
@@ -343,7 +343,7 @@
epsilon!() => { |_| Mutability::Immutable }
));
- named!(pub path<&str, Path>, do_parse!(
+ named!(pub path -> Path, do_parse!(
global: option!(punct!("::")) >>
segments: separated_nonempty_list!(punct!("::"), path_segment) >>
(Path {
@@ -352,7 +352,7 @@
})
));
- named!(path_segment<&str, PathSegment>, alt_complete!(
+ named!(path_segment -> PathSegment, alt!(
do_parse!(
id: ident >>
punct!("<") >>
@@ -384,7 +384,7 @@
map!(ident, PathSegment::ident)
));
- named!(type_binding<&str, TypeBinding>, do_parse!(
+ named!(type_binding -> TypeBinding, do_parse!(
id: ident >>
punct!("=") >>
ty: ty >>
@@ -394,7 +394,7 @@
})
));
- named!(pub poly_trait_ref<&str, PolyTraitRef>, do_parse!(
+ named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
bound_lifetimes: bound_lifetimes >>
trait_ref: path >>
(PolyTraitRef {
@@ -403,7 +403,7 @@
})
));
- named!(fn_arg<&str, FnArg>, do_parse!(
+ named!(fn_arg -> FnArg, do_parse!(
pat: option!(terminated!(ident, punct!(":"))) >>
ty: ty >>
(FnArg {