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