blob: 7b813cba6df3fb5c36d6040ec9f00307589376cf [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 Tolnay86eca752016-09-04 11:26:41 -07005#[cfg(feature = "parsing")]
David Tolnayb79ee962016-09-04 09:39:20 -07006#[macro_use]
David Tolnay6b7aaf02016-09-04 10:39:25 -07007mod do_parse;
8
9#[macro_use]
David Tolnayb79ee962016-09-04 09:39:20 -070010mod helper;
David Tolnay35161ff2016-09-03 11:33:15 -070011
David Tolnayb79ee962016-09-04 09:39:20 -070012mod attr;
13pub use attr::{
14 Attribute,
15 MetaItem,
16};
David Tolnay35161ff2016-09-03 11:33:15 -070017
David Tolnayb79ee962016-09-04 09:39:20 -070018mod common;
19pub use common::{
20 Ident,
21 Visibility,
22};
David Tolnay35161ff2016-09-03 11:33:15 -070023
David Tolnayb79ee962016-09-04 09:39:20 -070024mod generics;
25pub use generics::{
26 Generics,
27 Lifetime,
28 LifetimeDef,
29 TyParam,
30 TyParamBound,
31 WhereBoundPredicate,
32 WherePredicate,
33 WhereRegionPredicate,
34};
David Tolnay35161ff2016-09-03 11:33:15 -070035
David Tolnayb79ee962016-09-04 09:39:20 -070036mod item;
37pub use item::{
38 Body,
39 Field,
40 Item,
41 Style,
42 Variant,
43};
David Tolnay35161ff2016-09-03 11:33:15 -070044
David Tolnayb79ee962016-09-04 09:39:20 -070045mod ty;
46pub use ty::{
47 AngleBracketedParameterData,
48 Arg,
49 BareFnTy,
50 FnDecl,
51 FunctionRetTy,
52 MutTy,
53 Mutability,
54 ParenthesizedParameterData,
55 Path,
56 PathParameters,
57 PathSegment,
58 PolyTraitRef,
59 QSelf,
60 Ty,
61 TypeBinding,
62};
David Tolnay35161ff2016-09-03 11:33:15 -070063
David Tolnay86eca752016-09-04 11:26:41 -070064#[cfg(feature = "parsing")]
David Tolnay7ebb9fb2016-09-03 12:07:47 -070065pub fn parse(input: &str) -> Item {
David Tolnayb79ee962016-09-04 09:39:20 -070066 match item::item(input) {
67 nom::IResult::Done(rest, ast) => {
David Tolnay35161ff2016-09-03 11:33:15 -070068 if rest.is_empty() {
David Tolnay7ebb9fb2016-09-03 12:07:47 -070069 ast
David Tolnay35161ff2016-09-03 11:33:15 -070070 } else {
David Tolnay7ebb9fb2016-09-03 12:07:47 -070071 panic!("more than a single input item: {:?}", rest)
David Tolnay35161ff2016-09-03 11:33:15 -070072 }
73 }
David Tolnayb79ee962016-09-04 09:39:20 -070074 nom::IResult::Error(err) => raise(err),
75 nom::IResult::Incomplete(_) => panic!("incomplete input item"),
David Tolnay7ebb9fb2016-09-03 12:07:47 -070076 }
77}
78
David Tolnay86eca752016-09-04 11:26:41 -070079#[cfg(feature = "parsing")]
David Tolnay7ebb9fb2016-09-03 12:07:47 -070080fn raise(mut err: nom::Err<&str>) -> ! {
81 loop {
82 match err {
83 nom::Err::Code(kind) => {
84 panic!("failed to parse {:?}", kind)
85 }
86 nom::Err::Position(kind, pos) => {
87 panic!("failed to parse {:?}: {:?}", kind, pos)
88 }
89 nom::Err::Node(_, next) |
90 nom::Err::NodePosition(_, _, next) => {
91 err = *next;
92 }
David Tolnay35161ff2016-09-03 11:33:15 -070093 }
David Tolnay35161ff2016-09-03 11:33:15 -070094 }
95}