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