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, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 122 | Token, |
| 123 | TokenTree, |
| 124 | }; |
| 125 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 126 | mod macro_input; |
| 127 | pub use macro_input::{ |
| 128 | Body, |
| 129 | MacroInput, |
| 130 | }; |
| 131 | |
David Tolnay | e0aa55a | 2016-10-02 14:55:23 -0700 | [diff] [blame] | 132 | #[cfg(feature = "parsing")] |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 133 | mod space; |
| 134 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 135 | mod ty; |
| 136 | pub use ty::{ |
| 137 | AngleBracketedParameterData, |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 138 | BareFnArg, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 139 | BareFnTy, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 140 | FunctionRetTy, |
| 141 | MutTy, |
| 142 | Mutability, |
| 143 | ParenthesizedParameterData, |
| 144 | Path, |
| 145 | PathParameters, |
| 146 | PathSegment, |
| 147 | PolyTraitRef, |
| 148 | QSelf, |
| 149 | Ty, |
| 150 | TypeBinding, |
| 151 | }; |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 152 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 153 | #[cfg(feature = "aster")] |
| 154 | pub mod aster; |
David Tolnay | 7ebb9fb | 2016-09-03 12:07:47 -0700 | [diff] [blame] | 155 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 156 | #[cfg(feature = "visit")] |
| 157 | pub mod visit; |
| 158 | |
| 159 | #[cfg(feature = "parsing")] |
| 160 | pub use parsing::*; |
| 161 | |
| 162 | #[cfg(feature = "parsing")] |
| 163 | mod parsing { |
| 164 | use super::*; |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 165 | use {generics, macro_input, ty}; |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 166 | use nom::IResult; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 167 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 168 | #[cfg(feature = "full")] |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 169 | use {expr, item, krate}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 170 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 171 | pub fn parse_macro_input(input: &str) -> Result<MacroInput, String> { |
| 172 | unwrap("macro input", macro_input::parsing::macro_input, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 175 | #[cfg(feature = "full")] |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 176 | pub fn parse_crate(input: &str) -> Result<Crate, String> { |
| 177 | unwrap("crate", krate::parsing::krate, input) |
| 178 | } |
| 179 | |
| 180 | #[cfg(feature = "full")] |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 181 | pub fn parse_item(input: &str) -> Result<Item, String> { |
| 182 | unwrap("item", item::parsing::item, input) |
| 183 | } |
| 184 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 185 | #[cfg(feature = "full")] |
| 186 | pub fn parse_expr(input: &str) -> Result<Expr, String> { |
| 187 | unwrap("expression", expr::parsing::expr, input) |
| 188 | } |
| 189 | |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 190 | pub fn parse_type(input: &str) -> Result<Ty, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 191 | unwrap("type", ty::parsing::ty, input) |
David Tolnay | 32a112e | 2016-09-11 17:46:15 -0700 | [diff] [blame] | 192 | } |
| 193 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 194 | pub fn parse_path(input: &str) -> Result<Path, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 195 | unwrap("path", ty::parsing::path, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | pub fn parse_where_clause(input: &str) -> Result<WhereClause, String> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 199 | unwrap("where clause", generics::parsing::where_clause, input) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 202 | 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] | 203 | match f(input) { |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 204 | IResult::Done(rest, t) => { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 205 | if rest.is_empty() { |
| 206 | Ok(t) |
| 207 | } else { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 208 | Err(format!("failed to parse tokens after {}: {:?}", name, rest)) |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 209 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 210 | } |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 211 | IResult::Error => Err(format!("failed to parse {}: {:?}", name, input)), |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 212 | } |
David Tolnay | 35161ff | 2016-09-03 11:33:15 -0700 | [diff] [blame] | 213 | } |
| 214 | } |