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