blob: 6037fdf8bf4d87c2d15482986cbd14ddbbcbcb87 [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 Tolnay886d8ea2016-09-13 08:34:07 -070014mod escape;
15
David Tolnayb79ee962016-09-04 09:39:20 -070016mod attr;
17pub use attr::{
18 Attribute,
19 MetaItem,
20};
David Tolnay35161ff2016-09-03 11:33:15 -070021
David Tolnayb79ee962016-09-04 09:39:20 -070022mod generics;
23pub use generics::{
24 Generics,
25 Lifetime,
26 LifetimeDef,
David Tolnay55337722016-09-11 12:58:56 -070027 TraitBoundModifier,
David Tolnayb79ee962016-09-04 09:39:20 -070028 TyParam,
29 TyParamBound,
30 WhereBoundPredicate,
David Tolnay55337722016-09-11 12:58:56 -070031 WhereClause,
David Tolnayb79ee962016-09-04 09:39:20 -070032 WherePredicate,
33 WhereRegionPredicate,
34};
David Tolnay35161ff2016-09-03 11:33:15 -070035
David Tolnay55337722016-09-11 12:58:56 -070036mod ident;
37pub use ident::{
38 Ident,
39};
40
David Tolnayb79ee962016-09-04 09:39:20 -070041mod item;
42pub use item::{
43 Body,
44 Field,
45 Item,
David Tolnayb79ee962016-09-04 09:39:20 -070046 Variant,
David Tolnay55337722016-09-11 12:58:56 -070047 VariantData,
48 Visibility,
David Tolnayb79ee962016-09-04 09:39:20 -070049};
David Tolnay35161ff2016-09-03 11:33:15 -070050
David Tolnayb79ee962016-09-04 09:39:20 -070051mod ty;
52pub use ty::{
53 AngleBracketedParameterData,
David Tolnayb79ee962016-09-04 09:39:20 -070054 BareFnTy,
David Tolnay66daf742016-09-07 08:21:49 -070055 FnArg,
David Tolnayb79ee962016-09-04 09:39:20 -070056 FnDecl,
57 FunctionRetTy,
58 MutTy,
59 Mutability,
60 ParenthesizedParameterData,
61 Path,
62 PathParameters,
63 PathSegment,
64 PolyTraitRef,
65 QSelf,
66 Ty,
67 TypeBinding,
68};
David Tolnay35161ff2016-09-03 11:33:15 -070069
David Tolnay55337722016-09-11 12:58:56 -070070#[cfg(feature = "aster")]
71pub mod aster;
David Tolnay7ebb9fb2016-09-03 12:07:47 -070072
David Tolnay55337722016-09-11 12:58:56 -070073#[cfg(feature = "visit")]
74pub mod visit;
75
76#[cfg(feature = "parsing")]
77pub use parsing::*;
78
79#[cfg(feature = "parsing")]
80mod parsing {
81 use super::*;
82 use {generics, item, ty};
83 use nom;
84
85 pub fn parse_item(input: &str) -> Result<Item, String> {
86 unwrap("item", item::parsing::item(input))
87 }
88
David Tolnay32a112e2016-09-11 17:46:15 -070089 pub fn parse_type(input: &str) -> Result<Ty, String> {
90 unwrap("type", ty::parsing::ty(input))
91 }
92
David Tolnay55337722016-09-11 12:58:56 -070093 pub fn parse_path(input: &str) -> Result<Path, String> {
94 unwrap("path", ty::parsing::path(input))
95 }
96
97 pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> {
98 unwrap("where clause", generics::parsing::where_clause(input))
99 }
100
101 fn unwrap<T>(name: &'static str, ires: nom::IResult<&str, T>) -> Result<T, String> {
102 return match ires {
103 nom::IResult::Done(rest, t) => {
104 if rest.is_empty() {
105 Ok(t)
106 } else {
107 Err(format!("remaining tokens after {}: {:?}", name, rest))
David Tolnayc94c38a2016-09-05 17:02:03 -0700108 }
David Tolnay55337722016-09-11 12:58:56 -0700109 }
110 nom::IResult::Error(err) => Err(root_cause(err)),
111 nom::IResult::Incomplete(_) => Err(format!("incomplete {}", name)),
112 };
113
114 fn root_cause(mut err: nom::Err<&str>) -> String {
115 loop {
116 match err {
117 nom::Err::Code(kind) => {
118 return format!("failed to parse {:?}", kind);
119 }
120 nom::Err::Position(kind, pos) => {
121 return format!("failed to parse {:?}: {:?}", kind, pos);
122 }
123 nom::Err::Node(_, next) |
124 nom::Err::NodePosition(_, _, next) => {
125 err = *next;
126 }
David Tolnayc94c38a2016-09-05 17:02:03 -0700127 }
David Tolnay7ebb9fb2016-09-03 12:07:47 -0700128 }
David Tolnay35161ff2016-09-03 11:33:15 -0700129 }
David Tolnay35161ff2016-09-03 11:33:15 -0700130 }
131}