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