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 |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 6 | pub enum Type { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 7 | /// A variable-length array (`[T]`) |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 8 | pub Slice(TypeSlice { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 9 | pub bracket_token: token::Bracket, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 10 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 11 | }), |
| 12 | /// A fixed length array (`[T; n]`) |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 13 | pub Array(TypeArray { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 14 | pub bracket_token: token::Bracket, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 15 | pub ty: Box<Type>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 16 | pub semi_token: Token![;], |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame] | 17 | pub amt: Expr, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 18 | }), |
| 19 | /// A raw pointer (`*const T` or `*mut T`) |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 20 | pub Ptr(TypePtr { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 21 | pub star_token: Token![*], |
| 22 | pub const_token: Option<Token![const]>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 23 | pub ty: Box<MutType>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 24 | }), |
| 25 | /// A reference (`&'a T` or `&'a mut T`) |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 26 | pub Reference(TypeReference { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 27 | pub and_token: Token![&], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 28 | pub lifetime: Option<Lifetime>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 29 | pub ty: Box<MutType>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 30 | }), |
| 31 | /// A bare function (e.g. `fn(usize) -> bool`) |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 32 | pub BareFn(TypeBareFn { |
| 33 | pub ty: Box<BareFnType>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | }), |
| 35 | /// The never type (`!`) |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 36 | pub Never(TypeNever { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 37 | pub bang_token: Token![!], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 38 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 39 | /// A tuple (`(A, B, C, D, ...)`) |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 40 | pub Tuple(TypeTuple { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 41 | pub paren_token: token::Paren, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 42 | pub tys: Delimited<Type, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 43 | }), |
| 44 | /// A path (`module::module::...::Type`), optionally |
| 45 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 46 | /// |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 47 | /// Type arguments are stored in the Path itself |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 48 | pub Path(TypePath { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 49 | pub qself: Option<QSelf>, |
| 50 | pub path: Path, |
| 51 | }), |
| 52 | /// A trait object type `Bound1 + Bound2 + Bound3` |
| 53 | /// where `Bound` is a trait or a lifetime. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 54 | pub TraitObject(TypeTraitObject { |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 55 | pub dyn_token: Option<Token![dyn]>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 56 | pub bounds: Delimited<TypeParamBound, Token![+]>, |
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. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 60 | pub ImplTrait(TypeImplTrait { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 61 | pub impl_token: Token![impl], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 62 | pub bounds: Delimited<TypeParamBound, Token![+]>, |
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 |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 65 | pub Paren(TypeParen { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 66 | pub paren_token: token::Paren, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 67 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 68 | }), |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 69 | /// No-op: kept solely so that we can pretty-print faithfully |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 70 | pub Group(TypeGroup { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 71 | pub group_token: token::Group, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 72 | pub ty: Box<Type>, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 73 | }), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 74 | /// TypeKind::Infer means the type should be inferred instead of it having been |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 75 | /// specified. This can appear anywhere in a type. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 76 | pub Infer(TypeInfer { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 77 | pub underscore_token: Token![_], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 78 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 79 | /// A macro in the type position. |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 80 | pub Macro(Macro), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | ast_struct! { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 85 | pub struct MutType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 86 | pub mutability: Mutability, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 87 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 92 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 93 | pub enum Mutability { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 94 | Mutable(Token![mut]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 95 | Immutable, |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | ast_struct! { |
| 100 | /// A "Path" is essentially Rust's notion of a name. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 101 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 102 | /// It's represented as a sequence of identifiers, |
| 103 | /// along with a bunch of supporting information. |
| 104 | /// |
| 105 | /// E.g. `std::cmp::PartialEq` |
| 106 | pub struct Path { |
| 107 | /// A `::foo` path, is relative to the crate root rather than current |
| 108 | /// module (like paths in an import). |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 109 | pub leading_colon: Option<Token![::]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 110 | /// The segments in the path: the things separated by `::`. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 111 | pub segments: Delimited<PathSegment, Token![::]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 112 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 113 | } |
| 114 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 115 | impl Path { |
| 116 | pub fn global(&self) -> bool { |
| 117 | self.leading_colon.is_some() |
| 118 | } |
| 119 | } |
| 120 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 121 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 122 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 123 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
| 124 | pub struct PathTokens<'a>(pub &'a Option<QSelf>, pub &'a Path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 125 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 126 | impl<T> From<T> for Path |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 127 | where |
| 128 | T: Into<PathSegment>, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 129 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 130 | fn from(segment: T) -> Self { |
| 131 | Path { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 132 | leading_colon: None, |
| 133 | segments: vec![(segment.into(), None)].into(), |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 138 | ast_struct! { |
| 139 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
| 140 | /// |
| 141 | /// E.g. `std`, `String` or `Box<T>` |
| 142 | pub struct PathSegment { |
| 143 | /// The identifier portion of this path segment. |
| 144 | pub ident: Ident, |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 145 | /// Type/lifetime arguments attached to this path. They come in |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 146 | /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that |
| 147 | /// this is more than just simple syntactic sugar; the use of |
| 148 | /// parens affects the region binding rules, so we preserve the |
| 149 | /// distinction. |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 150 | pub arguments: PathArguments, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 151 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 154 | impl<T> From<T> for PathSegment |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 155 | where |
| 156 | T: Into<Ident>, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 157 | { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 158 | fn from(ident: T) -> Self { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 159 | PathSegment { |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 160 | ident: ident.into(), |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 161 | arguments: PathArguments::None, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 166 | ast_enum! { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 167 | /// Arguments of a path segment. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 168 | /// |
| 169 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 170 | pub enum PathArguments { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 171 | None, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 172 | /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>` |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 173 | AngleBracketed(AngleBracketedGenericArguments), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 174 | /// The `(A, B)` and `C` in `Foo(A, B) -> C` |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 175 | Parenthesized(ParenthesizedGenericArguments), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 176 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 179 | impl Default for PathArguments { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 180 | fn default() -> Self { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 181 | PathArguments::None |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 182 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 183 | } |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 184 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 185 | impl PathArguments { |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 186 | pub fn is_empty(&self) -> bool { |
| 187 | match *self { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 188 | PathArguments::None => true, |
| 189 | PathArguments::AngleBracketed(ref bracketed) => bracketed.args.is_empty(), |
| 190 | PathArguments::Parenthesized(_) => false, |
David Tolnay | 5332d4b | 2016-10-30 14:25:22 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 195 | ast_enum! { |
| 196 | /// A individual generic argument, like `'a`, `T`, or `Item=T`. |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 197 | pub enum GenericArgument { |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 198 | /// The lifetime parameters for this path segment. |
| 199 | Lifetime(Lifetime), |
| 200 | /// The type parameters for this path segment, if present. |
| 201 | Type(Type), |
| 202 | /// Bindings (equality constraints) on associated types, if present. |
| 203 | /// |
| 204 | /// E.g., `Foo<A=Bar>`. |
| 205 | TypeBinding(TypeBinding), |
Nika Layzell | c680e61 | 2017-12-04 19:07:20 -0500 | [diff] [blame] | 206 | /// Const expression. Must be inside of a block. |
| 207 | /// |
| 208 | /// NOTE: Identity expressions are represented as Type arguments, as |
| 209 | /// they are indistinguishable syntactically. |
Nika Layzell | ce37f33 | 2017-12-05 12:01:22 -0500 | [diff] [blame] | 210 | Const(Expr), |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 214 | ast_struct! { |
| 215 | /// A path like `Foo<'a, T>` |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 216 | pub struct AngleBracketedGenericArguments { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 217 | pub turbofish: Option<Token![::]>, |
| 218 | pub lt_token: Token![<], |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 219 | pub args: Delimited<GenericArgument, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 220 | pub gt_token: Token![>], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | ast_struct! { |
| 225 | /// Bind a type to an associated type: `A=Foo`. |
| 226 | pub struct TypeBinding { |
| 227 | pub ident: Ident, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 228 | pub eq_token: Token![=], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 229 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 233 | ast_struct! { |
| 234 | /// A path like `Foo(A,B) -> C` |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 235 | pub struct ParenthesizedGenericArguments { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 236 | pub paren_token: token::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 237 | /// `(A, B)` |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 238 | pub inputs: Delimited<Type, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 239 | /// `C` |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 240 | pub output: ReturnType, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | ast_struct! { |
| 245 | pub struct PolyTraitRef { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 246 | /// The `for<'a>` in `for<'a> Foo<&'a T>` |
| 247 | pub bound_lifetimes: Option<BoundLifetimes>, |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 248 | /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>` |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 249 | pub trait_ref: Path, |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ast_struct! { |
| 254 | /// The explicit Self type in a "qualified path". The actual |
| 255 | /// path, including the trait and the associated item, is stored |
| 256 | /// separately. `position` represents the index of the associated |
| 257 | /// item qualified with this Self type. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 258 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 259 | /// ```text |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 260 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 261 | /// ^~~~~~ ~~~~~~~~~~~~~~^ |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 262 | /// ty position = 3 |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 263 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 264 | /// <Vec<T>>::AssociatedItem |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 265 | /// ^~~~~~ ^ |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 266 | /// ty position = 0 |
| 267 | /// ``` |
| 268 | pub struct QSelf { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 269 | pub lt_token: Token![<], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 270 | pub ty: Box<Type>, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 271 | pub position: usize, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 272 | pub as_token: Option<Token![as]>, |
| 273 | pub gt_token: Token![>], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | ast_struct! { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 278 | pub struct BareFnType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 279 | pub unsafety: Unsafety, |
| 280 | pub abi: Option<Abi>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 281 | pub fn_token: Token![fn], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 282 | pub lifetimes: Option<BoundLifetimes>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 283 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 284 | pub inputs: Delimited<BareFnArg, Token![,]>, |
| 285 | pub variadic: Option<Token![...]>, |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 286 | pub output: ReturnType, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 291 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 292 | pub enum Unsafety { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 293 | Unsafe(Token![unsafe]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 294 | Normal, |
| 295 | } |
| 296 | } |
| 297 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 298 | ast_struct! { |
| 299 | pub struct Abi { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 300 | pub extern_token: Token![extern], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 301 | pub kind: AbiKind, |
| 302 | } |
| 303 | } |
| 304 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 305 | ast_enum! { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 306 | pub enum AbiKind { |
| 307 | Named(Lit), |
| 308 | Default, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | ast_struct! { |
| 313 | /// An argument in a function type. |
| 314 | /// |
| 315 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 316 | pub struct BareFnArg { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 317 | pub name: Option<(BareFnArgName, Token![:])>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 318 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 322 | ast_enum! { |
| 323 | /// Names of arguments in the `BareFnArg` structure |
| 324 | pub enum BareFnArgName { |
| 325 | /// Argument with the provided name |
| 326 | Named(Ident), |
| 327 | /// Argument matched with `_` |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 328 | Wild(Token![_]), |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 331 | |
| 332 | ast_enum! { |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 333 | pub enum ReturnType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 334 | /// Return type is not specified. |
| 335 | /// |
| 336 | /// Functions default to `()` and |
| 337 | /// closures default to inference. Span points to where return |
| 338 | /// type would be inserted. |
| 339 | Default, |
| 340 | /// Everything else |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 341 | Type(Token![->], Box<Type>), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 342 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 343 | } |
| 344 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 345 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 346 | pub mod parsing { |
| 347 | use super::*; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 348 | use synom::Synom; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 349 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 350 | impl Synom for Type { |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 351 | named!(parse -> Self, call!(ambig_ty, true)); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 352 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 353 | fn description() -> Option<&'static str> { |
| 354 | Some("type") |
| 355 | } |
| 356 | } |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 357 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 358 | impl Type { |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 359 | /// In some positions, types may not contain the `+` character, to |
| 360 | /// disambiguate them. For example in the expression `1 as T`, T may not |
| 361 | /// contain a `+` character. |
| 362 | /// |
| 363 | /// This parser does not allow a `+`, while the default parser does. |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 364 | named!(pub without_plus -> Self, call!(ambig_ty, false)); |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 365 | } |
| 366 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 367 | named!(ambig_ty(allow_plus: bool) -> Type, alt!( |
| 368 | syn!(TypeGroup) => { Type::Group } |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 369 | | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 370 | // must be before TypeTuple |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 371 | syn!(TypeParen) => { Type::Paren } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 372 | | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 373 | // must be before TypePath |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 374 | syn!(Macro) => { Type::Macro } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 375 | | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 376 | // must be before TypeTraitObject |
| 377 | call!(TypePath::parse, allow_plus) => { Type::Path } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 378 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 379 | syn!(TypeSlice) => { Type::Slice } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 380 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 381 | syn!(TypeArray) => { Type::Array } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 382 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 383 | syn!(TypePtr) => { Type::Ptr } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 384 | | |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 385 | syn!(TypeReference) => { Type::Reference } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 386 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 387 | syn!(TypeBareFn) => { Type::BareFn } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 388 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 389 | syn!(TypeNever) => { Type::Never } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 390 | | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 391 | syn!(TypeTuple) => { Type::Tuple } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 392 | | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 393 | // Don't try parsing more than one trait bound if we aren't allowing it |
| 394 | call!(TypeTraitObject::parse, allow_plus) => { Type::TraitObject } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 395 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 396 | syn!(TypeImplTrait) => { Type::ImplTrait } |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 397 | | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 398 | syn!(TypeInfer) => { Type::Infer } |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 399 | )); |
| 400 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 401 | impl Synom for TypeSlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 402 | named!(parse -> Self, map!( |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 403 | brackets!(syn!(Type)), |
| 404 | |(ty, b)| TypeSlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 405 | ty: Box::new(ty), |
| 406 | bracket_token: b, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 407 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 408 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 409 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 410 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 411 | impl Synom for TypeArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 412 | named!(parse -> Self, map!( |
| 413 | brackets!(do_parse!( |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 414 | elem: syn!(Type) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 415 | semi: punct!(;) >> |
Michael Layzell | d7ee910 | 2017-06-07 10:02:19 -0400 | [diff] [blame] | 416 | len: syn!(Expr) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 417 | (elem, semi, len) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 418 | )), |
| 419 | |((elem, semi, len), brackets)| { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 420 | TypeArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 421 | ty: Box::new(elem), |
| 422 | amt: len, |
| 423 | bracket_token: brackets, |
| 424 | semi_token: semi, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 427 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 428 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 429 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 430 | impl Synom for TypePtr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 431 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 432 | star: punct!(*) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 433 | mutability: alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 434 | keyword!(const) => { |c| (Mutability::Immutable, Some(c)) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 435 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 436 | keyword!(mut) => { |m| (Mutability::Mutable(m), None) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 437 | ) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 438 | target: call!(Type::without_plus) >> |
| 439 | (TypePtr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 440 | const_token: mutability.1, |
| 441 | star_token: star, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 442 | ty: Box::new(MutType { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 443 | ty: target, |
| 444 | mutability: mutability.0, |
| 445 | }), |
| 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 | |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 450 | impl Synom for TypeReference { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 451 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 452 | amp: punct!(&) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 453 | life: option!(syn!(Lifetime)) >> |
| 454 | mutability: syn!(Mutability) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 455 | // & binds tighter than +, so we don't allow + here. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 456 | target: call!(Type::without_plus) >> |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 457 | (TypeReference { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 458 | lifetime: life, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 459 | ty: Box::new(MutType { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 460 | ty: target, |
| 461 | mutability: mutability, |
| 462 | }), |
| 463 | and_token: amp, |
| 464 | }) |
| 465 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 466 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 467 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 468 | impl Synom for TypeBareFn { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 469 | named!(parse -> Self, do_parse!( |
| 470 | lifetimes: option!(syn!(BoundLifetimes)) >> |
| 471 | unsafety: syn!(Unsafety) >> |
| 472 | abi: option!(syn!(Abi)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 473 | fn_: keyword!(fn) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 474 | parens: parens!(do_parse!( |
| 475 | inputs: call!(Delimited::parse_terminated) >> |
| 476 | variadic: option!(cond_reduce!(inputs.is_empty() || inputs.trailing_delim(), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 477 | punct!(...))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 478 | (inputs, variadic) |
| 479 | )) >> |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 480 | output: syn!(ReturnType) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 481 | (TypeBareFn { |
| 482 | ty: Box::new(BareFnType { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 483 | unsafety: unsafety, |
| 484 | abi: abi, |
| 485 | lifetimes: lifetimes, |
| 486 | output: output, |
| 487 | variadic: (parens.0).1, |
| 488 | fn_token: fn_, |
| 489 | paren_token: parens.1, |
| 490 | inputs: (parens.0).0, |
| 491 | }), |
| 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 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 496 | impl Synom for TypeNever { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 497 | named!(parse -> Self, map!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 498 | punct!(!), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 499 | |b| TypeNever { bang_token: b } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 500 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 501 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 502 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 503 | impl Synom for TypeInfer { |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 504 | named!(parse -> Self, map!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 505 | punct!(_), |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 506 | |u| TypeInfer { underscore_token: u } |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 507 | )); |
| 508 | } |
| 509 | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 510 | impl Synom for TypeTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 511 | named!(parse -> Self, do_parse!( |
| 512 | data: parens!(call!(Delimited::parse_terminated)) >> |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 513 | (TypeTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 514 | tys: data.0, |
| 515 | paren_token: data.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 516 | }) |
| 517 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 518 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 519 | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 520 | impl TypePath { |
| 521 | named!(parse(allow_plus: bool) -> Self, do_parse!( |
| 522 | qpath: qpath >> |
| 523 | parenthesized: cond!( |
| 524 | qpath.1.segments.last().unwrap().item().arguments.is_empty(), |
| 525 | option!(syn!(ParenthesizedGenericArguments)) |
| 526 | ) >> |
| 527 | cond!(allow_plus, not!(peek!(punct!(+)))) >> |
| 528 | ({ |
| 529 | let (qself, mut path) = qpath; |
| 530 | if let Some(Some(parenthesized)) = parenthesized { |
| 531 | let parenthesized = PathArguments::Parenthesized(parenthesized); |
| 532 | path.segments.last_mut().unwrap().item_mut().arguments = parenthesized; |
| 533 | } |
| 534 | TypePath { qself: qself, path: path } |
| 535 | }) |
| 536 | )); |
| 537 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 538 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 539 | named!(pub qpath -> (Option<QSelf>, Path), alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 540 | map!(syn!(Path), |p| (None, p)) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 541 | | |
| 542 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 543 | lt: punct!(<) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 544 | this: syn!(Type) >> |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 545 | path: option!(tuple!(keyword!(as), syn!(Path))) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 546 | gt: punct!(>) >> |
| 547 | colon2: punct!(::) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 548 | rest: call!(Delimited::parse_separated_nonempty) >> |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 549 | ({ |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 550 | let (pos, as_, path) = match path { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 551 | Some((as_, mut path)) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 552 | let pos = path.segments.len(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 553 | if !path.segments.is_empty() && !path.segments.trailing_delim() { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 554 | path.segments.push_trailing(colon2); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 555 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 556 | for item in rest { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 557 | path.segments.push(item); |
| 558 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 559 | (pos, Some(as_), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 560 | } |
| 561 | None => { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 562 | (0, None, Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 563 | leading_colon: Some(colon2), |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 564 | segments: rest, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 565 | }) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 566 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 567 | }; |
| 568 | (Some(QSelf { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 569 | lt_token: lt, |
| 570 | ty: Box::new(this), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 571 | position: pos, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 572 | as_token: as_, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 573 | gt_token: gt, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 574 | }), path) |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 575 | }) |
| 576 | ) |
David Tolnay | 6cd2a23 | 2016-10-24 22:41:08 -0700 | [diff] [blame] | 577 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 578 | map!(keyword!(self), |s| (None, s.into())) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 579 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 580 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 581 | impl Synom for ParenthesizedGenericArguments { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 582 | named!(parse -> Self, do_parse!( |
| 583 | data: parens!(call!(Delimited::parse_terminated)) >> |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 584 | output: syn!(ReturnType) >> |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 585 | (ParenthesizedGenericArguments { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 586 | paren_token: data.1, |
| 587 | inputs: data.0, |
| 588 | output: output, |
| 589 | }) |
| 590 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 591 | } |
| 592 | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 593 | impl Synom for ReturnType { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 594 | named!(parse -> Self, alt!( |
| 595 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 596 | arrow: punct!(->) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 597 | ty: syn!(Type) >> |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 598 | (ReturnType::Type(arrow, Box::new(ty))) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 599 | ) |
| 600 | | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 601 | epsilon!() => { |_| ReturnType::Default } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 602 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 603 | } |
| 604 | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 605 | impl TypeTraitObject { |
| 606 | // Only allow multiple trait references if allow_plus is true. |
| 607 | named!(parse(allow_plus: bool) -> Self, do_parse!( |
| 608 | dyn_token: option!(keyword!(dyn)) >> |
| 609 | bounds: alt!( |
| 610 | cond_reduce!(allow_plus, call!(Delimited::parse_terminated_nonempty)) |
| 611 | | |
| 612 | syn!(TypeParamBound) => { |x| vec![x].into() } |
| 613 | ) >> |
| 614 | (TypeTraitObject { |
| 615 | dyn_token: dyn_token, |
| 616 | bounds: bounds, |
| 617 | }) |
| 618 | )); |
| 619 | } |
David Tolnay | 6414da7 | 2016-10-08 00:55:17 -0700 | [diff] [blame] | 620 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 621 | impl Synom for TypeImplTrait { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 622 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 623 | impl_: keyword!(impl) >> |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 624 | // NOTE: rust-lang/rust#34511 includes discussion about whether or |
| 625 | // not + should be allowed in ImplTrait directly without (). |
Nika Layzell | b49a9e5 | 2017-12-05 13:31:52 -0500 | [diff] [blame] | 626 | elem: call!(Delimited::parse_terminated_nonempty) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 627 | (TypeImplTrait { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 628 | impl_token: impl_, |
| 629 | bounds: elem, |
| 630 | }) |
| 631 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 632 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 633 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 634 | impl Synom for TypeGroup { |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 635 | named!(parse -> Self, do_parse!( |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 636 | data: grouped!(syn!(Type)) >> |
| 637 | (TypeGroup { |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 638 | group_token: data.1, |
| 639 | ty: Box::new(data.0), |
| 640 | }) |
| 641 | )); |
| 642 | } |
| 643 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 644 | impl Synom for TypeParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 645 | named!(parse -> Self, do_parse!( |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 646 | data: parens!(syn!(Type)) >> |
| 647 | (TypeParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 648 | paren_token: data.1, |
| 649 | ty: Box::new(data.0), |
| 650 | }) |
| 651 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 652 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 653 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 654 | impl Synom for Mutability { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 655 | named!(parse -> Self, alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 656 | keyword!(mut) => { Mutability::Mutable } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 657 | | |
| 658 | epsilon!() => { |_| Mutability::Immutable } |
| 659 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 660 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 661 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 662 | impl Synom for Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 663 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 664 | colon: option!(punct!(::)) >> |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 665 | segments: call!(Delimited::<PathSegment, Token![::]>::parse_separated_nonempty) >> |
| 666 | cond_reduce!(segments.first().map_or(true, |seg| seg.item().ident != "dyn"), epsilon!()) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 667 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 668 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 669 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 670 | }) |
| 671 | )); |
Alex Crichton | 36e91bf | 2017-07-06 14:59:56 -0700 | [diff] [blame] | 672 | |
| 673 | fn description() -> Option<&'static str> { |
| 674 | Some("path") |
| 675 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 676 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 677 | |
Nika Layzell | c680e61 | 2017-12-04 19:07:20 -0500 | [diff] [blame] | 678 | #[cfg(not(feature = "full"))] |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 679 | impl Synom for GenericArgument { |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 680 | named!(parse -> Self, alt!( |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 681 | call!(ty_no_eq_after) => { GenericArgument::Type } |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 682 | | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 683 | syn!(Lifetime) => { GenericArgument::Lifetime } |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 684 | | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 685 | syn!(TypeBinding) => { GenericArgument::TypeBinding } |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 686 | )); |
| 687 | } |
| 688 | |
Nika Layzell | c680e61 | 2017-12-04 19:07:20 -0500 | [diff] [blame] | 689 | #[cfg(feature = "full")] |
| 690 | impl Synom for GenericArgument { |
| 691 | named!(parse -> Self, alt!( |
| 692 | call!(ty_no_eq_after) => { GenericArgument::Type } |
| 693 | | |
| 694 | syn!(Lifetime) => { GenericArgument::Lifetime } |
| 695 | | |
| 696 | syn!(TypeBinding) => { GenericArgument::TypeBinding } |
| 697 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame^] | 698 | syn!(ExprLit) => { |l| GenericArgument::Const(Expr::Lit(l).into()) } |
Nika Layzell | ce37f33 | 2017-12-05 12:01:22 -0500 | [diff] [blame] | 699 | | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame^] | 700 | syn!(ExprBlock) => { |b| GenericArgument::Const(Expr::Block(b).into()) } |
Nika Layzell | c680e61 | 2017-12-04 19:07:20 -0500 | [diff] [blame] | 701 | )); |
| 702 | } |
| 703 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 704 | impl Synom for AngleBracketedGenericArguments { |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 705 | named!(parse -> Self, do_parse!( |
| 706 | turbofish: option!(punct!(::)) >> |
| 707 | lt: punct!(<) >> |
| 708 | args: call!(Delimited::parse_terminated) >> |
| 709 | gt: punct!(>) >> |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 710 | (AngleBracketedGenericArguments { |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 711 | turbofish: turbofish, |
| 712 | lt_token: lt, |
| 713 | args: args, |
| 714 | gt_token: gt, |
| 715 | }) |
| 716 | )); |
| 717 | } |
| 718 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 719 | impl Synom for PathSegment { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 720 | named!(parse -> Self, alt!( |
| 721 | do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 722 | ident: syn!(Ident) >> |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 723 | arguments: syn!(AngleBracketedGenericArguments) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 724 | (PathSegment { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 725 | ident: ident, |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 726 | arguments: PathArguments::AngleBracketed(arguments), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 727 | }) |
| 728 | ) |
| 729 | | |
| 730 | mod_style_path_segment |
| 731 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 732 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 733 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 734 | named!(ty_no_eq_after -> Type, terminated!(syn!(Type), not!(punct!(=)))); |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 735 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 736 | impl Path { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 737 | named!(pub parse_mod_style -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 738 | colon: option!(punct!(::)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 739 | segments: call!(Delimited::parse_separated_nonempty_with, |
| 740 | mod_style_path_segment) >> |
| 741 | (Path { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 742 | leading_colon: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 743 | segments: segments, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 744 | }) |
| 745 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 746 | } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 747 | |
| 748 | named!(mod_style_path_segment -> PathSegment, alt!( |
David Tolnay | 5f332a9 | 2017-12-26 00:42:45 -0500 | [diff] [blame] | 749 | syn!(Ident) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 750 | | |
David Tolnay | 5f332a9 | 2017-12-26 00:42:45 -0500 | [diff] [blame] | 751 | keyword!(super) => { Into::into } |
| 752 | | |
| 753 | keyword!(self) => { Into::into } |
| 754 | | |
| 755 | keyword!(Self) => { Into::into } |
| 756 | | |
| 757 | keyword!(crate) => { Into::into } |
Arnavion | f2dada1 | 2017-04-20 23:55:20 -0700 | [diff] [blame] | 758 | )); |
| 759 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 760 | impl Synom for TypeBinding { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 761 | named!(parse -> Self, do_parse!( |
| 762 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 763 | eq: punct!(=) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 764 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 765 | (TypeBinding { |
| 766 | ident: id, |
| 767 | eq_token: eq, |
| 768 | ty: ty, |
| 769 | }) |
| 770 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | impl Synom for PolyTraitRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 774 | named!(parse -> Self, do_parse!( |
| 775 | bound_lifetimes: option!(syn!(BoundLifetimes)) >> |
| 776 | trait_ref: syn!(Path) >> |
| 777 | parenthesized: option!(cond_reduce!( |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 778 | trait_ref.segments.get(trait_ref.segments.len() - 1).item().arguments.is_empty(), |
| 779 | syn!(ParenthesizedGenericArguments) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 780 | )) >> |
| 781 | ({ |
| 782 | let mut trait_ref = trait_ref; |
| 783 | if let Some(parenthesized) = parenthesized { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 784 | let parenthesized = PathArguments::Parenthesized(parenthesized); |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 785 | let len = trait_ref.segments.len(); |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 786 | trait_ref.segments.get_mut(len - 1).item_mut().arguments = parenthesized; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 787 | } |
| 788 | PolyTraitRef { |
| 789 | bound_lifetimes: bound_lifetimes, |
| 790 | trait_ref: trait_ref, |
| 791 | } |
| 792 | }) |
| 793 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 794 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 795 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 796 | impl Synom for BareFnArg { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 797 | named!(parse -> Self, do_parse!( |
| 798 | name: option!(do_parse!( |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 799 | name: syn!(BareFnArgName) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 800 | not!(punct!(::)) >> |
| 801 | colon: punct!(:) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 802 | (name, colon) |
| 803 | )) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 804 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 805 | (BareFnArg { |
| 806 | name: name, |
| 807 | ty: ty, |
| 808 | }) |
| 809 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 810 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 811 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 812 | impl Synom for BareFnArgName { |
| 813 | named!(parse -> Self, alt!( |
| 814 | map!(syn!(Ident), BareFnArgName::Named) |
| 815 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 816 | map!(punct!(_), BareFnArgName::Wild) |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 817 | )); |
| 818 | } |
| 819 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 820 | impl Synom for Unsafety { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 821 | named!(parse -> Self, alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 822 | keyword!(unsafe) => { Unsafety::Unsafe } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 823 | | |
| 824 | epsilon!() => { |_| Unsafety::Normal } |
| 825 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 826 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 827 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 828 | impl Synom for Abi { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 829 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 830 | extern_: keyword!(extern) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 831 | // TODO: this parses all literals, not just strings |
| 832 | name: option!(syn!(Lit)) >> |
| 833 | (Abi { |
| 834 | extern_token: extern_, |
| 835 | kind: match name { |
| 836 | Some(name) => AbiKind::Named(name), |
| 837 | None => AbiKind::Default, |
| 838 | }, |
| 839 | }) |
| 840 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 841 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 842 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 843 | |
| 844 | #[cfg(feature = "printing")] |
| 845 | mod printing { |
| 846 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 847 | use quote::{ToTokens, Tokens}; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 848 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 849 | impl ToTokens for TypeSlice { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 850 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 851 | self.bracket_token.surround(tokens, |tokens| { |
| 852 | self.ty.to_tokens(tokens); |
| 853 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 857 | impl ToTokens for TypeArray { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 858 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 859 | self.bracket_token.surround(tokens, |tokens| { |
| 860 | self.ty.to_tokens(tokens); |
| 861 | self.semi_token.to_tokens(tokens); |
| 862 | self.amt.to_tokens(tokens); |
| 863 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 867 | impl ToTokens for TypePtr { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 868 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 869 | self.star_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 870 | match self.ty.mutability { |
| 871 | Mutability::Mutable(ref tok) => tok.to_tokens(tokens), |
| 872 | Mutability::Immutable => { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 873 | TokensOrDefault(&self.const_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 874 | } |
| 875 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 876 | self.ty.ty.to_tokens(tokens); |
| 877 | } |
| 878 | } |
| 879 | |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 880 | impl ToTokens for TypeReference { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 881 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 882 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 883 | self.lifetime.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 884 | self.ty.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 885 | self.ty.ty.to_tokens(tokens); |
| 886 | } |
| 887 | } |
| 888 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 889 | impl ToTokens for TypeBareFn { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 890 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 891 | self.ty.to_tokens(tokens) |
| 892 | } |
| 893 | } |
| 894 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 895 | impl ToTokens for TypeNever { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 896 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 897 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 901 | impl ToTokens for TypeTuple { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 902 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 903 | self.paren_token.surround(tokens, |tokens| { |
| 904 | self.tys.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 905 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 909 | impl ToTokens for TypePath { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 910 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 911 | PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | impl<'a> ToTokens for PathTokens<'a> { |
| 916 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 917 | let qself = match *self.0 { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 918 | Some(ref qself) => qself, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 919 | None => return self.1.to_tokens(tokens), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 920 | }; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 921 | qself.lt_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 922 | qself.ty.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 923 | |
| 924 | // XXX: Gross. |
| 925 | let pos = if qself.position > 0 && qself.position >= self.1.segments.len() { |
| 926 | self.1.segments.len() - 1 |
| 927 | } else { |
| 928 | qself.position |
| 929 | }; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 930 | let mut segments = self.1.segments.iter(); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 931 | if pos > 0 { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 932 | TokensOrDefault(&qself.as_token).to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 933 | self.1.leading_colon.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 934 | for (i, segment) in (&mut segments).take(pos).enumerate() { |
| 935 | if i + 1 == pos { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 936 | segment.item().to_tokens(tokens); |
| 937 | qself.gt_token.to_tokens(tokens); |
| 938 | segment.delimiter().to_tokens(tokens); |
| 939 | } else { |
| 940 | segment.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 941 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 942 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 943 | } else { |
| 944 | qself.gt_token.to_tokens(tokens); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 945 | self.1.leading_colon.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 946 | } |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 947 | for segment in segments { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 948 | segment.to_tokens(tokens); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 953 | impl ToTokens for TypeTraitObject { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 954 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 955 | self.dyn_token.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 956 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 960 | impl ToTokens for TypeImplTrait { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 961 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 962 | self.impl_token.to_tokens(tokens); |
| 963 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 967 | impl ToTokens for TypeGroup { |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 968 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 969 | self.group_token.surround(tokens, |tokens| { |
| 970 | self.ty.to_tokens(tokens); |
| 971 | }); |
| 972 | } |
| 973 | } |
| 974 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 975 | impl ToTokens for TypeParen { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 976 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 977 | self.paren_token.surround(tokens, |tokens| { |
| 978 | self.ty.to_tokens(tokens); |
| 979 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 983 | impl ToTokens for TypeInfer { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 984 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 985 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 989 | impl ToTokens for Mutability { |
| 990 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 991 | if let Mutability::Mutable(ref t) = *self { |
| 992 | t.to_tokens(tokens); |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 997 | impl ToTokens for Path { |
| 998 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 999 | self.leading_colon.to_tokens(tokens); |
| 1000 | self.segments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | impl ToTokens for PathSegment { |
| 1005 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1006 | self.ident.to_tokens(tokens); |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1007 | self.arguments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1011 | impl ToTokens for PathArguments { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1012 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1013 | match *self { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1014 | PathArguments::None => {} |
| 1015 | PathArguments::AngleBracketed(ref arguments) => { |
| 1016 | arguments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1017 | } |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1018 | PathArguments::Parenthesized(ref arguments) => { |
| 1019 | arguments.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1025 | impl ToTokens for GenericArgument { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1026 | #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 1027 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1028 | match *self { |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1029 | GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens), |
| 1030 | GenericArgument::Type(ref ty) => ty.to_tokens(tokens), |
| 1031 | GenericArgument::TypeBinding(ref tb) => tb.to_tokens(tokens), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame^] | 1032 | GenericArgument::Const(ref e) => match *e { |
| 1033 | Expr::Lit(_) => e.to_tokens(tokens), |
Nika Layzell | ce37f33 | 2017-12-05 12:01:22 -0500 | [diff] [blame] | 1034 | |
| 1035 | // NOTE: We should probably support parsing blocks with only |
| 1036 | // expressions in them without the full feature for const |
| 1037 | // generics. |
| 1038 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame^] | 1039 | Expr::Block(_) => e.to_tokens(tokens), |
Nika Layzell | ce37f33 | 2017-12-05 12:01:22 -0500 | [diff] [blame] | 1040 | |
| 1041 | // ERROR CORRECTION: Add braces to make sure that the |
| 1042 | // generated code is valid. |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 1043 | _ => token::Brace::default().surround(tokens, |tokens| { |
Nika Layzell | ce37f33 | 2017-12-05 12:01:22 -0500 | [diff] [blame] | 1044 | e.to_tokens(tokens); |
| 1045 | }), |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1046 | }, |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1051 | impl ToTokens for AngleBracketedGenericArguments { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1052 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1053 | self.turbofish.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1054 | self.lt_token.to_tokens(tokens); |
Nika Layzell | 357885a | 2017-12-04 15:47:07 -0500 | [diff] [blame] | 1055 | self.args.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1056 | self.gt_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | impl ToTokens for TypeBinding { |
| 1061 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1062 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1063 | self.eq_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1064 | self.ty.to_tokens(tokens); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Nika Layzell | c08227a | 2017-12-04 16:30:17 -0500 | [diff] [blame] | 1068 | impl ToTokens for ParenthesizedGenericArguments { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1069 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1070 | self.paren_token.surround(tokens, |tokens| { |
| 1071 | self.inputs.to_tokens(tokens); |
| 1072 | }); |
| 1073 | self.output.to_tokens(tokens); |
| 1074 | } |
| 1075 | } |
| 1076 | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 1077 | impl ToTokens for ReturnType { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1078 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1079 | match *self { |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 1080 | ReturnType::Default => {} |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 1081 | ReturnType::Type(ref arrow, ref ty) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1082 | arrow.to_tokens(tokens); |
| 1083 | ty.to_tokens(tokens); |
| 1084 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | impl ToTokens for PolyTraitRef { |
| 1090 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1091 | self.bound_lifetimes.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1092 | self.trait_ref.to_tokens(tokens); |
| 1093 | } |
| 1094 | } |
| 1095 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1096 | impl ToTokens for BareFnType { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1097 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1098 | self.lifetimes.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1099 | self.unsafety.to_tokens(tokens); |
| 1100 | self.abi.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1101 | self.fn_token.to_tokens(tokens); |
| 1102 | self.paren_token.surround(tokens, |tokens| { |
| 1103 | self.inputs.to_tokens(tokens); |
David Tolnay | 73cc644 | 2017-12-27 22:17:57 -0500 | [diff] [blame] | 1104 | if let Some(ref variadic) = self.variadic { |
David Tolnay | 0bdb055 | 2017-12-27 21:31:51 -0500 | [diff] [blame] | 1105 | if !self.inputs.empty_or_trailing() { |
| 1106 | let span = variadic.0[0]; |
| 1107 | <Token![,]>::new(span).to_tokens(tokens); |
| 1108 | } |
| 1109 | variadic.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1110 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1111 | }); |
| 1112 | self.output.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1116 | impl ToTokens for BareFnArg { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1117 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1118 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 1119 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1120 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1121 | } |
| 1122 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1123 | } |
| 1124 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1125 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 1126 | impl ToTokens for BareFnArgName { |
| 1127 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1128 | match *self { |
| 1129 | BareFnArgName::Named(ref t) => t.to_tokens(tokens), |
| 1130 | BareFnArgName::Wild(ref t) => t.to_tokens(tokens), |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1135 | impl ToTokens for Unsafety { |
| 1136 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1137 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1138 | Unsafety::Unsafe(ref t) => t.to_tokens(tokens), |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1139 | Unsafety::Normal => { |
| 1140 | // nothing |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | impl ToTokens for Abi { |
| 1147 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1148 | self.extern_token.to_tokens(tokens); |
| 1149 | match self.kind { |
| 1150 | AbiKind::Named(ref named) => named.to_tokens(tokens), |
| 1151 | AbiKind::Default => {} |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1155 | } |