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