David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 9 | use super::*; |
David Tolnay | e303b7c | 2018-05-20 16:46:35 -0700 | [diff] [blame] | 10 | use proc_macro2::{Span, TokenStream}; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 11 | use punctuated::Punctuated; |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 12 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 13 | use std::hash::{Hash, Hasher}; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 14 | #[cfg(feature = "full")] |
| 15 | use std::mem; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 16 | #[cfg(feature = "extra-traits")] |
| 17 | use tt::TokenStreamHelper; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 18 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 19 | ast_enum_of_structs! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 20 | /// A Rust expression. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 21 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 22 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 23 | /// feature.* |
| 24 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 25 | /// # Syntax tree enums |
| 26 | /// |
| 27 | /// This type is a syntax tree enum. In Syn this and other syntax tree enums |
| 28 | /// are designed to be traversed using the following rebinding idiom. |
| 29 | /// |
| 30 | /// ``` |
| 31 | /// # use syn::Expr; |
| 32 | /// # |
| 33 | /// # fn example(expr: Expr) { |
| 34 | /// # const IGNORE: &str = stringify! { |
| 35 | /// let expr: Expr = /* ... */; |
| 36 | /// # }; |
| 37 | /// match expr { |
| 38 | /// Expr::MethodCall(expr) => { |
| 39 | /// /* ... */ |
| 40 | /// } |
| 41 | /// Expr::Cast(expr) => { |
| 42 | /// /* ... */ |
| 43 | /// } |
| 44 | /// Expr::IfLet(expr) => { |
| 45 | /// /* ... */ |
| 46 | /// } |
| 47 | /// /* ... */ |
| 48 | /// # _ => {} |
| 49 | /// } |
| 50 | /// # } |
| 51 | /// ``` |
| 52 | /// |
| 53 | /// We begin with a variable `expr` of type `Expr` that has no fields |
| 54 | /// (because it is an enum), and by matching on it and rebinding a variable |
| 55 | /// with the same name `expr` we effectively imbue our variable with all of |
| 56 | /// the data fields provided by the variant that it turned out to be. So for |
| 57 | /// example above if we ended up in the `MethodCall` case then we get to use |
| 58 | /// `expr.receiver`, `expr.args` etc; if we ended up in the `IfLet` case we |
| 59 | /// get to use `expr.pat`, `expr.then_branch`, `expr.else_branch`. |
| 60 | /// |
| 61 | /// The pattern is similar if the input expression is borrowed: |
| 62 | /// |
| 63 | /// ``` |
| 64 | /// # use syn::Expr; |
| 65 | /// # |
| 66 | /// # fn example(expr: &Expr) { |
| 67 | /// match *expr { |
| 68 | /// Expr::MethodCall(ref expr) => { |
| 69 | /// # } |
| 70 | /// # _ => {} |
| 71 | /// # } |
| 72 | /// # } |
| 73 | /// ``` |
| 74 | /// |
| 75 | /// This approach avoids repeating the variant names twice on every line. |
| 76 | /// |
| 77 | /// ``` |
| 78 | /// # use syn::{Expr, ExprMethodCall}; |
| 79 | /// # |
| 80 | /// # fn example(expr: Expr) { |
| 81 | /// # match expr { |
| 82 | /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive |
| 83 | /// # } |
| 84 | /// # _ => {} |
| 85 | /// # } |
| 86 | /// # } |
| 87 | /// ``` |
| 88 | /// |
| 89 | /// In general, the name to which a syntax tree enum variant is bound should |
| 90 | /// be a suitable name for the complete syntax tree enum type. |
| 91 | /// |
| 92 | /// ``` |
| 93 | /// # use syn::{Expr, ExprField}; |
| 94 | /// # |
| 95 | /// # fn example(discriminant: &ExprField) { |
| 96 | /// // Binding is called `base` which is the name I would use if I were |
| 97 | /// // assigning `*discriminant.base` without an `if let`. |
| 98 | /// if let Expr::Tuple(ref base) = *discriminant.base { |
| 99 | /// # } |
| 100 | /// # } |
| 101 | /// ``` |
| 102 | /// |
| 103 | /// A sign that you may not be choosing the right variable names is if you |
| 104 | /// see names getting repeated in your code, like accessing |
| 105 | /// `receiver.receiver` or `pat.pat` or `cond.cond`. |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 106 | pub enum Expr { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 107 | /// A box expression: `box f`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 108 | /// |
| 109 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 110 | pub Box(ExprBox #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 111 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 112 | pub box_token: Token![box], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 113 | pub expr: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 114 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 115 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 116 | /// A placement expression: `place <- value`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 117 | /// |
| 118 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 119 | pub InPlace(ExprInPlace #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 120 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 121 | pub place: Box<Expr>, |
David Tolnay | 8701a5c | 2017-12-28 23:31:10 -0500 | [diff] [blame] | 122 | pub arrow_token: Token![<-], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 123 | pub value: Box<Expr>, |
| 124 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 125 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 126 | /// A slice literal expression: `[a, b, c, d]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 127 | /// |
| 128 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 129 | pub Array(ExprArray #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 130 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 131 | pub bracket_token: token::Bracket, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 132 | pub elems: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 133 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 134 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 135 | /// A function call expression: `invoke(a, b)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 136 | /// |
| 137 | /// *This type is available if Syn is built with the `"derive"` or |
| 138 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 139 | pub Call(ExprCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 140 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 141 | pub func: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 142 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 143 | pub args: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 144 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 145 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 146 | /// A method call expression: `x.foo::<T>(a, b)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 147 | /// |
| 148 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 149 | pub MethodCall(ExprMethodCall #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 150 | pub attrs: Vec<Attribute>, |
David Tolnay | 7641851 | 2017-12-28 23:47:47 -0500 | [diff] [blame] | 151 | pub receiver: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 152 | pub dot_token: Token![.], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 153 | pub method: Ident, |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 154 | pub turbofish: Option<MethodTurbofish>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 155 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 156 | pub args: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 157 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 158 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 159 | /// A tuple expression: `(a, b, c, d)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 160 | /// |
| 161 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 162 | pub Tuple(ExprTuple #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 163 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 164 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 165 | pub elems: Punctuated<Expr, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 166 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 167 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 168 | /// A binary operation: `a + b`, `a * b`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 169 | /// |
| 170 | /// *This type is available if Syn is built with the `"derive"` or |
| 171 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 172 | pub Binary(ExprBinary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 173 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 174 | pub left: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 175 | pub op: BinOp, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 176 | pub right: Box<Expr>, |
| 177 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 178 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 179 | /// A unary operation: `!x`, `*x`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 180 | /// |
| 181 | /// *This type is available if Syn is built with the `"derive"` or |
| 182 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 183 | pub Unary(ExprUnary { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 184 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 185 | pub op: UnOp, |
| 186 | pub expr: Box<Expr>, |
| 187 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 188 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 189 | /// A literal in place of an expression: `1`, `"foo"`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 190 | /// |
| 191 | /// *This type is available if Syn is built with the `"derive"` or |
| 192 | /// `"full"` feature.* |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 193 | pub Lit(ExprLit { |
| 194 | pub attrs: Vec<Attribute>, |
| 195 | pub lit: Lit, |
| 196 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 197 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 198 | /// A cast expression: `foo as f64`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 199 | /// |
| 200 | /// *This type is available if Syn is built with the `"derive"` or |
| 201 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 202 | pub Cast(ExprCast { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 203 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 204 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 205 | pub as_token: Token![as], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 206 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 207 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 208 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 209 | /// A type ascription expression: `foo: f64`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 210 | /// |
| 211 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 212 | pub Type(ExprType #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 213 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 214 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 215 | pub colon_token: Token![:], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 216 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 217 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 218 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 219 | /// An `if` expression with an optional `else` block: `if expr { ... } |
| 220 | /// else { ... }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 221 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 222 | /// The `else` branch expression may only be an `If`, `IfLet`, or |
| 223 | /// `Block` expression, not any of the other types of expression. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 224 | /// |
| 225 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 226 | pub If(ExprIf #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 227 | pub attrs: Vec<Attribute>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 228 | pub if_token: Token![if], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 229 | pub cond: Box<Expr>, |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 230 | pub then_branch: Block, |
| 231 | pub else_branch: Option<(Token![else], Box<Expr>)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 232 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 233 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 234 | /// An `if let` expression with an optional `else` block: `if let pat = |
| 235 | /// expr { ... } else { ... }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 236 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 237 | /// The `else` branch expression may only be an `If`, `IfLet`, or |
| 238 | /// `Block` expression, not any of the other types of expression. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 239 | /// |
| 240 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 241 | pub IfLet(ExprIfLet #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 242 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 243 | pub if_token: Token![if], |
| 244 | pub let_token: Token![let], |
David Tolnay | 5b5b7d2 | 2018-03-31 21:05:00 +0200 | [diff] [blame] | 245 | pub pats: Punctuated<Pat, Token![|]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 246 | pub eq_token: Token![=], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 247 | pub expr: Box<Expr>, |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 248 | pub then_branch: Block, |
| 249 | pub else_branch: Option<(Token![else], Box<Expr>)>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 250 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 251 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 252 | /// A while loop: `while expr { ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 253 | /// |
| 254 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 255 | pub While(ExprWhile #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 256 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 257 | pub label: Option<Label>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 258 | pub while_token: Token![while], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 259 | pub cond: Box<Expr>, |
| 260 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 261 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 262 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 263 | /// A while-let loop: `while let pat = expr { ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 264 | /// |
| 265 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 266 | pub WhileLet(ExprWhileLet #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 267 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 268 | pub label: Option<Label>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 269 | pub while_token: Token![while], |
| 270 | pub let_token: Token![let], |
David Tolnay | 5b5b7d2 | 2018-03-31 21:05:00 +0200 | [diff] [blame] | 271 | pub pats: Punctuated<Pat, Token![|]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 272 | pub eq_token: Token![=], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 273 | pub expr: Box<Expr>, |
| 274 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 275 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 276 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 277 | /// A for loop: `for pat in expr { ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 278 | /// |
| 279 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 280 | pub ForLoop(ExprForLoop #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 281 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 282 | pub label: Option<Label>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 283 | pub for_token: Token![for], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 284 | pub pat: Box<Pat>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 285 | pub in_token: Token![in], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 286 | pub expr: Box<Expr>, |
| 287 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 288 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 289 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 290 | /// Conditionless loop: `loop { ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 291 | /// |
| 292 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 293 | pub Loop(ExprLoop #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 294 | pub attrs: Vec<Attribute>, |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 295 | pub label: Option<Label>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 296 | pub loop_token: Token![loop], |
| 297 | pub body: Block, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 298 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 299 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 300 | /// A `match` expression: `match n { Some(n) => {}, None => {} }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 301 | /// |
| 302 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 303 | pub Match(ExprMatch #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 304 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 305 | pub match_token: Token![match], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 306 | pub expr: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 307 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 308 | pub arms: Vec<Arm>, |
| 309 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 310 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 311 | /// A closure expression: `|a, b| a + b`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 312 | /// |
| 313 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 314 | pub Closure(ExprClosure #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 315 | pub attrs: Vec<Attribute>, |
Yusuke Sasaki | 4e5d966 | 2018-07-21 02:49:47 +0900 | [diff] [blame] | 316 | pub asyncness: Option<Token![async]>, |
David Tolnay | 13d4c0e | 2018-03-31 20:53:59 +0200 | [diff] [blame] | 317 | pub movability: Option<Token![static]>, |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 318 | pub capture: Option<Token![move]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 319 | pub or1_token: Token![|], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 320 | pub inputs: Punctuated<FnArg, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 321 | pub or2_token: Token![|], |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame] | 322 | pub output: ReturnType, |
| 323 | pub body: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 324 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 325 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 326 | /// An unsafe block: `unsafe { ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 327 | /// |
| 328 | /// *This type is available if Syn is built with the `"full"` feature.* |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 329 | pub Unsafe(ExprUnsafe #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 330 | pub attrs: Vec<Attribute>, |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 331 | pub unsafe_token: Token![unsafe], |
| 332 | pub block: Block, |
| 333 | }), |
| 334 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 335 | /// A blocked scope: `{ ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 336 | /// |
| 337 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 338 | pub Block(ExprBlock #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 339 | pub attrs: Vec<Attribute>, |
David Tolnay | 1d8e996 | 2018-08-24 19:04:20 -0400 | [diff] [blame] | 340 | pub label: Option<Label>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 341 | pub block: Block, |
| 342 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 343 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 344 | /// An assignment expression: `a = compute()`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 345 | /// |
| 346 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 347 | pub Assign(ExprAssign #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 348 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 349 | pub left: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 350 | pub eq_token: Token![=], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 351 | pub right: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 352 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 353 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 354 | /// A compound assignment expression: `counter += 1`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 355 | /// |
| 356 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 357 | pub AssignOp(ExprAssignOp #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 358 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 359 | pub left: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 360 | pub op: BinOp, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 361 | pub right: Box<Expr>, |
| 362 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 363 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 364 | /// Access of a named struct field (`obj.k`) or unnamed tuple struct |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 365 | /// field (`obj.0`). |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 366 | /// |
| 367 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | d514774 | 2018-06-30 10:09:52 -0700 | [diff] [blame] | 368 | pub Field(ExprField { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 369 | pub attrs: Vec<Attribute>, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 370 | pub base: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 371 | pub dot_token: Token![.], |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 372 | pub member: Member, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 373 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 374 | |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 375 | /// A square bracketed indexing expression: `vector[2]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 376 | /// |
| 377 | /// *This type is available if Syn is built with the `"derive"` or |
| 378 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 379 | pub Index(ExprIndex { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 380 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 381 | pub expr: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 382 | pub bracket_token: token::Bracket, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 383 | pub index: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 384 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 385 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 386 | /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 387 | /// |
| 388 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 389 | pub Range(ExprRange #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 390 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 391 | pub from: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 392 | pub limits: RangeLimits, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 393 | pub to: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 394 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 395 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 396 | /// A path like `std::mem::replace` possibly containing generic |
| 397 | /// parameters and a qualified self-type. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 398 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 399 | /// A plain identifier like `x` is a path of length 1. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 400 | /// |
| 401 | /// *This type is available if Syn is built with the `"derive"` or |
| 402 | /// `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 403 | pub Path(ExprPath { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 404 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 405 | pub qself: Option<QSelf>, |
| 406 | pub path: Path, |
| 407 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 408 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 409 | /// A referencing operation: `&a` or `&mut a`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 410 | /// |
| 411 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 00674ba | 2018-03-31 18:14:11 +0200 | [diff] [blame] | 412 | pub Reference(ExprReference #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 413 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 414 | pub and_token: Token![&], |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 415 | pub mutability: Option<Token![mut]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 416 | pub expr: Box<Expr>, |
| 417 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 418 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 419 | /// A `break`, with an optional label to break and an optional |
| 420 | /// expression. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 421 | /// |
| 422 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 423 | pub Break(ExprBreak #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 424 | pub attrs: Vec<Attribute>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 425 | pub break_token: Token![break], |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 426 | pub label: Option<Lifetime>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 427 | pub expr: Option<Box<Expr>>, |
| 428 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 429 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 430 | /// A `continue`, with an optional label. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 431 | /// |
| 432 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 433 | pub Continue(ExprContinue #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 434 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 435 | pub continue_token: Token![continue], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 436 | pub label: Option<Lifetime>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 437 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 438 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 439 | /// A `return`, with an optional value to be returned. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 440 | /// |
| 441 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | c246cd3 | 2017-12-28 23:14:32 -0500 | [diff] [blame] | 442 | pub Return(ExprReturn #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 443 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 444 | pub return_token: Token![return], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 445 | pub expr: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 446 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 447 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 448 | /// A macro invocation expression: `format!("{}", q)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 449 | /// |
| 450 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 451 | pub Macro(ExprMacro #full { |
| 452 | pub attrs: Vec<Attribute>, |
| 453 | pub mac: Macro, |
| 454 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 455 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 456 | /// A struct literal expression: `Point { x: 1, y: 1 }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 457 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 458 | /// The `rest` provides the value of the remaining fields as in `S { a: |
| 459 | /// 1, b: 1, ..rest }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 460 | /// |
| 461 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 462 | pub Struct(ExprStruct #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 463 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 464 | pub path: Path, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 465 | pub brace_token: token::Brace, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 466 | pub fields: Punctuated<FieldValue, Token![,]>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 467 | pub dot2_token: Option<Token![..]>, |
| 468 | pub rest: Option<Box<Expr>>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 469 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 470 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 471 | /// An array literal constructed from one repeated element: `[0u8; N]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 472 | /// |
| 473 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 474 | pub Repeat(ExprRepeat #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 475 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 476 | pub bracket_token: token::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 477 | pub expr: Box<Expr>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 478 | pub semi_token: Token![;], |
David Tolnay | 84d8044 | 2018-01-07 01:03:20 -0800 | [diff] [blame] | 479 | pub len: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 480 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 481 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 482 | /// A parenthesized expression: `(a + b)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 483 | /// |
| 484 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 9374bc0 | 2018-01-27 18:49:36 -0800 | [diff] [blame] | 485 | pub Paren(ExprParen { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 486 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 487 | pub paren_token: token::Paren, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 488 | pub expr: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 489 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 490 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 491 | /// An expression contained within invisible delimiters. |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 492 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 493 | /// This variant is important for faithfully representing the precedence |
| 494 | /// of expressions and is related to `None`-delimited spans in a |
| 495 | /// `TokenStream`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 496 | /// |
| 497 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 498 | pub Group(ExprGroup #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 499 | pub attrs: Vec<Attribute>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 500 | pub group_token: token::Group, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 501 | pub expr: Box<Expr>, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 502 | }), |
| 503 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 504 | /// A try-expression: `expr?`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 505 | /// |
| 506 | /// *This type is available if Syn is built with the `"full"` feature.* |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 507 | pub Try(ExprTry #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 508 | pub attrs: Vec<Attribute>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 509 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 510 | pub question_token: Token![?], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 511 | }), |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 512 | |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 513 | /// An async block: `async { ... }`. |
| 514 | /// |
| 515 | /// *This type is available if Syn is built with the `"full"` feature.* |
| 516 | pub Async(ExprAsync #full { |
| 517 | pub attrs: Vec<Attribute>, |
| 518 | pub async_token: Token![async], |
| 519 | pub capture: Option<Token![move]>, |
| 520 | pub block: Block, |
| 521 | }), |
| 522 | |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 523 | /// A try block: `try { ... }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 524 | /// |
| 525 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 526 | pub TryBlock(ExprTryBlock #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 527 | pub attrs: Vec<Attribute>, |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 528 | pub try_token: Token![try], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 529 | pub block: Block, |
| 530 | }), |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 531 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 532 | /// A yield expression: `yield expr`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 533 | /// |
| 534 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 535 | pub Yield(ExprYield #full { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 536 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 537 | pub yield_token: Token![yield], |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 538 | pub expr: Option<Box<Expr>>, |
| 539 | }), |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 540 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 541 | /// Tokens in expression position not interpreted by Syn. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 542 | /// |
| 543 | /// *This type is available if Syn is built with the `"derive"` or |
| 544 | /// `"full"` feature.* |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 545 | pub Verbatim(ExprVerbatim #manual_extra_traits { |
| 546 | pub tts: TokenStream, |
| 547 | }), |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | #[cfg(feature = "extra-traits")] |
| 552 | impl Eq for ExprVerbatim {} |
| 553 | |
| 554 | #[cfg(feature = "extra-traits")] |
| 555 | impl PartialEq for ExprVerbatim { |
| 556 | fn eq(&self, other: &Self) -> bool { |
| 557 | TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | #[cfg(feature = "extra-traits")] |
| 562 | impl Hash for ExprVerbatim { |
| 563 | fn hash<H>(&self, state: &mut H) |
| 564 | where |
| 565 | H: Hasher, |
| 566 | { |
| 567 | TokenStreamHelper(&self.tts).hash(state); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 568 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 569 | } |
| 570 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 571 | impl Expr { |
David Tolnay | 096d498 | 2017-12-28 23:18:18 -0500 | [diff] [blame] | 572 | #[cfg(feature = "full")] |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 573 | fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 574 | match *self { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 575 | Expr::Box(ExprBox { ref mut attrs, .. }) |
| 576 | | Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
| 577 | | Expr::Array(ExprArray { ref mut attrs, .. }) |
| 578 | | Expr::Call(ExprCall { ref mut attrs, .. }) |
| 579 | | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
| 580 | | Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
| 581 | | Expr::Binary(ExprBinary { ref mut attrs, .. }) |
| 582 | | Expr::Unary(ExprUnary { ref mut attrs, .. }) |
| 583 | | Expr::Lit(ExprLit { ref mut attrs, .. }) |
| 584 | | Expr::Cast(ExprCast { ref mut attrs, .. }) |
| 585 | | Expr::Type(ExprType { ref mut attrs, .. }) |
| 586 | | Expr::If(ExprIf { ref mut attrs, .. }) |
| 587 | | Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
| 588 | | Expr::While(ExprWhile { ref mut attrs, .. }) |
| 589 | | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
| 590 | | Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
| 591 | | Expr::Loop(ExprLoop { ref mut attrs, .. }) |
| 592 | | Expr::Match(ExprMatch { ref mut attrs, .. }) |
| 593 | | Expr::Closure(ExprClosure { ref mut attrs, .. }) |
| 594 | | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
| 595 | | Expr::Block(ExprBlock { ref mut attrs, .. }) |
| 596 | | Expr::Assign(ExprAssign { ref mut attrs, .. }) |
| 597 | | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
| 598 | | Expr::Field(ExprField { ref mut attrs, .. }) |
| 599 | | Expr::Index(ExprIndex { ref mut attrs, .. }) |
| 600 | | Expr::Range(ExprRange { ref mut attrs, .. }) |
| 601 | | Expr::Path(ExprPath { ref mut attrs, .. }) |
David Tolnay | 00674ba | 2018-03-31 18:14:11 +0200 | [diff] [blame] | 602 | | Expr::Reference(ExprReference { ref mut attrs, .. }) |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 603 | | Expr::Break(ExprBreak { ref mut attrs, .. }) |
| 604 | | Expr::Continue(ExprContinue { ref mut attrs, .. }) |
| 605 | | Expr::Return(ExprReturn { ref mut attrs, .. }) |
| 606 | | Expr::Macro(ExprMacro { ref mut attrs, .. }) |
| 607 | | Expr::Struct(ExprStruct { ref mut attrs, .. }) |
| 608 | | Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
| 609 | | Expr::Paren(ExprParen { ref mut attrs, .. }) |
| 610 | | Expr::Group(ExprGroup { ref mut attrs, .. }) |
| 611 | | Expr::Try(ExprTry { ref mut attrs, .. }) |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 612 | | Expr::Async(ExprAsync { ref mut attrs, .. }) |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 613 | | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. }) |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 614 | | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new), |
David Tolnay | 10f464a | 2018-08-30 18:48:55 -0700 | [diff] [blame] | 615 | Expr::Verbatim(_) => Vec::new(), |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 620 | ast_enum! { |
| 621 | /// A struct or tuple struct field accessed in a struct literal or field |
| 622 | /// expression. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 623 | /// |
| 624 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 625 | /// feature.* |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 626 | pub enum Member { |
| 627 | /// A named field like `self.x`. |
| 628 | Named(Ident), |
| 629 | /// An unnamed field like `self.0`. |
| 630 | Unnamed(Index), |
| 631 | } |
| 632 | } |
| 633 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 634 | ast_struct! { |
| 635 | /// The index of an unnamed tuple struct field. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 636 | /// |
| 637 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 638 | /// feature.* |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 639 | pub struct Index #manual_extra_traits { |
| 640 | pub index: u32, |
| 641 | pub span: Span, |
| 642 | } |
| 643 | } |
| 644 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 645 | impl From<usize> for Index { |
| 646 | fn from(index: usize) -> Index { |
David Tolnay | 34071ba | 2018-05-20 20:00:41 -0700 | [diff] [blame] | 647 | assert!(index < u32::max_value() as usize); |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 648 | Index { |
| 649 | index: index as u32, |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 650 | span: Span::call_site(), |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 656 | impl Eq for Index {} |
| 657 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 658 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 659 | impl PartialEq for Index { |
| 660 | fn eq(&self, other: &Self) -> bool { |
| 661 | self.index == other.index |
| 662 | } |
| 663 | } |
| 664 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 665 | #[cfg(feature = "extra-traits")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 666 | impl Hash for Index { |
| 667 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 668 | self.index.hash(state); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 673 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 674 | /// The `::<>` explicit type parameters passed to a method call: |
| 675 | /// `parse::<u64>()`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 676 | /// |
| 677 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 678 | pub struct MethodTurbofish { |
| 679 | pub colon2_token: Token![::], |
| 680 | pub lt_token: Token![<], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 681 | pub args: Punctuated<GenericMethodArgument, Token![,]>, |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 682 | pub gt_token: Token![>], |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | #[cfg(feature = "full")] |
| 687 | ast_enum! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 688 | /// An individual generic argument to a method, like `T`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 689 | /// |
| 690 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 691 | pub enum GenericMethodArgument { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 692 | /// A type argument. |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 693 | Type(Type), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 694 | /// A const expression. Must be inside of a block. |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 695 | /// |
| 696 | /// NOTE: Identity expressions are represented as Type arguments, as |
| 697 | /// they are indistinguishable syntactically. |
| 698 | Const(Expr), |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | #[cfg(feature = "full")] |
| 703 | ast_struct! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 704 | /// A field-value pair in a struct literal. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 705 | /// |
| 706 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 707 | pub struct FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 708 | /// Attributes tagged on the field. |
| 709 | pub attrs: Vec<Attribute>, |
| 710 | |
| 711 | /// Name or index of the field. |
| 712 | pub member: Member, |
| 713 | |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 714 | /// The colon in `Struct { x: x }`. If written in shorthand like |
| 715 | /// `Struct { x }`, there is no colon. |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 716 | pub colon_token: Option<Token![:]>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 717 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 718 | /// Value of the field. |
| 719 | pub expr: Expr, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 720 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 723 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 724 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 725 | /// A lifetime labeling a `for`, `while`, or `loop`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 726 | /// |
| 727 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 728 | pub struct Label { |
| 729 | pub name: Lifetime, |
| 730 | pub colon_token: Token![:], |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | #[cfg(feature = "full")] |
| 735 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 736 | /// A braced block containing Rust statements. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 737 | /// |
| 738 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 739 | pub struct Block { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 740 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 741 | /// Statements in a block |
| 742 | pub stmts: Vec<Stmt>, |
| 743 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 746 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 747 | ast_enum! { |
| 748 | /// A statement, usually ending in a semicolon. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 749 | /// |
| 750 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 751 | pub enum Stmt { |
| 752 | /// A local (let) binding. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 753 | Local(Local), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 754 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 755 | /// An item definition. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 756 | Item(Item), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 757 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 758 | /// Expr without trailing semicolon. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 759 | Expr(Expr), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 760 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 761 | /// Expression with trailing semicolon. |
David Tolnay | 1f0b7b8 | 2018-01-06 16:07:14 -0800 | [diff] [blame] | 762 | Semi(Expr, Token![;]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 763 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 766 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 767 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 768 | /// A local `let` binding: `let x: u64 = s.parse()?`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 769 | /// |
| 770 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 771 | pub struct Local { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 772 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 773 | pub let_token: Token![let], |
David Tolnay | 5b5b7d2 | 2018-03-31 21:05:00 +0200 | [diff] [blame] | 774 | pub pats: Punctuated<Pat, Token![|]>, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 775 | pub ty: Option<(Token![:], Box<Type>)>, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 776 | pub init: Option<(Token![=], Box<Expr>)>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 777 | pub semi_token: Token![;], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 778 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 781 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 782 | ast_enum_of_structs! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 783 | /// A pattern in a local binding, function signature, match expression, or |
| 784 | /// various other places. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 785 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 786 | /// *This type is available if Syn is built with the `"full"` feature.* |
| 787 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 788 | /// # Syntax tree enum |
| 789 | /// |
| 790 | /// This type is a [syntax tree enum]. |
| 791 | /// |
| 792 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 793 | // Clippy false positive |
| 794 | // https://github.com/Manishearth/rust-clippy/issues/1241 |
| 795 | #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))] |
| 796 | pub enum Pat { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 797 | /// A pattern that matches any value: `_`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 798 | /// |
| 799 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 800 | pub Wild(PatWild { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 801 | pub underscore_token: Token![_], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 802 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 803 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 804 | /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 805 | /// |
| 806 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 807 | pub Ident(PatIdent { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 808 | pub by_ref: Option<Token![ref]>, |
| 809 | pub mutability: Option<Token![mut]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 810 | pub ident: Ident, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 811 | pub subpat: Option<(Token![@], Box<Pat>)>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 812 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 813 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 814 | /// A struct or struct variant pattern: `Variant { x, y, .. }`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 815 | /// |
| 816 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 817 | pub Struct(PatStruct { |
| 818 | pub path: Path, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 819 | pub brace_token: token::Brace, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 820 | pub fields: Punctuated<FieldPat, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 821 | pub dot2_token: Option<Token![..]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 822 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 823 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 824 | /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 825 | /// |
| 826 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 827 | pub TupleStruct(PatTupleStruct { |
| 828 | pub path: Path, |
| 829 | pub pat: PatTuple, |
| 830 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 831 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 832 | /// A path pattern like `Color::Red`, optionally qualified with a |
| 833 | /// self-type. |
| 834 | /// |
| 835 | /// Unquailfied path patterns can legally refer to variants, structs, |
| 836 | /// constants or associated constants. Quailfied path patterns like |
| 837 | /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to |
| 838 | /// associated constants. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 839 | /// |
| 840 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 841 | pub Path(PatPath { |
| 842 | pub qself: Option<QSelf>, |
| 843 | pub path: Path, |
| 844 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 845 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 846 | /// A tuple pattern: `(a, b)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 847 | /// |
| 848 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 849 | pub Tuple(PatTuple { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 850 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 851 | pub front: Punctuated<Pat, Token![,]>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 852 | pub dot2_token: Option<Token![..]>, |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 853 | pub comma_token: Option<Token![,]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 854 | pub back: Punctuated<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 855 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 856 | |
| 857 | /// A box pattern: `box v`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 858 | /// |
| 859 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 860 | pub Box(PatBox { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 861 | pub box_token: Token![box], |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 862 | pub pat: Box<Pat>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 863 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 864 | |
| 865 | /// A reference pattern: `&mut (first, second)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 866 | /// |
| 867 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 868 | pub Ref(PatRef { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 869 | pub and_token: Token![&], |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 870 | pub mutability: Option<Token![mut]>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 871 | pub pat: Box<Pat>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 872 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 873 | |
| 874 | /// A literal pattern: `0`. |
| 875 | /// |
| 876 | /// This holds an `Expr` rather than a `Lit` because negative numbers |
| 877 | /// are represented as an `Expr::Unary`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 878 | /// |
| 879 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 880 | pub Lit(PatLit { |
| 881 | pub expr: Box<Expr>, |
| 882 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 883 | |
| 884 | /// A range pattern: `1..=2`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 885 | /// |
| 886 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 887 | pub Range(PatRange { |
| 888 | pub lo: Box<Expr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 889 | pub limits: RangeLimits, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 890 | pub hi: Box<Expr>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 891 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 892 | |
| 893 | /// A dynamically sized slice pattern: `[a, b, i.., y, z]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 894 | /// |
| 895 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 896 | pub Slice(PatSlice { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 897 | pub bracket_token: token::Bracket, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 898 | pub front: Punctuated<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 899 | pub middle: Option<Box<Pat>>, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 900 | pub dot2_token: Option<Token![..]>, |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 901 | pub comma_token: Option<Token![,]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 902 | pub back: Punctuated<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 903 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 904 | |
| 905 | /// A macro in expression position. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 906 | /// |
| 907 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 908 | pub Macro(PatMacro { |
| 909 | pub mac: Macro, |
| 910 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 911 | |
| 912 | /// Tokens in pattern position not interpreted by Syn. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 913 | /// |
| 914 | /// *This type is available if Syn is built with the `"full"` feature.* |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 915 | pub Verbatim(PatVerbatim #manual_extra_traits { |
| 916 | pub tts: TokenStream, |
| 917 | }), |
| 918 | } |
| 919 | } |
| 920 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 921 | #[cfg(all(feature = "full", feature = "extra-traits"))] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 922 | impl Eq for PatVerbatim {} |
| 923 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 924 | #[cfg(all(feature = "full", feature = "extra-traits"))] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 925 | impl PartialEq for PatVerbatim { |
| 926 | fn eq(&self, other: &Self) -> bool { |
| 927 | TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
| 928 | } |
| 929 | } |
| 930 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 931 | #[cfg(all(feature = "full", feature = "extra-traits"))] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 932 | impl Hash for PatVerbatim { |
| 933 | fn hash<H>(&self, state: &mut H) |
| 934 | where |
| 935 | H: Hasher, |
| 936 | { |
| 937 | TokenStreamHelper(&self.tts).hash(state); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 938 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 941 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 942 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 943 | /// One arm of a `match` expression: `0...10 => { return true; }`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 944 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 945 | /// As in: |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 946 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 947 | /// ```rust |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 948 | /// # fn f() -> bool { |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 949 | /// # let n = 0; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 950 | /// match n { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 951 | /// 0...10 => { |
| 952 | /// return true; |
| 953 | /// } |
| 954 | /// // ... |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 955 | /// # _ => {} |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 956 | /// } |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 957 | /// # false |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 958 | /// # } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 959 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 960 | /// |
| 961 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 962 | pub struct Arm { |
| 963 | pub attrs: Vec<Attribute>, |
David Tolnay | 18cc4d4 | 2018-03-31 18:47:20 +0200 | [diff] [blame] | 964 | pub leading_vert: Option<Token![|]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 965 | pub pats: Punctuated<Pat, Token![|]>, |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 966 | pub guard: Option<(Token![if], Box<Expr>)>, |
David Tolnay | dfb9143 | 2018-03-31 19:19:44 +0200 | [diff] [blame] | 967 | pub fat_arrow_token: Token![=>], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 968 | pub body: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 969 | pub comma: Option<Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 970 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 973 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 974 | ast_enum! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 975 | /// Limit types of a range, inclusive or exclusive. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 976 | /// |
| 977 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 978 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 979 | pub enum RangeLimits { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 980 | /// Inclusive at the beginning, exclusive at the end. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 981 | HalfOpen(Token![..]), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 982 | /// Inclusive at the beginning and end. |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 983 | Closed(Token![..=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 984 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 987 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 988 | ast_struct! { |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 989 | /// A single field in a struct pattern. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 990 | /// |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 991 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated |
| 992 | /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 993 | /// |
| 994 | /// *This type is available if Syn is built with the `"full"` feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 995 | pub struct FieldPat { |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 996 | pub attrs: Vec<Attribute>, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 997 | pub member: Member, |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 998 | pub colon_token: Option<Token![:]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 999 | pub pat: Box<Pat>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1000 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1003 | #[cfg(any(feature = "parsing", feature = "printing"))] |
| 1004 | #[cfg(feature = "full")] |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 1005 | fn requires_terminator(expr: &Expr) -> bool { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1006 | // see https://github.com/rust-lang/rust/blob/eb8f2586e/src/libsyntax/parse/classify.rs#L17-L37 |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1007 | match *expr { |
| 1008 | Expr::Unsafe(..) |
| 1009 | | Expr::Block(..) |
| 1010 | | Expr::If(..) |
| 1011 | | Expr::IfLet(..) |
| 1012 | | Expr::Match(..) |
| 1013 | | Expr::While(..) |
| 1014 | | Expr::WhileLet(..) |
| 1015 | | Expr::Loop(..) |
| 1016 | | Expr::ForLoop(..) |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 1017 | | Expr::Async(..) |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 1018 | | Expr::TryBlock(..) => false, |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1019 | _ => true, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 1023 | #[cfg(feature = "parsing")] |
| 1024 | pub mod parsing { |
| 1025 | use super::*; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 1026 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1027 | #[cfg(feature = "full")] |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 1028 | use ext::IdentExt; |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 1029 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 94d304f | 2018-08-30 23:43:53 -0700 | [diff] [blame] | 1030 | use path; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1031 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1032 | // When we're parsing expressions which occur before blocks, like in an if |
| 1033 | // statement's condition, we cannot parse a struct literal. |
| 1034 | // |
| 1035 | // Struct literals are ambiguous in certain positions |
| 1036 | // https://github.com/rust-lang/rfcs/pull/92 |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 1037 | #[derive(Copy, Clone)] |
| 1038 | pub struct AllowStruct(bool); |
| 1039 | |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1040 | #[derive(Copy, Clone, PartialEq, PartialOrd)] |
| 1041 | enum Precedence { |
| 1042 | Any, |
| 1043 | Assign, |
| 1044 | Placement, |
| 1045 | Range, |
| 1046 | Or, |
| 1047 | And, |
| 1048 | Compare, |
| 1049 | BitOr, |
| 1050 | BitXor, |
| 1051 | BitAnd, |
| 1052 | Shift, |
| 1053 | Arithmetic, |
| 1054 | Term, |
| 1055 | Cast, |
| 1056 | } |
| 1057 | |
| 1058 | impl Precedence { |
| 1059 | fn of(op: &BinOp) -> Self { |
| 1060 | match *op { |
| 1061 | BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic, |
| 1062 | BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term, |
| 1063 | BinOp::And(_) => Precedence::And, |
| 1064 | BinOp::Or(_) => Precedence::Or, |
| 1065 | BinOp::BitXor(_) => Precedence::BitXor, |
| 1066 | BinOp::BitAnd(_) => Precedence::BitAnd, |
| 1067 | BinOp::BitOr(_) => Precedence::BitOr, |
| 1068 | BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift, |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1069 | BinOp::Eq(_) |
| 1070 | | BinOp::Lt(_) |
| 1071 | | BinOp::Le(_) |
| 1072 | | BinOp::Ne(_) |
| 1073 | | BinOp::Ge(_) |
| 1074 | | BinOp::Gt(_) => Precedence::Compare, |
| 1075 | BinOp::AddEq(_) |
| 1076 | | BinOp::SubEq(_) |
| 1077 | | BinOp::MulEq(_) |
| 1078 | | BinOp::DivEq(_) |
| 1079 | | BinOp::RemEq(_) |
| 1080 | | BinOp::BitXorEq(_) |
| 1081 | | BinOp::BitAndEq(_) |
| 1082 | | BinOp::BitOrEq(_) |
| 1083 | | BinOp::ShlEq(_) |
| 1084 | | BinOp::ShrEq(_) => Precedence::Assign, |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 1089 | impl Parse for Expr { |
| 1090 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1091 | ambiguous_expr(input, AllowStruct(true)) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1095 | #[cfg(feature = "full")] |
David Tolnay | 9fb0aed | 2018-08-27 10:23:12 -0700 | [diff] [blame] | 1096 | fn expr_no_struct(input: ParseStream) -> Result<Expr> { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1097 | ambiguous_expr(input, AllowStruct(false)) |
David Tolnay | 9fb0aed | 2018-08-27 10:23:12 -0700 | [diff] [blame] | 1098 | } |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1099 | |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1100 | #[cfg(feature = "full")] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1101 | fn parse_expr( |
| 1102 | input: ParseStream, |
| 1103 | mut lhs: Expr, |
| 1104 | allow_struct: AllowStruct, |
| 1105 | base: Precedence, |
| 1106 | ) -> Result<Expr> { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1107 | loop { |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1108 | if input |
| 1109 | .fork() |
| 1110 | .parse::<BinOp>() |
| 1111 | .ok() |
| 1112 | .map_or(false, |op| Precedence::of(&op) >= base) |
| 1113 | { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1114 | let op: BinOp = input.parse()?; |
| 1115 | let precedence = Precedence::of(&op); |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1116 | let mut rhs = unary_expr(input, allow_struct)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1117 | loop { |
| 1118 | let next = peek_precedence(input); |
| 1119 | if next > precedence || next == precedence && precedence == Precedence::Assign { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1120 | rhs = parse_expr(input, rhs, allow_struct, next)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1121 | } else { |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
David Tolnay | 9027625 | 2018-08-31 10:50:08 -0700 | [diff] [blame] | 1125 | lhs = if precedence == Precedence::Assign { |
| 1126 | Expr::AssignOp(ExprAssignOp { |
| 1127 | attrs: Vec::new(), |
| 1128 | left: Box::new(lhs), |
| 1129 | op: op, |
| 1130 | right: Box::new(rhs), |
| 1131 | }) |
| 1132 | } else { |
| 1133 | Expr::Binary(ExprBinary { |
| 1134 | attrs: Vec::new(), |
| 1135 | left: Box::new(lhs), |
| 1136 | op: op, |
| 1137 | right: Box::new(rhs), |
| 1138 | }) |
| 1139 | }; |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1140 | } else if Precedence::Assign >= base |
| 1141 | && input.peek(Token![=]) |
| 1142 | && !input.peek(Token![==]) |
| 1143 | && !input.peek(Token![=>]) |
| 1144 | { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1145 | let eq_token: Token![=] = input.parse()?; |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1146 | let mut rhs = unary_expr(input, allow_struct)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1147 | loop { |
| 1148 | let next = peek_precedence(input); |
| 1149 | if next >= Precedence::Assign { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1150 | rhs = parse_expr(input, rhs, allow_struct, next)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1151 | } else { |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | lhs = Expr::Assign(ExprAssign { |
| 1156 | attrs: Vec::new(), |
| 1157 | left: Box::new(lhs), |
| 1158 | eq_token: eq_token, |
| 1159 | right: Box::new(rhs), |
| 1160 | }); |
| 1161 | } else if Precedence::Placement >= base && input.peek(Token![<-]) { |
| 1162 | let arrow_token: Token![<-] = input.parse()?; |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1163 | let mut rhs = unary_expr(input, allow_struct)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1164 | loop { |
| 1165 | let next = peek_precedence(input); |
| 1166 | if next > Precedence::Placement { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1167 | rhs = parse_expr(input, rhs, allow_struct, next)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1168 | } else { |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
| 1172 | lhs = Expr::InPlace(ExprInPlace { |
| 1173 | attrs: Vec::new(), |
| 1174 | place: Box::new(lhs), |
| 1175 | arrow_token: arrow_token, |
| 1176 | value: Box::new(rhs), |
| 1177 | }); |
| 1178 | } else if Precedence::Range >= base && input.peek(Token![..]) { |
| 1179 | let limits: RangeLimits = input.parse()?; |
| 1180 | let rhs = if input.is_empty() |
| 1181 | || input.peek(Token![,]) |
| 1182 | || input.peek(Token![;]) |
| 1183 | || !allow_struct.0 && input.peek(token::Brace) |
| 1184 | { |
| 1185 | None |
| 1186 | } else { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1187 | let mut rhs = unary_expr(input, allow_struct)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1188 | loop { |
| 1189 | let next = peek_precedence(input); |
| 1190 | if next > Precedence::Range { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1191 | rhs = parse_expr(input, rhs, allow_struct, next)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1192 | } else { |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | Some(rhs) |
| 1197 | }; |
| 1198 | lhs = Expr::Range(ExprRange { |
| 1199 | attrs: Vec::new(), |
| 1200 | from: Some(Box::new(lhs)), |
| 1201 | limits: limits, |
| 1202 | to: rhs.map(Box::new), |
| 1203 | }); |
| 1204 | } else if Precedence::Cast >= base && input.peek(Token![as]) { |
| 1205 | let as_token: Token![as] = input.parse()?; |
| 1206 | let ty = input.call(Type::without_plus)?; |
| 1207 | lhs = Expr::Cast(ExprCast { |
| 1208 | attrs: Vec::new(), |
| 1209 | expr: Box::new(lhs), |
| 1210 | as_token: as_token, |
| 1211 | ty: Box::new(ty), |
| 1212 | }); |
| 1213 | } else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) { |
| 1214 | let colon_token: Token![:] = input.parse()?; |
| 1215 | let ty = input.call(Type::without_plus)?; |
| 1216 | lhs = Expr::Type(ExprType { |
| 1217 | attrs: Vec::new(), |
| 1218 | expr: Box::new(lhs), |
| 1219 | colon_token: colon_token, |
| 1220 | ty: Box::new(ty), |
| 1221 | }); |
| 1222 | } else { |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | Ok(lhs) |
| 1227 | } |
| 1228 | |
David Tolnay | 3e54129 | 2018-08-30 11:42:15 -0700 | [diff] [blame] | 1229 | #[cfg(not(feature = "full"))] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1230 | fn parse_expr( |
| 1231 | input: ParseStream, |
| 1232 | mut lhs: Expr, |
| 1233 | allow_struct: AllowStruct, |
| 1234 | base: Precedence, |
| 1235 | ) -> Result<Expr> { |
David Tolnay | 3e54129 | 2018-08-30 11:42:15 -0700 | [diff] [blame] | 1236 | loop { |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1237 | if input |
| 1238 | .fork() |
| 1239 | .parse::<BinOp>() |
| 1240 | .ok() |
| 1241 | .map_or(false, |op| Precedence::of(&op) >= base) |
| 1242 | { |
David Tolnay | 3e54129 | 2018-08-30 11:42:15 -0700 | [diff] [blame] | 1243 | let op: BinOp = input.parse()?; |
| 1244 | let precedence = Precedence::of(&op); |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1245 | let mut rhs = unary_expr(input, allow_struct)?; |
David Tolnay | 3e54129 | 2018-08-30 11:42:15 -0700 | [diff] [blame] | 1246 | loop { |
| 1247 | let next = peek_precedence(input); |
| 1248 | if next > precedence || next == precedence && precedence == Precedence::Assign { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1249 | rhs = parse_expr(input, rhs, allow_struct, next)?; |
David Tolnay | 3e54129 | 2018-08-30 11:42:15 -0700 | [diff] [blame] | 1250 | } else { |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | lhs = Expr::Binary(ExprBinary { |
| 1255 | attrs: Vec::new(), |
| 1256 | left: Box::new(lhs), |
| 1257 | op: op, |
| 1258 | right: Box::new(rhs), |
| 1259 | }); |
| 1260 | } else if Precedence::Cast >= base && input.peek(Token![as]) { |
| 1261 | let as_token: Token![as] = input.parse()?; |
| 1262 | let ty = input.call(Type::without_plus)?; |
| 1263 | lhs = Expr::Cast(ExprCast { |
| 1264 | attrs: Vec::new(), |
| 1265 | expr: Box::new(lhs), |
| 1266 | as_token: as_token, |
| 1267 | ty: Box::new(ty), |
| 1268 | }); |
| 1269 | } else { |
| 1270 | break; |
| 1271 | } |
| 1272 | } |
| 1273 | Ok(lhs) |
| 1274 | } |
| 1275 | |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1276 | fn peek_precedence(input: ParseStream) -> Precedence { |
| 1277 | if let Ok(op) = input.fork().parse() { |
| 1278 | Precedence::of(&op) |
David Tolnay | 3e54129 | 2018-08-30 11:42:15 -0700 | [diff] [blame] | 1279 | } else if input.peek(Token![=]) && !input.peek(Token![=>]) { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1280 | Precedence::Assign |
| 1281 | } else if input.peek(Token![<-]) { |
| 1282 | Precedence::Placement |
| 1283 | } else if input.peek(Token![..]) { |
| 1284 | Precedence::Range |
| 1285 | } else if input.peek(Token![as]) || input.peek(Token![:]) && !input.peek(Token![::]) { |
| 1286 | Precedence::Cast |
| 1287 | } else { |
| 1288 | Precedence::Any |
| 1289 | } |
| 1290 | } |
| 1291 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1292 | // Parse an arbitrary expression. |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1293 | fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1294 | let lhs = unary_expr(input, allow_struct)?; |
| 1295 | parse_expr(input, lhs, allow_struct, Precedence::Any) |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1296 | } |
| 1297 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1298 | // <UnOp> <trailer> |
| 1299 | // & <trailer> |
| 1300 | // &mut <trailer> |
| 1301 | // box <trailer> |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1302 | #[cfg(feature = "full")] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1303 | fn unary_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1304 | let ahead = input.fork(); |
| 1305 | ahead.call(Attribute::parse_outer)?; |
| 1306 | if ahead.peek(Token![&]) |
| 1307 | || ahead.peek(Token![box]) |
| 1308 | || ahead.peek(Token![*]) |
| 1309 | || ahead.peek(Token![!]) |
| 1310 | || ahead.peek(Token![-]) |
| 1311 | { |
| 1312 | let attrs = input.call(Attribute::parse_outer)?; |
| 1313 | if input.peek(Token![&]) { |
| 1314 | Ok(Expr::Reference(ExprReference { |
| 1315 | attrs: attrs, |
| 1316 | and_token: input.parse()?, |
| 1317 | mutability: input.parse()?, |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1318 | expr: Box::new(unary_expr(input, allow_struct)?), |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1319 | })) |
| 1320 | } else if input.peek(Token![box]) { |
| 1321 | Ok(Expr::Box(ExprBox { |
| 1322 | attrs: attrs, |
| 1323 | box_token: input.parse()?, |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1324 | expr: Box::new(unary_expr(input, allow_struct)?), |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1325 | })) |
| 1326 | } else { |
| 1327 | Ok(Expr::Unary(ExprUnary { |
| 1328 | attrs: attrs, |
| 1329 | op: input.parse()?, |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1330 | expr: Box::new(unary_expr(input, allow_struct)?), |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1331 | })) |
| 1332 | } |
| 1333 | } else { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1334 | trailer_expr(input, allow_struct) |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1335 | } |
| 1336 | } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1337 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1338 | // XXX: This duplication is ugly |
| 1339 | #[cfg(not(feature = "full"))] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1340 | fn unary_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1341 | let ahead = input.fork(); |
| 1342 | ahead.call(Attribute::parse_outer)?; |
| 1343 | if ahead.peek(Token![*]) || ahead.peek(Token![!]) || ahead.peek(Token![-]) { |
| 1344 | Ok(Expr::Unary(ExprUnary { |
| 1345 | attrs: input.call(Attribute::parse_outer)?, |
| 1346 | op: input.parse()?, |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1347 | expr: Box::new(unary_expr(input, allow_struct)?), |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1348 | })) |
| 1349 | } else { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1350 | trailer_expr(input, allow_struct) |
David Tolnay | 377263f | 2018-08-27 13:48:30 -0700 | [diff] [blame] | 1351 | } |
| 1352 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1353 | |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 1354 | #[cfg(feature = "full")] |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 1355 | fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> { |
| 1356 | let mut outer = Vec::new(); |
| 1357 | let mut inner = Vec::new(); |
| 1358 | for attr in mem::replace(attrs, Vec::new()) { |
| 1359 | match attr.style { |
| 1360 | AttrStyle::Outer => outer.push(attr), |
| 1361 | AttrStyle::Inner(_) => inner.push(attr), |
| 1362 | } |
| 1363 | } |
| 1364 | *attrs = inner; |
| 1365 | outer |
| 1366 | } |
| 1367 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1368 | // <atom> (..<args>) ... |
| 1369 | // <atom> . <ident> (..<args>) ... |
| 1370 | // <atom> . <ident> ... |
| 1371 | // <atom> . <lit> ... |
| 1372 | // <atom> [ <expr> ] ... |
| 1373 | // <atom> ? ... |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1374 | #[cfg(feature = "full")] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1375 | fn trailer_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1376 | let mut e = atom_expr(input, allow_struct)?; |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1377 | |
| 1378 | let mut attrs = e.replace_attrs(Vec::new()); |
| 1379 | let outer_attrs = take_outer(&mut attrs); |
| 1380 | e.replace_attrs(attrs); |
| 1381 | |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1382 | e = trailer_helper(input, e)?; |
| 1383 | |
| 1384 | let mut attrs = outer_attrs; |
| 1385 | attrs.extend(e.replace_attrs(Vec::new())); |
| 1386 | e.replace_attrs(attrs); |
| 1387 | Ok(e) |
| 1388 | } |
| 1389 | |
| 1390 | #[cfg(feature = "full")] |
| 1391 | fn trailer_helper(input: ParseStream, mut e: Expr) -> Result<Expr> { |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1392 | loop { |
| 1393 | if input.peek(token::Paren) { |
| 1394 | let content; |
| 1395 | e = Expr::Call(ExprCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1396 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1397 | func: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1398 | paren_token: parenthesized!(content in input), |
David Tolnay | 60484fe | 2018-08-30 18:43:04 -0700 | [diff] [blame] | 1399 | args: content.parse_terminated(Expr::parse)?, |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1400 | }); |
| 1401 | } else if input.peek(Token![.]) && !input.peek(Token![..]) { |
| 1402 | let dot_token: Token![.] = input.parse()?; |
| 1403 | let member: Member = input.parse()?; |
| 1404 | let turbofish = if member.is_named() && input.peek(Token![::]) { |
| 1405 | Some(MethodTurbofish { |
| 1406 | colon2_token: input.parse()?, |
| 1407 | lt_token: input.parse()?, |
| 1408 | args: { |
| 1409 | let mut args = Punctuated::new(); |
| 1410 | loop { |
| 1411 | if input.peek(Token![>]) { |
| 1412 | break; |
| 1413 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1414 | let value = input.call(generic_method_argument)?; |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1415 | args.push_value(value); |
| 1416 | if input.peek(Token![>]) { |
| 1417 | break; |
| 1418 | } |
| 1419 | let punct = input.parse()?; |
| 1420 | args.push_punct(punct); |
| 1421 | } |
| 1422 | args |
| 1423 | }, |
| 1424 | gt_token: input.parse()?, |
| 1425 | }) |
| 1426 | } else { |
| 1427 | None |
| 1428 | }; |
| 1429 | |
| 1430 | if turbofish.is_some() || input.peek(token::Paren) { |
| 1431 | if let Member::Named(method) = member { |
| 1432 | let content; |
| 1433 | e = Expr::MethodCall(ExprMethodCall { |
| 1434 | attrs: Vec::new(), |
| 1435 | receiver: Box::new(e), |
| 1436 | dot_token: dot_token, |
| 1437 | method: method, |
| 1438 | turbofish: turbofish, |
| 1439 | paren_token: parenthesized!(content in input), |
David Tolnay | 60484fe | 2018-08-30 18:43:04 -0700 | [diff] [blame] | 1440 | args: content.parse_terminated(Expr::parse)?, |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1441 | }); |
| 1442 | continue; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | e = Expr::Field(ExprField { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1447 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1448 | base: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1449 | dot_token: dot_token, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1450 | member: member, |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1451 | }); |
| 1452 | } else if input.peek(token::Bracket) { |
| 1453 | let content; |
| 1454 | e = Expr::Index(ExprIndex { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1455 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1456 | expr: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1457 | bracket_token: bracketed!(content in input), |
| 1458 | index: content.parse()?, |
| 1459 | }); |
| 1460 | } else if input.peek(Token![?]) { |
| 1461 | e = Expr::Try(ExprTry { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1462 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1463 | expr: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1464 | question_token: input.parse()?, |
| 1465 | }); |
| 1466 | } else { |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1470 | Ok(e) |
| 1471 | } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1472 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1473 | // XXX: Duplication == ugly |
| 1474 | #[cfg(not(feature = "full"))] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1475 | fn trailer_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1476 | let mut e = atom_expr(input, allow_struct)?; |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1477 | |
| 1478 | loop { |
| 1479 | if input.peek(token::Paren) { |
| 1480 | let content; |
| 1481 | e = Expr::Call(ExprCall { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1482 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1483 | func: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1484 | paren_token: parenthesized!(content in input), |
David Tolnay | 60484fe | 2018-08-30 18:43:04 -0700 | [diff] [blame] | 1485 | args: content.parse_terminated(Expr::parse)?, |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1486 | }); |
| 1487 | } else if input.peek(Token![.]) { |
| 1488 | e = Expr::Field(ExprField { |
David Tolnay | d514774 | 2018-06-30 10:09:52 -0700 | [diff] [blame] | 1489 | attrs: Vec::new(), |
| 1490 | base: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1491 | dot_token: input.parse()?, |
| 1492 | member: input.parse()?, |
| 1493 | }); |
| 1494 | } else if input.peek(token::Bracket) { |
| 1495 | let content; |
| 1496 | e = Expr::Index(ExprIndex { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1497 | attrs: Vec::new(), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 1498 | expr: Box::new(e), |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 1499 | bracket_token: bracketed!(content in input), |
| 1500 | index: content.parse()?, |
| 1501 | }); |
| 1502 | } else { |
| 1503 | break; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | Ok(e) |
| 1508 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1509 | |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 1510 | // Parse all atomic expressions which don't have to worry about precedence |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1511 | // interactions, as they are fully contained. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1512 | #[cfg(feature = "full")] |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1513 | fn atom_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1514 | if input.peek(token::Group) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1515 | return input.call(expr_group).map(Expr::Group); |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 1519 | |
| 1520 | let mut expr = if input.peek(token::Group) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1521 | Expr::Group(input.call(expr_group)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1522 | } else if input.peek(Lit) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1523 | Expr::Lit(input.call(expr_lit)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1524 | } else if input.peek(Token![async]) |
| 1525 | && (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace)) |
| 1526 | { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1527 | Expr::Async(input.call(expr_async)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1528 | } else if input.peek(Token![try]) && input.peek2(token::Brace) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1529 | Expr::TryBlock(input.call(expr_try_block)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1530 | } else if input.peek(Token![|]) |
| 1531 | || input.peek(Token![async]) && (input.peek2(Token![|]) || input.peek2(Token![move])) |
| 1532 | || input.peek(Token![static]) |
| 1533 | || input.peek(Token![move]) |
| 1534 | { |
| 1535 | Expr::Closure(expr_closure(input, allow_struct)?) |
| 1536 | } else if input.peek(Ident) |
| 1537 | || input.peek(Token![::]) |
| 1538 | || input.peek(Token![<]) |
| 1539 | || input.peek(Token![self]) |
| 1540 | || input.peek(Token![Self]) |
| 1541 | || input.peek(Token![super]) |
| 1542 | || input.peek(Token![extern]) |
| 1543 | || input.peek(Token![crate]) |
| 1544 | { |
| 1545 | path_or_macro_or_struct(input, allow_struct)? |
| 1546 | } else if input.peek(token::Paren) { |
| 1547 | paren_or_tuple(input)? |
| 1548 | } else if input.peek(Token![break]) { |
| 1549 | Expr::Break(expr_break(input, allow_struct)?) |
| 1550 | } else if input.peek(Token![continue]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1551 | Expr::Continue(input.call(expr_continue)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1552 | } else if input.peek(Token![return]) { |
| 1553 | Expr::Return(expr_ret(input, allow_struct)?) |
| 1554 | } else if input.peek(token::Bracket) { |
| 1555 | array_or_repeat(input)? |
| 1556 | } else if input.peek(Token![if]) { |
| 1557 | if input.peek2(Token![let]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1558 | Expr::IfLet(input.call(expr_if_let)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1559 | } else { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1560 | Expr::If(input.call(expr_if)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1561 | } |
| 1562 | } else if input.peek(Token![while]) { |
| 1563 | if input.peek2(Token![let]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1564 | Expr::WhileLet(input.call(expr_while_let)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1565 | } else { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1566 | Expr::While(input.call(expr_while)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } else if input.peek(Token![for]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1569 | Expr::ForLoop(input.call(expr_for_loop)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1570 | } else if input.peek(Token![loop]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1571 | Expr::Loop(input.call(expr_loop)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1572 | } else if input.peek(Token![match]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1573 | Expr::Match(input.call(expr_match)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1574 | } else if input.peek(Token![yield]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1575 | Expr::Yield(input.call(expr_yield)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1576 | } else if input.peek(Token![unsafe]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1577 | Expr::Unsafe(input.call(expr_unsafe)?) |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1578 | } else if input.peek(token::Brace) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1579 | Expr::Block(input.call(expr_block)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1580 | } else if input.peek(Token![..]) { |
| 1581 | Expr::Range(expr_range(input, allow_struct)?) |
| 1582 | } else if input.peek(Lifetime) { |
| 1583 | let the_label: Label = input.parse()?; |
| 1584 | let mut expr = if input.peek(Token![while]) { |
| 1585 | if input.peek2(Token![let]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1586 | Expr::WhileLet(input.call(expr_while_let)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1587 | } else { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1588 | Expr::While(input.call(expr_while)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1589 | } |
| 1590 | } else if input.peek(Token![for]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1591 | Expr::ForLoop(input.call(expr_for_loop)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1592 | } else if input.peek(Token![loop]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1593 | Expr::Loop(input.call(expr_loop)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1594 | } else if input.peek(token::Brace) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1595 | Expr::Block(input.call(expr_block)?) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1596 | } else { |
| 1597 | return Err(input.error("expected loop or block expression")); |
| 1598 | }; |
| 1599 | match expr { |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1600 | Expr::WhileLet(ExprWhileLet { ref mut label, .. }) |
| 1601 | | Expr::While(ExprWhile { ref mut label, .. }) |
| 1602 | | Expr::ForLoop(ExprForLoop { ref mut label, .. }) |
| 1603 | | Expr::Loop(ExprLoop { ref mut label, .. }) |
| 1604 | | Expr::Block(ExprBlock { ref mut label, .. }) => *label = Some(the_label), |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1605 | _ => unreachable!(), |
| 1606 | } |
| 1607 | expr |
| 1608 | } else { |
| 1609 | return Err(input.error("expected expression")); |
| 1610 | }; |
| 1611 | |
| 1612 | attrs.extend(expr.replace_attrs(Vec::new())); |
| 1613 | expr.replace_attrs(attrs); |
| 1614 | Ok(expr) |
| 1615 | } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1616 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1617 | #[cfg(not(feature = "full"))] |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1618 | fn atom_expr(input: ParseStream, _allow_struct: AllowStruct) -> Result<Expr> { |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1619 | if input.peek(Lit) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1620 | input.call(expr_lit).map(Expr::Lit) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1621 | } else if input.peek(token::Paren) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1622 | input.call(expr_paren).map(Expr::Paren) |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 1623 | } else if input.peek(Ident) |
| 1624 | || input.peek(Token![::]) |
| 1625 | || input.peek(Token![<]) |
| 1626 | || input.peek(Token![self]) |
| 1627 | || input.peek(Token![Self]) |
| 1628 | || input.peek(Token![super]) |
| 1629 | || input.peek(Token![extern]) |
| 1630 | || input.peek(Token![crate]) |
| 1631 | { |
| 1632 | input.parse().map(Expr::Path) |
| 1633 | } else { |
| 1634 | Err(input.error("unsupported expression; enable syn's features=[\"full\"]")) |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | #[cfg(feature = "full")] |
| 1639 | fn path_or_macro_or_struct(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> { |
| 1640 | let expr: ExprPath = input.parse()?; |
| 1641 | if expr.qself.is_some() { |
| 1642 | return Ok(Expr::Path(expr)); |
| 1643 | } |
| 1644 | |
| 1645 | if input.peek(Token![!]) && !input.peek(Token![!=]) { |
| 1646 | let mut contains_arguments = false; |
| 1647 | for segment in &expr.path.segments { |
| 1648 | match segment.arguments { |
| 1649 | PathArguments::None => {} |
| 1650 | PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => { |
| 1651 | contains_arguments = true; |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | if !contains_arguments { |
| 1657 | let bang_token: Token![!] = input.parse()?; |
| 1658 | let (delimiter, tts) = mac::parse_delimiter(input)?; |
| 1659 | return Ok(Expr::Macro(ExprMacro { |
| 1660 | attrs: Vec::new(), |
| 1661 | mac: Macro { |
| 1662 | path: expr.path, |
| 1663 | bang_token: bang_token, |
| 1664 | delimiter: delimiter, |
| 1665 | tts: tts, |
| 1666 | }, |
| 1667 | })); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | if allow_struct.0 && input.peek(token::Brace) { |
| 1672 | let outer_attrs = Vec::new(); |
| 1673 | expr_struct_helper(input, outer_attrs, expr.path).map(Expr::Struct) |
| 1674 | } else { |
| 1675 | Ok(Expr::Path(expr)) |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | #[cfg(feature = "full")] |
| 1680 | fn paren_or_tuple(input: ParseStream) -> Result<Expr> { |
| 1681 | let content; |
| 1682 | let paren_token = parenthesized!(content in input); |
| 1683 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 1684 | if content.is_empty() { |
| 1685 | return Ok(Expr::Tuple(ExprTuple { |
| 1686 | attrs: inner_attrs, |
| 1687 | paren_token: paren_token, |
| 1688 | elems: Punctuated::new(), |
| 1689 | })); |
| 1690 | } |
| 1691 | |
| 1692 | let first: Expr = content.parse()?; |
| 1693 | if content.is_empty() { |
| 1694 | return Ok(Expr::Paren(ExprParen { |
| 1695 | attrs: inner_attrs, |
| 1696 | paren_token: paren_token, |
| 1697 | expr: Box::new(first), |
| 1698 | })); |
| 1699 | } |
| 1700 | |
| 1701 | let mut elems = Punctuated::new(); |
| 1702 | elems.push_value(first); |
| 1703 | while !content.is_empty() { |
| 1704 | let punct = content.parse()?; |
| 1705 | elems.push_punct(punct); |
| 1706 | if content.is_empty() { |
| 1707 | break; |
| 1708 | } |
| 1709 | let value = content.parse()?; |
| 1710 | elems.push_value(value); |
| 1711 | } |
| 1712 | Ok(Expr::Tuple(ExprTuple { |
| 1713 | attrs: inner_attrs, |
| 1714 | paren_token: paren_token, |
| 1715 | elems: elems, |
| 1716 | })) |
| 1717 | } |
| 1718 | |
| 1719 | #[cfg(feature = "full")] |
| 1720 | fn array_or_repeat(input: ParseStream) -> Result<Expr> { |
| 1721 | let content; |
| 1722 | let bracket_token = bracketed!(content in input); |
| 1723 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 1724 | if content.is_empty() { |
| 1725 | return Ok(Expr::Array(ExprArray { |
| 1726 | attrs: inner_attrs, |
| 1727 | bracket_token: bracket_token, |
| 1728 | elems: Punctuated::new(), |
| 1729 | })); |
| 1730 | } |
| 1731 | |
| 1732 | let first: Expr = content.parse()?; |
| 1733 | if content.is_empty() || content.peek(Token![,]) { |
| 1734 | let mut elems = Punctuated::new(); |
| 1735 | elems.push_value(first); |
| 1736 | while !content.is_empty() { |
| 1737 | let punct = content.parse()?; |
| 1738 | elems.push_punct(punct); |
| 1739 | if content.is_empty() { |
| 1740 | break; |
| 1741 | } |
| 1742 | let value = content.parse()?; |
| 1743 | elems.push_value(value); |
| 1744 | } |
| 1745 | Ok(Expr::Array(ExprArray { |
| 1746 | attrs: inner_attrs, |
| 1747 | bracket_token: bracket_token, |
| 1748 | elems: elems, |
| 1749 | })) |
| 1750 | } else if content.peek(Token![;]) { |
| 1751 | let semi_token: Token![;] = content.parse()?; |
| 1752 | let len: Expr = content.parse()?; |
| 1753 | Ok(Expr::Repeat(ExprRepeat { |
| 1754 | attrs: inner_attrs, |
| 1755 | bracket_token: bracket_token, |
| 1756 | expr: Box::new(first), |
| 1757 | semi_token: semi_token, |
| 1758 | len: Box::new(len), |
| 1759 | })) |
| 1760 | } else { |
| 1761 | Err(content.error("expected `,` or `;`")) |
| 1762 | } |
| 1763 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1764 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1765 | #[cfg(feature = "full")] |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1766 | fn expr_early(input: ParseStream) -> Result<Expr> { |
| 1767 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 1768 | let mut expr = if input.peek(Token![if]) { |
| 1769 | if input.peek2(Token![let]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1770 | Expr::IfLet(input.call(expr_if_let)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1771 | } else { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1772 | Expr::If(input.call(expr_if)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1773 | } |
| 1774 | } else if input.peek(Token![while]) { |
| 1775 | if input.peek2(Token![let]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1776 | Expr::WhileLet(input.call(expr_while_let)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1777 | } else { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1778 | Expr::While(input.call(expr_while)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1779 | } |
| 1780 | } else if input.peek(Token![for]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1781 | Expr::ForLoop(input.call(expr_for_loop)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1782 | } else if input.peek(Token![loop]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1783 | Expr::Loop(input.call(expr_loop)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1784 | } else if input.peek(Token![match]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1785 | Expr::Match(input.call(expr_match)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1786 | } else if input.peek(Token![try]) && input.peek2(token::Brace) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1787 | Expr::TryBlock(input.call(expr_try_block)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1788 | } else if input.peek(Token![unsafe]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1789 | Expr::Unsafe(input.call(expr_unsafe)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1790 | } else if input.peek(token::Brace) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1791 | Expr::Block(input.call(expr_block)?) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1792 | } else { |
| 1793 | let allow_struct = AllowStruct(true); |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1794 | let mut expr = unary_expr(input, allow_struct)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1795 | |
| 1796 | attrs.extend(expr.replace_attrs(Vec::new())); |
| 1797 | expr.replace_attrs(attrs); |
| 1798 | |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1799 | return parse_expr(input, expr, allow_struct, Precedence::Any); |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1800 | }; |
| 1801 | |
| 1802 | if input.peek(Token![.]) || input.peek(Token![?]) { |
| 1803 | expr = trailer_helper(input, expr)?; |
| 1804 | |
| 1805 | attrs.extend(expr.replace_attrs(Vec::new())); |
| 1806 | expr.replace_attrs(attrs); |
| 1807 | |
| 1808 | let allow_struct = AllowStruct(true); |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 1809 | return parse_expr(input, expr, allow_struct, Precedence::Any); |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | attrs.extend(expr.replace_attrs(Vec::new())); |
| 1813 | expr.replace_attrs(attrs); |
| 1814 | Ok(expr) |
| 1815 | } |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1816 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1817 | pub fn expr_lit(input: ParseStream) -> Result<ExprLit> { |
| 1818 | Ok(ExprLit { |
| 1819 | attrs: Vec::new(), |
| 1820 | lit: input.parse()?, |
| 1821 | }) |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1825 | fn expr_group(input: ParseStream) -> Result<ExprGroup> { |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 1826 | let group = private::parse_group(input)?; |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1827 | Ok(ExprGroup { |
| 1828 | attrs: Vec::new(), |
David Tolnay | f57f76f | 2018-08-31 10:23:17 -0700 | [diff] [blame] | 1829 | group_token: group.token, |
| 1830 | expr: group.content.parse()?, |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1831 | }) |
| 1832 | } |
| 1833 | |
| 1834 | #[cfg(not(feature = "full"))] |
| 1835 | fn expr_paren(input: ParseStream) -> Result<ExprParen> { |
| 1836 | let content; |
| 1837 | Ok(ExprParen { |
| 1838 | attrs: Vec::new(), |
| 1839 | paren_token: parenthesized!(content in input), |
| 1840 | expr: content.parse()?, |
| 1841 | }) |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 1842 | } |
| 1843 | |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 1844 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1845 | fn generic_method_argument(input: ParseStream) -> Result<GenericMethodArgument> { |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 1846 | // TODO parse const generics as well |
David Tolnay | 8db2d66 | 2018-08-30 17:40:59 -0700 | [diff] [blame] | 1847 | input.parse().map(GenericMethodArgument::Type) |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1851 | fn expr_if_let(input: ParseStream) -> Result<ExprIfLet> { |
| 1852 | Ok(ExprIfLet { |
| 1853 | attrs: Vec::new(), |
| 1854 | if_token: input.parse()?, |
| 1855 | let_token: input.parse()?, |
| 1856 | pats: { |
| 1857 | let mut pats = Punctuated::new(); |
| 1858 | let value: Pat = input.parse()?; |
| 1859 | pats.push_value(value); |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 1860 | while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1861 | let punct = input.parse()?; |
| 1862 | pats.push_punct(punct); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1863 | let value: Pat = input.parse()?; |
| 1864 | pats.push_value(value); |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1865 | } |
| 1866 | pats |
| 1867 | }, |
| 1868 | eq_token: input.parse()?, |
| 1869 | expr: Box::new(input.call(expr_no_struct)?), |
| 1870 | then_branch: input.parse()?, |
| 1871 | else_branch: { |
| 1872 | if input.peek(Token![else]) { |
| 1873 | Some(input.call(else_block)?) |
| 1874 | } else { |
| 1875 | None |
| 1876 | } |
| 1877 | }, |
| 1878 | }) |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1881 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1882 | fn expr_if(input: ParseStream) -> Result<ExprIf> { |
| 1883 | Ok(ExprIf { |
| 1884 | attrs: Vec::new(), |
| 1885 | if_token: input.parse()?, |
| 1886 | cond: Box::new(input.call(expr_no_struct)?), |
| 1887 | then_branch: input.parse()?, |
| 1888 | else_branch: { |
| 1889 | if input.peek(Token![else]) { |
| 1890 | Some(input.call(else_block)?) |
| 1891 | } else { |
| 1892 | None |
| 1893 | } |
| 1894 | }, |
| 1895 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1896 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1897 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1898 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1899 | fn else_block(input: ParseStream) -> Result<(Token![else], Box<Expr>)> { |
| 1900 | let else_token: Token![else] = input.parse()?; |
| 1901 | |
| 1902 | let lookahead = input.lookahead1(); |
| 1903 | let else_branch = if input.peek(Token![if]) { |
| 1904 | if input.peek2(Token![let]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1905 | input.call(expr_if_let).map(Expr::IfLet)? |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1906 | } else { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1907 | input.call(expr_if).map(Expr::If)? |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1908 | } |
| 1909 | } else if input.peek(token::Brace) { |
| 1910 | Expr::Block(ExprBlock { |
| 1911 | attrs: Vec::new(), |
| 1912 | label: None, |
| 1913 | block: input.parse()?, |
| 1914 | }) |
| 1915 | } else { |
| 1916 | return Err(lookahead.error()); |
| 1917 | }; |
| 1918 | |
| 1919 | Ok((else_token, Box::new(else_branch))) |
| 1920 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1921 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1922 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1923 | fn expr_for_loop(input: ParseStream) -> Result<ExprForLoop> { |
| 1924 | let label: Option<Label> = input.parse()?; |
| 1925 | let for_token: Token![for] = input.parse()?; |
| 1926 | let pat: Pat = input.parse()?; |
| 1927 | let in_token: Token![in] = input.parse()?; |
| 1928 | let expr: Expr = input.call(expr_no_struct)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1929 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1930 | let content; |
| 1931 | let brace_token = braced!(content in input); |
| 1932 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 1933 | let stmts = content.call(Block::parse_within)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1934 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1935 | Ok(ExprForLoop { |
| 1936 | attrs: inner_attrs, |
| 1937 | label: label, |
| 1938 | for_token: for_token, |
| 1939 | pat: Box::new(pat), |
| 1940 | in_token: in_token, |
| 1941 | expr: Box::new(expr), |
| 1942 | body: Block { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 1943 | brace_token: brace_token, |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1944 | stmts: stmts, |
| 1945 | }, |
| 1946 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1947 | } |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1948 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1949 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1950 | fn expr_loop(input: ParseStream) -> Result<ExprLoop> { |
| 1951 | let label: Option<Label> = input.parse()?; |
| 1952 | let loop_token: Token![loop] = input.parse()?; |
| 1953 | |
| 1954 | let content; |
| 1955 | let brace_token = braced!(content in input); |
| 1956 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 1957 | let stmts = content.call(Block::parse_within)?; |
| 1958 | |
| 1959 | Ok(ExprLoop { |
| 1960 | attrs: inner_attrs, |
| 1961 | label: label, |
| 1962 | loop_token: loop_token, |
| 1963 | body: Block { |
| 1964 | brace_token: brace_token, |
| 1965 | stmts: stmts, |
| 1966 | }, |
| 1967 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1968 | } |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1969 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1970 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 1971 | fn expr_match(input: ParseStream) -> Result<ExprMatch> { |
| 1972 | let match_token: Token![match] = input.parse()?; |
| 1973 | let expr = expr_no_struct(input)?; |
| 1974 | |
| 1975 | let content; |
| 1976 | let brace_token = braced!(content in input); |
| 1977 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 1978 | |
| 1979 | let mut arms = Vec::new(); |
| 1980 | while !content.is_empty() { |
| 1981 | arms.push(content.call(match_arm)?); |
| 1982 | } |
| 1983 | |
| 1984 | Ok(ExprMatch { |
| 1985 | attrs: inner_attrs, |
| 1986 | match_token: match_token, |
| 1987 | expr: Box::new(expr), |
| 1988 | brace_token: brace_token, |
| 1989 | arms: arms, |
| 1990 | }) |
| 1991 | } |
| 1992 | |
| 1993 | #[cfg(feature = "full")] |
| 1994 | fn expr_try_block(input: ParseStream) -> Result<ExprTryBlock> { |
| 1995 | Ok(ExprTryBlock { |
| 1996 | attrs: Vec::new(), |
| 1997 | try_token: input.parse()?, |
| 1998 | block: input.parse()?, |
| 1999 | }) |
| 2000 | } |
| 2001 | |
| 2002 | #[cfg(feature = "full")] |
| 2003 | fn expr_yield(input: ParseStream) -> Result<ExprYield> { |
| 2004 | Ok(ExprYield { |
| 2005 | attrs: Vec::new(), |
| 2006 | yield_token: input.parse()?, |
| 2007 | expr: { |
| 2008 | if !input.is_empty() && !input.peek(Token![,]) && !input.peek(Token![;]) { |
| 2009 | Some(input.parse()?) |
| 2010 | } else { |
| 2011 | None |
| 2012 | } |
| 2013 | }, |
| 2014 | }) |
| 2015 | } |
| 2016 | |
| 2017 | #[cfg(feature = "full")] |
| 2018 | fn match_arm(input: ParseStream) -> Result<Arm> { |
| 2019 | let requires_comma; |
| 2020 | Ok(Arm { |
| 2021 | attrs: input.call(Attribute::parse_outer)?, |
| 2022 | leading_vert: input.parse()?, |
| 2023 | pats: { |
| 2024 | let mut pats = Punctuated::new(); |
| 2025 | let value: Pat = input.parse()?; |
| 2026 | pats.push_value(value); |
| 2027 | loop { |
| 2028 | if !input.peek(Token![|]) { |
| 2029 | break; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2030 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2031 | let punct = input.parse()?; |
| 2032 | pats.push_punct(punct); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2033 | let value: Pat = input.parse()?; |
| 2034 | pats.push_value(value); |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2035 | } |
| 2036 | pats |
| 2037 | }, |
| 2038 | guard: { |
| 2039 | if input.peek(Token![if]) { |
| 2040 | let if_token: Token![if] = input.parse()?; |
| 2041 | let guard: Expr = input.parse()?; |
| 2042 | Some((if_token, Box::new(guard))) |
| 2043 | } else { |
| 2044 | None |
| 2045 | } |
| 2046 | }, |
| 2047 | fat_arrow_token: input.parse()?, |
| 2048 | body: { |
| 2049 | let body = input.call(expr_early)?; |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2050 | requires_comma = requires_terminator(&body); |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2051 | Box::new(body) |
| 2052 | }, |
| 2053 | comma: { |
| 2054 | if requires_comma && !input.is_empty() { |
| 2055 | Some(input.parse()?) |
| 2056 | } else { |
| 2057 | input.parse()? |
| 2058 | } |
| 2059 | }, |
| 2060 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2061 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2062 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2063 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2064 | fn expr_closure(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprClosure> { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2065 | let asyncness: Option<Token![async]> = input.parse()?; |
| 2066 | let movability: Option<Token![static]> = if asyncness.is_none() { |
| 2067 | input.parse()? |
| 2068 | } else { |
| 2069 | None |
| 2070 | }; |
| 2071 | let capture: Option<Token![move]> = input.parse()?; |
| 2072 | let or1_token: Token![|] = input.parse()?; |
| 2073 | |
| 2074 | let mut inputs = Punctuated::new(); |
| 2075 | loop { |
| 2076 | if input.peek(Token![|]) { |
| 2077 | break; |
| 2078 | } |
| 2079 | let value = fn_arg(input)?; |
| 2080 | inputs.push_value(value); |
| 2081 | if input.peek(Token![|]) { |
| 2082 | break; |
| 2083 | } |
| 2084 | let punct: Token![,] = input.parse()?; |
| 2085 | inputs.push_punct(punct); |
| 2086 | } |
| 2087 | |
| 2088 | let or2_token: Token![|] = input.parse()?; |
| 2089 | |
| 2090 | let (output, body) = if input.peek(Token![->]) { |
| 2091 | let arrow_token: Token![->] = input.parse()?; |
| 2092 | let ty: Type = input.parse()?; |
| 2093 | let body: Block = input.parse()?; |
| 2094 | let output = ReturnType::Type(arrow_token, Box::new(ty)); |
| 2095 | let block = Expr::Block(ExprBlock { |
| 2096 | attrs: Vec::new(), |
| 2097 | label: None, |
| 2098 | block: body, |
| 2099 | }); |
| 2100 | (output, block) |
| 2101 | } else { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 2102 | let body = ambiguous_expr(input, allow_struct)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2103 | (ReturnType::Default, body) |
| 2104 | }; |
| 2105 | |
| 2106 | Ok(ExprClosure { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2107 | attrs: Vec::new(), |
Yusuke Sasaki | 4e5d966 | 2018-07-21 02:49:47 +0900 | [diff] [blame] | 2108 | asyncness: asyncness, |
| 2109 | movability: movability, |
| 2110 | capture: capture, |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2111 | or1_token: or1_token, |
Yusuke Sasaki | 4e5d966 | 2018-07-21 02:49:47 +0900 | [diff] [blame] | 2112 | inputs: inputs, |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2113 | or2_token: or2_token, |
| 2114 | output: output, |
| 2115 | body: Box::new(body), |
| 2116 | }) |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 2117 | } |
Yusuke Sasaki | 851062f | 2018-08-01 06:28:28 +0900 | [diff] [blame] | 2118 | |
| 2119 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2120 | fn expr_async(input: ParseStream) -> Result<ExprAsync> { |
| 2121 | Ok(ExprAsync { |
| 2122 | attrs: Vec::new(), |
| 2123 | async_token: input.parse()?, |
| 2124 | capture: input.parse()?, |
| 2125 | block: input.parse()?, |
| 2126 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2127 | } |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 2128 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2129 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2130 | fn fn_arg(input: ParseStream) -> Result<FnArg> { |
| 2131 | let pat: Pat = input.parse()?; |
| 2132 | |
| 2133 | if input.peek(Token![:]) { |
| 2134 | Ok(FnArg::Captured(ArgCaptured { |
| 2135 | pat: pat, |
| 2136 | colon_token: input.parse()?, |
| 2137 | ty: input.parse()?, |
| 2138 | })) |
| 2139 | } else { |
| 2140 | Ok(FnArg::Inferred(pat)) |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2145 | fn expr_while(input: ParseStream) -> Result<ExprWhile> { |
| 2146 | let label: Option<Label> = input.parse()?; |
| 2147 | let while_token: Token![while] = input.parse()?; |
| 2148 | let cond = expr_no_struct(input)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2149 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2150 | let content; |
| 2151 | let brace_token = braced!(content in input); |
| 2152 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 2153 | let stmts = content.call(Block::parse_within)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2154 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2155 | Ok(ExprWhile { |
| 2156 | attrs: inner_attrs, |
| 2157 | label: label, |
| 2158 | while_token: while_token, |
| 2159 | cond: Box::new(cond), |
| 2160 | body: Block { |
| 2161 | brace_token: brace_token, |
| 2162 | stmts: stmts, |
| 2163 | }, |
| 2164 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2167 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2168 | fn expr_while_let(input: ParseStream) -> Result<ExprWhileLet> { |
| 2169 | let label: Option<Label> = input.parse()?; |
| 2170 | let while_token: Token![while] = input.parse()?; |
| 2171 | let let_token: Token![let] = input.parse()?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2172 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2173 | let mut pats = Punctuated::new(); |
| 2174 | let value: Pat = input.parse()?; |
| 2175 | pats.push_value(value); |
| 2176 | while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) { |
| 2177 | let punct = input.parse()?; |
| 2178 | pats.push_punct(punct); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2179 | let value: Pat = input.parse()?; |
| 2180 | pats.push_value(value); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2181 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2182 | |
| 2183 | let eq_token: Token![=] = input.parse()?; |
| 2184 | let expr = expr_no_struct(input)?; |
| 2185 | |
| 2186 | let content; |
| 2187 | let brace_token = braced!(content in input); |
| 2188 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 2189 | let stmts = content.call(Block::parse_within)?; |
| 2190 | |
| 2191 | Ok(ExprWhileLet { |
| 2192 | attrs: inner_attrs, |
| 2193 | label: label, |
| 2194 | while_token: while_token, |
| 2195 | let_token: let_token, |
| 2196 | pats: pats, |
| 2197 | eq_token: eq_token, |
| 2198 | expr: Box::new(expr), |
| 2199 | body: Block { |
| 2200 | brace_token: brace_token, |
| 2201 | stmts: stmts, |
| 2202 | }, |
| 2203 | }) |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2207 | impl Parse for Label { |
| 2208 | fn parse(input: ParseStream) -> Result<Self> { |
| 2209 | Ok(Label { |
| 2210 | name: input.parse()?, |
| 2211 | colon_token: input.parse()?, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2212 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2213 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2216 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2217 | impl Parse for Option<Label> { |
| 2218 | fn parse(input: ParseStream) -> Result<Self> { |
| 2219 | if input.peek(Lifetime) { |
| 2220 | input.parse().map(Some) |
| 2221 | } else { |
| 2222 | Ok(None) |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2228 | fn expr_continue(input: ParseStream) -> Result<ExprContinue> { |
| 2229 | Ok(ExprContinue { |
| 2230 | attrs: Vec::new(), |
| 2231 | continue_token: input.parse()?, |
| 2232 | label: input.parse()?, |
| 2233 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2234 | } |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 2235 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2236 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2237 | fn expr_break(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprBreak> { |
| 2238 | Ok(ExprBreak { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2239 | attrs: Vec::new(), |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2240 | break_token: input.parse()?, |
| 2241 | label: input.parse()?, |
| 2242 | expr: { |
| 2243 | if input.is_empty() |
| 2244 | || input.peek(Token![,]) |
| 2245 | || input.peek(Token![;]) |
| 2246 | || !allow_struct.0 && input.peek(token::Brace) |
| 2247 | { |
| 2248 | None |
| 2249 | } else { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 2250 | let expr = ambiguous_expr(input, allow_struct)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2251 | Some(Box::new(expr)) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2252 | } |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2253 | }, |
| 2254 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2257 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2258 | fn expr_ret(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprReturn> { |
| 2259 | Ok(ExprReturn { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2260 | attrs: Vec::new(), |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2261 | return_token: input.parse()?, |
| 2262 | expr: { |
| 2263 | if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) { |
| 2264 | None |
| 2265 | } else { |
| 2266 | // NOTE: return is greedy and eats blocks after it even when in a |
| 2267 | // position where structs are not allowed, such as in if statement |
| 2268 | // conditions. For example: |
| 2269 | // |
| 2270 | // if return { println!("A") } {} // Prints "A" |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 2271 | let expr = ambiguous_expr(input, allow_struct)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2272 | Some(Box::new(expr)) |
| 2273 | } |
| 2274 | }, |
| 2275 | }) |
| 2276 | } |
| 2277 | |
| 2278 | #[cfg(feature = "full")] |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 2279 | fn expr_struct_helper( |
| 2280 | input: ParseStream, |
| 2281 | outer_attrs: Vec<Attribute>, |
| 2282 | path: Path, |
| 2283 | ) -> Result<ExprStruct> { |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2284 | let content; |
| 2285 | let brace_token = braced!(content in input); |
| 2286 | let inner_attrs = content.call(Attribute::parse_inner)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2287 | |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2288 | let mut fields = Punctuated::new(); |
| 2289 | loop { |
| 2290 | let attrs = content.call(Attribute::parse_outer)?; |
| 2291 | if content.fork().parse::<Member>().is_err() { |
| 2292 | if attrs.is_empty() { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2293 | break; |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2294 | } else { |
| 2295 | return Err(content.error("expected struct field")); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2296 | } |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2299 | let member: Member = content.parse()?; |
| 2300 | let (colon_token, value) = if content.peek(Token![:]) || !member.is_named() { |
| 2301 | let colon_token: Token![:] = content.parse()?; |
| 2302 | let value: Expr = content.parse()?; |
| 2303 | (Some(colon_token), value) |
| 2304 | } else if let Member::Named(ref ident) = member { |
| 2305 | let value = Expr::Path(ExprPath { |
| 2306 | attrs: Vec::new(), |
| 2307 | qself: None, |
| 2308 | path: Path::from(ident.clone()), |
| 2309 | }); |
| 2310 | (None, value) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2311 | } else { |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2312 | unreachable!() |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2313 | }; |
| 2314 | |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2315 | fields.push(FieldValue { |
| 2316 | attrs: attrs, |
| 2317 | member: member, |
| 2318 | colon_token: colon_token, |
| 2319 | expr: value, |
| 2320 | }); |
| 2321 | |
| 2322 | if !content.peek(Token![,]) { |
| 2323 | break; |
| 2324 | } |
| 2325 | let punct: Token![,] = content.parse()?; |
| 2326 | fields.push_punct(punct); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2327 | } |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2328 | |
| 2329 | let (dot2_token, rest) = if fields.empty_or_trailing() && content.peek(Token![..]) { |
| 2330 | let dot2_token: Token![..] = content.parse()?; |
| 2331 | let rest: Expr = content.parse()?; |
| 2332 | (Some(dot2_token), Some(Box::new(rest))) |
| 2333 | } else { |
| 2334 | (None, None) |
| 2335 | }; |
| 2336 | |
| 2337 | Ok(ExprStruct { |
David Tolnay | b5f6fc0 | 2018-09-01 02:18:50 -0700 | [diff] [blame^] | 2338 | attrs: private::attrs(outer_attrs, inner_attrs), |
David Tolnay | 6e1e505 | 2018-08-30 10:21:48 -0700 | [diff] [blame] | 2339 | brace_token: brace_token, |
| 2340 | path: path, |
| 2341 | fields: fields, |
| 2342 | dot2_token: dot2_token, |
| 2343 | rest: rest, |
| 2344 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2345 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2346 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2347 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2348 | fn expr_unsafe(input: ParseStream) -> Result<ExprUnsafe> { |
| 2349 | let unsafe_token: Token![unsafe] = input.parse()?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2350 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2351 | let content; |
| 2352 | let brace_token = braced!(content in input); |
| 2353 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 2354 | let stmts = content.call(Block::parse_within)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2355 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2356 | Ok(ExprUnsafe { |
| 2357 | attrs: inner_attrs, |
| 2358 | unsafe_token: unsafe_token, |
| 2359 | block: Block { |
| 2360 | brace_token: brace_token, |
| 2361 | stmts: stmts, |
| 2362 | }, |
| 2363 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2364 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2365 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2366 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2367 | pub fn expr_block(input: ParseStream) -> Result<ExprBlock> { |
| 2368 | let label: Option<Label> = input.parse()?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2369 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2370 | let content; |
| 2371 | let brace_token = braced!(content in input); |
| 2372 | let inner_attrs = content.call(Attribute::parse_inner)?; |
| 2373 | let stmts = content.call(Block::parse_within)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2374 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2375 | Ok(ExprBlock { |
| 2376 | attrs: inner_attrs, |
| 2377 | label: label, |
| 2378 | block: Block { |
| 2379 | brace_token: brace_token, |
| 2380 | stmts: stmts, |
| 2381 | }, |
| 2382 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2383 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2384 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2385 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2386 | fn expr_range(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprRange> { |
| 2387 | Ok(ExprRange { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2388 | attrs: Vec::new(), |
| 2389 | from: None, |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2390 | limits: input.parse()?, |
| 2391 | to: { |
| 2392 | if input.is_empty() |
| 2393 | || input.peek(Token![,]) |
| 2394 | || input.peek(Token![;]) |
| 2395 | || !allow_struct.0 && input.peek(token::Brace) |
| 2396 | { |
| 2397 | None |
| 2398 | } else { |
David Tolnay | 7d2e1db | 2018-08-30 11:49:04 -0700 | [diff] [blame] | 2399 | let to = ambiguous_expr(input, allow_struct)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2400 | Some(Box::new(to)) |
| 2401 | } |
| 2402 | }, |
| 2403 | }) |
| 2404 | } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 2405 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2406 | #[cfg(feature = "full")] |
David Tolnay | c4c631e | 2018-08-27 10:44:37 -0700 | [diff] [blame] | 2407 | impl Parse for RangeLimits { |
| 2408 | fn parse(input: ParseStream) -> Result<Self> { |
| 2409 | let lookahead = input.lookahead1(); |
| 2410 | if lookahead.peek(Token![..=]) { |
| 2411 | input.parse().map(RangeLimits::Closed) |
| 2412 | } else if lookahead.peek(Token![...]) { |
| 2413 | let dot3: Token![...] = input.parse()?; |
| 2414 | Ok(RangeLimits::Closed(Token)) |
| 2415 | } else if lookahead.peek(Token![..]) { |
| 2416 | input.parse().map(RangeLimits::HalfOpen) |
| 2417 | } else { |
| 2418 | Err(lookahead.error()) |
| 2419 | } |
| 2420 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2421 | } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 2422 | |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2423 | impl Parse for ExprPath { |
| 2424 | fn parse(input: ParseStream) -> Result<Self> { |
| 2425 | #[cfg(not(feature = "full"))] |
| 2426 | let attrs = Vec::new(); |
| 2427 | #[cfg(feature = "full")] |
| 2428 | let attrs = input.call(Attribute::parse_outer)?; |
David Tolnay | eb981bb | 2018-07-21 19:31:38 -0700 | [diff] [blame] | 2429 | |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2430 | let (qself, path) = path::parsing::qpath(input, true)?; |
| 2431 | |
| 2432 | Ok(ExprPath { |
David Tolnay | 73c9b52 | 2018-07-21 15:58:40 -0700 | [diff] [blame] | 2433 | attrs: attrs, |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2434 | qself: qself, |
| 2435 | path: path, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2436 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2437 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2438 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2439 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2440 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2441 | impl Parse for Block { |
| 2442 | fn parse(input: ParseStream) -> Result<Self> { |
| 2443 | let content; |
| 2444 | Ok(Block { |
| 2445 | brace_token: braced!(content in input), |
| 2446 | stmts: content.call(Block::parse_within)?, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2447 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2448 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2449 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 2450 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2451 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2452 | impl Block { |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 2453 | pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> { |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2454 | let mut stmts = Vec::new(); |
David Tolnay | 7158c5f | 2018-08-30 17:28:34 -0700 | [diff] [blame] | 2455 | loop { |
| 2456 | while input.peek(Token![;]) { |
| 2457 | input.parse::<Token![;]>()?; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2458 | } |
David Tolnay | 7158c5f | 2018-08-30 17:28:34 -0700 | [diff] [blame] | 2459 | if input.is_empty() { |
| 2460 | break; |
| 2461 | } |
| 2462 | let s = parse_stmt(input, true)?; |
| 2463 | let requires_semicolon = if let Stmt::Expr(ref s) = s { |
| 2464 | requires_terminator(s) |
| 2465 | } else { |
| 2466 | false |
| 2467 | }; |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2468 | stmts.push(s); |
David Tolnay | 7158c5f | 2018-08-30 17:28:34 -0700 | [diff] [blame] | 2469 | if input.is_empty() { |
| 2470 | break; |
| 2471 | } else if requires_semicolon { |
| 2472 | return Err(input.error("unexpected token")); |
| 2473 | } |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2474 | } |
| 2475 | Ok(stmts) |
| 2476 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2479 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2480 | impl Parse for Stmt { |
| 2481 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2482 | parse_stmt(input, false) |
| 2483 | } |
| 2484 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 2485 | |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2486 | #[cfg(feature = "full")] |
| 2487 | fn parse_stmt(input: ParseStream, allow_nosemi: bool) -> Result<Stmt> { |
| 2488 | let ahead = input.fork(); |
| 2489 | ahead.call(Attribute::parse_outer)?; |
| 2490 | |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2491 | if { |
| 2492 | let ahead = ahead.fork(); |
| 2493 | // Only parse braces here; paren and bracket will get parsed as |
| 2494 | // expression statements |
| 2495 | ahead.call(Path::parse_mod_style).is_ok() |
| 2496 | && ahead.parse::<Token![!]>().is_ok() |
| 2497 | && (ahead.peek(token::Brace) || ahead.peek(Ident)) |
| 2498 | } { |
| 2499 | stmt_mac(input) |
| 2500 | } else if ahead.peek(Token![let]) { |
| 2501 | stmt_local(input).map(Stmt::Local) |
| 2502 | } else if ahead.peek(Token![pub]) |
| 2503 | || ahead.peek(Token![crate]) && !ahead.peek2(Token![::]) |
| 2504 | || ahead.peek(Token![extern]) && !ahead.peek2(Token![::]) |
| 2505 | || ahead.peek(Token![use]) |
| 2506 | || ahead.peek(Token![static]) && (ahead.peek2(Token![mut]) || ahead.peek2(Ident)) |
| 2507 | || ahead.peek(Token![const]) |
| 2508 | || ahead.peek(Token![unsafe]) && !ahead.peek2(token::Brace) |
| 2509 | || ahead.peek(Token![async]) && (ahead.peek2(Token![extern]) || ahead.peek2(Token![fn])) |
| 2510 | || ahead.peek(Token![fn]) |
| 2511 | || ahead.peek(Token![mod]) |
| 2512 | || ahead.peek(Token![type]) |
| 2513 | || ahead.peek(Token![existential]) && ahead.peek2(Token![type]) |
| 2514 | || ahead.peek(Token![struct]) |
| 2515 | || ahead.peek(Token![enum]) |
| 2516 | || ahead.peek(Token![union]) && ahead.peek2(Ident) |
| 2517 | || ahead.peek(Token![auto]) && ahead.peek2(Token![trait]) |
| 2518 | || ahead.peek(Token![trait]) |
David Tolnay | 73b7ca1 | 2018-08-30 21:05:13 -0700 | [diff] [blame] | 2519 | || ahead.peek(Token![default]) |
| 2520 | && (ahead.peek2(Token![unsafe]) || ahead.peek2(Token![impl ])) |
| 2521 | || ahead.peek(Token![impl ]) |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2522 | || ahead.peek(Token![macro]) |
| 2523 | { |
| 2524 | input.parse().map(Stmt::Item) |
| 2525 | } else { |
| 2526 | stmt_expr(input, allow_nosemi) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2527 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2528 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2529 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2530 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2531 | fn stmt_mac(input: ParseStream) -> Result<Stmt> { |
| 2532 | let attrs = input.call(Attribute::parse_outer)?; |
| 2533 | let path = input.call(Path::parse_mod_style)?; |
| 2534 | let bang_token: Token![!] = input.parse()?; |
| 2535 | let ident: Option<Ident> = input.parse()?; |
| 2536 | let (delimiter, tts) = mac::parse_delimiter(input)?; |
| 2537 | let semi_token: Option<Token![;]> = input.parse()?; |
| 2538 | |
| 2539 | Ok(Stmt::Item(Item::Macro(ItemMacro { |
| 2540 | attrs: attrs, |
| 2541 | ident: ident, |
| 2542 | mac: Macro { |
| 2543 | path: path, |
| 2544 | bang_token: bang_token, |
| 2545 | delimiter: delimiter, |
| 2546 | tts: tts, |
| 2547 | }, |
| 2548 | semi_token: semi_token, |
| 2549 | }))) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2550 | } |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 2551 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2552 | #[cfg(feature = "full")] |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2553 | fn stmt_local(input: ParseStream) -> Result<Local> { |
| 2554 | Ok(Local { |
| 2555 | attrs: input.call(Attribute::parse_outer)?, |
| 2556 | let_token: input.parse()?, |
| 2557 | pats: { |
| 2558 | let mut pats = Punctuated::new(); |
| 2559 | let value: Pat = input.parse()?; |
| 2560 | pats.push_value(value); |
| 2561 | while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) { |
| 2562 | let punct = input.parse()?; |
| 2563 | pats.push_punct(punct); |
| 2564 | let value: Pat = input.parse()?; |
| 2565 | pats.push_value(value); |
| 2566 | } |
| 2567 | pats |
| 2568 | }, |
| 2569 | ty: { |
| 2570 | if input.peek(Token![:]) { |
| 2571 | let colon_token: Token![:] = input.parse()?; |
| 2572 | let ty: Type = input.parse()?; |
| 2573 | Some((colon_token, Box::new(ty))) |
| 2574 | } else { |
| 2575 | None |
| 2576 | } |
| 2577 | }, |
| 2578 | init: { |
| 2579 | if input.peek(Token![=]) { |
| 2580 | let eq_token: Token![=] = input.parse()?; |
| 2581 | let init: Expr = input.parse()?; |
| 2582 | Some((eq_token, Box::new(init))) |
| 2583 | } else { |
| 2584 | None |
| 2585 | } |
| 2586 | }, |
| 2587 | semi_token: input.parse()?, |
| 2588 | }) |
| 2589 | } |
| 2590 | |
| 2591 | #[cfg(feature = "full")] |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2592 | fn stmt_expr(input: ParseStream, allow_nosemi: bool) -> Result<Stmt> { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2593 | let mut attrs = input.call(Attribute::parse_outer)?; |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 2594 | let mut e = expr_early(input)?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2595 | |
| 2596 | attrs.extend(e.replace_attrs(Vec::new())); |
| 2597 | e.replace_attrs(attrs); |
| 2598 | |
| 2599 | if input.peek(Token![;]) { |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 2600 | return Ok(Stmt::Semi(e, input.parse()?)); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2601 | } |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2602 | |
David Tolnay | f00a276 | 2018-08-30 17:22:22 -0700 | [diff] [blame] | 2603 | if allow_nosemi || !requires_terminator(&e) { |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 2604 | Ok(Stmt::Expr(e)) |
| 2605 | } else { |
| 2606 | Err(input.error("expected semicolon")) |
David Tolnay | 01218d1 | 2018-08-29 18:13:07 -0700 | [diff] [blame] | 2607 | } |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | #[cfg(feature = "full")] |
| 2611 | impl Parse for Pat { |
| 2612 | fn parse(input: ParseStream) -> Result<Self> { |
| 2613 | // TODO: better error messages |
| 2614 | let lookahead = input.lookahead1(); |
| 2615 | if lookahead.peek(Token![_]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2616 | input.call(pat_wild).map(Pat::Wild) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2617 | } else if lookahead.peek(Token![box]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2618 | input.call(pat_box).map(Pat::Box) |
| 2619 | } else if input.fork().call(pat_range).is_ok() { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2620 | // must be before Pat::Lit |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2621 | input.call(pat_range).map(Pat::Range) |
| 2622 | } else if input.fork().call(pat_tuple_struct).is_ok() { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2623 | // must be before Pat::Ident |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2624 | input.call(pat_tuple_struct).map(Pat::TupleStruct) |
| 2625 | } else if input.fork().call(pat_struct).is_ok() { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2626 | // must be before Pat::Ident |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2627 | input.call(pat_struct).map(Pat::Struct) |
| 2628 | } else if input.fork().call(pat_macro).is_ok() { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2629 | // must be before Pat::Ident |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2630 | input.call(pat_macro).map(Pat::Macro) |
| 2631 | } else if input.fork().call(pat_lit).is_ok() { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2632 | // must be before Pat::Ident |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2633 | input.call(pat_lit).map(Pat::Lit) |
| 2634 | } else if input.fork().call(pat_ident).is_ok() { |
| 2635 | input.call(pat_ident).map(Pat::Ident) |
| 2636 | } else if input.fork().call(pat_path).is_ok() { |
| 2637 | input.call(pat_path).map(Pat::Path) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2638 | } else if lookahead.peek(token::Paren) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2639 | input.call(pat_tuple).map(Pat::Tuple) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2640 | } else if lookahead.peek(Token![&]) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2641 | input.call(pat_ref).map(Pat::Ref) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2642 | } else if lookahead.peek(token::Bracket) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2643 | input.call(pat_slice).map(Pat::Slice) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2644 | } else { |
| 2645 | Err(lookahead.error()) |
| 2646 | } |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2651 | fn pat_wild(input: ParseStream) -> Result<PatWild> { |
| 2652 | Ok(PatWild { |
| 2653 | underscore_token: input.parse()?, |
| 2654 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2657 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2658 | fn pat_box(input: ParseStream) -> Result<PatBox> { |
| 2659 | Ok(PatBox { |
| 2660 | box_token: input.parse()?, |
| 2661 | pat: input.parse()?, |
| 2662 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2666 | fn pat_ident(input: ParseStream) -> Result<PatIdent> { |
| 2667 | Ok(PatIdent { |
| 2668 | by_ref: input.parse()?, |
| 2669 | mutability: input.parse()?, |
| 2670 | ident: { |
| 2671 | let ident = if input.peek(Ident) || input.peek(Token![self]) { |
David Tolnay | 0dea1b9 | 2018-08-30 17:47:29 -0700 | [diff] [blame] | 2672 | input.call(Ident::parse_any)? |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2673 | } else { |
| 2674 | return Err(input.error("expected identifier or `self`")); |
| 2675 | }; |
| 2676 | if input.peek(Token![<]) || input.peek(Token![::]) { |
| 2677 | return Err(input.error("unexpected token")); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2678 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2679 | ident |
| 2680 | }, |
| 2681 | subpat: { |
| 2682 | if input.peek(Token![@]) { |
| 2683 | let at_token: Token![@] = input.parse()?; |
| 2684 | let subpat: Pat = input.parse()?; |
| 2685 | Some((at_token, Box::new(subpat))) |
| 2686 | } else { |
| 2687 | None |
| 2688 | } |
| 2689 | }, |
| 2690 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2694 | fn pat_tuple_struct(input: ParseStream) -> Result<PatTupleStruct> { |
| 2695 | Ok(PatTupleStruct { |
| 2696 | path: input.parse()?, |
| 2697 | pat: input.call(pat_tuple)?, |
| 2698 | }) |
| 2699 | } |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2700 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2701 | #[cfg(feature = "full")] |
| 2702 | fn pat_struct(input: ParseStream) -> Result<PatStruct> { |
| 2703 | let path: Path = input.parse()?; |
| 2704 | |
| 2705 | let content; |
| 2706 | let brace_token = braced!(content in input); |
| 2707 | |
| 2708 | let mut fields = Punctuated::new(); |
| 2709 | while !content.is_empty() && !content.peek(Token![..]) { |
| 2710 | let value = content.call(field_pat)?; |
| 2711 | fields.push_value(value); |
| 2712 | if !content.peek(Token![,]) { |
| 2713 | break; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2714 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2715 | let punct: Token![,] = content.parse()?; |
| 2716 | fields.push_punct(punct); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2717 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2718 | |
| 2719 | let dot2_token = if fields.empty_or_trailing() && content.peek(Token![..]) { |
| 2720 | Some(content.parse()?) |
| 2721 | } else { |
| 2722 | None |
| 2723 | }; |
| 2724 | |
| 2725 | Ok(PatStruct { |
| 2726 | path: path, |
| 2727 | brace_token: brace_token, |
| 2728 | fields: fields, |
| 2729 | dot2_token: dot2_token, |
| 2730 | }) |
| 2731 | } |
| 2732 | |
| 2733 | #[cfg(feature = "full")] |
| 2734 | fn field_pat(input: ParseStream) -> Result<FieldPat> { |
| 2735 | let boxed: Option<Token![box]> = input.parse()?; |
| 2736 | let by_ref: Option<Token![ref]> = input.parse()?; |
| 2737 | let mutability: Option<Token![mut]> = input.parse()?; |
| 2738 | let member: Member = input.parse()?; |
| 2739 | |
| 2740 | if boxed.is_none() && by_ref.is_none() && mutability.is_none() && input.peek(Token![:]) |
| 2741 | || member.is_unnamed() |
| 2742 | { |
| 2743 | return Ok(FieldPat { |
| 2744 | attrs: Vec::new(), |
| 2745 | member: member, |
| 2746 | colon_token: input.parse()?, |
| 2747 | pat: input.parse()?, |
| 2748 | }); |
| 2749 | } |
| 2750 | |
| 2751 | let ident = match member { |
| 2752 | Member::Named(ident) => ident, |
| 2753 | Member::Unnamed(_) => unreachable!(), |
| 2754 | }; |
| 2755 | |
| 2756 | let mut pat = Pat::Ident(PatIdent { |
| 2757 | by_ref: by_ref, |
| 2758 | mutability: mutability, |
| 2759 | ident: ident.clone(), |
| 2760 | subpat: None, |
| 2761 | }); |
| 2762 | |
| 2763 | if let Some(boxed) = boxed { |
| 2764 | pat = Pat::Box(PatBox { |
| 2765 | pat: Box::new(pat), |
| 2766 | box_token: boxed, |
| 2767 | }); |
| 2768 | } |
| 2769 | |
| 2770 | Ok(FieldPat { |
| 2771 | member: Member::Named(ident), |
| 2772 | pat: Box::new(pat), |
| 2773 | attrs: Vec::new(), |
| 2774 | colon_token: None, |
| 2775 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 2778 | impl Parse for Member { |
| 2779 | fn parse(input: ParseStream) -> Result<Self> { |
| 2780 | if input.peek(Ident) { |
| 2781 | input.parse().map(Member::Named) |
| 2782 | } else if input.peek(LitInt) { |
| 2783 | input.parse().map(Member::Unnamed) |
| 2784 | } else { |
| 2785 | Err(input.error("expected identifier or integer")) |
| 2786 | } |
| 2787 | } |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2788 | } |
| 2789 | |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 2790 | impl Parse for Index { |
| 2791 | fn parse(input: ParseStream) -> Result<Self> { |
| 2792 | let lit: LitInt = input.parse()?; |
| 2793 | if let IntSuffix::None = lit.suffix() { |
| 2794 | Ok(Index { |
| 2795 | index: lit.value() as u32, |
| 2796 | span: lit.span(), |
| 2797 | }) |
| 2798 | } else { |
| 2799 | Err(input.error("expected unsuffixed integer")) |
| 2800 | } |
| 2801 | } |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2802 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2803 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2804 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2805 | fn pat_path(input: ParseStream) -> Result<PatPath> { |
| 2806 | let p: ExprPath = input.parse()?; |
| 2807 | Ok(PatPath { |
| 2808 | qself: p.qself, |
| 2809 | path: p.path, |
| 2810 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2811 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 2812 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2813 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2814 | fn pat_tuple(input: ParseStream) -> Result<PatTuple> { |
| 2815 | let content; |
| 2816 | let paren_token = parenthesized!(content in input); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2817 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2818 | let mut front = Punctuated::new(); |
| 2819 | let mut dot2_token = None::<Token![..]>; |
| 2820 | let mut comma_token = None::<Token![,]>; |
| 2821 | loop { |
| 2822 | if content.is_empty() { |
| 2823 | break; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2824 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2825 | if content.peek(Token![..]) { |
| 2826 | dot2_token = Some(content.parse()?); |
| 2827 | comma_token = content.parse()?; |
| 2828 | break; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2829 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2830 | let value: Pat = content.parse()?; |
| 2831 | front.push_value(value); |
| 2832 | if content.is_empty() { |
| 2833 | break; |
| 2834 | } |
| 2835 | let punct = content.parse()?; |
| 2836 | front.push_punct(punct); |
| 2837 | } |
| 2838 | |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 2839 | let mut back = Punctuated::new(); |
| 2840 | while !content.is_empty() { |
| 2841 | let value: Pat = content.parse()?; |
| 2842 | back.push_value(value); |
| 2843 | if content.is_empty() { |
| 2844 | break; |
| 2845 | } |
| 2846 | let punct = content.parse()?; |
| 2847 | back.push_punct(punct); |
| 2848 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2849 | |
| 2850 | Ok(PatTuple { |
| 2851 | paren_token: paren_token, |
| 2852 | front: front, |
| 2853 | dot2_token: dot2_token, |
| 2854 | comma_token: comma_token, |
| 2855 | back: back, |
| 2856 | }) |
| 2857 | } |
| 2858 | |
| 2859 | #[cfg(feature = "full")] |
| 2860 | fn pat_ref(input: ParseStream) -> Result<PatRef> { |
| 2861 | Ok(PatRef { |
| 2862 | and_token: input.parse()?, |
| 2863 | mutability: input.parse()?, |
| 2864 | pat: input.parse()?, |
| 2865 | }) |
| 2866 | } |
| 2867 | |
| 2868 | #[cfg(feature = "full")] |
| 2869 | fn pat_lit(input: ParseStream) -> Result<PatLit> { |
| 2870 | if input.peek(Lit) || input.peek(Token![-]) && input.peek2(Lit) { |
| 2871 | Ok(PatLit { |
| 2872 | expr: input.call(pat_lit_expr)?, |
| 2873 | }) |
| 2874 | } else { |
| 2875 | Err(input.error("expected literal pattern")) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2880 | fn pat_range(input: ParseStream) -> Result<PatRange> { |
| 2881 | Ok(PatRange { |
| 2882 | lo: input.call(pat_lit_expr)?, |
| 2883 | limits: input.parse()?, |
| 2884 | hi: input.call(pat_lit_expr)?, |
| 2885 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | #[cfg(feature = "full")] |
| 2889 | fn pat_lit_expr(input: ParseStream) -> Result<Box<Expr>> { |
| 2890 | let neg: Option<Token![-]> = input.parse()?; |
| 2891 | |
| 2892 | let lookahead = input.lookahead1(); |
| 2893 | let expr = if lookahead.peek(Lit) { |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2894 | Expr::Lit(input.call(expr_lit)?) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2895 | } else if lookahead.peek(Ident) |
| 2896 | || lookahead.peek(Token![::]) |
| 2897 | || lookahead.peek(Token![<]) |
| 2898 | || lookahead.peek(Token![self]) |
| 2899 | || lookahead.peek(Token![Self]) |
| 2900 | || lookahead.peek(Token![super]) |
| 2901 | || lookahead.peek(Token![extern]) |
| 2902 | || lookahead.peek(Token![crate]) |
| 2903 | { |
| 2904 | Expr::Path(input.parse()?) |
| 2905 | } else { |
| 2906 | return Err(lookahead.error()); |
| 2907 | }; |
| 2908 | |
| 2909 | Ok(Box::new(if let Some(neg) = neg { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 2910 | Expr::Unary(ExprUnary { |
| 2911 | attrs: Vec::new(), |
David Tolnay | c29b989 | 2017-12-27 22:58:14 -0500 | [diff] [blame] | 2912 | op: UnOp::Neg(neg), |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2913 | expr: Box::new(expr), |
David Tolnay | 3bc597f | 2017-12-31 02:31:11 -0500 | [diff] [blame] | 2914 | }) |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2915 | } else { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2916 | expr |
| 2917 | })) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2918 | } |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 2919 | |
| 2920 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2921 | fn pat_slice(input: ParseStream) -> Result<PatSlice> { |
| 2922 | let content; |
| 2923 | let bracket_token = bracketed!(content in input); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2924 | |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2925 | let mut front = Punctuated::new(); |
| 2926 | let mut middle = None; |
| 2927 | loop { |
| 2928 | if content.is_empty() || content.peek(Token![..]) { |
| 2929 | break; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2930 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2931 | let value: Pat = content.parse()?; |
| 2932 | if content.peek(Token![..]) { |
| 2933 | middle = Some(Box::new(value)); |
| 2934 | break; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2935 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2936 | front.push_value(value); |
| 2937 | if content.is_empty() { |
| 2938 | break; |
| 2939 | } |
| 2940 | let punct = content.parse()?; |
| 2941 | front.push_punct(punct); |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2942 | } |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2943 | |
| 2944 | let dot2_token: Option<Token![..]> = content.parse()?; |
| 2945 | let mut comma_token = None::<Token![,]>; |
| 2946 | let mut back = Punctuated::new(); |
| 2947 | if dot2_token.is_some() { |
| 2948 | comma_token = content.parse()?; |
| 2949 | if comma_token.is_some() { |
| 2950 | loop { |
| 2951 | if content.is_empty() { |
| 2952 | break; |
| 2953 | } |
| 2954 | let value: Pat = content.parse()?; |
| 2955 | back.push_value(value); |
| 2956 | if content.is_empty() { |
| 2957 | break; |
| 2958 | } |
| 2959 | let punct = content.parse()?; |
| 2960 | back.push_punct(punct); |
| 2961 | } |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | Ok(PatSlice { |
| 2966 | bracket_token: bracket_token, |
| 2967 | front: front, |
| 2968 | middle: middle, |
| 2969 | dot2_token: dot2_token, |
| 2970 | comma_token: comma_token, |
| 2971 | back: back, |
| 2972 | }) |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | #[cfg(feature = "full")] |
David Tolnay | 310b326 | 2018-08-30 15:33:00 -0700 | [diff] [blame] | 2976 | fn pat_macro(input: ParseStream) -> Result<PatMacro> { |
| 2977 | Ok(PatMacro { |
| 2978 | mac: input.parse()?, |
| 2979 | }) |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 2980 | } |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 2981 | |
| 2982 | #[cfg(feature = "full")] |
| 2983 | impl Member { |
| 2984 | fn is_named(&self) -> bool { |
| 2985 | match *self { |
| 2986 | Member::Named(_) => true, |
| 2987 | Member::Unnamed(_) => false, |
| 2988 | } |
| 2989 | } |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 2990 | |
| 2991 | fn is_unnamed(&self) -> bool { |
| 2992 | match *self { |
| 2993 | Member::Named(_) => false, |
| 2994 | Member::Unnamed(_) => true, |
| 2995 | } |
| 2996 | } |
David Tolnay | 1501f7e | 2018-08-27 14:21:03 -0700 | [diff] [blame] | 2997 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3000 | #[cfg(feature = "printing")] |
| 3001 | mod printing { |
| 3002 | use super::*; |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 3003 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3004 | use proc_macro2::{Literal, TokenStream}; |
| 3005 | use quote::{ToTokens, TokenStreamExt}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3006 | |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 3007 | #[cfg(feature = "full")] |
| 3008 | use attr::FilterAttrs; |
| 3009 | #[cfg(feature = "full")] |
| 3010 | use print::TokensOrDefault; |
| 3011 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 3012 | // If the given expression is a bare `ExprStruct`, wraps it in parenthesis |
David Tolnay | b6a0e7d | 2018-05-20 22:06:47 -0700 | [diff] [blame] | 3013 | // before appending it to `TokenStream`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3014 | #[cfg(feature = "full")] |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3015 | fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3016 | if let Expr::Struct(_) = *e { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 3017 | token::Paren::default().surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3018 | e.to_tokens(tokens); |
| 3019 | }); |
| 3020 | } else { |
| 3021 | e.to_tokens(tokens); |
| 3022 | } |
| 3023 | } |
| 3024 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3025 | #[cfg(feature = "full")] |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3026 | fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3027 | tokens.append_all(attrs.outer()); |
| 3028 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3029 | |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3030 | #[cfg(feature = "full")] |
| 3031 | fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) { |
| 3032 | tokens.append_all(attrs.inner()); |
| 3033 | } |
| 3034 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3035 | #[cfg(not(feature = "full"))] |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3036 | fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {} |
| 3037 | |
| 3038 | #[cfg(not(feature = "full"))] |
| 3039 | fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {} |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3040 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3041 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3042 | impl ToTokens for ExprBox { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3043 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3044 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3045 | self.box_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3046 | self.expr.to_tokens(tokens); |
| 3047 | } |
| 3048 | } |
| 3049 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3050 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3051 | impl ToTokens for ExprInPlace { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3052 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3053 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 8701a5c | 2017-12-28 23:31:10 -0500 | [diff] [blame] | 3054 | self.place.to_tokens(tokens); |
| 3055 | self.arrow_token.to_tokens(tokens); |
| 3056 | self.value.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3060 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3061 | impl ToTokens for ExprArray { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3062 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3063 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3064 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3065 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 2a86fdd | 2017-12-28 23:34:28 -0500 | [diff] [blame] | 3066 | self.elems.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3067 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | impl ToTokens for ExprCall { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3072 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3073 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3074 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3075 | self.paren_token.surround(tokens, |tokens| { |
| 3076 | self.args.to_tokens(tokens); |
| 3077 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3081 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3082 | impl ToTokens for ExprMethodCall { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3083 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3084 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 7641851 | 2017-12-28 23:47:47 -0500 | [diff] [blame] | 3085 | self.receiver.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3086 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3087 | self.method.to_tokens(tokens); |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 3088 | self.turbofish.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3089 | self.paren_token.surround(tokens, |tokens| { |
| 3090 | self.args.to_tokens(tokens); |
| 3091 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3095 | #[cfg(feature = "full")] |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 3096 | impl ToTokens for MethodTurbofish { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3097 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 3098 | self.colon2_token.to_tokens(tokens); |
| 3099 | self.lt_token.to_tokens(tokens); |
| 3100 | self.args.to_tokens(tokens); |
| 3101 | self.gt_token.to_tokens(tokens); |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | #[cfg(feature = "full")] |
| 3106 | impl ToTokens for GenericMethodArgument { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3107 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d60cfec | 2017-12-29 00:21:38 -0500 | [diff] [blame] | 3108 | match *self { |
| 3109 | GenericMethodArgument::Type(ref t) => t.to_tokens(tokens), |
| 3110 | GenericMethodArgument::Const(ref c) => c.to_tokens(tokens), |
| 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | #[cfg(feature = "full")] |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 3116 | impl ToTokens for ExprTuple { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3117 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3118 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3119 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3120 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 2a86fdd | 2017-12-28 23:34:28 -0500 | [diff] [blame] | 3121 | self.elems.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3122 | // If we only have one argument, we need a trailing comma to |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 3123 | // distinguish ExprTuple from ExprParen. |
David Tolnay | a0834b4 | 2018-01-01 21:30:02 -0800 | [diff] [blame] | 3124 | if self.elems.len() == 1 && !self.elems.trailing_punct() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3125 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3126 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3127 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | impl ToTokens for ExprBinary { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3132 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3133 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3134 | self.left.to_tokens(tokens); |
| 3135 | self.op.to_tokens(tokens); |
| 3136 | self.right.to_tokens(tokens); |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | impl ToTokens for ExprUnary { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3141 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3142 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3143 | self.op.to_tokens(tokens); |
| 3144 | self.expr.to_tokens(tokens); |
| 3145 | } |
| 3146 | } |
| 3147 | |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3148 | impl ToTokens for ExprLit { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3149 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3150 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3151 | self.lit.to_tokens(tokens); |
| 3152 | } |
| 3153 | } |
| 3154 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3155 | impl ToTokens for ExprCast { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3156 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3157 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3158 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3159 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3160 | self.ty.to_tokens(tokens); |
| 3161 | } |
| 3162 | } |
| 3163 | |
David Tolnay | 0cf94f2 | 2017-12-28 23:46:26 -0500 | [diff] [blame] | 3164 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3165 | impl ToTokens for ExprType { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3166 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3167 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3168 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3169 | self.colon_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3170 | self.ty.to_tokens(tokens); |
| 3171 | } |
| 3172 | } |
| 3173 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3174 | #[cfg(feature = "full")] |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3175 | fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) { |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 3176 | if let Some((ref else_token, ref else_)) = *else_ { |
| 3177 | else_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3178 | |
| 3179 | // If we are not one of the valid expressions to exist in an else |
| 3180 | // clause, wrap ourselves in a block. |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 3181 | match **else_ { |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3182 | Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => { |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 3183 | else_.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3184 | } |
| 3185 | _ => { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 3186 | token::Brace::default().surround(tokens, |tokens| { |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 3187 | else_.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3188 | }); |
| 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3195 | impl ToTokens for ExprIf { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3196 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3197 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3198 | self.if_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3199 | wrap_bare_struct(tokens, &self.cond); |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 3200 | self.then_branch.to_tokens(tokens); |
| 3201 | maybe_wrap_else(tokens, &self.else_branch); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3205 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3206 | impl ToTokens for ExprIfLet { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3207 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3208 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3209 | self.if_token.to_tokens(tokens); |
| 3210 | self.let_token.to_tokens(tokens); |
David Tolnay | 5b5b7d2 | 2018-03-31 21:05:00 +0200 | [diff] [blame] | 3211 | self.pats.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3212 | self.eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3213 | wrap_bare_struct(tokens, &self.expr); |
David Tolnay | 2ccf32a | 2017-12-29 00:34:26 -0500 | [diff] [blame] | 3214 | self.then_branch.to_tokens(tokens); |
| 3215 | maybe_wrap_else(tokens, &self.else_branch); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3219 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3220 | impl ToTokens for ExprWhile { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3221 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3222 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3223 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3224 | self.while_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3225 | wrap_bare_struct(tokens, &self.cond); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3226 | self.body.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3227 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3228 | tokens.append_all(&self.body.stmts); |
| 3229 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3230 | } |
| 3231 | } |
| 3232 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3233 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3234 | impl ToTokens for ExprWhileLet { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3235 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3236 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3237 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3238 | self.while_token.to_tokens(tokens); |
| 3239 | self.let_token.to_tokens(tokens); |
David Tolnay | 5b5b7d2 | 2018-03-31 21:05:00 +0200 | [diff] [blame] | 3240 | self.pats.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3241 | self.eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3242 | wrap_bare_struct(tokens, &self.expr); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3243 | self.body.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3244 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3245 | tokens.append_all(&self.body.stmts); |
| 3246 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3250 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3251 | impl ToTokens for ExprForLoop { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3252 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3253 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3254 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3255 | self.for_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3256 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3257 | self.in_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3258 | wrap_bare_struct(tokens, &self.expr); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3259 | self.body.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3260 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3261 | tokens.append_all(&self.body.stmts); |
| 3262 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3263 | } |
| 3264 | } |
| 3265 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3266 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3267 | impl ToTokens for ExprLoop { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3268 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3269 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3270 | self.label.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3271 | self.loop_token.to_tokens(tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3272 | self.body.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3273 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3274 | tokens.append_all(&self.body.stmts); |
| 3275 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3276 | } |
| 3277 | } |
| 3278 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3279 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3280 | impl ToTokens for ExprMatch { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3281 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3282 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3283 | self.match_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3284 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3285 | self.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3286 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 3287 | for (i, arm) in self.arms.iter().enumerate() { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3288 | arm.to_tokens(tokens); |
| 3289 | // Ensure that we have a comma after a non-block arm, except |
| 3290 | // for the last one. |
| 3291 | let is_last = i == self.arms.len() - 1; |
David Tolnay | e532d6b | 2018-08-30 16:55:01 -0700 | [diff] [blame] | 3292 | if !is_last && requires_terminator(&arm.body) && arm.comma.is_none() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3293 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3294 | } |
| 3295 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3296 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3300 | #[cfg(feature = "full")] |
David Tolnay | 02a9c6f | 2018-08-24 18:58:45 -0400 | [diff] [blame] | 3301 | impl ToTokens for ExprAsync { |
| 3302 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3303 | outer_attrs_to_tokens(&self.attrs, tokens); |
| 3304 | self.async_token.to_tokens(tokens); |
| 3305 | self.capture.to_tokens(tokens); |
| 3306 | self.block.to_tokens(tokens); |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | #[cfg(feature = "full")] |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 3311 | impl ToTokens for ExprTryBlock { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3312 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3313 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | fb2dd4b | 2018-08-24 16:45:34 -0400 | [diff] [blame] | 3314 | self.try_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3315 | self.block.to_tokens(tokens); |
| 3316 | } |
| 3317 | } |
| 3318 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3319 | #[cfg(feature = "full")] |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 3320 | impl ToTokens for ExprYield { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3321 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3322 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 3323 | self.yield_token.to_tokens(tokens); |
| 3324 | self.expr.to_tokens(tokens); |
| 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3329 | impl ToTokens for ExprClosure { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3330 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3331 | outer_attrs_to_tokens(&self.attrs, tokens); |
Yusuke Sasaki | 4e5d966 | 2018-07-21 02:49:47 +0900 | [diff] [blame] | 3332 | self.asyncness.to_tokens(tokens); |
David Tolnay | 13d4c0e | 2018-03-31 20:53:59 +0200 | [diff] [blame] | 3333 | self.movability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3334 | self.capture.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3335 | self.or1_token.to_tokens(tokens); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 3336 | for input in self.inputs.pairs() { |
| 3337 | match **input.value() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 3338 | FnArg::Captured(ArgCaptured { |
| 3339 | ref pat, |
| 3340 | ty: Type::Infer(_), |
| 3341 | .. |
| 3342 | }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3343 | pat.to_tokens(tokens); |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 3344 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 3345 | _ => input.value().to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 3346 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 3347 | input.punct().to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3348 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3349 | self.or2_token.to_tokens(tokens); |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame] | 3350 | self.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3351 | self.body.to_tokens(tokens); |
| 3352 | } |
| 3353 | } |
| 3354 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3355 | #[cfg(feature = "full")] |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 3356 | impl ToTokens for ExprUnsafe { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3357 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3358 | outer_attrs_to_tokens(&self.attrs, tokens); |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 3359 | self.unsafe_token.to_tokens(tokens); |
David Tolnay | c4be351 | 2018-08-27 06:25:44 -0700 | [diff] [blame] | 3360 | self.block.brace_token.surround(tokens, |tokens| { |
| 3361 | inner_attrs_to_tokens(&self.attrs, tokens); |
| 3362 | tokens.append_all(&self.block.stmts); |
| 3363 | }); |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3368 | impl ToTokens for ExprBlock { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3369 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3370 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 1d8e996 | 2018-08-24 19:04:20 -0400 | [diff] [blame] | 3371 | self.label.to_tokens(tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3372 | self.block.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3373 | inner_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 5d314dc | 2018-07-21 16:40:01 -0700 | [diff] [blame] | 3374 | tokens.append_all(&self.block.stmts); |
| 3375 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3376 | } |
| 3377 | } |
| 3378 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3379 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3380 | impl ToTokens for ExprAssign { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3381 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3382 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3383 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3384 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3385 | self.right.to_tokens(tokens); |
| 3386 | } |
| 3387 | } |
| 3388 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3389 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3390 | impl ToTokens for ExprAssignOp { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3391 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3392 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3393 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3394 | self.op.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3395 | self.right.to_tokens(tokens); |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | impl ToTokens for ExprField { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3400 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3401 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3402 | self.base.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3403 | self.dot_token.to_tokens(tokens); |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3404 | self.member.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3405 | } |
| 3406 | } |
| 3407 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3408 | impl ToTokens for Member { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3409 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3410 | match *self { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3411 | Member::Named(ref ident) => ident.to_tokens(tokens), |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3412 | Member::Unnamed(ref index) => index.to_tokens(tokens), |
| 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3417 | impl ToTokens for Index { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3418 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 3419 | let mut lit = Literal::i64_unsuffixed(i64::from(self.index)); |
| 3420 | lit.set_span(self.span); |
| 3421 | tokens.append(lit); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | impl ToTokens for ExprIndex { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3426 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3427 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3428 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3429 | self.bracket_token.surround(tokens, |tokens| { |
| 3430 | self.index.to_tokens(tokens); |
| 3431 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3435 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3436 | impl ToTokens for ExprRange { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3437 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3438 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3439 | self.from.to_tokens(tokens); |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 3440 | match self.limits { |
| 3441 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 3442 | RangeLimits::Closed(ref t) => t.to_tokens(tokens), |
| 3443 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3444 | self.to.to_tokens(tokens); |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | impl ToTokens for ExprPath { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3449 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3450 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3451 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3455 | #[cfg(feature = "full")] |
David Tolnay | 00674ba | 2018-03-31 18:14:11 +0200 | [diff] [blame] | 3456 | impl ToTokens for ExprReference { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3457 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3458 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3459 | self.and_token.to_tokens(tokens); |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 3460 | self.mutability.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3461 | self.expr.to_tokens(tokens); |
| 3462 | } |
| 3463 | } |
| 3464 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3465 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3466 | impl ToTokens for ExprBreak { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3467 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3468 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3469 | self.break_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3470 | self.label.to_tokens(tokens); |
| 3471 | self.expr.to_tokens(tokens); |
| 3472 | } |
| 3473 | } |
| 3474 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3475 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3476 | impl ToTokens for ExprContinue { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3477 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3478 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3479 | self.continue_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3480 | self.label.to_tokens(tokens); |
| 3481 | } |
| 3482 | } |
| 3483 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3484 | #[cfg(feature = "full")] |
David Tolnay | c246cd3 | 2017-12-28 23:14:32 -0500 | [diff] [blame] | 3485 | impl ToTokens for ExprReturn { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3486 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3487 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3488 | self.return_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3489 | self.expr.to_tokens(tokens); |
| 3490 | } |
| 3491 | } |
| 3492 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3493 | #[cfg(feature = "full")] |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3494 | impl ToTokens for ExprMacro { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3495 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3496 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 8c91b88 | 2017-12-28 23:04:32 -0500 | [diff] [blame] | 3497 | self.mac.to_tokens(tokens); |
| 3498 | } |
| 3499 | } |
| 3500 | |
| 3501 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3502 | impl ToTokens for ExprStruct { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3503 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3504 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3505 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3506 | self.brace_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3507 | inner_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3508 | self.fields.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3509 | if self.rest.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3510 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3511 | self.rest.to_tokens(tokens); |
| 3512 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3513 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3514 | } |
| 3515 | } |
| 3516 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3517 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3518 | impl ToTokens for ExprRepeat { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3519 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3520 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3521 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3522 | inner_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3523 | self.expr.to_tokens(tokens); |
| 3524 | self.semi_token.to_tokens(tokens); |
David Tolnay | 84d8044 | 2018-01-07 01:03:20 -0800 | [diff] [blame] | 3525 | self.len.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3526 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3527 | } |
| 3528 | } |
| 3529 | |
David Tolnay | e98775f | 2017-12-28 23:17:00 -0500 | [diff] [blame] | 3530 | #[cfg(feature = "full")] |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 3531 | impl ToTokens for ExprGroup { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3532 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3533 | outer_attrs_to_tokens(&self.attrs, tokens); |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 3534 | self.group_token.surround(tokens, |tokens| { |
| 3535 | self.expr.to_tokens(tokens); |
| 3536 | }); |
| 3537 | } |
| 3538 | } |
| 3539 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3540 | impl ToTokens for ExprParen { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3541 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3542 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3543 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3544 | inner_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3545 | self.expr.to_tokens(tokens); |
| 3546 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3547 | } |
| 3548 | } |
| 3549 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3550 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3551 | impl ToTokens for ExprTry { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3552 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3553 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 3554 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3555 | self.question_token.to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3556 | } |
| 3557 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3558 | |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 3559 | impl ToTokens for ExprVerbatim { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3560 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 3561 | self.tts.to_tokens(tokens); |
| 3562 | } |
| 3563 | } |
| 3564 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3565 | #[cfg(feature = "full")] |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3566 | impl ToTokens for Label { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3567 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | bcd498f | 2017-12-29 12:02:33 -0500 | [diff] [blame] | 3568 | self.name.to_tokens(tokens); |
| 3569 | self.colon_token.to_tokens(tokens); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | #[cfg(feature = "full")] |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 3574 | impl ToTokens for FieldValue { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3575 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3576 | outer_attrs_to_tokens(&self.attrs, tokens); |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3577 | self.member.to_tokens(tokens); |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 3578 | if let Some(ref colon_token) = self.colon_token { |
| 3579 | colon_token.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 3580 | self.expr.to_tokens(tokens); |
| 3581 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3585 | #[cfg(feature = "full")] |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3586 | impl ToTokens for Arm { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3587 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3588 | tokens.append_all(&self.attrs); |
David Tolnay | 18cc4d4 | 2018-03-31 18:47:20 +0200 | [diff] [blame] | 3589 | self.leading_vert.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3590 | self.pats.to_tokens(tokens); |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3591 | if let Some((ref if_token, ref guard)) = self.guard { |
| 3592 | if_token.to_tokens(tokens); |
| 3593 | guard.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3594 | } |
David Tolnay | dfb9143 | 2018-03-31 19:19:44 +0200 | [diff] [blame] | 3595 | self.fat_arrow_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3596 | self.body.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3597 | self.comma.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3598 | } |
| 3599 | } |
| 3600 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3601 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3602 | impl ToTokens for PatWild { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3603 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3604 | self.underscore_token.to_tokens(tokens); |
| 3605 | } |
| 3606 | } |
| 3607 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3608 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3609 | impl ToTokens for PatIdent { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3610 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 3611 | self.by_ref.to_tokens(tokens); |
David Tolnay | efc96fb | 2017-12-29 02:03:15 -0500 | [diff] [blame] | 3612 | self.mutability.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3613 | self.ident.to_tokens(tokens); |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3614 | if let Some((ref at_token, ref subpat)) = self.subpat { |
| 3615 | at_token.to_tokens(tokens); |
| 3616 | subpat.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3617 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3621 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3622 | impl ToTokens for PatStruct { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3623 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3624 | self.path.to_tokens(tokens); |
| 3625 | self.brace_token.surround(tokens, |tokens| { |
| 3626 | self.fields.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3627 | // NOTE: We need a comma before the dot2 token if it is present. |
| 3628 | if !self.fields.empty_or_trailing() && self.dot2_token.is_some() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3629 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3630 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3631 | self.dot2_token.to_tokens(tokens); |
| 3632 | }); |
| 3633 | } |
| 3634 | } |
| 3635 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3636 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3637 | impl ToTokens for PatTupleStruct { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3638 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3639 | self.path.to_tokens(tokens); |
| 3640 | self.pat.to_tokens(tokens); |
| 3641 | } |
| 3642 | } |
| 3643 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3644 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3645 | impl ToTokens for PatPath { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3646 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3647 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 3648 | } |
| 3649 | } |
| 3650 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3651 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3652 | impl ToTokens for PatTuple { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3653 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3654 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 3655 | self.front.to_tokens(tokens); |
| 3656 | if let Some(ref dot2_token) = self.dot2_token { |
| 3657 | if !self.front.empty_or_trailing() { |
| 3658 | // Ensure there is a comma before the .. token. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3659 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3660 | } |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 3661 | dot2_token.to_tokens(tokens); |
| 3662 | self.comma_token.to_tokens(tokens); |
| 3663 | if self.comma_token.is_none() && !self.back.is_empty() { |
| 3664 | // Ensure there is a comma after the .. token. |
| 3665 | <Token![,]>::default().to_tokens(tokens); |
| 3666 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 3667 | } |
David Tolnay | 4187192 | 2017-12-29 01:53:45 -0500 | [diff] [blame] | 3668 | self.back.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3669 | }); |
| 3670 | } |
| 3671 | } |
| 3672 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3673 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3674 | impl ToTokens for PatBox { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3675 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3676 | self.box_token.to_tokens(tokens); |
| 3677 | self.pat.to_tokens(tokens); |
| 3678 | } |
| 3679 | } |
| 3680 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3681 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3682 | impl ToTokens for PatRef { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3683 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3684 | self.and_token.to_tokens(tokens); |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 3685 | self.mutability.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3686 | self.pat.to_tokens(tokens); |
| 3687 | } |
| 3688 | } |
| 3689 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3690 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3691 | impl ToTokens for PatLit { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3692 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3693 | self.expr.to_tokens(tokens); |
| 3694 | } |
| 3695 | } |
| 3696 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3697 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3698 | impl ToTokens for PatRange { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3699 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3700 | self.lo.to_tokens(tokens); |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 3701 | match self.limits { |
| 3702 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 3703 | RangeLimits::Closed(ref t) => Token.to_tokens(tokens), |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 3704 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3705 | self.hi.to_tokens(tokens); |
| 3706 | } |
| 3707 | } |
| 3708 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3709 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3710 | impl ToTokens for PatSlice { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3711 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3712 | // XXX: This is a mess, and it will be so easy to screw it up. How |
| 3713 | // do we make this correct itself better? |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3714 | self.bracket_token.surround(tokens, |tokens| { |
| 3715 | self.front.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3716 | |
| 3717 | // If we need a comma before the middle or standalone .. token, |
| 3718 | // then make sure it's present. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 3719 | if !self.front.empty_or_trailing() |
| 3720 | && (self.middle.is_some() || self.dot2_token.is_some()) |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3721 | { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 3722 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | // If we have an identifier, we always need a .. token. |
| 3726 | if self.middle.is_some() { |
| 3727 | self.middle.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3728 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3729 | } else if self.dot2_token.is_some() { |
| 3730 | self.dot2_token.to_tokens(tokens); |
| 3731 | } |
| 3732 | |
| 3733 | // Make sure we have a comma before the back half. |
| 3734 | if !self.back.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3735 | TokensOrDefault(&self.comma_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3736 | self.back.to_tokens(tokens); |
| 3737 | } else { |
| 3738 | self.comma_token.to_tokens(tokens); |
| 3739 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3740 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3744 | #[cfg(feature = "full")] |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 3745 | impl ToTokens for PatMacro { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3746 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 3747 | self.mac.to_tokens(tokens); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | #[cfg(feature = "full")] |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 3752 | impl ToTokens for PatVerbatim { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3753 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 3754 | self.tts.to_tokens(tokens); |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | #[cfg(feature = "full")] |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 3759 | impl ToTokens for FieldPat { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3760 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 3761 | if let Some(ref colon_token) = self.colon_token { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3762 | self.member.to_tokens(tokens); |
David Tolnay | 5d7098a | 2017-12-29 01:35:24 -0500 | [diff] [blame] | 3763 | colon_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 3764 | } |
| 3765 | self.pat.to_tokens(tokens); |
| 3766 | } |
| 3767 | } |
| 3768 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3769 | #[cfg(feature = "full")] |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3770 | impl ToTokens for Block { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3771 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3772 | self.brace_token.surround(tokens, |tokens| { |
| 3773 | tokens.append_all(&self.stmts); |
| 3774 | }); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3775 | } |
| 3776 | } |
| 3777 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3778 | #[cfg(feature = "full")] |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3779 | impl ToTokens for Stmt { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3780 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3781 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3782 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3783 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 3784 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3785 | Stmt::Semi(ref expr, ref semi) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3786 | expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3787 | semi.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3788 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3789 | } |
| 3790 | } |
| 3791 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3792 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3793 | #[cfg(feature = "full")] |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3794 | impl ToTokens for Local { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 3795 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | d997aef | 2018-07-21 18:42:31 -0700 | [diff] [blame] | 3796 | outer_attrs_to_tokens(&self.attrs, tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3797 | self.let_token.to_tokens(tokens); |
David Tolnay | 5b5b7d2 | 2018-03-31 21:05:00 +0200 | [diff] [blame] | 3798 | self.pats.to_tokens(tokens); |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3799 | if let Some((ref colon_token, ref ty)) = self.ty { |
| 3800 | colon_token.to_tokens(tokens); |
| 3801 | ty.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3802 | } |
David Tolnay | 8b4d302 | 2017-12-29 12:11:10 -0500 | [diff] [blame] | 3803 | if let Some((ref eq_token, ref init)) = self.init { |
| 3804 | eq_token.to_tokens(tokens); |
| 3805 | init.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3806 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3807 | self.semi_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3808 | } |
| 3809 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3810 | } |