Arrange parsing into modules
diff --git a/src/attr.rs b/src/attr.rs
index 571cf09..7b09b47 100644
--- a/src/attr.rs
+++ b/src/attr.rs
@@ -1,12 +1,5 @@
use super::*;
-#[cfg(feature = "parsing")]
-use common::word;
-#[cfg(feature = "parsing")]
-use helper::escaped_string;
-#[cfg(feature = "parsing")]
-use nom::multispace;
-
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Attribute {
pub value: MetaItem,
@@ -33,55 +26,60 @@
}
#[cfg(feature = "parsing")]
-named!(pub attribute<&str, Attribute>, alt!(
- do_parse!(
- punct!("#") >>
- punct!("[") >>
- meta_item: meta_item >>
- punct!("]") >>
- (Attribute {
- value: meta_item,
- is_sugared_doc: false,
- })
- )
- |
- do_parse!(
- punct!("///") >>
- space: multispace >>
- content: take_until_s!("\n") >>
- (Attribute {
- value: MetaItem::NameValue(
- "doc".to_string(),
- format!("///{}{}", space, content),
- ),
- is_sugared_doc: true,
- })
- )
-));
+pub mod parsing {
+ use super::*;
+ use common::parsing::word;
+ use helper::escaped_string;
+ use nom::multispace;
-#[cfg(feature = "parsing")]
-named!(quoted<&str, String>, delimited!(
- punct!("\""),
- escaped_string,
- tag_s!("\"")
-));
+ named!(pub attribute<&str, Attribute>, alt!(
+ do_parse!(
+ punct!("#") >>
+ punct!("[") >>
+ meta_item: meta_item >>
+ punct!("]") >>
+ (Attribute {
+ value: meta_item,
+ is_sugared_doc: false,
+ })
+ )
+ |
+ do_parse!(
+ punct!("///") >>
+ space: multispace >>
+ content: take_until_s!("\n") >>
+ (Attribute {
+ value: MetaItem::NameValue(
+ "doc".to_string(),
+ format!("///{}{}", space, content),
+ ),
+ is_sugared_doc: true,
+ })
+ )
+ ));
-#[cfg(feature = "parsing")]
-named!(meta_item<&str, MetaItem>, alt!(
- do_parse!(
- ident: word >>
- punct!("(") >>
- inner: separated_list!(punct!(","), meta_item) >>
- punct!(")") >>
- (MetaItem::List(ident, inner))
- )
- |
- do_parse!(
- ident: word >>
- punct!("=") >>
- string: quoted >>
- (MetaItem::NameValue(ident, string))
- )
- |
- map!(word, MetaItem::Word)
-));
+ named!(quoted<&str, String>, delimited!(
+ punct!("\""),
+ escaped_string,
+ tag_s!("\"")
+ ));
+
+ named!(meta_item<&str, MetaItem>, alt!(
+ do_parse!(
+ ident: word >>
+ punct!("(") >>
+ inner: separated_list!(punct!(","), meta_item) >>
+ punct!(")") >>
+ (MetaItem::List(ident, inner))
+ )
+ |
+ do_parse!(
+ ident: word >>
+ punct!("=") >>
+ string: quoted >>
+ (MetaItem::NameValue(ident, string))
+ )
+ |
+ map!(word, MetaItem::Word)
+ ));
+}
diff --git a/src/common.rs b/src/common.rs
index b24e9ee..a5173d7 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -7,22 +7,24 @@
}
#[cfg(feature = "parsing")]
-fn ident_ch(ch: char) -> bool {
- ch.is_alphanumeric() || ch == '_'
+pub mod parsing {
+ use super::*;
+
+ fn ident_ch(ch: char) -> bool {
+ ch.is_alphanumeric() || ch == '_'
+ }
+
+ named!(pub word<&str, Ident>, preceded!(
+ opt!(call!(::nom::multispace)),
+ map!(take_while1_s!(ident_ch), String::from)
+ ));
+
+ named!(pub visibility<&str, Visibility>, preceded!(
+ opt!(call!(::nom::multispace)),
+ alt!(
+ terminated!(tag_s!("pub"), call!(::nom::multispace)) => { |_| Visibility::Public }
+ |
+ epsilon!() => { |_| Visibility::Inherited }
+ )
+ ));
}
-
-#[cfg(feature = "parsing")]
-named!(pub word<&str, Ident>, preceded!(
- opt!(call!(::nom::multispace)),
- map!(take_while1_s!(ident_ch), String::from)
-));
-
-#[cfg(feature = "parsing")]
-named!(pub visibility<&str, Visibility>, preceded!(
- opt!(call!(::nom::multispace)),
- alt!(
- terminated!(tag_s!("pub"), call!(::nom::multispace)) => { |_| Visibility::Public }
- |
- epsilon!() => { |_| Visibility::Inherited }
- )
-));
diff --git a/src/do_parse.rs b/src/do_parse.rs
index d56461c..2dc2525 100644
--- a/src/do_parse.rs
+++ b/src/do_parse.rs
@@ -2,6 +2,8 @@
// https://github.com/Geal/nom/blob/a38188f333c29d00c32a3082bec5491d2eefa33f/src/sequence.rs#L591-L687
// Will be released in nom 2.0.
+#![cfg(feature = "parsing")]
+
#[macro_export]
macro_rules! do_parse (
($i:expr, $($rest:tt)*) => (
diff --git a/src/generics.rs b/src/generics.rs
index d1e889c..80738ae 100644
--- a/src/generics.rs
+++ b/src/generics.rs
@@ -1,12 +1,5 @@
use super::*;
-#[cfg(feature = "parsing")]
-use common::word;
-#[cfg(feature = "parsing")]
-use ty::{ty, poly_trait_ref};
-#[cfg(feature = "parsing")]
-use nom::multispace;
-
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct Generics {
pub lifetimes: Vec<LifetimeDef>,
@@ -71,111 +64,112 @@
}
#[cfg(feature = "parsing")]
-named!(pub generics<&str, Generics>, do_parse!(
- bracketed: alt!(
+pub mod parsing {
+ use super::*;
+ use common::parsing::word;
+ use ty::parsing::{ty, poly_trait_ref};
+ use nom::multispace;
+
+ named!(pub generics<&str, Generics>, do_parse!(
+ bracketed: alt!(
+ do_parse!(
+ punct!("<") >>
+ lifetimes: separated_list!(punct!(","), lifetime_def) >>
+ ty_params: opt_vec!(preceded!(
+ cond!(!lifetimes.is_empty(), punct!(",")),
+ separated_nonempty_list!(punct!(","), ty_param)
+ )) >>
+ punct!(">") >>
+ (lifetimes, ty_params)
+ )
+ |
+ epsilon!() => { |_| (Vec::new(), Vec::new()) }
+ ) >>
+ where_clause: opt_vec!(do_parse!(
+ punct!("where") >>
+ multispace >>
+ predicates: separated_nonempty_list!(punct!(","), where_predicate) >>
+ opt!(punct!(",")) >>
+ (predicates)
+ )) >>
+ (Generics {
+ lifetimes: bracketed.0,
+ ty_params: bracketed.1,
+ where_clause: where_clause,
+ })
+ ));
+
+ named!(pub lifetime<&str, Lifetime>, preceded!(
+ punct!("'"),
+ map!(word, |ident| Lifetime { ident: ident })
+ ));
+
+ named!(pub lifetime_def<&str, LifetimeDef>, do_parse!(
+ life: lifetime >>
+ bounds: opt_vec!(preceded!(
+ punct!(":"),
+ separated_nonempty_list!(punct!(","), lifetime)
+ )) >>
+ (LifetimeDef {
+ lifetime: life,
+ bounds: bounds,
+ })
+ ));
+
+ named!(pub bound_lifetimes<&str, Vec<LifetimeDef> >, opt_vec!(do_parse!(
+ punct!("for") >>
+ punct!("<") >>
+ lifetimes: separated_list!(punct!(","), lifetime_def) >>
+ punct!(">") >>
+ (lifetimes)
+ )));
+
+ named!(ty_param<&str, TyParam>, do_parse!(
+ ident: word >>
+ bounds: opt_vec!(preceded!(
+ punct!(":"),
+ separated_nonempty_list!(punct!("+"), ty_param_bound)
+ )) >>
+ default: opt!(preceded!(
+ punct!("="),
+ ty
+ )) >>
+ (TyParam {
+ ident: ident,
+ bounds: bounds,
+ default: default,
+ })
+ ));
+
+ named!(pub ty_param_bound<&str, TyParamBound>, alt!(
+ tuple!(punct!("?"), punct!("Sized")) => { |_| TyParamBound::MaybeSized }
+ |
+ lifetime => { TyParamBound::Region }
+ |
+ poly_trait_ref => { TyParamBound::Trait }
+ ));
+
+ named!(where_predicate<&str, WherePredicate>, alt!(
do_parse!(
- punct!("<") >>
- lifetimes: separated_list!(punct!(","), lifetime_def) >>
- ty_params: opt_vec!(preceded!(
- cond!(!lifetimes.is_empty(), punct!(",")),
- separated_nonempty_list!(punct!(","), ty_param)
- )) >>
- punct!(">") >>
- (lifetimes, ty_params)
+ ident: lifetime >>
+ punct!(":") >>
+ bounds: separated_nonempty_list!(punct!("+"), lifetime) >>
+ (WherePredicate::RegionPredicate(WhereRegionPredicate {
+ lifetime: ident,
+ bounds: bounds,
+ }))
)
|
- epsilon!() => { |_| (Vec::new(), Vec::new()) }
- ) >>
- where_clause: opt_vec!(do_parse!(
- punct!("where") >>
- multispace >>
- predicates: separated_nonempty_list!(punct!(","), where_predicate) >>
- opt!(punct!(",")) >>
- (predicates)
- )) >>
- (Generics {
- lifetimes: bracketed.0,
- ty_params: bracketed.1,
- where_clause: where_clause,
- })
-));
-
-#[cfg(feature = "parsing")]
-named!(pub lifetime<&str, Lifetime>, preceded!(
- punct!("'"),
- map!(word, |ident| Lifetime { ident: ident })
-));
-
-#[cfg(feature = "parsing")]
-named!(pub lifetime_def<&str, LifetimeDef>, do_parse!(
- life: lifetime >>
- bounds: opt_vec!(preceded!(
- punct!(":"),
- separated_nonempty_list!(punct!(","), lifetime)
- )) >>
- (LifetimeDef {
- lifetime: life,
- bounds: bounds,
- })
-));
-
-#[cfg(feature = "parsing")]
-named!(pub bound_lifetimes<&str, Vec<LifetimeDef> >, opt_vec!(do_parse!(
- punct!("for") >>
- punct!("<") >>
- lifetimes: separated_list!(punct!(","), lifetime_def) >>
- punct!(">") >>
- (lifetimes)
-)));
-
-#[cfg(feature = "parsing")]
-named!(ty_param<&str, TyParam>, do_parse!(
- ident: word >>
- bounds: opt_vec!(preceded!(
- punct!(":"),
- separated_nonempty_list!(punct!("+"), ty_param_bound)
- )) >>
- default: opt!(preceded!(
- punct!("="),
- ty
- )) >>
- (TyParam {
- ident: ident,
- bounds: bounds,
- default: default,
- })
-));
-
-#[cfg(feature = "parsing")]
-named!(pub ty_param_bound<&str, TyParamBound>, alt!(
- tuple!(punct!("?"), punct!("Sized")) => { |_| TyParamBound::MaybeSized }
- |
- lifetime => { TyParamBound::Region }
- |
- poly_trait_ref => { TyParamBound::Trait }
-));
-
-#[cfg(feature = "parsing")]
-named!(where_predicate<&str, WherePredicate>, alt!(
- do_parse!(
- ident: lifetime >>
- punct!(":") >>
- bounds: separated_nonempty_list!(punct!("+"), lifetime) >>
- (WherePredicate::RegionPredicate(WhereRegionPredicate {
- lifetime: ident,
- bounds: bounds,
- }))
- )
- |
- do_parse!(
- bound_lifetimes: bound_lifetimes >>
- bounded_ty: ty >>
- punct!(":") >>
- bounds: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
- (WherePredicate::BoundPredicate(WhereBoundPredicate {
- bound_lifetimes: bound_lifetimes,
- bounded_ty: bounded_ty,
- bounds: bounds,
- }))
- )
-));
+ do_parse!(
+ bound_lifetimes: bound_lifetimes >>
+ bounded_ty: ty >>
+ punct!(":") >>
+ bounds: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
+ (WherePredicate::BoundPredicate(WhereBoundPredicate {
+ bound_lifetimes: bound_lifetimes,
+ bounded_ty: bounded_ty,
+ bounds: bounds,
+ }))
+ )
+ ));
+}
diff --git a/src/item.rs b/src/item.rs
index eb40ae0..e21dc4a 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -1,16 +1,5 @@
use super::*;
-#[cfg(feature = "parsing")]
-use attr::attribute;
-#[cfg(feature = "parsing")]
-use common::{word, visibility};
-#[cfg(feature = "parsing")]
-use generics::generics;
-#[cfg(feature = "parsing")]
-use ty::ty;
-#[cfg(feature = "parsing")]
-use nom::multispace;
-
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Item {
pub ident: Ident,
@@ -50,113 +39,115 @@
}
#[cfg(feature = "parsing")]
-named!(pub item<&str, Item>, do_parse!(
- attrs: many0!(attribute) >>
- vis: visibility >>
- which: alt!(tag_s!("struct") | tag_s!("enum")) >>
- multispace >>
- ident: word >>
- generics: generics >>
- item: switch!(value!(which),
- "struct" => map!(struct_body, move |(style, fields)| Item {
- ident: ident,
- vis: vis,
- attrs: attrs,
- generics: generics,
- body: Body::Struct(style, fields),
- })
- |
- "enum" => map!(enum_body, move |body| Item {
- ident: ident,
- vis: vis,
- attrs: attrs,
- generics: generics,
- body: body,
- })
- ) >>
- opt!(multispace) >>
- (item)
-));
+pub mod parsing {
+ use super::*;
+ use attr::parsing::attribute;
+ use common::parsing::{word, visibility};
+ use generics::parsing::generics;
+ use ty::parsing::ty;
+ use nom::multispace;
-#[cfg(feature = "parsing")]
-named!(struct_body<&str, (Style, Vec<Field>)>, alt!(
- struct_like_body => { |fields| (Style::Struct, fields) }
- |
- terminated!(tuple_like_body, punct!(";")) => { |fields| (Style::Tuple, fields) }
- |
- punct!(";") => { |_| (Style::Unit, Vec::new()) }
-));
+ named!(pub item<&str, Item>, do_parse!(
+ attrs: many0!(attribute) >>
+ vis: visibility >>
+ which: alt!(tag_s!("struct") | tag_s!("enum")) >>
+ multispace >>
+ ident: word >>
+ generics: generics >>
+ item: switch!(value!(which),
+ "struct" => map!(struct_body, move |(style, fields)| Item {
+ ident: ident,
+ vis: vis,
+ attrs: attrs,
+ generics: generics,
+ body: Body::Struct(style, fields),
+ })
+ |
+ "enum" => map!(enum_body, move |body| Item {
+ ident: ident,
+ vis: vis,
+ attrs: attrs,
+ generics: generics,
+ body: body,
+ })
+ ) >>
+ opt!(multispace) >>
+ (item)
+ ));
-#[cfg(feature = "parsing")]
-named!(enum_body<&str, Body>, do_parse!(
- punct!("{") >>
- variants: separated_list!(punct!(","), variant) >>
- opt!(punct!(",")) >>
- punct!("}") >>
- (Body::Enum(variants))
-));
-
-#[cfg(feature = "parsing")]
-named!(variant<&str, Variant>, do_parse!(
- attrs: many0!(attribute) >>
- ident: word >>
- body: alt!(
+ named!(struct_body<&str, (Style, Vec<Field>)>, alt!(
struct_like_body => { |fields| (Style::Struct, fields) }
|
- tuple_like_body => { |fields| (Style::Tuple, fields) }
+ terminated!(tuple_like_body, punct!(";")) => { |fields| (Style::Tuple, fields) }
|
- epsilon!() => { |_| (Style::Unit, Vec::new()) }
- ) >>
- (Variant {
- ident: ident,
- attrs: attrs,
- style: body.0,
- fields: body.1,
- })
-));
+ punct!(";") => { |_| (Style::Unit, Vec::new()) }
+ ));
-#[cfg(feature = "parsing")]
-named!(struct_like_body<&str, Vec<Field> >, do_parse!(
- punct!("{") >>
- fields: separated_list!(punct!(","), struct_field) >>
- opt!(punct!(",")) >>
- punct!("}") >>
- (fields)
-));
+ named!(enum_body<&str, Body>, do_parse!(
+ punct!("{") >>
+ variants: separated_list!(punct!(","), variant) >>
+ opt!(punct!(",")) >>
+ punct!("}") >>
+ (Body::Enum(variants))
+ ));
-#[cfg(feature = "parsing")]
-named!(tuple_like_body<&str, Vec<Field> >, do_parse!(
- punct!("(") >>
- fields: separated_list!(punct!(","), tuple_field) >>
- opt!(punct!(",")) >>
- punct!(")") >>
- (fields)
-));
+ named!(variant<&str, Variant>, do_parse!(
+ attrs: many0!(attribute) >>
+ ident: word >>
+ body: alt!(
+ struct_like_body => { |fields| (Style::Struct, fields) }
+ |
+ tuple_like_body => { |fields| (Style::Tuple, fields) }
+ |
+ epsilon!() => { |_| (Style::Unit, Vec::new()) }
+ ) >>
+ (Variant {
+ ident: ident,
+ attrs: attrs,
+ style: body.0,
+ fields: body.1,
+ })
+ ));
-#[cfg(feature = "parsing")]
-named!(struct_field<&str, Field>, do_parse!(
- attrs: many0!(attribute) >>
- vis: visibility >>
- ident: word >>
- punct!(":") >>
- ty: ty >>
- (Field {
- ident: Some(ident),
- vis: vis,
- attrs: attrs,
- ty: ty,
- })
-));
+ named!(struct_like_body<&str, Vec<Field> >, do_parse!(
+ punct!("{") >>
+ fields: separated_list!(punct!(","), struct_field) >>
+ opt!(punct!(",")) >>
+ punct!("}") >>
+ (fields)
+ ));
-#[cfg(feature = "parsing")]
-named!(tuple_field<&str, Field>, do_parse!(
- attrs: many0!(attribute) >>
- vis: visibility >>
- ty: ty >>
- (Field {
- ident: None,
- vis: vis,
- attrs: attrs,
- ty: ty,
- })
-));
+ named!(tuple_like_body<&str, Vec<Field> >, do_parse!(
+ punct!("(") >>
+ fields: separated_list!(punct!(","), tuple_field) >>
+ opt!(punct!(",")) >>
+ punct!(")") >>
+ (fields)
+ ));
+
+ named!(struct_field<&str, Field>, do_parse!(
+ attrs: many0!(attribute) >>
+ vis: visibility >>
+ ident: word >>
+ punct!(":") >>
+ ty: ty >>
+ (Field {
+ ident: Some(ident),
+ vis: vis,
+ attrs: attrs,
+ ty: ty,
+ })
+ ));
+
+ named!(tuple_field<&str, Field>, do_parse!(
+ attrs: many0!(attribute) >>
+ vis: visibility >>
+ ty: ty >>
+ (Field {
+ ident: None,
+ vis: vis,
+ attrs: attrs,
+ ty: ty,
+ })
+ ));
+}
diff --git a/src/lib.rs b/src/lib.rs
index 7b813cb..397b9e1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,7 +2,6 @@
#[macro_use]
extern crate nom;
-#[cfg(feature = "parsing")]
#[macro_use]
mod do_parse;
@@ -63,7 +62,7 @@
#[cfg(feature = "parsing")]
pub fn parse(input: &str) -> Item {
- match item::item(input) {
+ match item::parsing::item(input) {
nom::IResult::Done(rest, ast) => {
if rest.is_empty() {
ast
diff --git a/src/ty.rs b/src/ty.rs
index b364c8e..cbc0bb1 100644
--- a/src/ty.rs
+++ b/src/ty.rs
@@ -1,15 +1,5 @@
use super::*;
-#[cfg(feature = "parsing")]
-use common::word;
-#[cfg(feature = "parsing")]
-use generics::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
-#[cfg(feature = "parsing")]
-use nom::{digit, multispace};
-
-#[cfg(feature = "parsing")]
-use std::str;
-
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Ty {
/// A variable-length array (`[T]`)
@@ -191,241 +181,232 @@
}
#[cfg(feature = "parsing")]
-named!(pub ty<&str, Ty>, alt!(
- ty_vec
- |
- ty_fixed_length_vec
- |
- ty_ptr
- |
- ty_rptr
- |
- ty_bare_fn
- |
- ty_never
- |
- ty_tup
- |
- ty_path
- |
- ty_qpath
- |
- ty_impl_trait
- |
- ty_paren
-));
+pub mod parsing {
+ use super::*;
+ use common::parsing::word;
+ use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
+ use nom::{digit, multispace};
+ use std::str;
-#[cfg(feature = "parsing")]
-named!(ty_vec<&str, Ty>, do_parse!(
- punct!("[") >>
- elem: ty >>
- punct!("]") >>
- (Ty::Vec(Box::new(elem)))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_fixed_length_vec<&str, Ty>, do_parse!(
- punct!("[") >>
- elem: ty >>
- punct!(";") >>
- opt!(multispace) >>
- size: map_res!(digit, str::parse) >>
- (Ty::FixedLengthVec(Box::new(elem), size))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_ptr<&str, Ty>, do_parse!(
- punct!("*") >>
- mutability: alt!(
- punct!("const") => { |_| Mutability::Immutable }
+ named!(pub ty<&str, Ty>, alt!(
+ ty_vec
|
- punct!("mut") => { |_| Mutability::Mutable }
- ) >>
- target: ty >>
- (Ty::Ptr(Box::new(MutTy {
- ty: target,
- mutability: mutability,
- })))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_rptr<&str, Ty>, do_parse!(
- punct!("&") >>
- life: opt!(lifetime) >>
- mutability: mutability >>
- target: ty >>
- (Ty::Rptr(life, Box::new(MutTy {
- ty: target,
- mutability: mutability,
- })))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_bare_fn<&str, Ty>, do_parse!(
- punct!("fn") >>
- multispace >>
- lifetimes: opt_vec!(delimited!(
- punct!("<"),
- separated_list!(punct!(","), lifetime_def),
- punct!(">")
- )) >>
- punct!("(") >>
- inputs: separated_list!(punct!(","), fn_arg) >>
- punct!(")") >>
- output: opt!(preceded!(
- punct!("->"),
- ty
- )) >>
- (Ty::BareFn(Box::new(BareFnTy {
- lifetimes: lifetimes,
- decl: FnDecl {
- inputs: inputs,
- output: match output {
- Some(ty) => FunctionRetTy::Ty(ty),
- None => FunctionRetTy::Default,
- },
- },
- })))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_never<&str, Ty>, map!(punct!("!"), |_| Ty::Never));
-
-#[cfg(feature = "parsing")]
-named!(ty_tup<&str, Ty>, do_parse!(
- punct!("(") >>
- elems: separated_list!(punct!(","), ty) >>
- punct!(")") >>
- (Ty::Tup(elems))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_path<&str, Ty>, map!(path, |p| Ty::Path(None, p)));
-
-#[cfg(feature = "parsing")]
-named!(ty_qpath<&str, Ty>, do_parse!(
- punct!("<") >>
- this: map!(ty, Box::new) >>
- path: opt!(preceded!(
- tuple!(punct!("as"), multispace),
- path
- )) >>
- punct!(">") >>
- punct!("::") >>
- rest: separated_nonempty_list!(punct!("::"), path_segment) >>
- ({
- match path {
- Some(mut path) => {
- let pos = path.segments.len();
- path.segments.extend(rest);
- Ty::Path(Some(QSelf { ty: this, position: pos }), path)
- }
- None => {
- Ty::Path(Some(QSelf { ty: this, position: 0 }), Path {
- global: false,
- segments: rest,
- })
- }
- }
- })
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_impl_trait<&str, Ty>, do_parse!(
- punct!("impl") >>
- multispace >>
- elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
- (Ty::ImplTrait(elem))
-));
-
-#[cfg(feature = "parsing")]
-named!(ty_paren<&str, Ty>, do_parse!(
- punct!("(") >>
- elem: ty >>
- punct!(")") >>
- (Ty::Paren(Box::new(elem)))
-));
-
-#[cfg(feature = "parsing")]
-named!(mutability<&str, Mutability>, preceded!(
- opt!(multispace),
- alt!(
- terminated!(tag_s!("mut"), multispace) => { |_| Mutability::Mutable }
+ ty_fixed_length_vec
|
- epsilon!() => { |_| Mutability::Immutable }
- )
-));
+ ty_ptr
+ |
+ ty_rptr
+ |
+ ty_bare_fn
+ |
+ ty_never
+ |
+ ty_tup
+ |
+ ty_path
+ |
+ ty_qpath
+ |
+ ty_impl_trait
+ |
+ ty_paren
+ ));
-#[cfg(feature = "parsing")]
-named!(path<&str, Path>, do_parse!(
- global: opt!(punct!("::")) >>
- segments: separated_nonempty_list!(punct!("::"), path_segment) >>
- (Path {
- global: global.is_some(),
- segments: segments,
- })
-));
+ named!(ty_vec<&str, Ty>, do_parse!(
+ punct!("[") >>
+ elem: ty >>
+ punct!("]") >>
+ (Ty::Vec(Box::new(elem)))
+ ));
-#[cfg(feature = "parsing")]
-named!(path_segment<&str, PathSegment>, alt!(
- do_parse!(
- ident: word >>
- punct!("<") >>
- lifetimes: separated_list!(punct!(","), lifetime) >>
- types: opt_vec!(preceded!(
- cond!(!lifetimes.is_empty(), punct!(",")),
- separated_nonempty_list!(
- punct!(","),
- terminated!(ty, not!(peek!(punct!("="))))
- )
+ named!(ty_fixed_length_vec<&str, Ty>, do_parse!(
+ punct!("[") >>
+ elem: ty >>
+ punct!(";") >>
+ opt!(multispace) >>
+ size: map_res!(digit, str::parse) >>
+ (Ty::FixedLengthVec(Box::new(elem), size))
+ ));
+
+ named!(ty_ptr<&str, Ty>, do_parse!(
+ punct!("*") >>
+ mutability: alt!(
+ punct!("const") => { |_| Mutability::Immutable }
+ |
+ punct!("mut") => { |_| Mutability::Mutable }
+ ) >>
+ target: ty >>
+ (Ty::Ptr(Box::new(MutTy {
+ ty: target,
+ mutability: mutability,
+ })))
+ ));
+
+ named!(ty_rptr<&str, Ty>, do_parse!(
+ punct!("&") >>
+ life: opt!(lifetime) >>
+ mutability: mutability >>
+ target: ty >>
+ (Ty::Rptr(life, Box::new(MutTy {
+ ty: target,
+ mutability: mutability,
+ })))
+ ));
+
+ named!(ty_bare_fn<&str, Ty>, do_parse!(
+ punct!("fn") >>
+ multispace >>
+ lifetimes: opt_vec!(delimited!(
+ punct!("<"),
+ separated_list!(punct!(","), lifetime_def),
+ punct!(">")
)) >>
- bindings: opt_vec!(preceded!(
- cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
- separated_nonempty_list!(punct!(","), type_binding)
+ punct!("(") >>
+ inputs: separated_list!(punct!(","), fn_arg) >>
+ punct!(")") >>
+ output: opt!(preceded!(
+ punct!("->"),
+ ty
+ )) >>
+ (Ty::BareFn(Box::new(BareFnTy {
+ lifetimes: lifetimes,
+ decl: FnDecl {
+ inputs: inputs,
+ output: match output {
+ Some(ty) => FunctionRetTy::Ty(ty),
+ None => FunctionRetTy::Default,
+ },
+ },
+ })))
+ ));
+
+ named!(ty_never<&str, Ty>, map!(punct!("!"), |_| Ty::Never));
+
+ named!(ty_tup<&str, 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_qpath<&str, Ty>, do_parse!(
+ punct!("<") >>
+ this: map!(ty, Box::new) >>
+ path: opt!(preceded!(
+ tuple!(punct!("as"), multispace),
+ path
)) >>
punct!(">") >>
- (PathSegment {
- ident: ident,
- parameters: PathParameters::AngleBracketed(
- AngleBracketedParameterData {
- lifetimes: lifetimes,
- types: types,
- bindings: bindings,
+ punct!("::") >>
+ rest: separated_nonempty_list!(punct!("::"), path_segment) >>
+ ({
+ match path {
+ Some(mut path) => {
+ let pos = path.segments.len();
+ path.segments.extend(rest);
+ Ty::Path(Some(QSelf { ty: this, position: pos }), path)
}
- ),
+ None => {
+ Ty::Path(Some(QSelf { ty: this, position: 0 }), Path {
+ global: false,
+ segments: rest,
+ })
+ }
+ }
})
- )
- |
- map!(word, PathSegment::ident)
-));
+ ));
-#[cfg(feature = "parsing")]
-named!(type_binding<&str, TypeBinding>, do_parse!(
- ident: word >>
- punct!("=") >>
- ty: ty >>
- (TypeBinding {
- ident: ident,
- ty: ty,
- })
-));
+ named!(ty_impl_trait<&str, Ty>, do_parse!(
+ punct!("impl") >>
+ multispace >>
+ elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
+ (Ty::ImplTrait(elem))
+ ));
-#[cfg(feature = "parsing")]
-named!(pub poly_trait_ref<&str, PolyTraitRef>, do_parse!(
- bound_lifetimes: bound_lifetimes >>
- trait_ref: path >>
- (PolyTraitRef {
- bound_lifetimes: bound_lifetimes,
- trait_ref: trait_ref,
- })
-));
+ named!(ty_paren<&str, Ty>, do_parse!(
+ punct!("(") >>
+ elem: ty >>
+ punct!(")") >>
+ (Ty::Paren(Box::new(elem)))
+ ));
-#[cfg(feature = "parsing")]
-named!(fn_arg<&str, Arg>, do_parse!(
- pat: opt!(terminated!(word, punct!(":"))) >>
- ty: ty >>
- (Arg {
- pat: pat,
- ty: ty,
- })
-));
+ named!(mutability<&str, Mutability>, preceded!(
+ opt!(multispace),
+ alt!(
+ terminated!(tag_s!("mut"), multispace) => { |_| Mutability::Mutable }
+ |
+ epsilon!() => { |_| Mutability::Immutable }
+ )
+ ));
+
+ named!(path<&str, Path>, do_parse!(
+ global: opt!(punct!("::")) >>
+ segments: separated_nonempty_list!(punct!("::"), path_segment) >>
+ (Path {
+ global: global.is_some(),
+ segments: segments,
+ })
+ ));
+
+ named!(path_segment<&str, PathSegment>, alt!(
+ do_parse!(
+ ident: word >>
+ punct!("<") >>
+ lifetimes: separated_list!(punct!(","), lifetime) >>
+ types: opt_vec!(preceded!(
+ cond!(!lifetimes.is_empty(), punct!(",")),
+ separated_nonempty_list!(
+ punct!(","),
+ terminated!(ty, not!(peek!(punct!("="))))
+ )
+ )) >>
+ bindings: opt_vec!(preceded!(
+ cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
+ separated_nonempty_list!(punct!(","), type_binding)
+ )) >>
+ punct!(">") >>
+ (PathSegment {
+ ident: ident,
+ parameters: PathParameters::AngleBracketed(
+ AngleBracketedParameterData {
+ lifetimes: lifetimes,
+ types: types,
+ bindings: bindings,
+ }
+ ),
+ })
+ )
+ |
+ map!(word, PathSegment::ident)
+ ));
+
+ named!(type_binding<&str, TypeBinding>, do_parse!(
+ ident: word >>
+ punct!("=") >>
+ ty: ty >>
+ (TypeBinding {
+ ident: ident,
+ ty: ty,
+ })
+ ));
+
+ named!(pub poly_trait_ref<&str, PolyTraitRef>, do_parse!(
+ bound_lifetimes: bound_lifetimes >>
+ trait_ref: path >>
+ (PolyTraitRef {
+ bound_lifetimes: bound_lifetimes,
+ trait_ref: trait_ref,
+ })
+ ));
+
+ named!(fn_arg<&str, Arg>, do_parse!(
+ pat: opt!(terminated!(word, punct!(":"))) >>
+ ty: ty >>
+ (Arg {
+ pat: pat,
+ ty: ty,
+ })
+ ));
+}