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