Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1 | use delimited::Delimited; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 2 | use super::*; |
| 3 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 4 | ast_enum_of_structs! { |
| 5 | /// The different kinds of types recognized by the compiler |
| 6 | pub enum Ty { |
| 7 | /// A variable-length array (`[T]`) |
| 8 | pub Slice(TySlice { |
| 9 | pub ty: Box<Ty>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 10 | pub bracket_token: tokens::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 11 | }), |
| 12 | /// A fixed length array (`[T; n]`) |
| 13 | pub Array(TyArray { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 14 | pub bracket_token: tokens::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 15 | pub ty: Box<Ty>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 16 | pub semi_token: tokens::Semi, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 17 | pub amt: ConstExpr, |
| 18 | }), |
| 19 | /// A raw pointer (`*const T` or `*mut T`) |
| 20 | pub Ptr(TyPtr { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 21 | pub star_token: tokens::Star, |
| 22 | pub const_token: Option<tokens::Const>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 23 | pub ty: Box<MutTy>, |
| 24 | }), |
| 25 | /// A reference (`&'a T` or `&'a mut T`) |
| 26 | pub Rptr(TyRptr { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 27 | pub and_token: tokens::And, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 28 | pub lifetime: Option<Lifetime>, |
| 29 | pub ty: Box<MutTy>, |
| 30 | }), |
| 31 | /// A bare function (e.g. `fn(usize) -> bool`) |
| 32 | pub BareFn(TyBareFn { |
| 33 | pub ty: Box<BareFnTy>, |
| 34 | }), |
| 35 | /// The never type (`!`) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 36 | pub Never(TyNever { |
| 37 | pub bang_token: tokens::Bang, |
| 38 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 39 | /// A tuple (`(A, B, C, D, ...)`) |
| 40 | pub Tup(TyTup { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 41 | pub paren_token: tokens::Paren, |
| 42 | pub tys: Delimited<Ty, tokens::Comma>, |
| 43 | pub lone_comma: Option<tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 44 | }), |
| 45 | /// A path (`module::module::...::Type`), optionally |
| 46 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 47 | /// |
| 48 | /// Type parameters are stored in the Path itself |
| 49 | pub Path(TyPath { |
| 50 | pub qself: Option<QSelf>, |
| 51 | pub path: Path, |
| 52 | }), |
| 53 | /// A trait object type `Bound1 + Bound2 + Bound3` |
| 54 | /// where `Bound` is a trait or a lifetime. |
| 55 | pub TraitObject(TyTraitObject { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 56 | pub bounds: Delimited<TyParamBound, tokens::Add>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 57 | }), |
| 58 | /// An `impl Bound1 + Bound2 + Bound3` type |
| 59 | /// where `Bound` is a trait or a lifetime. |
| 60 | pub ImplTrait(TyImplTrait { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 61 | pub impl_token: tokens::Impl, |
| 62 | pub bounds: Delimited<TyParamBound, tokens::Add>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 63 | }), |
| 64 | /// No-op; kept solely so that we can pretty-print faithfully |
| 65 | pub Paren(TyParen { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 66 | pub paren_token: tokens::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 67 | pub ty: Box<Ty>, |
| 68 | }), |
| 69 | /// TyKind::Infer means the type should be inferred instead of it having been |
| 70 | /// specified. This can appear anywhere in a type. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 71 | pub Infer(TyInfer { |
| 72 | pub underscore_token: tokens::Underscore |
| 73 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 74 | /// A macro in the type position. |
| 75 | pub Mac(Mac), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | ast_struct! { |
| 80 | pub struct MutTy { |
| 81 | pub ty: Ty, |
| 82 | pub mutability: Mutability, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 87 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 88 | pub enum Mutability { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 89 | Mutable(tokens::Mut), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 90 | Immutable, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | ast_struct! { |
| 95 | /// A "Path" is essentially Rust's notion of a name. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 96 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 97 | /// It's represented as a sequence of identifiers, |
| 98 | /// along with a bunch of supporting information. |
| 99 | /// |
| 100 | /// E.g. `std::cmp::PartialEq` |
| 101 | pub struct Path { |
| 102 | /// A `::foo` path, is relative to the crate root rather than current |
| 103 | /// module (like paths in an import). |
| 104 | pub global: bool, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 105 | pub leading_colon: Option<tokens::Colon2>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 106 | /// The segments in the path: the things separated by `::`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 107 | pub segments: Delimited<PathSegment, tokens::Colon2>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 108 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 111 | #[cfg(feature = "printing")] |
| 112 | ast_struct! { |
| 113 | pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path); |
| 114 | } |
| 115 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 116 | impl<T> From<T> for Path |
| 117 | where T: Into<PathSegment> |
| 118 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 119 | fn from(segment: T) -> Self { |
| 120 | Path { |
| 121 | global: false, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 122 | leading_colon: None, |
| 123 | segments: vec![(segment.into(), None)].into(), |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 128 | ast_struct! { |
| 129 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
| 130 | /// |
| 131 | /// E.g. `std`, `String` or `Box<T>` |
| 132 | pub struct PathSegment { |
| 133 | /// The identifier portion of this path segment. |
| 134 | pub ident: Ident, |
| 135 | /// Type/lifetime parameters attached to this path. They come in |
| 136 | /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that |
| 137 | /// this is more than just simple syntactic sugar; the use of |
| 138 | /// parens affects the region binding rules, so we preserve the |
| 139 | /// distinction. |
| 140 | pub parameters: PathParameters, |
| 141 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 142 | } |
| 143 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 144 | impl<T> From<T> for PathSegment |
| 145 | where T: Into<Ident> |
| 146 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 147 | fn from(ident: T) -> Self { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 148 | PathSegment { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 149 | ident: ident.into(), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 150 | parameters: PathParameters::none(), |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 155 | ast_enum! { |
| 156 | /// Parameters of a path segment. |
| 157 | /// |
| 158 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
| 159 | pub enum PathParameters { |
| 160 | /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>` |
| 161 | AngleBracketed(AngleBracketedParameterData), |
| 162 | /// The `(A, B)` and `C` in `Foo(A, B) -> C` |
| 163 | Parenthesized(ParenthesizedParameterData), |
| 164 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | impl PathParameters { |
| 168 | pub fn none() -> Self { |
| 169 | PathParameters::AngleBracketed(AngleBracketedParameterData::default()) |
| 170 | } |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 171 | |
| 172 | pub fn is_empty(&self) -> bool { |
| 173 | match *self { |
| 174 | PathParameters::AngleBracketed(ref bracketed) => { |
David Tolnay | c1fea50 | 2016-10-30 17:54:02 -0700 | [diff] [blame] | 175 | bracketed.lifetimes.is_empty() && bracketed.types.is_empty() && |
| 176 | bracketed.bindings.is_empty() |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 177 | } |
| 178 | PathParameters::Parenthesized(_) => false, |
| 179 | } |
| 180 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 183 | ast_struct! { |
| 184 | /// A path like `Foo<'a, T>` |
| 185 | #[derive(Default)] |
| 186 | pub struct AngleBracketedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 187 | pub lt_token: Option<tokens::Lt>, |
| 188 | pub gt_token: Option<tokens::Gt>, |
| 189 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 190 | /// The lifetime parameters for this path segment. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 191 | pub lifetimes: Delimited<Lifetime, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 192 | /// The type parameters for this path segment, if present. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 193 | pub types: Delimited<Ty, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 194 | /// Bindings (equality constraints) on associated types, if present. |
| 195 | /// |
| 196 | /// E.g., `Foo<A=Bar>`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 197 | pub bindings: Delimited<TypeBinding, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | ast_struct! { |
| 202 | /// Bind a type to an associated type: `A=Foo`. |
| 203 | pub struct TypeBinding { |
| 204 | pub ident: Ident, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 205 | pub eq_token: tokens::Eq, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 206 | pub ty: Ty, |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | ast_struct! { |
| 212 | /// A path like `Foo(A,B) -> C` |
| 213 | pub struct ParenthesizedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 214 | pub paren_token: tokens::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 215 | /// `(A, B)` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 216 | pub inputs: Delimited<Ty, tokens::Comma>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 217 | /// `C` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 218 | pub output: FunctionRetTy, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | ast_struct! { |
| 223 | pub struct PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 224 | /// The `for<'a>` in `for<'a> Foo<&'a T>` |
| 225 | pub bound_lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 226 | /// The `Foo<&'a T>` in `<'a> Foo<&'a T>` |
| 227 | pub trait_ref: Path, |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | ast_struct! { |
| 232 | /// The explicit Self type in a "qualified path". The actual |
| 233 | /// path, including the trait and the associated item, is stored |
| 234 | /// separately. `position` represents the index of the associated |
| 235 | /// item qualified with this Self type. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 236 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 237 | /// ```rust,ignore |
| 238 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
| 239 | /// ^~~~~ ~~~~~~~~~~~~~~^ |
| 240 | /// ty position = 3 |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 241 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 242 | /// <Vec<T>>::AssociatedItem |
| 243 | /// ^~~~~ ^ |
| 244 | /// ty position = 0 |
| 245 | /// ``` |
| 246 | pub struct QSelf { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 247 | pub lt_token: tokens::Lt, |
| 248 | pub gt_token: tokens::Gt, |
| 249 | pub as_token: Option<tokens::As>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 250 | pub ty: Box<Ty>, |
| 251 | pub position: usize, |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | ast_struct! { |
| 256 | pub struct BareFnTy { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 257 | pub lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 258 | pub unsafety: Unsafety, |
| 259 | pub abi: Option<Abi>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 260 | pub fn_token: tokens::Fn, |
| 261 | pub paren_token: tokens::Paren, |
| 262 | pub inputs: Delimited<BareFnArg, tokens::Comma>, |
| 263 | pub variadic: Option<tokens::Dot3>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 264 | pub output: FunctionRetTy, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 269 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 270 | pub enum Unsafety { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 271 | Unsafe(tokens::Unsafe), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 272 | Normal, |
| 273 | } |
| 274 | } |
| 275 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 276 | ast_struct! { |
| 277 | pub struct Abi { |
| 278 | pub extern_token: tokens::Extern, |
| 279 | pub kind: AbiKind, |
| 280 | } |
| 281 | } |
| 282 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 283 | ast_enum! { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 284 | pub enum AbiKind { |
| 285 | Named(Lit), |
| 286 | Default, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | ast_struct! { |
| 291 | /// An argument in a function type. |
| 292 | /// |
| 293 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 294 | pub struct BareFnArg { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 295 | pub name: Option<(Ident, tokens::Colon)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 296 | pub ty: Ty, |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | ast_enum! { |
| 302 | pub enum FunctionRetTy { |
| 303 | /// Return type is not specified. |
| 304 | /// |
| 305 | /// Functions default to `()` and |
| 306 | /// closures default to inference. Span points to where return |
| 307 | /// type would be inserted. |
| 308 | Default, |
| 309 | /// Everything else |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 310 | Ty(Ty, tokens::RArrow), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 311 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 312 | } |
| 313 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 314 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 315 | pub mod parsing { |
| 316 | use super::*; |
David Tolnay | a6ac181 | 2017-01-23 00:29:11 -0800 | [diff] [blame] | 317 | use {TyParamBound, TraitBoundModifier}; |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 318 | #[cfg(feature = "full")] |
| 319 | use ConstExpr; |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 320 | #[cfg(feature = "full")] |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 321 | use constant::parsing::const_expr; |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 322 | #[cfg(feature = "full")] |
| 323 | use expr::parsing::expr; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 324 | use generics::parsing::{lifetime, ty_param_bound, bound_lifetimes}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 325 | use ident::parsing::ident; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 326 | use lit::parsing::string; |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 327 | use mac::parsing::mac; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 328 | #[cfg(feature = "full")] |
| 329 | use synom::{IResult, TokenTree}; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 330 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 331 | named!(pub ty -> Ty, alt!( |
David Tolnay | d040d77 | 2016-10-25 21:33:51 -0700 | [diff] [blame] | 332 | ty_paren // must be before ty_tup |
| 333 | | |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 334 | ty_mac // must be before ty_path |
| 335 | | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 336 | ty_path // must be before ty_poly_trait_ref |
| 337 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 338 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 339 | | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 340 | ty_array |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 341 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 342 | ty_ptr |
| 343 | | |
| 344 | ty_rptr |
| 345 | | |
| 346 | ty_bare_fn |
| 347 | | |
| 348 | ty_never |
| 349 | | |
| 350 | ty_tup |
| 351 | | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 352 | ty_poly_trait_ref |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 353 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 354 | ty_impl_trait |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 355 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 356 | |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 357 | named!(ty_mac -> Ty, map!(mac, Ty::Mac)); |
| 358 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 359 | named!(ty_vec -> Ty, delim!(Bracket, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 360 | elem: ty >> |
| 361 | punct!("]") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 362 | (TySlice { |
| 363 | ty: Box::new(elem), |
| 364 | bracket_token: tokens::Bracket::default(), |
| 365 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 366 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 367 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 368 | named!(ty_array -> Ty, delim!(Bracket, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 369 | elem: ty >> |
| 370 | punct!(";") >> |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 371 | len: array_len >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 372 | punct!("]") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 373 | (TyArray { |
| 374 | ty: Box::new(elem), |
| 375 | amt: len, |
| 376 | bracket_token: tokens::Bracket::default(), |
| 377 | semi_token: tokens::Semi::default(), |
| 378 | }.into()) |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 379 | )); |
| 380 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 381 | #[cfg(not(feature = "full"))] |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 382 | use constant::parsing::const_expr as array_len; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 383 | |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 384 | #[cfg(feature = "full")] |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 385 | named!(array_len -> ConstExpr, alt!( |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 386 | terminated!(const_expr, input_end!()) |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 387 | | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 388 | terminated!(expr, input_end!()) => { ConstExpr::Other } |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 389 | )); |
| 390 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 391 | named!(ty_ptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 392 | punct!("*") >> |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 393 | mutability: alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 394 | keyword!("const") => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 395 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 396 | keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 397 | ) >> |
| 398 | target: ty >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 399 | (TyPtr { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 400 | const_token: match mutability { |
| 401 | Mutability::Mutable(_) => None, |
| 402 | Mutability::Immutable => Some(tokens::Const::default()), |
| 403 | }, |
| 404 | star_token: tokens::Star::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 405 | ty: Box::new(MutTy { |
| 406 | ty: target, |
| 407 | mutability: mutability, |
| 408 | }), |
| 409 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 410 | )); |
| 411 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 412 | named!(ty_rptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 413 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 414 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 415 | mutability: mutability >> |
| 416 | target: ty >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 417 | (TyRptr { |
| 418 | lifetime: life, |
| 419 | ty: Box::new(MutTy { |
| 420 | ty: target, |
| 421 | mutability: mutability, |
| 422 | }), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 423 | and_token: tokens::And::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 424 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 425 | )); |
| 426 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 427 | named!(ty_bare_fn -> Ty, do_parse!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 428 | lifetimes: bound_lifetimes >> |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 429 | unsafety: unsafety >> |
| 430 | abi: option!(abi) >> |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame] | 431 | keyword!("fn") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 432 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 433 | inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 434 | fn_arg) >> |
| 435 | variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(), |
| 436 | punct!("..."))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 437 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 438 | output: fn_ret_ty >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 439 | (TyBareFn { |
| 440 | ty: Box::new(BareFnTy { |
| 441 | unsafety: unsafety, |
| 442 | abi: abi, |
| 443 | lifetimes: lifetimes, |
| 444 | inputs: inputs, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 445 | output: output, |
| 446 | variadic: variadic.map(|_| tokens::Dot3::default()), |
| 447 | fn_token: tokens::Fn::default(), |
| 448 | paren_token: tokens::Paren::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 449 | }), |
| 450 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 451 | )); |
| 452 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 453 | named!(ty_never -> Ty, map!(punct!("!"), |_| TyNever { |
| 454 | bang_token: tokens::Bang::default(), |
| 455 | }.into())); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 456 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 457 | named!(ty_tup -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 458 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 459 | elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 460 | ty) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 461 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 462 | (TyTup { |
| 463 | tys: elems, |
| 464 | paren_token: tokens::Paren::default(), |
| 465 | lone_comma: None, // TODO: does this just not parse? |
| 466 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 467 | )); |
| 468 | |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 469 | named!(ty_path -> Ty, do_parse!( |
| 470 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 471 | parenthesized: cond!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 472 | qpath.1.segments.get(qpath.1.segments.len() - 1).item().parameters.is_empty(), |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 473 | option!(parenthesized_parameter_data) |
| 474 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 475 | bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >> |
| 476 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 477 | let (qself, mut path) = qpath; |
| 478 | if let Some(Some(parenthesized)) = parenthesized { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 479 | let len = path.segments.len(); |
| 480 | path.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 481 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 482 | if bounds.is_empty() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 483 | TyPath { qself: qself, path: path }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 484 | } else { |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 485 | let path = TyParamBound::Trait( |
| 486 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 487 | bound_lifetimes: None, |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 488 | trait_ref: path, |
| 489 | }, |
| 490 | TraitBoundModifier::None, |
| 491 | ); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 492 | let mut new_bounds = Delimited::new(); |
| 493 | new_bounds.push_first(path); |
| 494 | for bound in bounds { |
| 495 | new_bounds.push_default(bound); |
| 496 | } |
| 497 | TyTraitObject { bounds: new_bounds }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 498 | } |
| 499 | }) |
| 500 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 501 | |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 502 | named!(parenthesized_parameter_data -> PathParameters, do_parse!( |
| 503 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 504 | inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 505 | ty) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 506 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 507 | output: fn_ret_ty >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 508 | (PathParameters::Parenthesized( |
| 509 | ParenthesizedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 510 | paren_token: tokens::Paren::default(), |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 511 | inputs: inputs, |
| 512 | output: output, |
| 513 | }, |
| 514 | )) |
| 515 | )); |
| 516 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 517 | named!(pub fn_ret_ty -> FunctionRetTy, map!( |
| 518 | option!(preceded!( |
| 519 | punct!("->"), |
| 520 | ty |
| 521 | )), |
| 522 | |ty| { |
| 523 | match ty { |
| 524 | Some(ty) => FunctionRetTy::Ty(ty, tokens::RArrow::default()), |
| 525 | None => FunctionRetTy::Default, |
| 526 | } |
| 527 | } |
| 528 | )); |
| 529 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 530 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
| 531 | map!(path, |p| (None, p)) |
| 532 | | |
| 533 | do_parse!( |
| 534 | punct!("<") >> |
| 535 | this: map!(ty, Box::new) >> |
| 536 | path: option!(preceded!( |
| 537 | keyword!("as"), |
| 538 | path |
| 539 | )) >> |
| 540 | punct!(">") >> |
| 541 | punct!("::") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 542 | rest: separated_nonempty_list!( |
| 543 | map!(punct!("::"), |_| tokens::Colon2::default()), |
| 544 | path_segment |
| 545 | ) >> |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 546 | ({ |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 547 | let as_token = path.as_ref().map(|_| tokens::As::default()); |
| 548 | let (pos, path) = match path { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 549 | Some(mut path) => { |
| 550 | let pos = path.segments.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 551 | if !path.segments.is_empty() && !path.segments.trailing_delim() { |
| 552 | path.segments.push_trailing(tokens::Colon2::default()); |
| 553 | } |
| 554 | for item in rest.into_iter() { |
| 555 | path.segments.push(item); |
| 556 | } |
| 557 | (pos, path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 558 | } |
| 559 | None => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 560 | (0, Path { |
| 561 | leading_colon: None, |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 562 | global: false, |
| 563 | segments: rest, |
| 564 | }) |
| 565 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 566 | }; |
| 567 | (Some(QSelf { |
| 568 | ty: this, |
| 569 | position: pos, |
| 570 | gt_token: tokens::Gt::default(), |
| 571 | lt_token: tokens::Lt::default(), |
| 572 | as_token: as_token, |
| 573 | }), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 574 | }) |
| 575 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 576 | | |
| 577 | map!(keyword!("self"), |_| (None, "self".into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 578 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 579 | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 580 | named!(ty_poly_trait_ref -> Ty, map!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 581 | separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()), |
| 582 | ty_param_bound), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 583 | |x| TyTraitObject { bounds: x }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 584 | )); |
| 585 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 586 | named!(ty_impl_trait -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 587 | keyword!("impl") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 588 | elem: separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()), |
| 589 | ty_param_bound) >> |
| 590 | (TyImplTrait { |
| 591 | impl_token: tokens::Impl::default(), |
| 592 | bounds: elem, |
| 593 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 594 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 595 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 596 | named!(ty_paren -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 597 | punct!("(") >> |
| 598 | elem: ty >> |
| 599 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 600 | (TyParen { |
| 601 | paren_token: tokens::Paren::default(), |
| 602 | ty: Box::new(elem), |
| 603 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 604 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 605 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 606 | named!(pub mutability -> Mutability, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 607 | keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) } |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 608 | | |
| 609 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 610 | )); |
| 611 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 612 | named!(pub path -> Path, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 613 | global: option!(punct!("::")) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 614 | segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()), |
| 615 | path_segment) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 616 | (Path { |
| 617 | global: global.is_some(), |
| 618 | segments: segments, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 619 | leading_colon: global.map(|_| tokens::Colon2::default()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 620 | }) |
| 621 | )); |
| 622 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 623 | named!(path_segment -> PathSegment, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 624 | do_parse!( |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 625 | id: option!(ident) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 626 | punct!("<") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 627 | lifetimes: terminated_list!( |
| 628 | map!(punct!(","), |_| tokens::Comma::default()), |
| 629 | lifetime |
| 630 | ) >> |
| 631 | types: cond!( |
| 632 | lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 633 | terminated_list!( |
| 634 | map!(punct!(","), |_| tokens::Comma::default()), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 635 | terminated!(ty, not!(punct!("="))) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 636 | ) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 637 | ) >> |
| 638 | bindings: cond!( |
| 639 | match types { |
| 640 | Some(ref t) => t.is_empty() || t.trailing_delim(), |
| 641 | None => lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 642 | }, |
| 643 | terminated_list!( |
| 644 | map!(punct!(","), |_| tokens::Comma::default()), |
| 645 | type_binding |
| 646 | ) |
| 647 | ) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 648 | punct!(">") >> |
| 649 | (PathSegment { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 650 | ident: id.unwrap_or_else(|| "".into()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 651 | parameters: PathParameters::AngleBracketed( |
| 652 | AngleBracketedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 653 | gt_token: Some(tokens::Gt::default()), |
| 654 | lt_token: Some(tokens::Lt::default()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 655 | lifetimes: lifetimes, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 656 | types: types.unwrap_or_default(), |
| 657 | bindings: bindings.unwrap_or_default(), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 658 | } |
| 659 | ), |
| 660 | }) |
| 661 | ) |
| 662 | | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 663 | map!(ident, Into::into) |
David Tolnay | 7780722 | 2016-10-24 22:30:15 -0700 | [diff] [blame] | 664 | | |
David Tolnay | e14e3be | 2016-10-24 22:53:07 -0700 | [diff] [blame] | 665 | map!(alt!( |
| 666 | keyword!("super") |
| 667 | | |
| 668 | keyword!("self") |
| 669 | | |
| 670 | keyword!("Self") |
| 671 | ), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 672 | )); |
| 673 | |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 674 | named!(pub mod_style_path -> Path, do_parse!( |
| 675 | global: option!(punct!("::")) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 676 | segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()), |
| 677 | mod_style_path_segment) >> |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 678 | (Path { |
| 679 | global: global.is_some(), |
| 680 | segments: segments, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 681 | leading_colon: global.map(|_| tokens::Colon2::default()), |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 682 | }) |
| 683 | )); |
| 684 | |
| 685 | named!(mod_style_path_segment -> PathSegment, alt!( |
| 686 | map!(ident, Into::into) |
| 687 | | |
| 688 | map!(alt!( |
| 689 | keyword!("super") |
| 690 | | |
| 691 | keyword!("self") |
| 692 | | |
| 693 | keyword!("Self") |
| 694 | ), Into::into) |
| 695 | )); |
| 696 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 697 | named!(type_binding -> TypeBinding, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 698 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 699 | punct!("=") >> |
| 700 | ty: ty >> |
| 701 | (TypeBinding { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 702 | ident: id, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 703 | eq_token: tokens::Eq::default(), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 704 | ty: ty, |
| 705 | }) |
| 706 | )); |
| 707 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 708 | named!(pub poly_trait_ref -> PolyTraitRef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 709 | bound_lifetimes: bound_lifetimes >> |
| 710 | trait_ref: path >> |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 711 | parenthesized: option!(cond_reduce!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 712 | trait_ref.segments.get(trait_ref.segments.len() - 1).item().parameters.is_empty(), |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 713 | parenthesized_parameter_data |
| 714 | )) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 715 | ({ |
| 716 | let mut trait_ref = trait_ref; |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 717 | if let Some(parenthesized) = parenthesized { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 718 | let len = trait_ref.segments.len(); |
| 719 | trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 720 | } |
| 721 | PolyTraitRef { |
| 722 | bound_lifetimes: bound_lifetimes, |
| 723 | trait_ref: trait_ref, |
| 724 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 725 | }) |
| 726 | )); |
| 727 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 728 | named!(pub fn_arg -> BareFnArg, do_parse!( |
David Tolnay | b0417d7 | 2016-10-25 21:46:35 -0700 | [diff] [blame] | 729 | name: option!(do_parse!( |
| 730 | name: ident >> |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 731 | not!(punct!("::")) >> |
David Tolnay | b0417d7 | 2016-10-25 21:46:35 -0700 | [diff] [blame] | 732 | punct!(":") >> |
David Tolnay | b0417d7 | 2016-10-25 21:46:35 -0700 | [diff] [blame] | 733 | (name) |
| 734 | )) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 735 | ty: ty >> |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 736 | (BareFnArg { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 737 | name: name.map(|t| (t, tokens::Colon::default())), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 738 | ty: ty, |
| 739 | }) |
| 740 | )); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 741 | |
| 742 | named!(pub unsafety -> Unsafety, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 743 | keyword!("unsafe") => { |_| Unsafety::Unsafe(tokens::Unsafe::default()) } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 744 | | |
| 745 | epsilon!() => { |_| Unsafety::Normal } |
| 746 | )); |
| 747 | |
| 748 | named!(pub abi -> Abi, do_parse!( |
| 749 | keyword!("extern") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 750 | name: option!(string) >> |
| 751 | (Abi { |
| 752 | extern_token: tokens::Extern::default(), |
| 753 | kind: match name { |
| 754 | Some(name) => AbiKind::Named(name), |
| 755 | None => AbiKind::Default, |
| 756 | }, |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 757 | }) |
| 758 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 759 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 760 | |
| 761 | #[cfg(feature = "printing")] |
| 762 | mod printing { |
| 763 | use super::*; |
| 764 | use quote::{Tokens, ToTokens}; |
| 765 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 766 | impl ToTokens for TySlice { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 767 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 768 | self.bracket_token.surround(tokens, |tokens| { |
| 769 | self.ty.to_tokens(tokens); |
| 770 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
| 774 | impl ToTokens for TyArray { |
| 775 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 776 | self.bracket_token.surround(tokens, |tokens| { |
| 777 | self.ty.to_tokens(tokens); |
| 778 | self.semi_token.to_tokens(tokens); |
| 779 | self.amt.to_tokens(tokens); |
| 780 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | impl ToTokens for TyPtr { |
| 785 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 786 | self.star_token.to_tokens(tokens); |
| 787 | self.const_token.to_tokens(tokens); |
| 788 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 789 | self.ty.ty.to_tokens(tokens); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | impl ToTokens for TyRptr { |
| 794 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 795 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 796 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 797 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 798 | self.ty.ty.to_tokens(tokens); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | impl ToTokens for TyBareFn { |
| 803 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 804 | self.ty.to_tokens(tokens) |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | impl ToTokens for TyNever { |
| 809 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 810 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
| 814 | impl ToTokens for TyTup { |
| 815 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 816 | self.paren_token.surround(tokens, |tokens| { |
| 817 | self.tys.to_tokens(tokens); |
| 818 | self.lone_comma.to_tokens(tokens); |
| 819 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
| 823 | impl ToTokens for TyPath { |
| 824 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 825 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | impl<'a> ToTokens for PathTokens<'a> { |
| 830 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 831 | let qself = match *self.0 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 832 | Some(ref qself) => qself, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 833 | None => return self.1.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 834 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 835 | qself.lt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 836 | qself.ty.to_tokens(tokens); |
| 837 | if qself.position > 0 { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 838 | qself.as_token.to_tokens(tokens); |
| 839 | self.1.leading_colon.to_tokens(tokens); |
| 840 | for (i, segment) in self.1.segments |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 841 | .iter() |
| 842 | .take(qself.position) |
| 843 | .enumerate() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 844 | if i == qself.position - 1 { |
| 845 | segment.item().to_tokens(tokens); |
| 846 | qself.gt_token.to_tokens(tokens); |
| 847 | segment.delimiter().to_tokens(tokens); |
| 848 | } else { |
| 849 | segment.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 850 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 851 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 852 | } else { |
| 853 | qself.gt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 854 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 855 | for segment in self.1.segments.iter().skip(qself.position) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 856 | segment.to_tokens(tokens); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | impl ToTokens for TyTraitObject { |
| 862 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 863 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
| 867 | impl ToTokens for TyImplTrait { |
| 868 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 869 | self.impl_token.to_tokens(tokens); |
| 870 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
| 874 | impl ToTokens for TyParen { |
| 875 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 876 | self.paren_token.surround(tokens, |tokens| { |
| 877 | self.ty.to_tokens(tokens); |
| 878 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | impl ToTokens for TyInfer { |
| 883 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 884 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 888 | impl ToTokens for Mutability { |
| 889 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 890 | if let Mutability::Mutable(ref t) = *self { |
| 891 | t.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 896 | impl ToTokens for Path { |
| 897 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 898 | self.leading_colon.to_tokens(tokens); |
| 899 | self.segments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | impl ToTokens for PathSegment { |
| 904 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 905 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 906 | self.parameters.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | impl ToTokens for PathParameters { |
| 911 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 912 | match *self { |
| 913 | PathParameters::AngleBracketed(ref parameters) => { |
| 914 | parameters.to_tokens(tokens); |
| 915 | } |
| 916 | PathParameters::Parenthesized(ref parameters) => { |
| 917 | parameters.to_tokens(tokens); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | impl ToTokens for AngleBracketedParameterData { |
| 924 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 925 | self.lt_token.to_tokens(tokens); |
| 926 | self.lifetimes.to_tokens(tokens); |
| 927 | self.types.to_tokens(tokens); |
| 928 | self.bindings.to_tokens(tokens); |
| 929 | self.gt_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
| 933 | impl ToTokens for TypeBinding { |
| 934 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 935 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 936 | self.eq_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 937 | self.ty.to_tokens(tokens); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | impl ToTokens for ParenthesizedParameterData { |
| 942 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 943 | self.paren_token.surround(tokens, |tokens| { |
| 944 | self.inputs.to_tokens(tokens); |
| 945 | }); |
| 946 | self.output.to_tokens(tokens); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | impl ToTokens for FunctionRetTy { |
| 951 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 952 | match *self { |
| 953 | FunctionRetTy::Default => {} |
| 954 | FunctionRetTy::Ty(ref ty, ref arrow) => { |
| 955 | arrow.to_tokens(tokens); |
| 956 | ty.to_tokens(tokens); |
| 957 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | impl ToTokens for PolyTraitRef { |
| 963 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 964 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 965 | self.trait_ref.to_tokens(tokens); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | impl ToTokens for BareFnTy { |
| 970 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 971 | self.lifetimes.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 972 | self.unsafety.to_tokens(tokens); |
| 973 | self.abi.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 974 | self.fn_token.to_tokens(tokens); |
| 975 | self.paren_token.surround(tokens, |tokens| { |
| 976 | self.inputs.to_tokens(tokens); |
| 977 | self.variadic.to_tokens(tokens); |
| 978 | }); |
| 979 | self.output.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 983 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 984 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 985 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 986 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 987 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 988 | } |
| 989 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 992 | |
| 993 | impl ToTokens for Unsafety { |
| 994 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 995 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 996 | Unsafety::Unsafe(ref t) => t.to_tokens(tokens), |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 997 | Unsafety::Normal => { |
| 998 | // nothing |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | impl ToTokens for Abi { |
| 1005 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1006 | self.extern_token.to_tokens(tokens); |
| 1007 | match self.kind { |
| 1008 | AbiKind::Named(ref named) => named.to_tokens(tokens), |
| 1009 | AbiKind::Default => {} |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1013 | } |