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; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 328 | use std::str; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 329 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 330 | named!(pub ty -> Ty, alt!( |
David Tolnay | d040d77 | 2016-10-25 21:33:51 -0700 | [diff] [blame] | 331 | ty_paren // must be before ty_tup |
| 332 | | |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 333 | ty_mac // must be before ty_path |
| 334 | | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 335 | ty_path // must be before ty_poly_trait_ref |
| 336 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 337 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 338 | | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 339 | ty_array |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 340 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 341 | ty_ptr |
| 342 | | |
| 343 | ty_rptr |
| 344 | | |
| 345 | ty_bare_fn |
| 346 | | |
| 347 | ty_never |
| 348 | | |
| 349 | ty_tup |
| 350 | | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 351 | ty_poly_trait_ref |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 352 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 353 | ty_impl_trait |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 354 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 355 | |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 356 | named!(ty_mac -> Ty, map!(mac, Ty::Mac)); |
| 357 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 358 | named!(ty_vec -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 359 | punct!("[") >> |
| 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 | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 368 | named!(ty_array -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 369 | punct!("[") >> |
| 370 | elem: ty >> |
| 371 | punct!(";") >> |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 372 | len: array_len >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 373 | punct!("]") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 374 | (TyArray { |
| 375 | ty: Box::new(elem), |
| 376 | amt: len, |
| 377 | bracket_token: tokens::Bracket::default(), |
| 378 | semi_token: tokens::Semi::default(), |
| 379 | }.into()) |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 380 | )); |
| 381 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 382 | #[cfg(not(feature = "full"))] |
David Tolnay | f945fb5 | 2017-02-27 12:53:54 -0800 | [diff] [blame] | 383 | use constant::parsing::const_expr as array_len; |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 384 | |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 385 | #[cfg(feature = "full")] |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 386 | named!(array_len -> ConstExpr, alt!( |
| 387 | terminated!(const_expr, after_array_len) |
| 388 | | |
| 389 | terminated!(expr, after_array_len) => { ConstExpr::Other } |
David Tolnay | fe2cc9a | 2016-10-30 12:47:36 -0700 | [diff] [blame] | 390 | )); |
| 391 | |
David Tolnay | 514f129 | 2017-02-27 12:30:57 -0800 | [diff] [blame] | 392 | #[cfg(feature = "full")] |
| 393 | named!(after_array_len -> &str, peek!(punct!("]"))); |
| 394 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 395 | named!(ty_ptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 396 | punct!("*") >> |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 397 | mutability: alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 398 | keyword!("const") => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 399 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 400 | keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 401 | ) >> |
| 402 | target: ty >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 403 | (TyPtr { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 404 | const_token: match mutability { |
| 405 | Mutability::Mutable(_) => None, |
| 406 | Mutability::Immutable => Some(tokens::Const::default()), |
| 407 | }, |
| 408 | star_token: tokens::Star::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 409 | ty: Box::new(MutTy { |
| 410 | ty: target, |
| 411 | mutability: mutability, |
| 412 | }), |
| 413 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 414 | )); |
| 415 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 416 | named!(ty_rptr -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 417 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 418 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 419 | mutability: mutability >> |
| 420 | target: ty >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 421 | (TyRptr { |
| 422 | lifetime: life, |
| 423 | ty: Box::new(MutTy { |
| 424 | ty: target, |
| 425 | mutability: mutability, |
| 426 | }), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 427 | and_token: tokens::And::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 428 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 429 | )); |
| 430 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 431 | named!(ty_bare_fn -> Ty, do_parse!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 432 | lifetimes: bound_lifetimes >> |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 433 | unsafety: unsafety >> |
| 434 | abi: option!(abi) >> |
David Tolnay | 4f12183 | 2016-10-25 21:33:36 -0700 | [diff] [blame] | 435 | keyword!("fn") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 436 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 437 | inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 438 | fn_arg) >> |
| 439 | variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(), |
| 440 | punct!("..."))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 441 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 442 | output: fn_ret_ty >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 443 | (TyBareFn { |
| 444 | ty: Box::new(BareFnTy { |
| 445 | unsafety: unsafety, |
| 446 | abi: abi, |
| 447 | lifetimes: lifetimes, |
| 448 | inputs: inputs, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 449 | output: output, |
| 450 | variadic: variadic.map(|_| tokens::Dot3::default()), |
| 451 | fn_token: tokens::Fn::default(), |
| 452 | paren_token: tokens::Paren::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 453 | }), |
| 454 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 455 | )); |
| 456 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 457 | named!(ty_never -> Ty, map!(punct!("!"), |_| TyNever { |
| 458 | bang_token: tokens::Bang::default(), |
| 459 | }.into())); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 460 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 461 | named!(ty_tup -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 462 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 463 | elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 464 | ty) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 465 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 466 | (TyTup { |
| 467 | tys: elems, |
| 468 | paren_token: tokens::Paren::default(), |
| 469 | lone_comma: None, // TODO: does this just not parse? |
| 470 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 471 | )); |
| 472 | |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 473 | named!(ty_path -> Ty, do_parse!( |
| 474 | qpath: qpath >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 475 | parenthesized: cond!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 476 | 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] | 477 | option!(parenthesized_parameter_data) |
| 478 | ) >> |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 479 | bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >> |
| 480 | ({ |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 481 | let (qself, mut path) = qpath; |
| 482 | if let Some(Some(parenthesized)) = parenthesized { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 483 | let len = path.segments.len(); |
| 484 | path.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 485 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 486 | if bounds.is_empty() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 487 | TyPath { qself: qself, path: path }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 488 | } else { |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 489 | let path = TyParamBound::Trait( |
| 490 | PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 491 | bound_lifetimes: None, |
David Tolnay | 02c907f | 2017-01-23 00:06:37 -0800 | [diff] [blame] | 492 | trait_ref: path, |
| 493 | }, |
| 494 | TraitBoundModifier::None, |
| 495 | ); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 496 | let mut new_bounds = Delimited::new(); |
| 497 | new_bounds.push_first(path); |
| 498 | for bound in bounds { |
| 499 | new_bounds.push_default(bound); |
| 500 | } |
| 501 | TyTraitObject { bounds: new_bounds }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 502 | } |
| 503 | }) |
| 504 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 505 | |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 506 | named!(parenthesized_parameter_data -> PathParameters, do_parse!( |
| 507 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 508 | inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 509 | ty) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 510 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 511 | output: fn_ret_ty >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 512 | (PathParameters::Parenthesized( |
| 513 | ParenthesizedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 514 | paren_token: tokens::Paren::default(), |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 515 | inputs: inputs, |
| 516 | output: output, |
| 517 | }, |
| 518 | )) |
| 519 | )); |
| 520 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 521 | named!(pub fn_ret_ty -> FunctionRetTy, map!( |
| 522 | option!(preceded!( |
| 523 | punct!("->"), |
| 524 | ty |
| 525 | )), |
| 526 | |ty| { |
| 527 | match ty { |
| 528 | Some(ty) => FunctionRetTy::Ty(ty, tokens::RArrow::default()), |
| 529 | None => FunctionRetTy::Default, |
| 530 | } |
| 531 | } |
| 532 | )); |
| 533 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 534 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
| 535 | map!(path, |p| (None, p)) |
| 536 | | |
| 537 | do_parse!( |
| 538 | punct!("<") >> |
| 539 | this: map!(ty, Box::new) >> |
| 540 | path: option!(preceded!( |
| 541 | keyword!("as"), |
| 542 | path |
| 543 | )) >> |
| 544 | punct!(">") >> |
| 545 | punct!("::") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 546 | rest: separated_nonempty_list!( |
| 547 | map!(punct!("::"), |_| tokens::Colon2::default()), |
| 548 | path_segment |
| 549 | ) >> |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 550 | ({ |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 551 | let as_token = path.as_ref().map(|_| tokens::As::default()); |
| 552 | let (pos, path) = match path { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 553 | Some(mut path) => { |
| 554 | let pos = path.segments.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 555 | if !path.segments.is_empty() && !path.segments.trailing_delim() { |
| 556 | path.segments.push_trailing(tokens::Colon2::default()); |
| 557 | } |
| 558 | for item in rest.into_iter() { |
| 559 | path.segments.push(item); |
| 560 | } |
| 561 | (pos, path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 562 | } |
| 563 | None => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 564 | (0, Path { |
| 565 | leading_colon: None, |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 566 | global: false, |
| 567 | segments: rest, |
| 568 | }) |
| 569 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 570 | }; |
| 571 | (Some(QSelf { |
| 572 | ty: this, |
| 573 | position: pos, |
| 574 | gt_token: tokens::Gt::default(), |
| 575 | lt_token: tokens::Lt::default(), |
| 576 | as_token: as_token, |
| 577 | }), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 578 | }) |
| 579 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 580 | | |
| 581 | map!(keyword!("self"), |_| (None, "self".into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 582 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 583 | |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 584 | named!(ty_poly_trait_ref -> Ty, map!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 585 | separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()), |
| 586 | ty_param_bound), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 587 | |x| TyTraitObject { bounds: x }.into() |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 588 | )); |
| 589 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 590 | named!(ty_impl_trait -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 591 | keyword!("impl") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 592 | elem: separated_nonempty_list!(map!(punct!("+"), |_| tokens::Add::default()), |
| 593 | ty_param_bound) >> |
| 594 | (TyImplTrait { |
| 595 | impl_token: tokens::Impl::default(), |
| 596 | bounds: elem, |
| 597 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 598 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 599 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 600 | named!(ty_paren -> Ty, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 601 | punct!("(") >> |
| 602 | elem: ty >> |
| 603 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 604 | (TyParen { |
| 605 | paren_token: tokens::Paren::default(), |
| 606 | ty: Box::new(elem), |
| 607 | }.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 608 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 609 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 610 | named!(pub mutability -> Mutability, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 611 | keyword!("mut") => { |_| Mutability::Mutable(tokens::Mut::default()) } |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 612 | | |
| 613 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 614 | )); |
| 615 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 616 | named!(pub path -> Path, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 617 | global: option!(punct!("::")) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 618 | segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()), |
| 619 | path_segment) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 620 | (Path { |
| 621 | global: global.is_some(), |
| 622 | segments: segments, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 623 | leading_colon: global.map(|_| tokens::Colon2::default()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 624 | }) |
| 625 | )); |
| 626 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 627 | named!(path_segment -> PathSegment, alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 628 | do_parse!( |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 629 | id: option!(ident) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 630 | punct!("<") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 631 | lifetimes: terminated_list!( |
| 632 | map!(punct!(","), |_| tokens::Comma::default()), |
| 633 | lifetime |
| 634 | ) >> |
| 635 | types: cond!( |
| 636 | lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 637 | terminated_list!( |
| 638 | map!(punct!(","), |_| tokens::Comma::default()), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 639 | terminated!(ty, not!(punct!("="))) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 640 | ) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 641 | ) >> |
| 642 | bindings: cond!( |
| 643 | match types { |
| 644 | Some(ref t) => t.is_empty() || t.trailing_delim(), |
| 645 | None => lifetimes.is_empty() || lifetimes.trailing_delim(), |
| 646 | }, |
| 647 | terminated_list!( |
| 648 | map!(punct!(","), |_| tokens::Comma::default()), |
| 649 | type_binding |
| 650 | ) |
| 651 | ) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 652 | punct!(">") >> |
| 653 | (PathSegment { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 654 | ident: id.unwrap_or_else(|| "".into()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 655 | parameters: PathParameters::AngleBracketed( |
| 656 | AngleBracketedParameterData { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 657 | gt_token: Some(tokens::Gt::default()), |
| 658 | lt_token: Some(tokens::Lt::default()), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 659 | lifetimes: lifetimes, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 660 | types: types.unwrap_or_default(), |
| 661 | bindings: bindings.unwrap_or_default(), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 662 | } |
| 663 | ), |
| 664 | }) |
| 665 | ) |
| 666 | | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 667 | map!(ident, Into::into) |
David Tolnay | 7780722 | 2016-10-24 22:30:15 -0700 | [diff] [blame] | 668 | | |
David Tolnay | e14e3be | 2016-10-24 22:53:07 -0700 | [diff] [blame] | 669 | map!(alt!( |
| 670 | keyword!("super") |
| 671 | | |
| 672 | keyword!("self") |
| 673 | | |
| 674 | keyword!("Self") |
| 675 | ), Into::into) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 676 | )); |
| 677 | |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 678 | named!(pub mod_style_path -> Path, do_parse!( |
| 679 | global: option!(punct!("::")) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 680 | segments: separated_nonempty_list!(map!(punct!("::"), |_| tokens::Colon2::default()), |
| 681 | mod_style_path_segment) >> |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 682 | (Path { |
| 683 | global: global.is_some(), |
| 684 | segments: segments, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 685 | leading_colon: global.map(|_| tokens::Colon2::default()), |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 686 | }) |
| 687 | )); |
| 688 | |
| 689 | named!(mod_style_path_segment -> PathSegment, alt!( |
| 690 | map!(ident, Into::into) |
| 691 | | |
| 692 | map!(alt!( |
| 693 | keyword!("super") |
| 694 | | |
| 695 | keyword!("self") |
| 696 | | |
| 697 | keyword!("Self") |
| 698 | ), Into::into) |
| 699 | )); |
| 700 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 701 | named!(type_binding -> TypeBinding, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 702 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 703 | punct!("=") >> |
| 704 | ty: ty >> |
| 705 | (TypeBinding { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 706 | ident: id, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 707 | eq_token: tokens::Eq::default(), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 708 | ty: ty, |
| 709 | }) |
| 710 | )); |
| 711 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 712 | named!(pub poly_trait_ref -> PolyTraitRef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 713 | bound_lifetimes: bound_lifetimes >> |
| 714 | trait_ref: path >> |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 715 | parenthesized: option!(cond_reduce!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 716 | 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] | 717 | parenthesized_parameter_data |
| 718 | )) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 719 | ({ |
| 720 | let mut trait_ref = trait_ref; |
David Tolnay | 4f0f251 | 2016-10-30 09:28:14 -0700 | [diff] [blame] | 721 | if let Some(parenthesized) = parenthesized { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 722 | let len = trait_ref.segments.len(); |
| 723 | trait_ref.segments.get_mut(len - 1).item_mut().parameters = parenthesized; |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 724 | } |
| 725 | PolyTraitRef { |
| 726 | bound_lifetimes: bound_lifetimes, |
| 727 | trait_ref: trait_ref, |
| 728 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 729 | }) |
| 730 | )); |
| 731 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 732 | named!(pub fn_arg -> BareFnArg, do_parse!( |
David Tolnay | b0417d7 | 2016-10-25 21:46:35 -0700 | [diff] [blame] | 733 | name: option!(do_parse!( |
| 734 | name: ident >> |
| 735 | punct!(":") >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 736 | not!(tag!(":")) >> // not :: |
David Tolnay | b0417d7 | 2016-10-25 21:46:35 -0700 | [diff] [blame] | 737 | (name) |
| 738 | )) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 739 | ty: ty >> |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 740 | (BareFnArg { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 741 | name: name.map(|t| (t, tokens::Colon::default())), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 742 | ty: ty, |
| 743 | }) |
| 744 | )); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 745 | |
| 746 | named!(pub unsafety -> Unsafety, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 747 | keyword!("unsafe") => { |_| Unsafety::Unsafe(tokens::Unsafe::default()) } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 748 | | |
| 749 | epsilon!() => { |_| Unsafety::Normal } |
| 750 | )); |
| 751 | |
| 752 | named!(pub abi -> Abi, do_parse!( |
| 753 | keyword!("extern") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 754 | name: option!(string) >> |
| 755 | (Abi { |
| 756 | extern_token: tokens::Extern::default(), |
| 757 | kind: match name { |
| 758 | Some(name) => AbiKind::Named(name), |
| 759 | None => AbiKind::Default, |
| 760 | }, |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 761 | }) |
| 762 | )); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 763 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 764 | |
| 765 | #[cfg(feature = "printing")] |
| 766 | mod printing { |
| 767 | use super::*; |
| 768 | use quote::{Tokens, ToTokens}; |
| 769 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 770 | impl ToTokens for TySlice { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 771 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 772 | self.bracket_token.surround(tokens, |tokens| { |
| 773 | self.ty.to_tokens(tokens); |
| 774 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | impl ToTokens for TyArray { |
| 779 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 780 | self.bracket_token.surround(tokens, |tokens| { |
| 781 | self.ty.to_tokens(tokens); |
| 782 | self.semi_token.to_tokens(tokens); |
| 783 | self.amt.to_tokens(tokens); |
| 784 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | impl ToTokens for TyPtr { |
| 789 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 790 | self.star_token.to_tokens(tokens); |
| 791 | self.const_token.to_tokens(tokens); |
| 792 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 793 | self.ty.ty.to_tokens(tokens); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | impl ToTokens for TyRptr { |
| 798 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 799 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 800 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 801 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 802 | self.ty.ty.to_tokens(tokens); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | impl ToTokens for TyBareFn { |
| 807 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 808 | self.ty.to_tokens(tokens) |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | impl ToTokens for TyNever { |
| 813 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 814 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | impl ToTokens for TyTup { |
| 819 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 820 | self.paren_token.surround(tokens, |tokens| { |
| 821 | self.tys.to_tokens(tokens); |
| 822 | self.lone_comma.to_tokens(tokens); |
| 823 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
| 827 | impl ToTokens for TyPath { |
| 828 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 829 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | impl<'a> ToTokens for PathTokens<'a> { |
| 834 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 835 | let qself = match *self.0 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 836 | Some(ref qself) => qself, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 837 | None => return self.1.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 838 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 839 | qself.lt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 840 | qself.ty.to_tokens(tokens); |
| 841 | if qself.position > 0 { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 842 | qself.as_token.to_tokens(tokens); |
| 843 | self.1.leading_colon.to_tokens(tokens); |
| 844 | for (i, segment) in self.1.segments |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 845 | .iter() |
| 846 | .take(qself.position) |
| 847 | .enumerate() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 848 | if i == qself.position - 1 { |
| 849 | segment.item().to_tokens(tokens); |
| 850 | qself.gt_token.to_tokens(tokens); |
| 851 | segment.delimiter().to_tokens(tokens); |
| 852 | } else { |
| 853 | segment.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 854 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 855 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 856 | } else { |
| 857 | qself.gt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 858 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 859 | for segment in self.1.segments.iter().skip(qself.position) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 860 | segment.to_tokens(tokens); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | impl ToTokens for TyTraitObject { |
| 866 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 867 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | impl ToTokens for TyImplTrait { |
| 872 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 873 | self.impl_token.to_tokens(tokens); |
| 874 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | impl ToTokens for TyParen { |
| 879 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 880 | self.paren_token.surround(tokens, |tokens| { |
| 881 | self.ty.to_tokens(tokens); |
| 882 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | impl ToTokens for TyInfer { |
| 887 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 888 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 892 | impl ToTokens for Mutability { |
| 893 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 894 | if let Mutability::Mutable(ref t) = *self { |
| 895 | t.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 900 | impl ToTokens for Path { |
| 901 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 902 | self.leading_colon.to_tokens(tokens); |
| 903 | self.segments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
| 907 | impl ToTokens for PathSegment { |
| 908 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 909 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 910 | self.parameters.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | impl ToTokens for PathParameters { |
| 915 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 916 | match *self { |
| 917 | PathParameters::AngleBracketed(ref parameters) => { |
| 918 | parameters.to_tokens(tokens); |
| 919 | } |
| 920 | PathParameters::Parenthesized(ref parameters) => { |
| 921 | parameters.to_tokens(tokens); |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | impl ToTokens for AngleBracketedParameterData { |
| 928 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 929 | self.lt_token.to_tokens(tokens); |
| 930 | self.lifetimes.to_tokens(tokens); |
| 931 | self.types.to_tokens(tokens); |
| 932 | self.bindings.to_tokens(tokens); |
| 933 | self.gt_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | impl ToTokens for TypeBinding { |
| 938 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 939 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 940 | self.eq_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 941 | self.ty.to_tokens(tokens); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | impl ToTokens for ParenthesizedParameterData { |
| 946 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 947 | self.paren_token.surround(tokens, |tokens| { |
| 948 | self.inputs.to_tokens(tokens); |
| 949 | }); |
| 950 | self.output.to_tokens(tokens); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | impl ToTokens for FunctionRetTy { |
| 955 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 956 | match *self { |
| 957 | FunctionRetTy::Default => {} |
| 958 | FunctionRetTy::Ty(ref ty, ref arrow) => { |
| 959 | arrow.to_tokens(tokens); |
| 960 | ty.to_tokens(tokens); |
| 961 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | impl ToTokens for PolyTraitRef { |
| 967 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 968 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 969 | self.trait_ref.to_tokens(tokens); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | impl ToTokens for BareFnTy { |
| 974 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 975 | self.lifetimes.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 976 | self.unsafety.to_tokens(tokens); |
| 977 | self.abi.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 978 | self.fn_token.to_tokens(tokens); |
| 979 | self.paren_token.surround(tokens, |tokens| { |
| 980 | self.inputs.to_tokens(tokens); |
| 981 | self.variadic.to_tokens(tokens); |
| 982 | }); |
| 983 | self.output.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 987 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 988 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 989 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 990 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 991 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 992 | } |
| 993 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 996 | |
| 997 | impl ToTokens for Unsafety { |
| 998 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 999 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 1000 | Unsafety::Unsafe(ref t) => t.to_tokens(tokens), |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1001 | Unsafety::Normal => { |
| 1002 | // nothing |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | impl ToTokens for Abi { |
| 1009 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 1010 | self.extern_token.to_tokens(tokens); |
| 1011 | match self.kind { |
| 1012 | AbiKind::Named(ref named) => named.to_tokens(tokens), |
| 1013 | AbiKind::Default => {} |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1017 | } |