blob: 2bee31453d6a2bf782e7b9c27f08e32a91c0b425 [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
David Tolnay32a112e2016-09-11 17:46:15 -070087 pub fn parse_type(input: &str) -> Result<Ty, String> {
88 unwrap("type", ty::parsing::ty(input))
89 }
90
David Tolnay55337722016-09-11 12:58:56 -070091 pub fn parse_path(input: &str) -> Result<Path, String> {
92 unwrap("path", ty::parsing::path(input))
93 }
94
95 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
96 unwrap("where clause", generics::parsing::where_clause(input))
97 }
98
99 fn unwrap<T>(name: &'static str, ires: nom::IResult<&str, T>) -> Result<T, String> {
100 return match ires {
101 nom::IResult::Done(rest, t) => {
102 if rest.is_empty() {
103 Ok(t)
104 } else {
105 Err(format!("remaining tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700106 }
David Tolnay55337722016-09-11 12:58:56 -0700107 }
108 nom::IResult::Error(err) => Err(root_cause(err)),
109 nom::IResult::Incomplete(_) => Err(format!("incomplete {}", name)),
110 };
111
112 fn root_cause(mut err: nom::Err<&str>) -> String {
113 loop {
114 match err {
115 nom::Err::Code(kind) => {
116 return format!("failed to parse {:?}", kind);
117 }
118 nom::Err::Position(kind, pos) => {
119 return format!("failed to parse {:?}: {:?}", kind, pos);
120 }
121 nom::Err::Node(_, next) |
122 nom::Err::NodePosition(_, _, next) => {
123 err = *next;
124 }
David Tolnayc94c38a2016-09-05 17:02:03 -0700125 }
David Tolnay7ebb9fb2016-09-03 12:07:47 -0700126 }
David Tolnay35161ff2016-09-03 11:33:15 -0700127 }
David Tolnay35161ff2016-09-03 11:33:15 -0700128 }
129}