blob: adbae5eeaaa16205b3202293c58fb4202fbbcee2 [file] [log] [blame]
David Tolnay86eca752016-09-04 11:26:41 -07001#[cfg(feature = "parsing")]
David Tolnay35161ff2016-09-03 11:33:15 -07002#[macro_use]
3extern crate nom;
4
David Tolnay87d0b442016-09-04 11:52:12 -07005#[cfg(feature = "printing")]
6extern crate quote;
7
David Tolnayb79ee962016-09-04 09:39:20 -07008#[macro_use]
David Tolnay6b7aaf02016-09-04 10:39:25 -07009mod do_parse;
10
11#[macro_use]
David Tolnayb79ee962016-09-04 09:39:20 -070012mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -070013
David Tolnayb79ee962016-09-04 09:39:20 -070014mod attr;
15pub use attr::{
16 Attribute,
17 MetaItem,
18};
David Tolnay35161ff2016-09-03 11:33:15 -070019
David Tolnayb79ee962016-09-04 09:39:20 -070020mod generics;
21pub use generics::{
22 Generics,
23 Lifetime,
24 LifetimeDef,
David Tolnay55337722016-09-11 12:58:56 -070025 TraitBoundModifier,
David Tolnayb79ee962016-09-04 09:39:20 -070026 TyParam,
27 TyParamBound,
28 WhereBoundPredicate,
David Tolnay55337722016-09-11 12:58:56 -070029 WhereClause,
David Tolnayb79ee962016-09-04 09:39:20 -070030 WherePredicate,
31 WhereRegionPredicate,
32};
David Tolnay35161ff2016-09-03 11:33:15 -070033
David Tolnay55337722016-09-11 12:58:56 -070034mod ident;
35pub use ident::{
36 Ident,
37};
38
David Tolnayb79ee962016-09-04 09:39:20 -070039mod item;
40pub use item::{
41 Body,
42 Field,
43 Item,
David Tolnayb79ee962016-09-04 09:39:20 -070044 Variant,
David Tolnay55337722016-09-11 12:58:56 -070045 VariantData,
46 Visibility,
David Tolnayb79ee962016-09-04 09:39:20 -070047};
David Tolnay35161ff2016-09-03 11:33:15 -070048
David Tolnayb79ee962016-09-04 09:39:20 -070049mod ty;
50pub use ty::{
51 AngleBracketedParameterData,
David Tolnayb79ee962016-09-04 09:39:20 -070052 BareFnTy,
David Tolnay66daf742016-09-07 08:21:49 -070053 FnArg,
David Tolnayb79ee962016-09-04 09:39:20 -070054 FnDecl,
55 FunctionRetTy,
56 MutTy,
57 Mutability,
58 ParenthesizedParameterData,
59 Path,
60 PathParameters,
61 PathSegment,
62 PolyTraitRef,
63 QSelf,
64 Ty,
65 TypeBinding,
66};
David Tolnay35161ff2016-09-03 11:33:15 -070067
David Tolnay55337722016-09-11 12:58:56 -070068#[cfg(feature = "aster")]
69pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070070
David Tolnay55337722016-09-11 12:58:56 -070071#[cfg(feature = "visit")]
72pub mod visit;
73
74#[cfg(feature = "parsing")]
75pub use parsing::*;
76
77#[cfg(feature = "parsing")]
78mod parsing {
79 use super::*;
80 use {generics, item, ty};
81 use nom;
82
83 pub fn parse_item(input: &str) -> Result<Item, String> {
84 unwrap("item", item::parsing::item(input))
85 }
86
87 pub fn parse_path(input: &str) -> Result<Path, String> {
88 unwrap("path", ty::parsing::path(input))
89 }
90
91 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
92 unwrap("where clause", generics::parsing::where_clause(input))
93 }
94
95 fn unwrap<T>(name: &'static str, ires: nom::IResult<&str, T>) -> Result<T, String> {
96 return match ires {
97 nom::IResult::Done(rest, t) => {
98 if rest.is_empty() {
99 Ok(t)
100 } else {
101 Err(format!("remaining tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700102 }
David Tolnay55337722016-09-11 12:58:56 -0700103 }
104 nom::IResult::Error(err) => Err(root_cause(err)),
105 nom::IResult::Incomplete(_) => Err(format!("incomplete {}", name)),
106 };
107
108 fn root_cause(mut err: nom::Err<&str>) -> String {
109 loop {
110 match err {
111 nom::Err::Code(kind) => {
112 return format!("failed to parse {:?}", kind);
113 }
114 nom::Err::Position(kind, pos) => {
115 return format!("failed to parse {:?}: {:?}", kind, pos);
116 }
117 nom::Err::Node(_, next) |
118 nom::Err::NodePosition(_, _, next) => {
119 err = *next;
120 }
David Tolnayc94c38a2016-09-05 17:02:03 -0700121 }
David Tolnay7ebb9fb2016-09-03 12:07:47 -0700122 }
David Tolnay35161ff2016-09-03 11:33:15 -0700123 }
David Tolnay35161ff2016-09-03 11:33:15 -0700124 }
125}