blob: de2418c6ffa41b92254ccc243a3bef6c56a68c66 [file] [log] [blame]
David Tolnay35161ff2016-09-03 11:33:15 -07001#[macro_use]
2extern crate nom;
3
David Tolnayb79ee962016-09-04 09:39:20 -07004#[macro_use]
5mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -07006
David Tolnayb79ee962016-09-04 09:39:20 -07007mod attr;
8pub use attr::{
9 Attribute,
10 MetaItem,
11};
David Tolnay35161ff2016-09-03 11:33:15 -070012
David Tolnayb79ee962016-09-04 09:39:20 -070013mod common;
14pub use common::{
15 Ident,
16 Visibility,
17};
David Tolnay35161ff2016-09-03 11:33:15 -070018
David Tolnayb79ee962016-09-04 09:39:20 -070019mod generics;
20pub use generics::{
21 Generics,
22 Lifetime,
23 LifetimeDef,
24 TyParam,
25 TyParamBound,
26 WhereBoundPredicate,
27 WherePredicate,
28 WhereRegionPredicate,
29};
David Tolnay35161ff2016-09-03 11:33:15 -070030
David Tolnayb79ee962016-09-04 09:39:20 -070031mod item;
32pub use item::{
33 Body,
34 Field,
35 Item,
36 Style,
37 Variant,
38};
David Tolnay35161ff2016-09-03 11:33:15 -070039
David Tolnayb79ee962016-09-04 09:39:20 -070040mod ty;
41pub use ty::{
42 AngleBracketedParameterData,
43 Arg,
44 BareFnTy,
45 FnDecl,
46 FunctionRetTy,
47 MutTy,
48 Mutability,
49 ParenthesizedParameterData,
50 Path,
51 PathParameters,
52 PathSegment,
53 PolyTraitRef,
54 QSelf,
55 Ty,
56 TypeBinding,
57};
David Tolnay35161ff2016-09-03 11:33:15 -070058
David Tolnay7ebb9fb2016-09-03 12:07:47 -070059pub fn parse(input: &str) -> Item {
David Tolnayb79ee962016-09-04 09:39:20 -070060 match item::item(input) {
61 nom::IResult::Done(rest, ast) => {
David Tolnay35161ff2016-09-03 11:33:15 -070062 if rest.is_empty() {
David Tolnay7ebb9fb2016-09-03 12:07:47 -070063 ast
David Tolnay35161ff2016-09-03 11:33:15 -070064 } else {
David Tolnay7ebb9fb2016-09-03 12:07:47 -070065 panic!("more than a single input item: {:?}", rest)
David Tolnay35161ff2016-09-03 11:33:15 -070066 }
67 }
David Tolnayb79ee962016-09-04 09:39:20 -070068 nom::IResult::Error(err) => raise(err),
69 nom::IResult::Incomplete(_) => panic!("incomplete input item"),
David Tolnay7ebb9fb2016-09-03 12:07:47 -070070 }
71}
72
73fn raise(mut err: nom::Err<&str>) -> ! {
74 loop {
75 match err {
76 nom::Err::Code(kind) => {
77 panic!("failed to parse {:?}", kind)
78 }
79 nom::Err::Position(kind, pos) => {
80 panic!("failed to parse {:?}: {:?}", kind, pos)
81 }
82 nom::Err::Node(_, next) |
83 nom::Err::NodePosition(_, _, next) => {
84 err = *next;
85 }
David Tolnay35161ff2016-09-03 11:33:15 -070086 }
David Tolnay35161ff2016-09-03 11:33:15 -070087 }
88}