blob: 6503ece51c30d9a30eab79fb5d3d096cff168cae [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 {
1011 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1012 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001013 match *expr {
1014 Expr::Unsafe(..)
1015 | Expr::Block(..)
1016 | Expr::If(..)
1017 | Expr::IfLet(..)
1018 | Expr::Match(..)
1019 | Expr::While(..)
1020 | Expr::WhileLet(..)
1021 | Expr::Loop(..)
1022 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001023 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001024 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001025 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001026 }
1027}
1028
David Tolnayb9c8e322016-09-23 20:48:37 -07001029#[cfg(feature = "parsing")]
1030pub mod parsing {
1031 use super::*;
David Tolnaya7d69fc2018-08-26 13:30:24 -04001032 use path::parsing::old_mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001033 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001034 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001035
David Tolnay9389c382018-08-27 09:13:37 -07001036 use parse::{Parse, ParseStream, Result};
Michael Layzell734adb42017-06-07 16:58:31 -04001037 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001038 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001039 #[cfg(feature = "full")]
1040 use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -07001041 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001042
David Tolnay9389c382018-08-27 09:13:37 -07001043 macro_rules! named2 {
1044 ($name:ident ($($arg:ident : $argty:ty),*) -> $ret:ty, $($rest:tt)*) => {
1045 fn $name(input: ParseStream $(, $arg : $argty)*) -> Result<$ret> {
1046 named!(_synom ($($arg : $argty),*) -> $ret, $($rest)*);
1047 input.step_cursor(|cursor| _synom(*cursor $(, $arg)*))
1048 }
1049 };
1050 ($name:ident -> $ret:ty, $($rest:tt)*) => {
1051 fn $name(input: ParseStream) -> Result<$ret> {
1052 named!(_synom -> $ret, $($rest)*);
1053 input.step_cursor(|cursor| _synom(*cursor))
1054 }
1055 };
1056 }
1057
1058 #[cfg(feature = "full")]
1059 macro_rules! ambiguous_expr {
1060 ($i:expr, $allow_struct:ident) => {
1061 shim!($i, ambiguous_expr, $allow_struct, AllowBlock(true))
1062 };
1063 }
1064
David Tolnaybcf26022017-12-25 22:10:52 -05001065 // When we're parsing expressions which occur before blocks, like in an if
1066 // statement's condition, we cannot parse a struct literal.
1067 //
1068 // Struct literals are ambiguous in certain positions
1069 // https://github.com/rust-lang/rfcs/pull/92
David Tolnay9389c382018-08-27 09:13:37 -07001070 #[derive(Copy, Clone)]
1071 pub struct AllowStruct(bool);
1072
1073 #[derive(Copy, Clone)]
1074 pub struct AllowBlock(bool);
David Tolnayaf2557e2016-10-24 11:52:21 -07001075
David Tolnaybcf26022017-12-25 22:10:52 -05001076 // When we are parsing an optional suffix expression, we cannot allow blocks
1077 // if structs are not allowed.
1078 //
1079 // Example:
1080 //
1081 // if break {} {}
1082 //
1083 // is ambiguous between:
1084 //
1085 // if (break {}) {}
1086 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001087 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001088 macro_rules! opt_ambiguous_expr {
1089 ($i:expr, $allow_struct:ident) => {
David Tolnay9389c382018-08-27 09:13:37 -07001090 option!($i, shim!(ambiguous_expr, $allow_struct, AllowBlock($allow_struct.0)))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001091 };
1092 }
1093
David Tolnay9389c382018-08-27 09:13:37 -07001094 impl Parse for Expr {
1095 fn parse(input: ParseStream) -> Result<Self> {
1096 ambiguous_expr(input, AllowStruct(true), AllowBlock(true))
Alex Crichton954046c2017-05-30 21:49:42 -07001097 }
1098 }
1099
Michael Layzell734adb42017-06-07 16:58:31 -04001100 #[cfg(feature = "full")]
David Tolnay9fb0aed2018-08-27 10:23:12 -07001101 fn expr_no_struct(input: ParseStream) -> Result<Expr> {
1102 ambiguous_expr(input, AllowStruct(false), AllowBlock(true))
1103 }
David Tolnayaf2557e2016-10-24 11:52:21 -07001104
David Tolnaybcf26022017-12-25 22:10:52 -05001105 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001106 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001107 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1108 assign_expr(input, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001109 }
1110
Michael Layzell734adb42017-06-07 16:58:31 -04001111 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001112 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001113 // NOTE: We intentionally skip assign_expr, placement_expr, and
David Tolnay9389c382018-08-27 09:13:37 -07001114 // range_expr as they are only parsed in full mode.
1115 or_expr(input, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001116 }
1117
David Tolnaybcf26022017-12-25 22:10:52 -05001118 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001119 macro_rules! binop {
David Tolnay378175c2018-08-27 10:01:12 -07001120 ($name:ident, $next:ident, |$var:ident| $parse_op:expr) => {
David Tolnay9389c382018-08-27 09:13:37 -07001121 fn $name(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
David Tolnay378175c2018-08-27 10:01:12 -07001122 let $var = input;
1123 let mut e: Expr = $next(input, allow_struct, allow_block)?;
1124 while let Some(op) = $parse_op {
1125 e = Expr::Binary(ExprBinary {
1126 attrs: Vec::new(),
1127 left: Box::new(e),
1128 op: op,
1129 right: Box::new($next(input, allow_struct, AllowBlock(true))?),
1130 });
David Tolnay9389c382018-08-27 09:13:37 -07001131 }
David Tolnay378175c2018-08-27 10:01:12 -07001132 Ok(e)
David Tolnay9389c382018-08-27 09:13:37 -07001133 }
Alex Crichton954046c2017-05-30 21:49:42 -07001134 }
David Tolnay54e854d2016-10-24 12:03:30 -07001135 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001136
David Tolnaybcf26022017-12-25 22:10:52 -05001137 // <placement> = <placement> ..
1138 // <placement> += <placement> ..
1139 // <placement> -= <placement> ..
1140 // <placement> *= <placement> ..
1141 // <placement> /= <placement> ..
1142 // <placement> %= <placement> ..
1143 // <placement> ^= <placement> ..
1144 // <placement> &= <placement> ..
1145 // <placement> |= <placement> ..
1146 // <placement> <<= <placement> ..
1147 // <placement> >>= <placement> ..
1148 //
1149 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001150 #[cfg(feature = "full")]
David Tolnay1d2391c2018-08-27 10:29:28 -07001151 fn assign_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1152 let mut e = placement_expr(input, allow_struct, allow_block)?;
1153 if input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1154 e = Expr::Assign(ExprAssign {
1155 attrs: Vec::new(),
1156 left: Box::new(e),
1157 eq_token: input.parse()?,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001158 // Recurse into self to parse right-associative operator.
David Tolnay1d2391c2018-08-27 10:29:28 -07001159 right: Box::new(assign_expr(input, allow_struct, AllowBlock(true))?),
1160 });
1161 } else if input.peek(Token![+=])
1162 || input.peek(Token![-=])
1163 || input.peek(Token![*=])
1164 || input.peek(Token![/=])
1165 || input.peek(Token![%=])
1166 || input.peek(Token![^=])
1167 || input.peek(Token![&=])
1168 || input.peek(Token![|=])
1169 || input.peek(Token![<<=])
1170 || input.peek(Token![>>=])
1171 {
1172 e = Expr::AssignOp(ExprAssignOp {
1173 attrs: Vec::new(),
1174 left: Box::new(e),
1175 op: BinOp::parse_assign_op(input)?,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001176 // Recurse into self to parse right-associative operator.
David Tolnay1d2391c2018-08-27 10:29:28 -07001177 right: Box::new(assign_expr(input, allow_struct, AllowBlock(true))?),
1178 });
1179 }
1180 Ok(e)
1181 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001182
David Tolnaybcf26022017-12-25 22:10:52 -05001183 // <range> <- <range> ..
1184 //
1185 // NOTE: The `in place { expr }` version of this syntax is parsed in
1186 // `atom_expr`, not here.
1187 //
1188 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001189 #[cfg(feature = "full")]
David Tolnay1da8c212018-08-27 10:35:50 -07001190 fn placement_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1191 let mut e = range_expr(input, allow_struct, allow_block)?;
1192 if input.peek(Token![<-]) {
1193 e = Expr::InPlace(ExprInPlace {
1194 attrs: Vec::new(),
1195 place: Box::new(e),
1196 arrow_token: input.parse()?,
1197 value: Box::new(placement_expr(input, allow_struct, AllowBlock(true))?),
1198 });
1199 }
1200 Ok(e)
1201 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001202
David Tolnaybcf26022017-12-25 22:10:52 -05001203 // <or> ... <or> ..
1204 // <or> .. <or> ..
1205 // <or> ..
1206 //
1207 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1208 // rules are for parsing these expressions are, but this is not correct.
1209 // For example, `a .. b .. c` is not a legal expression. It should not
1210 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1211 //
1212 // NOTE: The form of ranges which don't include a preceding expression are
1213 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001214 #[cfg(feature = "full")]
David Tolnayc4c631e2018-08-27 10:44:37 -07001215 fn range_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1216 let mut e = or_expr(input, allow_struct, allow_block)?;
1217 while input.peek(Token![..]) {
1218 e = Expr::Range(ExprRange {
1219 attrs: Vec::new(),
1220 from: Some(Box::new(e)),
1221 limits: input.parse()?,
1222 to: {
1223 if input.is_empty()
1224 || input.peek(Token![,])
1225 || input.peek(Token![;])
1226 || !allow_struct.0 && input.peek(token::Brace)
1227 {
1228 None
1229 } else {
1230 // We don't want to allow blocks here if we don't allow
1231 // structs. See the reasoning for `opt_ambiguous_expr!`
1232 // above.
1233 Some(Box::new(or_expr(input, allow_struct, AllowBlock(allow_struct.0))?))
1234 }
1235 },
1236 });
1237 }
1238 Ok(e)
1239 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001240
David Tolnaybcf26022017-12-25 22:10:52 -05001241 // <and> || <and> ...
David Tolnay378175c2018-08-27 10:01:12 -07001242 binop!(or_expr, and_expr, |input| {
1243 if input.peek(Token![||]) {
1244 Some(BinOp::Or(input.parse()?))
1245 } else {
1246 None
1247 }
1248 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001249
David Tolnaybcf26022017-12-25 22:10:52 -05001250 // <compare> && <compare> ...
David Tolnay378175c2018-08-27 10:01:12 -07001251 binop!(and_expr, compare_expr, |input| {
1252 if input.peek(Token![&&]) {
1253 Some(BinOp::And(input.parse()?))
1254 } else {
1255 None
1256 }
1257 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001258
David Tolnaybcf26022017-12-25 22:10:52 -05001259 // <bitor> == <bitor> ...
1260 // <bitor> != <bitor> ...
1261 // <bitor> >= <bitor> ...
1262 // <bitor> <= <bitor> ...
1263 // <bitor> > <bitor> ...
1264 // <bitor> < <bitor> ...
1265 //
1266 // NOTE: This operator appears to be parsed as left-associative, but errors
1267 // if it is used in a non-associative manner.
David Tolnay378175c2018-08-27 10:01:12 -07001268 binop!(compare_expr, bitor_expr, |input| {
1269 if input.peek(Token![==]) {
1270 Some(BinOp::Eq(input.parse()?))
1271 } else if input.peek(Token![!=]) {
1272 Some(BinOp::Ne(input.parse()?))
1273 // must be before `<`
1274 } else if input.peek(Token![<=]) {
1275 Some(BinOp::Le(input.parse()?))
1276 // must be before `>`
1277 } else if input.peek(Token![>=]) {
1278 Some(BinOp::Ge(input.parse()?))
1279 } else if input.peek(Token![<]) && !input.peek(Token![<<]) && !input.peek(Token![<-]) {
1280 Some(BinOp::Lt(input.parse()?))
1281 } else if input.peek(Token![>]) && !input.peek(Token![>>]) {
1282 Some(BinOp::Gt(input.parse()?))
1283 } else {
1284 None
1285 }
1286 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001287
David Tolnaybcf26022017-12-25 22:10:52 -05001288 // <bitxor> | <bitxor> ...
David Tolnay378175c2018-08-27 10:01:12 -07001289 binop!(bitor_expr, bitxor_expr, |input| {
1290 if input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
1291 Some(BinOp::BitOr(input.parse()?))
1292 } else {
1293 None
1294 }
1295 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001296
David Tolnaybcf26022017-12-25 22:10:52 -05001297 // <bitand> ^ <bitand> ...
David Tolnay378175c2018-08-27 10:01:12 -07001298 binop!(bitxor_expr, bitand_expr, |input| {
1299 if input.peek(Token![^]) && !input.peek(Token![^=]) {
1300 Some(BinOp::BitXor(input.parse()?))
1301 } else {
1302 None
1303 }
1304 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001305
David Tolnaybcf26022017-12-25 22:10:52 -05001306 // <shift> & <shift> ...
David Tolnay378175c2018-08-27 10:01:12 -07001307 binop!(bitand_expr, shift_expr, |input| {
1308 if input.peek(Token![&]) && !input.peek(Token![&&]) && !input.peek(Token![&=]) {
1309 Some(BinOp::BitAnd(input.parse()?))
1310 } else {
1311 None
1312 }
1313 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001314
David Tolnaybcf26022017-12-25 22:10:52 -05001315 // <arith> << <arith> ...
1316 // <arith> >> <arith> ...
David Tolnay378175c2018-08-27 10:01:12 -07001317 binop!(shift_expr, arith_expr, |input| {
1318 if input.peek(Token![<<]) && !input.peek(Token![<<=]) {
1319 Some(BinOp::Shl(input.parse()?))
1320 } else if input.peek(Token![>>]) && !input.peek(Token![>>=]) {
1321 Some(BinOp::Shr(input.parse()?))
1322 } else {
1323 None
1324 }
1325 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326
David Tolnaybcf26022017-12-25 22:10:52 -05001327 // <term> + <term> ...
1328 // <term> - <term> ...
David Tolnay378175c2018-08-27 10:01:12 -07001329 binop!(arith_expr, term_expr, |input| {
1330 if input.peek(Token![+]) && !input.peek(Token![+=]) {
1331 Some(BinOp::Add(input.parse()?))
1332 } else if input.peek(Token![-]) && !input.peek(Token![-=]) {
1333 Some(BinOp::Sub(input.parse()?))
1334 } else {
1335 None
1336 }
1337 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338
David Tolnaybcf26022017-12-25 22:10:52 -05001339 // <cast> * <cast> ...
1340 // <cast> / <cast> ...
1341 // <cast> % <cast> ...
David Tolnay378175c2018-08-27 10:01:12 -07001342 binop!(term_expr, cast_expr, |input| {
1343 if input.peek(Token![*]) && !input.peek(Token![*=]) {
1344 Some(BinOp::Mul(input.parse()?))
1345 } else if input.peek(Token![/]) && !input.peek(Token![/=]) {
1346 Some(BinOp::Div(input.parse()?))
1347 } else if input.peek(Token![%]) && !input.peek(Token![%=]) {
1348 Some(BinOp::Rem(input.parse()?))
1349 } else {
1350 None
1351 }
1352 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001353
David Tolnaybcf26022017-12-25 22:10:52 -05001354 // <unary> as <ty>
1355 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001356 #[cfg(feature = "full")]
David Tolnayaf5a8112018-08-27 10:52:20 -07001357 fn cast_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1358 let mut e = unary_expr(input, allow_struct, allow_block)?;
1359 loop {
1360 if input.peek(Token![as]) {
1361 e = Expr::Cast(ExprCast {
1362 attrs: Vec::new(),
1363 expr: Box::new(e),
1364 as_token: input.parse()?,
1365 // We can't accept `A + B` in cast expressions, as it's
1366 // ambiguous with the + expression.
1367 ty: Box::new(input.call(Type::without_plus)?),
1368 });
1369 } else if input.peek(Token![:]) {
1370 e = Expr::Type(ExprType {
1371 attrs: Vec::new(),
1372 expr: Box::new(e),
1373 colon_token: input.parse()?,
1374 // We can't accept `A + B` in cast expressions, as it's
1375 // ambiguous with the + expression.
1376 ty: Box::new(input.call(Type::without_plus)?),
1377 });
1378 } else {
1379 break;
1380 }
1381 }
1382 Ok(e)
1383 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001384
David Tolnay0cf94f22017-12-28 23:46:26 -05001385 // <unary> as <ty>
1386 #[cfg(not(feature = "full"))]
David Tolnayaf5a8112018-08-27 10:52:20 -07001387 fn cast_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1388 let mut e = unary_expr(input, allow_struct, allow_block)?;
1389 while input.peek(Token![as]) {
1390 e = Expr::Cast(ExprCast {
1391 attrs: Vec::new(),
1392 expr: Box::new(e),
1393 as_token: input.parse()?,
1394 // We can't accept `A + B` in cast expressions, as it's
1395 // ambiguous with the + expression.
1396 ty: Box::new(input.call(Type::without_plus)?),
1397 });
1398 }
1399 Ok(e)
1400 }
David Tolnay0cf94f22017-12-28 23:46:26 -05001401
David Tolnaybcf26022017-12-25 22:10:52 -05001402 // <UnOp> <trailer>
1403 // & <trailer>
1404 // &mut <trailer>
1405 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001406 #[cfg(feature = "full")]
David Tolnay377263f2018-08-27 13:48:30 -07001407 fn unary_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1408 let ahead = input.fork();
1409 ahead.call(Attribute::parse_outer)?;
1410 if ahead.peek(Token![&])
1411 || ahead.peek(Token![box])
1412 || ahead.peek(Token![*])
1413 || ahead.peek(Token![!])
1414 || ahead.peek(Token![-])
1415 {
1416 let attrs = input.call(Attribute::parse_outer)?;
1417 if input.peek(Token![&]) {
1418 Ok(Expr::Reference(ExprReference {
1419 attrs: attrs,
1420 and_token: input.parse()?,
1421 mutability: input.parse()?,
1422 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1423 }))
1424 } else if input.peek(Token![box]) {
1425 Ok(Expr::Box(ExprBox {
1426 attrs: attrs,
1427 box_token: input.parse()?,
1428 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1429 }))
1430 } else {
1431 Ok(Expr::Unary(ExprUnary {
1432 attrs: attrs,
1433 op: input.parse()?,
1434 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1435 }))
1436 }
1437 } else {
1438 trailer_expr(input, allow_struct, allow_block)
1439 }
1440 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001441
Michael Layzell734adb42017-06-07 16:58:31 -04001442 // XXX: This duplication is ugly
1443 #[cfg(not(feature = "full"))]
David Tolnay377263f2018-08-27 13:48:30 -07001444 fn unary_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1445 let ahead = input.fork();
1446 ahead.call(Attribute::parse_outer)?;
1447 if ahead.peek(Token![*]) || ahead.peek(Token![!]) || ahead.peek(Token![-]) {
1448 Ok(Expr::Unary(ExprUnary {
1449 attrs: input.call(Attribute::parse_outer)?,
1450 op: input.parse()?,
1451 expr: Box::new(unary_expr(input, allow_struct, AllowBlock(true))?),
1452 }))
1453 } else {
1454 trailer_expr(input, allow_struct, allow_block)
1455 }
1456 }
Michael Layzell734adb42017-06-07 16:58:31 -04001457
David Tolnayd997aef2018-07-21 18:42:31 -07001458 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001459 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1460 let mut outer = Vec::new();
1461 let mut inner = Vec::new();
1462 for attr in mem::replace(attrs, Vec::new()) {
1463 match attr.style {
1464 AttrStyle::Outer => outer.push(attr),
1465 AttrStyle::Inner(_) => inner.push(attr),
1466 }
1467 }
1468 *attrs = inner;
1469 outer
1470 }
1471
David Tolnaybcf26022017-12-25 22:10:52 -05001472 // <atom> (..<args>) ...
1473 // <atom> . <ident> (..<args>) ...
1474 // <atom> . <ident> ...
1475 // <atom> . <lit> ...
1476 // <atom> [ <expr> ] ...
1477 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001478 #[cfg(feature = "full")]
David Tolnay1501f7e2018-08-27 14:21:03 -07001479 fn trailer_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1480 let mut e = atom_expr(input, allow_struct, allow_block)?;
1481
1482 let mut attrs = e.replace_attrs(Vec::new());
1483 let outer_attrs = take_outer(&mut attrs);
1484 e.replace_attrs(attrs);
1485
1486 loop {
1487 if input.peek(token::Paren) {
1488 let content;
1489 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001490 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001491 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001492 paren_token: parenthesized!(content in input),
1493 args: content.parse_terminated(<Expr as Parse>::parse)?,
1494 });
1495 } else if input.peek(Token![.]) && !input.peek(Token![..]) {
1496 let dot_token: Token![.] = input.parse()?;
1497 let member: Member = input.parse()?;
1498 let turbofish = if member.is_named() && input.peek(Token![::]) {
1499 Some(MethodTurbofish {
1500 colon2_token: input.parse()?,
1501 lt_token: input.parse()?,
1502 args: {
1503 let mut args = Punctuated::new();
1504 loop {
1505 if input.peek(Token![>]) {
1506 break;
1507 }
1508 let value = input.parse()?;
1509 args.push_value(value);
1510 if input.peek(Token![>]) {
1511 break;
1512 }
1513 let punct = input.parse()?;
1514 args.push_punct(punct);
1515 }
1516 args
1517 },
1518 gt_token: input.parse()?,
1519 })
1520 } else {
1521 None
1522 };
1523
1524 if turbofish.is_some() || input.peek(token::Paren) {
1525 if let Member::Named(method) = member {
1526 let content;
1527 e = Expr::MethodCall(ExprMethodCall {
1528 attrs: Vec::new(),
1529 receiver: Box::new(e),
1530 dot_token: dot_token,
1531 method: method,
1532 turbofish: turbofish,
1533 paren_token: parenthesized!(content in input),
1534 args: content.parse_terminated(<Expr as Parse>::parse)?,
1535 });
1536 continue;
1537 }
1538 }
1539
1540 e = Expr::Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001541 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001542 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001543 dot_token: dot_token,
David Tolnay85b69a42017-12-27 20:43:10 -05001544 member: member,
David Tolnay1501f7e2018-08-27 14:21:03 -07001545 });
1546 } else if input.peek(token::Bracket) {
1547 let content;
1548 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001549 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001550 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001551 bracket_token: bracketed!(content in input),
1552 index: content.parse()?,
1553 });
1554 } else if input.peek(Token![?]) {
1555 e = Expr::Try(ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001556 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001557 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001558 question_token: input.parse()?,
1559 });
1560 } else {
1561 break;
1562 }
1563 }
1564
1565 let mut attrs = outer_attrs;
1566 attrs.extend(e.replace_attrs(Vec::new()));
1567 e.replace_attrs(attrs);
1568 Ok(e)
1569 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001570
Michael Layzell734adb42017-06-07 16:58:31 -04001571 // XXX: Duplication == ugly
1572 #[cfg(not(feature = "full"))]
David Tolnay1501f7e2018-08-27 14:21:03 -07001573 fn trailer_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1574 let mut e = atom_expr(input, allow_struct, allow_block)?;
1575
1576 loop {
1577 if input.peek(token::Paren) {
1578 let content;
1579 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001580 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001581 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001582 paren_token: parenthesized!(content in input),
1583 args: content.parse_terminated(<Expr as Parse>::parse)?,
1584 });
1585 } else if input.peek(Token![.]) {
1586 e = Expr::Field(ExprField {
David Tolnayd5147742018-06-30 10:09:52 -07001587 attrs: Vec::new(),
1588 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001589 dot_token: input.parse()?,
1590 member: input.parse()?,
1591 });
1592 } else if input.peek(token::Bracket) {
1593 let content;
1594 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001595 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001596 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001597 bracket_token: bracketed!(content in input),
1598 index: content.parse()?,
1599 });
1600 } else {
1601 break;
1602 }
1603 }
1604
1605 Ok(e)
1606 }
Michael Layzell734adb42017-06-07 16:58:31 -04001607
David Tolnaya454c8f2018-01-07 01:01:10 -08001608 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001609 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001610 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001611 named2!(atom_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001612 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001613 |
David Tolnay8c91b882017-12-28 23:04:32 -05001614 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001615 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001616 // must be before ExprStruct
David Tolnay02a9c6f2018-08-24 18:58:45 -04001617 syn!(ExprAsync) => { Expr::Async }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001618 |
David Tolnayf7177052018-08-24 15:31:50 -04001619 // must be before ExprStruct
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001620 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnayf7177052018-08-24 15:31:50 -04001621 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001622 // must be before expr_path
David Tolnay9389c382018-08-27 09:13:37 -07001623 cond_reduce!(allow_struct.0, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001624 |
David Tolnay8c91b882017-12-28 23:04:32 -05001625 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001626 |
David Tolnay8c91b882017-12-28 23:04:32 -05001627 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001628 |
David Tolnay9389c382018-08-27 09:13:37 -07001629 shim!(expr_break, allow_struct) // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001630 |
David Tolnay8c91b882017-12-28 23:04:32 -05001631 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001632 |
David Tolnay9389c382018-08-27 09:13:37 -07001633 shim!(expr_ret, allow_struct) // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001634 |
David Tolnay8c91b882017-12-28 23:04:32 -05001635 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001636 |
David Tolnay8c91b882017-12-28 23:04:32 -05001637 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001638 |
David Tolnay8c91b882017-12-28 23:04:32 -05001639 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001640 |
David Tolnay8c91b882017-12-28 23:04:32 -05001641 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001642 |
David Tolnay8c91b882017-12-28 23:04:32 -05001643 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001644 |
David Tolnay8c91b882017-12-28 23:04:32 -05001645 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001646 |
David Tolnay8c91b882017-12-28 23:04:32 -05001647 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001648 |
David Tolnay8c91b882017-12-28 23:04:32 -05001649 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001650 |
David Tolnay8c91b882017-12-28 23:04:32 -05001651 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001652 |
David Tolnay8c91b882017-12-28 23:04:32 -05001653 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001654 |
David Tolnay8c91b882017-12-28 23:04:32 -05001655 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001656 |
David Tolnay9389c382018-08-27 09:13:37 -07001657 shim!(expr_closure, allow_struct)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001658 |
David Tolnay9389c382018-08-27 09:13:37 -07001659 cond_reduce!(allow_block.0, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001660 |
1661 // NOTE: This is the prefix-form of range
David Tolnay9389c382018-08-27 09:13:37 -07001662 shim!(expr_range, allow_struct)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001663 |
David Tolnay8c91b882017-12-28 23:04:32 -05001664 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001665 |
David Tolnay8c91b882017-12-28 23:04:32 -05001666 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001667 ));
1668
Michael Layzell734adb42017-06-07 16:58:31 -04001669 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001670 named2!(atom_expr(_allow_struct: AllowStruct, _allow_block: AllowBlock) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001671 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001672 |
David Tolnay9374bc02018-01-27 18:49:36 -08001673 syn!(ExprParen) => { Expr::Paren }
1674 |
David Tolnay8c91b882017-12-28 23:04:32 -05001675 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001676 ));
1677
Michael Layzell734adb42017-06-07 16:58:31 -04001678 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001679 named2!(expr_nosemi -> Expr, do_parse!(
David Tolnay313a36f2018-04-29 20:13:04 -07001680 nosemi: alt!(
1681 syn!(ExprIf) => { Expr::If }
1682 |
1683 syn!(ExprIfLet) => { Expr::IfLet }
1684 |
1685 syn!(ExprWhile) => { Expr::While }
1686 |
1687 syn!(ExprWhileLet) => { Expr::WhileLet }
1688 |
1689 syn!(ExprForLoop) => { Expr::ForLoop }
1690 |
1691 syn!(ExprLoop) => { Expr::Loop }
1692 |
1693 syn!(ExprMatch) => { Expr::Match }
1694 |
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001695 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnay313a36f2018-04-29 20:13:04 -07001696 |
1697 syn!(ExprYield) => { Expr::Yield }
1698 |
1699 syn!(ExprUnsafe) => { Expr::Unsafe }
1700 |
1701 syn!(ExprBlock) => { Expr::Block }
1702 ) >>
1703 // If the next token is a `.` or a `?` it is special-cased to parse
1704 // as an expression instead of a blockexpression.
1705 not!(punct!(.)) >>
1706 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001707 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001708 ));
Michael Layzell35418782017-06-07 09:20:25 -04001709
David Tolnay8c91b882017-12-28 23:04:32 -05001710 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001711 #[cfg(not(feature = "full"))]
1712 named!(parse -> Self, do_parse!(
1713 lit: syn!(Lit) >>
1714 (ExprLit {
1715 attrs: Vec::new(),
1716 lit: lit,
1717 })
1718 ));
1719
1720 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001721 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001722 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001723 lit: syn!(Lit) >>
1724 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001725 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001726 lit: lit,
1727 })
1728 ));
1729 }
1730
1731 #[cfg(feature = "full")]
1732 impl Synom for ExprMacro {
1733 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001734 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001735 mac: syn!(Macro) >>
1736 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001737 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001738 mac: mac,
1739 })
1740 ));
1741 }
1742
David Tolnaye98775f2017-12-28 23:17:00 -05001743 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001744 impl Synom for ExprGroup {
1745 named!(parse -> Self, do_parse!(
David Tolnaya7d69fc2018-08-26 13:30:24 -04001746 e: old_grouped!(syn!(Expr)) >>
Michael Layzell93c36282017-06-04 20:43:14 -04001747 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001748 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001749 expr: Box::new(e.1),
1750 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001751 })
Michael Layzell93c36282017-06-04 20:43:14 -04001752 ));
1753 }
1754
Alex Crichton954046c2017-05-30 21:49:42 -07001755 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001756 #[cfg(not(feature = "full"))]
1757 named!(parse -> Self, do_parse!(
1758 e: parens!(syn!(Expr)) >>
1759 (ExprParen {
1760 attrs: Vec::new(),
1761 paren_token: e.0,
1762 expr: Box::new(e.1),
1763 })
1764 ));
1765
1766 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001767 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001768 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001769 e: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001770 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001771 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001772 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001773 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001774 attrs: {
1775 let mut attrs = outer_attrs;
1776 attrs.extend((e.1).0);
1777 attrs
1778 },
David Tolnay8875fca2017-12-31 13:52:37 -05001779 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001780 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001781 })
Michael Layzell92639a52017-06-01 00:07:44 -04001782 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001783 }
David Tolnay89e05672016-10-02 14:39:42 -07001784
Michael Layzell734adb42017-06-07 16:58:31 -04001785 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001786 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001787 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001788 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001789 elems: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001790 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001791 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001792 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001793 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001794 attrs: {
1795 let mut attrs = outer_attrs;
1796 attrs.extend((elems.1).0);
1797 attrs
1798 },
David Tolnay8875fca2017-12-31 13:52:37 -05001799 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001800 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001801 })
1802 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001803 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001804
Michael Layzell734adb42017-06-07 16:58:31 -04001805 #[cfg(feature = "full")]
David Tolnay1501f7e2018-08-27 14:21:03 -07001806 impl Parse for GenericMethodArgument {
David Tolnayd60cfec2017-12-29 00:21:38 -05001807 // TODO parse const generics as well
David Tolnay1501f7e2018-08-27 14:21:03 -07001808 fn parse(input: ParseStream) -> Result<Self> {
1809 input.parse_synom(ty_no_eq_after).map(GenericMethodArgument::Type)
1810 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001811 }
1812
1813 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001814 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001815 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001816 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001817 elems: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001818 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001819 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001820 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001821 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001822 attrs: {
1823 let mut attrs = outer_attrs;
1824 attrs.extend((elems.1).0);
1825 attrs
1826 },
1827 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001828 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001829 })
1830 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001831 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001832
Michael Layzell734adb42017-06-07 16:58:31 -04001833 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001834 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001835 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001836 if_: keyword!(if) >>
1837 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001838 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001839 eq: punct!(=) >>
David Tolnay9389c382018-08-27 09:13:37 -07001840 cond: shim!(expr_no_struct) >>
1841 then_block: braces!(Block::old_parse_within) >>
1842 else_block: option!(shim!(else_block)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001843 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001844 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001845 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001846 let_token: let_,
1847 eq_token: eq,
1848 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001849 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001850 brace_token: then_block.0,
1851 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001852 },
1853 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001854 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001855 })
1856 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001857 }
1858
Michael Layzell734adb42017-06-07 16:58:31 -04001859 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001860 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001861 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001862 if_: keyword!(if) >>
David Tolnay9389c382018-08-27 09:13:37 -07001863 cond: shim!(expr_no_struct) >>
1864 then_block: braces!(Block::old_parse_within) >>
1865 else_block: option!(shim!(else_block)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001866 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001867 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001868 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001869 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001870 brace_token: then_block.0,
1871 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001872 },
1873 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001874 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001875 })
1876 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001877 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001878
Michael Layzell734adb42017-06-07 16:58:31 -04001879 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001880 named2!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001881 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001882 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001883 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001884 |
David Tolnay8c91b882017-12-28 23:04:32 -05001885 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001886 |
1887 do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07001888 else_block: braces!(Block::old_parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001889 (Expr::Block(ExprBlock {
1890 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04001891 label: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001892 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001893 brace_token: else_block.0,
1894 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001895 },
1896 }))
David Tolnay939766a2016-09-23 23:48:12 -07001897 )
Alex Crichton954046c2017-05-30 21:49:42 -07001898 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001899 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001900 ));
1901
Michael Layzell734adb42017-06-07 16:58:31 -04001902 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001903 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001904 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001905 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001906 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001907 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001908 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001909 in_: keyword!(in) >>
David Tolnay9389c382018-08-27 09:13:37 -07001910 expr: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001911 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001912 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07001913 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001914 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001915 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001916 attrs: {
1917 let mut attrs = outer_attrs;
1918 attrs.extend((block.1).0);
1919 attrs
1920 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001921 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001922 for_token: for_,
1923 pat: Box::new(pat),
1924 in_token: in_,
1925 expr: Box::new(expr),
1926 body: Block {
1927 brace_token: block.0,
1928 stmts: (block.1).1,
1929 },
Michael Layzell92639a52017-06-01 00:07:44 -04001930 })
1931 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001932 }
Gregory Katze5f35682016-09-27 14:20:55 -04001933
Michael Layzell734adb42017-06-07 16:58:31 -04001934 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001935 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001936 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001937 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001938 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001939 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001940 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001941 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07001942 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001943 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001944 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001945 attrs: {
1946 let mut attrs = outer_attrs;
1947 attrs.extend((block.1).0);
1948 attrs
1949 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001950 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001951 loop_token: loop_,
1952 body: Block {
1953 brace_token: block.0,
1954 stmts: (block.1).1,
1955 },
Michael Layzell92639a52017-06-01 00:07:44 -04001956 })
1957 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001958 }
1959
Michael Layzell734adb42017-06-07 16:58:31 -04001960 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001961 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001962 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001963 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001964 match_: keyword!(match) >>
David Tolnay9389c382018-08-27 09:13:37 -07001965 obj: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001966 braced_content: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001967 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001968 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07001969 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001970 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07001971 attrs: {
1972 let mut attrs = outer_attrs;
1973 attrs.extend((braced_content.1).0);
1974 attrs
1975 },
David Tolnay8875fca2017-12-31 13:52:37 -05001976 expr: Box::new(obj),
1977 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07001978 brace_token: braced_content.0,
1979 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001980 })
1981 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001982 }
David Tolnay1978c672016-10-27 22:05:52 -07001983
Michael Layzell734adb42017-06-07 16:58:31 -04001984 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001985 impl Synom for ExprTryBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001986 named!(parse -> Self, do_parse!(
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001987 try_token: keyword!(try) >>
1988 block: syn!(Block) >>
1989 (ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001990 attrs: Vec::new(),
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001991 try_token: try_token,
1992 block: block,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001993 })
Michael Layzell92639a52017-06-01 00:07:44 -04001994 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001995 }
Arnavion02ef13f2017-04-25 00:54:31 -07001996
Michael Layzell734adb42017-06-07 16:58:31 -04001997 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001998 impl Synom for ExprYield {
1999 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002000 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002001 expr: option!(syn!(Expr)) >>
2002 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002003 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002004 yield_token: yield_,
2005 expr: expr.map(Box::new),
2006 })
2007 ));
2008 }
2009
2010 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002011 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002012 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002013 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002014 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002015 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002016 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002017 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002018 body: do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002019 expr: alt!(shim!(expr_nosemi) | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002020 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2021 true => alt!(
2022 input_end!() => { |_| None }
2023 |
2024 punct!(,) => { Some }
2025 )
Alex Crichton03b30272017-08-28 09:35:24 -07002026 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002027 false => option!(punct!(,))
2028 ) >>
2029 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002030 ) >>
2031 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002032 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002033 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002034 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002035 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002036 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002037 body: Box::new(body.0),
2038 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002039 })
2040 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002041 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002042
Michael Layzell734adb42017-06-07 16:58:31 -04002043 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002044 named2!(expr_closure(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002045 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002046 asyncness: option!(keyword!(async)) >>
2047 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002048 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002049 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002050 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002051 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002052 ret_and_body: alt!(
2053 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002054 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002055 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002056 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002057 (
2058 ReturnType::Type(arrow, Box::new(ty)),
2059 Expr::Block(ExprBlock {
2060 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04002061 label: None,
David Tolnay76178be2018-07-31 23:06:15 -07002062 block: body,
2063 },
2064 ))
David Tolnay89e05672016-10-02 14:39:42 -07002065 )
2066 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002067 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002068 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002069 (Expr::Closure(ExprClosure {
2070 attrs: attrs,
2071 asyncness: asyncness,
2072 movability: movability,
2073 capture: capture,
2074 or1_token: or1,
2075 inputs: inputs,
2076 or2_token: or2,
2077 output: ret_and_body.0,
2078 body: Box::new(ret_and_body.1),
2079 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002080 ));
2081
2082 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04002083 impl Synom for ExprAsync {
2084 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002085 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay02a9c6f2018-08-24 18:58:45 -04002086 async_token: keyword!(async) >>
2087 capture: option!(keyword!(move)) >>
2088 block: syn!(Block) >>
2089 (ExprAsync {
2090 attrs: attrs,
2091 async_token: async_token,
2092 capture: capture,
2093 block: block,
2094 })
2095 ));
2096 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002097
2098 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002099 named!(fn_arg -> FnArg, do_parse!(
2100 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002101 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002102 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002103 if let Some((colon, ty)) = ty {
2104 FnArg::Captured(ArgCaptured {
2105 pat: pat,
2106 colon_token: colon,
2107 ty: ty,
2108 })
2109 } else {
2110 FnArg::Inferred(pat)
2111 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002112 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002113 ));
2114
Michael Layzell734adb42017-06-07 16:58:31 -04002115 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002116 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002117 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002118 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002119 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002120 while_: keyword!(while) >>
David Tolnay9389c382018-08-27 09:13:37 -07002121 cond: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002122 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002123 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002124 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002125 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002126 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002127 attrs: {
2128 let mut attrs = outer_attrs;
2129 attrs.extend((block.1).0);
2130 attrs
2131 },
2132 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002133 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002134 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002135 body: Block {
2136 brace_token: block.0,
2137 stmts: (block.1).1,
2138 },
Michael Layzell92639a52017-06-01 00:07:44 -04002139 })
2140 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002141 }
2142
Michael Layzell734adb42017-06-07 16:58:31 -04002143 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002144 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002145 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002146 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002147 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002148 while_: keyword!(while) >>
2149 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002150 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002151 eq: punct!(=) >>
David Tolnay9389c382018-08-27 09:13:37 -07002152 value: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002153 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002154 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002155 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002156 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002157 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002158 attrs: {
2159 let mut attrs = outer_attrs;
2160 attrs.extend((block.1).0);
2161 attrs
2162 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002163 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002164 while_token: while_,
2165 let_token: let_,
2166 pats: pats,
2167 eq_token: eq,
2168 expr: Box::new(value),
2169 body: Block {
2170 brace_token: block.0,
2171 stmts: (block.1).1,
2172 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002173 })
2174 ));
2175 }
2176
2177 #[cfg(feature = "full")]
2178 impl Synom for Label {
2179 named!(parse -> Self, do_parse!(
2180 name: syn!(Lifetime) >>
2181 colon: punct!(:) >>
2182 (Label {
2183 name: name,
2184 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002185 })
2186 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002187 }
2188
Michael Layzell734adb42017-06-07 16:58:31 -04002189 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002190 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002191 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002192 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002193 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002194 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002195 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002196 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002197 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002198 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002199 })
2200 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002201 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002202
Michael Layzell734adb42017-06-07 16:58:31 -04002203 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002204 named2!(expr_break(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002205 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002206 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002207 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002208 // We can't allow blocks after a `break` expression when we wouldn't
2209 // allow structs, as this expression is ambiguous.
2210 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002211 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002212 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002213 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002214 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002215 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002216 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002217 ));
2218
Michael Layzell734adb42017-06-07 16:58:31 -04002219 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002220 named2!(expr_ret(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002221 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002222 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002223 // NOTE: return is greedy and eats blocks after it even when in a
2224 // position where structs are not allowed, such as in if statement
2225 // conditions. For example:
2226 //
David Tolnaybcf26022017-12-25 22:10:52 -05002227 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002228 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002229 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002230 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002231 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002232 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002233 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002234 ));
2235
Michael Layzell734adb42017-06-07 16:58:31 -04002236 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002237 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002238 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002239 outer_attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002240 path: syn!(Path) >>
2241 data: braces!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002242 inner_attrs: many0!(Attribute::old_parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002243 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002244 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2245 dots: punct!(..) >>
2246 base: syn!(Expr) >>
2247 (dots, base)
2248 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002249 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002250 )) >>
2251 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002252 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002253 let (dots, rest) = match base.and_then(|b| b) {
2254 Some((dots, base)) => (Some(dots), Some(base)),
2255 None => (None, None),
2256 };
2257 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002258 attrs: {
2259 let mut attrs = outer_attrs;
2260 attrs.extend(inner_attrs);
2261 attrs
2262 },
Michael Layzell92639a52017-06-01 00:07:44 -04002263 brace_token: brace,
2264 path: path,
2265 fields: fields,
2266 dot2_token: dots,
2267 rest: rest.map(Box::new),
2268 }
2269 })
2270 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002271 }
2272
Michael Layzell734adb42017-06-07 16:58:31 -04002273 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002274 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002275 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002276 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayc42b90a2018-01-18 23:11:37 -08002277 field_value: alt!(
2278 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2279 |
2280 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002281 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002282 None,
2283 Expr::Path(ExprPath {
2284 attrs: Vec::new(),
2285 qself: None,
2286 path: name.into(),
2287 }),
2288 ))
2289 ) >>
2290 (FieldValue {
2291 attrs: attrs,
2292 member: field_value.0,
2293 colon_token: field_value.1,
2294 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002295 })
2296 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002297 }
David Tolnay055a7042016-10-02 19:23:54 -07002298
Michael Layzell734adb42017-06-07 16:58:31 -04002299 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002300 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002301 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002302 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002303 data: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002304 many0!(Attribute::old_parse_inner),
David Tolnay5d314dc2018-07-21 16:40:01 -07002305 syn!(Expr),
2306 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002307 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002308 )) >>
2309 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002310 attrs: {
2311 let mut attrs = outer_attrs;
2312 attrs.extend((data.1).0);
2313 attrs
2314 },
2315 expr: Box::new((data.1).1),
2316 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002317 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002318 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002319 })
2320 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002321 }
David Tolnay055a7042016-10-02 19:23:54 -07002322
Michael Layzell734adb42017-06-07 16:58:31 -04002323 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002324 impl Synom for ExprUnsafe {
2325 named!(parse -> Self, do_parse!(
David Tolnay8b493772018-08-27 06:30:18 -07002326 outer_attrs: many0!(Attribute::old_parse_outer) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002327 unsafe_: keyword!(unsafe) >>
David Tolnayc4be3512018-08-27 06:25:44 -07002328 block: braces!(tuple!(
David Tolnay8b493772018-08-27 06:30:18 -07002329 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002330 shim!(Block::parse_within),
David Tolnayc4be3512018-08-27 06:25:44 -07002331 )) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002332 (ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002333 attrs: {
2334 let mut attrs = outer_attrs;
2335 attrs.extend((block.1).0);
2336 attrs
2337 },
Nika Layzell640832a2017-12-04 13:37:09 -05002338 unsafe_token: unsafe_,
David Tolnayc4be3512018-08-27 06:25:44 -07002339 block: Block {
2340 brace_token: block.0,
2341 stmts: (block.1).1,
2342 },
Nika Layzell640832a2017-12-04 13:37:09 -05002343 })
2344 ));
2345 }
2346
2347 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002348 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002349 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002350 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay1d8e9962018-08-24 19:04:20 -04002351 label: option!(syn!(Label)) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002352 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002353 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002354 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002355 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002356 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002357 attrs: {
2358 let mut attrs = outer_attrs;
2359 attrs.extend((block.1).0);
2360 attrs
2361 },
David Tolnay1d8e9962018-08-24 19:04:20 -04002362 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002363 block: Block {
2364 brace_token: block.0,
2365 stmts: (block.1).1,
2366 },
Michael Layzell92639a52017-06-01 00:07:44 -04002367 })
2368 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002369 }
David Tolnay89e05672016-10-02 14:39:42 -07002370
Michael Layzell734adb42017-06-07 16:58:31 -04002371 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002372 named2!(expr_range(allow_struct: AllowStruct) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002373 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002374 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002375 (ExprRange {
2376 attrs: Vec::new(),
2377 from: None,
2378 to: hi.map(Box::new),
2379 limits: limits,
2380 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002381 ));
2382
Michael Layzell734adb42017-06-07 16:58:31 -04002383 #[cfg(feature = "full")]
David Tolnayc4c631e2018-08-27 10:44:37 -07002384 impl Parse for RangeLimits {
2385 fn parse(input: ParseStream) -> Result<Self> {
2386 let lookahead = input.lookahead1();
2387 if lookahead.peek(Token![..=]) {
2388 input.parse().map(RangeLimits::Closed)
2389 } else if lookahead.peek(Token![...]) {
2390 let dot3: Token![...] = input.parse()?;
2391 Ok(RangeLimits::Closed(Token![..=](dot3.spans)))
2392 } else if lookahead.peek(Token![..]) {
2393 input.parse().map(RangeLimits::HalfOpen)
2394 } else {
2395 Err(lookahead.error())
2396 }
2397 }
Alex Crichton954046c2017-05-30 21:49:42 -07002398 }
David Tolnay438c9052016-10-07 23:24:48 -07002399
Alex Crichton954046c2017-05-30 21:49:42 -07002400 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002401 #[cfg(not(feature = "full"))]
2402 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002403 pair: shim!(qpath) >>
David Tolnayeb981bb2018-07-21 19:31:38 -07002404 (ExprPath {
2405 attrs: Vec::new(),
2406 qself: pair.0,
2407 path: pair.1,
2408 })
2409 ));
2410
2411 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002412 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002413 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay9389c382018-08-27 09:13:37 -07002414 pair: shim!(qpath) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002415 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002416 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002417 qself: pair.0,
2418 path: pair.1,
2419 })
2420 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002421 }
David Tolnay42602292016-10-01 22:25:45 -07002422
David Tolnay9389c382018-08-27 09:13:37 -07002423 named2!(path -> Path, do_parse!(
David Tolnay9cc2f092018-08-24 15:51:37 -04002424 colon: option!(punct!(::)) >>
2425 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2426 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2427 (Path {
2428 leading_colon: colon,
2429 segments: segments,
2430 })
2431 ));
2432
2433 named!(path_segment -> PathSegment, alt!(
2434 do_parse!(
2435 ident: syn!(Ident) >>
2436 colon2: punct!(::) >>
2437 lt: punct!(<) >>
2438 args: call!(Punctuated::parse_terminated) >>
2439 gt: punct!(>) >>
2440 (PathSegment {
2441 ident: ident,
2442 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2443 colon2_token: Some(colon2),
2444 lt_token: lt,
2445 args: args,
2446 gt_token: gt,
2447 }),
2448 })
2449 )
2450 |
David Tolnaya7d69fc2018-08-26 13:30:24 -04002451 old_mod_style_path_segment
David Tolnay9cc2f092018-08-24 15:51:37 -04002452 ));
2453
David Tolnay9389c382018-08-27 09:13:37 -07002454 named2!(qpath -> (Option<QSelf>, Path), alt!(
2455 map!(shim!(path), |p| (None, p))
David Tolnay9cc2f092018-08-24 15:51:37 -04002456 |
2457 do_parse!(
2458 lt: punct!(<) >>
2459 this: syn!(Type) >>
2460 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2461 gt: punct!(>) >>
2462 colon2: punct!(::) >>
2463 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2464 ({
2465 let (pos, as_, path) = match path {
2466 Some((as_, mut path)) => {
2467 let pos = path.segments.len();
2468 path.segments.push_punct(colon2);
2469 path.segments.extend(rest.into_pairs());
2470 (pos, Some(as_), path)
2471 }
2472 None => {
2473 (0, None, Path {
2474 leading_colon: Some(colon2),
2475 segments: rest,
2476 })
2477 }
2478 };
2479 (Some(QSelf {
2480 lt_token: lt,
2481 ty: Box::new(this),
2482 position: pos,
2483 as_token: as_,
2484 gt_token: gt,
2485 }), path)
2486 })
2487 )
2488 |
2489 map!(keyword!(self), |s| (None, s.into()))
2490 ));
2491
Michael Layzell734adb42017-06-07 16:58:31 -04002492 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002493 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002494 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002495 stmts: braces!(Block::old_parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002496 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002497 brace_token: stmts.0,
2498 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002499 })
2500 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002501 }
David Tolnay939766a2016-09-23 23:48:12 -07002502
Michael Layzell734adb42017-06-07 16:58:31 -04002503 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002504 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002505 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
2506 input.step_cursor(|cursor| Self::old_parse_within(*cursor))
2507 }
2508
2509 named!(old_parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002510 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002511 mut standalone: many0!(do_parse!(
2512 stmt: syn!(Stmt) >>
2513 many0!(punct!(;)) >>
2514 (stmt)
2515 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002516 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002517 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002518 mut e: syn!(Expr) >>
2519 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002520 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002521 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002522 })
2523 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002524 (match last {
2525 None => standalone,
2526 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002527 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002528 standalone
2529 }
2530 })
2531 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002532 }
2533
Michael Layzell734adb42017-06-07 16:58:31 -04002534 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002535 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002536 named!(parse -> Self, alt!(
David Tolnay9389c382018-08-27 09:13:37 -07002537 shim!(stmt_mac)
Michael Layzell92639a52017-06-01 00:07:44 -04002538 |
David Tolnay9389c382018-08-27 09:13:37 -07002539 shim!(stmt_local)
Michael Layzell92639a52017-06-01 00:07:44 -04002540 |
David Tolnay9389c382018-08-27 09:13:37 -07002541 shim!(stmt_item)
Michael Layzell92639a52017-06-01 00:07:44 -04002542 |
David Tolnay9389c382018-08-27 09:13:37 -07002543 shim!(stmt_blockexpr)
Michael Layzell35418782017-06-07 09:20:25 -04002544 |
David Tolnay9389c382018-08-27 09:13:37 -07002545 shim!(stmt_expr)
Michael Layzell92639a52017-06-01 00:07:44 -04002546 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002547 }
David Tolnay939766a2016-09-23 23:48:12 -07002548
Michael Layzell734adb42017-06-07 16:58:31 -04002549 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002550 named2!(stmt_mac -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002551 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaya7d69fc2018-08-26 13:30:24 -04002552 what: call!(Path::old_parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002553 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002554 // Only parse braces here; paren and bracket will get parsed as
2555 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002556 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002557 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002558 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002559 attrs: attrs,
2560 ident: None,
2561 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002562 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002563 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002564 delimiter: MacroDelimiter::Brace(data.0),
2565 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002566 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002567 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002568 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002569 ));
2570
Michael Layzell734adb42017-06-07 16:58:31 -04002571 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002572 named2!(stmt_local -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002573 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002574 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002575 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002576 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002577 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2578 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002579 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002580 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002581 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002582 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002583 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2584 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2585 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002586 }))
David Tolnay191e0582016-10-02 18:31:09 -07002587 ));
2588
Michael Layzell734adb42017-06-07 16:58:31 -04002589 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002590 named2!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002591
Michael Layzell734adb42017-06-07 16:58:31 -04002592 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002593 named2!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002594 mut attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay9389c382018-08-27 09:13:37 -07002595 mut e: shim!(expr_nosemi) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002596 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002597 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002598 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002599 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002600 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002601 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002602 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002603 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002604 }
2605 })
2606 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002607
Michael Layzell734adb42017-06-07 16:58:31 -04002608 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002609 named2!(stmt_expr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002610 mut attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002611 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002612 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002613 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002614 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002615 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002616 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002617 })
David Tolnay939766a2016-09-23 23:48:12 -07002618 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002619
Michael Layzell734adb42017-06-07 16:58:31 -04002620 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002621 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002622 named!(parse -> Self, alt!(
2623 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2624 |
2625 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2626 |
2627 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2628 |
2629 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2630 |
2631 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2632 |
David Tolnay323279a2017-12-29 11:26:32 -05002633 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002634 |
2635 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2636 |
2637 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2638 |
2639 syn!(PatPath) => { Pat::Path }
2640 |
2641 syn!(PatTuple) => { Pat::Tuple }
2642 |
2643 syn!(PatRef) => { Pat::Ref }
2644 |
2645 syn!(PatSlice) => { Pat::Slice }
2646 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002647 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002648
Michael Layzell734adb42017-06-07 16:58:31 -04002649 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002650 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002651 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002652 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002653 |u| PatWild { underscore_token: u }
2654 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002655 }
David Tolnay84aa0752016-10-02 23:01:13 -07002656
Michael Layzell734adb42017-06-07 16:58:31 -04002657 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002658 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002659 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002660 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002661 pat: syn!(Pat) >>
2662 (PatBox {
2663 pat: Box::new(pat),
2664 box_token: boxed,
2665 })
2666 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002667 }
2668
Michael Layzell734adb42017-06-07 16:58:31 -04002669 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002670 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002671 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002672 by_ref: option!(keyword!(ref)) >>
2673 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002674 name: alt!(
2675 syn!(Ident)
2676 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002677 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002678 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002679 not!(punct!(<)) >>
2680 not!(punct!(::)) >>
2681 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002682 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002683 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002684 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002685 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002686 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002687 })
2688 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002689 }
2690
Michael Layzell734adb42017-06-07 16:58:31 -04002691 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002692 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002693 named!(parse -> Self, do_parse!(
2694 path: syn!(Path) >>
2695 tuple: syn!(PatTuple) >>
2696 (PatTupleStruct {
2697 path: path,
2698 pat: tuple,
2699 })
2700 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002701 }
2702
Michael Layzell734adb42017-06-07 16:58:31 -04002703 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002704 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002705 named!(parse -> Self, do_parse!(
2706 path: syn!(Path) >>
2707 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002708 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002709 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002710 (fields, base)
2711 )) >>
2712 (PatStruct {
2713 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002714 fields: (data.1).0,
2715 brace_token: data.0,
2716 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002717 })
2718 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002719 }
2720
Michael Layzell734adb42017-06-07 16:58:31 -04002721 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002722 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002723 named!(parse -> Self, alt!(
2724 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002725 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002726 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002727 pat: syn!(Pat) >>
2728 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002729 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002730 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002731 attrs: Vec::new(),
2732 colon_token: Some(colon),
2733 })
2734 )
2735 |
2736 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002737 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002738 by_ref: option!(keyword!(ref)) >>
2739 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002740 ident: syn!(Ident) >>
2741 ({
2742 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002743 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002744 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002745 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002746 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002747 }.into();
2748 if let Some(boxed) = boxed {
2749 pat = PatBox {
2750 pat: Box::new(pat),
2751 box_token: boxed,
2752 }.into();
2753 }
2754 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002755 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002756 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002757 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002758 colon_token: None,
2759 }
2760 })
2761 )
2762 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002763 }
2764
David Tolnay1501f7e2018-08-27 14:21:03 -07002765 impl Parse for Member {
2766 fn parse(input: ParseStream) -> Result<Self> {
2767 if input.peek(Ident) {
2768 input.parse().map(Member::Named)
2769 } else if input.peek(LitInt) {
2770 input.parse().map(Member::Unnamed)
2771 } else {
2772 Err(input.error("expected identifier or integer"))
2773 }
2774 }
David Tolnay85b69a42017-12-27 20:43:10 -05002775 }
2776
David Tolnay1501f7e2018-08-27 14:21:03 -07002777 impl Parse for Index {
2778 fn parse(input: ParseStream) -> Result<Self> {
2779 let lit: LitInt = input.parse()?;
2780 if let IntSuffix::None = lit.suffix() {
2781 Ok(Index {
2782 index: lit.value() as u32,
2783 span: lit.span(),
2784 })
2785 } else {
2786 Err(input.error("expected unsuffixed integer"))
2787 }
2788 }
David Tolnay85b69a42017-12-27 20:43:10 -05002789 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002790
Michael Layzell734adb42017-06-07 16:58:31 -04002791 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002792 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002793 named!(parse -> Self, map!(
2794 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002795 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002796 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002797 }
David Tolnay9636c052016-10-02 17:11:17 -07002798
Michael Layzell734adb42017-06-07 16:58:31 -04002799 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002800 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002801 named!(parse -> Self, do_parse!(
2802 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002803 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002804 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2805 tuple!(punct!(..), option!(punct!(,)))
2806 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002807 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002808 Some((_, Some(_))) => true,
2809 _ => false,
2810 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002811 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002812 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002813 )) >>
2814 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002815 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002816 let (dotdot, trailing) = match dotdot {
2817 Some((a, b)) => (Some(a), Some(b)),
2818 None => (None, None),
2819 };
2820 PatTuple {
2821 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002822 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002823 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002824 comma_token: trailing.unwrap_or_default(),
2825 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002826 }
2827 })
2828 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002829 }
David Tolnayfbb73232016-10-03 01:00:06 -07002830
Michael Layzell734adb42017-06-07 16:58:31 -04002831 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002832 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002833 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002834 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002835 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002836 pat: syn!(Pat) >>
2837 (PatRef {
2838 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002839 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002840 and_token: and,
2841 })
2842 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002843 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002844
Michael Layzell734adb42017-06-07 16:58:31 -04002845 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002846 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002847 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002848 lit: shim!(pat_lit_expr) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002849 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002850 return parse_error(); // these need to be parsed by pat_path
2851 } else {
2852 PatLit {
2853 expr: Box::new(lit),
2854 }
2855 })
2856 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002857 }
David Tolnaye1310902016-10-29 23:40:00 -07002858
Michael Layzell734adb42017-06-07 16:58:31 -04002859 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002860 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002861 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002862 lo: shim!(pat_lit_expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002863 limits: syn!(RangeLimits) >>
David Tolnay9389c382018-08-27 09:13:37 -07002864 hi: shim!(pat_lit_expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002865 (PatRange {
2866 lo: Box::new(lo),
2867 hi: Box::new(hi),
2868 limits: limits,
2869 })
2870 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002871 }
David Tolnaye1310902016-10-29 23:40:00 -07002872
Michael Layzell734adb42017-06-07 16:58:31 -04002873 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002874 named2!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002875 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002876 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002877 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002878 |
David Tolnay8c91b882017-12-28 23:04:32 -05002879 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002880 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002881 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002882 Expr::Unary(ExprUnary {
2883 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002884 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002885 expr: Box::new(v)
2886 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002887 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002888 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002889 })
2890 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002891
Michael Layzell734adb42017-06-07 16:58:31 -04002892 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002893 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002894 named!(parse -> Self, map!(
2895 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002896 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002897 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002898 dots: punct!(..) >>
2899 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002900 (dots, trailing)
2901 )) >>
2902 after: cond!(
2903 match middle {
2904 Some((_, ref trailing)) => trailing.is_some(),
2905 _ => false,
2906 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002907 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002908 ) >>
2909 (before, middle, after)
2910 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002911 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002912 let mut before: Punctuated<Pat, Token![,]> = before;
2913 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002914 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002915 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04002916 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04002917 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04002918 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04002919 }),
2920 bracket_token: brackets,
2921 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002922 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002923 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002924 } else {
David Tolnay56080682018-01-06 14:01:52 -08002925 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002926 }
2927 }),
2928 front: before,
2929 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002930 }
Alex Crichton954046c2017-05-30 21:49:42 -07002931 }
Michael Layzell92639a52017-06-01 00:07:44 -04002932 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002933 }
David Tolnay323279a2017-12-29 11:26:32 -05002934
2935 #[cfg(feature = "full")]
2936 impl Synom for PatMacro {
2937 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
2938 }
David Tolnay1501f7e2018-08-27 14:21:03 -07002939
2940 #[cfg(feature = "full")]
2941 impl Member {
2942 fn is_named(&self) -> bool {
2943 match *self {
2944 Member::Named(_) => true,
2945 Member::Unnamed(_) => false,
2946 }
2947 }
2948 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002949}
2950
David Tolnayf4bbbd92016-09-23 14:41:55 -07002951#[cfg(feature = "printing")]
2952mod printing {
2953 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002954 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002955 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07002956 use proc_macro2::{Literal, TokenStream};
2957 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002958
David Tolnaybcf26022017-12-25 22:10:52 -05002959 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07002960 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002961 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07002962 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002963 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002964 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002965 e.to_tokens(tokens);
2966 });
2967 } else {
2968 e.to_tokens(tokens);
2969 }
2970 }
2971
David Tolnay8c91b882017-12-28 23:04:32 -05002972 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07002973 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05002974 tokens.append_all(attrs.outer());
2975 }
Michael Layzell734adb42017-06-07 16:58:31 -04002976
David Tolnayd997aef2018-07-21 18:42:31 -07002977 #[cfg(feature = "full")]
2978 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
2979 tokens.append_all(attrs.inner());
2980 }
2981
David Tolnay8c91b882017-12-28 23:04:32 -05002982 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07002983 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
2984
2985 #[cfg(not(feature = "full"))]
2986 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002987
Michael Layzell734adb42017-06-07 16:58:31 -04002988 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002989 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002990 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07002991 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002992 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002993 self.expr.to_tokens(tokens);
2994 }
2995 }
2996
Michael Layzell734adb42017-06-07 16:58:31 -04002997 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002998 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002999 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003000 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003001 self.place.to_tokens(tokens);
3002 self.arrow_token.to_tokens(tokens);
3003 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003004 }
3005 }
3006
Michael Layzell734adb42017-06-07 16:58:31 -04003007 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003008 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003009 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003010 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003012 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003013 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003014 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003015 }
3016 }
3017
3018 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003019 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003020 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003021 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003022 self.paren_token.surround(tokens, |tokens| {
3023 self.args.to_tokens(tokens);
3024 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003025 }
3026 }
3027
Michael Layzell734adb42017-06-07 16:58:31 -04003028 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003029 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003030 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003031 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003032 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003033 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003034 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003035 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003036 self.paren_token.surround(tokens, |tokens| {
3037 self.args.to_tokens(tokens);
3038 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003039 }
3040 }
3041
Michael Layzell734adb42017-06-07 16:58:31 -04003042 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003043 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003044 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003045 self.colon2_token.to_tokens(tokens);
3046 self.lt_token.to_tokens(tokens);
3047 self.args.to_tokens(tokens);
3048 self.gt_token.to_tokens(tokens);
3049 }
3050 }
3051
3052 #[cfg(feature = "full")]
3053 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003054 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003055 match *self {
3056 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3057 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3058 }
3059 }
3060 }
3061
3062 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003063 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003064 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003065 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003066 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003067 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003068 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003069 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003070 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003071 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003072 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003073 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003074 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003075 }
3076 }
3077
3078 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003079 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003080 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003081 self.left.to_tokens(tokens);
3082 self.op.to_tokens(tokens);
3083 self.right.to_tokens(tokens);
3084 }
3085 }
3086
3087 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003088 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003089 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003090 self.op.to_tokens(tokens);
3091 self.expr.to_tokens(tokens);
3092 }
3093 }
3094
David Tolnay8c91b882017-12-28 23:04:32 -05003095 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003096 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003097 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003098 self.lit.to_tokens(tokens);
3099 }
3100 }
3101
Alex Crichton62a0a592017-05-22 13:58:53 -07003102 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003103 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003104 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003105 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003106 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003107 self.ty.to_tokens(tokens);
3108 }
3109 }
3110
David Tolnay0cf94f22017-12-28 23:46:26 -05003111 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003112 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003113 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003114 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003115 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003116 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003117 self.ty.to_tokens(tokens);
3118 }
3119 }
3120
Michael Layzell734adb42017-06-07 16:58:31 -04003121 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003122 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003123 if let Some((ref else_token, ref else_)) = *else_ {
3124 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003125
3126 // If we are not one of the valid expressions to exist in an else
3127 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003128 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003129 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003130 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003131 }
3132 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003133 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003134 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003135 });
3136 }
3137 }
3138 }
3139 }
3140
3141 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003142 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003143 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003144 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003145 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003146 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003147 self.then_branch.to_tokens(tokens);
3148 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003149 }
3150 }
3151
Michael Layzell734adb42017-06-07 16:58:31 -04003152 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003153 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003154 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003155 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003156 self.if_token.to_tokens(tokens);
3157 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003158 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003159 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003160 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003161 self.then_branch.to_tokens(tokens);
3162 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003163 }
3164 }
3165
Michael Layzell734adb42017-06-07 16:58:31 -04003166 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003167 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003168 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003169 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003170 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003171 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003172 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003173 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003174 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003175 tokens.append_all(&self.body.stmts);
3176 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003177 }
3178 }
3179
Michael Layzell734adb42017-06-07 16:58:31 -04003180 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003181 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003182 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003183 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003184 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003185 self.while_token.to_tokens(tokens);
3186 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003187 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003188 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003189 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003190 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003191 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003192 tokens.append_all(&self.body.stmts);
3193 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003194 }
3195 }
3196
Michael Layzell734adb42017-06-07 16:58:31 -04003197 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003198 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003199 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003200 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003201 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003202 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003203 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003204 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003205 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003206 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003207 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003208 tokens.append_all(&self.body.stmts);
3209 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003210 }
3211 }
3212
Michael Layzell734adb42017-06-07 16:58:31 -04003213 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003214 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003215 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003216 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003217 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003218 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003219 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003220 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003221 tokens.append_all(&self.body.stmts);
3222 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003223 }
3224 }
3225
Michael Layzell734adb42017-06-07 16:58:31 -04003226 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003227 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003228 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003229 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003230 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003231 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003232 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003233 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003234 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003235 arm.to_tokens(tokens);
3236 // Ensure that we have a comma after a non-block arm, except
3237 // for the last one.
3238 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003239 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003240 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003241 }
3242 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003243 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003244 }
3245 }
3246
Michael Layzell734adb42017-06-07 16:58:31 -04003247 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003248 impl ToTokens for ExprAsync {
3249 fn to_tokens(&self, tokens: &mut TokenStream) {
3250 outer_attrs_to_tokens(&self.attrs, tokens);
3251 self.async_token.to_tokens(tokens);
3252 self.capture.to_tokens(tokens);
3253 self.block.to_tokens(tokens);
3254 }
3255 }
3256
3257 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003258 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003259 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003260 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003261 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003262 self.block.to_tokens(tokens);
3263 }
3264 }
3265
Michael Layzell734adb42017-06-07 16:58:31 -04003266 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003267 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003268 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003269 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003270 self.yield_token.to_tokens(tokens);
3271 self.expr.to_tokens(tokens);
3272 }
3273 }
3274
3275 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003276 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003277 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003278 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003279 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003280 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003281 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003282 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003283 for input in self.inputs.pairs() {
3284 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003285 FnArg::Captured(ArgCaptured {
3286 ref pat,
3287 ty: Type::Infer(_),
3288 ..
3289 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003290 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003291 }
David Tolnay56080682018-01-06 14:01:52 -08003292 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003293 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003294 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003295 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003296 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003297 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003298 self.body.to_tokens(tokens);
3299 }
3300 }
3301
Michael Layzell734adb42017-06-07 16:58:31 -04003302 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003303 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003304 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003305 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003306 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003307 self.block.brace_token.surround(tokens, |tokens| {
3308 inner_attrs_to_tokens(&self.attrs, tokens);
3309 tokens.append_all(&self.block.stmts);
3310 });
Nika Layzell640832a2017-12-04 13:37:09 -05003311 }
3312 }
3313
3314 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003315 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003316 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003317 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003318 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003319 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003320 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003321 tokens.append_all(&self.block.stmts);
3322 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003323 }
3324 }
3325
Michael Layzell734adb42017-06-07 16:58:31 -04003326 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003327 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003328 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003329 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003330 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003331 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003332 self.right.to_tokens(tokens);
3333 }
3334 }
3335
Michael Layzell734adb42017-06-07 16:58:31 -04003336 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003337 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003338 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003339 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003340 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003341 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003342 self.right.to_tokens(tokens);
3343 }
3344 }
3345
3346 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003347 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003348 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003349 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003350 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003351 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003352 }
3353 }
3354
David Tolnay85b69a42017-12-27 20:43:10 -05003355 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003356 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003357 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003358 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003359 Member::Unnamed(ref index) => index.to_tokens(tokens),
3360 }
3361 }
3362 }
3363
David Tolnay85b69a42017-12-27 20:43:10 -05003364 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003365 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003366 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3367 lit.set_span(self.span);
3368 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003369 }
3370 }
3371
3372 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003373 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003374 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003375 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003376 self.bracket_token.surround(tokens, |tokens| {
3377 self.index.to_tokens(tokens);
3378 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003379 }
3380 }
3381
Michael Layzell734adb42017-06-07 16:58:31 -04003382 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003383 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003384 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003385 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003386 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003387 match self.limits {
3388 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3389 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3390 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003391 self.to.to_tokens(tokens);
3392 }
3393 }
3394
3395 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003396 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003397 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003398 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003399 }
3400 }
3401
Michael Layzell734adb42017-06-07 16:58:31 -04003402 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003403 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003404 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003405 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003406 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003407 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003408 self.expr.to_tokens(tokens);
3409 }
3410 }
3411
Michael Layzell734adb42017-06-07 16:58:31 -04003412 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003413 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003414 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003415 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003416 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003417 self.label.to_tokens(tokens);
3418 self.expr.to_tokens(tokens);
3419 }
3420 }
3421
Michael Layzell734adb42017-06-07 16:58:31 -04003422 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003423 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003424 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003425 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003426 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003427 self.label.to_tokens(tokens);
3428 }
3429 }
3430
Michael Layzell734adb42017-06-07 16:58:31 -04003431 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003432 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003433 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003434 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003435 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003436 self.expr.to_tokens(tokens);
3437 }
3438 }
3439
Michael Layzell734adb42017-06-07 16:58:31 -04003440 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003441 impl ToTokens for ExprMacro {
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 Tolnay8c91b882017-12-28 23:04:32 -05003444 self.mac.to_tokens(tokens);
3445 }
3446 }
3447
3448 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003449 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003450 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003451 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003452 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003453 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003454 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003455 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003456 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003457 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003458 self.rest.to_tokens(tokens);
3459 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003460 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003461 }
3462 }
3463
Michael Layzell734adb42017-06-07 16:58:31 -04003464 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003465 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003466 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003467 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003468 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003469 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003470 self.expr.to_tokens(tokens);
3471 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003472 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003473 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003474 }
3475 }
3476
David Tolnaye98775f2017-12-28 23:17:00 -05003477 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003478 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003479 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003480 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003481 self.group_token.surround(tokens, |tokens| {
3482 self.expr.to_tokens(tokens);
3483 });
3484 }
3485 }
3486
Alex Crichton62a0a592017-05-22 13:58:53 -07003487 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003488 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003489 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003490 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003491 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003492 self.expr.to_tokens(tokens);
3493 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003494 }
3495 }
3496
Michael Layzell734adb42017-06-07 16:58:31 -04003497 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003498 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003499 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003500 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003501 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003502 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003503 }
3504 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003505
David Tolnay2ae520a2017-12-29 11:19:50 -05003506 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003507 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003508 self.tts.to_tokens(tokens);
3509 }
3510 }
3511
Michael Layzell734adb42017-06-07 16:58:31 -04003512 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003513 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003514 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003515 self.name.to_tokens(tokens);
3516 self.colon_token.to_tokens(tokens);
3517 }
3518 }
3519
3520 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003521 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003522 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003523 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003524 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003525 if let Some(ref colon_token) = self.colon_token {
3526 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003527 self.expr.to_tokens(tokens);
3528 }
David Tolnay055a7042016-10-02 19:23:54 -07003529 }
3530 }
3531
Michael Layzell734adb42017-06-07 16:58:31 -04003532 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003533 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003534 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003535 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003536 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003537 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003538 if let Some((ref if_token, ref guard)) = self.guard {
3539 if_token.to_tokens(tokens);
3540 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003541 }
David Tolnaydfb91432018-03-31 19:19:44 +02003542 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003543 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003544 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003545 }
3546 }
3547
Michael Layzell734adb42017-06-07 16:58:31 -04003548 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003549 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003550 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003551 self.underscore_token.to_tokens(tokens);
3552 }
3553 }
3554
Michael Layzell734adb42017-06-07 16:58:31 -04003555 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003556 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003557 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003558 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003559 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003560 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003561 if let Some((ref at_token, ref subpat)) = self.subpat {
3562 at_token.to_tokens(tokens);
3563 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003564 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003565 }
3566 }
3567
Michael Layzell734adb42017-06-07 16:58:31 -04003568 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003569 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003570 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003571 self.path.to_tokens(tokens);
3572 self.brace_token.surround(tokens, |tokens| {
3573 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003574 // NOTE: We need a comma before the dot2 token if it is present.
3575 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003576 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003577 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003578 self.dot2_token.to_tokens(tokens);
3579 });
3580 }
3581 }
3582
Michael Layzell734adb42017-06-07 16:58:31 -04003583 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003584 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003585 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003586 self.path.to_tokens(tokens);
3587 self.pat.to_tokens(tokens);
3588 }
3589 }
3590
Michael Layzell734adb42017-06-07 16:58:31 -04003591 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003592 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003593 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003594 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3595 }
3596 }
3597
Michael Layzell734adb42017-06-07 16:58:31 -04003598 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003599 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003600 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003601 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003602 self.front.to_tokens(tokens);
3603 if let Some(ref dot2_token) = self.dot2_token {
3604 if !self.front.empty_or_trailing() {
3605 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003606 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003607 }
David Tolnay41871922017-12-29 01:53:45 -05003608 dot2_token.to_tokens(tokens);
3609 self.comma_token.to_tokens(tokens);
3610 if self.comma_token.is_none() && !self.back.is_empty() {
3611 // Ensure there is a comma after the .. token.
3612 <Token![,]>::default().to_tokens(tokens);
3613 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003614 }
David Tolnay41871922017-12-29 01:53:45 -05003615 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003616 });
3617 }
3618 }
3619
Michael Layzell734adb42017-06-07 16:58:31 -04003620 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003621 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003622 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003623 self.box_token.to_tokens(tokens);
3624 self.pat.to_tokens(tokens);
3625 }
3626 }
3627
Michael Layzell734adb42017-06-07 16:58:31 -04003628 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003629 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003630 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003631 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003632 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003633 self.pat.to_tokens(tokens);
3634 }
3635 }
3636
Michael Layzell734adb42017-06-07 16:58:31 -04003637 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003638 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003639 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003640 self.expr.to_tokens(tokens);
3641 }
3642 }
3643
Michael Layzell734adb42017-06-07 16:58:31 -04003644 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003645 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003646 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003647 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003648 match self.limits {
3649 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003650 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003651 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003652 self.hi.to_tokens(tokens);
3653 }
3654 }
3655
Michael Layzell734adb42017-06-07 16:58:31 -04003656 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003657 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003658 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003659 // XXX: This is a mess, and it will be so easy to screw it up. How
3660 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003661 self.bracket_token.surround(tokens, |tokens| {
3662 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003663
3664 // If we need a comma before the middle or standalone .. token,
3665 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003666 if !self.front.empty_or_trailing()
3667 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003668 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003669 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003670 }
3671
3672 // If we have an identifier, we always need a .. token.
3673 if self.middle.is_some() {
3674 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003675 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003676 } else if self.dot2_token.is_some() {
3677 self.dot2_token.to_tokens(tokens);
3678 }
3679
3680 // Make sure we have a comma before the back half.
3681 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003682 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003683 self.back.to_tokens(tokens);
3684 } else {
3685 self.comma_token.to_tokens(tokens);
3686 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003687 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003688 }
3689 }
3690
Michael Layzell734adb42017-06-07 16:58:31 -04003691 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003692 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003693 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003694 self.mac.to_tokens(tokens);
3695 }
3696 }
3697
3698 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003699 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003700 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003701 self.tts.to_tokens(tokens);
3702 }
3703 }
3704
3705 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003706 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003707 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003708 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003709 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003710 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003711 }
3712 self.pat.to_tokens(tokens);
3713 }
3714 }
3715
Michael Layzell734adb42017-06-07 16:58:31 -04003716 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003717 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003718 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003719 self.brace_token.surround(tokens, |tokens| {
3720 tokens.append_all(&self.stmts);
3721 });
David Tolnay42602292016-10-01 22:25:45 -07003722 }
3723 }
3724
Michael Layzell734adb42017-06-07 16:58:31 -04003725 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003726 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003727 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003728 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003729 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003730 Stmt::Item(ref item) => item.to_tokens(tokens),
3731 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003732 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003733 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003734 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003735 }
David Tolnay42602292016-10-01 22:25:45 -07003736 }
3737 }
3738 }
David Tolnay191e0582016-10-02 18:31:09 -07003739
Michael Layzell734adb42017-06-07 16:58:31 -04003740 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003741 impl ToTokens for Local {
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 Crichtonccbb45d2017-05-23 10:58:24 -07003744 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003745 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003746 if let Some((ref colon_token, ref ty)) = self.ty {
3747 colon_token.to_tokens(tokens);
3748 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003749 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003750 if let Some((ref eq_token, ref init)) = self.init {
3751 eq_token.to_tokens(tokens);
3752 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003753 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003754 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003755 }
3756 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003757}