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