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