blob: d4ce03a013ba7c9b511e27e2878d997a25beacc7 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnaye303b7c2018-05-20 16:46:35 -070010use proc_macro2::{Span, TokenStream};
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay14982012017-12-29 00:49:51 -050012#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -050013use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -050014#[cfg(feature = "full")]
15use std::mem;
David Tolnay94d2b792018-04-29 12:26:10 -070016#[cfg(feature = "extra-traits")]
17use tt::TokenStreamHelper;
David Tolnayf4bbbd92016-09-23 14:41:55 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -080020 /// A Rust expression.
David Tolnay614a0142018-01-07 10:25:43 -080021 ///
David Tolnay461d98e2018-01-07 11:07:19 -080022 /// *This type is available if Syn is built with the `"derive"` or `"full"`
23 /// feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enums
26 ///
27 /// This type is a syntax tree enum. In Syn this and other syntax tree enums
28 /// are designed to be traversed using the following rebinding idiom.
29 ///
30 /// ```
31 /// # use syn::Expr;
32 /// #
33 /// # fn example(expr: Expr) {
34 /// # const IGNORE: &str = stringify! {
35 /// let expr: Expr = /* ... */;
36 /// # };
37 /// match expr {
38 /// Expr::MethodCall(expr) => {
39 /// /* ... */
40 /// }
41 /// Expr::Cast(expr) => {
42 /// /* ... */
43 /// }
44 /// Expr::IfLet(expr) => {
45 /// /* ... */
46 /// }
47 /// /* ... */
48 /// # _ => {}
49 /// }
50 /// # }
51 /// ```
52 ///
53 /// We begin with a variable `expr` of type `Expr` that has no fields
54 /// (because it is an enum), and by matching on it and rebinding a variable
55 /// with the same name `expr` we effectively imbue our variable with all of
56 /// the data fields provided by the variant that it turned out to be. So for
57 /// example above if we ended up in the `MethodCall` case then we get to use
58 /// `expr.receiver`, `expr.args` etc; if we ended up in the `IfLet` case we
59 /// get to use `expr.pat`, `expr.then_branch`, `expr.else_branch`.
60 ///
61 /// The pattern is similar if the input expression is borrowed:
62 ///
63 /// ```
64 /// # use syn::Expr;
65 /// #
66 /// # fn example(expr: &Expr) {
67 /// match *expr {
68 /// Expr::MethodCall(ref expr) => {
69 /// # }
70 /// # _ => {}
71 /// # }
72 /// # }
73 /// ```
74 ///
75 /// This approach avoids repeating the variant names twice on every line.
76 ///
77 /// ```
78 /// # use syn::{Expr, ExprMethodCall};
79 /// #
80 /// # fn example(expr: Expr) {
81 /// # match expr {
82 /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive
83 /// # }
84 /// # _ => {}
85 /// # }
86 /// # }
87 /// ```
88 ///
89 /// In general, the name to which a syntax tree enum variant is bound should
90 /// be a suitable name for the complete syntax tree enum type.
91 ///
92 /// ```
93 /// # use syn::{Expr, ExprField};
94 /// #
95 /// # fn example(discriminant: &ExprField) {
96 /// // Binding is called `base` which is the name I would use if I were
97 /// // assigning `*discriminant.base` without an `if let`.
98 /// if let Expr::Tuple(ref base) = *discriminant.base {
99 /// # }
100 /// # }
101 /// ```
102 ///
103 /// A sign that you may not be choosing the right variable names is if you
104 /// see names getting repeated in your code, like accessing
105 /// `receiver.receiver` or `pat.pat` or `cond.cond`.
David Tolnay8c91b882017-12-28 23:04:32 -0500106 pub enum Expr {
David Tolnaya454c8f2018-01-07 01:01:10 -0800107 /// A box expression: `box f`.
David Tolnay461d98e2018-01-07 11:07:19 -0800108 ///
109 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400110 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500111 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500113 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500115
David Tolnaya454c8f2018-01-07 01:01:10 -0800116 /// A placement expression: `place <- value`.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -0500122 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 pub value: Box<Expr>,
124 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500125
David Tolnaya454c8f2018-01-07 01:01:10 -0800126 /// A slice literal expression: `[a, b, c, d]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400129 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500130 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500131 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500134
David Tolnaya454c8f2018-01-07 01:01:10 -0800135 /// A function call expression: `invoke(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -0500140 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700141 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500142 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500143 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500145
David Tolnaya454c8f2018-01-07 01:01:10 -0800146 /// A method call expression: `x.foo::<T>(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800147 ///
148 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -0500151 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -0500154 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500156 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500158
David Tolnaya454c8f2018-01-07 01:01:10 -0800159 /// A tuple expression: `(a, b, c, d)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800160 ///
161 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -0500162 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500163 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500164 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500165 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500167
David Tolnaya454c8f2018-01-07 01:01:10 -0800168 /// A binary operation: `a + b`, `a * b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800169 ///
170 /// *This type is available if Syn is built with the `"derive"` or
171 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500173 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500175 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 pub right: Box<Expr>,
177 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500178
David Tolnaya454c8f2018-01-07 01:01:10 -0800179 /// A unary operation: `!x`, `*x`.
David Tolnay461d98e2018-01-07 11:07:19 -0800180 ///
181 /// *This type is available if Syn is built with the `"derive"` or
182 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 pub op: UnOp,
186 pub expr: Box<Expr>,
187 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500188
David Tolnaya454c8f2018-01-07 01:01:10 -0800189 /// A literal in place of an expression: `1`, `"foo"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800190 ///
191 /// *This type is available if Syn is built with the `"derive"` or
192 /// `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500193 pub Lit(ExprLit {
194 pub attrs: Vec<Attribute>,
195 pub lit: Lit,
196 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500197
David Tolnaya454c8f2018-01-07 01:01:10 -0800198 /// A cast expression: `foo as f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800199 ///
200 /// *This type is available if Syn is built with the `"derive"` or
201 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -0500203 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500208
David Tolnaya454c8f2018-01-07 01:01:10 -0800209 /// A type ascription expression: `foo: f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800210 ///
211 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay0cf94f22017-12-28 23:46:26 -0500212 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500213 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700214 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800215 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800216 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500218
David Tolnaya454c8f2018-01-07 01:01:10 -0800219 /// An `if` expression with an optional `else` block: `if expr { ... }
220 /// else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800222 /// The `else` branch expression may only be an `If`, `IfLet`, or
223 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800224 ///
225 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500228 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500230 pub then_branch: Block,
231 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500233
David Tolnaya454c8f2018-01-07 01:01:10 -0800234 /// An `if let` expression with an optional `else` block: `if let pat =
235 /// expr { ... } else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800237 /// The `else` branch expression may only be an `If`, `IfLet`, or
238 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800243 pub if_token: Token![if],
244 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200245 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500248 pub then_branch: Block,
249 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500251
David Tolnaya454c8f2018-01-07 01:01:10 -0800252 /// A while loop: `while expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400255 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500256 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500257 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500259 pub cond: Box<Expr>,
260 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500262
David Tolnaya454c8f2018-01-07 01:01:10 -0800263 /// A while-let loop: `while let pat = expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800264 ///
265 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400266 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500267 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500268 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800269 pub while_token: Token![while],
270 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200271 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500273 pub expr: Box<Expr>,
274 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
David Tolnaya454c8f2018-01-07 01:01:10 -0800277 /// A for loop: `for pat in expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800278 ///
279 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400280 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500281 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500282 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500283 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500285 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 pub expr: Box<Expr>,
287 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500289
David Tolnaya454c8f2018-01-07 01:01:10 -0800290 /// Conditionless loop: `loop { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800291 ///
292 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400293 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500295 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub loop_token: Token![loop],
297 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500299
David Tolnaya454c8f2018-01-07 01:01:10 -0800300 /// A `match` expression: `match n { Some(n) => {}, None => {} }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800301 ///
302 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400303 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500304 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800305 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500307 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 pub arms: Vec<Arm>,
309 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500310
David Tolnaya454c8f2018-01-07 01:01:10 -0800311 /// A closure expression: `|a, b| a + b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800312 ///
313 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400314 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500315 pub attrs: Vec<Attribute>,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +0900316 pub asyncness: Option<Token![async]>,
David Tolnay13d4c0e2018-03-31 20:53:59 +0200317 pub movability: Option<Token![static]>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500318 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500320 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800321 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500322 pub output: ReturnType,
323 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500325
David Tolnaya454c8f2018-01-07 01:01:10 -0800326 /// An unsafe block: `unsafe { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800327 ///
328 /// *This type is available if Syn is built with the `"full"` feature.*
Nika Layzell640832a2017-12-04 13:37:09 -0500329 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500330 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500331 pub unsafe_token: Token![unsafe],
332 pub block: Block,
333 }),
334
David Tolnaya454c8f2018-01-07 01:01:10 -0800335 /// A blocked scope: `{ ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800336 ///
337 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400338 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500339 pub attrs: Vec<Attribute>,
David Tolnay1d8e9962018-08-24 19:04:20 -0400340 pub label: Option<Label>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 pub block: Block,
342 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700343
David Tolnaya454c8f2018-01-07 01:01:10 -0800344 /// An assignment expression: `a = compute()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800345 ///
346 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400347 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500348 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800350 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500351 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500353
David Tolnaya454c8f2018-01-07 01:01:10 -0800354 /// A compound assignment expression: `counter += 1`.
David Tolnay461d98e2018-01-07 11:07:19 -0800355 ///
356 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400357 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500358 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500360 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub right: Box<Expr>,
362 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500363
David Tolnaya454c8f2018-01-07 01:01:10 -0800364 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500365 /// field (`obj.0`).
David Tolnay461d98e2018-01-07 11:07:19 -0800366 ///
367 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd5147742018-06-30 10:09:52 -0700368 pub Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -0500369 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500370 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800371 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500372 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500374
David Tolnay05658502018-01-07 09:56:37 -0800375 /// A square bracketed indexing expression: `vector[2]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800376 ///
377 /// *This type is available if Syn is built with the `"derive"` or
378 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500380 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500382 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500383 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500385
David Tolnaya454c8f2018-01-07 01:01:10 -0800386 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800387 ///
388 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400389 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500390 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500393 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700395
David Tolnaya454c8f2018-01-07 01:01:10 -0800396 /// A path like `std::mem::replace` possibly containing generic
397 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800399 /// A plain identifier like `x` is a path of length 1.
David Tolnay461d98e2018-01-07 11:07:19 -0800400 ///
401 /// *This type is available if Syn is built with the `"derive"` or
402 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500404 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 pub qself: Option<QSelf>,
406 pub path: Path,
407 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700408
David Tolnaya454c8f2018-01-07 01:01:10 -0800409 /// A referencing operation: `&a` or `&mut a`.
David Tolnay461d98e2018-01-07 11:07:19 -0800410 ///
411 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay00674ba2018-03-31 18:14:11 +0200412 pub Reference(ExprReference #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500413 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500415 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700416 pub expr: Box<Expr>,
417 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500418
David Tolnaya454c8f2018-01-07 01:01:10 -0800419 /// A `break`, with an optional label to break and an optional
420 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800421 ///
422 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400423 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500424 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500425 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700426 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 pub expr: Option<Box<Expr>>,
428 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500429
David Tolnaya454c8f2018-01-07 01:01:10 -0800430 /// A `continue`, with an optional label.
David Tolnay461d98e2018-01-07 11:07:19 -0800431 ///
432 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400433 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500434 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500436 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700437 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500438
David Tolnaya454c8f2018-01-07 01:01:10 -0800439 /// A `return`, with an optional value to be returned.
David Tolnay461d98e2018-01-07 11:07:19 -0800440 ///
441 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayc246cd32017-12-28 23:14:32 -0500442 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500443 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800444 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500445 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700447
David Tolnaya454c8f2018-01-07 01:01:10 -0800448 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500451 pub Macro(ExprMacro #full {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700455
David Tolnaya454c8f2018-01-07 01:01:10 -0800456 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700457 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800458 /// The `rest` provides the value of the remaining fields as in `S { a:
459 /// 1, b: 1, ..rest }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800460 ///
461 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400462 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500463 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500465 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500466 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500467 pub dot2_token: Option<Token![..]>,
468 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700470
David Tolnaya454c8f2018-01-07 01:01:10 -0800471 /// An array literal constructed from one repeated element: `[0u8; N]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800472 ///
473 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400474 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500475 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500476 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700477 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500478 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800479 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700480 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700481
David Tolnaya454c8f2018-01-07 01:01:10 -0800482 /// A parenthesized expression: `(a + b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800483 ///
484 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9374bc02018-01-27 18:49:36 -0800485 pub Paren(ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -0500486 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500487 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500488 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700490
David Tolnaya454c8f2018-01-07 01:01:10 -0800491 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400492 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800493 /// This variant is important for faithfully representing the precedence
494 /// of expressions and is related to `None`-delimited spans in a
495 /// `TokenStream`.
David Tolnay461d98e2018-01-07 11:07:19 -0800496 ///
497 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye98775f2017-12-28 23:17:00 -0500498 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500499 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500500 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500501 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400502 }),
503
David Tolnaya454c8f2018-01-07 01:01:10 -0800504 /// A try-expression: `expr?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800505 ///
506 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400507 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500508 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700512
David Tolnay02a9c6f2018-08-24 18:58:45 -0400513 /// An async block: `async { ... }`.
514 ///
515 /// *This type is available if Syn is built with the `"full"` feature.*
516 pub Async(ExprAsync #full {
517 pub attrs: Vec<Attribute>,
518 pub async_token: Token![async],
519 pub capture: Option<Token![move]>,
520 pub block: Block,
521 }),
522
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400523 /// A try block: `try { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800524 ///
525 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400526 pub TryBlock(ExprTryBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500527 pub attrs: Vec<Attribute>,
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400528 pub try_token: Token![try],
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 pub block: Block,
530 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700531
David Tolnaya454c8f2018-01-07 01:01:10 -0800532 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800533 ///
534 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700535 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500536 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800537 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700538 pub expr: Option<Box<Expr>>,
539 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500540
David Tolnaya454c8f2018-01-07 01:01:10 -0800541 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800542 ///
543 /// *This type is available if Syn is built with the `"derive"` or
544 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500545 pub Verbatim(ExprVerbatim #manual_extra_traits {
546 pub tts: TokenStream,
547 }),
548 }
549}
550
551#[cfg(feature = "extra-traits")]
552impl Eq for ExprVerbatim {}
553
554#[cfg(feature = "extra-traits")]
555impl PartialEq for ExprVerbatim {
556 fn eq(&self, other: &Self) -> bool {
557 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
558 }
559}
560
561#[cfg(feature = "extra-traits")]
562impl Hash for ExprVerbatim {
563 fn hash<H>(&self, state: &mut H)
564 where
565 H: Hasher,
566 {
567 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700568 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700569}
570
David Tolnay8c91b882017-12-28 23:04:32 -0500571impl Expr {
572 // Not public API.
573 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500574 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500575 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500576 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800577 Expr::Box(ExprBox { ref mut attrs, .. })
578 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
579 | Expr::Array(ExprArray { ref mut attrs, .. })
580 | Expr::Call(ExprCall { ref mut attrs, .. })
581 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
582 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
583 | Expr::Binary(ExprBinary { ref mut attrs, .. })
584 | Expr::Unary(ExprUnary { ref mut attrs, .. })
585 | Expr::Lit(ExprLit { ref mut attrs, .. })
586 | Expr::Cast(ExprCast { ref mut attrs, .. })
587 | Expr::Type(ExprType { ref mut attrs, .. })
588 | Expr::If(ExprIf { ref mut attrs, .. })
589 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
590 | Expr::While(ExprWhile { ref mut attrs, .. })
591 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
592 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
593 | Expr::Loop(ExprLoop { ref mut attrs, .. })
594 | Expr::Match(ExprMatch { ref mut attrs, .. })
595 | Expr::Closure(ExprClosure { ref mut attrs, .. })
596 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
597 | Expr::Block(ExprBlock { ref mut attrs, .. })
598 | Expr::Assign(ExprAssign { ref mut attrs, .. })
599 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
600 | Expr::Field(ExprField { ref mut attrs, .. })
601 | Expr::Index(ExprIndex { ref mut attrs, .. })
602 | Expr::Range(ExprRange { ref mut attrs, .. })
603 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200604 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800605 | Expr::Break(ExprBreak { ref mut attrs, .. })
606 | Expr::Continue(ExprContinue { ref mut attrs, .. })
607 | Expr::Return(ExprReturn { ref mut attrs, .. })
608 | Expr::Macro(ExprMacro { ref mut attrs, .. })
609 | Expr::Struct(ExprStruct { ref mut attrs, .. })
610 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
611 | Expr::Paren(ExprParen { ref mut attrs, .. })
612 | Expr::Group(ExprGroup { ref mut attrs, .. })
613 | Expr::Try(ExprTry { ref mut attrs, .. })
David Tolnay02a9c6f2018-08-24 18:58:45 -0400614 | Expr::Async(ExprAsync { ref mut attrs, .. })
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400615 | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800616 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500617 Expr::Verbatim(_) => {
618 // TODO
619 Vec::new()
620 }
David Tolnay8c91b882017-12-28 23:04:32 -0500621 }
622 }
623}
624
David Tolnay85b69a42017-12-27 20:43:10 -0500625ast_enum! {
626 /// A struct or tuple struct field accessed in a struct literal or field
627 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800628 ///
629 /// *This type is available if Syn is built with the `"derive"` or `"full"`
630 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500631 pub enum Member {
632 /// A named field like `self.x`.
633 Named(Ident),
634 /// An unnamed field like `self.0`.
635 Unnamed(Index),
636 }
637}
638
David Tolnay85b69a42017-12-27 20:43:10 -0500639ast_struct! {
640 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800641 ///
642 /// *This type is available if Syn is built with the `"derive"` or `"full"`
643 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500644 pub struct Index #manual_extra_traits {
645 pub index: u32,
646 pub span: Span,
647 }
648}
649
David Tolnay14982012017-12-29 00:49:51 -0500650impl From<usize> for Index {
651 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700652 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500653 Index {
654 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700655 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500656 }
657 }
658}
659
660#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500661impl Eq for Index {}
662
David Tolnay14982012017-12-29 00:49:51 -0500663#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500664impl PartialEq for Index {
665 fn eq(&self, other: &Self) -> bool {
666 self.index == other.index
667 }
668}
669
David Tolnay14982012017-12-29 00:49:51 -0500670#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500671impl Hash for Index {
672 fn hash<H: Hasher>(&self, state: &mut H) {
673 self.index.hash(state);
674 }
675}
676
677#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700678ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800679 /// The `::<>` explicit type parameters passed to a method call:
680 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800681 ///
682 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500683 pub struct MethodTurbofish {
684 pub colon2_token: Token![::],
685 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500686 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500687 pub gt_token: Token![>],
688 }
689}
690
691#[cfg(feature = "full")]
692ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800693 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500696 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800697 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500698 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800699 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500700 ///
701 /// NOTE: Identity expressions are represented as Type arguments, as
702 /// they are indistinguishable syntactically.
703 Const(Expr),
704 }
705}
706
707#[cfg(feature = "full")]
708ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800710 ///
711 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500713 /// Attributes tagged on the field.
714 pub attrs: Vec<Attribute>,
715
716 /// Name or index of the field.
717 pub member: Member,
718
David Tolnay5d7098a2017-12-29 01:35:24 -0500719 /// The colon in `Struct { x: x }`. If written in shorthand like
720 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500721 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500722
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 /// Value of the field.
724 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 }
David Tolnay055a7042016-10-02 19:23:54 -0700726}
727
Michael Layzell734adb42017-06-07 16:58:31 -0400728#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700729ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800730 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800731 ///
732 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500733 pub struct Label {
734 pub name: Lifetime,
735 pub colon_token: Token![:],
736 }
737}
738
739#[cfg(feature = "full")]
740ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800741 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800742 ///
743 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500745 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 /// Statements in a block
747 pub stmts: Vec<Stmt>,
748 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700749}
750
Michael Layzell734adb42017-06-07 16:58:31 -0400751#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700752ast_enum! {
753 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800754 ///
755 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700756 pub enum Stmt {
757 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800758 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700759
Alex Crichton62a0a592017-05-22 13:58:53 -0700760 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800761 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700762
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800764 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700765
David Tolnaya454c8f2018-01-07 01:01:10 -0800766 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800767 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700768 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700769}
770
Michael Layzell734adb42017-06-07 16:58:31 -0400771#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700772ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800773 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800774 ///
775 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700776 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500777 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800778 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200779 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500780 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500781 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500782 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700783 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700784}
785
Michael Layzell734adb42017-06-07 16:58:31 -0400786#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700787ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800788 /// A pattern in a local binding, function signature, match expression, or
789 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800790 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800791 /// *This type is available if Syn is built with the `"full"` feature.*
792 ///
David Tolnay614a0142018-01-07 10:25:43 -0800793 /// # Syntax tree enum
794 ///
795 /// This type is a [syntax tree enum].
796 ///
797 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700798 // Clippy false positive
799 // https://github.com/Manishearth/rust-clippy/issues/1241
800 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
801 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800802 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800803 ///
804 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800806 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700807 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700808
David Tolnaya454c8f2018-01-07 01:01:10 -0800809 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800810 ///
811 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700812 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500813 pub by_ref: Option<Token![ref]>,
814 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700815 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500816 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700817 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700818
David Tolnaya454c8f2018-01-07 01:01:10 -0800819 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800820 ///
821 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700822 pub Struct(PatStruct {
823 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500824 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500825 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800826 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700828
David Tolnaya454c8f2018-01-07 01:01:10 -0800829 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800830 ///
831 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700832 pub TupleStruct(PatTupleStruct {
833 pub path: Path,
834 pub pat: PatTuple,
835 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700836
David Tolnaya454c8f2018-01-07 01:01:10 -0800837 /// A path pattern like `Color::Red`, optionally qualified with a
838 /// self-type.
839 ///
840 /// Unquailfied path patterns can legally refer to variants, structs,
841 /// constants or associated constants. Quailfied path patterns like
842 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
843 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800844 ///
845 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700846 pub Path(PatPath {
847 pub qself: Option<QSelf>,
848 pub path: Path,
849 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700850
David Tolnaya454c8f2018-01-07 01:01:10 -0800851 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800852 ///
853 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700854 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500855 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500856 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500857 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500858 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500859 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700860 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800861
862 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800863 ///
864 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700865 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500867 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700868 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800869
870 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800871 ///
872 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800874 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500875 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500876 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700877 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800878
879 /// A literal pattern: `0`.
880 ///
881 /// This holds an `Expr` rather than a `Lit` because negative numbers
882 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800883 ///
884 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700885 pub Lit(PatLit {
886 pub expr: Box<Expr>,
887 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800888
889 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800890 ///
891 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 pub Range(PatRange {
893 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700894 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500895 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800897
898 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800899 ///
900 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700901 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500902 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500903 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700904 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500905 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500906 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500907 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700908 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800909
910 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800911 ///
912 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500913 pub Macro(PatMacro {
914 pub mac: Macro,
915 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800916
917 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800918 ///
919 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500920 pub Verbatim(PatVerbatim #manual_extra_traits {
921 pub tts: TokenStream,
922 }),
923 }
924}
925
David Tolnayc43b44e2017-12-30 23:55:54 -0500926#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500927impl Eq for PatVerbatim {}
928
David Tolnayc43b44e2017-12-30 23:55:54 -0500929#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500930impl PartialEq for PatVerbatim {
931 fn eq(&self, other: &Self) -> bool {
932 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
933 }
934}
935
David Tolnayc43b44e2017-12-30 23:55:54 -0500936#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500937impl Hash for PatVerbatim {
938 fn hash<H>(&self, state: &mut H)
939 where
940 H: Hasher,
941 {
942 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700944}
945
Michael Layzell734adb42017-06-07 16:58:31 -0400946#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700947ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800948 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800950 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500952 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800953 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500954 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800956 /// 0...10 => {
957 /// return true;
958 /// }
959 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500960 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800962 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500963 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800965 ///
966 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700967 pub struct Arm {
968 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200969 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500970 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500971 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200972 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700973 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700976}
977
Michael Layzell734adb42017-06-07 16:58:31 -0400978#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700979ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800980 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800981 ///
982 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700983 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800985 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800987 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800988 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700989 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700990}
991
Michael Layzell734adb42017-06-07 16:58:31 -0400992#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700993ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800994 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700995 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800996 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
997 /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token.
David Tolnay461d98e2018-01-07 11:07:19 -0800998 ///
999 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -05001001 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -05001002 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -05001003 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001004 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001005 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001006}
1007
Michael Layzell3936ceb2017-07-08 00:28:36 -04001008#[cfg(any(feature = "parsing", feature = "printing"))]
1009#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -07001010fn arm_expr_requires_comma(expr: &Expr) -> bool {
David Tolnay01218d12018-08-29 18:13:07 -07001011 // see https://github.com/rust-lang/rust/blob/eb8f2586e/src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001012 match *expr {
1013 Expr::Unsafe(..)
1014 | Expr::Block(..)
1015 | Expr::If(..)
1016 | Expr::IfLet(..)
1017 | Expr::Match(..)
1018 | Expr::While(..)
1019 | Expr::WhileLet(..)
1020 | Expr::Loop(..)
1021 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001022 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001023 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001024 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001025 }
1026}
1027
David Tolnayb9c8e322016-09-23 20:48:37 -07001028#[cfg(feature = "parsing")]
1029pub mod parsing {
1030 use super::*;
David Tolnay60291082018-08-28 09:54:49 -07001031 use path;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001032 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001033 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001034
David Tolnay9389c382018-08-27 09:13:37 -07001035 use parse::{Parse, ParseStream, Result};
Michael Layzell734adb42017-06-07 16:58:31 -04001036 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001037 use synom::ext::IdentExt;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001038
David Tolnay9389c382018-08-27 09:13:37 -07001039 macro_rules! named2 {
1040 ($name:ident ($($arg:ident : $argty:ty),*) -> $ret:ty, $($rest:tt)*) => {
1041 fn $name(input: ParseStream $(, $arg : $argty)*) -> Result<$ret> {
1042 named!(_synom ($($arg : $argty),*) -> $ret, $($rest)*);
1043 input.step_cursor(|cursor| _synom(*cursor $(, $arg)*))
1044 }
1045 };
1046 ($name:ident -> $ret:ty, $($rest:tt)*) => {
1047 fn $name(input: ParseStream) -> Result<$ret> {
1048 named!(_synom -> $ret, $($rest)*);
1049 input.step_cursor(|cursor| _synom(*cursor))
1050 }
1051 };
1052 }
1053
David Tolnaybcf26022017-12-25 22:10:52 -05001054 // When we're parsing expressions which occur before blocks, like in an if
1055 // statement's condition, we cannot parse a struct literal.
1056 //
1057 // Struct literals are ambiguous in certain positions
1058 // https://github.com/rust-lang/rfcs/pull/92
David Tolnay9389c382018-08-27 09:13:37 -07001059 #[derive(Copy, Clone)]
1060 pub struct AllowStruct(bool);
1061
1062 #[derive(Copy, Clone)]
1063 pub struct AllowBlock(bool);
David Tolnayaf2557e2016-10-24 11:52:21 -07001064
David Tolnay01218d12018-08-29 18:13:07 -07001065 #[derive(Copy, Clone, PartialEq, PartialOrd)]
1066 enum Precedence {
1067 Any,
1068 Assign,
1069 Placement,
1070 Range,
1071 Or,
1072 And,
1073 Compare,
1074 BitOr,
1075 BitXor,
1076 BitAnd,
1077 Shift,
1078 Arithmetic,
1079 Term,
1080 Cast,
1081 }
1082
1083 impl Precedence {
1084 fn of(op: &BinOp) -> Self {
1085 match *op {
1086 BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic,
1087 BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term,
1088 BinOp::And(_) => Precedence::And,
1089 BinOp::Or(_) => Precedence::Or,
1090 BinOp::BitXor(_) => Precedence::BitXor,
1091 BinOp::BitAnd(_) => Precedence::BitAnd,
1092 BinOp::BitOr(_) => Precedence::BitOr,
1093 BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift,
1094 BinOp::Eq(_) | BinOp::Lt(_) | BinOp::Le(_) | BinOp::Ne(_) | BinOp::Ge(_) | BinOp::Gt(_) => Precedence::Compare,
1095 BinOp::AddEq(_) | BinOp::SubEq(_) | BinOp::MulEq(_) | BinOp::DivEq(_) | BinOp::RemEq(_) | BinOp::BitXorEq(_) | BinOp::BitAndEq(_) | BinOp::BitOrEq(_) | BinOp::ShlEq(_) | BinOp::ShrEq(_) => Precedence::Assign,
1096 }
1097 }
1098 }
1099
David Tolnay9389c382018-08-27 09:13:37 -07001100 impl Parse for Expr {
1101 fn parse(input: ParseStream) -> Result<Self> {
1102 ambiguous_expr(input, AllowStruct(true), AllowBlock(true))
Alex Crichton954046c2017-05-30 21:49:42 -07001103 }
1104 }
1105
Michael Layzell734adb42017-06-07 16:58:31 -04001106 #[cfg(feature = "full")]
David Tolnay9fb0aed2018-08-27 10:23:12 -07001107 fn expr_no_struct(input: ParseStream) -> Result<Expr> {
1108 ambiguous_expr(input, AllowStruct(false), AllowBlock(true))
1109 }
David Tolnayaf2557e2016-10-24 11:52:21 -07001110
David Tolnay01218d12018-08-29 18:13:07 -07001111 #[cfg(feature = "full")]
1112 fn parse_expr(input: ParseStream, mut lhs: Expr, allow_struct: AllowStruct, allow_block: AllowBlock, base: Precedence) -> Result<Expr> {
1113 loop {
1114 if input.fork().parse::<BinOp>().ok().map_or(false, |op| Precedence::of(&op) >= base) {
1115 let op: BinOp = input.parse()?;
1116 let precedence = Precedence::of(&op);
1117 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1118 loop {
1119 let next = peek_precedence(input);
1120 if next > precedence || next == precedence && precedence == Precedence::Assign {
1121 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1122 } else {
1123 break;
1124 }
1125 }
1126 lhs = Expr::Binary(ExprBinary {
1127 attrs: Vec::new(),
1128 left: Box::new(lhs),
1129 op: op,
1130 right: Box::new(rhs),
1131 });
1132 } else if Precedence::Assign >= base && input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1133 let eq_token: Token![=] = input.parse()?;
1134 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1135 loop {
1136 let next = peek_precedence(input);
1137 if next >= Precedence::Assign {
1138 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1139 } else {
1140 break;
1141 }
1142 }
1143 lhs = Expr::Assign(ExprAssign {
1144 attrs: Vec::new(),
1145 left: Box::new(lhs),
1146 eq_token: eq_token,
1147 right: Box::new(rhs),
1148 });
1149 } else if Precedence::Placement >= base && input.peek(Token![<-]) {
1150 let arrow_token: Token![<-] = input.parse()?;
1151 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1152 loop {
1153 let next = peek_precedence(input);
1154 if next > Precedence::Placement {
1155 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1156 } else {
1157 break;
1158 }
1159 }
1160 lhs = Expr::InPlace(ExprInPlace {
1161 attrs: Vec::new(),
1162 place: Box::new(lhs),
1163 arrow_token: arrow_token,
1164 value: Box::new(rhs),
1165 });
1166 } else if Precedence::Range >= base && input.peek(Token![..]) {
1167 let limits: RangeLimits = input.parse()?;
1168 let rhs = if input.is_empty()
1169 || input.peek(Token![,])
1170 || input.peek(Token![;])
1171 || !allow_struct.0 && input.peek(token::Brace)
1172 {
1173 None
1174 } else {
1175 // We don't want to allow blocks in the rhs if we don't
1176 // allow structs.
1177 let allow_block = AllowBlock(allow_struct.0);
1178 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1179 loop {
1180 let next = peek_precedence(input);
1181 if next > Precedence::Range {
1182 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1183 } else {
1184 break;
1185 }
1186 }
1187 Some(rhs)
1188 };
1189 lhs = Expr::Range(ExprRange {
1190 attrs: Vec::new(),
1191 from: Some(Box::new(lhs)),
1192 limits: limits,
1193 to: rhs.map(Box::new),
1194 });
1195 } else if Precedence::Cast >= base && input.peek(Token![as]) {
1196 let as_token: Token![as] = input.parse()?;
1197 let ty = input.call(Type::without_plus)?;
1198 lhs = Expr::Cast(ExprCast {
1199 attrs: Vec::new(),
1200 expr: Box::new(lhs),
1201 as_token: as_token,
1202 ty: Box::new(ty),
1203 });
1204 } else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) {
1205 let colon_token: Token![:] = input.parse()?;
1206 let ty = input.call(Type::without_plus)?;
1207 lhs = Expr::Type(ExprType {
1208 attrs: Vec::new(),
1209 expr: Box::new(lhs),
1210 colon_token: colon_token,
1211 ty: Box::new(ty),
1212 });
1213 } else {
1214 break;
1215 }
1216 }
1217 Ok(lhs)
1218 }
1219
1220 #[cfg(feature = "full")]
1221 fn peek_precedence(input: ParseStream) -> Precedence {
1222 if let Ok(op) = input.fork().parse() {
1223 Precedence::of(&op)
1224 } else if input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1225 Precedence::Assign
1226 } else if input.peek(Token![<-]) {
1227 Precedence::Placement
1228 } else if input.peek(Token![..]) {
1229 Precedence::Range
1230 } else if input.peek(Token![as]) || input.peek(Token![:]) && !input.peek(Token![::]) {
1231 Precedence::Cast
1232 } else {
1233 Precedence::Any
1234 }
1235 }
1236
David Tolnaybcf26022017-12-25 22:10:52 -05001237 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001238 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001239 fn ambiguous_expr(
1240 input: ParseStream,
1241 allow_struct: AllowStruct,
1242 allow_block: AllowBlock,
1243 ) -> Result<Expr> {
David Tolnay01218d12018-08-29 18:13:07 -07001244 //assign_expr(input, allow_struct, allow_block)
1245 let lhs = unary_expr(input, allow_struct, allow_block)?;
1246 parse_expr(input, lhs, allow_struct, allow_block, Precedence::Any)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 }
1248
Michael Layzell734adb42017-06-07 16:58:31 -04001249 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001250 fn ambiguous_expr(
1251 input: ParseStream,
1252 allow_struct: AllowStruct,
1253 allow_block: AllowBlock,
1254 ) -> Result<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001255 // NOTE: We intentionally skip assign_expr, placement_expr, and
David Tolnay9389c382018-08-27 09:13:37 -07001256 // range_expr as they are only parsed in full mode.
1257 or_expr(input, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001258 }
1259
David Tolnaybcf26022017-12-25 22:10:52 -05001260 // <UnOp> <trailer>
1261 // & <trailer>
1262 // &mut <trailer>
1263 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001264 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001265 fn unary_expr(
1266 input: ParseStream,
1267 allow_struct: AllowStruct,
1268 allow_block: AllowBlock,
1269 ) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001270 let ahead = input.fork();
1271 ahead.call(Attribute::parse_outer)?;
1272 if ahead.peek(Token![&])
1273 || ahead.peek(Token![box])
1274 || ahead.peek(Token![*])
1275 || ahead.peek(Token![!])
1276 || ahead.peek(Token![-])
1277 {
1278 let attrs = input.call(Attribute::parse_outer)?;
1279 if input.peek(Token![&]) {
1280 Ok(Expr::Reference(ExprReference {
1281 attrs: attrs,
1282 and_token: input.parse()?,
1283 mutability: input.parse()?,
1284 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1285 }))
1286 } else if input.peek(Token![box]) {
1287 Ok(Expr::Box(ExprBox {
1288 attrs: attrs,
1289 box_token: input.parse()?,
1290 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1291 }))
1292 } else {
1293 Ok(Expr::Unary(ExprUnary {
1294 attrs: attrs,
1295 op: input.parse()?,
1296 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1297 }))
1298 }
1299 } else {
1300 trailer_expr(input, allow_struct, allow_block)
1301 }
1302 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001303
Michael Layzell734adb42017-06-07 16:58:31 -04001304 // XXX: This duplication is ugly
1305 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001306 fn unary_expr(
1307 input: ParseStream,
1308 allow_struct: AllowStruct,
1309 allow_block: AllowBlock,
1310 ) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001311 let ahead = input.fork();
1312 ahead.call(Attribute::parse_outer)?;
1313 if ahead.peek(Token![*]) || ahead.peek(Token![!]) || ahead.peek(Token![-]) {
1314 Ok(Expr::Unary(ExprUnary {
1315 attrs: input.call(Attribute::parse_outer)?,
1316 op: input.parse()?,
1317 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1318 }))
1319 } else {
1320 trailer_expr(input, allow_struct, allow_block)
1321 }
1322 }
Michael Layzell734adb42017-06-07 16:58:31 -04001323
David Tolnayd997aef2018-07-21 18:42:31 -07001324 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001325 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1326 let mut outer = Vec::new();
1327 let mut inner = Vec::new();
1328 for attr in mem::replace(attrs, Vec::new()) {
1329 match attr.style {
1330 AttrStyle::Outer => outer.push(attr),
1331 AttrStyle::Inner(_) => inner.push(attr),
1332 }
1333 }
1334 *attrs = inner;
1335 outer
1336 }
1337
David Tolnaybcf26022017-12-25 22:10:52 -05001338 // <atom> (..<args>) ...
1339 // <atom> . <ident> (..<args>) ...
1340 // <atom> . <ident> ...
1341 // <atom> . <lit> ...
1342 // <atom> [ <expr> ] ...
1343 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001344 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001345 fn trailer_expr(
1346 input: ParseStream,
1347 allow_struct: AllowStruct,
1348 allow_block: AllowBlock,
1349 ) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001350 let mut e = atom_expr(input, allow_struct, allow_block)?;
1351
1352 let mut attrs = e.replace_attrs(Vec::new());
1353 let outer_attrs = take_outer(&mut attrs);
1354 e.replace_attrs(attrs);
1355
David Tolnay01218d12018-08-29 18:13:07 -07001356 e = trailer_helper(input, e)?;
1357
1358 let mut attrs = outer_attrs;
1359 attrs.extend(e.replace_attrs(Vec::new()));
1360 e.replace_attrs(attrs);
1361 Ok(e)
1362 }
1363
1364 #[cfg(feature = "full")]
1365 fn trailer_helper(input: ParseStream, mut e: Expr) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001366 loop {
1367 if input.peek(token::Paren) {
1368 let content;
1369 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001370 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001371 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001372 paren_token: parenthesized!(content in input),
1373 args: content.parse_terminated(<Expr as Parse>::parse)?,
1374 });
1375 } else if input.peek(Token![.]) && !input.peek(Token![..]) {
1376 let dot_token: Token![.] = input.parse()?;
1377 let member: Member = input.parse()?;
1378 let turbofish = if member.is_named() && input.peek(Token![::]) {
1379 Some(MethodTurbofish {
1380 colon2_token: input.parse()?,
1381 lt_token: input.parse()?,
1382 args: {
1383 let mut args = Punctuated::new();
1384 loop {
1385 if input.peek(Token![>]) {
1386 break;
1387 }
1388 let value = input.parse()?;
1389 args.push_value(value);
1390 if input.peek(Token![>]) {
1391 break;
1392 }
1393 let punct = input.parse()?;
1394 args.push_punct(punct);
1395 }
1396 args
1397 },
1398 gt_token: input.parse()?,
1399 })
1400 } else {
1401 None
1402 };
1403
1404 if turbofish.is_some() || input.peek(token::Paren) {
1405 if let Member::Named(method) = member {
1406 let content;
1407 e = Expr::MethodCall(ExprMethodCall {
1408 attrs: Vec::new(),
1409 receiver: Box::new(e),
1410 dot_token: dot_token,
1411 method: method,
1412 turbofish: turbofish,
1413 paren_token: parenthesized!(content in input),
1414 args: content.parse_terminated(<Expr as Parse>::parse)?,
1415 });
1416 continue;
1417 }
1418 }
1419
1420 e = Expr::Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001421 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001422 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001423 dot_token: dot_token,
David Tolnay85b69a42017-12-27 20:43:10 -05001424 member: member,
David Tolnay1501f7e2018-08-27 14:21:03 -07001425 });
1426 } else if input.peek(token::Bracket) {
1427 let content;
1428 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001429 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001430 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001431 bracket_token: bracketed!(content in input),
1432 index: content.parse()?,
1433 });
1434 } else if input.peek(Token![?]) {
1435 e = Expr::Try(ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001436 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001437 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001438 question_token: input.parse()?,
1439 });
1440 } else {
1441 break;
1442 }
1443 }
David Tolnay1501f7e2018-08-27 14:21:03 -07001444 Ok(e)
1445 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001446
Michael Layzell734adb42017-06-07 16:58:31 -04001447 // XXX: Duplication == ugly
1448 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001449 fn trailer_expr(
1450 input: ParseStream,
1451 allow_struct: AllowStruct,
1452 allow_block: AllowBlock,
1453 ) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001454 let mut e = atom_expr(input, allow_struct, allow_block)?;
1455
1456 loop {
1457 if input.peek(token::Paren) {
1458 let content;
1459 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001460 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001461 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001462 paren_token: parenthesized!(content in input),
1463 args: content.parse_terminated(<Expr as Parse>::parse)?,
1464 });
1465 } else if input.peek(Token![.]) {
1466 e = Expr::Field(ExprField {
David Tolnayd5147742018-06-30 10:09:52 -07001467 attrs: Vec::new(),
1468 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001469 dot_token: input.parse()?,
1470 member: input.parse()?,
1471 });
1472 } else if input.peek(token::Bracket) {
1473 let content;
1474 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001475 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001476 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001477 bracket_token: bracketed!(content in input),
1478 index: content.parse()?,
1479 });
1480 } else {
1481 break;
1482 }
1483 }
1484
1485 Ok(e)
1486 }
Michael Layzell734adb42017-06-07 16:58:31 -04001487
David Tolnaya454c8f2018-01-07 01:01:10 -08001488 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001489 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001490 #[cfg(feature = "full")]
David Tolnay6e1e5052018-08-30 10:21:48 -07001491 fn atom_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1492 if input.peek(token::Group) {
1493 return input.parse().map(Expr::Group);
1494 }
1495
1496 let mut attrs = input.call(Attribute::parse_outer)?;
1497
1498 let mut expr = if input.peek(token::Group) {
1499 Expr::Group(input.parse()?)
1500 } else if input.peek(Lit) {
1501 Expr::Lit(input.parse()?)
1502 } else if input.peek(Token![async])
1503 && (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace))
1504 {
1505 Expr::Async(input.parse()?)
1506 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
1507 Expr::TryBlock(input.parse()?)
1508 } else if input.peek(Token![|])
1509 || input.peek(Token![async]) && (input.peek2(Token![|]) || input.peek2(Token![move]))
1510 || input.peek(Token![static])
1511 || input.peek(Token![move])
1512 {
1513 Expr::Closure(expr_closure(input, allow_struct)?)
1514 } else if input.peek(Ident)
1515 || input.peek(Token![::])
1516 || input.peek(Token![<])
1517 || input.peek(Token![self])
1518 || input.peek(Token![Self])
1519 || input.peek(Token![super])
1520 || input.peek(Token![extern])
1521 || input.peek(Token![crate])
1522 {
1523 path_or_macro_or_struct(input, allow_struct)?
1524 } else if input.peek(token::Paren) {
1525 paren_or_tuple(input)?
1526 } else if input.peek(Token![break]) {
1527 Expr::Break(expr_break(input, allow_struct)?)
1528 } else if input.peek(Token![continue]) {
1529 Expr::Continue(input.parse()?)
1530 } else if input.peek(Token![return]) {
1531 Expr::Return(expr_ret(input, allow_struct)?)
1532 } else if input.peek(token::Bracket) {
1533 array_or_repeat(input)?
1534 } else if input.peek(Token![if]) {
1535 if input.peek2(Token![let]) {
1536 Expr::IfLet(input.parse()?)
1537 } else {
1538 Expr::If(input.parse()?)
1539 }
1540 } else if input.peek(Token![while]) {
1541 if input.peek2(Token![let]) {
1542 Expr::WhileLet(input.parse()?)
1543 } else {
1544 Expr::While(input.parse()?)
1545 }
1546 } else if input.peek(Token![for]) {
1547 Expr::ForLoop(input.parse()?)
1548 } else if input.peek(Token![loop]) {
1549 Expr::Loop(input.parse()?)
1550 } else if input.peek(Token![match]) {
1551 Expr::Match(input.parse()?)
1552 } else if input.peek(Token![yield]) {
1553 Expr::Yield(input.parse()?)
1554 } else if input.peek(Token![unsafe]) {
1555 Expr::Unsafe(input.parse()?)
1556 } else if allow_block.0 && input.peek(token::Brace) {
1557 Expr::Block(input.parse()?)
1558 } else if input.peek(Token![..]) {
1559 Expr::Range(expr_range(input, allow_struct)?)
1560 } else if input.peek(Lifetime) {
1561 let the_label: Label = input.parse()?;
1562 let mut expr = if input.peek(Token![while]) {
1563 if input.peek2(Token![let]) {
1564 Expr::WhileLet(input.parse()?)
1565 } else {
1566 Expr::While(input.parse()?)
1567 }
1568 } else if input.peek(Token![for]) {
1569 Expr::ForLoop(input.parse()?)
1570 } else if input.peek(Token![loop]) {
1571 Expr::Loop(input.parse()?)
1572 } else if input.peek(token::Brace) {
1573 Expr::Block(input.parse()?)
1574 } else {
1575 return Err(input.error("expected loop or block expression"));
1576 };
1577 match expr {
1578 Expr::WhileLet(ExprWhileLet { ref mut label, .. }) |
1579 Expr::While(ExprWhile { ref mut label, .. }) |
1580 Expr::ForLoop(ExprForLoop { ref mut label, .. }) |
1581 Expr::Loop(ExprLoop { ref mut label, .. }) |
1582 Expr::Block(ExprBlock { ref mut label, .. }) => *label = Some(the_label),
1583 _ => unreachable!(),
1584 }
1585 expr
1586 } else {
1587 return Err(input.error("expected expression"));
1588 };
1589
1590 attrs.extend(expr.replace_attrs(Vec::new()));
1591 expr.replace_attrs(attrs);
1592 Ok(expr)
1593 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001594
Michael Layzell734adb42017-06-07 16:58:31 -04001595 #[cfg(not(feature = "full"))]
David Tolnay6e1e5052018-08-30 10:21:48 -07001596 fn atom_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1597 if input.peek(Lit) {
1598 input.parse().map(Expr::Lit)
1599 } else if input.peek(token::Paren) {
1600 input.parse().map(Expr::Paren)
1601 } else if input.peek(Ident)
1602 || input.peek(Token![::])
1603 || input.peek(Token![<])
1604 || input.peek(Token![self])
1605 || input.peek(Token![Self])
1606 || input.peek(Token![super])
1607 || input.peek(Token![extern])
1608 || input.peek(Token![crate])
1609 {
1610 input.parse().map(Expr::Path)
1611 } else {
1612 Err(input.error("unsupported expression; enable syn's features=[\"full\"]"))
1613 }
1614 }
1615
1616 #[cfg(feature = "full")]
1617 fn path_or_macro_or_struct(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
1618 let expr: ExprPath = input.parse()?;
1619 if expr.qself.is_some() {
1620 return Ok(Expr::Path(expr));
1621 }
1622
1623 if input.peek(Token![!]) && !input.peek(Token![!=]) {
1624 let mut contains_arguments = false;
1625 for segment in &expr.path.segments {
1626 match segment.arguments {
1627 PathArguments::None => {}
1628 PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
1629 contains_arguments = true;
1630 }
1631 }
1632 }
1633
1634 if !contains_arguments {
1635 let bang_token: Token![!] = input.parse()?;
1636 let (delimiter, tts) = mac::parse_delimiter(input)?;
1637 return Ok(Expr::Macro(ExprMacro {
1638 attrs: Vec::new(),
1639 mac: Macro {
1640 path: expr.path,
1641 bang_token: bang_token,
1642 delimiter: delimiter,
1643 tts: tts,
1644 },
1645 }));
1646 }
1647 }
1648
1649 if allow_struct.0 && input.peek(token::Brace) {
1650 let outer_attrs = Vec::new();
1651 expr_struct_helper(input, outer_attrs, expr.path).map(Expr::Struct)
1652 } else {
1653 Ok(Expr::Path(expr))
1654 }
1655 }
1656
1657 #[cfg(feature = "full")]
1658 fn paren_or_tuple(input: ParseStream) -> Result<Expr> {
1659 let content;
1660 let paren_token = parenthesized!(content in input);
1661 let inner_attrs = content.call(Attribute::parse_inner)?;
1662 if content.is_empty() {
1663 return Ok(Expr::Tuple(ExprTuple {
1664 attrs: inner_attrs,
1665 paren_token: paren_token,
1666 elems: Punctuated::new(),
1667 }));
1668 }
1669
1670 let first: Expr = content.parse()?;
1671 if content.is_empty() {
1672 return Ok(Expr::Paren(ExprParen {
1673 attrs: inner_attrs,
1674 paren_token: paren_token,
1675 expr: Box::new(first),
1676 }));
1677 }
1678
1679 let mut elems = Punctuated::new();
1680 elems.push_value(first);
1681 while !content.is_empty() {
1682 let punct = content.parse()?;
1683 elems.push_punct(punct);
1684 if content.is_empty() {
1685 break;
1686 }
1687 let value = content.parse()?;
1688 elems.push_value(value);
1689 }
1690 Ok(Expr::Tuple(ExprTuple {
1691 attrs: inner_attrs,
1692 paren_token: paren_token,
1693 elems: elems,
1694 }))
1695 }
1696
1697 #[cfg(feature = "full")]
1698 fn array_or_repeat(input: ParseStream) -> Result<Expr> {
1699 let content;
1700 let bracket_token = bracketed!(content in input);
1701 let inner_attrs = content.call(Attribute::parse_inner)?;
1702 if content.is_empty() {
1703 return Ok(Expr::Array(ExprArray {
1704 attrs: inner_attrs,
1705 bracket_token: bracket_token,
1706 elems: Punctuated::new(),
1707 }));
1708 }
1709
1710 let first: Expr = content.parse()?;
1711 if content.is_empty() || content.peek(Token![,]) {
1712 let mut elems = Punctuated::new();
1713 elems.push_value(first);
1714 while !content.is_empty() {
1715 let punct = content.parse()?;
1716 elems.push_punct(punct);
1717 if content.is_empty() {
1718 break;
1719 }
1720 let value = content.parse()?;
1721 elems.push_value(value);
1722 }
1723 Ok(Expr::Array(ExprArray {
1724 attrs: inner_attrs,
1725 bracket_token: bracket_token,
1726 elems: elems,
1727 }))
1728 } else if content.peek(Token![;]) {
1729 let semi_token: Token![;] = content.parse()?;
1730 let len: Expr = content.parse()?;
1731 Ok(Expr::Repeat(ExprRepeat {
1732 attrs: inner_attrs,
1733 bracket_token: bracket_token,
1734 expr: Box::new(first),
1735 semi_token: semi_token,
1736 len: Box::new(len),
1737 }))
1738 } else {
1739 Err(content.error("expected `,` or `;`"))
1740 }
1741 }
Michael Layzell734adb42017-06-07 16:58:31 -04001742
Michael Layzell734adb42017-06-07 16:58:31 -04001743 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07001744 fn expr_early(input: ParseStream) -> Result<Expr> {
1745 let mut attrs = input.call(Attribute::parse_outer)?;
1746 let mut expr = if input.peek(Token![if]) {
1747 if input.peek2(Token![let]) {
1748 Expr::IfLet(input.parse()?)
1749 } else {
1750 Expr::If(input.parse()?)
1751 }
1752 } else if input.peek(Token![while]) {
1753 if input.peek2(Token![let]) {
1754 Expr::WhileLet(input.parse()?)
1755 } else {
1756 Expr::While(input.parse()?)
1757 }
1758 } else if input.peek(Token![for]) {
1759 Expr::ForLoop(input.parse()?)
1760 } else if input.peek(Token![loop]) {
1761 Expr::Loop(input.parse()?)
1762 } else if input.peek(Token![match]) {
1763 Expr::Match(input.parse()?)
1764 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
1765 Expr::TryBlock(input.parse()?)
1766 } else if input.peek(Token![unsafe]) {
1767 Expr::Unsafe(input.parse()?)
1768 } else if input.peek(token::Brace) {
1769 Expr::Block(input.parse()?)
1770 } else {
1771 let allow_struct = AllowStruct(true);
1772 let allow_block = AllowBlock(true);
1773 let mut expr = unary_expr(input, allow_struct, allow_block)?;
1774
1775 attrs.extend(expr.replace_attrs(Vec::new()));
1776 expr.replace_attrs(attrs);
1777
1778 return parse_expr(input, expr, allow_struct, allow_block, Precedence::Any);
1779 };
1780
1781 if input.peek(Token![.]) || input.peek(Token![?]) {
1782 expr = trailer_helper(input, expr)?;
1783
1784 attrs.extend(expr.replace_attrs(Vec::new()));
1785 expr.replace_attrs(attrs);
1786
1787 let allow_struct = AllowStruct(true);
1788 let allow_block = AllowBlock(true);
1789 return parse_expr(input, expr, allow_struct, allow_block, Precedence::Any);
1790 }
1791
1792 attrs.extend(expr.replace_attrs(Vec::new()));
1793 expr.replace_attrs(attrs);
1794 Ok(expr)
1795 }
Michael Layzell35418782017-06-07 09:20:25 -04001796
David Tolnay60291082018-08-28 09:54:49 -07001797 impl Parse for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001798 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001799 fn parse(input: ParseStream) -> Result<Self> {
1800 Ok(ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001801 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001802 lit: input.parse()?,
David Tolnayeb981bb2018-07-21 19:31:38 -07001803 })
David Tolnay60291082018-08-28 09:54:49 -07001804 }
David Tolnayeb981bb2018-07-21 19:31:38 -07001805
1806 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001807 fn parse(input: ParseStream) -> Result<Self> {
1808 Ok(ExprLit {
1809 attrs: input.call(Attribute::parse_outer)?,
1810 lit: input.parse()?,
David Tolnay8c91b882017-12-28 23:04:32 -05001811 })
David Tolnay60291082018-08-28 09:54:49 -07001812 }
David Tolnay8c91b882017-12-28 23:04:32 -05001813 }
1814
1815 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001816 impl Parse for ExprMacro {
1817 fn parse(input: ParseStream) -> Result<Self> {
1818 Ok(ExprMacro {
1819 attrs: input.call(Attribute::parse_outer)?,
1820 mac: input.parse()?,
David Tolnay8c91b882017-12-28 23:04:32 -05001821 })
David Tolnay60291082018-08-28 09:54:49 -07001822 }
David Tolnay8c91b882017-12-28 23:04:32 -05001823 }
1824
David Tolnaye98775f2017-12-28 23:17:00 -05001825 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001826 impl Parse for ExprGroup {
1827 fn parse(input: ParseStream) -> Result<Self> {
1828 let content;
1829 Ok(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001830 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001831 group_token: grouped!(content in input),
1832 expr: content.parse()?,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001833 })
David Tolnay60291082018-08-28 09:54:49 -07001834 }
Michael Layzell93c36282017-06-04 20:43:14 -04001835 }
1836
David Tolnay60291082018-08-28 09:54:49 -07001837 impl Parse for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001838 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001839 fn parse(input: ParseStream) -> Result<Self> {
1840 let content;
1841 Ok(ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001842 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001843 paren_token: parenthesized!(content in input),
1844 expr: content.parse()?,
David Tolnayeb981bb2018-07-21 19:31:38 -07001845 })
David Tolnay60291082018-08-28 09:54:49 -07001846 }
David Tolnayeb981bb2018-07-21 19:31:38 -07001847
1848 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001849 fn parse(input: ParseStream) -> Result<Self> {
1850 let outer_attrs = input.call(Attribute::parse_outer)?;
1851
1852 let content;
1853 let paren_token = parenthesized!(content in input);
1854 let inner_attrs = content.call(Attribute::parse_inner)?;
1855 let expr: Expr = content.parse()?;
1856
1857 Ok(ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001858 attrs: {
1859 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07001860 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07001861 attrs
1862 },
David Tolnay60291082018-08-28 09:54:49 -07001863 paren_token: paren_token,
1864 expr: Box::new(expr),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001865 })
David Tolnay60291082018-08-28 09:54:49 -07001866 }
Alex Crichton954046c2017-05-30 21:49:42 -07001867 }
David Tolnay89e05672016-10-02 14:39:42 -07001868
Michael Layzell734adb42017-06-07 16:58:31 -04001869 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001870 impl Parse for ExprArray {
1871 fn parse(input: ParseStream) -> Result<Self> {
1872 let outer_attrs = input.call(Attribute::parse_outer)?;
1873
1874 let content;
1875 let bracket_token = bracketed!(content in input);
1876 let inner_attrs = content.call(Attribute::parse_inner)?;
1877 let elems = content.parse_terminated(<Expr as Parse>::parse)?;
1878
1879 Ok(ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001880 attrs: {
1881 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07001882 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07001883 attrs
1884 },
David Tolnay60291082018-08-28 09:54:49 -07001885 bracket_token: bracket_token,
1886 elems: elems,
Michael Layzell92639a52017-06-01 00:07:44 -04001887 })
David Tolnay60291082018-08-28 09:54:49 -07001888 }
Alex Crichton954046c2017-05-30 21:49:42 -07001889 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001890
Michael Layzell734adb42017-06-07 16:58:31 -04001891 #[cfg(feature = "full")]
David Tolnay1501f7e2018-08-27 14:21:03 -07001892 impl Parse for GenericMethodArgument {
David Tolnayd60cfec2017-12-29 00:21:38 -05001893 // TODO parse const generics as well
David Tolnay1501f7e2018-08-27 14:21:03 -07001894 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay60291082018-08-28 09:54:49 -07001895 input
1896 .parse_synom(ty_no_eq_after)
1897 .map(GenericMethodArgument::Type)
David Tolnay1501f7e2018-08-27 14:21:03 -07001898 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001899 }
1900
1901 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001902 impl Parse for ExprTuple {
1903 fn parse(input: ParseStream) -> Result<Self> {
1904 let outer_attrs = input.call(Attribute::parse_outer)?;
1905
1906 let content;
1907 let paren_token = parenthesized!(content in input);
1908 let inner_attrs = content.call(Attribute::parse_inner)?;
1909 let elems = content.parse_terminated(<Expr as Parse>::parse)?;
1910
1911 Ok(ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001912 attrs: {
1913 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07001914 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07001915 attrs
1916 },
David Tolnay60291082018-08-28 09:54:49 -07001917 paren_token: paren_token,
1918 elems: elems,
Michael Layzell92639a52017-06-01 00:07:44 -04001919 })
David Tolnay60291082018-08-28 09:54:49 -07001920 }
Alex Crichton954046c2017-05-30 21:49:42 -07001921 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001922
Michael Layzell734adb42017-06-07 16:58:31 -04001923 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001924 impl Parse for ExprIfLet {
1925 fn parse(input: ParseStream) -> Result<Self> {
1926 Ok(ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001927 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001928 if_token: input.parse()?,
1929 let_token: input.parse()?,
1930 pats: {
1931 let mut pats = Punctuated::new();
1932 let value: Pat = input.parse()?;
1933 pats.push_value(value);
1934 while input.peek(Token![|])
1935 && !input.peek(Token![||])
1936 && !input.peek(Token![|=])
1937 {
1938 let punct = input.parse()?;
1939 pats.push_punct(punct);
1940 let value: Pat = input.parse()?;
1941 pats.push_value(value);
1942 }
1943 pats
Michael Layzell92639a52017-06-01 00:07:44 -04001944 },
David Tolnay60291082018-08-28 09:54:49 -07001945 eq_token: input.parse()?,
1946 expr: Box::new(input.call(expr_no_struct)?),
1947 then_branch: input.parse()?,
1948 else_branch: {
1949 if input.peek(Token![else]) {
1950 Some(input.call(else_block)?)
1951 } else {
1952 None
1953 }
1954 },
Michael Layzell92639a52017-06-01 00:07:44 -04001955 })
David Tolnay60291082018-08-28 09:54:49 -07001956 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001957 }
1958
Michael Layzell734adb42017-06-07 16:58:31 -04001959 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001960 impl Parse for ExprIf {
1961 fn parse(input: ParseStream) -> Result<Self> {
1962 Ok(ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001963 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001964 if_token: input.parse()?,
1965 cond: Box::new(input.call(expr_no_struct)?),
1966 then_branch: input.parse()?,
1967 else_branch: {
1968 if input.peek(Token![else]) {
1969 Some(input.call(else_block)?)
1970 } else {
1971 None
1972 }
Michael Layzell92639a52017-06-01 00:07:44 -04001973 },
Michael Layzell92639a52017-06-01 00:07:44 -04001974 })
David Tolnay60291082018-08-28 09:54:49 -07001975 }
Alex Crichton954046c2017-05-30 21:49:42 -07001976 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001977
Michael Layzell734adb42017-06-07 16:58:31 -04001978 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001979 fn else_block(input: ParseStream) -> Result<(Token![else], Box<Expr>)> {
1980 let else_token: Token![else] = input.parse()?;
1981
1982 let lookahead = input.lookahead1();
1983 let else_branch = if input.peek(Token![if]) {
1984 if input.peek2(Token![let]) {
1985 input.parse().map(Expr::IfLet)?
1986 } else {
1987 input.parse().map(Expr::If)?
1988 }
1989 } else if input.peek(token::Brace) {
1990 Expr::Block(ExprBlock {
1991 attrs: Vec::new(),
1992 label: None,
1993 block: input.parse()?,
1994 })
1995 } else {
1996 return Err(lookahead.error());
1997 };
1998
1999 Ok((else_token, Box::new(else_branch)))
2000 }
David Tolnay939766a2016-09-23 23:48:12 -07002001
Michael Layzell734adb42017-06-07 16:58:31 -04002002 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002003 impl Parse for ExprForLoop {
2004 fn parse(input: ParseStream) -> Result<Self> {
2005 let outer_attrs = input.call(Attribute::parse_outer)?;
2006 let label: Option<Label> = input.parse()?;
2007 let for_token: Token![for] = input.parse()?;
2008 let pat: Pat = input.parse()?;
2009 let in_token: Token![in] = input.parse()?;
2010 let expr: Expr = input.call(expr_no_struct)?;
2011
2012 let content;
2013 let brace_token = braced!(content in input);
2014 let inner_attrs = content.call(Attribute::parse_inner)?;
2015 let stmts = content.call(Block::parse_within)?;
2016
2017 Ok(ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07002018 attrs: {
2019 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002020 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002021 attrs
2022 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002023 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002024 for_token: for_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002025 pat: Box::new(pat),
David Tolnay60291082018-08-28 09:54:49 -07002026 in_token: in_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002027 expr: Box::new(expr),
2028 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002029 brace_token: brace_token,
2030 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002031 },
Michael Layzell92639a52017-06-01 00:07:44 -04002032 })
David Tolnay60291082018-08-28 09:54:49 -07002033 }
Alex Crichton954046c2017-05-30 21:49:42 -07002034 }
Gregory Katze5f35682016-09-27 14:20:55 -04002035
Michael Layzell734adb42017-06-07 16:58:31 -04002036 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002037 impl Parse for ExprLoop {
2038 fn parse(input: ParseStream) -> Result<Self> {
2039 let outer_attrs = input.call(Attribute::parse_outer)?;
2040 let label: Option<Label> = input.parse()?;
2041 let loop_token: Token![loop] = input.parse()?;
2042
2043 let content;
2044 let brace_token = braced!(content in input);
2045 let inner_attrs = content.call(Attribute::parse_inner)?;
2046 let stmts = content.call(Block::parse_within)?;
2047
2048 Ok(ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07002049 attrs: {
2050 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002051 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002052 attrs
2053 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002054 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002055 loop_token: loop_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002056 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002057 brace_token: brace_token,
2058 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002059 },
Michael Layzell92639a52017-06-01 00:07:44 -04002060 })
David Tolnay60291082018-08-28 09:54:49 -07002061 }
Alex Crichton954046c2017-05-30 21:49:42 -07002062 }
2063
Michael Layzell734adb42017-06-07 16:58:31 -04002064 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002065 impl Parse for ExprMatch {
2066 fn parse(input: ParseStream) -> Result<Self> {
2067 let outer_attrs = input.call(Attribute::parse_outer)?;
2068 let match_token: Token![match] = input.parse()?;
2069 let expr = expr_no_struct(input)?;
2070
2071 let content;
2072 let brace_token = braced!(content in input);
2073 let inner_attrs = content.call(Attribute::parse_inner)?;
2074
2075 let mut arms = Vec::new();
2076 while !content.is_empty() {
2077 arms.push(content.parse()?);
2078 }
2079
2080 Ok(ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002081 attrs: {
2082 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002083 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002084 attrs
2085 },
David Tolnay60291082018-08-28 09:54:49 -07002086 match_token: match_token,
2087 expr: Box::new(expr),
2088 brace_token: brace_token,
2089 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04002090 })
David Tolnay60291082018-08-28 09:54:49 -07002091 }
Alex Crichton954046c2017-05-30 21:49:42 -07002092 }
David Tolnay1978c672016-10-27 22:05:52 -07002093
Michael Layzell734adb42017-06-07 16:58:31 -04002094 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002095 impl Parse for ExprTryBlock {
2096 fn parse(input: ParseStream) -> Result<Self> {
2097 Ok(ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002098 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002099 try_token: input.parse()?,
2100 block: input.parse()?,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002101 })
David Tolnay60291082018-08-28 09:54:49 -07002102 }
Alex Crichton954046c2017-05-30 21:49:42 -07002103 }
Arnavion02ef13f2017-04-25 00:54:31 -07002104
Michael Layzell734adb42017-06-07 16:58:31 -04002105 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002106 impl Parse for ExprYield {
2107 fn parse(input: ParseStream) -> Result<Self> {
2108 Ok(ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002109 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002110 yield_token: input.parse()?,
2111 expr: {
2112 if !input.is_empty() && !input.peek(Token![,]) && !input.peek(Token![;]) {
2113 Some(input.parse()?)
2114 } else {
2115 None
2116 }
2117 },
Alex Crichtonfe110462017-06-01 12:49:27 -07002118 })
David Tolnay60291082018-08-28 09:54:49 -07002119 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002120 }
2121
2122 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002123 impl Parse for Arm {
2124 fn parse(input: ParseStream) -> Result<Self> {
2125 let requires_comma;
2126 Ok(Arm {
2127 attrs: input.call(Attribute::parse_outer)?,
2128 leading_vert: input.parse()?,
2129 pats: {
2130 let mut pats = Punctuated::new();
2131 let value: Pat = input.parse()?;
2132 pats.push_value(value);
2133 loop {
2134 if !input.peek(Token![|]) {
2135 break;
2136 }
2137 let punct = input.parse()?;
2138 pats.push_punct(punct);
2139 let value: Pat = input.parse()?;
2140 pats.push_value(value);
2141 }
2142 pats
2143 },
2144 guard: {
2145 if input.peek(Token![if]) {
2146 let if_token: Token![if] = input.parse()?;
2147 let guard: Expr = input.parse()?;
2148 Some((if_token, Box::new(guard)))
2149 } else {
2150 None
2151 }
2152 },
2153 fat_arrow_token: input.parse()?,
2154 body: {
David Tolnay01218d12018-08-29 18:13:07 -07002155 let body = input.call(expr_early)?;
David Tolnay60291082018-08-28 09:54:49 -07002156 requires_comma = arm_expr_requires_comma(&body);
2157 Box::new(body)
2158 },
2159 comma: {
2160 if requires_comma && !input.is_empty() {
2161 Some(input.parse()?)
2162 } else {
2163 input.parse()?
2164 }
2165 },
Michael Layzell92639a52017-06-01 00:07:44 -04002166 })
David Tolnay60291082018-08-28 09:54:49 -07002167 }
Alex Crichton954046c2017-05-30 21:49:42 -07002168 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002169
Michael Layzell734adb42017-06-07 16:58:31 -04002170 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002171 fn expr_closure(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprClosure> {
2172 let attrs = input.call(Attribute::parse_outer)?;
2173 let asyncness: Option<Token![async]> = input.parse()?;
2174 let movability: Option<Token![static]> = if asyncness.is_none() {
2175 input.parse()?
2176 } else {
2177 None
2178 };
2179 let capture: Option<Token![move]> = input.parse()?;
2180 let or1_token: Token![|] = input.parse()?;
2181
2182 let mut inputs = Punctuated::new();
2183 loop {
2184 if input.peek(Token![|]) {
2185 break;
2186 }
2187 let value = fn_arg(input)?;
2188 inputs.push_value(value);
2189 if input.peek(Token![|]) {
2190 break;
2191 }
2192 let punct: Token![,] = input.parse()?;
2193 inputs.push_punct(punct);
2194 }
2195
2196 let or2_token: Token![|] = input.parse()?;
2197
2198 let (output, body) = if input.peek(Token![->]) {
2199 let arrow_token: Token![->] = input.parse()?;
2200 let ty: Type = input.parse()?;
2201 let body: Block = input.parse()?;
2202 let output = ReturnType::Type(arrow_token, Box::new(ty));
2203 let block = Expr::Block(ExprBlock {
2204 attrs: Vec::new(),
2205 label: None,
2206 block: body,
2207 });
2208 (output, block)
2209 } else {
2210 let body = ambiguous_expr(input, allow_struct, AllowBlock(true))?;
2211 (ReturnType::Default, body)
2212 };
2213
2214 Ok(ExprClosure {
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002215 attrs: attrs,
2216 asyncness: asyncness,
2217 movability: movability,
2218 capture: capture,
David Tolnay60291082018-08-28 09:54:49 -07002219 or1_token: or1_token,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002220 inputs: inputs,
David Tolnay60291082018-08-28 09:54:49 -07002221 or2_token: or2_token,
2222 output: output,
2223 body: Box::new(body),
2224 })
David Tolnay02a9c6f2018-08-24 18:58:45 -04002225 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002226
2227 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002228 impl Parse for ExprAsync {
2229 fn parse(input: ParseStream) -> Result<Self> {
2230 Ok(ExprAsync {
2231 attrs: input.call(Attribute::parse_outer)?,
2232 async_token: input.parse()?,
2233 capture: input.parse()?,
2234 block: input.parse()?,
2235 })
2236 }
2237 }
Gregory Katz3e562cc2016-09-28 18:33:02 -04002238
Michael Layzell734adb42017-06-07 16:58:31 -04002239 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002240 fn fn_arg(input: ParseStream) -> Result<FnArg> {
2241 let pat: Pat = input.parse()?;
2242
2243 if input.peek(Token![:]) {
2244 Ok(FnArg::Captured(ArgCaptured {
2245 pat: pat,
2246 colon_token: input.parse()?,
2247 ty: input.parse()?,
2248 }))
2249 } else {
2250 Ok(FnArg::Inferred(pat))
2251 }
2252 }
2253
2254 #[cfg(feature = "full")]
2255 impl Parse for ExprWhile {
2256 fn parse(input: ParseStream) -> Result<Self> {
2257 let outer_attrs = input.call(Attribute::parse_outer)?;
2258 let label: Option<Label> = input.parse()?;
2259 let while_token: Token![while] = input.parse()?;
2260 let cond = expr_no_struct(input)?;
2261
2262 let content;
2263 let brace_token = braced!(content in input);
2264 let inner_attrs = content.call(Attribute::parse_inner)?;
2265 let stmts = content.call(Block::parse_within)?;
2266
2267 Ok(ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002268 attrs: {
2269 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002270 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002271 attrs
2272 },
2273 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002274 while_token: while_token,
Michael Layzell92639a52017-06-01 00:07:44 -04002275 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002276 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002277 brace_token: brace_token,
2278 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002279 },
Michael Layzell92639a52017-06-01 00:07:44 -04002280 })
David Tolnay60291082018-08-28 09:54:49 -07002281 }
Alex Crichton954046c2017-05-30 21:49:42 -07002282 }
2283
Michael Layzell734adb42017-06-07 16:58:31 -04002284 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002285 impl Parse for ExprWhileLet {
2286 fn parse(input: ParseStream) -> Result<Self> {
2287 let outer_attrs = input.call(Attribute::parse_outer)?;
2288 let label: Option<Label> = input.parse()?;
2289 let while_token: Token![while] = input.parse()?;
2290 let let_token: Token![let] = input.parse()?;
2291
2292 let mut pats = Punctuated::new();
2293 let value: Pat = input.parse()?;
2294 pats.push_value(value);
2295 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2296 let punct = input.parse()?;
2297 pats.push_punct(punct);
2298 let value: Pat = input.parse()?;
2299 pats.push_value(value);
2300 }
2301
2302 let eq_token: Token![=] = input.parse()?;
2303 let expr = expr_no_struct(input)?;
2304
2305 let content;
2306 let brace_token = braced!(content in input);
2307 let inner_attrs = content.call(Attribute::parse_inner)?;
2308 let stmts = content.call(Block::parse_within)?;
2309
2310 Ok(ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002311 attrs: {
2312 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002313 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002314 attrs
2315 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002316 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002317 while_token: while_token,
2318 let_token: let_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002319 pats: pats,
David Tolnay60291082018-08-28 09:54:49 -07002320 eq_token: eq_token,
2321 expr: Box::new(expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07002322 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002323 brace_token: brace_token,
2324 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002325 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002326 })
David Tolnay60291082018-08-28 09:54:49 -07002327 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002328 }
2329
2330 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002331 impl Parse for Label {
2332 fn parse(input: ParseStream) -> Result<Self> {
2333 Ok(Label {
2334 name: input.parse()?,
2335 colon_token: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002336 })
David Tolnay60291082018-08-28 09:54:49 -07002337 }
Alex Crichton954046c2017-05-30 21:49:42 -07002338 }
2339
Michael Layzell734adb42017-06-07 16:58:31 -04002340 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002341 impl Parse for Option<Label> {
2342 fn parse(input: ParseStream) -> Result<Self> {
2343 if input.peek(Lifetime) {
2344 input.parse().map(Some)
2345 } else {
2346 Ok(None)
2347 }
2348 }
2349 }
2350
2351 #[cfg(feature = "full")]
2352 impl Parse for ExprContinue {
2353 fn parse(input: ParseStream) -> Result<Self> {
2354 Ok(ExprContinue {
2355 attrs: input.call(Attribute::parse_outer)?,
2356 continue_token: input.parse()?,
2357 label: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002358 })
David Tolnay60291082018-08-28 09:54:49 -07002359 }
Alex Crichton954046c2017-05-30 21:49:42 -07002360 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002361
Michael Layzell734adb42017-06-07 16:58:31 -04002362 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002363 fn expr_break(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprBreak> {
2364 Ok(ExprBreak {
2365 attrs: input.call(Attribute::parse_outer)?,
2366 break_token: input.parse()?,
2367 label: input.parse()?,
2368 expr: {
2369 if input.is_empty()
2370 || input.peek(Token![,])
2371 || input.peek(Token![;])
2372 || !allow_struct.0 && input.peek(token::Brace)
2373 {
2374 None
2375 } else {
2376 // We can't allow blocks after a `break` expression when we
2377 // wouldn't allow structs, as this expression is ambiguous.
2378 let allow_block = AllowBlock(allow_struct.0);
2379 let expr = ambiguous_expr(input, allow_struct, allow_block)?;
2380 Some(Box::new(expr))
Michael Layzell92639a52017-06-01 00:07:44 -04002381 }
David Tolnay60291082018-08-28 09:54:49 -07002382 },
2383 })
Alex Crichton954046c2017-05-30 21:49:42 -07002384 }
2385
Michael Layzell734adb42017-06-07 16:58:31 -04002386 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002387 fn expr_ret(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprReturn> {
2388 Ok(ExprReturn {
2389 attrs: input.call(Attribute::parse_outer)?,
2390 return_token: input.parse()?,
2391 expr: {
2392 if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) {
2393 None
2394 } else {
2395 // NOTE: return is greedy and eats blocks after it even when in a
2396 // position where structs are not allowed, such as in if statement
2397 // conditions. For example:
2398 //
2399 // if return { println!("A") } {} // Prints "A"
2400 let expr = ambiguous_expr(input, allow_struct, AllowBlock(true))?;
2401 Some(Box::new(expr))
2402 }
2403 },
2404 })
2405 }
2406
2407 #[cfg(feature = "full")]
2408 impl Parse for ExprStruct {
2409 fn parse(input: ParseStream) -> Result<Self> {
2410 let outer_attrs = input.call(Attribute::parse_outer)?;
2411 let path: Path = input.parse()?;
2412
David Tolnay6e1e5052018-08-30 10:21:48 -07002413 expr_struct_helper(input, outer_attrs, path)
2414 }
2415 }
David Tolnay60291082018-08-28 09:54:49 -07002416
David Tolnay6e1e5052018-08-30 10:21:48 -07002417 #[cfg(feature = "full")]
2418 fn expr_struct_helper(input: ParseStream, outer_attrs: Vec<Attribute>, path: Path) -> Result<ExprStruct> {
2419 let content;
2420 let brace_token = braced!(content in input);
2421 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay60291082018-08-28 09:54:49 -07002422
David Tolnay6e1e5052018-08-30 10:21:48 -07002423 let mut fields = Punctuated::new();
2424 loop {
2425 let attrs = content.call(Attribute::parse_outer)?;
2426 if content.fork().parse::<Member>().is_err() {
2427 if attrs.is_empty() {
David Tolnay60291082018-08-28 09:54:49 -07002428 break;
David Tolnay6e1e5052018-08-30 10:21:48 -07002429 } else {
2430 return Err(content.error("expected struct field"));
David Tolnay60291082018-08-28 09:54:49 -07002431 }
David Tolnay60291082018-08-28 09:54:49 -07002432 }
2433
David Tolnay6e1e5052018-08-30 10:21:48 -07002434 let member: Member = content.parse()?;
2435 let (colon_token, value) = if content.peek(Token![:]) || !member.is_named() {
2436 let colon_token: Token![:] = content.parse()?;
2437 let value: Expr = content.parse()?;
2438 (Some(colon_token), value)
2439 } else if let Member::Named(ref ident) = member {
2440 let value = Expr::Path(ExprPath {
2441 attrs: Vec::new(),
2442 qself: None,
2443 path: Path::from(ident.clone()),
2444 });
2445 (None, value)
David Tolnay60291082018-08-28 09:54:49 -07002446 } else {
David Tolnay6e1e5052018-08-30 10:21:48 -07002447 unreachable!()
David Tolnay60291082018-08-28 09:54:49 -07002448 };
2449
David Tolnay6e1e5052018-08-30 10:21:48 -07002450 fields.push(FieldValue {
2451 attrs: attrs,
2452 member: member,
2453 colon_token: colon_token,
2454 expr: value,
2455 });
2456
2457 if !content.peek(Token![,]) {
2458 break;
2459 }
2460 let punct: Token![,] = content.parse()?;
2461 fields.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002462 }
David Tolnay6e1e5052018-08-30 10:21:48 -07002463
2464 let (dot2_token, rest) = if fields.empty_or_trailing() && content.peek(Token![..]) {
2465 let dot2_token: Token![..] = content.parse()?;
2466 let rest: Expr = content.parse()?;
2467 (Some(dot2_token), Some(Box::new(rest)))
2468 } else {
2469 (None, None)
2470 };
2471
2472 Ok(ExprStruct {
2473 attrs: {
2474 let mut attrs = outer_attrs;
2475 attrs.extend(inner_attrs);
2476 attrs
2477 },
2478 brace_token: brace_token,
2479 path: path,
2480 fields: fields,
2481 dot2_token: dot2_token,
2482 rest: rest,
2483 })
Alex Crichton954046c2017-05-30 21:49:42 -07002484 }
David Tolnay055a7042016-10-02 19:23:54 -07002485
Michael Layzell734adb42017-06-07 16:58:31 -04002486 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002487 impl Parse for ExprRepeat {
2488 fn parse(input: ParseStream) -> Result<Self> {
2489 let outer_attrs = input.call(Attribute::parse_outer)?;
2490
2491 let content;
2492 let bracket_token = bracketed!(content in input);
2493 let inner_attrs = content.call(Attribute::parse_inner)?;
2494 let expr: Expr = content.parse()?;
2495 let semi_token: Token![;] = content.parse()?;
2496 let len: Expr = content.parse()?;
2497
2498 Ok(ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002499 attrs: {
2500 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002501 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002502 attrs
2503 },
David Tolnay60291082018-08-28 09:54:49 -07002504 bracket_token: bracket_token,
2505 expr: Box::new(expr),
2506 semi_token: semi_token,
2507 len: Box::new(len),
Michael Layzell92639a52017-06-01 00:07:44 -04002508 })
David Tolnay60291082018-08-28 09:54:49 -07002509 }
Alex Crichton954046c2017-05-30 21:49:42 -07002510 }
David Tolnay055a7042016-10-02 19:23:54 -07002511
Michael Layzell734adb42017-06-07 16:58:31 -04002512 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002513 impl Parse for ExprUnsafe {
2514 fn parse(input: ParseStream) -> Result<Self> {
2515 let outer_attrs = input.call(Attribute::parse_outer)?;
2516 let unsafe_token: Token![unsafe] = input.parse()?;
2517
2518 let content;
2519 let brace_token = braced!(content in input);
2520 let inner_attrs = content.call(Attribute::parse_inner)?;
2521 let stmts = content.call(Block::parse_within)?;
2522
2523 Ok(ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002524 attrs: {
2525 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002526 attrs.extend(inner_attrs);
David Tolnayc4be3512018-08-27 06:25:44 -07002527 attrs
2528 },
David Tolnay60291082018-08-28 09:54:49 -07002529 unsafe_token: unsafe_token,
David Tolnayc4be3512018-08-27 06:25:44 -07002530 block: Block {
David Tolnay60291082018-08-28 09:54:49 -07002531 brace_token: brace_token,
2532 stmts: stmts,
David Tolnayc4be3512018-08-27 06:25:44 -07002533 },
Nika Layzell640832a2017-12-04 13:37:09 -05002534 })
David Tolnay60291082018-08-28 09:54:49 -07002535 }
Nika Layzell640832a2017-12-04 13:37:09 -05002536 }
2537
2538 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002539 impl Parse for ExprBlock {
2540 fn parse(input: ParseStream) -> Result<Self> {
2541 let outer_attrs = input.call(Attribute::parse_outer)?;
2542 let label: Option<Label> = input.parse()?;
2543
2544 let content;
2545 let brace_token = braced!(content in input);
2546 let inner_attrs = content.call(Attribute::parse_inner)?;
2547 let stmts = content.call(Block::parse_within)?;
2548
2549 Ok(ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002550 attrs: {
2551 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002552 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002553 attrs
2554 },
David Tolnay1d8e9962018-08-24 19:04:20 -04002555 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002556 block: Block {
David Tolnay60291082018-08-28 09:54:49 -07002557 brace_token: brace_token,
2558 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002559 },
Michael Layzell92639a52017-06-01 00:07:44 -04002560 })
David Tolnay60291082018-08-28 09:54:49 -07002561 }
Alex Crichton954046c2017-05-30 21:49:42 -07002562 }
David Tolnay89e05672016-10-02 14:39:42 -07002563
Michael Layzell734adb42017-06-07 16:58:31 -04002564 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002565 fn expr_range(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprRange> {
2566 Ok(ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05002567 attrs: Vec::new(),
2568 from: None,
David Tolnay60291082018-08-28 09:54:49 -07002569 limits: input.parse()?,
2570 to: {
2571 if input.is_empty()
2572 || input.peek(Token![,])
2573 || input.peek(Token![;])
2574 || !allow_struct.0 && input.peek(token::Brace)
2575 {
2576 None
2577 } else {
2578 let to = ambiguous_expr(input, allow_struct, AllowBlock(allow_struct.0))?;
2579 Some(Box::new(to))
2580 }
2581 },
2582 })
2583 }
David Tolnay438c9052016-10-07 23:24:48 -07002584
Michael Layzell734adb42017-06-07 16:58:31 -04002585 #[cfg(feature = "full")]
David Tolnayc4c631e2018-08-27 10:44:37 -07002586 impl Parse for RangeLimits {
2587 fn parse(input: ParseStream) -> Result<Self> {
2588 let lookahead = input.lookahead1();
2589 if lookahead.peek(Token![..=]) {
2590 input.parse().map(RangeLimits::Closed)
2591 } else if lookahead.peek(Token![...]) {
2592 let dot3: Token![...] = input.parse()?;
2593 Ok(RangeLimits::Closed(Token![..=](dot3.spans)))
2594 } else if lookahead.peek(Token![..]) {
2595 input.parse().map(RangeLimits::HalfOpen)
2596 } else {
2597 Err(lookahead.error())
2598 }
2599 }
Alex Crichton954046c2017-05-30 21:49:42 -07002600 }
David Tolnay438c9052016-10-07 23:24:48 -07002601
David Tolnay60291082018-08-28 09:54:49 -07002602 impl Parse for ExprPath {
2603 fn parse(input: ParseStream) -> Result<Self> {
2604 #[cfg(not(feature = "full"))]
2605 let attrs = Vec::new();
2606 #[cfg(feature = "full")]
2607 let attrs = input.call(Attribute::parse_outer)?;
David Tolnayeb981bb2018-07-21 19:31:38 -07002608
David Tolnay60291082018-08-28 09:54:49 -07002609 let (qself, path) = path::parsing::qpath(input, true)?;
2610
2611 Ok(ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002612 attrs: attrs,
David Tolnay60291082018-08-28 09:54:49 -07002613 qself: qself,
2614 path: path,
Michael Layzell92639a52017-06-01 00:07:44 -04002615 })
David Tolnay60291082018-08-28 09:54:49 -07002616 }
Alex Crichton954046c2017-05-30 21:49:42 -07002617 }
David Tolnay42602292016-10-01 22:25:45 -07002618
Michael Layzell734adb42017-06-07 16:58:31 -04002619 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002620 impl Parse for Block {
2621 fn parse(input: ParseStream) -> Result<Self> {
2622 let content;
2623 Ok(Block {
2624 brace_token: braced!(content in input),
2625 stmts: content.call(Block::parse_within)?,
Michael Layzell92639a52017-06-01 00:07:44 -04002626 })
David Tolnay60291082018-08-28 09:54:49 -07002627 }
Alex Crichton954046c2017-05-30 21:49:42 -07002628 }
David Tolnay939766a2016-09-23 23:48:12 -07002629
Michael Layzell734adb42017-06-07 16:58:31 -04002630 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002631 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002632 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
2633 input.step_cursor(|cursor| Self::old_parse_within(*cursor))
2634 }
2635
2636 named!(old_parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002637 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002638 mut standalone: many0!(do_parse!(
2639 stmt: syn!(Stmt) >>
2640 many0!(punct!(;)) >>
2641 (stmt)
2642 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002643 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002644 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002645 mut e: syn!(Expr) >>
2646 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002647 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002648 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002649 })
2650 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002651 (match last {
2652 None => standalone,
2653 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002654 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002655 standalone
2656 }
2657 })
2658 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002659 }
2660
Michael Layzell734adb42017-06-07 16:58:31 -04002661 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002662 impl Parse for Stmt {
2663 fn parse(input: ParseStream) -> Result<Self> {
2664 let ahead = input.fork();
2665 ahead.call(Attribute::parse_outer)?;
David Tolnay939766a2016-09-23 23:48:12 -07002666
David Tolnay60291082018-08-28 09:54:49 -07002667 // TODO: better error messages
2668 if {
2669 let ahead = ahead.fork();
2670 // Only parse braces here; paren and bracket will get parsed as
2671 // expression statements
2672 ahead.call(Path::parse_mod_style).is_ok()
2673 && ahead.parse::<Token![!]>().is_ok()
2674 && (ahead.peek(token::Brace) || ahead.peek(Ident))
2675 } {
2676 stmt_mac(input)
2677 } else if ahead.peek(Token![let]) {
2678 stmt_local(input).map(Stmt::Local)
2679 } else if ahead.peek(Token![pub])
2680 || ahead.peek(Token![crate]) && !ahead.peek2(Token![::])
2681 || ahead.peek(Token![extern]) && !ahead.peek2(Token![::])
2682 || ahead.peek(Token![use])
2683 || ahead.peek(Token![static]) && (ahead.peek2(Token![mut]) || ahead.peek2(Ident))
2684 || ahead.peek(Token![const])
2685 || ahead.peek(Token![unsafe]) && !ahead.peek2(token::Brace)
2686 || ahead.peek(Token![async]) && (ahead.peek2(Token![extern]) || ahead.peek2(Token![fn]))
2687 || ahead.peek(Token![fn])
2688 || ahead.peek(Token![mod])
2689 || ahead.peek(Token![type])
2690 || ahead.peek(Token![existential]) && ahead.peek2(Token![type])
2691 || ahead.peek(Token![struct])
2692 || ahead.peek(Token![enum])
2693 || ahead.peek(Token![union]) && ahead.peek2(Ident)
2694 || ahead.peek(Token![auto]) && ahead.peek2(Token![trait])
2695 || ahead.peek(Token![trait])
2696 || ahead.peek(Token![default]) && (ahead.peek2(Token![unsafe]) || ahead.peek2(Token![impl]))
2697 || ahead.peek(Token![impl])
2698 || ahead.peek(Token![macro])
2699 {
2700 input.parse().map(Stmt::Item)
Michael Layzell35418782017-06-07 09:20:25 -04002701 } else {
David Tolnay01218d12018-08-29 18:13:07 -07002702 input.call(stmt_expr)
Michael Layzell35418782017-06-07 09:20:25 -04002703 }
David Tolnay60291082018-08-28 09:54:49 -07002704 }
Alex Crichton954046c2017-05-30 21:49:42 -07002705 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002706
Michael Layzell734adb42017-06-07 16:58:31 -04002707 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002708 fn stmt_mac(input: ParseStream) -> Result<Stmt> {
2709 let attrs = input.call(Attribute::parse_outer)?;
2710 let path = input.call(Path::parse_mod_style)?;
2711 let bang_token: Token![!] = input.parse()?;
2712 let ident: Option<Ident> = input.parse()?;
2713 let (delimiter, tts) = mac::parse_delimiter(input)?;
2714 let semi_token: Option<Token![;]> = input.parse()?;
2715
2716 Ok(Stmt::Item(Item::Macro(ItemMacro {
2717 attrs: attrs,
2718 ident: ident,
2719 mac: Macro {
2720 path: path,
2721 bang_token: bang_token,
2722 delimiter: delimiter,
2723 tts: tts,
2724 },
2725 semi_token: semi_token,
2726 })))
Alex Crichton954046c2017-05-30 21:49:42 -07002727 }
David Tolnay84aa0752016-10-02 23:01:13 -07002728
Michael Layzell734adb42017-06-07 16:58:31 -04002729 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002730 fn stmt_local(input: ParseStream) -> Result<Local> {
2731 Ok(Local {
2732 attrs: input.call(Attribute::parse_outer)?,
2733 let_token: input.parse()?,
2734 pats: {
2735 let mut pats = Punctuated::new();
2736 let value: Pat = input.parse()?;
2737 pats.push_value(value);
2738 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2739 let punct = input.parse()?;
2740 pats.push_punct(punct);
2741 let value: Pat = input.parse()?;
2742 pats.push_value(value);
2743 }
2744 pats
2745 },
2746 ty: {
2747 if input.peek(Token![:]) {
2748 let colon_token: Token![:] = input.parse()?;
2749 let ty: Type = input.parse()?;
2750 Some((colon_token, Box::new(ty)))
2751 } else {
2752 None
2753 }
2754 },
2755 init: {
2756 if input.peek(Token![=]) {
2757 let eq_token: Token![=] = input.parse()?;
2758 let init: Expr = input.parse()?;
2759 Some((eq_token, Box::new(init)))
2760 } else {
2761 None
2762 }
2763 },
2764 semi_token: input.parse()?,
2765 })
2766 }
2767
2768 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07002769 fn stmt_expr(input: ParseStream) -> Result<Stmt> {
David Tolnay60291082018-08-28 09:54:49 -07002770 let mut attrs = input.call(Attribute::parse_outer)?;
David Tolnay01218d12018-08-29 18:13:07 -07002771 let mut e = expr_early(input)?;
David Tolnay60291082018-08-28 09:54:49 -07002772
2773 attrs.extend(e.replace_attrs(Vec::new()));
2774 e.replace_attrs(attrs);
2775
2776 if input.peek(Token![;]) {
David Tolnay01218d12018-08-29 18:13:07 -07002777 return Ok(Stmt::Semi(e, input.parse()?));
David Tolnay60291082018-08-28 09:54:49 -07002778 }
David Tolnay60291082018-08-28 09:54:49 -07002779
David Tolnay01218d12018-08-29 18:13:07 -07002780 match e {
2781 Expr::IfLet(_) |
2782 Expr::If(_) |
2783 Expr::WhileLet(_) |
2784 Expr::While(_) |
2785 Expr::ForLoop(_) |
2786 Expr::Loop(_) |
2787 Expr::Match(_) |
2788 Expr::TryBlock(_) |
2789 Expr::Yield(_) |
2790 Expr::Unsafe(_) |
2791 Expr::Block(_) => Ok(Stmt::Expr(e)),
2792 _ => {
2793 Err(input.error("expected semicolon"))
2794 }
2795 }
David Tolnay60291082018-08-28 09:54:49 -07002796 }
2797
2798 #[cfg(feature = "full")]
2799 impl Parse for Pat {
2800 fn parse(input: ParseStream) -> Result<Self> {
2801 // TODO: better error messages
2802 let lookahead = input.lookahead1();
2803 if lookahead.peek(Token![_]) {
2804 input.parse().map(Pat::Wild)
2805 } else if lookahead.peek(Token![box]) {
2806 input.parse().map(Pat::Box)
2807 } else if input.fork().parse::<PatRange>().is_ok() {
2808 // must be before Pat::Lit
2809 input.parse().map(Pat::Range)
2810 } else if input.fork().parse::<PatTupleStruct>().is_ok() {
2811 // must be before Pat::Ident
2812 input.parse().map(Pat::TupleStruct)
2813 } else if input.fork().parse::<PatStruct>().is_ok() {
2814 // must be before Pat::Ident
2815 input.parse().map(Pat::Struct)
2816 } else if input.fork().parse::<PatMacro>().is_ok() {
2817 // must be before Pat::Ident
2818 input.parse().map(Pat::Macro)
2819 } else if input.fork().parse::<PatLit>().is_ok() {
2820 // must be before Pat::Ident
2821 input.parse().map(Pat::Lit)
2822 } else if input.fork().parse::<PatIdent>().is_ok() {
2823 input.parse().map(Pat::Ident)
2824 } else if input.fork().parse::<PatPath>().is_ok() {
2825 input.parse().map(Pat::Path)
2826 } else if lookahead.peek(token::Paren) {
2827 input.parse().map(Pat::Tuple)
2828 } else if lookahead.peek(Token![&]) {
2829 input.parse().map(Pat::Ref)
2830 } else if lookahead.peek(token::Bracket) {
2831 input.parse().map(Pat::Slice)
2832 } else {
2833 Err(lookahead.error())
2834 }
2835 }
2836 }
2837
2838 #[cfg(feature = "full")]
2839 impl Parse for PatWild {
2840 fn parse(input: ParseStream) -> Result<Self> {
2841 Ok(PatWild {
2842 underscore_token: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002843 })
David Tolnay60291082018-08-28 09:54:49 -07002844 }
Alex Crichton954046c2017-05-30 21:49:42 -07002845 }
2846
Michael Layzell734adb42017-06-07 16:58:31 -04002847 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002848 impl Parse for PatBox {
2849 fn parse(input: ParseStream) -> Result<Self> {
2850 Ok(PatBox {
2851 box_token: input.parse()?,
2852 pat: input.parse()?,
2853 })
2854 }
2855 }
2856
2857 #[cfg(feature = "full")]
2858 impl Parse for PatIdent {
2859 fn parse(input: ParseStream) -> Result<Self> {
2860 Ok(PatIdent {
2861 by_ref: input.parse()?,
2862 mutability: input.parse()?,
2863 ident: {
2864 let ident = if input.peek(Ident) || input.peek(Token![self]) {
2865 input.call(Ident::parse_any2)?
2866 } else {
2867 return Err(input.error("expected identifier or `self`"));
2868 };
2869 if input.peek(Token![<]) || input.peek(Token![::]) {
2870 return Err(input.error("unexpected token"));
2871 }
2872 ident
2873 },
2874 subpat: {
2875 if input.peek(Token![@]) {
2876 let at_token: Token![@] = input.parse()?;
2877 let subpat: Pat = input.parse()?;
2878 Some((at_token, Box::new(subpat)))
2879 } else {
2880 None
2881 }
2882 },
2883 })
2884 }
2885 }
2886
2887 #[cfg(feature = "full")]
2888 impl Parse for PatTupleStruct {
2889 fn parse(input: ParseStream) -> Result<Self> {
2890 Ok(PatTupleStruct {
2891 path: input.parse()?,
2892 pat: input.parse()?,
2893 })
2894 }
2895 }
2896
2897 #[cfg(feature = "full")]
2898 impl Parse for PatStruct {
2899 fn parse(input: ParseStream) -> Result<Self> {
2900 let path: Path = input.parse()?;
2901
2902 let content;
2903 let brace_token = braced!(content in input);
2904
2905 let mut fields = Punctuated::new();
2906 while !content.is_empty() && !content.peek(Token![..]) {
2907 let value: FieldPat = content.parse()?;
2908 fields.push_value(value);
2909 if !content.peek(Token![,]) {
2910 break;
2911 }
2912 let punct: Token![,] = content.parse()?;
2913 fields.push_punct(punct);
2914 }
2915
2916 let dot2_token = if fields.empty_or_trailing() && content.peek(Token![..]) {
2917 Some(content.parse()?)
2918 } else {
2919 None
2920 };
2921
2922 Ok(PatStruct {
2923 path: path,
2924 brace_token: brace_token,
2925 fields: fields,
2926 dot2_token: dot2_token,
2927 })
2928 }
2929 }
2930
2931 #[cfg(feature = "full")]
2932 impl Parse for FieldPat {
2933 fn parse(input: ParseStream) -> Result<Self> {
2934 let boxed: Option<Token![box]> = input.parse()?;
2935 let by_ref: Option<Token![ref]> = input.parse()?;
2936 let mutability: Option<Token![mut]> = input.parse()?;
2937 let member: Member = input.parse()?;
2938
2939 if boxed.is_none() && by_ref.is_none() && mutability.is_none() && input.peek(Token![:])
2940 || member.is_unnamed()
2941 {
2942 return Ok(FieldPat {
2943 attrs: Vec::new(),
2944 member: member,
2945 colon_token: input.parse()?,
2946 pat: input.parse()?,
2947 });
2948 }
2949
2950 let ident = match member {
2951 Member::Named(ident) => ident,
2952 Member::Unnamed(_) => unreachable!(),
2953 };
2954
2955 let mut pat = Pat::Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002956 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002957 mutability: mutability,
David Tolnay60291082018-08-28 09:54:49 -07002958 ident: ident.clone(),
2959 subpat: None,
2960 });
Alex Crichton954046c2017-05-30 21:49:42 -07002961
David Tolnay60291082018-08-28 09:54:49 -07002962 if let Some(boxed) = boxed {
2963 pat = Pat::Box(PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002964 pat: Box::new(pat),
David Tolnay60291082018-08-28 09:54:49 -07002965 box_token: boxed,
2966 });
2967 }
2968
2969 Ok(FieldPat {
2970 member: Member::Named(ident),
2971 pat: Box::new(pat),
2972 attrs: Vec::new(),
2973 colon_token: None,
2974 })
2975 }
Alex Crichton954046c2017-05-30 21:49:42 -07002976 }
2977
David Tolnay1501f7e2018-08-27 14:21:03 -07002978 impl Parse for Member {
2979 fn parse(input: ParseStream) -> Result<Self> {
2980 if input.peek(Ident) {
2981 input.parse().map(Member::Named)
2982 } else if input.peek(LitInt) {
2983 input.parse().map(Member::Unnamed)
2984 } else {
2985 Err(input.error("expected identifier or integer"))
2986 }
2987 }
David Tolnay85b69a42017-12-27 20:43:10 -05002988 }
2989
David Tolnay1501f7e2018-08-27 14:21:03 -07002990 impl Parse for Index {
2991 fn parse(input: ParseStream) -> Result<Self> {
2992 let lit: LitInt = input.parse()?;
2993 if let IntSuffix::None = lit.suffix() {
2994 Ok(Index {
2995 index: lit.value() as u32,
2996 span: lit.span(),
2997 })
2998 } else {
2999 Err(input.error("expected unsuffixed integer"))
3000 }
3001 }
David Tolnay85b69a42017-12-27 20:43:10 -05003002 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003003
Michael Layzell734adb42017-06-07 16:58:31 -04003004 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003005 impl Parse for PatPath {
3006 fn parse(input: ParseStream) -> Result<Self> {
3007 let p: ExprPath = input.parse()?;
3008 Ok(PatPath {
3009 qself: p.qself,
3010 path: p.path,
3011 })
3012 }
Alex Crichton954046c2017-05-30 21:49:42 -07003013 }
David Tolnay9636c052016-10-02 17:11:17 -07003014
Michael Layzell734adb42017-06-07 16:58:31 -04003015 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003016 impl Parse for PatTuple {
3017 fn parse(input: ParseStream) -> Result<Self> {
3018 let content;
3019 let paren_token = parenthesized!(content in input);
3020
3021 let mut front = Punctuated::new();
3022 let mut dot2_token = None::<Token![..]>;
3023 let mut comma_token = None::<Token![,]>;
3024 loop {
3025 if content.is_empty() {
3026 break;
Michael Layzell92639a52017-06-01 00:07:44 -04003027 }
David Tolnay60291082018-08-28 09:54:49 -07003028 if content.peek(Token![..]) {
3029 dot2_token = Some(content.parse()?);
3030 comma_token = content.parse()?;
3031 break;
3032 }
3033 let value: Pat = content.parse()?;
3034 front.push_value(value);
3035 if content.is_empty() {
3036 break;
3037 }
3038 let punct = content.parse()?;
3039 front.push_punct(punct);
3040 }
David Tolnayfbb73232016-10-03 01:00:06 -07003041
David Tolnay60291082018-08-28 09:54:49 -07003042 let back = if comma_token.is_some() {
3043 content.parse_synom(Punctuated::parse_terminated)?
Michael Layzell92639a52017-06-01 00:07:44 -04003044 } else {
David Tolnay60291082018-08-28 09:54:49 -07003045 Punctuated::new()
3046 };
3047
3048 Ok(PatTuple {
3049 paren_token: paren_token,
3050 front: front,
3051 dot2_token: dot2_token,
3052 comma_token: comma_token,
3053 back: back,
Michael Layzell92639a52017-06-01 00:07:44 -04003054 })
David Tolnay60291082018-08-28 09:54:49 -07003055 }
Alex Crichton954046c2017-05-30 21:49:42 -07003056 }
David Tolnaye1310902016-10-29 23:40:00 -07003057
Michael Layzell734adb42017-06-07 16:58:31 -04003058 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003059 impl Parse for PatRef {
3060 fn parse(input: ParseStream) -> Result<Self> {
3061 Ok(PatRef {
3062 and_token: input.parse()?,
3063 mutability: input.parse()?,
3064 pat: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04003065 })
David Tolnay60291082018-08-28 09:54:49 -07003066 }
Alex Crichton954046c2017-05-30 21:49:42 -07003067 }
David Tolnaye1310902016-10-29 23:40:00 -07003068
Michael Layzell734adb42017-06-07 16:58:31 -04003069 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003070 impl Parse for PatLit {
3071 fn parse(input: ParseStream) -> Result<Self> {
3072 if input.peek(Lit) || input.peek(Token![-]) && input.peek2(Lit) {
3073 Ok(PatLit {
3074 expr: input.call(pat_lit_expr)?,
3075 })
3076 } else {
3077 Err(input.error("expected literal pattern"))
3078 }
3079 }
3080 }
3081
3082 #[cfg(feature = "full")]
3083 impl Parse for PatRange {
3084 fn parse(input: ParseStream) -> Result<Self> {
3085 Ok(PatRange {
3086 lo: input.call(pat_lit_expr)?,
3087 limits: input.parse()?,
3088 hi: input.call(pat_lit_expr)?,
3089 })
3090 }
3091 }
3092
3093 #[cfg(feature = "full")]
3094 fn pat_lit_expr(input: ParseStream) -> Result<Box<Expr>> {
3095 let neg: Option<Token![-]> = input.parse()?;
3096
3097 let lookahead = input.lookahead1();
3098 let expr = if lookahead.peek(Lit) {
3099 Expr::Lit(input.parse()?)
3100 } else if lookahead.peek(Ident)
3101 || lookahead.peek(Token![::])
3102 || lookahead.peek(Token![<])
3103 || lookahead.peek(Token![self])
3104 || lookahead.peek(Token![Self])
3105 || lookahead.peek(Token![super])
3106 || lookahead.peek(Token![extern])
3107 || lookahead.peek(Token![crate])
3108 {
3109 Expr::Path(input.parse()?)
3110 } else {
3111 return Err(lookahead.error());
3112 };
3113
3114 Ok(Box::new(if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003115 Expr::Unary(ExprUnary {
3116 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003117 op: UnOp::Neg(neg),
David Tolnay60291082018-08-28 09:54:49 -07003118 expr: Box::new(expr),
David Tolnay3bc597f2017-12-31 02:31:11 -05003119 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003120 } else {
David Tolnay60291082018-08-28 09:54:49 -07003121 expr
3122 }))
Alex Crichton954046c2017-05-30 21:49:42 -07003123 }
David Tolnay323279a2017-12-29 11:26:32 -05003124
3125 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003126 impl Parse for PatSlice {
3127 fn parse(input: ParseStream) -> Result<Self> {
3128 let content;
3129 let bracket_token = bracketed!(content in input);
3130
3131 let mut front = Punctuated::new();
3132 let mut middle = None;
3133 loop {
3134 if content.is_empty() || content.peek(Token![..]) {
3135 break;
3136 }
3137 let value: Pat = content.parse()?;
3138 if content.peek(Token![..]) {
3139 middle = Some(Box::new(value));
3140 break;
3141 }
3142 front.push_value(value);
3143 if content.is_empty() {
3144 break;
3145 }
3146 let punct = content.parse()?;
3147 front.push_punct(punct);
3148 }
3149
3150 let dot2_token: Option<Token![..]> = content.parse()?;
3151 let mut comma_token = None::<Token![,]>;
3152 let mut back = Punctuated::new();
3153 if dot2_token.is_some() {
3154 comma_token = content.parse()?;
3155 if comma_token.is_some() {
3156 loop {
3157 if content.is_empty() {
3158 break;
3159 }
3160 let value: Pat = content.parse()?;
3161 back.push_value(value);
3162 if content.is_empty() {
3163 break;
3164 }
3165 let punct = content.parse()?;
3166 back.push_punct(punct);
3167 }
3168 }
3169 }
3170
3171 Ok(PatSlice {
3172 bracket_token: bracket_token,
3173 front: front,
3174 middle: middle,
3175 dot2_token: dot2_token,
3176 comma_token: comma_token,
3177 back: back,
3178 })
3179 }
3180 }
3181
3182 #[cfg(feature = "full")]
3183 impl Parse for PatMacro {
3184 fn parse(input: ParseStream) -> Result<Self> {
3185 Ok(PatMacro {
3186 mac: input.parse()?,
3187 })
3188 }
David Tolnay323279a2017-12-29 11:26:32 -05003189 }
David Tolnay1501f7e2018-08-27 14:21:03 -07003190
3191 #[cfg(feature = "full")]
3192 impl Member {
3193 fn is_named(&self) -> bool {
3194 match *self {
3195 Member::Named(_) => true,
3196 Member::Unnamed(_) => false,
3197 }
3198 }
David Tolnay60291082018-08-28 09:54:49 -07003199
3200 fn is_unnamed(&self) -> bool {
3201 match *self {
3202 Member::Named(_) => false,
3203 Member::Unnamed(_) => true,
3204 }
3205 }
David Tolnay1501f7e2018-08-27 14:21:03 -07003206 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003207}
3208
David Tolnayf4bbbd92016-09-23 14:41:55 -07003209#[cfg(feature = "printing")]
3210mod printing {
3211 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003212 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003213 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003214 use proc_macro2::{Literal, TokenStream};
3215 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003216
David Tolnaybcf26022017-12-25 22:10:52 -05003217 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003218 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003219 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003220 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003221 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003222 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003223 e.to_tokens(tokens);
3224 });
3225 } else {
3226 e.to_tokens(tokens);
3227 }
3228 }
3229
David Tolnay8c91b882017-12-28 23:04:32 -05003230 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003231 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003232 tokens.append_all(attrs.outer());
3233 }
Michael Layzell734adb42017-06-07 16:58:31 -04003234
David Tolnayd997aef2018-07-21 18:42:31 -07003235 #[cfg(feature = "full")]
3236 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3237 tokens.append_all(attrs.inner());
3238 }
3239
David Tolnay8c91b882017-12-28 23:04:32 -05003240 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003241 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3242
3243 #[cfg(not(feature = "full"))]
3244 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003245
Michael Layzell734adb42017-06-07 16:58:31 -04003246 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003247 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003248 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003249 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003250 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003251 self.expr.to_tokens(tokens);
3252 }
3253 }
3254
Michael Layzell734adb42017-06-07 16:58:31 -04003255 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003256 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003257 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003258 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003259 self.place.to_tokens(tokens);
3260 self.arrow_token.to_tokens(tokens);
3261 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003262 }
3263 }
3264
Michael Layzell734adb42017-06-07 16:58:31 -04003265 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003266 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003267 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003268 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003269 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003270 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003271 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003272 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003273 }
3274 }
3275
3276 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003277 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003278 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003279 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003280 self.paren_token.surround(tokens, |tokens| {
3281 self.args.to_tokens(tokens);
3282 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003283 }
3284 }
3285
Michael Layzell734adb42017-06-07 16:58:31 -04003286 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003287 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003288 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003289 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003290 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003291 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003292 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003293 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003294 self.paren_token.surround(tokens, |tokens| {
3295 self.args.to_tokens(tokens);
3296 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003297 }
3298 }
3299
Michael Layzell734adb42017-06-07 16:58:31 -04003300 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003301 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003302 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003303 self.colon2_token.to_tokens(tokens);
3304 self.lt_token.to_tokens(tokens);
3305 self.args.to_tokens(tokens);
3306 self.gt_token.to_tokens(tokens);
3307 }
3308 }
3309
3310 #[cfg(feature = "full")]
3311 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003312 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003313 match *self {
3314 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3315 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3316 }
3317 }
3318 }
3319
3320 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003321 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003322 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003323 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003324 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003325 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003326 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003327 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003328 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003329 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003330 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003331 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003332 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003333 }
3334 }
3335
3336 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003337 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003338 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003339 self.left.to_tokens(tokens);
3340 self.op.to_tokens(tokens);
3341 self.right.to_tokens(tokens);
3342 }
3343 }
3344
3345 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003346 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003347 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003348 self.op.to_tokens(tokens);
3349 self.expr.to_tokens(tokens);
3350 }
3351 }
3352
David Tolnay8c91b882017-12-28 23:04:32 -05003353 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003354 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003355 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003356 self.lit.to_tokens(tokens);
3357 }
3358 }
3359
Alex Crichton62a0a592017-05-22 13:58:53 -07003360 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003361 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003362 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003363 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003364 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003365 self.ty.to_tokens(tokens);
3366 }
3367 }
3368
David Tolnay0cf94f22017-12-28 23:46:26 -05003369 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003370 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003371 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003372 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003373 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003374 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003375 self.ty.to_tokens(tokens);
3376 }
3377 }
3378
Michael Layzell734adb42017-06-07 16:58:31 -04003379 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003380 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003381 if let Some((ref else_token, ref else_)) = *else_ {
3382 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003383
3384 // If we are not one of the valid expressions to exist in an else
3385 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003386 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003387 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003388 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003389 }
3390 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003391 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003392 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003393 });
3394 }
3395 }
3396 }
3397 }
3398
3399 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003400 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003401 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003402 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003403 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003404 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003405 self.then_branch.to_tokens(tokens);
3406 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003407 }
3408 }
3409
Michael Layzell734adb42017-06-07 16:58:31 -04003410 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003411 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003412 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003413 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003414 self.if_token.to_tokens(tokens);
3415 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003416 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003417 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003418 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003419 self.then_branch.to_tokens(tokens);
3420 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003421 }
3422 }
3423
Michael Layzell734adb42017-06-07 16:58:31 -04003424 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003425 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003426 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003427 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003428 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003429 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003430 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003431 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003432 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003433 tokens.append_all(&self.body.stmts);
3434 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003435 }
3436 }
3437
Michael Layzell734adb42017-06-07 16:58:31 -04003438 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003439 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003440 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003441 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003442 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003443 self.while_token.to_tokens(tokens);
3444 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003445 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003446 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003447 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003448 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003449 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003450 tokens.append_all(&self.body.stmts);
3451 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003452 }
3453 }
3454
Michael Layzell734adb42017-06-07 16:58:31 -04003455 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003456 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003457 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003458 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003459 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003460 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003461 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003462 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003463 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003464 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003465 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003466 tokens.append_all(&self.body.stmts);
3467 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003468 }
3469 }
3470
Michael Layzell734adb42017-06-07 16:58:31 -04003471 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003472 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003473 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003474 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003475 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003476 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003477 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003478 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003479 tokens.append_all(&self.body.stmts);
3480 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003481 }
3482 }
3483
Michael Layzell734adb42017-06-07 16:58:31 -04003484 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003485 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003486 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003487 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003488 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003489 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003490 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003491 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003492 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003493 arm.to_tokens(tokens);
3494 // Ensure that we have a comma after a non-block arm, except
3495 // for the last one.
3496 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003497 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003498 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003499 }
3500 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003501 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003502 }
3503 }
3504
Michael Layzell734adb42017-06-07 16:58:31 -04003505 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003506 impl ToTokens for ExprAsync {
3507 fn to_tokens(&self, tokens: &mut TokenStream) {
3508 outer_attrs_to_tokens(&self.attrs, tokens);
3509 self.async_token.to_tokens(tokens);
3510 self.capture.to_tokens(tokens);
3511 self.block.to_tokens(tokens);
3512 }
3513 }
3514
3515 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003516 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003517 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003518 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003519 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003520 self.block.to_tokens(tokens);
3521 }
3522 }
3523
Michael Layzell734adb42017-06-07 16:58:31 -04003524 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003525 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003526 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003527 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003528 self.yield_token.to_tokens(tokens);
3529 self.expr.to_tokens(tokens);
3530 }
3531 }
3532
3533 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003534 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003535 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003536 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003537 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003538 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003539 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003540 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003541 for input in self.inputs.pairs() {
3542 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003543 FnArg::Captured(ArgCaptured {
3544 ref pat,
3545 ty: Type::Infer(_),
3546 ..
3547 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003548 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003549 }
David Tolnay56080682018-01-06 14:01:52 -08003550 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003551 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003552 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003553 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003554 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003555 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003556 self.body.to_tokens(tokens);
3557 }
3558 }
3559
Michael Layzell734adb42017-06-07 16:58:31 -04003560 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003561 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003562 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003563 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003564 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003565 self.block.brace_token.surround(tokens, |tokens| {
3566 inner_attrs_to_tokens(&self.attrs, tokens);
3567 tokens.append_all(&self.block.stmts);
3568 });
Nika Layzell640832a2017-12-04 13:37:09 -05003569 }
3570 }
3571
3572 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003573 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003574 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003575 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003576 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003577 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003578 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003579 tokens.append_all(&self.block.stmts);
3580 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003581 }
3582 }
3583
Michael Layzell734adb42017-06-07 16:58:31 -04003584 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003585 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003586 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003587 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003588 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003589 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003590 self.right.to_tokens(tokens);
3591 }
3592 }
3593
Michael Layzell734adb42017-06-07 16:58:31 -04003594 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003595 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003596 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003597 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003598 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003599 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003600 self.right.to_tokens(tokens);
3601 }
3602 }
3603
3604 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003605 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003606 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003607 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003608 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003609 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003610 }
3611 }
3612
David Tolnay85b69a42017-12-27 20:43:10 -05003613 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003614 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003615 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003616 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003617 Member::Unnamed(ref index) => index.to_tokens(tokens),
3618 }
3619 }
3620 }
3621
David Tolnay85b69a42017-12-27 20:43:10 -05003622 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003623 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003624 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3625 lit.set_span(self.span);
3626 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003627 }
3628 }
3629
3630 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003631 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003632 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003633 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003634 self.bracket_token.surround(tokens, |tokens| {
3635 self.index.to_tokens(tokens);
3636 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003637 }
3638 }
3639
Michael Layzell734adb42017-06-07 16:58:31 -04003640 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003641 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003642 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003643 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003644 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003645 match self.limits {
3646 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3647 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3648 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003649 self.to.to_tokens(tokens);
3650 }
3651 }
3652
3653 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003654 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003655 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003656 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003657 }
3658 }
3659
Michael Layzell734adb42017-06-07 16:58:31 -04003660 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003661 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003662 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003663 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003664 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003665 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003666 self.expr.to_tokens(tokens);
3667 }
3668 }
3669
Michael Layzell734adb42017-06-07 16:58:31 -04003670 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003671 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003672 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003673 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003674 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003675 self.label.to_tokens(tokens);
3676 self.expr.to_tokens(tokens);
3677 }
3678 }
3679
Michael Layzell734adb42017-06-07 16:58:31 -04003680 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003681 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003682 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003683 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003684 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003685 self.label.to_tokens(tokens);
3686 }
3687 }
3688
Michael Layzell734adb42017-06-07 16:58:31 -04003689 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003690 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003691 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003692 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003693 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003694 self.expr.to_tokens(tokens);
3695 }
3696 }
3697
Michael Layzell734adb42017-06-07 16:58:31 -04003698 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003699 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003700 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003701 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003702 self.mac.to_tokens(tokens);
3703 }
3704 }
3705
3706 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003707 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003708 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003709 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003710 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003711 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003712 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003713 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003714 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003715 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003716 self.rest.to_tokens(tokens);
3717 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003718 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003719 }
3720 }
3721
Michael Layzell734adb42017-06-07 16:58:31 -04003722 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003723 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003724 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003725 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003726 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003727 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003728 self.expr.to_tokens(tokens);
3729 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003730 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003731 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003732 }
3733 }
3734
David Tolnaye98775f2017-12-28 23:17:00 -05003735 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003736 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003737 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003738 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003739 self.group_token.surround(tokens, |tokens| {
3740 self.expr.to_tokens(tokens);
3741 });
3742 }
3743 }
3744
Alex Crichton62a0a592017-05-22 13:58:53 -07003745 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003746 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003747 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003748 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003749 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003750 self.expr.to_tokens(tokens);
3751 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003752 }
3753 }
3754
Michael Layzell734adb42017-06-07 16:58:31 -04003755 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003756 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003757 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003758 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003759 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003760 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003761 }
3762 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003763
David Tolnay2ae520a2017-12-29 11:19:50 -05003764 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003765 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003766 self.tts.to_tokens(tokens);
3767 }
3768 }
3769
Michael Layzell734adb42017-06-07 16:58:31 -04003770 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003771 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003772 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003773 self.name.to_tokens(tokens);
3774 self.colon_token.to_tokens(tokens);
3775 }
3776 }
3777
3778 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003779 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003780 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003781 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003782 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003783 if let Some(ref colon_token) = self.colon_token {
3784 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003785 self.expr.to_tokens(tokens);
3786 }
David Tolnay055a7042016-10-02 19:23:54 -07003787 }
3788 }
3789
Michael Layzell734adb42017-06-07 16:58:31 -04003790 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003791 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003792 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003793 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003794 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003795 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003796 if let Some((ref if_token, ref guard)) = self.guard {
3797 if_token.to_tokens(tokens);
3798 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003799 }
David Tolnaydfb91432018-03-31 19:19:44 +02003800 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003801 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003802 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003803 }
3804 }
3805
Michael Layzell734adb42017-06-07 16:58:31 -04003806 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003807 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003808 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003809 self.underscore_token.to_tokens(tokens);
3810 }
3811 }
3812
Michael Layzell734adb42017-06-07 16:58:31 -04003813 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003814 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003815 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003816 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003817 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003818 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003819 if let Some((ref at_token, ref subpat)) = self.subpat {
3820 at_token.to_tokens(tokens);
3821 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003822 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003823 }
3824 }
3825
Michael Layzell734adb42017-06-07 16:58:31 -04003826 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003827 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003828 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003829 self.path.to_tokens(tokens);
3830 self.brace_token.surround(tokens, |tokens| {
3831 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003832 // NOTE: We need a comma before the dot2 token if it is present.
3833 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003834 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003835 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003836 self.dot2_token.to_tokens(tokens);
3837 });
3838 }
3839 }
3840
Michael Layzell734adb42017-06-07 16:58:31 -04003841 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003842 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003843 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003844 self.path.to_tokens(tokens);
3845 self.pat.to_tokens(tokens);
3846 }
3847 }
3848
Michael Layzell734adb42017-06-07 16:58:31 -04003849 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003850 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003851 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003852 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3853 }
3854 }
3855
Michael Layzell734adb42017-06-07 16:58:31 -04003856 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003857 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003858 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003859 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003860 self.front.to_tokens(tokens);
3861 if let Some(ref dot2_token) = self.dot2_token {
3862 if !self.front.empty_or_trailing() {
3863 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003864 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003865 }
David Tolnay41871922017-12-29 01:53:45 -05003866 dot2_token.to_tokens(tokens);
3867 self.comma_token.to_tokens(tokens);
3868 if self.comma_token.is_none() && !self.back.is_empty() {
3869 // Ensure there is a comma after the .. token.
3870 <Token![,]>::default().to_tokens(tokens);
3871 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003872 }
David Tolnay41871922017-12-29 01:53:45 -05003873 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003874 });
3875 }
3876 }
3877
Michael Layzell734adb42017-06-07 16:58:31 -04003878 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003879 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003880 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003881 self.box_token.to_tokens(tokens);
3882 self.pat.to_tokens(tokens);
3883 }
3884 }
3885
Michael Layzell734adb42017-06-07 16:58:31 -04003886 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003887 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003888 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003889 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003890 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003891 self.pat.to_tokens(tokens);
3892 }
3893 }
3894
Michael Layzell734adb42017-06-07 16:58:31 -04003895 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003896 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003897 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003898 self.expr.to_tokens(tokens);
3899 }
3900 }
3901
Michael Layzell734adb42017-06-07 16:58:31 -04003902 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003903 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003904 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003905 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003906 match self.limits {
3907 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003908 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003909 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003910 self.hi.to_tokens(tokens);
3911 }
3912 }
3913
Michael Layzell734adb42017-06-07 16:58:31 -04003914 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003915 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003916 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003917 // XXX: This is a mess, and it will be so easy to screw it up. How
3918 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003919 self.bracket_token.surround(tokens, |tokens| {
3920 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003921
3922 // If we need a comma before the middle or standalone .. token,
3923 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003924 if !self.front.empty_or_trailing()
3925 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003926 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003927 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003928 }
3929
3930 // If we have an identifier, we always need a .. token.
3931 if self.middle.is_some() {
3932 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003933 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003934 } else if self.dot2_token.is_some() {
3935 self.dot2_token.to_tokens(tokens);
3936 }
3937
3938 // Make sure we have a comma before the back half.
3939 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003940 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003941 self.back.to_tokens(tokens);
3942 } else {
3943 self.comma_token.to_tokens(tokens);
3944 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003945 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003946 }
3947 }
3948
Michael Layzell734adb42017-06-07 16:58:31 -04003949 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003950 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003951 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003952 self.mac.to_tokens(tokens);
3953 }
3954 }
3955
3956 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003957 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003958 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003959 self.tts.to_tokens(tokens);
3960 }
3961 }
3962
3963 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003964 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003965 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003966 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003967 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003968 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003969 }
3970 self.pat.to_tokens(tokens);
3971 }
3972 }
3973
Michael Layzell734adb42017-06-07 16:58:31 -04003974 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003975 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003976 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003977 self.brace_token.surround(tokens, |tokens| {
3978 tokens.append_all(&self.stmts);
3979 });
David Tolnay42602292016-10-01 22:25:45 -07003980 }
3981 }
3982
Michael Layzell734adb42017-06-07 16:58:31 -04003983 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003984 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003985 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003986 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003987 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003988 Stmt::Item(ref item) => item.to_tokens(tokens),
3989 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003990 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003991 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003992 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003993 }
David Tolnay42602292016-10-01 22:25:45 -07003994 }
3995 }
3996 }
David Tolnay191e0582016-10-02 18:31:09 -07003997
Michael Layzell734adb42017-06-07 16:58:31 -04003998 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003999 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07004000 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07004001 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07004002 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02004003 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05004004 if let Some((ref colon_token, ref ty)) = self.ty {
4005 colon_token.to_tokens(tokens);
4006 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04004007 }
David Tolnay8b4d3022017-12-29 12:11:10 -05004008 if let Some((ref eq_token, ref init)) = self.init {
4009 eq_token.to_tokens(tokens);
4010 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04004011 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07004012 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07004013 }
4014 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07004015}