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