blob: b27062c1ddb68153dd2b9767f6eccab532b4419d [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 Tolnay9389c382018-08-27 09:13:37 -07001101 named2!(expr_no_struct -> Expr, shim!(ambiguous_expr, AllowStruct(false), AllowBlock(true)));
David Tolnayaf2557e2016-10-24 11:52:21 -07001102
David Tolnaybcf26022017-12-25 22:10:52 -05001103 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001104 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001105 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1106 assign_expr(input, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001107 }
1108
Michael Layzell734adb42017-06-07 16:58:31 -04001109 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001110 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001111 // NOTE: We intentionally skip assign_expr, placement_expr, and
David Tolnay9389c382018-08-27 09:13:37 -07001112 // range_expr as they are only parsed in full mode.
1113 or_expr(input, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001114 }
1115
David Tolnaybcf26022017-12-25 22:10:52 -05001116 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001117 macro_rules! binop {
1118 (
1119 $name: ident,
1120 $next: ident,
1121 $submac: ident!( $($args:tt)* )
1122 ) => {
David Tolnay9389c382018-08-27 09:13:37 -07001123 fn $name(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1124 mod synom {
1125 use super::*;
1126
1127 named!(pub $name(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1128 mut e: shim!($next, allow_struct, allow_block) >>
1129 many0!(do_parse!(
1130 op: $submac!($($args)*) >>
1131 rhs: shim!($next, allow_struct, AllowBlock(true)) >>
1132 ({
1133 e = ExprBinary {
1134 attrs: Vec::new(),
1135 left: Box::new(e.into()),
1136 op: op,
1137 right: Box::new(rhs.into()),
1138 }.into();
1139 })
1140 )) >>
1141 (e)
1142 ));
1143 }
1144 input.step_cursor(|cursor| synom::$name(*cursor, allow_struct, allow_block))
1145 }
Alex Crichton954046c2017-05-30 21:49:42 -07001146 }
David Tolnay54e854d2016-10-24 12:03:30 -07001147 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001148
David Tolnaybcf26022017-12-25 22:10:52 -05001149 // <placement> = <placement> ..
1150 // <placement> += <placement> ..
1151 // <placement> -= <placement> ..
1152 // <placement> *= <placement> ..
1153 // <placement> /= <placement> ..
1154 // <placement> %= <placement> ..
1155 // <placement> ^= <placement> ..
1156 // <placement> &= <placement> ..
1157 // <placement> |= <placement> ..
1158 // <placement> <<= <placement> ..
1159 // <placement> >>= <placement> ..
1160 //
1161 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001162 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001163 named2!(assign_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1164 mut e: shim!(placement_expr, allow_struct, allow_block) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001165 alt!(
1166 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001167 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 // Recurse into self to parse right-associative operator.
David Tolnay9389c382018-08-27 09:13:37 -07001169 rhs: shim!(assign_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001170 ({
1171 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001172 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001173 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001174 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001175 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001176 }.into();
1177 })
1178 )
1179 |
1180 do_parse!(
David Tolnay2a54cfb2018-08-26 18:54:19 -04001181 op: shim!(BinOp::parse_assign_op) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001182 // Recurse into self to parse right-associative operator.
David Tolnay9389c382018-08-27 09:13:37 -07001183 rhs: shim!(assign_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 ({
1185 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001186 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001187 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001189 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001190 }.into();
1191 })
1192 )
1193 |
1194 epsilon!()
1195 ) >>
1196 (e)
1197 ));
1198
David Tolnaybcf26022017-12-25 22:10:52 -05001199 // <range> <- <range> ..
1200 //
1201 // NOTE: The `in place { expr }` version of this syntax is parsed in
1202 // `atom_expr`, not here.
1203 //
1204 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001205 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001206 named2!(placement_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1207 mut e: shim!(range_expr, allow_struct, allow_block) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001208 alt!(
1209 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001210 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001211 // Recurse into self to parse right-associative operator.
David Tolnay9389c382018-08-27 09:13:37 -07001212 rhs: shim!(placement_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001213 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001214 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001215 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001216 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001217 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001218 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001219 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001220 }.into();
1221 })
1222 )
1223 |
1224 epsilon!()
1225 ) >>
1226 (e)
1227 ));
1228
David Tolnaybcf26022017-12-25 22:10:52 -05001229 // <or> ... <or> ..
1230 // <or> .. <or> ..
1231 // <or> ..
1232 //
1233 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1234 // rules are for parsing these expressions are, but this is not correct.
1235 // For example, `a .. b .. c` is not a legal expression. It should not
1236 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1237 //
1238 // NOTE: The form of ranges which don't include a preceding expression are
1239 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001240 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001241 named2!(range_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1242 mut e: shim!(or_expr, allow_struct, allow_block) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001243 many0!(do_parse!(
1244 limits: syn!(RangeLimits) >>
1245 // We don't want to allow blocks here if we don't allow structs. See
1246 // the reasoning for `opt_ambiguous_expr!` above.
David Tolnay9389c382018-08-27 09:13:37 -07001247 hi: option!(shim!(or_expr, allow_struct, AllowBlock(allow_struct.0))) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 ({
1249 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001250 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001251 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001253 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001254 }.into();
1255 })
1256 )) >>
1257 (e)
1258 ));
1259
David Tolnaybcf26022017-12-25 22:10:52 -05001260 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001262
David Tolnaybcf26022017-12-25 22:10:52 -05001263 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001264 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001265
David Tolnaybcf26022017-12-25 22:10:52 -05001266 // <bitor> == <bitor> ...
1267 // <bitor> != <bitor> ...
1268 // <bitor> >= <bitor> ...
1269 // <bitor> <= <bitor> ...
1270 // <bitor> > <bitor> ...
1271 // <bitor> < <bitor> ...
1272 //
1273 // NOTE: This operator appears to be parsed as left-associative, but errors
1274 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001275 binop!(
1276 compare_expr,
1277 bitor_expr,
1278 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001279 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001280 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001281 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001282 |
1283 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001284 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001285 |
1286 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001287 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001288 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001289 do_parse!(
1290 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001291 not!(punct!(<-)) >>
1292 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001293 (BinOp::Lt(t))
1294 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001295 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001296 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001297 )
1298 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001299
David Tolnaybcf26022017-12-25 22:10:52 -05001300 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001301 binop!(
1302 bitor_expr,
1303 bitxor_expr,
1304 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1305 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001306
David Tolnaybcf26022017-12-25 22:10:52 -05001307 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001308 binop!(
1309 bitxor_expr,
1310 bitand_expr,
1311 do_parse!(
1312 // NOTE: Make sure we aren't looking at ^=.
1313 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1314 )
1315 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001316
David Tolnaybcf26022017-12-25 22:10:52 -05001317 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001318 binop!(
1319 bitand_expr,
1320 shift_expr,
1321 do_parse!(
1322 // NOTE: Make sure we aren't looking at && or &=.
1323 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1324 )
1325 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326
David Tolnaybcf26022017-12-25 22:10:52 -05001327 // <arith> << <arith> ...
1328 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001329 binop!(
1330 shift_expr,
1331 arith_expr,
1332 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001333 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001335 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001336 )
1337 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338
David Tolnaybcf26022017-12-25 22:10:52 -05001339 // <term> + <term> ...
1340 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001341 binop!(
1342 arith_expr,
1343 term_expr,
1344 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001345 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001346 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001347 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001348 )
1349 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001350
David Tolnaybcf26022017-12-25 22:10:52 -05001351 // <cast> * <cast> ...
1352 // <cast> / <cast> ...
1353 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001354 binop!(
1355 term_expr,
1356 cast_expr,
1357 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001358 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001359 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001360 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001361 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001362 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001363 )
1364 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001365
David Tolnaybcf26022017-12-25 22:10:52 -05001366 // <unary> as <ty>
1367 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001368 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001369 named2!(cast_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1370 mut e: shim!(unary_expr, allow_struct, allow_block) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001371 many0!(alt!(
1372 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001373 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001374 // We can't accept `A + B` in cast expressions, as it's
1375 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001376 ty: shim!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001377 ({
1378 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001379 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001380 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001381 as_token: as_,
1382 ty: Box::new(ty),
1383 }.into();
1384 })
1385 )
1386 |
1387 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001388 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001389 // We can't accept `A + B` in cast expressions, as it's
1390 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001391 ty: shim!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001392 ({
1393 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001394 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001395 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001396 colon_token: colon,
1397 ty: Box::new(ty),
1398 }.into();
1399 })
1400 )
1401 )) >>
1402 (e)
1403 ));
1404
David Tolnay0cf94f22017-12-28 23:46:26 -05001405 // <unary> as <ty>
1406 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001407 named2!(cast_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1408 mut e: shim!(unary_expr, allow_struct, allow_block) >>
David Tolnay0cf94f22017-12-28 23:46:26 -05001409 many0!(do_parse!(
1410 as_: keyword!(as) >>
1411 // We can't accept `A + B` in cast expressions, as it's
1412 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001413 ty: shim!(Type::without_plus) >>
David Tolnay0cf94f22017-12-28 23:46:26 -05001414 ({
1415 e = ExprCast {
1416 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001417 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001418 as_token: as_,
1419 ty: Box::new(ty),
1420 }.into();
1421 })
1422 )) >>
1423 (e)
1424 ));
1425
David Tolnaybcf26022017-12-25 22:10:52 -05001426 // <UnOp> <trailer>
1427 // & <trailer>
1428 // &mut <trailer>
1429 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001430 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001431 named2!(unary_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001432 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001433 attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001434 op: syn!(UnOp) >>
David Tolnay9389c382018-08-27 09:13:37 -07001435 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001436 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001437 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001438 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001439 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001440 }.into())
1441 )
1442 |
1443 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001444 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001445 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001446 mutability: option!(keyword!(mut)) >>
David Tolnay9389c382018-08-27 09:13:37 -07001447 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001448 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001449 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001450 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001451 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001452 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001453 }.into())
1454 )
1455 |
1456 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001457 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001458 box_: keyword!(box) >>
David Tolnay9389c382018-08-27 09:13:37 -07001459 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001460 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001461 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001462 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001463 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001464 }.into())
1465 )
1466 |
David Tolnay9389c382018-08-27 09:13:37 -07001467 shim!(trailer_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001468 ));
1469
Michael Layzell734adb42017-06-07 16:58:31 -04001470 // XXX: This duplication is ugly
1471 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001472 named2!(unary_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001473 do_parse!(
1474 op: syn!(UnOp) >>
David Tolnay9389c382018-08-27 09:13:37 -07001475 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
Michael Layzell734adb42017-06-07 16:58:31 -04001476 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001477 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001478 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001479 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001480 }.into())
1481 )
1482 |
David Tolnay9389c382018-08-27 09:13:37 -07001483 shim!(trailer_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001484 ));
1485
David Tolnayd997aef2018-07-21 18:42:31 -07001486 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001487 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1488 let mut outer = Vec::new();
1489 let mut inner = Vec::new();
1490 for attr in mem::replace(attrs, Vec::new()) {
1491 match attr.style {
1492 AttrStyle::Outer => outer.push(attr),
1493 AttrStyle::Inner(_) => inner.push(attr),
1494 }
1495 }
1496 *attrs = inner;
1497 outer
1498 }
1499
David Tolnaybcf26022017-12-25 22:10:52 -05001500 // <atom> (..<args>) ...
1501 // <atom> . <ident> (..<args>) ...
1502 // <atom> . <ident> ...
1503 // <atom> . <lit> ...
1504 // <atom> [ <expr> ] ...
1505 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001506 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001507 named2!(trailer_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1508 mut e: shim!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001509 outer_attrs: value!({
1510 let mut attrs = e.replace_attrs(Vec::new());
1511 let outer_attrs = take_outer(&mut attrs);
1512 e.replace_attrs(attrs);
1513 outer_attrs
1514 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001515 many0!(alt!(
David Tolnay9389c382018-08-27 09:13:37 -07001516 tap!(args: shim!(and_call) => {
David Tolnay8875fca2017-12-31 13:52:37 -05001517 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001518 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001519 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001520 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001521 args: args,
1522 paren_token: paren,
1523 }.into();
1524 })
1525 |
David Tolnay9389c382018-08-27 09:13:37 -07001526 tap!(more: shim!(and_method_call) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001527 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001528 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001529 e = call.into();
1530 })
1531 |
David Tolnay9389c382018-08-27 09:13:37 -07001532 tap!(field: shim!(and_field) => {
David Tolnay85b69a42017-12-27 20:43:10 -05001533 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001534 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001535 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001536 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001537 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001538 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001539 }.into();
1540 })
1541 |
David Tolnay9389c382018-08-27 09:13:37 -07001542 tap!(i: shim!(and_index) => {
David Tolnay8875fca2017-12-31 13:52:37 -05001543 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001544 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001545 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001546 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001547 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001548 index: Box::new(i),
1549 }.into();
1550 })
1551 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001552 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001553 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001554 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001555 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001556 question_token: question,
1557 }.into();
1558 })
1559 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001560 ({
1561 let mut attrs = outer_attrs;
1562 attrs.extend(e.replace_attrs(Vec::new()));
1563 e.replace_attrs(attrs);
1564 e
1565 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001566 ));
1567
Michael Layzell734adb42017-06-07 16:58:31 -04001568 // XXX: Duplication == ugly
1569 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001570 named2!(trailer_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1571 mut e: shim!(atom_expr, allow_struct, allow_block) >>
Michael Layzell734adb42017-06-07 16:58:31 -04001572 many0!(alt!(
David Tolnay9389c382018-08-27 09:13:37 -07001573 tap!(args: shim!(and_call) => {
Michael Layzell734adb42017-06-07 16:58:31 -04001574 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001575 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001576 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001577 paren_token: args.0,
1578 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001579 }.into();
1580 })
1581 |
David Tolnay9389c382018-08-27 09:13:37 -07001582 tap!(field: shim!(and_field) => {
David Tolnayd5147742018-06-30 10:09:52 -07001583 let (token, member) = field;
1584 e = ExprField {
1585 attrs: Vec::new(),
1586 base: Box::new(e),
1587 dot_token: token,
1588 member: member,
1589 }.into();
1590 })
1591 |
David Tolnay9389c382018-08-27 09:13:37 -07001592 tap!(i: shim!(and_index) => {
Michael Layzell734adb42017-06-07 16:58:31 -04001593 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001594 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001595 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001596 bracket_token: i.0,
1597 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001598 }.into();
1599 })
1600 )) >>
1601 (e)
1602 ));
1603
David Tolnaya454c8f2018-01-07 01:01:10 -08001604 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001605 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001606 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001607 named2!(atom_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001608 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001609 |
David Tolnay8c91b882017-12-28 23:04:32 -05001610 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001611 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001612 // must be before ExprStruct
David Tolnay02a9c6f2018-08-24 18:58:45 -04001613 syn!(ExprAsync) => { Expr::Async }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001614 |
David Tolnayf7177052018-08-24 15:31:50 -04001615 // must be before ExprStruct
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001616 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnayf7177052018-08-24 15:31:50 -04001617 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001618 // must be before expr_path
David Tolnay9389c382018-08-27 09:13:37 -07001619 cond_reduce!(allow_struct.0, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001620 |
David Tolnay8c91b882017-12-28 23:04:32 -05001621 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001622 |
David Tolnay8c91b882017-12-28 23:04:32 -05001623 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001624 |
David Tolnay9389c382018-08-27 09:13:37 -07001625 shim!(expr_break, allow_struct) // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001626 |
David Tolnay8c91b882017-12-28 23:04:32 -05001627 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001628 |
David Tolnay9389c382018-08-27 09:13:37 -07001629 shim!(expr_ret, allow_struct) // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001630 |
David Tolnay8c91b882017-12-28 23:04:32 -05001631 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001632 |
David Tolnay8c91b882017-12-28 23:04:32 -05001633 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001634 |
David Tolnay8c91b882017-12-28 23:04:32 -05001635 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001636 |
David Tolnay8c91b882017-12-28 23:04:32 -05001637 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001638 |
David Tolnay8c91b882017-12-28 23:04:32 -05001639 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001640 |
David Tolnay8c91b882017-12-28 23:04:32 -05001641 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001642 |
David Tolnay8c91b882017-12-28 23:04:32 -05001643 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001644 |
David Tolnay8c91b882017-12-28 23:04:32 -05001645 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001646 |
David Tolnay8c91b882017-12-28 23:04:32 -05001647 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001648 |
David Tolnay8c91b882017-12-28 23:04:32 -05001649 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001650 |
David Tolnay8c91b882017-12-28 23:04:32 -05001651 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001652 |
David Tolnay9389c382018-08-27 09:13:37 -07001653 shim!(expr_closure, allow_struct)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001654 |
David Tolnay9389c382018-08-27 09:13:37 -07001655 cond_reduce!(allow_block.0, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001656 |
1657 // NOTE: This is the prefix-form of range
David Tolnay9389c382018-08-27 09:13:37 -07001658 shim!(expr_range, allow_struct)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001659 |
David Tolnay8c91b882017-12-28 23:04:32 -05001660 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001661 |
David Tolnay8c91b882017-12-28 23:04:32 -05001662 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001663 ));
1664
Michael Layzell734adb42017-06-07 16:58:31 -04001665 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001666 named2!(atom_expr(_allow_struct: AllowStruct, _allow_block: AllowBlock) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001667 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001668 |
David Tolnay9374bc02018-01-27 18:49:36 -08001669 syn!(ExprParen) => { Expr::Paren }
1670 |
David Tolnay8c91b882017-12-28 23:04:32 -05001671 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001672 ));
1673
Michael Layzell734adb42017-06-07 16:58:31 -04001674 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001675 named2!(expr_nosemi -> Expr, do_parse!(
David Tolnay313a36f2018-04-29 20:13:04 -07001676 nosemi: alt!(
1677 syn!(ExprIf) => { Expr::If }
1678 |
1679 syn!(ExprIfLet) => { Expr::IfLet }
1680 |
1681 syn!(ExprWhile) => { Expr::While }
1682 |
1683 syn!(ExprWhileLet) => { Expr::WhileLet }
1684 |
1685 syn!(ExprForLoop) => { Expr::ForLoop }
1686 |
1687 syn!(ExprLoop) => { Expr::Loop }
1688 |
1689 syn!(ExprMatch) => { Expr::Match }
1690 |
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001691 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnay313a36f2018-04-29 20:13:04 -07001692 |
1693 syn!(ExprYield) => { Expr::Yield }
1694 |
1695 syn!(ExprUnsafe) => { Expr::Unsafe }
1696 |
1697 syn!(ExprBlock) => { Expr::Block }
1698 ) >>
1699 // If the next token is a `.` or a `?` it is special-cased to parse
1700 // as an expression instead of a blockexpression.
1701 not!(punct!(.)) >>
1702 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001703 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001704 ));
Michael Layzell35418782017-06-07 09:20:25 -04001705
David Tolnay8c91b882017-12-28 23:04:32 -05001706 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001707 #[cfg(not(feature = "full"))]
1708 named!(parse -> Self, do_parse!(
1709 lit: syn!(Lit) >>
1710 (ExprLit {
1711 attrs: Vec::new(),
1712 lit: lit,
1713 })
1714 ));
1715
1716 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001717 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001718 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001719 lit: syn!(Lit) >>
1720 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001721 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001722 lit: lit,
1723 })
1724 ));
1725 }
1726
1727 #[cfg(feature = "full")]
1728 impl Synom for ExprMacro {
1729 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001730 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001731 mac: syn!(Macro) >>
1732 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001733 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001734 mac: mac,
1735 })
1736 ));
1737 }
1738
David Tolnaye98775f2017-12-28 23:17:00 -05001739 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001740 impl Synom for ExprGroup {
1741 named!(parse -> Self, do_parse!(
David Tolnaya7d69fc2018-08-26 13:30:24 -04001742 e: old_grouped!(syn!(Expr)) >>
Michael Layzell93c36282017-06-04 20:43:14 -04001743 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001744 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001745 expr: Box::new(e.1),
1746 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001747 })
Michael Layzell93c36282017-06-04 20:43:14 -04001748 ));
1749 }
1750
Alex Crichton954046c2017-05-30 21:49:42 -07001751 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001752 #[cfg(not(feature = "full"))]
1753 named!(parse -> Self, do_parse!(
1754 e: parens!(syn!(Expr)) >>
1755 (ExprParen {
1756 attrs: Vec::new(),
1757 paren_token: e.0,
1758 expr: Box::new(e.1),
1759 })
1760 ));
1761
1762 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001763 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001764 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001765 e: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001766 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001767 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001768 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001769 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001770 attrs: {
1771 let mut attrs = outer_attrs;
1772 attrs.extend((e.1).0);
1773 attrs
1774 },
David Tolnay8875fca2017-12-31 13:52:37 -05001775 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001776 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001777 })
Michael Layzell92639a52017-06-01 00:07:44 -04001778 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001779 }
David Tolnay89e05672016-10-02 14:39:42 -07001780
Michael Layzell734adb42017-06-07 16:58:31 -04001781 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001782 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001783 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001784 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001785 elems: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001786 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001787 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001788 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001789 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001790 attrs: {
1791 let mut attrs = outer_attrs;
1792 attrs.extend((elems.1).0);
1793 attrs
1794 },
David Tolnay8875fca2017-12-31 13:52:37 -05001795 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001796 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001797 })
1798 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001799 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001800
David Tolnay9389c382018-08-27 09:13:37 -07001801 named2!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001802 parens!(Punctuated::parse_terminated)
1803 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001804
Michael Layzell734adb42017-06-07 16:58:31 -04001805 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001806 named2!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001807 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001808 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001809 turbofish: option!(tuple!(
1810 punct!(::),
1811 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001812 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001813 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001814 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001815 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001816 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001817 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001818 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001819 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001820 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001821 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001822 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001823
Alex Crichton954046c2017-05-30 21:49:42 -07001824 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001825 turbofish: turbofish.map(|fish| MethodTurbofish {
1826 colon2_token: fish.0,
1827 lt_token: fish.1,
1828 args: fish.2,
1829 gt_token: fish.3,
1830 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001831 args: args.1,
1832 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001833 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001834 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001835 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001836 ));
1837
Michael Layzell734adb42017-06-07 16:58:31 -04001838 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001839 impl Synom for GenericMethodArgument {
1840 // TODO parse const generics as well
1841 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1842 }
1843
1844 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001845 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001846 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001847 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001848 elems: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001849 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001850 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001851 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001852 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001853 attrs: {
1854 let mut attrs = outer_attrs;
1855 attrs.extend((elems.1).0);
1856 attrs
1857 },
1858 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001859 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001860 })
1861 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001862 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001863
Michael Layzell734adb42017-06-07 16:58:31 -04001864 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001865 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001866 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001867 if_: keyword!(if) >>
1868 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001869 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001870 eq: punct!(=) >>
David Tolnay9389c382018-08-27 09:13:37 -07001871 cond: shim!(expr_no_struct) >>
1872 then_block: braces!(Block::old_parse_within) >>
1873 else_block: option!(shim!(else_block)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001874 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001875 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001876 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001877 let_token: let_,
1878 eq_token: eq,
1879 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001880 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001881 brace_token: then_block.0,
1882 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001883 },
1884 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001885 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001886 })
1887 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001888 }
1889
Michael Layzell734adb42017-06-07 16:58:31 -04001890 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001891 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001892 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001893 if_: keyword!(if) >>
David Tolnay9389c382018-08-27 09:13:37 -07001894 cond: shim!(expr_no_struct) >>
1895 then_block: braces!(Block::old_parse_within) >>
1896 else_block: option!(shim!(else_block)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001897 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001898 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001899 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001900 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001901 brace_token: then_block.0,
1902 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001903 },
1904 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001905 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001906 })
1907 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001908 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001909
Michael Layzell734adb42017-06-07 16:58:31 -04001910 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001911 named2!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001912 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001913 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001914 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001915 |
David Tolnay8c91b882017-12-28 23:04:32 -05001916 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001917 |
1918 do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07001919 else_block: braces!(Block::old_parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001920 (Expr::Block(ExprBlock {
1921 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04001922 label: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001923 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001924 brace_token: else_block.0,
1925 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001926 },
1927 }))
David Tolnay939766a2016-09-23 23:48:12 -07001928 )
Alex Crichton954046c2017-05-30 21:49:42 -07001929 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001930 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001931 ));
1932
Michael Layzell734adb42017-06-07 16:58:31 -04001933 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001934 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001935 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001936 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001937 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001938 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001939 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001940 in_: keyword!(in) >>
David Tolnay9389c382018-08-27 09:13:37 -07001941 expr: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001942 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001943 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07001944 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001945 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001946 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001947 attrs: {
1948 let mut attrs = outer_attrs;
1949 attrs.extend((block.1).0);
1950 attrs
1951 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001952 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001953 for_token: for_,
1954 pat: Box::new(pat),
1955 in_token: in_,
1956 expr: Box::new(expr),
1957 body: Block {
1958 brace_token: block.0,
1959 stmts: (block.1).1,
1960 },
Michael Layzell92639a52017-06-01 00:07:44 -04001961 })
1962 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001963 }
Gregory Katze5f35682016-09-27 14:20:55 -04001964
Michael Layzell734adb42017-06-07 16:58:31 -04001965 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001966 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001967 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001968 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001969 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001970 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001971 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001972 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07001973 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001974 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001975 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001976 attrs: {
1977 let mut attrs = outer_attrs;
1978 attrs.extend((block.1).0);
1979 attrs
1980 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001981 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001982 loop_token: loop_,
1983 body: Block {
1984 brace_token: block.0,
1985 stmts: (block.1).1,
1986 },
Michael Layzell92639a52017-06-01 00:07:44 -04001987 })
1988 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001989 }
1990
Michael Layzell734adb42017-06-07 16:58:31 -04001991 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001992 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001993 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001994 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001995 match_: keyword!(match) >>
David Tolnay9389c382018-08-27 09:13:37 -07001996 obj: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001997 braced_content: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001998 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001999 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07002000 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05002001 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002002 attrs: {
2003 let mut attrs = outer_attrs;
2004 attrs.extend((braced_content.1).0);
2005 attrs
2006 },
David Tolnay8875fca2017-12-31 13:52:37 -05002007 expr: Box::new(obj),
2008 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07002009 brace_token: braced_content.0,
2010 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002011 })
2012 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002013 }
David Tolnay1978c672016-10-27 22:05:52 -07002014
Michael Layzell734adb42017-06-07 16:58:31 -04002015 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002016 impl Synom for ExprTryBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002017 named!(parse -> Self, do_parse!(
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002018 try_token: keyword!(try) >>
2019 block: syn!(Block) >>
2020 (ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002021 attrs: Vec::new(),
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002022 try_token: try_token,
2023 block: block,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002024 })
Michael Layzell92639a52017-06-01 00:07:44 -04002025 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002026 }
Arnavion02ef13f2017-04-25 00:54:31 -07002027
Michael Layzell734adb42017-06-07 16:58:31 -04002028 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002029 impl Synom for ExprYield {
2030 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002031 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002032 expr: option!(syn!(Expr)) >>
2033 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002034 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002035 yield_token: yield_,
2036 expr: expr.map(Box::new),
2037 })
2038 ));
2039 }
2040
2041 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002042 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002043 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002044 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002045 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002046 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002047 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002048 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002049 body: do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002050 expr: alt!(shim!(expr_nosemi) | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002051 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2052 true => alt!(
2053 input_end!() => { |_| None }
2054 |
2055 punct!(,) => { Some }
2056 )
Alex Crichton03b30272017-08-28 09:35:24 -07002057 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002058 false => option!(punct!(,))
2059 ) >>
2060 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002061 ) >>
2062 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002063 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002064 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002065 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002066 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002067 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002068 body: Box::new(body.0),
2069 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002070 })
2071 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002072 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002073
Michael Layzell734adb42017-06-07 16:58:31 -04002074 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002075 named2!(expr_closure(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002076 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002077 asyncness: option!(keyword!(async)) >>
2078 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002079 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002080 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002081 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002082 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002083 ret_and_body: alt!(
2084 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002085 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002086 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002087 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002088 (
2089 ReturnType::Type(arrow, Box::new(ty)),
2090 Expr::Block(ExprBlock {
2091 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04002092 label: None,
David Tolnay76178be2018-07-31 23:06:15 -07002093 block: body,
2094 },
2095 ))
David Tolnay89e05672016-10-02 14:39:42 -07002096 )
2097 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002098 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002099 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002100 (Expr::Closure(ExprClosure {
2101 attrs: attrs,
2102 asyncness: asyncness,
2103 movability: movability,
2104 capture: capture,
2105 or1_token: or1,
2106 inputs: inputs,
2107 or2_token: or2,
2108 output: ret_and_body.0,
2109 body: Box::new(ret_and_body.1),
2110 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002111 ));
2112
2113 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04002114 impl Synom for ExprAsync {
2115 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002116 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay02a9c6f2018-08-24 18:58:45 -04002117 async_token: keyword!(async) >>
2118 capture: option!(keyword!(move)) >>
2119 block: syn!(Block) >>
2120 (ExprAsync {
2121 attrs: attrs,
2122 async_token: async_token,
2123 capture: capture,
2124 block: block,
2125 })
2126 ));
2127 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002128
2129 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002130 named!(fn_arg -> FnArg, do_parse!(
2131 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002132 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002133 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002134 if let Some((colon, ty)) = ty {
2135 FnArg::Captured(ArgCaptured {
2136 pat: pat,
2137 colon_token: colon,
2138 ty: ty,
2139 })
2140 } else {
2141 FnArg::Inferred(pat)
2142 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002143 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002144 ));
2145
Michael Layzell734adb42017-06-07 16:58:31 -04002146 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002147 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002148 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002149 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002150 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002151 while_: keyword!(while) >>
David Tolnay9389c382018-08-27 09:13:37 -07002152 cond: 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 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002158 attrs: {
2159 let mut attrs = outer_attrs;
2160 attrs.extend((block.1).0);
2161 attrs
2162 },
2163 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002164 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002165 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002166 body: Block {
2167 brace_token: block.0,
2168 stmts: (block.1).1,
2169 },
Michael Layzell92639a52017-06-01 00:07:44 -04002170 })
2171 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002172 }
2173
Michael Layzell734adb42017-06-07 16:58:31 -04002174 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002175 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002176 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002177 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002178 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002179 while_: keyword!(while) >>
2180 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002181 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002182 eq: punct!(=) >>
David Tolnay9389c382018-08-27 09:13:37 -07002183 value: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002184 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002185 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002186 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002187 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002188 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002189 attrs: {
2190 let mut attrs = outer_attrs;
2191 attrs.extend((block.1).0);
2192 attrs
2193 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002194 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002195 while_token: while_,
2196 let_token: let_,
2197 pats: pats,
2198 eq_token: eq,
2199 expr: Box::new(value),
2200 body: Block {
2201 brace_token: block.0,
2202 stmts: (block.1).1,
2203 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002204 })
2205 ));
2206 }
2207
2208 #[cfg(feature = "full")]
2209 impl Synom for Label {
2210 named!(parse -> Self, do_parse!(
2211 name: syn!(Lifetime) >>
2212 colon: punct!(:) >>
2213 (Label {
2214 name: name,
2215 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002216 })
2217 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002218 }
2219
Michael Layzell734adb42017-06-07 16:58:31 -04002220 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002221 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002222 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002223 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002224 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002225 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002226 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002227 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002228 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002229 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002230 })
2231 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002232 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002233
Michael Layzell734adb42017-06-07 16:58:31 -04002234 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002235 named2!(expr_break(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002236 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002237 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002238 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002239 // We can't allow blocks after a `break` expression when we wouldn't
2240 // allow structs, as this expression is ambiguous.
2241 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002242 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002243 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002244 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002245 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002246 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002247 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002248 ));
2249
Michael Layzell734adb42017-06-07 16:58:31 -04002250 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002251 named2!(expr_ret(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002252 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002253 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002254 // NOTE: return is greedy and eats blocks after it even when in a
2255 // position where structs are not allowed, such as in if statement
2256 // conditions. For example:
2257 //
David Tolnaybcf26022017-12-25 22:10:52 -05002258 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002259 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002260 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002261 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002262 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002263 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002264 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002265 ));
2266
Michael Layzell734adb42017-06-07 16:58:31 -04002267 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002268 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002269 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002270 outer_attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002271 path: syn!(Path) >>
2272 data: braces!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002273 inner_attrs: many0!(Attribute::old_parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002274 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002275 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2276 dots: punct!(..) >>
2277 base: syn!(Expr) >>
2278 (dots, base)
2279 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002280 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002281 )) >>
2282 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002283 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002284 let (dots, rest) = match base.and_then(|b| b) {
2285 Some((dots, base)) => (Some(dots), Some(base)),
2286 None => (None, None),
2287 };
2288 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002289 attrs: {
2290 let mut attrs = outer_attrs;
2291 attrs.extend(inner_attrs);
2292 attrs
2293 },
Michael Layzell92639a52017-06-01 00:07:44 -04002294 brace_token: brace,
2295 path: path,
2296 fields: fields,
2297 dot2_token: dots,
2298 rest: rest.map(Box::new),
2299 }
2300 })
2301 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002302 }
2303
Michael Layzell734adb42017-06-07 16:58:31 -04002304 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002305 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002306 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002307 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayc42b90a2018-01-18 23:11:37 -08002308 field_value: alt!(
2309 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2310 |
2311 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002312 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002313 None,
2314 Expr::Path(ExprPath {
2315 attrs: Vec::new(),
2316 qself: None,
2317 path: name.into(),
2318 }),
2319 ))
2320 ) >>
2321 (FieldValue {
2322 attrs: attrs,
2323 member: field_value.0,
2324 colon_token: field_value.1,
2325 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002326 })
2327 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002328 }
David Tolnay055a7042016-10-02 19:23:54 -07002329
Michael Layzell734adb42017-06-07 16:58:31 -04002330 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002331 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002332 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002333 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002334 data: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002335 many0!(Attribute::old_parse_inner),
David Tolnay5d314dc2018-07-21 16:40:01 -07002336 syn!(Expr),
2337 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002338 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002339 )) >>
2340 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002341 attrs: {
2342 let mut attrs = outer_attrs;
2343 attrs.extend((data.1).0);
2344 attrs
2345 },
2346 expr: Box::new((data.1).1),
2347 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002348 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002349 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002350 })
2351 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002352 }
David Tolnay055a7042016-10-02 19:23:54 -07002353
Michael Layzell734adb42017-06-07 16:58:31 -04002354 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002355 impl Synom for ExprUnsafe {
2356 named!(parse -> Self, do_parse!(
David Tolnay8b493772018-08-27 06:30:18 -07002357 outer_attrs: many0!(Attribute::old_parse_outer) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002358 unsafe_: keyword!(unsafe) >>
David Tolnayc4be3512018-08-27 06:25:44 -07002359 block: braces!(tuple!(
David Tolnay8b493772018-08-27 06:30:18 -07002360 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002361 shim!(Block::parse_within),
David Tolnayc4be3512018-08-27 06:25:44 -07002362 )) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002363 (ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002364 attrs: {
2365 let mut attrs = outer_attrs;
2366 attrs.extend((block.1).0);
2367 attrs
2368 },
Nika Layzell640832a2017-12-04 13:37:09 -05002369 unsafe_token: unsafe_,
David Tolnayc4be3512018-08-27 06:25:44 -07002370 block: Block {
2371 brace_token: block.0,
2372 stmts: (block.1).1,
2373 },
Nika Layzell640832a2017-12-04 13:37:09 -05002374 })
2375 ));
2376 }
2377
2378 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002379 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002380 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002381 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay1d8e9962018-08-24 19:04:20 -04002382 label: option!(syn!(Label)) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002383 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002384 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002385 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002386 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002387 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002388 attrs: {
2389 let mut attrs = outer_attrs;
2390 attrs.extend((block.1).0);
2391 attrs
2392 },
David Tolnay1d8e9962018-08-24 19:04:20 -04002393 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002394 block: Block {
2395 brace_token: block.0,
2396 stmts: (block.1).1,
2397 },
Michael Layzell92639a52017-06-01 00:07:44 -04002398 })
2399 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002400 }
David Tolnay89e05672016-10-02 14:39:42 -07002401
Michael Layzell734adb42017-06-07 16:58:31 -04002402 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002403 named2!(expr_range(allow_struct: AllowStruct) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002404 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002405 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002406 (ExprRange {
2407 attrs: Vec::new(),
2408 from: None,
2409 to: hi.map(Box::new),
2410 limits: limits,
2411 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002412 ));
2413
Michael Layzell734adb42017-06-07 16:58:31 -04002414 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002415 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002416 named!(parse -> Self, alt!(
2417 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002418 punct!(..=) => { RangeLimits::Closed }
2419 |
2420 // Must come before Dot2
David Tolnay7ac699c2018-08-24 14:00:58 -04002421 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.spans)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002422 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002423 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002424 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002425 }
David Tolnay438c9052016-10-07 23:24:48 -07002426
Alex Crichton954046c2017-05-30 21:49:42 -07002427 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002428 #[cfg(not(feature = "full"))]
2429 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002430 pair: shim!(qpath) >>
David Tolnayeb981bb2018-07-21 19:31:38 -07002431 (ExprPath {
2432 attrs: Vec::new(),
2433 qself: pair.0,
2434 path: pair.1,
2435 })
2436 ));
2437
2438 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002439 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002440 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay9389c382018-08-27 09:13:37 -07002441 pair: shim!(qpath) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002442 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002443 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002444 qself: pair.0,
2445 path: pair.1,
2446 })
2447 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002448 }
David Tolnay42602292016-10-01 22:25:45 -07002449
David Tolnay9389c382018-08-27 09:13:37 -07002450 named2!(path -> Path, do_parse!(
David Tolnay9cc2f092018-08-24 15:51:37 -04002451 colon: option!(punct!(::)) >>
2452 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2453 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2454 (Path {
2455 leading_colon: colon,
2456 segments: segments,
2457 })
2458 ));
2459
2460 named!(path_segment -> PathSegment, alt!(
2461 do_parse!(
2462 ident: syn!(Ident) >>
2463 colon2: punct!(::) >>
2464 lt: punct!(<) >>
2465 args: call!(Punctuated::parse_terminated) >>
2466 gt: punct!(>) >>
2467 (PathSegment {
2468 ident: ident,
2469 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2470 colon2_token: Some(colon2),
2471 lt_token: lt,
2472 args: args,
2473 gt_token: gt,
2474 }),
2475 })
2476 )
2477 |
David Tolnaya7d69fc2018-08-26 13:30:24 -04002478 old_mod_style_path_segment
David Tolnay9cc2f092018-08-24 15:51:37 -04002479 ));
2480
David Tolnay9389c382018-08-27 09:13:37 -07002481 named2!(qpath -> (Option<QSelf>, Path), alt!(
2482 map!(shim!(path), |p| (None, p))
David Tolnay9cc2f092018-08-24 15:51:37 -04002483 |
2484 do_parse!(
2485 lt: punct!(<) >>
2486 this: syn!(Type) >>
2487 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2488 gt: punct!(>) >>
2489 colon2: punct!(::) >>
2490 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2491 ({
2492 let (pos, as_, path) = match path {
2493 Some((as_, mut path)) => {
2494 let pos = path.segments.len();
2495 path.segments.push_punct(colon2);
2496 path.segments.extend(rest.into_pairs());
2497 (pos, Some(as_), path)
2498 }
2499 None => {
2500 (0, None, Path {
2501 leading_colon: Some(colon2),
2502 segments: rest,
2503 })
2504 }
2505 };
2506 (Some(QSelf {
2507 lt_token: lt,
2508 ty: Box::new(this),
2509 position: pos,
2510 as_token: as_,
2511 gt_token: gt,
2512 }), path)
2513 })
2514 )
2515 |
2516 map!(keyword!(self), |s| (None, s.into()))
2517 ));
2518
David Tolnay9389c382018-08-27 09:13:37 -07002519 named2!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002520
David Tolnay9389c382018-08-27 09:13:37 -07002521 named2!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002522
Michael Layzell734adb42017-06-07 16:58:31 -04002523 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002524 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002525 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002526 stmts: braces!(Block::old_parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002527 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002528 brace_token: stmts.0,
2529 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002530 })
2531 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002532 }
David Tolnay939766a2016-09-23 23:48:12 -07002533
Michael Layzell734adb42017-06-07 16:58:31 -04002534 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002535 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002536 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
2537 input.step_cursor(|cursor| Self::old_parse_within(*cursor))
2538 }
2539
2540 named!(old_parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002541 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002542 mut standalone: many0!(do_parse!(
2543 stmt: syn!(Stmt) >>
2544 many0!(punct!(;)) >>
2545 (stmt)
2546 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002547 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002548 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002549 mut e: syn!(Expr) >>
2550 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002551 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002552 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002553 })
2554 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002555 (match last {
2556 None => standalone,
2557 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002558 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002559 standalone
2560 }
2561 })
2562 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002563 }
2564
Michael Layzell734adb42017-06-07 16:58:31 -04002565 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002566 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002567 named!(parse -> Self, alt!(
David Tolnay9389c382018-08-27 09:13:37 -07002568 shim!(stmt_mac)
Michael Layzell92639a52017-06-01 00:07:44 -04002569 |
David Tolnay9389c382018-08-27 09:13:37 -07002570 shim!(stmt_local)
Michael Layzell92639a52017-06-01 00:07:44 -04002571 |
David Tolnay9389c382018-08-27 09:13:37 -07002572 shim!(stmt_item)
Michael Layzell92639a52017-06-01 00:07:44 -04002573 |
David Tolnay9389c382018-08-27 09:13:37 -07002574 shim!(stmt_blockexpr)
Michael Layzell35418782017-06-07 09:20:25 -04002575 |
David Tolnay9389c382018-08-27 09:13:37 -07002576 shim!(stmt_expr)
Michael Layzell92639a52017-06-01 00:07:44 -04002577 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002578 }
David Tolnay939766a2016-09-23 23:48:12 -07002579
Michael Layzell734adb42017-06-07 16:58:31 -04002580 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002581 named2!(stmt_mac -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002582 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaya7d69fc2018-08-26 13:30:24 -04002583 what: call!(Path::old_parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002584 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002585 // Only parse braces here; paren and bracket will get parsed as
2586 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002587 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002588 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002589 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002590 attrs: attrs,
2591 ident: None,
2592 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002593 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002594 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002595 delimiter: MacroDelimiter::Brace(data.0),
2596 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002597 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002598 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002599 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002600 ));
2601
Michael Layzell734adb42017-06-07 16:58:31 -04002602 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002603 named2!(stmt_local -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002604 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002605 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002606 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002607 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002608 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2609 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002610 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002611 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002612 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002613 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002614 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2615 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2616 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002617 }))
David Tolnay191e0582016-10-02 18:31:09 -07002618 ));
2619
Michael Layzell734adb42017-06-07 16:58:31 -04002620 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002621 named2!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002622
Michael Layzell734adb42017-06-07 16:58:31 -04002623 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002624 named2!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002625 mut attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay9389c382018-08-27 09:13:37 -07002626 mut e: shim!(expr_nosemi) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002627 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002628 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002629 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002630 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002631 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002632 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002633 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002634 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002635 }
2636 })
2637 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002638
Michael Layzell734adb42017-06-07 16:58:31 -04002639 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002640 named2!(stmt_expr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002641 mut attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002642 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002643 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002644 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002645 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002646 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002647 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002648 })
David Tolnay939766a2016-09-23 23:48:12 -07002649 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002650
Michael Layzell734adb42017-06-07 16:58:31 -04002651 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002652 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002653 named!(parse -> Self, alt!(
2654 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2655 |
2656 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2657 |
2658 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2659 |
2660 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2661 |
2662 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2663 |
David Tolnay323279a2017-12-29 11:26:32 -05002664 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002665 |
2666 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2667 |
2668 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2669 |
2670 syn!(PatPath) => { Pat::Path }
2671 |
2672 syn!(PatTuple) => { Pat::Tuple }
2673 |
2674 syn!(PatRef) => { Pat::Ref }
2675 |
2676 syn!(PatSlice) => { Pat::Slice }
2677 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002678 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002679
Michael Layzell734adb42017-06-07 16:58:31 -04002680 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002681 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002682 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002683 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002684 |u| PatWild { underscore_token: u }
2685 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002686 }
David Tolnay84aa0752016-10-02 23:01:13 -07002687
Michael Layzell734adb42017-06-07 16:58:31 -04002688 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002689 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002690 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002691 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002692 pat: syn!(Pat) >>
2693 (PatBox {
2694 pat: Box::new(pat),
2695 box_token: boxed,
2696 })
2697 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002698 }
2699
Michael Layzell734adb42017-06-07 16:58:31 -04002700 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002701 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002702 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002703 by_ref: option!(keyword!(ref)) >>
2704 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002705 name: alt!(
2706 syn!(Ident)
2707 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002708 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002709 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002710 not!(punct!(<)) >>
2711 not!(punct!(::)) >>
2712 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002713 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002714 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002715 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002716 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002717 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002718 })
2719 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002720 }
2721
Michael Layzell734adb42017-06-07 16:58:31 -04002722 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002723 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002724 named!(parse -> Self, do_parse!(
2725 path: syn!(Path) >>
2726 tuple: syn!(PatTuple) >>
2727 (PatTupleStruct {
2728 path: path,
2729 pat: tuple,
2730 })
2731 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002732 }
2733
Michael Layzell734adb42017-06-07 16:58:31 -04002734 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002735 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002736 named!(parse -> Self, do_parse!(
2737 path: syn!(Path) >>
2738 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002739 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002740 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002741 (fields, base)
2742 )) >>
2743 (PatStruct {
2744 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002745 fields: (data.1).0,
2746 brace_token: data.0,
2747 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002748 })
2749 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002750 }
2751
Michael Layzell734adb42017-06-07 16:58:31 -04002752 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002753 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002754 named!(parse -> Self, alt!(
2755 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002756 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002757 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002758 pat: syn!(Pat) >>
2759 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002760 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002761 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002762 attrs: Vec::new(),
2763 colon_token: Some(colon),
2764 })
2765 )
2766 |
2767 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002768 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002769 by_ref: option!(keyword!(ref)) >>
2770 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002771 ident: syn!(Ident) >>
2772 ({
2773 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002774 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002775 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002776 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002777 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002778 }.into();
2779 if let Some(boxed) = boxed {
2780 pat = PatBox {
2781 pat: Box::new(pat),
2782 box_token: boxed,
2783 }.into();
2784 }
2785 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002786 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002787 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002788 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002789 colon_token: None,
2790 }
2791 })
2792 )
2793 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002794 }
2795
David Tolnay85b69a42017-12-27 20:43:10 -05002796 impl Synom for Member {
2797 named!(parse -> Self, alt!(
2798 syn!(Ident) => { Member::Named }
2799 |
2800 syn!(Index) => { Member::Unnamed }
2801 ));
2802 }
2803
David Tolnay85b69a42017-12-27 20:43:10 -05002804 impl Synom for Index {
2805 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002806 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002807 ({
David Tolnay360efd22018-01-04 23:35:26 -08002808 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002809 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002810 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002811 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002812 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002813 })
David Tolnay85b69a42017-12-27 20:43:10 -05002814 ));
2815 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002816
Michael Layzell734adb42017-06-07 16:58:31 -04002817 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002818 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002819 named!(parse -> Self, map!(
2820 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002821 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002822 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002823 }
David Tolnay9636c052016-10-02 17:11:17 -07002824
Michael Layzell734adb42017-06-07 16:58:31 -04002825 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002826 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002827 named!(parse -> Self, do_parse!(
2828 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002829 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002830 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2831 tuple!(punct!(..), option!(punct!(,)))
2832 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002833 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002834 Some((_, Some(_))) => true,
2835 _ => false,
2836 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002837 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002838 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002839 )) >>
2840 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002841 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002842 let (dotdot, trailing) = match dotdot {
2843 Some((a, b)) => (Some(a), Some(b)),
2844 None => (None, None),
2845 };
2846 PatTuple {
2847 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002848 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002849 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002850 comma_token: trailing.unwrap_or_default(),
2851 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002852 }
2853 })
2854 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002855 }
David Tolnayfbb73232016-10-03 01:00:06 -07002856
Michael Layzell734adb42017-06-07 16:58:31 -04002857 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002858 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002859 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002860 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002861 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002862 pat: syn!(Pat) >>
2863 (PatRef {
2864 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002865 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002866 and_token: and,
2867 })
2868 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002869 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002870
Michael Layzell734adb42017-06-07 16:58:31 -04002871 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002872 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002873 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002874 lit: shim!(pat_lit_expr) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002875 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002876 return parse_error(); // these need to be parsed by pat_path
2877 } else {
2878 PatLit {
2879 expr: Box::new(lit),
2880 }
2881 })
2882 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002883 }
David Tolnaye1310902016-10-29 23:40:00 -07002884
Michael Layzell734adb42017-06-07 16:58:31 -04002885 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002886 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002887 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002888 lo: shim!(pat_lit_expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002889 limits: syn!(RangeLimits) >>
David Tolnay9389c382018-08-27 09:13:37 -07002890 hi: shim!(pat_lit_expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002891 (PatRange {
2892 lo: Box::new(lo),
2893 hi: Box::new(hi),
2894 limits: limits,
2895 })
2896 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002897 }
David Tolnaye1310902016-10-29 23:40:00 -07002898
Michael Layzell734adb42017-06-07 16:58:31 -04002899 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002900 named2!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002901 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002902 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002903 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002904 |
David Tolnay8c91b882017-12-28 23:04:32 -05002905 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002906 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002907 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002908 Expr::Unary(ExprUnary {
2909 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002910 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002911 expr: Box::new(v)
2912 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002913 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002914 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002915 })
2916 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002917
Michael Layzell734adb42017-06-07 16:58:31 -04002918 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002919 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002920 named!(parse -> Self, map!(
2921 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002922 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002923 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002924 dots: punct!(..) >>
2925 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002926 (dots, trailing)
2927 )) >>
2928 after: cond!(
2929 match middle {
2930 Some((_, ref trailing)) => trailing.is_some(),
2931 _ => false,
2932 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002933 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002934 ) >>
2935 (before, middle, after)
2936 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002937 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002938 let mut before: Punctuated<Pat, Token![,]> = before;
2939 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002940 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002941 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04002942 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04002943 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04002944 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04002945 }),
2946 bracket_token: brackets,
2947 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002948 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002949 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002950 } else {
David Tolnay56080682018-01-06 14:01:52 -08002951 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002952 }
2953 }),
2954 front: before,
2955 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002956 }
Alex Crichton954046c2017-05-30 21:49:42 -07002957 }
Michael Layzell92639a52017-06-01 00:07:44 -04002958 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002959 }
David Tolnay323279a2017-12-29 11:26:32 -05002960
2961 #[cfg(feature = "full")]
2962 impl Synom for PatMacro {
2963 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
2964 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002965}
2966
David Tolnayf4bbbd92016-09-23 14:41:55 -07002967#[cfg(feature = "printing")]
2968mod printing {
2969 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002970 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002971 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07002972 use proc_macro2::{Literal, TokenStream};
2973 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002974
David Tolnaybcf26022017-12-25 22:10:52 -05002975 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07002976 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002977 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07002978 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002979 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002980 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002981 e.to_tokens(tokens);
2982 });
2983 } else {
2984 e.to_tokens(tokens);
2985 }
2986 }
2987
David Tolnay8c91b882017-12-28 23:04:32 -05002988 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07002989 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05002990 tokens.append_all(attrs.outer());
2991 }
Michael Layzell734adb42017-06-07 16:58:31 -04002992
David Tolnayd997aef2018-07-21 18:42:31 -07002993 #[cfg(feature = "full")]
2994 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
2995 tokens.append_all(attrs.inner());
2996 }
2997
David Tolnay8c91b882017-12-28 23:04:32 -05002998 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07002999 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3000
3001 #[cfg(not(feature = "full"))]
3002 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003003
Michael Layzell734adb42017-06-07 16:58:31 -04003004 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003005 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003006 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003007 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003008 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003009 self.expr.to_tokens(tokens);
3010 }
3011 }
3012
Michael Layzell734adb42017-06-07 16:58:31 -04003013 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003014 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003015 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003016 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003017 self.place.to_tokens(tokens);
3018 self.arrow_token.to_tokens(tokens);
3019 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003020 }
3021 }
3022
Michael Layzell734adb42017-06-07 16:58:31 -04003023 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003024 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003025 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003026 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003027 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003028 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003029 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003030 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003031 }
3032 }
3033
3034 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003035 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003036 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003037 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003038 self.paren_token.surround(tokens, |tokens| {
3039 self.args.to_tokens(tokens);
3040 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 }
3042 }
3043
Michael Layzell734adb42017-06-07 16:58:31 -04003044 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003045 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003046 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003047 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003048 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003049 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003050 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003051 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003052 self.paren_token.surround(tokens, |tokens| {
3053 self.args.to_tokens(tokens);
3054 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003055 }
3056 }
3057
Michael Layzell734adb42017-06-07 16:58:31 -04003058 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003059 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003060 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003061 self.colon2_token.to_tokens(tokens);
3062 self.lt_token.to_tokens(tokens);
3063 self.args.to_tokens(tokens);
3064 self.gt_token.to_tokens(tokens);
3065 }
3066 }
3067
3068 #[cfg(feature = "full")]
3069 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003070 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003071 match *self {
3072 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3073 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3074 }
3075 }
3076 }
3077
3078 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003079 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003080 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003081 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003082 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003083 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003084 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003085 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003086 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003087 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003088 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003089 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003090 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003091 }
3092 }
3093
3094 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003095 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003096 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003097 self.left.to_tokens(tokens);
3098 self.op.to_tokens(tokens);
3099 self.right.to_tokens(tokens);
3100 }
3101 }
3102
3103 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003104 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003105 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003106 self.op.to_tokens(tokens);
3107 self.expr.to_tokens(tokens);
3108 }
3109 }
3110
David Tolnay8c91b882017-12-28 23:04:32 -05003111 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003112 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003113 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003114 self.lit.to_tokens(tokens);
3115 }
3116 }
3117
Alex Crichton62a0a592017-05-22 13:58:53 -07003118 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003119 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003120 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003121 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003122 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003123 self.ty.to_tokens(tokens);
3124 }
3125 }
3126
David Tolnay0cf94f22017-12-28 23:46:26 -05003127 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003128 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003129 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003130 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003131 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003132 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003133 self.ty.to_tokens(tokens);
3134 }
3135 }
3136
Michael Layzell734adb42017-06-07 16:58:31 -04003137 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003138 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003139 if let Some((ref else_token, ref else_)) = *else_ {
3140 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003141
3142 // If we are not one of the valid expressions to exist in an else
3143 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003144 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003145 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003146 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003147 }
3148 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003149 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003150 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003151 });
3152 }
3153 }
3154 }
3155 }
3156
3157 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003158 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003159 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003160 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003162 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003163 self.then_branch.to_tokens(tokens);
3164 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003165 }
3166 }
3167
Michael Layzell734adb42017-06-07 16:58:31 -04003168 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003169 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003170 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003171 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003172 self.if_token.to_tokens(tokens);
3173 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003174 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003175 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003176 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003177 self.then_branch.to_tokens(tokens);
3178 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003179 }
3180 }
3181
Michael Layzell734adb42017-06-07 16:58:31 -04003182 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003183 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003184 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003185 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003186 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003187 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003188 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003189 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003190 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003191 tokens.append_all(&self.body.stmts);
3192 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003193 }
3194 }
3195
Michael Layzell734adb42017-06-07 16:58:31 -04003196 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003197 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003198 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003199 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003200 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003201 self.while_token.to_tokens(tokens);
3202 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003203 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003204 self.eq_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 ExprForLoop {
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.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003219 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003220 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003221 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003222 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003223 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003224 tokens.append_all(&self.body.stmts);
3225 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003226 }
3227 }
3228
Michael Layzell734adb42017-06-07 16:58:31 -04003229 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003230 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003231 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003232 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003233 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003234 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003235 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003236 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003237 tokens.append_all(&self.body.stmts);
3238 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003239 }
3240 }
3241
Michael Layzell734adb42017-06-07 16:58:31 -04003242 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003243 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003244 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003245 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003246 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003247 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003248 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003249 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003250 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003251 arm.to_tokens(tokens);
3252 // Ensure that we have a comma after a non-block arm, except
3253 // for the last one.
3254 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003255 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003256 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003257 }
3258 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003259 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003260 }
3261 }
3262
Michael Layzell734adb42017-06-07 16:58:31 -04003263 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003264 impl ToTokens for ExprAsync {
3265 fn to_tokens(&self, tokens: &mut TokenStream) {
3266 outer_attrs_to_tokens(&self.attrs, tokens);
3267 self.async_token.to_tokens(tokens);
3268 self.capture.to_tokens(tokens);
3269 self.block.to_tokens(tokens);
3270 }
3271 }
3272
3273 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003274 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003275 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003276 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003277 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003278 self.block.to_tokens(tokens);
3279 }
3280 }
3281
Michael Layzell734adb42017-06-07 16:58:31 -04003282 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003283 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003284 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003285 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003286 self.yield_token.to_tokens(tokens);
3287 self.expr.to_tokens(tokens);
3288 }
3289 }
3290
3291 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003292 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003293 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003294 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003295 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003296 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003297 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003298 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003299 for input in self.inputs.pairs() {
3300 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003301 FnArg::Captured(ArgCaptured {
3302 ref pat,
3303 ty: Type::Infer(_),
3304 ..
3305 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003306 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003307 }
David Tolnay56080682018-01-06 14:01:52 -08003308 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003309 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003310 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003311 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003312 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003313 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003314 self.body.to_tokens(tokens);
3315 }
3316 }
3317
Michael Layzell734adb42017-06-07 16:58:31 -04003318 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003319 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003320 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003321 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003322 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003323 self.block.brace_token.surround(tokens, |tokens| {
3324 inner_attrs_to_tokens(&self.attrs, tokens);
3325 tokens.append_all(&self.block.stmts);
3326 });
Nika Layzell640832a2017-12-04 13:37:09 -05003327 }
3328 }
3329
3330 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003331 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003332 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003333 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003334 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003335 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003336 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003337 tokens.append_all(&self.block.stmts);
3338 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003339 }
3340 }
3341
Michael Layzell734adb42017-06-07 16:58:31 -04003342 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003343 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003344 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003345 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003346 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003347 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003348 self.right.to_tokens(tokens);
3349 }
3350 }
3351
Michael Layzell734adb42017-06-07 16:58:31 -04003352 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003353 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003354 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003355 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003356 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003357 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003358 self.right.to_tokens(tokens);
3359 }
3360 }
3361
3362 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003363 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003364 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003365 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003366 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003367 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003368 }
3369 }
3370
David Tolnay85b69a42017-12-27 20:43:10 -05003371 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003372 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003373 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003374 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003375 Member::Unnamed(ref index) => index.to_tokens(tokens),
3376 }
3377 }
3378 }
3379
David Tolnay85b69a42017-12-27 20:43:10 -05003380 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003381 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003382 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3383 lit.set_span(self.span);
3384 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003385 }
3386 }
3387
3388 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003389 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003390 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003391 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003392 self.bracket_token.surround(tokens, |tokens| {
3393 self.index.to_tokens(tokens);
3394 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003395 }
3396 }
3397
Michael Layzell734adb42017-06-07 16:58:31 -04003398 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003399 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003400 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003401 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003402 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003403 match self.limits {
3404 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3405 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3406 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003407 self.to.to_tokens(tokens);
3408 }
3409 }
3410
3411 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003412 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003413 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003414 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003415 }
3416 }
3417
Michael Layzell734adb42017-06-07 16:58:31 -04003418 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003419 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003420 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003421 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003422 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003423 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003424 self.expr.to_tokens(tokens);
3425 }
3426 }
3427
Michael Layzell734adb42017-06-07 16:58:31 -04003428 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003429 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003430 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003431 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003432 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003433 self.label.to_tokens(tokens);
3434 self.expr.to_tokens(tokens);
3435 }
3436 }
3437
Michael Layzell734adb42017-06-07 16:58:31 -04003438 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003439 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003440 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003441 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003442 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003443 self.label.to_tokens(tokens);
3444 }
3445 }
3446
Michael Layzell734adb42017-06-07 16:58:31 -04003447 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003448 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003449 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003450 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003451 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003452 self.expr.to_tokens(tokens);
3453 }
3454 }
3455
Michael Layzell734adb42017-06-07 16:58:31 -04003456 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003457 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003458 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003459 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003460 self.mac.to_tokens(tokens);
3461 }
3462 }
3463
3464 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003465 impl ToTokens for ExprStruct {
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 Crichton62a0a592017-05-22 13:58:53 -07003468 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003469 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003470 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003471 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003472 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003473 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003474 self.rest.to_tokens(tokens);
3475 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003476 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003477 }
3478 }
3479
Michael Layzell734adb42017-06-07 16:58:31 -04003480 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003481 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003482 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003483 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003484 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003485 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003486 self.expr.to_tokens(tokens);
3487 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003488 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003489 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003490 }
3491 }
3492
David Tolnaye98775f2017-12-28 23:17:00 -05003493 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003494 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003495 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003496 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003497 self.group_token.surround(tokens, |tokens| {
3498 self.expr.to_tokens(tokens);
3499 });
3500 }
3501 }
3502
Alex Crichton62a0a592017-05-22 13:58:53 -07003503 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003504 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003505 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003506 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003507 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003508 self.expr.to_tokens(tokens);
3509 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003510 }
3511 }
3512
Michael Layzell734adb42017-06-07 16:58:31 -04003513 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003514 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003515 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003516 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003517 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003518 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003519 }
3520 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003521
David Tolnay2ae520a2017-12-29 11:19:50 -05003522 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003523 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003524 self.tts.to_tokens(tokens);
3525 }
3526 }
3527
Michael Layzell734adb42017-06-07 16:58:31 -04003528 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003529 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003530 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003531 self.name.to_tokens(tokens);
3532 self.colon_token.to_tokens(tokens);
3533 }
3534 }
3535
3536 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003537 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003538 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003539 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003540 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003541 if let Some(ref colon_token) = self.colon_token {
3542 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003543 self.expr.to_tokens(tokens);
3544 }
David Tolnay055a7042016-10-02 19:23:54 -07003545 }
3546 }
3547
Michael Layzell734adb42017-06-07 16:58:31 -04003548 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003549 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003550 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003551 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003552 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003553 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003554 if let Some((ref if_token, ref guard)) = self.guard {
3555 if_token.to_tokens(tokens);
3556 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003557 }
David Tolnaydfb91432018-03-31 19:19:44 +02003558 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003559 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003560 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003561 }
3562 }
3563
Michael Layzell734adb42017-06-07 16:58:31 -04003564 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003565 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003566 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003567 self.underscore_token.to_tokens(tokens);
3568 }
3569 }
3570
Michael Layzell734adb42017-06-07 16:58:31 -04003571 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003572 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003573 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003574 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003575 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003576 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003577 if let Some((ref at_token, ref subpat)) = self.subpat {
3578 at_token.to_tokens(tokens);
3579 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003580 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003581 }
3582 }
3583
Michael Layzell734adb42017-06-07 16:58:31 -04003584 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003585 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003586 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003587 self.path.to_tokens(tokens);
3588 self.brace_token.surround(tokens, |tokens| {
3589 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003590 // NOTE: We need a comma before the dot2 token if it is present.
3591 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003592 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003593 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003594 self.dot2_token.to_tokens(tokens);
3595 });
3596 }
3597 }
3598
Michael Layzell734adb42017-06-07 16:58:31 -04003599 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003600 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003601 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003602 self.path.to_tokens(tokens);
3603 self.pat.to_tokens(tokens);
3604 }
3605 }
3606
Michael Layzell734adb42017-06-07 16:58:31 -04003607 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003608 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003609 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003610 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3611 }
3612 }
3613
Michael Layzell734adb42017-06-07 16:58:31 -04003614 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003615 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003616 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003617 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003618 self.front.to_tokens(tokens);
3619 if let Some(ref dot2_token) = self.dot2_token {
3620 if !self.front.empty_or_trailing() {
3621 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003622 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003623 }
David Tolnay41871922017-12-29 01:53:45 -05003624 dot2_token.to_tokens(tokens);
3625 self.comma_token.to_tokens(tokens);
3626 if self.comma_token.is_none() && !self.back.is_empty() {
3627 // Ensure there is a comma after the .. token.
3628 <Token![,]>::default().to_tokens(tokens);
3629 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003630 }
David Tolnay41871922017-12-29 01:53:45 -05003631 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003632 });
3633 }
3634 }
3635
Michael Layzell734adb42017-06-07 16:58:31 -04003636 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003637 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003638 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003639 self.box_token.to_tokens(tokens);
3640 self.pat.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 PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003646 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003647 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003648 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003649 self.pat.to_tokens(tokens);
3650 }
3651 }
3652
Michael Layzell734adb42017-06-07 16:58:31 -04003653 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003654 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003655 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003656 self.expr.to_tokens(tokens);
3657 }
3658 }
3659
Michael Layzell734adb42017-06-07 16:58:31 -04003660 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003661 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003662 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003663 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003664 match self.limits {
3665 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003666 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003667 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003668 self.hi.to_tokens(tokens);
3669 }
3670 }
3671
Michael Layzell734adb42017-06-07 16:58:31 -04003672 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003673 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003674 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003675 // XXX: This is a mess, and it will be so easy to screw it up. How
3676 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003677 self.bracket_token.surround(tokens, |tokens| {
3678 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003679
3680 // If we need a comma before the middle or standalone .. token,
3681 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003682 if !self.front.empty_or_trailing()
3683 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003684 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003685 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003686 }
3687
3688 // If we have an identifier, we always need a .. token.
3689 if self.middle.is_some() {
3690 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003691 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003692 } else if self.dot2_token.is_some() {
3693 self.dot2_token.to_tokens(tokens);
3694 }
3695
3696 // Make sure we have a comma before the back half.
3697 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003698 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003699 self.back.to_tokens(tokens);
3700 } else {
3701 self.comma_token.to_tokens(tokens);
3702 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003703 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003704 }
3705 }
3706
Michael Layzell734adb42017-06-07 16:58:31 -04003707 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003708 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003709 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003710 self.mac.to_tokens(tokens);
3711 }
3712 }
3713
3714 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003715 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003716 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003717 self.tts.to_tokens(tokens);
3718 }
3719 }
3720
3721 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003722 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003723 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003724 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003725 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003726 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003727 }
3728 self.pat.to_tokens(tokens);
3729 }
3730 }
3731
Michael Layzell734adb42017-06-07 16:58:31 -04003732 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003733 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003734 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003735 self.brace_token.surround(tokens, |tokens| {
3736 tokens.append_all(&self.stmts);
3737 });
David Tolnay42602292016-10-01 22:25:45 -07003738 }
3739 }
3740
Michael Layzell734adb42017-06-07 16:58:31 -04003741 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003742 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003743 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003744 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003745 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003746 Stmt::Item(ref item) => item.to_tokens(tokens),
3747 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003748 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003749 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003750 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003751 }
David Tolnay42602292016-10-01 22:25:45 -07003752 }
3753 }
3754 }
David Tolnay191e0582016-10-02 18:31:09 -07003755
Michael Layzell734adb42017-06-07 16:58:31 -04003756 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003757 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003758 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003759 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003760 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003761 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003762 if let Some((ref colon_token, ref ty)) = self.ty {
3763 colon_token.to_tokens(tokens);
3764 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003765 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003766 if let Some((ref eq_token, ref init)) = self.init {
3767 eq_token.to_tokens(tokens);
3768 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003769 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003770 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003771 }
3772 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003773}