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