blob: 11902d4ea20e95d3fbd5ed24a6dc8b02301ea8f4 [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 Tolnaybcf26022017-12-25 22:10:52 -05001039 // When we're parsing expressions which occur before blocks, like in an if
1040 // statement's condition, we cannot parse a struct literal.
1041 //
1042 // Struct literals are ambiguous in certain positions
1043 // https://github.com/rust-lang/rfcs/pull/92
David Tolnay9389c382018-08-27 09:13:37 -07001044 #[derive(Copy, Clone)]
1045 pub struct AllowStruct(bool);
1046
1047 #[derive(Copy, Clone)]
1048 pub struct AllowBlock(bool);
David Tolnayaf2557e2016-10-24 11:52:21 -07001049
David Tolnay01218d12018-08-29 18:13:07 -07001050 #[derive(Copy, Clone, PartialEq, PartialOrd)]
1051 enum Precedence {
1052 Any,
1053 Assign,
1054 Placement,
1055 Range,
1056 Or,
1057 And,
1058 Compare,
1059 BitOr,
1060 BitXor,
1061 BitAnd,
1062 Shift,
1063 Arithmetic,
1064 Term,
1065 Cast,
1066 }
1067
1068 impl Precedence {
1069 fn of(op: &BinOp) -> Self {
1070 match *op {
1071 BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic,
1072 BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term,
1073 BinOp::And(_) => Precedence::And,
1074 BinOp::Or(_) => Precedence::Or,
1075 BinOp::BitXor(_) => Precedence::BitXor,
1076 BinOp::BitAnd(_) => Precedence::BitAnd,
1077 BinOp::BitOr(_) => Precedence::BitOr,
1078 BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift,
1079 BinOp::Eq(_) | BinOp::Lt(_) | BinOp::Le(_) | BinOp::Ne(_) | BinOp::Ge(_) | BinOp::Gt(_) => Precedence::Compare,
1080 BinOp::AddEq(_) | BinOp::SubEq(_) | BinOp::MulEq(_) | BinOp::DivEq(_) | BinOp::RemEq(_) | BinOp::BitXorEq(_) | BinOp::BitAndEq(_) | BinOp::BitOrEq(_) | BinOp::ShlEq(_) | BinOp::ShrEq(_) => Precedence::Assign,
1081 }
1082 }
1083 }
1084
David Tolnay9389c382018-08-27 09:13:37 -07001085 impl Parse for Expr {
1086 fn parse(input: ParseStream) -> Result<Self> {
1087 ambiguous_expr(input, AllowStruct(true), AllowBlock(true))
Alex Crichton954046c2017-05-30 21:49:42 -07001088 }
1089 }
1090
Michael Layzell734adb42017-06-07 16:58:31 -04001091 #[cfg(feature = "full")]
David Tolnay9fb0aed2018-08-27 10:23:12 -07001092 fn expr_no_struct(input: ParseStream) -> Result<Expr> {
1093 ambiguous_expr(input, AllowStruct(false), AllowBlock(true))
1094 }
David Tolnayaf2557e2016-10-24 11:52:21 -07001095
David Tolnay01218d12018-08-29 18:13:07 -07001096 #[cfg(feature = "full")]
1097 fn parse_expr(input: ParseStream, mut lhs: Expr, allow_struct: AllowStruct, allow_block: AllowBlock, base: Precedence) -> Result<Expr> {
1098 loop {
1099 if input.fork().parse::<BinOp>().ok().map_or(false, |op| Precedence::of(&op) >= base) {
1100 let op: BinOp = input.parse()?;
1101 let precedence = Precedence::of(&op);
1102 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1103 loop {
1104 let next = peek_precedence(input);
1105 if next > precedence || next == precedence && precedence == Precedence::Assign {
1106 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1107 } else {
1108 break;
1109 }
1110 }
1111 lhs = Expr::Binary(ExprBinary {
1112 attrs: Vec::new(),
1113 left: Box::new(lhs),
1114 op: op,
1115 right: Box::new(rhs),
1116 });
1117 } else if Precedence::Assign >= base && input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1118 let eq_token: Token![=] = input.parse()?;
1119 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1120 loop {
1121 let next = peek_precedence(input);
1122 if next >= Precedence::Assign {
1123 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1124 } else {
1125 break;
1126 }
1127 }
1128 lhs = Expr::Assign(ExprAssign {
1129 attrs: Vec::new(),
1130 left: Box::new(lhs),
1131 eq_token: eq_token,
1132 right: Box::new(rhs),
1133 });
1134 } else if Precedence::Placement >= base && input.peek(Token![<-]) {
1135 let arrow_token: Token![<-] = input.parse()?;
1136 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1137 loop {
1138 let next = peek_precedence(input);
1139 if next > Precedence::Placement {
1140 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1141 } else {
1142 break;
1143 }
1144 }
1145 lhs = Expr::InPlace(ExprInPlace {
1146 attrs: Vec::new(),
1147 place: Box::new(lhs),
1148 arrow_token: arrow_token,
1149 value: Box::new(rhs),
1150 });
1151 } else if Precedence::Range >= base && input.peek(Token![..]) {
1152 let limits: RangeLimits = input.parse()?;
1153 let rhs = if input.is_empty()
1154 || input.peek(Token![,])
1155 || input.peek(Token![;])
1156 || !allow_struct.0 && input.peek(token::Brace)
1157 {
1158 None
1159 } else {
1160 // We don't want to allow blocks in the rhs if we don't
1161 // allow structs.
1162 let allow_block = AllowBlock(allow_struct.0);
1163 let mut rhs = unary_expr(input, allow_struct, allow_block)?;
1164 loop {
1165 let next = peek_precedence(input);
1166 if next > Precedence::Range {
1167 rhs = parse_expr(input, rhs, allow_struct, allow_block, next)?;
1168 } else {
1169 break;
1170 }
1171 }
1172 Some(rhs)
1173 };
1174 lhs = Expr::Range(ExprRange {
1175 attrs: Vec::new(),
1176 from: Some(Box::new(lhs)),
1177 limits: limits,
1178 to: rhs.map(Box::new),
1179 });
1180 } else if Precedence::Cast >= base && input.peek(Token![as]) {
1181 let as_token: Token![as] = input.parse()?;
1182 let ty = input.call(Type::without_plus)?;
1183 lhs = Expr::Cast(ExprCast {
1184 attrs: Vec::new(),
1185 expr: Box::new(lhs),
1186 as_token: as_token,
1187 ty: Box::new(ty),
1188 });
1189 } else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) {
1190 let colon_token: Token![:] = input.parse()?;
1191 let ty = input.call(Type::without_plus)?;
1192 lhs = Expr::Type(ExprType {
1193 attrs: Vec::new(),
1194 expr: Box::new(lhs),
1195 colon_token: colon_token,
1196 ty: Box::new(ty),
1197 });
1198 } else {
1199 break;
1200 }
1201 }
1202 Ok(lhs)
1203 }
1204
1205 #[cfg(feature = "full")]
1206 fn peek_precedence(input: ParseStream) -> Precedence {
1207 if let Ok(op) = input.fork().parse() {
1208 Precedence::of(&op)
1209 } else if input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1210 Precedence::Assign
1211 } else if input.peek(Token![<-]) {
1212 Precedence::Placement
1213 } else if input.peek(Token![..]) {
1214 Precedence::Range
1215 } else if input.peek(Token![as]) || input.peek(Token![:]) && !input.peek(Token![::]) {
1216 Precedence::Cast
1217 } else {
1218 Precedence::Any
1219 }
1220 }
1221
David Tolnaybcf26022017-12-25 22:10:52 -05001222 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001223 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001224 fn ambiguous_expr(
1225 input: ParseStream,
1226 allow_struct: AllowStruct,
1227 allow_block: AllowBlock,
1228 ) -> Result<Expr> {
David Tolnay01218d12018-08-29 18:13:07 -07001229 //assign_expr(input, allow_struct, allow_block)
1230 let lhs = unary_expr(input, allow_struct, allow_block)?;
1231 parse_expr(input, lhs, allow_struct, allow_block, Precedence::Any)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001232 }
1233
Michael Layzell734adb42017-06-07 16:58:31 -04001234 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001235 fn ambiguous_expr(
1236 input: ParseStream,
1237 allow_struct: AllowStruct,
1238 allow_block: AllowBlock,
1239 ) -> Result<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001240 // NOTE: We intentionally skip assign_expr, placement_expr, and
David Tolnay9389c382018-08-27 09:13:37 -07001241 // range_expr as they are only parsed in full mode.
1242 or_expr(input, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001243 }
1244
David Tolnaybcf26022017-12-25 22:10:52 -05001245 // <UnOp> <trailer>
1246 // & <trailer>
1247 // &mut <trailer>
1248 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001249 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001250 fn unary_expr(
1251 input: ParseStream,
1252 allow_struct: AllowStruct,
1253 allow_block: AllowBlock,
1254 ) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001255 let ahead = input.fork();
1256 ahead.call(Attribute::parse_outer)?;
1257 if ahead.peek(Token![&])
1258 || ahead.peek(Token![box])
1259 || ahead.peek(Token![*])
1260 || ahead.peek(Token![!])
1261 || ahead.peek(Token![-])
1262 {
1263 let attrs = input.call(Attribute::parse_outer)?;
1264 if input.peek(Token![&]) {
1265 Ok(Expr::Reference(ExprReference {
1266 attrs: attrs,
1267 and_token: input.parse()?,
1268 mutability: input.parse()?,
1269 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1270 }))
1271 } else if input.peek(Token![box]) {
1272 Ok(Expr::Box(ExprBox {
1273 attrs: attrs,
1274 box_token: input.parse()?,
1275 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1276 }))
1277 } else {
1278 Ok(Expr::Unary(ExprUnary {
1279 attrs: attrs,
1280 op: input.parse()?,
1281 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1282 }))
1283 }
1284 } else {
1285 trailer_expr(input, allow_struct, allow_block)
1286 }
1287 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001288
Michael Layzell734adb42017-06-07 16:58:31 -04001289 // XXX: This duplication is ugly
1290 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001291 fn unary_expr(
1292 input: ParseStream,
1293 allow_struct: AllowStruct,
1294 allow_block: AllowBlock,
1295 ) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001296 let ahead = input.fork();
1297 ahead.call(Attribute::parse_outer)?;
1298 if ahead.peek(Token![*]) || ahead.peek(Token![!]) || ahead.peek(Token![-]) {
1299 Ok(Expr::Unary(ExprUnary {
1300 attrs: input.call(Attribute::parse_outer)?,
1301 op: input.parse()?,
1302 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1303 }))
1304 } else {
1305 trailer_expr(input, allow_struct, allow_block)
1306 }
1307 }
Michael Layzell734adb42017-06-07 16:58:31 -04001308
David Tolnayd997aef2018-07-21 18:42:31 -07001309 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001310 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1311 let mut outer = Vec::new();
1312 let mut inner = Vec::new();
1313 for attr in mem::replace(attrs, Vec::new()) {
1314 match attr.style {
1315 AttrStyle::Outer => outer.push(attr),
1316 AttrStyle::Inner(_) => inner.push(attr),
1317 }
1318 }
1319 *attrs = inner;
1320 outer
1321 }
1322
David Tolnaybcf26022017-12-25 22:10:52 -05001323 // <atom> (..<args>) ...
1324 // <atom> . <ident> (..<args>) ...
1325 // <atom> . <ident> ...
1326 // <atom> . <lit> ...
1327 // <atom> [ <expr> ] ...
1328 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001329 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001330 fn trailer_expr(
1331 input: ParseStream,
1332 allow_struct: AllowStruct,
1333 allow_block: AllowBlock,
1334 ) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001335 let mut e = atom_expr(input, allow_struct, allow_block)?;
1336
1337 let mut attrs = e.replace_attrs(Vec::new());
1338 let outer_attrs = take_outer(&mut attrs);
1339 e.replace_attrs(attrs);
1340
David Tolnay01218d12018-08-29 18:13:07 -07001341 e = trailer_helper(input, e)?;
1342
1343 let mut attrs = outer_attrs;
1344 attrs.extend(e.replace_attrs(Vec::new()));
1345 e.replace_attrs(attrs);
1346 Ok(e)
1347 }
1348
1349 #[cfg(feature = "full")]
1350 fn trailer_helper(input: ParseStream, mut e: Expr) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001351 loop {
1352 if input.peek(token::Paren) {
1353 let content;
1354 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001355 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001356 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001357 paren_token: parenthesized!(content in input),
1358 args: content.parse_terminated(<Expr as Parse>::parse)?,
1359 });
1360 } else if input.peek(Token![.]) && !input.peek(Token![..]) {
1361 let dot_token: Token![.] = input.parse()?;
1362 let member: Member = input.parse()?;
1363 let turbofish = if member.is_named() && input.peek(Token![::]) {
1364 Some(MethodTurbofish {
1365 colon2_token: input.parse()?,
1366 lt_token: input.parse()?,
1367 args: {
1368 let mut args = Punctuated::new();
1369 loop {
1370 if input.peek(Token![>]) {
1371 break;
1372 }
1373 let value = input.parse()?;
1374 args.push_value(value);
1375 if input.peek(Token![>]) {
1376 break;
1377 }
1378 let punct = input.parse()?;
1379 args.push_punct(punct);
1380 }
1381 args
1382 },
1383 gt_token: input.parse()?,
1384 })
1385 } else {
1386 None
1387 };
1388
1389 if turbofish.is_some() || input.peek(token::Paren) {
1390 if let Member::Named(method) = member {
1391 let content;
1392 e = Expr::MethodCall(ExprMethodCall {
1393 attrs: Vec::new(),
1394 receiver: Box::new(e),
1395 dot_token: dot_token,
1396 method: method,
1397 turbofish: turbofish,
1398 paren_token: parenthesized!(content in input),
1399 args: content.parse_terminated(<Expr as Parse>::parse)?,
1400 });
1401 continue;
1402 }
1403 }
1404
1405 e = Expr::Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001406 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001407 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001408 dot_token: dot_token,
David Tolnay85b69a42017-12-27 20:43:10 -05001409 member: member,
David Tolnay1501f7e2018-08-27 14:21:03 -07001410 });
1411 } else if input.peek(token::Bracket) {
1412 let content;
1413 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001414 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001415 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001416 bracket_token: bracketed!(content in input),
1417 index: content.parse()?,
1418 });
1419 } else if input.peek(Token![?]) {
1420 e = Expr::Try(ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001421 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001422 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001423 question_token: input.parse()?,
1424 });
1425 } else {
1426 break;
1427 }
1428 }
David Tolnay1501f7e2018-08-27 14:21:03 -07001429 Ok(e)
1430 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001431
Michael Layzell734adb42017-06-07 16:58:31 -04001432 // XXX: Duplication == ugly
1433 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001434 fn trailer_expr(
1435 input: ParseStream,
1436 allow_struct: AllowStruct,
1437 allow_block: AllowBlock,
1438 ) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001439 let mut e = atom_expr(input, allow_struct, allow_block)?;
1440
1441 loop {
1442 if input.peek(token::Paren) {
1443 let content;
1444 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001445 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001446 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001447 paren_token: parenthesized!(content in input),
1448 args: content.parse_terminated(<Expr as Parse>::parse)?,
1449 });
1450 } else if input.peek(Token![.]) {
1451 e = Expr::Field(ExprField {
David Tolnayd5147742018-06-30 10:09:52 -07001452 attrs: Vec::new(),
1453 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001454 dot_token: input.parse()?,
1455 member: input.parse()?,
1456 });
1457 } else if input.peek(token::Bracket) {
1458 let content;
1459 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001460 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001461 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001462 bracket_token: bracketed!(content in input),
1463 index: content.parse()?,
1464 });
1465 } else {
1466 break;
1467 }
1468 }
1469
1470 Ok(e)
1471 }
Michael Layzell734adb42017-06-07 16:58:31 -04001472
David Tolnaya454c8f2018-01-07 01:01:10 -08001473 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001474 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001475 #[cfg(feature = "full")]
David Tolnay6e1e5052018-08-30 10:21:48 -07001476 fn atom_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1477 if input.peek(token::Group) {
1478 return input.parse().map(Expr::Group);
1479 }
1480
1481 let mut attrs = input.call(Attribute::parse_outer)?;
1482
1483 let mut expr = if input.peek(token::Group) {
1484 Expr::Group(input.parse()?)
1485 } else if input.peek(Lit) {
1486 Expr::Lit(input.parse()?)
1487 } else if input.peek(Token![async])
1488 && (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace))
1489 {
1490 Expr::Async(input.parse()?)
1491 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
1492 Expr::TryBlock(input.parse()?)
1493 } else if input.peek(Token![|])
1494 || input.peek(Token![async]) && (input.peek2(Token![|]) || input.peek2(Token![move]))
1495 || input.peek(Token![static])
1496 || input.peek(Token![move])
1497 {
1498 Expr::Closure(expr_closure(input, allow_struct)?)
1499 } else if input.peek(Ident)
1500 || input.peek(Token![::])
1501 || input.peek(Token![<])
1502 || input.peek(Token![self])
1503 || input.peek(Token![Self])
1504 || input.peek(Token![super])
1505 || input.peek(Token![extern])
1506 || input.peek(Token![crate])
1507 {
1508 path_or_macro_or_struct(input, allow_struct)?
1509 } else if input.peek(token::Paren) {
1510 paren_or_tuple(input)?
1511 } else if input.peek(Token![break]) {
1512 Expr::Break(expr_break(input, allow_struct)?)
1513 } else if input.peek(Token![continue]) {
1514 Expr::Continue(input.parse()?)
1515 } else if input.peek(Token![return]) {
1516 Expr::Return(expr_ret(input, allow_struct)?)
1517 } else if input.peek(token::Bracket) {
1518 array_or_repeat(input)?
1519 } else if input.peek(Token![if]) {
1520 if input.peek2(Token![let]) {
1521 Expr::IfLet(input.parse()?)
1522 } else {
1523 Expr::If(input.parse()?)
1524 }
1525 } else if input.peek(Token![while]) {
1526 if input.peek2(Token![let]) {
1527 Expr::WhileLet(input.parse()?)
1528 } else {
1529 Expr::While(input.parse()?)
1530 }
1531 } else if input.peek(Token![for]) {
1532 Expr::ForLoop(input.parse()?)
1533 } else if input.peek(Token![loop]) {
1534 Expr::Loop(input.parse()?)
1535 } else if input.peek(Token![match]) {
1536 Expr::Match(input.parse()?)
1537 } else if input.peek(Token![yield]) {
1538 Expr::Yield(input.parse()?)
1539 } else if input.peek(Token![unsafe]) {
1540 Expr::Unsafe(input.parse()?)
1541 } else if allow_block.0 && input.peek(token::Brace) {
1542 Expr::Block(input.parse()?)
1543 } else if input.peek(Token![..]) {
1544 Expr::Range(expr_range(input, allow_struct)?)
1545 } else if input.peek(Lifetime) {
1546 let the_label: Label = input.parse()?;
1547 let mut expr = if input.peek(Token![while]) {
1548 if input.peek2(Token![let]) {
1549 Expr::WhileLet(input.parse()?)
1550 } else {
1551 Expr::While(input.parse()?)
1552 }
1553 } else if input.peek(Token![for]) {
1554 Expr::ForLoop(input.parse()?)
1555 } else if input.peek(Token![loop]) {
1556 Expr::Loop(input.parse()?)
1557 } else if input.peek(token::Brace) {
1558 Expr::Block(input.parse()?)
1559 } else {
1560 return Err(input.error("expected loop or block expression"));
1561 };
1562 match expr {
1563 Expr::WhileLet(ExprWhileLet { ref mut label, .. }) |
1564 Expr::While(ExprWhile { ref mut label, .. }) |
1565 Expr::ForLoop(ExprForLoop { ref mut label, .. }) |
1566 Expr::Loop(ExprLoop { ref mut label, .. }) |
1567 Expr::Block(ExprBlock { ref mut label, .. }) => *label = Some(the_label),
1568 _ => unreachable!(),
1569 }
1570 expr
1571 } else {
1572 return Err(input.error("expected expression"));
1573 };
1574
1575 attrs.extend(expr.replace_attrs(Vec::new()));
1576 expr.replace_attrs(attrs);
1577 Ok(expr)
1578 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001579
Michael Layzell734adb42017-06-07 16:58:31 -04001580 #[cfg(not(feature = "full"))]
David Tolnay6e1e5052018-08-30 10:21:48 -07001581 fn atom_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1582 if input.peek(Lit) {
1583 input.parse().map(Expr::Lit)
1584 } else if input.peek(token::Paren) {
1585 input.parse().map(Expr::Paren)
1586 } else if input.peek(Ident)
1587 || input.peek(Token![::])
1588 || input.peek(Token![<])
1589 || input.peek(Token![self])
1590 || input.peek(Token![Self])
1591 || input.peek(Token![super])
1592 || input.peek(Token![extern])
1593 || input.peek(Token![crate])
1594 {
1595 input.parse().map(Expr::Path)
1596 } else {
1597 Err(input.error("unsupported expression; enable syn's features=[\"full\"]"))
1598 }
1599 }
1600
1601 #[cfg(feature = "full")]
1602 fn path_or_macro_or_struct(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
1603 let expr: ExprPath = input.parse()?;
1604 if expr.qself.is_some() {
1605 return Ok(Expr::Path(expr));
1606 }
1607
1608 if input.peek(Token![!]) && !input.peek(Token![!=]) {
1609 let mut contains_arguments = false;
1610 for segment in &expr.path.segments {
1611 match segment.arguments {
1612 PathArguments::None => {}
1613 PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
1614 contains_arguments = true;
1615 }
1616 }
1617 }
1618
1619 if !contains_arguments {
1620 let bang_token: Token![!] = input.parse()?;
1621 let (delimiter, tts) = mac::parse_delimiter(input)?;
1622 return Ok(Expr::Macro(ExprMacro {
1623 attrs: Vec::new(),
1624 mac: Macro {
1625 path: expr.path,
1626 bang_token: bang_token,
1627 delimiter: delimiter,
1628 tts: tts,
1629 },
1630 }));
1631 }
1632 }
1633
1634 if allow_struct.0 && input.peek(token::Brace) {
1635 let outer_attrs = Vec::new();
1636 expr_struct_helper(input, outer_attrs, expr.path).map(Expr::Struct)
1637 } else {
1638 Ok(Expr::Path(expr))
1639 }
1640 }
1641
1642 #[cfg(feature = "full")]
1643 fn paren_or_tuple(input: ParseStream) -> Result<Expr> {
1644 let content;
1645 let paren_token = parenthesized!(content in input);
1646 let inner_attrs = content.call(Attribute::parse_inner)?;
1647 if content.is_empty() {
1648 return Ok(Expr::Tuple(ExprTuple {
1649 attrs: inner_attrs,
1650 paren_token: paren_token,
1651 elems: Punctuated::new(),
1652 }));
1653 }
1654
1655 let first: Expr = content.parse()?;
1656 if content.is_empty() {
1657 return Ok(Expr::Paren(ExprParen {
1658 attrs: inner_attrs,
1659 paren_token: paren_token,
1660 expr: Box::new(first),
1661 }));
1662 }
1663
1664 let mut elems = Punctuated::new();
1665 elems.push_value(first);
1666 while !content.is_empty() {
1667 let punct = content.parse()?;
1668 elems.push_punct(punct);
1669 if content.is_empty() {
1670 break;
1671 }
1672 let value = content.parse()?;
1673 elems.push_value(value);
1674 }
1675 Ok(Expr::Tuple(ExprTuple {
1676 attrs: inner_attrs,
1677 paren_token: paren_token,
1678 elems: elems,
1679 }))
1680 }
1681
1682 #[cfg(feature = "full")]
1683 fn array_or_repeat(input: ParseStream) -> Result<Expr> {
1684 let content;
1685 let bracket_token = bracketed!(content in input);
1686 let inner_attrs = content.call(Attribute::parse_inner)?;
1687 if content.is_empty() {
1688 return Ok(Expr::Array(ExprArray {
1689 attrs: inner_attrs,
1690 bracket_token: bracket_token,
1691 elems: Punctuated::new(),
1692 }));
1693 }
1694
1695 let first: Expr = content.parse()?;
1696 if content.is_empty() || content.peek(Token![,]) {
1697 let mut elems = Punctuated::new();
1698 elems.push_value(first);
1699 while !content.is_empty() {
1700 let punct = content.parse()?;
1701 elems.push_punct(punct);
1702 if content.is_empty() {
1703 break;
1704 }
1705 let value = content.parse()?;
1706 elems.push_value(value);
1707 }
1708 Ok(Expr::Array(ExprArray {
1709 attrs: inner_attrs,
1710 bracket_token: bracket_token,
1711 elems: elems,
1712 }))
1713 } else if content.peek(Token![;]) {
1714 let semi_token: Token![;] = content.parse()?;
1715 let len: Expr = content.parse()?;
1716 Ok(Expr::Repeat(ExprRepeat {
1717 attrs: inner_attrs,
1718 bracket_token: bracket_token,
1719 expr: Box::new(first),
1720 semi_token: semi_token,
1721 len: Box::new(len),
1722 }))
1723 } else {
1724 Err(content.error("expected `,` or `;`"))
1725 }
1726 }
Michael Layzell734adb42017-06-07 16:58:31 -04001727
Michael Layzell734adb42017-06-07 16:58:31 -04001728 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07001729 fn expr_early(input: ParseStream) -> Result<Expr> {
1730 let mut attrs = input.call(Attribute::parse_outer)?;
1731 let mut expr = if input.peek(Token![if]) {
1732 if input.peek2(Token![let]) {
1733 Expr::IfLet(input.parse()?)
1734 } else {
1735 Expr::If(input.parse()?)
1736 }
1737 } else if input.peek(Token![while]) {
1738 if input.peek2(Token![let]) {
1739 Expr::WhileLet(input.parse()?)
1740 } else {
1741 Expr::While(input.parse()?)
1742 }
1743 } else if input.peek(Token![for]) {
1744 Expr::ForLoop(input.parse()?)
1745 } else if input.peek(Token![loop]) {
1746 Expr::Loop(input.parse()?)
1747 } else if input.peek(Token![match]) {
1748 Expr::Match(input.parse()?)
1749 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
1750 Expr::TryBlock(input.parse()?)
1751 } else if input.peek(Token![unsafe]) {
1752 Expr::Unsafe(input.parse()?)
1753 } else if input.peek(token::Brace) {
1754 Expr::Block(input.parse()?)
1755 } else {
1756 let allow_struct = AllowStruct(true);
1757 let allow_block = AllowBlock(true);
1758 let mut expr = unary_expr(input, allow_struct, allow_block)?;
1759
1760 attrs.extend(expr.replace_attrs(Vec::new()));
1761 expr.replace_attrs(attrs);
1762
1763 return parse_expr(input, expr, allow_struct, allow_block, Precedence::Any);
1764 };
1765
1766 if input.peek(Token![.]) || input.peek(Token![?]) {
1767 expr = trailer_helper(input, expr)?;
1768
1769 attrs.extend(expr.replace_attrs(Vec::new()));
1770 expr.replace_attrs(attrs);
1771
1772 let allow_struct = AllowStruct(true);
1773 let allow_block = AllowBlock(true);
1774 return parse_expr(input, expr, allow_struct, allow_block, Precedence::Any);
1775 }
1776
1777 attrs.extend(expr.replace_attrs(Vec::new()));
1778 expr.replace_attrs(attrs);
1779 Ok(expr)
1780 }
Michael Layzell35418782017-06-07 09:20:25 -04001781
David Tolnay60291082018-08-28 09:54:49 -07001782 impl Parse for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001783 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001784 fn parse(input: ParseStream) -> Result<Self> {
1785 Ok(ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001786 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001787 lit: input.parse()?,
David Tolnayeb981bb2018-07-21 19:31:38 -07001788 })
David Tolnay60291082018-08-28 09:54:49 -07001789 }
David Tolnayeb981bb2018-07-21 19:31:38 -07001790
1791 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001792 fn parse(input: ParseStream) -> Result<Self> {
1793 Ok(ExprLit {
1794 attrs: input.call(Attribute::parse_outer)?,
1795 lit: input.parse()?,
David Tolnay8c91b882017-12-28 23:04:32 -05001796 })
David Tolnay60291082018-08-28 09:54:49 -07001797 }
David Tolnay8c91b882017-12-28 23:04:32 -05001798 }
1799
1800 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001801 impl Parse for ExprMacro {
1802 fn parse(input: ParseStream) -> Result<Self> {
1803 Ok(ExprMacro {
1804 attrs: input.call(Attribute::parse_outer)?,
1805 mac: input.parse()?,
David Tolnay8c91b882017-12-28 23:04:32 -05001806 })
David Tolnay60291082018-08-28 09:54:49 -07001807 }
David Tolnay8c91b882017-12-28 23:04:32 -05001808 }
1809
David Tolnaye98775f2017-12-28 23:17:00 -05001810 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001811 impl Parse for ExprGroup {
1812 fn parse(input: ParseStream) -> Result<Self> {
1813 let content;
1814 Ok(ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001815 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001816 group_token: grouped!(content in input),
1817 expr: content.parse()?,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001818 })
David Tolnay60291082018-08-28 09:54:49 -07001819 }
Michael Layzell93c36282017-06-04 20:43:14 -04001820 }
1821
David Tolnay60291082018-08-28 09:54:49 -07001822 impl Parse for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001823 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001824 fn parse(input: ParseStream) -> Result<Self> {
1825 let content;
1826 Ok(ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001827 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001828 paren_token: parenthesized!(content in input),
1829 expr: content.parse()?,
David Tolnayeb981bb2018-07-21 19:31:38 -07001830 })
David Tolnay60291082018-08-28 09:54:49 -07001831 }
David Tolnayeb981bb2018-07-21 19:31:38 -07001832
1833 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001834 fn parse(input: ParseStream) -> Result<Self> {
1835 let outer_attrs = input.call(Attribute::parse_outer)?;
1836
1837 let content;
1838 let paren_token = parenthesized!(content in input);
1839 let inner_attrs = content.call(Attribute::parse_inner)?;
1840 let expr: Expr = content.parse()?;
1841
1842 Ok(ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001843 attrs: {
1844 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07001845 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07001846 attrs
1847 },
David Tolnay60291082018-08-28 09:54:49 -07001848 paren_token: paren_token,
1849 expr: Box::new(expr),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001850 })
David Tolnay60291082018-08-28 09:54:49 -07001851 }
Alex Crichton954046c2017-05-30 21:49:42 -07001852 }
David Tolnay89e05672016-10-02 14:39:42 -07001853
Michael Layzell734adb42017-06-07 16:58:31 -04001854 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001855 impl Parse for ExprArray {
1856 fn parse(input: ParseStream) -> Result<Self> {
1857 let outer_attrs = input.call(Attribute::parse_outer)?;
1858
1859 let content;
1860 let bracket_token = bracketed!(content in input);
1861 let inner_attrs = content.call(Attribute::parse_inner)?;
1862 let elems = content.parse_terminated(<Expr as Parse>::parse)?;
1863
1864 Ok(ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001865 attrs: {
1866 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07001867 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07001868 attrs
1869 },
David Tolnay60291082018-08-28 09:54:49 -07001870 bracket_token: bracket_token,
1871 elems: elems,
Michael Layzell92639a52017-06-01 00:07:44 -04001872 })
David Tolnay60291082018-08-28 09:54:49 -07001873 }
Alex Crichton954046c2017-05-30 21:49:42 -07001874 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001875
Michael Layzell734adb42017-06-07 16:58:31 -04001876 #[cfg(feature = "full")]
David Tolnay1501f7e2018-08-27 14:21:03 -07001877 impl Parse for GenericMethodArgument {
David Tolnayd60cfec2017-12-29 00:21:38 -05001878 // TODO parse const generics as well
David Tolnay1501f7e2018-08-27 14:21:03 -07001879 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay60291082018-08-28 09:54:49 -07001880 input
1881 .parse_synom(ty_no_eq_after)
1882 .map(GenericMethodArgument::Type)
David Tolnay1501f7e2018-08-27 14:21:03 -07001883 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001884 }
1885
1886 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001887 impl Parse for ExprTuple {
1888 fn parse(input: ParseStream) -> Result<Self> {
1889 let outer_attrs = input.call(Attribute::parse_outer)?;
1890
1891 let content;
1892 let paren_token = parenthesized!(content in input);
1893 let inner_attrs = content.call(Attribute::parse_inner)?;
1894 let elems = content.parse_terminated(<Expr as Parse>::parse)?;
1895
1896 Ok(ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001897 attrs: {
1898 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07001899 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07001900 attrs
1901 },
David Tolnay60291082018-08-28 09:54:49 -07001902 paren_token: paren_token,
1903 elems: elems,
Michael Layzell92639a52017-06-01 00:07:44 -04001904 })
David Tolnay60291082018-08-28 09:54:49 -07001905 }
Alex Crichton954046c2017-05-30 21:49:42 -07001906 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001907
Michael Layzell734adb42017-06-07 16:58:31 -04001908 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001909 impl Parse for ExprIfLet {
1910 fn parse(input: ParseStream) -> Result<Self> {
1911 Ok(ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001912 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001913 if_token: input.parse()?,
1914 let_token: input.parse()?,
1915 pats: {
1916 let mut pats = Punctuated::new();
1917 let value: Pat = input.parse()?;
1918 pats.push_value(value);
1919 while input.peek(Token![|])
1920 && !input.peek(Token![||])
1921 && !input.peek(Token![|=])
1922 {
1923 let punct = input.parse()?;
1924 pats.push_punct(punct);
1925 let value: Pat = input.parse()?;
1926 pats.push_value(value);
1927 }
1928 pats
Michael Layzell92639a52017-06-01 00:07:44 -04001929 },
David Tolnay60291082018-08-28 09:54:49 -07001930 eq_token: input.parse()?,
1931 expr: Box::new(input.call(expr_no_struct)?),
1932 then_branch: input.parse()?,
1933 else_branch: {
1934 if input.peek(Token![else]) {
1935 Some(input.call(else_block)?)
1936 } else {
1937 None
1938 }
1939 },
Michael Layzell92639a52017-06-01 00:07:44 -04001940 })
David Tolnay60291082018-08-28 09:54:49 -07001941 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001942 }
1943
Michael Layzell734adb42017-06-07 16:58:31 -04001944 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001945 impl Parse for ExprIf {
1946 fn parse(input: ParseStream) -> Result<Self> {
1947 Ok(ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001948 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07001949 if_token: input.parse()?,
1950 cond: Box::new(input.call(expr_no_struct)?),
1951 then_branch: input.parse()?,
1952 else_branch: {
1953 if input.peek(Token![else]) {
1954 Some(input.call(else_block)?)
1955 } else {
1956 None
1957 }
Michael Layzell92639a52017-06-01 00:07:44 -04001958 },
Michael Layzell92639a52017-06-01 00:07:44 -04001959 })
David Tolnay60291082018-08-28 09:54:49 -07001960 }
Alex Crichton954046c2017-05-30 21:49:42 -07001961 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001962
Michael Layzell734adb42017-06-07 16:58:31 -04001963 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001964 fn else_block(input: ParseStream) -> Result<(Token![else], Box<Expr>)> {
1965 let else_token: Token![else] = input.parse()?;
1966
1967 let lookahead = input.lookahead1();
1968 let else_branch = if input.peek(Token![if]) {
1969 if input.peek2(Token![let]) {
1970 input.parse().map(Expr::IfLet)?
1971 } else {
1972 input.parse().map(Expr::If)?
1973 }
1974 } else if input.peek(token::Brace) {
1975 Expr::Block(ExprBlock {
1976 attrs: Vec::new(),
1977 label: None,
1978 block: input.parse()?,
1979 })
1980 } else {
1981 return Err(lookahead.error());
1982 };
1983
1984 Ok((else_token, Box::new(else_branch)))
1985 }
David Tolnay939766a2016-09-23 23:48:12 -07001986
Michael Layzell734adb42017-06-07 16:58:31 -04001987 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001988 impl Parse for ExprForLoop {
1989 fn parse(input: ParseStream) -> Result<Self> {
1990 let outer_attrs = input.call(Attribute::parse_outer)?;
1991 let label: Option<Label> = input.parse()?;
1992 let for_token: Token![for] = input.parse()?;
1993 let pat: Pat = input.parse()?;
1994 let in_token: Token![in] = input.parse()?;
1995 let expr: Expr = input.call(expr_no_struct)?;
1996
1997 let content;
1998 let brace_token = braced!(content in input);
1999 let inner_attrs = content.call(Attribute::parse_inner)?;
2000 let stmts = content.call(Block::parse_within)?;
2001
2002 Ok(ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07002003 attrs: {
2004 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002005 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002006 attrs
2007 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002008 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002009 for_token: for_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002010 pat: Box::new(pat),
David Tolnay60291082018-08-28 09:54:49 -07002011 in_token: in_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002012 expr: Box::new(expr),
2013 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002014 brace_token: brace_token,
2015 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002016 },
Michael Layzell92639a52017-06-01 00:07:44 -04002017 })
David Tolnay60291082018-08-28 09:54:49 -07002018 }
Alex Crichton954046c2017-05-30 21:49:42 -07002019 }
Gregory Katze5f35682016-09-27 14:20:55 -04002020
Michael Layzell734adb42017-06-07 16:58:31 -04002021 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002022 impl Parse for ExprLoop {
2023 fn parse(input: ParseStream) -> Result<Self> {
2024 let outer_attrs = input.call(Attribute::parse_outer)?;
2025 let label: Option<Label> = input.parse()?;
2026 let loop_token: Token![loop] = input.parse()?;
2027
2028 let content;
2029 let brace_token = braced!(content in input);
2030 let inner_attrs = content.call(Attribute::parse_inner)?;
2031 let stmts = content.call(Block::parse_within)?;
2032
2033 Ok(ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07002034 attrs: {
2035 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002036 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002037 attrs
2038 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002039 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002040 loop_token: loop_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002041 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002042 brace_token: brace_token,
2043 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002044 },
Michael Layzell92639a52017-06-01 00:07:44 -04002045 })
David Tolnay60291082018-08-28 09:54:49 -07002046 }
Alex Crichton954046c2017-05-30 21:49:42 -07002047 }
2048
Michael Layzell734adb42017-06-07 16:58:31 -04002049 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002050 impl Parse for ExprMatch {
2051 fn parse(input: ParseStream) -> Result<Self> {
2052 let outer_attrs = input.call(Attribute::parse_outer)?;
2053 let match_token: Token![match] = input.parse()?;
2054 let expr = expr_no_struct(input)?;
2055
2056 let content;
2057 let brace_token = braced!(content in input);
2058 let inner_attrs = content.call(Attribute::parse_inner)?;
2059
2060 let mut arms = Vec::new();
2061 while !content.is_empty() {
2062 arms.push(content.parse()?);
2063 }
2064
2065 Ok(ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002066 attrs: {
2067 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002068 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002069 attrs
2070 },
David Tolnay60291082018-08-28 09:54:49 -07002071 match_token: match_token,
2072 expr: Box::new(expr),
2073 brace_token: brace_token,
2074 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04002075 })
David Tolnay60291082018-08-28 09:54:49 -07002076 }
Alex Crichton954046c2017-05-30 21:49:42 -07002077 }
David Tolnay1978c672016-10-27 22:05:52 -07002078
Michael Layzell734adb42017-06-07 16:58:31 -04002079 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002080 impl Parse for ExprTryBlock {
2081 fn parse(input: ParseStream) -> Result<Self> {
2082 Ok(ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002083 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002084 try_token: input.parse()?,
2085 block: input.parse()?,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002086 })
David Tolnay60291082018-08-28 09:54:49 -07002087 }
Alex Crichton954046c2017-05-30 21:49:42 -07002088 }
Arnavion02ef13f2017-04-25 00:54:31 -07002089
Michael Layzell734adb42017-06-07 16:58:31 -04002090 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002091 impl Parse for ExprYield {
2092 fn parse(input: ParseStream) -> Result<Self> {
2093 Ok(ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002094 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002095 yield_token: input.parse()?,
2096 expr: {
2097 if !input.is_empty() && !input.peek(Token![,]) && !input.peek(Token![;]) {
2098 Some(input.parse()?)
2099 } else {
2100 None
2101 }
2102 },
Alex Crichtonfe110462017-06-01 12:49:27 -07002103 })
David Tolnay60291082018-08-28 09:54:49 -07002104 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002105 }
2106
2107 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002108 impl Parse for Arm {
2109 fn parse(input: ParseStream) -> Result<Self> {
2110 let requires_comma;
2111 Ok(Arm {
2112 attrs: input.call(Attribute::parse_outer)?,
2113 leading_vert: input.parse()?,
2114 pats: {
2115 let mut pats = Punctuated::new();
2116 let value: Pat = input.parse()?;
2117 pats.push_value(value);
2118 loop {
2119 if !input.peek(Token![|]) {
2120 break;
2121 }
2122 let punct = input.parse()?;
2123 pats.push_punct(punct);
2124 let value: Pat = input.parse()?;
2125 pats.push_value(value);
2126 }
2127 pats
2128 },
2129 guard: {
2130 if input.peek(Token![if]) {
2131 let if_token: Token![if] = input.parse()?;
2132 let guard: Expr = input.parse()?;
2133 Some((if_token, Box::new(guard)))
2134 } else {
2135 None
2136 }
2137 },
2138 fat_arrow_token: input.parse()?,
2139 body: {
David Tolnay01218d12018-08-29 18:13:07 -07002140 let body = input.call(expr_early)?;
David Tolnay60291082018-08-28 09:54:49 -07002141 requires_comma = arm_expr_requires_comma(&body);
2142 Box::new(body)
2143 },
2144 comma: {
2145 if requires_comma && !input.is_empty() {
2146 Some(input.parse()?)
2147 } else {
2148 input.parse()?
2149 }
2150 },
Michael Layzell92639a52017-06-01 00:07:44 -04002151 })
David Tolnay60291082018-08-28 09:54:49 -07002152 }
Alex Crichton954046c2017-05-30 21:49:42 -07002153 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002154
Michael Layzell734adb42017-06-07 16:58:31 -04002155 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002156 fn expr_closure(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprClosure> {
2157 let attrs = input.call(Attribute::parse_outer)?;
2158 let asyncness: Option<Token![async]> = input.parse()?;
2159 let movability: Option<Token![static]> = if asyncness.is_none() {
2160 input.parse()?
2161 } else {
2162 None
2163 };
2164 let capture: Option<Token![move]> = input.parse()?;
2165 let or1_token: Token![|] = input.parse()?;
2166
2167 let mut inputs = Punctuated::new();
2168 loop {
2169 if input.peek(Token![|]) {
2170 break;
2171 }
2172 let value = fn_arg(input)?;
2173 inputs.push_value(value);
2174 if input.peek(Token![|]) {
2175 break;
2176 }
2177 let punct: Token![,] = input.parse()?;
2178 inputs.push_punct(punct);
2179 }
2180
2181 let or2_token: Token![|] = input.parse()?;
2182
2183 let (output, body) = if input.peek(Token![->]) {
2184 let arrow_token: Token![->] = input.parse()?;
2185 let ty: Type = input.parse()?;
2186 let body: Block = input.parse()?;
2187 let output = ReturnType::Type(arrow_token, Box::new(ty));
2188 let block = Expr::Block(ExprBlock {
2189 attrs: Vec::new(),
2190 label: None,
2191 block: body,
2192 });
2193 (output, block)
2194 } else {
2195 let body = ambiguous_expr(input, allow_struct, AllowBlock(true))?;
2196 (ReturnType::Default, body)
2197 };
2198
2199 Ok(ExprClosure {
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002200 attrs: attrs,
2201 asyncness: asyncness,
2202 movability: movability,
2203 capture: capture,
David Tolnay60291082018-08-28 09:54:49 -07002204 or1_token: or1_token,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002205 inputs: inputs,
David Tolnay60291082018-08-28 09:54:49 -07002206 or2_token: or2_token,
2207 output: output,
2208 body: Box::new(body),
2209 })
David Tolnay02a9c6f2018-08-24 18:58:45 -04002210 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002211
2212 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002213 impl Parse for ExprAsync {
2214 fn parse(input: ParseStream) -> Result<Self> {
2215 Ok(ExprAsync {
2216 attrs: input.call(Attribute::parse_outer)?,
2217 async_token: input.parse()?,
2218 capture: input.parse()?,
2219 block: input.parse()?,
2220 })
2221 }
2222 }
Gregory Katz3e562cc2016-09-28 18:33:02 -04002223
Michael Layzell734adb42017-06-07 16:58:31 -04002224 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002225 fn fn_arg(input: ParseStream) -> Result<FnArg> {
2226 let pat: Pat = input.parse()?;
2227
2228 if input.peek(Token![:]) {
2229 Ok(FnArg::Captured(ArgCaptured {
2230 pat: pat,
2231 colon_token: input.parse()?,
2232 ty: input.parse()?,
2233 }))
2234 } else {
2235 Ok(FnArg::Inferred(pat))
2236 }
2237 }
2238
2239 #[cfg(feature = "full")]
2240 impl Parse for ExprWhile {
2241 fn parse(input: ParseStream) -> Result<Self> {
2242 let outer_attrs = input.call(Attribute::parse_outer)?;
2243 let label: Option<Label> = input.parse()?;
2244 let while_token: Token![while] = input.parse()?;
2245 let cond = expr_no_struct(input)?;
2246
2247 let content;
2248 let brace_token = braced!(content in input);
2249 let inner_attrs = content.call(Attribute::parse_inner)?;
2250 let stmts = content.call(Block::parse_within)?;
2251
2252 Ok(ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002253 attrs: {
2254 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002255 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002256 attrs
2257 },
2258 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002259 while_token: while_token,
Michael Layzell92639a52017-06-01 00:07:44 -04002260 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002261 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002262 brace_token: brace_token,
2263 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002264 },
Michael Layzell92639a52017-06-01 00:07:44 -04002265 })
David Tolnay60291082018-08-28 09:54:49 -07002266 }
Alex Crichton954046c2017-05-30 21:49:42 -07002267 }
2268
Michael Layzell734adb42017-06-07 16:58:31 -04002269 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002270 impl Parse for ExprWhileLet {
2271 fn parse(input: ParseStream) -> Result<Self> {
2272 let outer_attrs = input.call(Attribute::parse_outer)?;
2273 let label: Option<Label> = input.parse()?;
2274 let while_token: Token![while] = input.parse()?;
2275 let let_token: Token![let] = input.parse()?;
2276
2277 let mut pats = Punctuated::new();
2278 let value: Pat = input.parse()?;
2279 pats.push_value(value);
2280 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2281 let punct = input.parse()?;
2282 pats.push_punct(punct);
2283 let value: Pat = input.parse()?;
2284 pats.push_value(value);
2285 }
2286
2287 let eq_token: Token![=] = input.parse()?;
2288 let expr = expr_no_struct(input)?;
2289
2290 let content;
2291 let brace_token = braced!(content in input);
2292 let inner_attrs = content.call(Attribute::parse_inner)?;
2293 let stmts = content.call(Block::parse_within)?;
2294
2295 Ok(ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002296 attrs: {
2297 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002298 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002299 attrs
2300 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002301 label: label,
David Tolnay60291082018-08-28 09:54:49 -07002302 while_token: while_token,
2303 let_token: let_token,
David Tolnay5d314dc2018-07-21 16:40:01 -07002304 pats: pats,
David Tolnay60291082018-08-28 09:54:49 -07002305 eq_token: eq_token,
2306 expr: Box::new(expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07002307 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07002308 brace_token: brace_token,
2309 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002310 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002311 })
David Tolnay60291082018-08-28 09:54:49 -07002312 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002313 }
2314
2315 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002316 impl Parse for Label {
2317 fn parse(input: ParseStream) -> Result<Self> {
2318 Ok(Label {
2319 name: input.parse()?,
2320 colon_token: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002321 })
David Tolnay60291082018-08-28 09:54:49 -07002322 }
Alex Crichton954046c2017-05-30 21:49:42 -07002323 }
2324
Michael Layzell734adb42017-06-07 16:58:31 -04002325 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002326 impl Parse for Option<Label> {
2327 fn parse(input: ParseStream) -> Result<Self> {
2328 if input.peek(Lifetime) {
2329 input.parse().map(Some)
2330 } else {
2331 Ok(None)
2332 }
2333 }
2334 }
2335
2336 #[cfg(feature = "full")]
2337 impl Parse for ExprContinue {
2338 fn parse(input: ParseStream) -> Result<Self> {
2339 Ok(ExprContinue {
2340 attrs: input.call(Attribute::parse_outer)?,
2341 continue_token: input.parse()?,
2342 label: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002343 })
David Tolnay60291082018-08-28 09:54:49 -07002344 }
Alex Crichton954046c2017-05-30 21:49:42 -07002345 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002346
Michael Layzell734adb42017-06-07 16:58:31 -04002347 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002348 fn expr_break(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprBreak> {
2349 Ok(ExprBreak {
2350 attrs: input.call(Attribute::parse_outer)?,
2351 break_token: input.parse()?,
2352 label: input.parse()?,
2353 expr: {
2354 if input.is_empty()
2355 || input.peek(Token![,])
2356 || input.peek(Token![;])
2357 || !allow_struct.0 && input.peek(token::Brace)
2358 {
2359 None
2360 } else {
2361 // We can't allow blocks after a `break` expression when we
2362 // wouldn't allow structs, as this expression is ambiguous.
2363 let allow_block = AllowBlock(allow_struct.0);
2364 let expr = ambiguous_expr(input, allow_struct, allow_block)?;
2365 Some(Box::new(expr))
Michael Layzell92639a52017-06-01 00:07:44 -04002366 }
David Tolnay60291082018-08-28 09:54:49 -07002367 },
2368 })
Alex Crichton954046c2017-05-30 21:49:42 -07002369 }
2370
Michael Layzell734adb42017-06-07 16:58:31 -04002371 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002372 fn expr_ret(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprReturn> {
2373 Ok(ExprReturn {
2374 attrs: input.call(Attribute::parse_outer)?,
2375 return_token: input.parse()?,
2376 expr: {
2377 if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) {
2378 None
2379 } else {
2380 // NOTE: return is greedy and eats blocks after it even when in a
2381 // position where structs are not allowed, such as in if statement
2382 // conditions. For example:
2383 //
2384 // if return { println!("A") } {} // Prints "A"
2385 let expr = ambiguous_expr(input, allow_struct, AllowBlock(true))?;
2386 Some(Box::new(expr))
2387 }
2388 },
2389 })
2390 }
2391
2392 #[cfg(feature = "full")]
2393 impl Parse for ExprStruct {
2394 fn parse(input: ParseStream) -> Result<Self> {
2395 let outer_attrs = input.call(Attribute::parse_outer)?;
2396 let path: Path = input.parse()?;
2397
David Tolnay6e1e5052018-08-30 10:21:48 -07002398 expr_struct_helper(input, outer_attrs, path)
2399 }
2400 }
David Tolnay60291082018-08-28 09:54:49 -07002401
David Tolnay6e1e5052018-08-30 10:21:48 -07002402 #[cfg(feature = "full")]
2403 fn expr_struct_helper(input: ParseStream, outer_attrs: Vec<Attribute>, path: Path) -> Result<ExprStruct> {
2404 let content;
2405 let brace_token = braced!(content in input);
2406 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay60291082018-08-28 09:54:49 -07002407
David Tolnay6e1e5052018-08-30 10:21:48 -07002408 let mut fields = Punctuated::new();
2409 loop {
2410 let attrs = content.call(Attribute::parse_outer)?;
2411 if content.fork().parse::<Member>().is_err() {
2412 if attrs.is_empty() {
David Tolnay60291082018-08-28 09:54:49 -07002413 break;
David Tolnay6e1e5052018-08-30 10:21:48 -07002414 } else {
2415 return Err(content.error("expected struct field"));
David Tolnay60291082018-08-28 09:54:49 -07002416 }
David Tolnay60291082018-08-28 09:54:49 -07002417 }
2418
David Tolnay6e1e5052018-08-30 10:21:48 -07002419 let member: Member = content.parse()?;
2420 let (colon_token, value) = if content.peek(Token![:]) || !member.is_named() {
2421 let colon_token: Token![:] = content.parse()?;
2422 let value: Expr = content.parse()?;
2423 (Some(colon_token), value)
2424 } else if let Member::Named(ref ident) = member {
2425 let value = Expr::Path(ExprPath {
2426 attrs: Vec::new(),
2427 qself: None,
2428 path: Path::from(ident.clone()),
2429 });
2430 (None, value)
David Tolnay60291082018-08-28 09:54:49 -07002431 } else {
David Tolnay6e1e5052018-08-30 10:21:48 -07002432 unreachable!()
David Tolnay60291082018-08-28 09:54:49 -07002433 };
2434
David Tolnay6e1e5052018-08-30 10:21:48 -07002435 fields.push(FieldValue {
2436 attrs: attrs,
2437 member: member,
2438 colon_token: colon_token,
2439 expr: value,
2440 });
2441
2442 if !content.peek(Token![,]) {
2443 break;
2444 }
2445 let punct: Token![,] = content.parse()?;
2446 fields.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002447 }
David Tolnay6e1e5052018-08-30 10:21:48 -07002448
2449 let (dot2_token, rest) = if fields.empty_or_trailing() && content.peek(Token![..]) {
2450 let dot2_token: Token![..] = content.parse()?;
2451 let rest: Expr = content.parse()?;
2452 (Some(dot2_token), Some(Box::new(rest)))
2453 } else {
2454 (None, None)
2455 };
2456
2457 Ok(ExprStruct {
2458 attrs: {
2459 let mut attrs = outer_attrs;
2460 attrs.extend(inner_attrs);
2461 attrs
2462 },
2463 brace_token: brace_token,
2464 path: path,
2465 fields: fields,
2466 dot2_token: dot2_token,
2467 rest: rest,
2468 })
Alex Crichton954046c2017-05-30 21:49:42 -07002469 }
David Tolnay055a7042016-10-02 19:23:54 -07002470
Michael Layzell734adb42017-06-07 16:58:31 -04002471 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002472 impl Parse for ExprRepeat {
2473 fn parse(input: ParseStream) -> Result<Self> {
2474 let outer_attrs = input.call(Attribute::parse_outer)?;
2475
2476 let content;
2477 let bracket_token = bracketed!(content in input);
2478 let inner_attrs = content.call(Attribute::parse_inner)?;
2479 let expr: Expr = content.parse()?;
2480 let semi_token: Token![;] = content.parse()?;
2481 let len: Expr = content.parse()?;
2482
2483 Ok(ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002484 attrs: {
2485 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002486 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002487 attrs
2488 },
David Tolnay60291082018-08-28 09:54:49 -07002489 bracket_token: bracket_token,
2490 expr: Box::new(expr),
2491 semi_token: semi_token,
2492 len: Box::new(len),
Michael Layzell92639a52017-06-01 00:07:44 -04002493 })
David Tolnay60291082018-08-28 09:54:49 -07002494 }
Alex Crichton954046c2017-05-30 21:49:42 -07002495 }
David Tolnay055a7042016-10-02 19:23:54 -07002496
Michael Layzell734adb42017-06-07 16:58:31 -04002497 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002498 impl Parse for ExprUnsafe {
2499 fn parse(input: ParseStream) -> Result<Self> {
2500 let outer_attrs = input.call(Attribute::parse_outer)?;
2501 let unsafe_token: Token![unsafe] = input.parse()?;
2502
2503 let content;
2504 let brace_token = braced!(content in input);
2505 let inner_attrs = content.call(Attribute::parse_inner)?;
2506 let stmts = content.call(Block::parse_within)?;
2507
2508 Ok(ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002509 attrs: {
2510 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002511 attrs.extend(inner_attrs);
David Tolnayc4be3512018-08-27 06:25:44 -07002512 attrs
2513 },
David Tolnay60291082018-08-28 09:54:49 -07002514 unsafe_token: unsafe_token,
David Tolnayc4be3512018-08-27 06:25:44 -07002515 block: Block {
David Tolnay60291082018-08-28 09:54:49 -07002516 brace_token: brace_token,
2517 stmts: stmts,
David Tolnayc4be3512018-08-27 06:25:44 -07002518 },
Nika Layzell640832a2017-12-04 13:37:09 -05002519 })
David Tolnay60291082018-08-28 09:54:49 -07002520 }
Nika Layzell640832a2017-12-04 13:37:09 -05002521 }
2522
2523 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002524 impl Parse for ExprBlock {
2525 fn parse(input: ParseStream) -> Result<Self> {
2526 let outer_attrs = input.call(Attribute::parse_outer)?;
2527 let label: Option<Label> = input.parse()?;
2528
2529 let content;
2530 let brace_token = braced!(content in input);
2531 let inner_attrs = content.call(Attribute::parse_inner)?;
2532 let stmts = content.call(Block::parse_within)?;
2533
2534 Ok(ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002535 attrs: {
2536 let mut attrs = outer_attrs;
David Tolnay60291082018-08-28 09:54:49 -07002537 attrs.extend(inner_attrs);
David Tolnay5d314dc2018-07-21 16:40:01 -07002538 attrs
2539 },
David Tolnay1d8e9962018-08-24 19:04:20 -04002540 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002541 block: Block {
David Tolnay60291082018-08-28 09:54:49 -07002542 brace_token: brace_token,
2543 stmts: stmts,
David Tolnay5d314dc2018-07-21 16:40:01 -07002544 },
Michael Layzell92639a52017-06-01 00:07:44 -04002545 })
David Tolnay60291082018-08-28 09:54:49 -07002546 }
Alex Crichton954046c2017-05-30 21:49:42 -07002547 }
David Tolnay89e05672016-10-02 14:39:42 -07002548
Michael Layzell734adb42017-06-07 16:58:31 -04002549 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002550 fn expr_range(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprRange> {
2551 Ok(ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05002552 attrs: Vec::new(),
2553 from: None,
David Tolnay60291082018-08-28 09:54:49 -07002554 limits: input.parse()?,
2555 to: {
2556 if input.is_empty()
2557 || input.peek(Token![,])
2558 || input.peek(Token![;])
2559 || !allow_struct.0 && input.peek(token::Brace)
2560 {
2561 None
2562 } else {
2563 let to = ambiguous_expr(input, allow_struct, AllowBlock(allow_struct.0))?;
2564 Some(Box::new(to))
2565 }
2566 },
2567 })
2568 }
David Tolnay438c9052016-10-07 23:24:48 -07002569
Michael Layzell734adb42017-06-07 16:58:31 -04002570 #[cfg(feature = "full")]
David Tolnayc4c631e2018-08-27 10:44:37 -07002571 impl Parse for RangeLimits {
2572 fn parse(input: ParseStream) -> Result<Self> {
2573 let lookahead = input.lookahead1();
2574 if lookahead.peek(Token![..=]) {
2575 input.parse().map(RangeLimits::Closed)
2576 } else if lookahead.peek(Token![...]) {
2577 let dot3: Token![...] = input.parse()?;
2578 Ok(RangeLimits::Closed(Token![..=](dot3.spans)))
2579 } else if lookahead.peek(Token![..]) {
2580 input.parse().map(RangeLimits::HalfOpen)
2581 } else {
2582 Err(lookahead.error())
2583 }
2584 }
Alex Crichton954046c2017-05-30 21:49:42 -07002585 }
David Tolnay438c9052016-10-07 23:24:48 -07002586
David Tolnay60291082018-08-28 09:54:49 -07002587 impl Parse for ExprPath {
2588 fn parse(input: ParseStream) -> Result<Self> {
2589 #[cfg(not(feature = "full"))]
2590 let attrs = Vec::new();
2591 #[cfg(feature = "full")]
2592 let attrs = input.call(Attribute::parse_outer)?;
David Tolnayeb981bb2018-07-21 19:31:38 -07002593
David Tolnay60291082018-08-28 09:54:49 -07002594 let (qself, path) = path::parsing::qpath(input, true)?;
2595
2596 Ok(ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002597 attrs: attrs,
David Tolnay60291082018-08-28 09:54:49 -07002598 qself: qself,
2599 path: path,
Michael Layzell92639a52017-06-01 00:07:44 -04002600 })
David Tolnay60291082018-08-28 09:54:49 -07002601 }
Alex Crichton954046c2017-05-30 21:49:42 -07002602 }
David Tolnay42602292016-10-01 22:25:45 -07002603
Michael Layzell734adb42017-06-07 16:58:31 -04002604 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002605 impl Parse for Block {
2606 fn parse(input: ParseStream) -> Result<Self> {
2607 let content;
2608 Ok(Block {
2609 brace_token: braced!(content in input),
2610 stmts: content.call(Block::parse_within)?,
Michael Layzell92639a52017-06-01 00:07:44 -04002611 })
David Tolnay60291082018-08-28 09:54:49 -07002612 }
Alex Crichton954046c2017-05-30 21:49:42 -07002613 }
David Tolnay939766a2016-09-23 23:48:12 -07002614
Michael Layzell734adb42017-06-07 16:58:31 -04002615 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002616 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002617 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
2618 input.step_cursor(|cursor| Self::old_parse_within(*cursor))
2619 }
2620
2621 named!(old_parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002622 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002623 mut standalone: many0!(do_parse!(
2624 stmt: syn!(Stmt) >>
2625 many0!(punct!(;)) >>
2626 (stmt)
2627 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002628 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002629 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002630 mut e: syn!(Expr) >>
2631 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002632 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002633 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002634 })
2635 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002636 (match last {
2637 None => standalone,
2638 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002639 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002640 standalone
2641 }
2642 })
2643 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002644 }
2645
Michael Layzell734adb42017-06-07 16:58:31 -04002646 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002647 impl Parse for Stmt {
2648 fn parse(input: ParseStream) -> Result<Self> {
2649 let ahead = input.fork();
2650 ahead.call(Attribute::parse_outer)?;
David Tolnay939766a2016-09-23 23:48:12 -07002651
David Tolnay60291082018-08-28 09:54:49 -07002652 // TODO: better error messages
2653 if {
2654 let ahead = ahead.fork();
2655 // Only parse braces here; paren and bracket will get parsed as
2656 // expression statements
2657 ahead.call(Path::parse_mod_style).is_ok()
2658 && ahead.parse::<Token![!]>().is_ok()
2659 && (ahead.peek(token::Brace) || ahead.peek(Ident))
2660 } {
2661 stmt_mac(input)
2662 } else if ahead.peek(Token![let]) {
2663 stmt_local(input).map(Stmt::Local)
2664 } else if ahead.peek(Token![pub])
2665 || ahead.peek(Token![crate]) && !ahead.peek2(Token![::])
2666 || ahead.peek(Token![extern]) && !ahead.peek2(Token![::])
2667 || ahead.peek(Token![use])
2668 || ahead.peek(Token![static]) && (ahead.peek2(Token![mut]) || ahead.peek2(Ident))
2669 || ahead.peek(Token![const])
2670 || ahead.peek(Token![unsafe]) && !ahead.peek2(token::Brace)
2671 || ahead.peek(Token![async]) && (ahead.peek2(Token![extern]) || ahead.peek2(Token![fn]))
2672 || ahead.peek(Token![fn])
2673 || ahead.peek(Token![mod])
2674 || ahead.peek(Token![type])
2675 || ahead.peek(Token![existential]) && ahead.peek2(Token![type])
2676 || ahead.peek(Token![struct])
2677 || ahead.peek(Token![enum])
2678 || ahead.peek(Token![union]) && ahead.peek2(Ident)
2679 || ahead.peek(Token![auto]) && ahead.peek2(Token![trait])
2680 || ahead.peek(Token![trait])
2681 || ahead.peek(Token![default]) && (ahead.peek2(Token![unsafe]) || ahead.peek2(Token![impl]))
2682 || ahead.peek(Token![impl])
2683 || ahead.peek(Token![macro])
2684 {
2685 input.parse().map(Stmt::Item)
Michael Layzell35418782017-06-07 09:20:25 -04002686 } else {
David Tolnay01218d12018-08-29 18:13:07 -07002687 input.call(stmt_expr)
Michael Layzell35418782017-06-07 09:20:25 -04002688 }
David Tolnay60291082018-08-28 09:54:49 -07002689 }
Alex Crichton954046c2017-05-30 21:49:42 -07002690 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002691
Michael Layzell734adb42017-06-07 16:58:31 -04002692 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002693 fn stmt_mac(input: ParseStream) -> Result<Stmt> {
2694 let attrs = input.call(Attribute::parse_outer)?;
2695 let path = input.call(Path::parse_mod_style)?;
2696 let bang_token: Token![!] = input.parse()?;
2697 let ident: Option<Ident> = input.parse()?;
2698 let (delimiter, tts) = mac::parse_delimiter(input)?;
2699 let semi_token: Option<Token![;]> = input.parse()?;
2700
2701 Ok(Stmt::Item(Item::Macro(ItemMacro {
2702 attrs: attrs,
2703 ident: ident,
2704 mac: Macro {
2705 path: path,
2706 bang_token: bang_token,
2707 delimiter: delimiter,
2708 tts: tts,
2709 },
2710 semi_token: semi_token,
2711 })))
Alex Crichton954046c2017-05-30 21:49:42 -07002712 }
David Tolnay84aa0752016-10-02 23:01:13 -07002713
Michael Layzell734adb42017-06-07 16:58:31 -04002714 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002715 fn stmt_local(input: ParseStream) -> Result<Local> {
2716 Ok(Local {
2717 attrs: input.call(Attribute::parse_outer)?,
2718 let_token: input.parse()?,
2719 pats: {
2720 let mut pats = Punctuated::new();
2721 let value: Pat = input.parse()?;
2722 pats.push_value(value);
2723 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2724 let punct = input.parse()?;
2725 pats.push_punct(punct);
2726 let value: Pat = input.parse()?;
2727 pats.push_value(value);
2728 }
2729 pats
2730 },
2731 ty: {
2732 if input.peek(Token![:]) {
2733 let colon_token: Token![:] = input.parse()?;
2734 let ty: Type = input.parse()?;
2735 Some((colon_token, Box::new(ty)))
2736 } else {
2737 None
2738 }
2739 },
2740 init: {
2741 if input.peek(Token![=]) {
2742 let eq_token: Token![=] = input.parse()?;
2743 let init: Expr = input.parse()?;
2744 Some((eq_token, Box::new(init)))
2745 } else {
2746 None
2747 }
2748 },
2749 semi_token: input.parse()?,
2750 })
2751 }
2752
2753 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07002754 fn stmt_expr(input: ParseStream) -> Result<Stmt> {
David Tolnay60291082018-08-28 09:54:49 -07002755 let mut attrs = input.call(Attribute::parse_outer)?;
David Tolnay01218d12018-08-29 18:13:07 -07002756 let mut e = expr_early(input)?;
David Tolnay60291082018-08-28 09:54:49 -07002757
2758 attrs.extend(e.replace_attrs(Vec::new()));
2759 e.replace_attrs(attrs);
2760
2761 if input.peek(Token![;]) {
David Tolnay01218d12018-08-29 18:13:07 -07002762 return Ok(Stmt::Semi(e, input.parse()?));
David Tolnay60291082018-08-28 09:54:49 -07002763 }
David Tolnay60291082018-08-28 09:54:49 -07002764
David Tolnay01218d12018-08-29 18:13:07 -07002765 match e {
2766 Expr::IfLet(_) |
2767 Expr::If(_) |
2768 Expr::WhileLet(_) |
2769 Expr::While(_) |
2770 Expr::ForLoop(_) |
2771 Expr::Loop(_) |
2772 Expr::Match(_) |
2773 Expr::TryBlock(_) |
2774 Expr::Yield(_) |
2775 Expr::Unsafe(_) |
2776 Expr::Block(_) => Ok(Stmt::Expr(e)),
2777 _ => {
2778 Err(input.error("expected semicolon"))
2779 }
2780 }
David Tolnay60291082018-08-28 09:54:49 -07002781 }
2782
2783 #[cfg(feature = "full")]
2784 impl Parse for Pat {
2785 fn parse(input: ParseStream) -> Result<Self> {
2786 // TODO: better error messages
2787 let lookahead = input.lookahead1();
2788 if lookahead.peek(Token![_]) {
2789 input.parse().map(Pat::Wild)
2790 } else if lookahead.peek(Token![box]) {
2791 input.parse().map(Pat::Box)
2792 } else if input.fork().parse::<PatRange>().is_ok() {
2793 // must be before Pat::Lit
2794 input.parse().map(Pat::Range)
2795 } else if input.fork().parse::<PatTupleStruct>().is_ok() {
2796 // must be before Pat::Ident
2797 input.parse().map(Pat::TupleStruct)
2798 } else if input.fork().parse::<PatStruct>().is_ok() {
2799 // must be before Pat::Ident
2800 input.parse().map(Pat::Struct)
2801 } else if input.fork().parse::<PatMacro>().is_ok() {
2802 // must be before Pat::Ident
2803 input.parse().map(Pat::Macro)
2804 } else if input.fork().parse::<PatLit>().is_ok() {
2805 // must be before Pat::Ident
2806 input.parse().map(Pat::Lit)
2807 } else if input.fork().parse::<PatIdent>().is_ok() {
2808 input.parse().map(Pat::Ident)
2809 } else if input.fork().parse::<PatPath>().is_ok() {
2810 input.parse().map(Pat::Path)
2811 } else if lookahead.peek(token::Paren) {
2812 input.parse().map(Pat::Tuple)
2813 } else if lookahead.peek(Token![&]) {
2814 input.parse().map(Pat::Ref)
2815 } else if lookahead.peek(token::Bracket) {
2816 input.parse().map(Pat::Slice)
2817 } else {
2818 Err(lookahead.error())
2819 }
2820 }
2821 }
2822
2823 #[cfg(feature = "full")]
2824 impl Parse for PatWild {
2825 fn parse(input: ParseStream) -> Result<Self> {
2826 Ok(PatWild {
2827 underscore_token: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002828 })
David Tolnay60291082018-08-28 09:54:49 -07002829 }
Alex Crichton954046c2017-05-30 21:49:42 -07002830 }
2831
Michael Layzell734adb42017-06-07 16:58:31 -04002832 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002833 impl Parse for PatBox {
2834 fn parse(input: ParseStream) -> Result<Self> {
2835 Ok(PatBox {
2836 box_token: input.parse()?,
2837 pat: input.parse()?,
2838 })
2839 }
2840 }
2841
2842 #[cfg(feature = "full")]
2843 impl Parse for PatIdent {
2844 fn parse(input: ParseStream) -> Result<Self> {
2845 Ok(PatIdent {
2846 by_ref: input.parse()?,
2847 mutability: input.parse()?,
2848 ident: {
2849 let ident = if input.peek(Ident) || input.peek(Token![self]) {
2850 input.call(Ident::parse_any2)?
2851 } else {
2852 return Err(input.error("expected identifier or `self`"));
2853 };
2854 if input.peek(Token![<]) || input.peek(Token![::]) {
2855 return Err(input.error("unexpected token"));
2856 }
2857 ident
2858 },
2859 subpat: {
2860 if input.peek(Token![@]) {
2861 let at_token: Token![@] = input.parse()?;
2862 let subpat: Pat = input.parse()?;
2863 Some((at_token, Box::new(subpat)))
2864 } else {
2865 None
2866 }
2867 },
2868 })
2869 }
2870 }
2871
2872 #[cfg(feature = "full")]
2873 impl Parse for PatTupleStruct {
2874 fn parse(input: ParseStream) -> Result<Self> {
2875 Ok(PatTupleStruct {
2876 path: input.parse()?,
2877 pat: input.parse()?,
2878 })
2879 }
2880 }
2881
2882 #[cfg(feature = "full")]
2883 impl Parse for PatStruct {
2884 fn parse(input: ParseStream) -> Result<Self> {
2885 let path: Path = input.parse()?;
2886
2887 let content;
2888 let brace_token = braced!(content in input);
2889
2890 let mut fields = Punctuated::new();
2891 while !content.is_empty() && !content.peek(Token![..]) {
2892 let value: FieldPat = content.parse()?;
2893 fields.push_value(value);
2894 if !content.peek(Token![,]) {
2895 break;
2896 }
2897 let punct: Token![,] = content.parse()?;
2898 fields.push_punct(punct);
2899 }
2900
2901 let dot2_token = if fields.empty_or_trailing() && content.peek(Token![..]) {
2902 Some(content.parse()?)
2903 } else {
2904 None
2905 };
2906
2907 Ok(PatStruct {
2908 path: path,
2909 brace_token: brace_token,
2910 fields: fields,
2911 dot2_token: dot2_token,
2912 })
2913 }
2914 }
2915
2916 #[cfg(feature = "full")]
2917 impl Parse for FieldPat {
2918 fn parse(input: ParseStream) -> Result<Self> {
2919 let boxed: Option<Token![box]> = input.parse()?;
2920 let by_ref: Option<Token![ref]> = input.parse()?;
2921 let mutability: Option<Token![mut]> = input.parse()?;
2922 let member: Member = input.parse()?;
2923
2924 if boxed.is_none() && by_ref.is_none() && mutability.is_none() && input.peek(Token![:])
2925 || member.is_unnamed()
2926 {
2927 return Ok(FieldPat {
2928 attrs: Vec::new(),
2929 member: member,
2930 colon_token: input.parse()?,
2931 pat: input.parse()?,
2932 });
2933 }
2934
2935 let ident = match member {
2936 Member::Named(ident) => ident,
2937 Member::Unnamed(_) => unreachable!(),
2938 };
2939
2940 let mut pat = Pat::Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002941 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002942 mutability: mutability,
David Tolnay60291082018-08-28 09:54:49 -07002943 ident: ident.clone(),
2944 subpat: None,
2945 });
Alex Crichton954046c2017-05-30 21:49:42 -07002946
David Tolnay60291082018-08-28 09:54:49 -07002947 if let Some(boxed) = boxed {
2948 pat = Pat::Box(PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002949 pat: Box::new(pat),
David Tolnay60291082018-08-28 09:54:49 -07002950 box_token: boxed,
2951 });
2952 }
2953
2954 Ok(FieldPat {
2955 member: Member::Named(ident),
2956 pat: Box::new(pat),
2957 attrs: Vec::new(),
2958 colon_token: None,
2959 })
2960 }
Alex Crichton954046c2017-05-30 21:49:42 -07002961 }
2962
David Tolnay1501f7e2018-08-27 14:21:03 -07002963 impl Parse for Member {
2964 fn parse(input: ParseStream) -> Result<Self> {
2965 if input.peek(Ident) {
2966 input.parse().map(Member::Named)
2967 } else if input.peek(LitInt) {
2968 input.parse().map(Member::Unnamed)
2969 } else {
2970 Err(input.error("expected identifier or integer"))
2971 }
2972 }
David Tolnay85b69a42017-12-27 20:43:10 -05002973 }
2974
David Tolnay1501f7e2018-08-27 14:21:03 -07002975 impl Parse for Index {
2976 fn parse(input: ParseStream) -> Result<Self> {
2977 let lit: LitInt = input.parse()?;
2978 if let IntSuffix::None = lit.suffix() {
2979 Ok(Index {
2980 index: lit.value() as u32,
2981 span: lit.span(),
2982 })
2983 } else {
2984 Err(input.error("expected unsuffixed integer"))
2985 }
2986 }
David Tolnay85b69a42017-12-27 20:43:10 -05002987 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002988
Michael Layzell734adb42017-06-07 16:58:31 -04002989 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002990 impl Parse for PatPath {
2991 fn parse(input: ParseStream) -> Result<Self> {
2992 let p: ExprPath = input.parse()?;
2993 Ok(PatPath {
2994 qself: p.qself,
2995 path: p.path,
2996 })
2997 }
Alex Crichton954046c2017-05-30 21:49:42 -07002998 }
David Tolnay9636c052016-10-02 17:11:17 -07002999
Michael Layzell734adb42017-06-07 16:58:31 -04003000 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003001 impl Parse for PatTuple {
3002 fn parse(input: ParseStream) -> Result<Self> {
3003 let content;
3004 let paren_token = parenthesized!(content in input);
3005
3006 let mut front = Punctuated::new();
3007 let mut dot2_token = None::<Token![..]>;
3008 let mut comma_token = None::<Token![,]>;
3009 loop {
3010 if content.is_empty() {
3011 break;
Michael Layzell92639a52017-06-01 00:07:44 -04003012 }
David Tolnay60291082018-08-28 09:54:49 -07003013 if content.peek(Token![..]) {
3014 dot2_token = Some(content.parse()?);
3015 comma_token = content.parse()?;
3016 break;
3017 }
3018 let value: Pat = content.parse()?;
3019 front.push_value(value);
3020 if content.is_empty() {
3021 break;
3022 }
3023 let punct = content.parse()?;
3024 front.push_punct(punct);
3025 }
David Tolnayfbb73232016-10-03 01:00:06 -07003026
David Tolnay60291082018-08-28 09:54:49 -07003027 let back = if comma_token.is_some() {
3028 content.parse_synom(Punctuated::parse_terminated)?
Michael Layzell92639a52017-06-01 00:07:44 -04003029 } else {
David Tolnay60291082018-08-28 09:54:49 -07003030 Punctuated::new()
3031 };
3032
3033 Ok(PatTuple {
3034 paren_token: paren_token,
3035 front: front,
3036 dot2_token: dot2_token,
3037 comma_token: comma_token,
3038 back: back,
Michael Layzell92639a52017-06-01 00:07:44 -04003039 })
David Tolnay60291082018-08-28 09:54:49 -07003040 }
Alex Crichton954046c2017-05-30 21:49:42 -07003041 }
David Tolnaye1310902016-10-29 23:40:00 -07003042
Michael Layzell734adb42017-06-07 16:58:31 -04003043 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003044 impl Parse for PatRef {
3045 fn parse(input: ParseStream) -> Result<Self> {
3046 Ok(PatRef {
3047 and_token: input.parse()?,
3048 mutability: input.parse()?,
3049 pat: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04003050 })
David Tolnay60291082018-08-28 09:54:49 -07003051 }
Alex Crichton954046c2017-05-30 21:49:42 -07003052 }
David Tolnaye1310902016-10-29 23:40:00 -07003053
Michael Layzell734adb42017-06-07 16:58:31 -04003054 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003055 impl Parse for PatLit {
3056 fn parse(input: ParseStream) -> Result<Self> {
3057 if input.peek(Lit) || input.peek(Token![-]) && input.peek2(Lit) {
3058 Ok(PatLit {
3059 expr: input.call(pat_lit_expr)?,
3060 })
3061 } else {
3062 Err(input.error("expected literal pattern"))
3063 }
3064 }
3065 }
3066
3067 #[cfg(feature = "full")]
3068 impl Parse for PatRange {
3069 fn parse(input: ParseStream) -> Result<Self> {
3070 Ok(PatRange {
3071 lo: input.call(pat_lit_expr)?,
3072 limits: input.parse()?,
3073 hi: input.call(pat_lit_expr)?,
3074 })
3075 }
3076 }
3077
3078 #[cfg(feature = "full")]
3079 fn pat_lit_expr(input: ParseStream) -> Result<Box<Expr>> {
3080 let neg: Option<Token![-]> = input.parse()?;
3081
3082 let lookahead = input.lookahead1();
3083 let expr = if lookahead.peek(Lit) {
3084 Expr::Lit(input.parse()?)
3085 } else if lookahead.peek(Ident)
3086 || lookahead.peek(Token![::])
3087 || lookahead.peek(Token![<])
3088 || lookahead.peek(Token![self])
3089 || lookahead.peek(Token![Self])
3090 || lookahead.peek(Token![super])
3091 || lookahead.peek(Token![extern])
3092 || lookahead.peek(Token![crate])
3093 {
3094 Expr::Path(input.parse()?)
3095 } else {
3096 return Err(lookahead.error());
3097 };
3098
3099 Ok(Box::new(if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003100 Expr::Unary(ExprUnary {
3101 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003102 op: UnOp::Neg(neg),
David Tolnay60291082018-08-28 09:54:49 -07003103 expr: Box::new(expr),
David Tolnay3bc597f2017-12-31 02:31:11 -05003104 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003105 } else {
David Tolnay60291082018-08-28 09:54:49 -07003106 expr
3107 }))
Alex Crichton954046c2017-05-30 21:49:42 -07003108 }
David Tolnay323279a2017-12-29 11:26:32 -05003109
3110 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07003111 impl Parse for PatSlice {
3112 fn parse(input: ParseStream) -> Result<Self> {
3113 let content;
3114 let bracket_token = bracketed!(content in input);
3115
3116 let mut front = Punctuated::new();
3117 let mut middle = None;
3118 loop {
3119 if content.is_empty() || content.peek(Token![..]) {
3120 break;
3121 }
3122 let value: Pat = content.parse()?;
3123 if content.peek(Token![..]) {
3124 middle = Some(Box::new(value));
3125 break;
3126 }
3127 front.push_value(value);
3128 if content.is_empty() {
3129 break;
3130 }
3131 let punct = content.parse()?;
3132 front.push_punct(punct);
3133 }
3134
3135 let dot2_token: Option<Token![..]> = content.parse()?;
3136 let mut comma_token = None::<Token![,]>;
3137 let mut back = Punctuated::new();
3138 if dot2_token.is_some() {
3139 comma_token = content.parse()?;
3140 if comma_token.is_some() {
3141 loop {
3142 if content.is_empty() {
3143 break;
3144 }
3145 let value: Pat = content.parse()?;
3146 back.push_value(value);
3147 if content.is_empty() {
3148 break;
3149 }
3150 let punct = content.parse()?;
3151 back.push_punct(punct);
3152 }
3153 }
3154 }
3155
3156 Ok(PatSlice {
3157 bracket_token: bracket_token,
3158 front: front,
3159 middle: middle,
3160 dot2_token: dot2_token,
3161 comma_token: comma_token,
3162 back: back,
3163 })
3164 }
3165 }
3166
3167 #[cfg(feature = "full")]
3168 impl Parse for PatMacro {
3169 fn parse(input: ParseStream) -> Result<Self> {
3170 Ok(PatMacro {
3171 mac: input.parse()?,
3172 })
3173 }
David Tolnay323279a2017-12-29 11:26:32 -05003174 }
David Tolnay1501f7e2018-08-27 14:21:03 -07003175
3176 #[cfg(feature = "full")]
3177 impl Member {
3178 fn is_named(&self) -> bool {
3179 match *self {
3180 Member::Named(_) => true,
3181 Member::Unnamed(_) => false,
3182 }
3183 }
David Tolnay60291082018-08-28 09:54:49 -07003184
3185 fn is_unnamed(&self) -> bool {
3186 match *self {
3187 Member::Named(_) => false,
3188 Member::Unnamed(_) => true,
3189 }
3190 }
David Tolnay1501f7e2018-08-27 14:21:03 -07003191 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003192}
3193
David Tolnayf4bbbd92016-09-23 14:41:55 -07003194#[cfg(feature = "printing")]
3195mod printing {
3196 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003197 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003198 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003199 use proc_macro2::{Literal, TokenStream};
3200 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003201
David Tolnaybcf26022017-12-25 22:10:52 -05003202 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003203 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003204 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003205 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003206 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003207 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003208 e.to_tokens(tokens);
3209 });
3210 } else {
3211 e.to_tokens(tokens);
3212 }
3213 }
3214
David Tolnay8c91b882017-12-28 23:04:32 -05003215 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003216 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003217 tokens.append_all(attrs.outer());
3218 }
Michael Layzell734adb42017-06-07 16:58:31 -04003219
David Tolnayd997aef2018-07-21 18:42:31 -07003220 #[cfg(feature = "full")]
3221 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3222 tokens.append_all(attrs.inner());
3223 }
3224
David Tolnay8c91b882017-12-28 23:04:32 -05003225 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003226 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3227
3228 #[cfg(not(feature = "full"))]
3229 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003230
Michael Layzell734adb42017-06-07 16:58:31 -04003231 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003232 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003233 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003234 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003235 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003236 self.expr.to_tokens(tokens);
3237 }
3238 }
3239
Michael Layzell734adb42017-06-07 16:58:31 -04003240 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003241 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003242 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003243 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003244 self.place.to_tokens(tokens);
3245 self.arrow_token.to_tokens(tokens);
3246 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003247 }
3248 }
3249
Michael Layzell734adb42017-06-07 16:58:31 -04003250 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003251 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003252 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003253 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003254 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003255 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003256 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003257 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003258 }
3259 }
3260
3261 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003262 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003263 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003264 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003265 self.paren_token.surround(tokens, |tokens| {
3266 self.args.to_tokens(tokens);
3267 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003268 }
3269 }
3270
Michael Layzell734adb42017-06-07 16:58:31 -04003271 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003272 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003273 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003274 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003275 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003276 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003277 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003278 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003279 self.paren_token.surround(tokens, |tokens| {
3280 self.args.to_tokens(tokens);
3281 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003282 }
3283 }
3284
Michael Layzell734adb42017-06-07 16:58:31 -04003285 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003286 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003287 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003288 self.colon2_token.to_tokens(tokens);
3289 self.lt_token.to_tokens(tokens);
3290 self.args.to_tokens(tokens);
3291 self.gt_token.to_tokens(tokens);
3292 }
3293 }
3294
3295 #[cfg(feature = "full")]
3296 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003297 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003298 match *self {
3299 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3300 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3301 }
3302 }
3303 }
3304
3305 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003306 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003307 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003308 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003309 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003310 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003311 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003312 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003313 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003314 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003315 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003316 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003317 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003318 }
3319 }
3320
3321 impl ToTokens for ExprBinary {
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 Crichton62a0a592017-05-22 13:58:53 -07003324 self.left.to_tokens(tokens);
3325 self.op.to_tokens(tokens);
3326 self.right.to_tokens(tokens);
3327 }
3328 }
3329
3330 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003331 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003332 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003333 self.op.to_tokens(tokens);
3334 self.expr.to_tokens(tokens);
3335 }
3336 }
3337
David Tolnay8c91b882017-12-28 23:04:32 -05003338 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003339 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003340 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003341 self.lit.to_tokens(tokens);
3342 }
3343 }
3344
Alex Crichton62a0a592017-05-22 13:58:53 -07003345 impl ToTokens for ExprCast {
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.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003349 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003350 self.ty.to_tokens(tokens);
3351 }
3352 }
3353
David Tolnay0cf94f22017-12-28 23:46:26 -05003354 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003355 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003356 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003357 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003358 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003359 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003360 self.ty.to_tokens(tokens);
3361 }
3362 }
3363
Michael Layzell734adb42017-06-07 16:58:31 -04003364 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003365 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003366 if let Some((ref else_token, ref else_)) = *else_ {
3367 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003368
3369 // If we are not one of the valid expressions to exist in an else
3370 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003371 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003372 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003373 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003374 }
3375 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003376 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003377 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003378 });
3379 }
3380 }
3381 }
3382 }
3383
3384 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003385 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003386 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003387 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003388 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003389 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003390 self.then_branch.to_tokens(tokens);
3391 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003392 }
3393 }
3394
Michael Layzell734adb42017-06-07 16:58:31 -04003395 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003396 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003397 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003398 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003399 self.if_token.to_tokens(tokens);
3400 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003401 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003402 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003403 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003404 self.then_branch.to_tokens(tokens);
3405 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003406 }
3407 }
3408
Michael Layzell734adb42017-06-07 16:58:31 -04003409 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003410 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003411 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003412 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003413 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003414 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003415 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003416 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003417 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003418 tokens.append_all(&self.body.stmts);
3419 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003420 }
3421 }
3422
Michael Layzell734adb42017-06-07 16:58:31 -04003423 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003424 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003425 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003426 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003427 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003428 self.while_token.to_tokens(tokens);
3429 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003430 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003431 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003432 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003433 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003434 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003435 tokens.append_all(&self.body.stmts);
3436 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003437 }
3438 }
3439
Michael Layzell734adb42017-06-07 16:58:31 -04003440 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003441 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003442 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003443 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003444 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003445 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003446 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003447 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003448 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003449 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003450 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003451 tokens.append_all(&self.body.stmts);
3452 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003453 }
3454 }
3455
Michael Layzell734adb42017-06-07 16:58:31 -04003456 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003457 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003458 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003459 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003460 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003461 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003462 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003463 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003464 tokens.append_all(&self.body.stmts);
3465 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003466 }
3467 }
3468
Michael Layzell734adb42017-06-07 16:58:31 -04003469 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003470 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003471 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003472 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003473 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003474 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003475 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003476 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003477 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003478 arm.to_tokens(tokens);
3479 // Ensure that we have a comma after a non-block arm, except
3480 // for the last one.
3481 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003482 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003483 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003484 }
3485 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003486 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003487 }
3488 }
3489
Michael Layzell734adb42017-06-07 16:58:31 -04003490 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003491 impl ToTokens for ExprAsync {
3492 fn to_tokens(&self, tokens: &mut TokenStream) {
3493 outer_attrs_to_tokens(&self.attrs, tokens);
3494 self.async_token.to_tokens(tokens);
3495 self.capture.to_tokens(tokens);
3496 self.block.to_tokens(tokens);
3497 }
3498 }
3499
3500 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003501 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003502 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003503 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003504 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003505 self.block.to_tokens(tokens);
3506 }
3507 }
3508
Michael Layzell734adb42017-06-07 16:58:31 -04003509 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003510 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003511 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003512 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003513 self.yield_token.to_tokens(tokens);
3514 self.expr.to_tokens(tokens);
3515 }
3516 }
3517
3518 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003519 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003520 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003521 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003522 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003523 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003524 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003525 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003526 for input in self.inputs.pairs() {
3527 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003528 FnArg::Captured(ArgCaptured {
3529 ref pat,
3530 ty: Type::Infer(_),
3531 ..
3532 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003533 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003534 }
David Tolnay56080682018-01-06 14:01:52 -08003535 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003536 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003537 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003538 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003539 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003540 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003541 self.body.to_tokens(tokens);
3542 }
3543 }
3544
Michael Layzell734adb42017-06-07 16:58:31 -04003545 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003546 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003547 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003548 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003549 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003550 self.block.brace_token.surround(tokens, |tokens| {
3551 inner_attrs_to_tokens(&self.attrs, tokens);
3552 tokens.append_all(&self.block.stmts);
3553 });
Nika Layzell640832a2017-12-04 13:37:09 -05003554 }
3555 }
3556
3557 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003558 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003559 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003560 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003561 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003562 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003563 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003564 tokens.append_all(&self.block.stmts);
3565 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003566 }
3567 }
3568
Michael Layzell734adb42017-06-07 16:58:31 -04003569 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003570 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003571 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003572 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003573 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003574 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003575 self.right.to_tokens(tokens);
3576 }
3577 }
3578
Michael Layzell734adb42017-06-07 16:58:31 -04003579 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003580 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003581 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003582 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003583 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003584 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003585 self.right.to_tokens(tokens);
3586 }
3587 }
3588
3589 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003590 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003591 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003592 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003593 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003594 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003595 }
3596 }
3597
David Tolnay85b69a42017-12-27 20:43:10 -05003598 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003599 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003600 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003601 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003602 Member::Unnamed(ref index) => index.to_tokens(tokens),
3603 }
3604 }
3605 }
3606
David Tolnay85b69a42017-12-27 20:43:10 -05003607 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003608 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003609 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3610 lit.set_span(self.span);
3611 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003612 }
3613 }
3614
3615 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003616 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003617 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003618 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003619 self.bracket_token.surround(tokens, |tokens| {
3620 self.index.to_tokens(tokens);
3621 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003622 }
3623 }
3624
Michael Layzell734adb42017-06-07 16:58:31 -04003625 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003626 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003627 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003628 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003629 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003630 match self.limits {
3631 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3632 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3633 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003634 self.to.to_tokens(tokens);
3635 }
3636 }
3637
3638 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003639 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003640 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003641 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003642 }
3643 }
3644
Michael Layzell734adb42017-06-07 16:58:31 -04003645 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003646 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003647 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003648 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003649 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003650 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003651 self.expr.to_tokens(tokens);
3652 }
3653 }
3654
Michael Layzell734adb42017-06-07 16:58:31 -04003655 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003656 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003657 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003658 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003659 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003660 self.label.to_tokens(tokens);
3661 self.expr.to_tokens(tokens);
3662 }
3663 }
3664
Michael Layzell734adb42017-06-07 16:58:31 -04003665 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003666 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003667 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003668 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003669 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003670 self.label.to_tokens(tokens);
3671 }
3672 }
3673
Michael Layzell734adb42017-06-07 16:58:31 -04003674 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003675 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003676 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003677 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003678 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003679 self.expr.to_tokens(tokens);
3680 }
3681 }
3682
Michael Layzell734adb42017-06-07 16:58:31 -04003683 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003684 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003685 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003686 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003687 self.mac.to_tokens(tokens);
3688 }
3689 }
3690
3691 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003692 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003693 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003694 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003695 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003696 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003697 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003698 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003699 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003700 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003701 self.rest.to_tokens(tokens);
3702 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003703 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003704 }
3705 }
3706
Michael Layzell734adb42017-06-07 16:58:31 -04003707 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003708 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003709 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003710 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003711 self.bracket_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.expr.to_tokens(tokens);
3714 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003715 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003716 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003717 }
3718 }
3719
David Tolnaye98775f2017-12-28 23:17:00 -05003720 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003721 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003722 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003723 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003724 self.group_token.surround(tokens, |tokens| {
3725 self.expr.to_tokens(tokens);
3726 });
3727 }
3728 }
3729
Alex Crichton62a0a592017-05-22 13:58:53 -07003730 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003731 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003732 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003733 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003734 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003735 self.expr.to_tokens(tokens);
3736 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003737 }
3738 }
3739
Michael Layzell734adb42017-06-07 16:58:31 -04003740 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003741 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003742 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003743 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003744 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003745 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003746 }
3747 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003748
David Tolnay2ae520a2017-12-29 11:19:50 -05003749 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003750 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003751 self.tts.to_tokens(tokens);
3752 }
3753 }
3754
Michael Layzell734adb42017-06-07 16:58:31 -04003755 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003756 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003757 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003758 self.name.to_tokens(tokens);
3759 self.colon_token.to_tokens(tokens);
3760 }
3761 }
3762
3763 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003764 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003765 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003766 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003767 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003768 if let Some(ref colon_token) = self.colon_token {
3769 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003770 self.expr.to_tokens(tokens);
3771 }
David Tolnay055a7042016-10-02 19:23:54 -07003772 }
3773 }
3774
Michael Layzell734adb42017-06-07 16:58:31 -04003775 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003776 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003777 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003778 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003779 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003780 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003781 if let Some((ref if_token, ref guard)) = self.guard {
3782 if_token.to_tokens(tokens);
3783 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003784 }
David Tolnaydfb91432018-03-31 19:19:44 +02003785 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003786 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003787 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003788 }
3789 }
3790
Michael Layzell734adb42017-06-07 16:58:31 -04003791 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003792 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003793 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003794 self.underscore_token.to_tokens(tokens);
3795 }
3796 }
3797
Michael Layzell734adb42017-06-07 16:58:31 -04003798 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003799 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003800 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003801 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003802 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003803 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003804 if let Some((ref at_token, ref subpat)) = self.subpat {
3805 at_token.to_tokens(tokens);
3806 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003807 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003808 }
3809 }
3810
Michael Layzell734adb42017-06-07 16:58:31 -04003811 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003812 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003813 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003814 self.path.to_tokens(tokens);
3815 self.brace_token.surround(tokens, |tokens| {
3816 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003817 // NOTE: We need a comma before the dot2 token if it is present.
3818 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003819 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003820 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003821 self.dot2_token.to_tokens(tokens);
3822 });
3823 }
3824 }
3825
Michael Layzell734adb42017-06-07 16:58:31 -04003826 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003827 impl ToTokens for PatTupleStruct {
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.pat.to_tokens(tokens);
3831 }
3832 }
3833
Michael Layzell734adb42017-06-07 16:58:31 -04003834 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003835 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003836 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003837 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
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 PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003843 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003844 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003845 self.front.to_tokens(tokens);
3846 if let Some(ref dot2_token) = self.dot2_token {
3847 if !self.front.empty_or_trailing() {
3848 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003849 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003850 }
David Tolnay41871922017-12-29 01:53:45 -05003851 dot2_token.to_tokens(tokens);
3852 self.comma_token.to_tokens(tokens);
3853 if self.comma_token.is_none() && !self.back.is_empty() {
3854 // Ensure there is a comma after the .. token.
3855 <Token![,]>::default().to_tokens(tokens);
3856 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003857 }
David Tolnay41871922017-12-29 01:53:45 -05003858 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003859 });
3860 }
3861 }
3862
Michael Layzell734adb42017-06-07 16:58:31 -04003863 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003864 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003865 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003866 self.box_token.to_tokens(tokens);
3867 self.pat.to_tokens(tokens);
3868 }
3869 }
3870
Michael Layzell734adb42017-06-07 16:58:31 -04003871 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003872 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003873 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003874 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003875 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003876 self.pat.to_tokens(tokens);
3877 }
3878 }
3879
Michael Layzell734adb42017-06-07 16:58:31 -04003880 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003881 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003882 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003883 self.expr.to_tokens(tokens);
3884 }
3885 }
3886
Michael Layzell734adb42017-06-07 16:58:31 -04003887 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003888 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003889 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003890 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003891 match self.limits {
3892 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003893 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003894 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003895 self.hi.to_tokens(tokens);
3896 }
3897 }
3898
Michael Layzell734adb42017-06-07 16:58:31 -04003899 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003900 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003901 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003902 // XXX: This is a mess, and it will be so easy to screw it up. How
3903 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003904 self.bracket_token.surround(tokens, |tokens| {
3905 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003906
3907 // If we need a comma before the middle or standalone .. token,
3908 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003909 if !self.front.empty_or_trailing()
3910 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003911 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003912 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003913 }
3914
3915 // If we have an identifier, we always need a .. token.
3916 if self.middle.is_some() {
3917 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003918 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003919 } else if self.dot2_token.is_some() {
3920 self.dot2_token.to_tokens(tokens);
3921 }
3922
3923 // Make sure we have a comma before the back half.
3924 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003925 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003926 self.back.to_tokens(tokens);
3927 } else {
3928 self.comma_token.to_tokens(tokens);
3929 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003930 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003931 }
3932 }
3933
Michael Layzell734adb42017-06-07 16:58:31 -04003934 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003935 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003936 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003937 self.mac.to_tokens(tokens);
3938 }
3939 }
3940
3941 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003942 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003943 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003944 self.tts.to_tokens(tokens);
3945 }
3946 }
3947
3948 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003949 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003950 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003951 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003952 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003953 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003954 }
3955 self.pat.to_tokens(tokens);
3956 }
3957 }
3958
Michael Layzell734adb42017-06-07 16:58:31 -04003959 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003960 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003961 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003962 self.brace_token.surround(tokens, |tokens| {
3963 tokens.append_all(&self.stmts);
3964 });
David Tolnay42602292016-10-01 22:25:45 -07003965 }
3966 }
3967
Michael Layzell734adb42017-06-07 16:58:31 -04003968 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003969 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003970 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003971 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003972 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003973 Stmt::Item(ref item) => item.to_tokens(tokens),
3974 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003975 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003976 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003977 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003978 }
David Tolnay42602292016-10-01 22:25:45 -07003979 }
3980 }
3981 }
David Tolnay191e0582016-10-02 18:31:09 -07003982
Michael Layzell734adb42017-06-07 16:58:31 -04003983 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003984 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003985 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003986 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003987 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003988 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003989 if let Some((ref colon_token, ref ty)) = self.ty {
3990 colon_token.to_tokens(tokens);
3991 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003992 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003993 if let Some((ref eq_token, ref init)) = self.init {
3994 eq_token.to_tokens(tokens);
3995 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003996 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003997 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003998 }
3999 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07004000}