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