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