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