David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame^] | 1 | #![cfg_attr(feature = "clippy", feature(plugin))] |
| 2 | #![cfg_attr(feature = "clippy", plugin(clippy))] |
| 3 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 4 | #[cfg(feature = "printing")] |
| 5 | extern crate quote; |
| 6 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 7 | #[cfg(feature = "parsing")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | #[macro_use] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 9 | mod nom; |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 10 | |
| 11 | #[macro_use] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 12 | mod helper; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 13 | |
David Tolnay | 886d8ea | 2016-09-13 08:34:07 -0700 | [diff] [blame] | 14 | mod escape; |
| 15 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 16 | mod attr; |
| 17 | pub use attr::{ |
| 18 | Attribute, |
| 19 | MetaItem, |
| 20 | }; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 21 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 22 | mod data; |
| 23 | pub use data::{ |
| 24 | Discriminant, |
| 25 | Field, |
| 26 | Variant, |
| 27 | VariantData, |
| 28 | Visibility, |
| 29 | }; |
| 30 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 31 | #[cfg(feature = "full")] |
| 32 | mod expr; |
| 33 | #[cfg(feature = "full")] |
| 34 | pub use expr::{ |
| 35 | Arm, |
| 36 | BinOp, |
| 37 | BindingMode, |
| 38 | Block, |
| 39 | BlockCheckMode, |
| 40 | CaptureBy, |
| 41 | Expr, |
| 42 | FieldPat, |
| 43 | Local, |
| 44 | MacStmtStyle, |
| 45 | Pat, |
| 46 | RangeLimits, |
| 47 | Stmt, |
| 48 | UnOp, |
| 49 | }; |
| 50 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 51 | mod generics; |
| 52 | pub use generics::{ |
| 53 | Generics, |
| 54 | Lifetime, |
| 55 | LifetimeDef, |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 56 | TraitBoundModifier, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 57 | TyParam, |
| 58 | TyParamBound, |
| 59 | WhereBoundPredicate, |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 60 | WhereClause, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 61 | WherePredicate, |
| 62 | WhereRegionPredicate, |
| 63 | }; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 64 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 65 | mod ident; |
| 66 | pub use ident::{ |
| 67 | Ident, |
| 68 | }; |
| 69 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 70 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 71 | mod item; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 72 | #[cfg(feature = "full")] |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 73 | pub use item::{ |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 74 | Abi, |
| 75 | Constness, |
| 76 | Defaultness, |
| 77 | ForeignItemKind, |
| 78 | ForeignItem, |
| 79 | ForeignMod, |
| 80 | ImplItem, |
| 81 | ImplItemKind, |
| 82 | ImplPolarity, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 83 | Item, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 84 | ItemKind, |
| 85 | MethodSig, |
| 86 | PathListItem, |
| 87 | TraitItem, |
| 88 | TraitItemKind, |
| 89 | Unsafety, |
| 90 | ViewPath, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 91 | }; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 92 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 93 | mod lit; |
| 94 | pub use lit::{ |
| 95 | FloatTy, |
| 96 | IntTy, |
| 97 | Lit, |
| 98 | StrStyle, |
| 99 | }; |
| 100 | |
| 101 | #[cfg(feature = "full")] |
| 102 | mod mac; |
| 103 | #[cfg(feature = "full")] |
| 104 | pub use mac::{ |
| 105 | BinOpToken, |
| 106 | DelimToken, |
| 107 | Delimited, |
| 108 | Mac, |
| 109 | SequenceRepetition, |
| 110 | Token, |
| 111 | TokenTree, |
| 112 | }; |
| 113 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 114 | mod macro_input; |
| 115 | pub use macro_input::{ |
| 116 | Body, |
| 117 | MacroInput, |
| 118 | }; |
| 119 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 120 | mod ty; |
| 121 | pub use ty::{ |
| 122 | AngleBracketedParameterData, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 123 | BareFnTy, |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame] | 124 | FnArg, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 125 | FnDecl, |
| 126 | FunctionRetTy, |
| 127 | MutTy, |
| 128 | Mutability, |
| 129 | ParenthesizedParameterData, |
| 130 | Path, |
| 131 | PathParameters, |
| 132 | PathSegment, |
| 133 | PolyTraitRef, |
| 134 | QSelf, |
| 135 | Ty, |
| 136 | TypeBinding, |
| 137 | }; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 138 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 139 | #[cfg(feature = "aster")] |
| 140 | pub mod aster; |
David Tolnay | 7ebb9fb | 2016-09-03 12:07:47 -0700 | [diff] [blame] | 141 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 142 | #[cfg(feature = "visit")] |
| 143 | pub mod visit; |
| 144 | |
| 145 | #[cfg(feature = "parsing")] |
| 146 | pub use parsing::*; |
| 147 | |
| 148 | #[cfg(feature = "parsing")] |
| 149 | mod parsing { |
| 150 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 151 | use {generics, macro_input, ty}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 152 | use nom; |
| 153 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 154 | #[cfg(feature = "full")] |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 155 | use {expr, item}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 156 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 157 | pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> { |
| 158 | unwrap("macro input", macro_input::parsing::macro_input, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 159 | } |
| 160 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 161 | #[cfg(feature = "full")] |
| 162 | pub fn parse_item(input: &str) -> Result<Item, String> { |
| 163 | unwrap("item", item::parsing::item, input) |
| 164 | } |
| 165 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 166 | #[cfg(feature = "full")] |
| 167 | pub fn parse_expr(input: &str) -> Result<Expr, String> { |
| 168 | unwrap("expression", expr::parsing::expr, input) |
| 169 | } |
| 170 | |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 171 | pub fn parse_type(input: &str) -> Result<Ty, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 172 | unwrap("type", ty::parsing::ty, input) |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 173 | } |
| 174 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 175 | pub fn parse_path(input: &str) -> Result<Path, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 176 | unwrap("path", ty::parsing::path, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 180 | unwrap("where clause", generics::parsing::where_clause, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 181 | } |
| 182 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 183 | fn unwrap<T>(name: &'static str, f: fn(&str) -> nom::IResult<&str, T>, input: &str) -> Result<T, String> { |
| 184 | match f(input) { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 185 | nom::IResult::Done(rest, t) => { |
| 186 | if rest.is_empty() { |
| 187 | Ok(t) |
| 188 | } else { |
| 189 | Err(format!("remaining tokens after {}: {:?}", name, rest)) |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 190 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 191 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 192 | nom::IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)), |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 193 | } |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 194 | } |
| 195 | } |