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