blob: f74b6528e37516af611f4c20e02bb346105623e4 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnaye303b7c2018-05-20 16:46:35 -070010use proc_macro2::{Span, TokenStream};
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay14982012017-12-29 00:49:51 -050012#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -050013use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -050014#[cfg(feature = "full")]
15use std::mem;
David Tolnay94d2b792018-04-29 12:26:10 -070016#[cfg(feature = "extra-traits")]
17use tt::TokenStreamHelper;
David Tolnayf4bbbd92016-09-23 14:41:55 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -080020 /// A Rust expression.
David Tolnay614a0142018-01-07 10:25:43 -080021 ///
David Tolnay461d98e2018-01-07 11:07:19 -080022 /// *This type is available if Syn is built with the `"derive"` or `"full"`
23 /// feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enums
26 ///
27 /// This type is a syntax tree enum. In Syn this and other syntax tree enums
28 /// are designed to be traversed using the following rebinding idiom.
29 ///
30 /// ```
31 /// # use syn::Expr;
32 /// #
33 /// # fn example(expr: Expr) {
34 /// # const IGNORE: &str = stringify! {
35 /// let expr: Expr = /* ... */;
36 /// # };
37 /// match expr {
38 /// Expr::MethodCall(expr) => {
39 /// /* ... */
40 /// }
41 /// Expr::Cast(expr) => {
42 /// /* ... */
43 /// }
44 /// Expr::IfLet(expr) => {
45 /// /* ... */
46 /// }
47 /// /* ... */
48 /// # _ => {}
49 /// }
50 /// # }
51 /// ```
52 ///
53 /// We begin with a variable `expr` of type `Expr` that has no fields
54 /// (because it is an enum), and by matching on it and rebinding a variable
55 /// with the same name `expr` we effectively imbue our variable with all of
56 /// the data fields provided by the variant that it turned out to be. So for
57 /// example above if we ended up in the `MethodCall` case then we get to use
58 /// `expr.receiver`, `expr.args` etc; if we ended up in the `IfLet` case we
59 /// get to use `expr.pat`, `expr.then_branch`, `expr.else_branch`.
60 ///
61 /// The pattern is similar if the input expression is borrowed:
62 ///
63 /// ```
64 /// # use syn::Expr;
65 /// #
66 /// # fn example(expr: &Expr) {
67 /// match *expr {
68 /// Expr::MethodCall(ref expr) => {
69 /// # }
70 /// # _ => {}
71 /// # }
72 /// # }
73 /// ```
74 ///
75 /// This approach avoids repeating the variant names twice on every line.
76 ///
77 /// ```
78 /// # use syn::{Expr, ExprMethodCall};
79 /// #
80 /// # fn example(expr: Expr) {
81 /// # match expr {
82 /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive
83 /// # }
84 /// # _ => {}
85 /// # }
86 /// # }
87 /// ```
88 ///
89 /// In general, the name to which a syntax tree enum variant is bound should
90 /// be a suitable name for the complete syntax tree enum type.
91 ///
92 /// ```
93 /// # use syn::{Expr, ExprField};
94 /// #
95 /// # fn example(discriminant: &ExprField) {
96 /// // Binding is called `base` which is the name I would use if I were
97 /// // assigning `*discriminant.base` without an `if let`.
98 /// if let Expr::Tuple(ref base) = *discriminant.base {
99 /// # }
100 /// # }
101 /// ```
102 ///
103 /// A sign that you may not be choosing the right variable names is if you
104 /// see names getting repeated in your code, like accessing
105 /// `receiver.receiver` or `pat.pat` or `cond.cond`.
David Tolnay8c91b882017-12-28 23:04:32 -0500106 pub enum Expr {
David Tolnaya454c8f2018-01-07 01:01:10 -0800107 /// A box expression: `box f`.
David Tolnay461d98e2018-01-07 11:07:19 -0800108 ///
109 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400110 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500111 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500113 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500115
David Tolnaya454c8f2018-01-07 01:01:10 -0800116 /// A placement expression: `place <- value`.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -0500122 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 pub value: Box<Expr>,
124 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500125
David Tolnaya454c8f2018-01-07 01:01:10 -0800126 /// A slice literal expression: `[a, b, c, d]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400129 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500130 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500131 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500134
David Tolnaya454c8f2018-01-07 01:01:10 -0800135 /// A function call expression: `invoke(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -0500140 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700141 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500142 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500143 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500145
David Tolnaya454c8f2018-01-07 01:01:10 -0800146 /// A method call expression: `x.foo::<T>(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800147 ///
148 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -0500151 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -0500154 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500156 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500158
David Tolnaya454c8f2018-01-07 01:01:10 -0800159 /// A tuple expression: `(a, b, c, d)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800160 ///
161 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -0500162 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500163 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500164 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500165 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500167
David Tolnaya454c8f2018-01-07 01:01:10 -0800168 /// A binary operation: `a + b`, `a * b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800169 ///
170 /// *This type is available if Syn is built with the `"derive"` or
171 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500173 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500175 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 pub right: Box<Expr>,
177 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500178
David Tolnaya454c8f2018-01-07 01:01:10 -0800179 /// A unary operation: `!x`, `*x`.
David Tolnay461d98e2018-01-07 11:07:19 -0800180 ///
181 /// *This type is available if Syn is built with the `"derive"` or
182 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 pub op: UnOp,
186 pub expr: Box<Expr>,
187 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500188
David Tolnaya454c8f2018-01-07 01:01:10 -0800189 /// A literal in place of an expression: `1`, `"foo"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800190 ///
191 /// *This type is available if Syn is built with the `"derive"` or
192 /// `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500193 pub Lit(ExprLit {
194 pub attrs: Vec<Attribute>,
195 pub lit: Lit,
196 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500197
David Tolnaya454c8f2018-01-07 01:01:10 -0800198 /// A cast expression: `foo as f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800199 ///
200 /// *This type is available if Syn is built with the `"derive"` or
201 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -0500203 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500208
David Tolnaya454c8f2018-01-07 01:01:10 -0800209 /// A type ascription expression: `foo: f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800210 ///
211 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay0cf94f22017-12-28 23:46:26 -0500212 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500213 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700214 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800215 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800216 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500218
David Tolnaya454c8f2018-01-07 01:01:10 -0800219 /// An `if` expression with an optional `else` block: `if expr { ... }
220 /// else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800222 /// The `else` branch expression may only be an `If`, `IfLet`, or
223 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800224 ///
225 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500228 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500230 pub then_branch: Block,
231 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500233
David Tolnaya454c8f2018-01-07 01:01:10 -0800234 /// An `if let` expression with an optional `else` block: `if let pat =
235 /// expr { ... } else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800237 /// The `else` branch expression may only be an `If`, `IfLet`, or
238 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800243 pub if_token: Token![if],
244 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200245 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500248 pub then_branch: Block,
249 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500251
David Tolnaya454c8f2018-01-07 01:01:10 -0800252 /// A while loop: `while expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400255 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500256 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500257 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500259 pub cond: Box<Expr>,
260 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500262
David Tolnaya454c8f2018-01-07 01:01:10 -0800263 /// A while-let loop: `while let pat = expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800264 ///
265 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400266 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500267 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500268 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800269 pub while_token: Token![while],
270 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200271 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500273 pub expr: Box<Expr>,
274 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
David Tolnaya454c8f2018-01-07 01:01:10 -0800277 /// A for loop: `for pat in expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800278 ///
279 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400280 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500281 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500282 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500283 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500285 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 pub expr: Box<Expr>,
287 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500289
David Tolnaya454c8f2018-01-07 01:01:10 -0800290 /// Conditionless loop: `loop { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800291 ///
292 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400293 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500295 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub loop_token: Token![loop],
297 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500299
David Tolnaya454c8f2018-01-07 01:01:10 -0800300 /// A `match` expression: `match n { Some(n) => {}, None => {} }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800301 ///
302 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400303 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500304 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800305 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500307 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 pub arms: Vec<Arm>,
309 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500310
David Tolnaya454c8f2018-01-07 01:01:10 -0800311 /// A closure expression: `|a, b| a + b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800312 ///
313 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400314 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500315 pub attrs: Vec<Attribute>,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +0900316 pub asyncness: Option<Token![async]>,
David Tolnay13d4c0e2018-03-31 20:53:59 +0200317 pub movability: Option<Token![static]>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500318 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500320 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800321 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500322 pub output: ReturnType,
323 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500325
David Tolnaya454c8f2018-01-07 01:01:10 -0800326 /// An unsafe block: `unsafe { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800327 ///
328 /// *This type is available if Syn is built with the `"full"` feature.*
Nika Layzell640832a2017-12-04 13:37:09 -0500329 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500330 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500331 pub unsafe_token: Token![unsafe],
332 pub block: Block,
333 }),
334
David Tolnaya454c8f2018-01-07 01:01:10 -0800335 /// A blocked scope: `{ ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800336 ///
337 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400338 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500339 pub attrs: Vec<Attribute>,
David Tolnay1d8e9962018-08-24 19:04:20 -0400340 pub label: Option<Label>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 pub block: Block,
342 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700343
David Tolnaya454c8f2018-01-07 01:01:10 -0800344 /// An assignment expression: `a = compute()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800345 ///
346 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400347 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500348 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800350 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500351 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500353
David Tolnaya454c8f2018-01-07 01:01:10 -0800354 /// A compound assignment expression: `counter += 1`.
David Tolnay461d98e2018-01-07 11:07:19 -0800355 ///
356 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400357 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500358 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500360 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub right: Box<Expr>,
362 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500363
David Tolnaya454c8f2018-01-07 01:01:10 -0800364 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500365 /// field (`obj.0`).
David Tolnay461d98e2018-01-07 11:07:19 -0800366 ///
367 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd5147742018-06-30 10:09:52 -0700368 pub Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -0500369 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500370 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800371 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500372 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500374
David Tolnay05658502018-01-07 09:56:37 -0800375 /// A square bracketed indexing expression: `vector[2]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800376 ///
377 /// *This type is available if Syn is built with the `"derive"` or
378 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500380 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500382 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500383 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500385
David Tolnaya454c8f2018-01-07 01:01:10 -0800386 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800387 ///
388 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400389 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500390 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500393 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700395
David Tolnaya454c8f2018-01-07 01:01:10 -0800396 /// A path like `std::mem::replace` possibly containing generic
397 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800399 /// A plain identifier like `x` is a path of length 1.
David Tolnay461d98e2018-01-07 11:07:19 -0800400 ///
401 /// *This type is available if Syn is built with the `"derive"` or
402 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500404 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 pub qself: Option<QSelf>,
406 pub path: Path,
407 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700408
David Tolnaya454c8f2018-01-07 01:01:10 -0800409 /// A referencing operation: `&a` or `&mut a`.
David Tolnay461d98e2018-01-07 11:07:19 -0800410 ///
411 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay00674ba2018-03-31 18:14:11 +0200412 pub Reference(ExprReference #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500413 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500415 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700416 pub expr: Box<Expr>,
417 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500418
David Tolnaya454c8f2018-01-07 01:01:10 -0800419 /// A `break`, with an optional label to break and an optional
420 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800421 ///
422 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400423 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500424 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500425 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700426 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 pub expr: Option<Box<Expr>>,
428 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500429
David Tolnaya454c8f2018-01-07 01:01:10 -0800430 /// A `continue`, with an optional label.
David Tolnay461d98e2018-01-07 11:07:19 -0800431 ///
432 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400433 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500434 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500436 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700437 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500438
David Tolnaya454c8f2018-01-07 01:01:10 -0800439 /// A `return`, with an optional value to be returned.
David Tolnay461d98e2018-01-07 11:07:19 -0800440 ///
441 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayc246cd32017-12-28 23:14:32 -0500442 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500443 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800444 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500445 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700447
David Tolnaya454c8f2018-01-07 01:01:10 -0800448 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500451 pub Macro(ExprMacro #full {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700455
David Tolnaya454c8f2018-01-07 01:01:10 -0800456 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700457 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800458 /// The `rest` provides the value of the remaining fields as in `S { a:
459 /// 1, b: 1, ..rest }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800460 ///
461 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400462 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500463 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500465 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500466 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500467 pub dot2_token: Option<Token![..]>,
468 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700470
David Tolnaya454c8f2018-01-07 01:01:10 -0800471 /// An array literal constructed from one repeated element: `[0u8; N]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800472 ///
473 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400474 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500475 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500476 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700477 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500478 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800479 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700480 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700481
David Tolnaya454c8f2018-01-07 01:01:10 -0800482 /// A parenthesized expression: `(a + b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800483 ///
484 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9374bc02018-01-27 18:49:36 -0800485 pub Paren(ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -0500486 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500487 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500488 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700490
David Tolnaya454c8f2018-01-07 01:01:10 -0800491 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400492 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800493 /// This variant is important for faithfully representing the precedence
494 /// of expressions and is related to `None`-delimited spans in a
495 /// `TokenStream`.
David Tolnay461d98e2018-01-07 11:07:19 -0800496 ///
497 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye98775f2017-12-28 23:17:00 -0500498 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500499 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500500 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500501 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400502 }),
503
David Tolnaya454c8f2018-01-07 01:01:10 -0800504 /// A try-expression: `expr?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800505 ///
506 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400507 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500508 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700512
David Tolnay02a9c6f2018-08-24 18:58:45 -0400513 /// An async block: `async { ... }`.
514 ///
515 /// *This type is available if Syn is built with the `"full"` feature.*
516 pub Async(ExprAsync #full {
517 pub attrs: Vec<Attribute>,
518 pub async_token: Token![async],
519 pub capture: Option<Token![move]>,
520 pub block: Block,
521 }),
522
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400523 /// A try block: `try { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800524 ///
525 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400526 pub TryBlock(ExprTryBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500527 pub attrs: Vec<Attribute>,
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400528 pub try_token: Token![try],
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 pub block: Block,
530 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700531
David Tolnaya454c8f2018-01-07 01:01:10 -0800532 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800533 ///
534 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700535 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500536 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800537 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700538 pub expr: Option<Box<Expr>>,
539 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500540
David Tolnaya454c8f2018-01-07 01:01:10 -0800541 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800542 ///
543 /// *This type is available if Syn is built with the `"derive"` or
544 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500545 pub Verbatim(ExprVerbatim #manual_extra_traits {
546 pub tts: TokenStream,
547 }),
548 }
549}
550
551#[cfg(feature = "extra-traits")]
552impl Eq for ExprVerbatim {}
553
554#[cfg(feature = "extra-traits")]
555impl PartialEq for ExprVerbatim {
556 fn eq(&self, other: &Self) -> bool {
557 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
558 }
559}
560
561#[cfg(feature = "extra-traits")]
562impl Hash for ExprVerbatim {
563 fn hash<H>(&self, state: &mut H)
564 where
565 H: Hasher,
566 {
567 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700568 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700569}
570
David Tolnay8c91b882017-12-28 23:04:32 -0500571impl Expr {
572 // Not public API.
573 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500574 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500575 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500576 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800577 Expr::Box(ExprBox { ref mut attrs, .. })
578 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
579 | Expr::Array(ExprArray { ref mut attrs, .. })
580 | Expr::Call(ExprCall { ref mut attrs, .. })
581 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
582 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
583 | Expr::Binary(ExprBinary { ref mut attrs, .. })
584 | Expr::Unary(ExprUnary { ref mut attrs, .. })
585 | Expr::Lit(ExprLit { ref mut attrs, .. })
586 | Expr::Cast(ExprCast { ref mut attrs, .. })
587 | Expr::Type(ExprType { ref mut attrs, .. })
588 | Expr::If(ExprIf { ref mut attrs, .. })
589 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
590 | Expr::While(ExprWhile { ref mut attrs, .. })
591 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
592 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
593 | Expr::Loop(ExprLoop { ref mut attrs, .. })
594 | Expr::Match(ExprMatch { ref mut attrs, .. })
595 | Expr::Closure(ExprClosure { ref mut attrs, .. })
596 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
597 | Expr::Block(ExprBlock { ref mut attrs, .. })
598 | Expr::Assign(ExprAssign { ref mut attrs, .. })
599 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
600 | Expr::Field(ExprField { ref mut attrs, .. })
601 | Expr::Index(ExprIndex { ref mut attrs, .. })
602 | Expr::Range(ExprRange { ref mut attrs, .. })
603 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200604 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800605 | Expr::Break(ExprBreak { ref mut attrs, .. })
606 | Expr::Continue(ExprContinue { ref mut attrs, .. })
607 | Expr::Return(ExprReturn { ref mut attrs, .. })
608 | Expr::Macro(ExprMacro { ref mut attrs, .. })
609 | Expr::Struct(ExprStruct { ref mut attrs, .. })
610 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
611 | Expr::Paren(ExprParen { ref mut attrs, .. })
612 | Expr::Group(ExprGroup { ref mut attrs, .. })
613 | Expr::Try(ExprTry { ref mut attrs, .. })
David Tolnay02a9c6f2018-08-24 18:58:45 -0400614 | Expr::Async(ExprAsync { ref mut attrs, .. })
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400615 | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800616 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500617 Expr::Verbatim(_) => {
618 // TODO
619 Vec::new()
620 }
David Tolnay8c91b882017-12-28 23:04:32 -0500621 }
622 }
623}
624
David Tolnay85b69a42017-12-27 20:43:10 -0500625ast_enum! {
626 /// A struct or tuple struct field accessed in a struct literal or field
627 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800628 ///
629 /// *This type is available if Syn is built with the `"derive"` or `"full"`
630 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500631 pub enum Member {
632 /// A named field like `self.x`.
633 Named(Ident),
634 /// An unnamed field like `self.0`.
635 Unnamed(Index),
636 }
637}
638
David Tolnay85b69a42017-12-27 20:43:10 -0500639ast_struct! {
640 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800641 ///
642 /// *This type is available if Syn is built with the `"derive"` or `"full"`
643 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500644 pub struct Index #manual_extra_traits {
645 pub index: u32,
646 pub span: Span,
647 }
648}
649
David Tolnay14982012017-12-29 00:49:51 -0500650impl From<usize> for Index {
651 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700652 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500653 Index {
654 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700655 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500656 }
657 }
658}
659
660#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500661impl Eq for Index {}
662
David Tolnay14982012017-12-29 00:49:51 -0500663#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500664impl PartialEq for Index {
665 fn eq(&self, other: &Self) -> bool {
666 self.index == other.index
667 }
668}
669
David Tolnay14982012017-12-29 00:49:51 -0500670#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500671impl Hash for Index {
672 fn hash<H: Hasher>(&self, state: &mut H) {
673 self.index.hash(state);
674 }
675}
676
677#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700678ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800679 /// The `::<>` explicit type parameters passed to a method call:
680 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800681 ///
682 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500683 pub struct MethodTurbofish {
684 pub colon2_token: Token![::],
685 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500686 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500687 pub gt_token: Token![>],
688 }
689}
690
691#[cfg(feature = "full")]
692ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800693 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500696 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800697 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500698 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800699 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500700 ///
701 /// NOTE: Identity expressions are represented as Type arguments, as
702 /// they are indistinguishable syntactically.
703 Const(Expr),
704 }
705}
706
707#[cfg(feature = "full")]
708ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800710 ///
711 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500713 /// Attributes tagged on the field.
714 pub attrs: Vec<Attribute>,
715
716 /// Name or index of the field.
717 pub member: Member,
718
David Tolnay5d7098a2017-12-29 01:35:24 -0500719 /// The colon in `Struct { x: x }`. If written in shorthand like
720 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500721 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500722
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 /// Value of the field.
724 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 }
David Tolnay055a7042016-10-02 19:23:54 -0700726}
727
Michael Layzell734adb42017-06-07 16:58:31 -0400728#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700729ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800730 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800731 ///
732 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500733 pub struct Label {
734 pub name: Lifetime,
735 pub colon_token: Token![:],
736 }
737}
738
739#[cfg(feature = "full")]
740ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800741 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800742 ///
743 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500745 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 /// Statements in a block
747 pub stmts: Vec<Stmt>,
748 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700749}
750
Michael Layzell734adb42017-06-07 16:58:31 -0400751#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700752ast_enum! {
753 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800754 ///
755 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700756 pub enum Stmt {
757 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800758 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700759
Alex Crichton62a0a592017-05-22 13:58:53 -0700760 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800761 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700762
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800764 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700765
David Tolnaya454c8f2018-01-07 01:01:10 -0800766 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800767 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700768 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700769}
770
Michael Layzell734adb42017-06-07 16:58:31 -0400771#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700772ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800773 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800774 ///
775 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700776 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500777 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800778 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200779 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500780 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500781 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500782 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700783 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700784}
785
Michael Layzell734adb42017-06-07 16:58:31 -0400786#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700787ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800788 /// A pattern in a local binding, function signature, match expression, or
789 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800790 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800791 /// *This type is available if Syn is built with the `"full"` feature.*
792 ///
David Tolnay614a0142018-01-07 10:25:43 -0800793 /// # Syntax tree enum
794 ///
795 /// This type is a [syntax tree enum].
796 ///
797 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700798 // Clippy false positive
799 // https://github.com/Manishearth/rust-clippy/issues/1241
800 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
801 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800802 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800803 ///
804 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800806 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700807 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700808
David Tolnaya454c8f2018-01-07 01:01:10 -0800809 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800810 ///
811 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700812 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500813 pub by_ref: Option<Token![ref]>,
814 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700815 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500816 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700817 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700818
David Tolnaya454c8f2018-01-07 01:01:10 -0800819 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800820 ///
821 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700822 pub Struct(PatStruct {
823 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500824 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500825 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800826 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700828
David Tolnaya454c8f2018-01-07 01:01:10 -0800829 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800830 ///
831 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700832 pub TupleStruct(PatTupleStruct {
833 pub path: Path,
834 pub pat: PatTuple,
835 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700836
David Tolnaya454c8f2018-01-07 01:01:10 -0800837 /// A path pattern like `Color::Red`, optionally qualified with a
838 /// self-type.
839 ///
840 /// Unquailfied path patterns can legally refer to variants, structs,
841 /// constants or associated constants. Quailfied path patterns like
842 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
843 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800844 ///
845 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700846 pub Path(PatPath {
847 pub qself: Option<QSelf>,
848 pub path: Path,
849 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700850
David Tolnaya454c8f2018-01-07 01:01:10 -0800851 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800852 ///
853 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700854 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500855 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500856 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500857 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500858 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500859 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700860 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800861
862 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800863 ///
864 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700865 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500867 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700868 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800869
870 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800871 ///
872 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800874 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500875 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500876 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700877 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800878
879 /// A literal pattern: `0`.
880 ///
881 /// This holds an `Expr` rather than a `Lit` because negative numbers
882 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800883 ///
884 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700885 pub Lit(PatLit {
886 pub expr: Box<Expr>,
887 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800888
889 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800890 ///
891 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 pub Range(PatRange {
893 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700894 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500895 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800897
898 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800899 ///
900 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700901 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500902 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500903 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700904 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500905 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500906 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500907 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700908 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800909
910 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800911 ///
912 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500913 pub Macro(PatMacro {
914 pub mac: Macro,
915 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800916
917 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800918 ///
919 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500920 pub Verbatim(PatVerbatim #manual_extra_traits {
921 pub tts: TokenStream,
922 }),
923 }
924}
925
David Tolnayc43b44e2017-12-30 23:55:54 -0500926#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500927impl Eq for PatVerbatim {}
928
David Tolnayc43b44e2017-12-30 23:55:54 -0500929#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500930impl PartialEq for PatVerbatim {
931 fn eq(&self, other: &Self) -> bool {
932 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
933 }
934}
935
David Tolnayc43b44e2017-12-30 23:55:54 -0500936#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500937impl Hash for PatVerbatim {
938 fn hash<H>(&self, state: &mut H)
939 where
940 H: Hasher,
941 {
942 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700944}
945
Michael Layzell734adb42017-06-07 16:58:31 -0400946#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700947ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800948 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800950 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500952 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800953 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500954 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800956 /// 0...10 => {
957 /// return true;
958 /// }
959 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500960 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800962 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500963 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800965 ///
966 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700967 pub struct Arm {
968 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200969 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500970 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500971 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200972 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700973 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700976}
977
Michael Layzell734adb42017-06-07 16:58:31 -0400978#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700979ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800980 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800981 ///
982 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700983 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800985 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800987 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800988 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700989 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700990}
991
Michael Layzell734adb42017-06-07 16:58:31 -0400992#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700993ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800994 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700995 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800996 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
997 /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token.
David Tolnay461d98e2018-01-07 11:07:19 -0800998 ///
999 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -05001001 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -05001002 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -05001003 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001004 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001005 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001006}
1007
Michael Layzell3936ceb2017-07-08 00:28:36 -04001008#[cfg(any(feature = "parsing", feature = "printing"))]
1009#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -07001010fn arm_expr_requires_comma(expr: &Expr) -> bool {
1011 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1012 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001013 match *expr {
1014 Expr::Unsafe(..)
1015 | Expr::Block(..)
1016 | Expr::If(..)
1017 | Expr::IfLet(..)
1018 | Expr::Match(..)
1019 | Expr::While(..)
1020 | Expr::WhileLet(..)
1021 | Expr::Loop(..)
1022 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001023 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001024 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001025 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001026 }
1027}
1028
David Tolnayb9c8e322016-09-23 20:48:37 -07001029#[cfg(feature = "parsing")]
1030pub mod parsing {
1031 use super::*;
David Tolnaya7d69fc2018-08-26 13:30:24 -04001032 use path::parsing::old_mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001033 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001034 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001035
David Tolnay9389c382018-08-27 09:13:37 -07001036 use parse::{Parse, ParseStream, Result};
Michael Layzell734adb42017-06-07 16:58:31 -04001037 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001038 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001039 #[cfg(feature = "full")]
1040 use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -07001041 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001042
David Tolnay9389c382018-08-27 09:13:37 -07001043 macro_rules! named2 {
1044 ($name:ident ($($arg:ident : $argty:ty),*) -> $ret:ty, $($rest:tt)*) => {
1045 fn $name(input: ParseStream $(, $arg : $argty)*) -> Result<$ret> {
1046 named!(_synom ($($arg : $argty),*) -> $ret, $($rest)*);
1047 input.step_cursor(|cursor| _synom(*cursor $(, $arg)*))
1048 }
1049 };
1050 ($name:ident -> $ret:ty, $($rest:tt)*) => {
1051 fn $name(input: ParseStream) -> Result<$ret> {
1052 named!(_synom -> $ret, $($rest)*);
1053 input.step_cursor(|cursor| _synom(*cursor))
1054 }
1055 };
1056 }
1057
1058 #[cfg(feature = "full")]
1059 macro_rules! ambiguous_expr {
1060 ($i:expr, $allow_struct:ident) => {
1061 shim!($i, ambiguous_expr, $allow_struct, AllowBlock(true))
1062 };
1063 }
1064
David Tolnaybcf26022017-12-25 22:10:52 -05001065 // When we're parsing expressions which occur before blocks, like in an if
1066 // statement's condition, we cannot parse a struct literal.
1067 //
1068 // Struct literals are ambiguous in certain positions
1069 // https://github.com/rust-lang/rfcs/pull/92
David Tolnay9389c382018-08-27 09:13:37 -07001070 #[derive(Copy, Clone)]
1071 pub struct AllowStruct(bool);
1072
1073 #[derive(Copy, Clone)]
1074 pub struct AllowBlock(bool);
David Tolnayaf2557e2016-10-24 11:52:21 -07001075
David Tolnaybcf26022017-12-25 22:10:52 -05001076 // When we are parsing an optional suffix expression, we cannot allow blocks
1077 // if structs are not allowed.
1078 //
1079 // Example:
1080 //
1081 // if break {} {}
1082 //
1083 // is ambiguous between:
1084 //
1085 // if (break {}) {}
1086 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001087 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001088 macro_rules! opt_ambiguous_expr {
1089 ($i:expr, $allow_struct:ident) => {
David Tolnay9389c382018-08-27 09:13:37 -07001090 option!($i, shim!(ambiguous_expr, $allow_struct, AllowBlock($allow_struct.0)))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001091 };
1092 }
1093
David Tolnay9389c382018-08-27 09:13:37 -07001094 impl Parse for Expr {
1095 fn parse(input: ParseStream) -> Result<Self> {
1096 ambiguous_expr(input, AllowStruct(true), AllowBlock(true))
Alex Crichton954046c2017-05-30 21:49:42 -07001097 }
1098 }
1099
Michael Layzell734adb42017-06-07 16:58:31 -04001100 #[cfg(feature = "full")]
David Tolnay9fb0aed2018-08-27 10:23:12 -07001101 fn expr_no_struct(input: ParseStream) -> Result<Expr> {
1102 ambiguous_expr(input, AllowStruct(false), AllowBlock(true))
1103 }
David Tolnayaf2557e2016-10-24 11:52:21 -07001104
David Tolnaybcf26022017-12-25 22:10:52 -05001105 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001106 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001107 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1108 assign_expr(input, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001109 }
1110
Michael Layzell734adb42017-06-07 16:58:31 -04001111 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001112 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001113 // NOTE: We intentionally skip assign_expr, placement_expr, and
David Tolnay9389c382018-08-27 09:13:37 -07001114 // range_expr as they are only parsed in full mode.
1115 or_expr(input, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001116 }
1117
David Tolnaybcf26022017-12-25 22:10:52 -05001118 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001119 macro_rules! binop {
David Tolnay378175c2018-08-27 10:01:12 -07001120 ($name:ident, $next:ident, |$var:ident| $parse_op:expr) => {
David Tolnay9389c382018-08-27 09:13:37 -07001121 fn $name(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
David Tolnay378175c2018-08-27 10:01:12 -07001122 let $var = input;
1123 let mut e: Expr = $next(input, allow_struct, allow_block)?;
1124 while let Some(op) = $parse_op {
1125 e = Expr::Binary(ExprBinary {
1126 attrs: Vec::new(),
1127 left: Box::new(e),
1128 op: op,
1129 right: Box::new($next(input, allow_struct, AllowBlock(true))?),
1130 });
David Tolnay9389c382018-08-27 09:13:37 -07001131 }
David Tolnay378175c2018-08-27 10:01:12 -07001132 Ok(e)
David Tolnay9389c382018-08-27 09:13:37 -07001133 }
Alex Crichton954046c2017-05-30 21:49:42 -07001134 }
David Tolnay54e854d2016-10-24 12:03:30 -07001135 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001136
David Tolnaybcf26022017-12-25 22:10:52 -05001137 // <placement> = <placement> ..
1138 // <placement> += <placement> ..
1139 // <placement> -= <placement> ..
1140 // <placement> *= <placement> ..
1141 // <placement> /= <placement> ..
1142 // <placement> %= <placement> ..
1143 // <placement> ^= <placement> ..
1144 // <placement> &= <placement> ..
1145 // <placement> |= <placement> ..
1146 // <placement> <<= <placement> ..
1147 // <placement> >>= <placement> ..
1148 //
1149 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001150 #[cfg(feature = "full")]
David Tolnay1d2391c2018-08-27 10:29:28 -07001151 fn assign_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1152 let mut e = placement_expr(input, allow_struct, allow_block)?;
1153 if input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1154 e = Expr::Assign(ExprAssign {
1155 attrs: Vec::new(),
1156 left: Box::new(e),
1157 eq_token: input.parse()?,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001158 // Recurse into self to parse right-associative operator.
David Tolnay1d2391c2018-08-27 10:29:28 -07001159 right: Box::new(assign_expr(input, allow_struct, AllowBlock(true))?),
1160 });
1161 } else if input.peek(Token![+=])
1162 || input.peek(Token![-=])
1163 || input.peek(Token![*=])
1164 || input.peek(Token![/=])
1165 || input.peek(Token![%=])
1166 || input.peek(Token![^=])
1167 || input.peek(Token![&=])
1168 || input.peek(Token![|=])
1169 || input.peek(Token![<<=])
1170 || input.peek(Token![>>=])
1171 {
1172 e = Expr::AssignOp(ExprAssignOp {
1173 attrs: Vec::new(),
1174 left: Box::new(e),
1175 op: BinOp::parse_assign_op(input)?,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001176 // Recurse into self to parse right-associative operator.
David Tolnay1d2391c2018-08-27 10:29:28 -07001177 right: Box::new(assign_expr(input, allow_struct, AllowBlock(true))?),
1178 });
1179 }
1180 Ok(e)
1181 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001182
David Tolnaybcf26022017-12-25 22:10:52 -05001183 // <range> <- <range> ..
1184 //
1185 // NOTE: The `in place { expr }` version of this syntax is parsed in
1186 // `atom_expr`, not here.
1187 //
1188 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001189 #[cfg(feature = "full")]
David Tolnay1da8c212018-08-27 10:35:50 -07001190 fn placement_expr(input: ParseStream, allow_struct: AllowStruct, allow_block: AllowBlock) -> Result<Expr> {
1191 let mut e = range_expr(input, allow_struct, allow_block)?;
1192 if input.peek(Token![<-]) {
1193 e = Expr::InPlace(ExprInPlace {
1194 attrs: Vec::new(),
1195 place: Box::new(e),
1196 arrow_token: input.parse()?,
1197 value: Box::new(placement_expr(input, allow_struct, AllowBlock(true))?),
1198 });
1199 }
1200 Ok(e)
1201 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001202
David Tolnaybcf26022017-12-25 22:10:52 -05001203 // <or> ... <or> ..
1204 // <or> .. <or> ..
1205 // <or> ..
1206 //
1207 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1208 // rules are for parsing these expressions are, but this is not correct.
1209 // For example, `a .. b .. c` is not a legal expression. It should not
1210 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1211 //
1212 // NOTE: The form of ranges which don't include a preceding expression are
1213 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001214 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001215 named2!(range_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1216 mut e: shim!(or_expr, allow_struct, allow_block) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001217 many0!(do_parse!(
1218 limits: syn!(RangeLimits) >>
1219 // We don't want to allow blocks here if we don't allow structs. See
1220 // the reasoning for `opt_ambiguous_expr!` above.
David Tolnay9389c382018-08-27 09:13:37 -07001221 hi: option!(shim!(or_expr, allow_struct, AllowBlock(allow_struct.0))) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001222 ({
1223 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001224 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001225 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001226 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001227 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001228 }.into();
1229 })
1230 )) >>
1231 (e)
1232 ));
1233
David Tolnaybcf26022017-12-25 22:10:52 -05001234 // <and> || <and> ...
David Tolnay378175c2018-08-27 10:01:12 -07001235 binop!(or_expr, and_expr, |input| {
1236 if input.peek(Token![||]) {
1237 Some(BinOp::Or(input.parse()?))
1238 } else {
1239 None
1240 }
1241 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242
David Tolnaybcf26022017-12-25 22:10:52 -05001243 // <compare> && <compare> ...
David Tolnay378175c2018-08-27 10:01:12 -07001244 binop!(and_expr, compare_expr, |input| {
1245 if input.peek(Token![&&]) {
1246 Some(BinOp::And(input.parse()?))
1247 } else {
1248 None
1249 }
1250 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001251
David Tolnaybcf26022017-12-25 22:10:52 -05001252 // <bitor> == <bitor> ...
1253 // <bitor> != <bitor> ...
1254 // <bitor> >= <bitor> ...
1255 // <bitor> <= <bitor> ...
1256 // <bitor> > <bitor> ...
1257 // <bitor> < <bitor> ...
1258 //
1259 // NOTE: This operator appears to be parsed as left-associative, but errors
1260 // if it is used in a non-associative manner.
David Tolnay378175c2018-08-27 10:01:12 -07001261 binop!(compare_expr, bitor_expr, |input| {
1262 if input.peek(Token![==]) {
1263 Some(BinOp::Eq(input.parse()?))
1264 } else if input.peek(Token![!=]) {
1265 Some(BinOp::Ne(input.parse()?))
1266 // must be before `<`
1267 } else if input.peek(Token![<=]) {
1268 Some(BinOp::Le(input.parse()?))
1269 // must be before `>`
1270 } else if input.peek(Token![>=]) {
1271 Some(BinOp::Ge(input.parse()?))
1272 } else if input.peek(Token![<]) && !input.peek(Token![<<]) && !input.peek(Token![<-]) {
1273 Some(BinOp::Lt(input.parse()?))
1274 } else if input.peek(Token![>]) && !input.peek(Token![>>]) {
1275 Some(BinOp::Gt(input.parse()?))
1276 } else {
1277 None
1278 }
1279 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001280
David Tolnaybcf26022017-12-25 22:10:52 -05001281 // <bitxor> | <bitxor> ...
David Tolnay378175c2018-08-27 10:01:12 -07001282 binop!(bitor_expr, bitxor_expr, |input| {
1283 if input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
1284 Some(BinOp::BitOr(input.parse()?))
1285 } else {
1286 None
1287 }
1288 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001289
David Tolnaybcf26022017-12-25 22:10:52 -05001290 // <bitand> ^ <bitand> ...
David Tolnay378175c2018-08-27 10:01:12 -07001291 binop!(bitxor_expr, bitand_expr, |input| {
1292 if input.peek(Token![^]) && !input.peek(Token![^=]) {
1293 Some(BinOp::BitXor(input.parse()?))
1294 } else {
1295 None
1296 }
1297 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001298
David Tolnaybcf26022017-12-25 22:10:52 -05001299 // <shift> & <shift> ...
David Tolnay378175c2018-08-27 10:01:12 -07001300 binop!(bitand_expr, shift_expr, |input| {
1301 if input.peek(Token![&]) && !input.peek(Token![&&]) && !input.peek(Token![&=]) {
1302 Some(BinOp::BitAnd(input.parse()?))
1303 } else {
1304 None
1305 }
1306 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001307
David Tolnaybcf26022017-12-25 22:10:52 -05001308 // <arith> << <arith> ...
1309 // <arith> >> <arith> ...
David Tolnay378175c2018-08-27 10:01:12 -07001310 binop!(shift_expr, arith_expr, |input| {
1311 if input.peek(Token![<<]) && !input.peek(Token![<<=]) {
1312 Some(BinOp::Shl(input.parse()?))
1313 } else if input.peek(Token![>>]) && !input.peek(Token![>>=]) {
1314 Some(BinOp::Shr(input.parse()?))
1315 } else {
1316 None
1317 }
1318 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001319
David Tolnaybcf26022017-12-25 22:10:52 -05001320 // <term> + <term> ...
1321 // <term> - <term> ...
David Tolnay378175c2018-08-27 10:01:12 -07001322 binop!(arith_expr, term_expr, |input| {
1323 if input.peek(Token![+]) && !input.peek(Token![+=]) {
1324 Some(BinOp::Add(input.parse()?))
1325 } else if input.peek(Token![-]) && !input.peek(Token![-=]) {
1326 Some(BinOp::Sub(input.parse()?))
1327 } else {
1328 None
1329 }
1330 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001331
David Tolnaybcf26022017-12-25 22:10:52 -05001332 // <cast> * <cast> ...
1333 // <cast> / <cast> ...
1334 // <cast> % <cast> ...
David Tolnay378175c2018-08-27 10:01:12 -07001335 binop!(term_expr, cast_expr, |input| {
1336 if input.peek(Token![*]) && !input.peek(Token![*=]) {
1337 Some(BinOp::Mul(input.parse()?))
1338 } else if input.peek(Token![/]) && !input.peek(Token![/=]) {
1339 Some(BinOp::Div(input.parse()?))
1340 } else if input.peek(Token![%]) && !input.peek(Token![%=]) {
1341 Some(BinOp::Rem(input.parse()?))
1342 } else {
1343 None
1344 }
1345 });
Michael Layzellb78f3b52017-06-04 19:03:03 -04001346
David Tolnaybcf26022017-12-25 22:10:52 -05001347 // <unary> as <ty>
1348 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001349 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001350 named2!(cast_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1351 mut e: shim!(unary_expr, allow_struct, allow_block) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001352 many0!(alt!(
1353 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001354 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001355 // We can't accept `A + B` in cast expressions, as it's
1356 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001357 ty: shim!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001358 ({
1359 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001360 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001361 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001362 as_token: as_,
1363 ty: Box::new(ty),
1364 }.into();
1365 })
1366 )
1367 |
1368 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001369 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001370 // We can't accept `A + B` in cast expressions, as it's
1371 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001372 ty: shim!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001373 ({
1374 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001375 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001376 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001377 colon_token: colon,
1378 ty: Box::new(ty),
1379 }.into();
1380 })
1381 )
1382 )) >>
1383 (e)
1384 ));
1385
David Tolnay0cf94f22017-12-28 23:46:26 -05001386 // <unary> as <ty>
1387 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001388 named2!(cast_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1389 mut e: shim!(unary_expr, allow_struct, allow_block) >>
David Tolnay0cf94f22017-12-28 23:46:26 -05001390 many0!(do_parse!(
1391 as_: keyword!(as) >>
1392 // We can't accept `A + B` in cast expressions, as it's
1393 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001394 ty: shim!(Type::without_plus) >>
David Tolnay0cf94f22017-12-28 23:46:26 -05001395 ({
1396 e = ExprCast {
1397 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001398 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001399 as_token: as_,
1400 ty: Box::new(ty),
1401 }.into();
1402 })
1403 )) >>
1404 (e)
1405 ));
1406
David Tolnaybcf26022017-12-25 22:10:52 -05001407 // <UnOp> <trailer>
1408 // & <trailer>
1409 // &mut <trailer>
1410 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001411 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001412 named2!(unary_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001413 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001414 attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001415 op: syn!(UnOp) >>
David Tolnay9389c382018-08-27 09:13:37 -07001416 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001417 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001418 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001419 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001420 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001421 }.into())
1422 )
1423 |
1424 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001425 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001426 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001427 mutability: option!(keyword!(mut)) >>
David Tolnay9389c382018-08-27 09:13:37 -07001428 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001429 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001430 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001431 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001432 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001433 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001434 }.into())
1435 )
1436 |
1437 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001438 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001439 box_: keyword!(box) >>
David Tolnay9389c382018-08-27 09:13:37 -07001440 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001441 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001442 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001443 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001444 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001445 }.into())
1446 )
1447 |
David Tolnay9389c382018-08-27 09:13:37 -07001448 shim!(trailer_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001449 ));
1450
Michael Layzell734adb42017-06-07 16:58:31 -04001451 // XXX: This duplication is ugly
1452 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001453 named2!(unary_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001454 do_parse!(
1455 op: syn!(UnOp) >>
David Tolnay9389c382018-08-27 09:13:37 -07001456 expr: shim!(unary_expr, allow_struct, AllowBlock(true)) >>
Michael Layzell734adb42017-06-07 16:58:31 -04001457 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001458 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001459 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001460 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001461 }.into())
1462 )
1463 |
David Tolnay9389c382018-08-27 09:13:37 -07001464 shim!(trailer_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001465 ));
1466
David Tolnayd997aef2018-07-21 18:42:31 -07001467 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001468 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1469 let mut outer = Vec::new();
1470 let mut inner = Vec::new();
1471 for attr in mem::replace(attrs, Vec::new()) {
1472 match attr.style {
1473 AttrStyle::Outer => outer.push(attr),
1474 AttrStyle::Inner(_) => inner.push(attr),
1475 }
1476 }
1477 *attrs = inner;
1478 outer
1479 }
1480
David Tolnaybcf26022017-12-25 22:10:52 -05001481 // <atom> (..<args>) ...
1482 // <atom> . <ident> (..<args>) ...
1483 // <atom> . <ident> ...
1484 // <atom> . <lit> ...
1485 // <atom> [ <expr> ] ...
1486 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001487 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001488 named2!(trailer_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1489 mut e: shim!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001490 outer_attrs: value!({
1491 let mut attrs = e.replace_attrs(Vec::new());
1492 let outer_attrs = take_outer(&mut attrs);
1493 e.replace_attrs(attrs);
1494 outer_attrs
1495 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001496 many0!(alt!(
David Tolnay9389c382018-08-27 09:13:37 -07001497 tap!(args: shim!(and_call) => {
David Tolnay8875fca2017-12-31 13:52:37 -05001498 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001499 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001500 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001501 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001502 args: args,
1503 paren_token: paren,
1504 }.into();
1505 })
1506 |
David Tolnay9389c382018-08-27 09:13:37 -07001507 tap!(more: shim!(and_method_call) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001508 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001509 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001510 e = call.into();
1511 })
1512 |
David Tolnay9389c382018-08-27 09:13:37 -07001513 tap!(field: shim!(and_field) => {
David Tolnay85b69a42017-12-27 20:43:10 -05001514 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001515 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001516 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001517 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001518 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001519 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001520 }.into();
1521 })
1522 |
David Tolnay9389c382018-08-27 09:13:37 -07001523 tap!(i: shim!(and_index) => {
David Tolnay8875fca2017-12-31 13:52:37 -05001524 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001525 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001526 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001527 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001528 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001529 index: Box::new(i),
1530 }.into();
1531 })
1532 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001533 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001534 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001535 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001536 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001537 question_token: question,
1538 }.into();
1539 })
1540 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001541 ({
1542 let mut attrs = outer_attrs;
1543 attrs.extend(e.replace_attrs(Vec::new()));
1544 e.replace_attrs(attrs);
1545 e
1546 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001547 ));
1548
Michael Layzell734adb42017-06-07 16:58:31 -04001549 // XXX: Duplication == ugly
1550 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001551 named2!(trailer_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, do_parse!(
1552 mut e: shim!(atom_expr, allow_struct, allow_block) >>
Michael Layzell734adb42017-06-07 16:58:31 -04001553 many0!(alt!(
David Tolnay9389c382018-08-27 09:13:37 -07001554 tap!(args: shim!(and_call) => {
Michael Layzell734adb42017-06-07 16:58:31 -04001555 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001556 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001557 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001558 paren_token: args.0,
1559 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001560 }.into();
1561 })
1562 |
David Tolnay9389c382018-08-27 09:13:37 -07001563 tap!(field: shim!(and_field) => {
David Tolnayd5147742018-06-30 10:09:52 -07001564 let (token, member) = field;
1565 e = ExprField {
1566 attrs: Vec::new(),
1567 base: Box::new(e),
1568 dot_token: token,
1569 member: member,
1570 }.into();
1571 })
1572 |
David Tolnay9389c382018-08-27 09:13:37 -07001573 tap!(i: shim!(and_index) => {
Michael Layzell734adb42017-06-07 16:58:31 -04001574 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001575 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001576 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001577 bracket_token: i.0,
1578 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001579 }.into();
1580 })
1581 )) >>
1582 (e)
1583 ));
1584
David Tolnaya454c8f2018-01-07 01:01:10 -08001585 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001586 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001587 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001588 named2!(atom_expr(allow_struct: AllowStruct, allow_block: AllowBlock) -> Expr, alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001589 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001590 |
David Tolnay8c91b882017-12-28 23:04:32 -05001591 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001592 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001593 // must be before ExprStruct
David Tolnay02a9c6f2018-08-24 18:58:45 -04001594 syn!(ExprAsync) => { Expr::Async }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001595 |
David Tolnayf7177052018-08-24 15:31:50 -04001596 // must be before ExprStruct
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001597 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnayf7177052018-08-24 15:31:50 -04001598 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001599 // must be before expr_path
David Tolnay9389c382018-08-27 09:13:37 -07001600 cond_reduce!(allow_struct.0, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001601 |
David Tolnay8c91b882017-12-28 23:04:32 -05001602 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001603 |
David Tolnay8c91b882017-12-28 23:04:32 -05001604 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001605 |
David Tolnay9389c382018-08-27 09:13:37 -07001606 shim!(expr_break, allow_struct) // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001607 |
David Tolnay8c91b882017-12-28 23:04:32 -05001608 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001609 |
David Tolnay9389c382018-08-27 09:13:37 -07001610 shim!(expr_ret, allow_struct) // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001611 |
David Tolnay8c91b882017-12-28 23:04:32 -05001612 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001613 |
David Tolnay8c91b882017-12-28 23:04:32 -05001614 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001615 |
David Tolnay8c91b882017-12-28 23:04:32 -05001616 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001617 |
David Tolnay8c91b882017-12-28 23:04:32 -05001618 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001619 |
David Tolnay8c91b882017-12-28 23:04:32 -05001620 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001621 |
David Tolnay8c91b882017-12-28 23:04:32 -05001622 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001623 |
David Tolnay8c91b882017-12-28 23:04:32 -05001624 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001625 |
David Tolnay8c91b882017-12-28 23:04:32 -05001626 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001627 |
David Tolnay8c91b882017-12-28 23:04:32 -05001628 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001629 |
David Tolnay8c91b882017-12-28 23:04:32 -05001630 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001631 |
David Tolnay8c91b882017-12-28 23:04:32 -05001632 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001633 |
David Tolnay9389c382018-08-27 09:13:37 -07001634 shim!(expr_closure, allow_struct)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001635 |
David Tolnay9389c382018-08-27 09:13:37 -07001636 cond_reduce!(allow_block.0, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001637 |
1638 // NOTE: This is the prefix-form of range
David Tolnay9389c382018-08-27 09:13:37 -07001639 shim!(expr_range, allow_struct)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001640 |
David Tolnay8c91b882017-12-28 23:04:32 -05001641 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001642 |
David Tolnay8c91b882017-12-28 23:04:32 -05001643 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001644 ));
1645
Michael Layzell734adb42017-06-07 16:58:31 -04001646 #[cfg(not(feature = "full"))]
David Tolnay9389c382018-08-27 09:13:37 -07001647 named2!(atom_expr(_allow_struct: AllowStruct, _allow_block: AllowBlock) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001648 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001649 |
David Tolnay9374bc02018-01-27 18:49:36 -08001650 syn!(ExprParen) => { Expr::Paren }
1651 |
David Tolnay8c91b882017-12-28 23:04:32 -05001652 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001653 ));
1654
Michael Layzell734adb42017-06-07 16:58:31 -04001655 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001656 named2!(expr_nosemi -> Expr, do_parse!(
David Tolnay313a36f2018-04-29 20:13:04 -07001657 nosemi: alt!(
1658 syn!(ExprIf) => { Expr::If }
1659 |
1660 syn!(ExprIfLet) => { Expr::IfLet }
1661 |
1662 syn!(ExprWhile) => { Expr::While }
1663 |
1664 syn!(ExprWhileLet) => { Expr::WhileLet }
1665 |
1666 syn!(ExprForLoop) => { Expr::ForLoop }
1667 |
1668 syn!(ExprLoop) => { Expr::Loop }
1669 |
1670 syn!(ExprMatch) => { Expr::Match }
1671 |
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001672 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnay313a36f2018-04-29 20:13:04 -07001673 |
1674 syn!(ExprYield) => { Expr::Yield }
1675 |
1676 syn!(ExprUnsafe) => { Expr::Unsafe }
1677 |
1678 syn!(ExprBlock) => { Expr::Block }
1679 ) >>
1680 // If the next token is a `.` or a `?` it is special-cased to parse
1681 // as an expression instead of a blockexpression.
1682 not!(punct!(.)) >>
1683 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001684 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001685 ));
Michael Layzell35418782017-06-07 09:20:25 -04001686
David Tolnay8c91b882017-12-28 23:04:32 -05001687 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001688 #[cfg(not(feature = "full"))]
1689 named!(parse -> Self, do_parse!(
1690 lit: syn!(Lit) >>
1691 (ExprLit {
1692 attrs: Vec::new(),
1693 lit: lit,
1694 })
1695 ));
1696
1697 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001698 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001699 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001700 lit: syn!(Lit) >>
1701 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001702 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001703 lit: lit,
1704 })
1705 ));
1706 }
1707
1708 #[cfg(feature = "full")]
1709 impl Synom for ExprMacro {
1710 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001711 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001712 mac: syn!(Macro) >>
1713 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001714 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001715 mac: mac,
1716 })
1717 ));
1718 }
1719
David Tolnaye98775f2017-12-28 23:17:00 -05001720 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001721 impl Synom for ExprGroup {
1722 named!(parse -> Self, do_parse!(
David Tolnaya7d69fc2018-08-26 13:30:24 -04001723 e: old_grouped!(syn!(Expr)) >>
Michael Layzell93c36282017-06-04 20:43:14 -04001724 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001725 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001726 expr: Box::new(e.1),
1727 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001728 })
Michael Layzell93c36282017-06-04 20:43:14 -04001729 ));
1730 }
1731
Alex Crichton954046c2017-05-30 21:49:42 -07001732 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001733 #[cfg(not(feature = "full"))]
1734 named!(parse -> Self, do_parse!(
1735 e: parens!(syn!(Expr)) >>
1736 (ExprParen {
1737 attrs: Vec::new(),
1738 paren_token: e.0,
1739 expr: Box::new(e.1),
1740 })
1741 ));
1742
1743 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001744 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001745 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001746 e: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001747 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001748 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001749 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001750 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001751 attrs: {
1752 let mut attrs = outer_attrs;
1753 attrs.extend((e.1).0);
1754 attrs
1755 },
David Tolnay8875fca2017-12-31 13:52:37 -05001756 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001757 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001758 })
Michael Layzell92639a52017-06-01 00:07:44 -04001759 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001760 }
David Tolnay89e05672016-10-02 14:39:42 -07001761
Michael Layzell734adb42017-06-07 16:58:31 -04001762 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001763 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001764 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001765 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001766 elems: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001767 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001768 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001769 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001770 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001771 attrs: {
1772 let mut attrs = outer_attrs;
1773 attrs.extend((elems.1).0);
1774 attrs
1775 },
David Tolnay8875fca2017-12-31 13:52:37 -05001776 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001777 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001778 })
1779 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001780 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001781
David Tolnay9389c382018-08-27 09:13:37 -07001782 named2!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001783 parens!(Punctuated::parse_terminated)
1784 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001785
Michael Layzell734adb42017-06-07 16:58:31 -04001786 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001787 named2!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001788 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001789 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001790 turbofish: option!(tuple!(
1791 punct!(::),
1792 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001793 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001794 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001795 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001796 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001797 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001798 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001799 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001800 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001801 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001802 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001803 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001804
Alex Crichton954046c2017-05-30 21:49:42 -07001805 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001806 turbofish: turbofish.map(|fish| MethodTurbofish {
1807 colon2_token: fish.0,
1808 lt_token: fish.1,
1809 args: fish.2,
1810 gt_token: fish.3,
1811 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001812 args: args.1,
1813 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001814 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001815 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001816 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001817 ));
1818
Michael Layzell734adb42017-06-07 16:58:31 -04001819 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001820 impl Synom for GenericMethodArgument {
1821 // TODO parse const generics as well
1822 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1823 }
1824
1825 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001826 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001827 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001828 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001829 elems: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001830 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001831 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001832 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001833 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001834 attrs: {
1835 let mut attrs = outer_attrs;
1836 attrs.extend((elems.1).0);
1837 attrs
1838 },
1839 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001840 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001841 })
1842 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001843 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001844
Michael Layzell734adb42017-06-07 16:58:31 -04001845 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001846 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001847 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001848 if_: keyword!(if) >>
1849 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001850 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001851 eq: punct!(=) >>
David Tolnay9389c382018-08-27 09:13:37 -07001852 cond: shim!(expr_no_struct) >>
1853 then_block: braces!(Block::old_parse_within) >>
1854 else_block: option!(shim!(else_block)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001855 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001856 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001857 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001858 let_token: let_,
1859 eq_token: eq,
1860 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001861 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001862 brace_token: then_block.0,
1863 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001864 },
1865 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001866 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001867 })
1868 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001869 }
1870
Michael Layzell734adb42017-06-07 16:58:31 -04001871 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001872 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001873 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001874 if_: keyword!(if) >>
David Tolnay9389c382018-08-27 09:13:37 -07001875 cond: shim!(expr_no_struct) >>
1876 then_block: braces!(Block::old_parse_within) >>
1877 else_block: option!(shim!(else_block)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001878 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001879 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001880 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001881 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001882 brace_token: then_block.0,
1883 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001884 },
1885 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001886 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001887 })
1888 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001889 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001890
Michael Layzell734adb42017-06-07 16:58:31 -04001891 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07001892 named2!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001893 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001894 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001895 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001896 |
David Tolnay8c91b882017-12-28 23:04:32 -05001897 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001898 |
1899 do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07001900 else_block: braces!(Block::old_parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001901 (Expr::Block(ExprBlock {
1902 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04001903 label: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001904 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001905 brace_token: else_block.0,
1906 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001907 },
1908 }))
David Tolnay939766a2016-09-23 23:48:12 -07001909 )
Alex Crichton954046c2017-05-30 21:49:42 -07001910 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001911 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001912 ));
1913
Michael Layzell734adb42017-06-07 16:58:31 -04001914 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001915 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001916 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001917 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001918 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001919 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001920 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001921 in_: keyword!(in) >>
David Tolnay9389c382018-08-27 09:13:37 -07001922 expr: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001923 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001924 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07001925 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001926 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001927 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001928 attrs: {
1929 let mut attrs = outer_attrs;
1930 attrs.extend((block.1).0);
1931 attrs
1932 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001933 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001934 for_token: for_,
1935 pat: Box::new(pat),
1936 in_token: in_,
1937 expr: Box::new(expr),
1938 body: Block {
1939 brace_token: block.0,
1940 stmts: (block.1).1,
1941 },
Michael Layzell92639a52017-06-01 00:07:44 -04001942 })
1943 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001944 }
Gregory Katze5f35682016-09-27 14:20:55 -04001945
Michael Layzell734adb42017-06-07 16:58:31 -04001946 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001947 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001948 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001949 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001950 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001951 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001952 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001953 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07001954 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001955 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001956 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001957 attrs: {
1958 let mut attrs = outer_attrs;
1959 attrs.extend((block.1).0);
1960 attrs
1961 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001962 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001963 loop_token: loop_,
1964 body: Block {
1965 brace_token: block.0,
1966 stmts: (block.1).1,
1967 },
Michael Layzell92639a52017-06-01 00:07:44 -04001968 })
1969 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001970 }
1971
Michael Layzell734adb42017-06-07 16:58:31 -04001972 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001973 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001974 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001975 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001976 match_: keyword!(match) >>
David Tolnay9389c382018-08-27 09:13:37 -07001977 obj: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001978 braced_content: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001979 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001980 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07001981 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001982 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07001983 attrs: {
1984 let mut attrs = outer_attrs;
1985 attrs.extend((braced_content.1).0);
1986 attrs
1987 },
David Tolnay8875fca2017-12-31 13:52:37 -05001988 expr: Box::new(obj),
1989 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07001990 brace_token: braced_content.0,
1991 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001992 })
1993 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001994 }
David Tolnay1978c672016-10-27 22:05:52 -07001995
Michael Layzell734adb42017-06-07 16:58:31 -04001996 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001997 impl Synom for ExprTryBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001998 named!(parse -> Self, do_parse!(
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001999 try_token: keyword!(try) >>
2000 block: syn!(Block) >>
2001 (ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002002 attrs: Vec::new(),
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002003 try_token: try_token,
2004 block: block,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002005 })
Michael Layzell92639a52017-06-01 00:07:44 -04002006 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002007 }
Arnavion02ef13f2017-04-25 00:54:31 -07002008
Michael Layzell734adb42017-06-07 16:58:31 -04002009 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002010 impl Synom for ExprYield {
2011 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002012 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002013 expr: option!(syn!(Expr)) >>
2014 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002015 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002016 yield_token: yield_,
2017 expr: expr.map(Box::new),
2018 })
2019 ));
2020 }
2021
2022 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002023 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002024 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002025 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002026 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002027 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002028 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002029 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002030 body: do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002031 expr: alt!(shim!(expr_nosemi) | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002032 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2033 true => alt!(
2034 input_end!() => { |_| None }
2035 |
2036 punct!(,) => { Some }
2037 )
Alex Crichton03b30272017-08-28 09:35:24 -07002038 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002039 false => option!(punct!(,))
2040 ) >>
2041 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002042 ) >>
2043 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002044 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002045 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002046 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002047 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002048 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002049 body: Box::new(body.0),
2050 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002051 })
2052 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002053 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002054
Michael Layzell734adb42017-06-07 16:58:31 -04002055 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002056 named2!(expr_closure(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002057 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002058 asyncness: option!(keyword!(async)) >>
2059 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002060 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002061 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002062 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002063 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002064 ret_and_body: alt!(
2065 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002066 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002067 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002068 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002069 (
2070 ReturnType::Type(arrow, Box::new(ty)),
2071 Expr::Block(ExprBlock {
2072 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04002073 label: None,
David Tolnay76178be2018-07-31 23:06:15 -07002074 block: body,
2075 },
2076 ))
David Tolnay89e05672016-10-02 14:39:42 -07002077 )
2078 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002079 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002080 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002081 (Expr::Closure(ExprClosure {
2082 attrs: attrs,
2083 asyncness: asyncness,
2084 movability: movability,
2085 capture: capture,
2086 or1_token: or1,
2087 inputs: inputs,
2088 or2_token: or2,
2089 output: ret_and_body.0,
2090 body: Box::new(ret_and_body.1),
2091 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002092 ));
2093
2094 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04002095 impl Synom for ExprAsync {
2096 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002097 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay02a9c6f2018-08-24 18:58:45 -04002098 async_token: keyword!(async) >>
2099 capture: option!(keyword!(move)) >>
2100 block: syn!(Block) >>
2101 (ExprAsync {
2102 attrs: attrs,
2103 async_token: async_token,
2104 capture: capture,
2105 block: block,
2106 })
2107 ));
2108 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002109
2110 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002111 named!(fn_arg -> FnArg, do_parse!(
2112 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002113 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002114 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002115 if let Some((colon, ty)) = ty {
2116 FnArg::Captured(ArgCaptured {
2117 pat: pat,
2118 colon_token: colon,
2119 ty: ty,
2120 })
2121 } else {
2122 FnArg::Inferred(pat)
2123 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002124 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002125 ));
2126
Michael Layzell734adb42017-06-07 16:58:31 -04002127 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002128 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002129 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002130 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002131 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002132 while_: keyword!(while) >>
David Tolnay9389c382018-08-27 09:13:37 -07002133 cond: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002134 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002135 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002136 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002137 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002138 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002139 attrs: {
2140 let mut attrs = outer_attrs;
2141 attrs.extend((block.1).0);
2142 attrs
2143 },
2144 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002145 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002146 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002147 body: Block {
2148 brace_token: block.0,
2149 stmts: (block.1).1,
2150 },
Michael Layzell92639a52017-06-01 00:07:44 -04002151 })
2152 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002153 }
2154
Michael Layzell734adb42017-06-07 16:58:31 -04002155 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002156 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002157 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002158 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002159 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002160 while_: keyword!(while) >>
2161 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002162 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002163 eq: punct!(=) >>
David Tolnay9389c382018-08-27 09:13:37 -07002164 value: shim!(expr_no_struct) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002165 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002166 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002167 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002168 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002169 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002170 attrs: {
2171 let mut attrs = outer_attrs;
2172 attrs.extend((block.1).0);
2173 attrs
2174 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002175 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002176 while_token: while_,
2177 let_token: let_,
2178 pats: pats,
2179 eq_token: eq,
2180 expr: Box::new(value),
2181 body: Block {
2182 brace_token: block.0,
2183 stmts: (block.1).1,
2184 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002185 })
2186 ));
2187 }
2188
2189 #[cfg(feature = "full")]
2190 impl Synom for Label {
2191 named!(parse -> Self, do_parse!(
2192 name: syn!(Lifetime) >>
2193 colon: punct!(:) >>
2194 (Label {
2195 name: name,
2196 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002197 })
2198 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002199 }
2200
Michael Layzell734adb42017-06-07 16:58:31 -04002201 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002202 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002203 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002204 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002205 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002206 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002207 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002208 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002209 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002210 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002211 })
2212 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002213 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002214
Michael Layzell734adb42017-06-07 16:58:31 -04002215 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002216 named2!(expr_break(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002217 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002218 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002219 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002220 // We can't allow blocks after a `break` expression when we wouldn't
2221 // allow structs, as this expression is ambiguous.
2222 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002223 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002224 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002225 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002226 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002227 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002228 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002229 ));
2230
Michael Layzell734adb42017-06-07 16:58:31 -04002231 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002232 named2!(expr_ret(allow_struct: AllowStruct) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002233 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002234 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002235 // NOTE: return is greedy and eats blocks after it even when in a
2236 // position where structs are not allowed, such as in if statement
2237 // conditions. For example:
2238 //
David Tolnaybcf26022017-12-25 22:10:52 -05002239 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002240 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002241 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002242 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002243 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002244 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002245 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002246 ));
2247
Michael Layzell734adb42017-06-07 16:58:31 -04002248 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002249 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002250 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002251 outer_attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002252 path: syn!(Path) >>
2253 data: braces!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002254 inner_attrs: many0!(Attribute::old_parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002255 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002256 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2257 dots: punct!(..) >>
2258 base: syn!(Expr) >>
2259 (dots, base)
2260 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002261 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002262 )) >>
2263 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002264 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002265 let (dots, rest) = match base.and_then(|b| b) {
2266 Some((dots, base)) => (Some(dots), Some(base)),
2267 None => (None, None),
2268 };
2269 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002270 attrs: {
2271 let mut attrs = outer_attrs;
2272 attrs.extend(inner_attrs);
2273 attrs
2274 },
Michael Layzell92639a52017-06-01 00:07:44 -04002275 brace_token: brace,
2276 path: path,
2277 fields: fields,
2278 dot2_token: dots,
2279 rest: rest.map(Box::new),
2280 }
2281 })
2282 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002283 }
2284
Michael Layzell734adb42017-06-07 16:58:31 -04002285 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002286 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002287 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002288 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayc42b90a2018-01-18 23:11:37 -08002289 field_value: alt!(
2290 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2291 |
2292 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002293 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002294 None,
2295 Expr::Path(ExprPath {
2296 attrs: Vec::new(),
2297 qself: None,
2298 path: name.into(),
2299 }),
2300 ))
2301 ) >>
2302 (FieldValue {
2303 attrs: attrs,
2304 member: field_value.0,
2305 colon_token: field_value.1,
2306 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002307 })
2308 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002309 }
David Tolnay055a7042016-10-02 19:23:54 -07002310
Michael Layzell734adb42017-06-07 16:58:31 -04002311 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002312 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002313 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002314 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002315 data: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002316 many0!(Attribute::old_parse_inner),
David Tolnay5d314dc2018-07-21 16:40:01 -07002317 syn!(Expr),
2318 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002319 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002320 )) >>
2321 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002322 attrs: {
2323 let mut attrs = outer_attrs;
2324 attrs.extend((data.1).0);
2325 attrs
2326 },
2327 expr: Box::new((data.1).1),
2328 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002329 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002330 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002331 })
2332 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002333 }
David Tolnay055a7042016-10-02 19:23:54 -07002334
Michael Layzell734adb42017-06-07 16:58:31 -04002335 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002336 impl Synom for ExprUnsafe {
2337 named!(parse -> Self, do_parse!(
David Tolnay8b493772018-08-27 06:30:18 -07002338 outer_attrs: many0!(Attribute::old_parse_outer) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002339 unsafe_: keyword!(unsafe) >>
David Tolnayc4be3512018-08-27 06:25:44 -07002340 block: braces!(tuple!(
David Tolnay8b493772018-08-27 06:30:18 -07002341 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002342 shim!(Block::parse_within),
David Tolnayc4be3512018-08-27 06:25:44 -07002343 )) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002344 (ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002345 attrs: {
2346 let mut attrs = outer_attrs;
2347 attrs.extend((block.1).0);
2348 attrs
2349 },
Nika Layzell640832a2017-12-04 13:37:09 -05002350 unsafe_token: unsafe_,
David Tolnayc4be3512018-08-27 06:25:44 -07002351 block: Block {
2352 brace_token: block.0,
2353 stmts: (block.1).1,
2354 },
Nika Layzell640832a2017-12-04 13:37:09 -05002355 })
2356 ));
2357 }
2358
2359 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002360 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002361 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002362 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay1d8e9962018-08-24 19:04:20 -04002363 label: option!(syn!(Label)) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002364 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002365 many0!(Attribute::old_parse_inner),
David Tolnay9389c382018-08-27 09:13:37 -07002366 shim!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002367 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002368 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002369 attrs: {
2370 let mut attrs = outer_attrs;
2371 attrs.extend((block.1).0);
2372 attrs
2373 },
David Tolnay1d8e9962018-08-24 19:04:20 -04002374 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002375 block: Block {
2376 brace_token: block.0,
2377 stmts: (block.1).1,
2378 },
Michael Layzell92639a52017-06-01 00:07:44 -04002379 })
2380 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002381 }
David Tolnay89e05672016-10-02 14:39:42 -07002382
Michael Layzell734adb42017-06-07 16:58:31 -04002383 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002384 named2!(expr_range(allow_struct: AllowStruct) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002385 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002386 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002387 (ExprRange {
2388 attrs: Vec::new(),
2389 from: None,
2390 to: hi.map(Box::new),
2391 limits: limits,
2392 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002393 ));
2394
Michael Layzell734adb42017-06-07 16:58:31 -04002395 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002396 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002397 named!(parse -> Self, alt!(
2398 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002399 punct!(..=) => { RangeLimits::Closed }
2400 |
2401 // Must come before Dot2
David Tolnay7ac699c2018-08-24 14:00:58 -04002402 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.spans)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002403 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002404 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002405 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002406 }
David Tolnay438c9052016-10-07 23:24:48 -07002407
Alex Crichton954046c2017-05-30 21:49:42 -07002408 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002409 #[cfg(not(feature = "full"))]
2410 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002411 pair: shim!(qpath) >>
David Tolnayeb981bb2018-07-21 19:31:38 -07002412 (ExprPath {
2413 attrs: Vec::new(),
2414 qself: pair.0,
2415 path: pair.1,
2416 })
2417 ));
2418
2419 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002420 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002421 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay9389c382018-08-27 09:13:37 -07002422 pair: shim!(qpath) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002423 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002424 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002425 qself: pair.0,
2426 path: pair.1,
2427 })
2428 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002429 }
David Tolnay42602292016-10-01 22:25:45 -07002430
David Tolnay9389c382018-08-27 09:13:37 -07002431 named2!(path -> Path, do_parse!(
David Tolnay9cc2f092018-08-24 15:51:37 -04002432 colon: option!(punct!(::)) >>
2433 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2434 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2435 (Path {
2436 leading_colon: colon,
2437 segments: segments,
2438 })
2439 ));
2440
2441 named!(path_segment -> PathSegment, alt!(
2442 do_parse!(
2443 ident: syn!(Ident) >>
2444 colon2: punct!(::) >>
2445 lt: punct!(<) >>
2446 args: call!(Punctuated::parse_terminated) >>
2447 gt: punct!(>) >>
2448 (PathSegment {
2449 ident: ident,
2450 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2451 colon2_token: Some(colon2),
2452 lt_token: lt,
2453 args: args,
2454 gt_token: gt,
2455 }),
2456 })
2457 )
2458 |
David Tolnaya7d69fc2018-08-26 13:30:24 -04002459 old_mod_style_path_segment
David Tolnay9cc2f092018-08-24 15:51:37 -04002460 ));
2461
David Tolnay9389c382018-08-27 09:13:37 -07002462 named2!(qpath -> (Option<QSelf>, Path), alt!(
2463 map!(shim!(path), |p| (None, p))
David Tolnay9cc2f092018-08-24 15:51:37 -04002464 |
2465 do_parse!(
2466 lt: punct!(<) >>
2467 this: syn!(Type) >>
2468 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2469 gt: punct!(>) >>
2470 colon2: punct!(::) >>
2471 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2472 ({
2473 let (pos, as_, path) = match path {
2474 Some((as_, mut path)) => {
2475 let pos = path.segments.len();
2476 path.segments.push_punct(colon2);
2477 path.segments.extend(rest.into_pairs());
2478 (pos, Some(as_), path)
2479 }
2480 None => {
2481 (0, None, Path {
2482 leading_colon: Some(colon2),
2483 segments: rest,
2484 })
2485 }
2486 };
2487 (Some(QSelf {
2488 lt_token: lt,
2489 ty: Box::new(this),
2490 position: pos,
2491 as_token: as_,
2492 gt_token: gt,
2493 }), path)
2494 })
2495 )
2496 |
2497 map!(keyword!(self), |s| (None, s.into()))
2498 ));
2499
David Tolnay9389c382018-08-27 09:13:37 -07002500 named2!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002501
David Tolnay9389c382018-08-27 09:13:37 -07002502 named2!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002503
Michael Layzell734adb42017-06-07 16:58:31 -04002504 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002505 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002506 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002507 stmts: braces!(Block::old_parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002508 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002509 brace_token: stmts.0,
2510 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002511 })
2512 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002513 }
David Tolnay939766a2016-09-23 23:48:12 -07002514
Michael Layzell734adb42017-06-07 16:58:31 -04002515 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002516 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002517 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
2518 input.step_cursor(|cursor| Self::old_parse_within(*cursor))
2519 }
2520
2521 named!(old_parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002522 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002523 mut standalone: many0!(do_parse!(
2524 stmt: syn!(Stmt) >>
2525 many0!(punct!(;)) >>
2526 (stmt)
2527 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002528 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002529 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002530 mut e: syn!(Expr) >>
2531 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002532 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002533 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002534 })
2535 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002536 (match last {
2537 None => standalone,
2538 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002539 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002540 standalone
2541 }
2542 })
2543 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002544 }
2545
Michael Layzell734adb42017-06-07 16:58:31 -04002546 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002547 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002548 named!(parse -> Self, alt!(
David Tolnay9389c382018-08-27 09:13:37 -07002549 shim!(stmt_mac)
Michael Layzell92639a52017-06-01 00:07:44 -04002550 |
David Tolnay9389c382018-08-27 09:13:37 -07002551 shim!(stmt_local)
Michael Layzell92639a52017-06-01 00:07:44 -04002552 |
David Tolnay9389c382018-08-27 09:13:37 -07002553 shim!(stmt_item)
Michael Layzell92639a52017-06-01 00:07:44 -04002554 |
David Tolnay9389c382018-08-27 09:13:37 -07002555 shim!(stmt_blockexpr)
Michael Layzell35418782017-06-07 09:20:25 -04002556 |
David Tolnay9389c382018-08-27 09:13:37 -07002557 shim!(stmt_expr)
Michael Layzell92639a52017-06-01 00:07:44 -04002558 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002559 }
David Tolnay939766a2016-09-23 23:48:12 -07002560
Michael Layzell734adb42017-06-07 16:58:31 -04002561 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002562 named2!(stmt_mac -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002563 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaya7d69fc2018-08-26 13:30:24 -04002564 what: call!(Path::old_parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002565 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002566 // Only parse braces here; paren and bracket will get parsed as
2567 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002568 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002569 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002570 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002571 attrs: attrs,
2572 ident: None,
2573 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002574 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002575 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002576 delimiter: MacroDelimiter::Brace(data.0),
2577 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002578 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002579 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002580 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002581 ));
2582
Michael Layzell734adb42017-06-07 16:58:31 -04002583 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002584 named2!(stmt_local -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002585 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002586 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002587 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002588 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002589 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2590 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002591 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002592 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002593 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002594 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002595 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2596 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2597 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002598 }))
David Tolnay191e0582016-10-02 18:31:09 -07002599 ));
2600
Michael Layzell734adb42017-06-07 16:58:31 -04002601 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002602 named2!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002603
Michael Layzell734adb42017-06-07 16:58:31 -04002604 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002605 named2!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002606 mut attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay9389c382018-08-27 09:13:37 -07002607 mut e: shim!(expr_nosemi) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002608 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002609 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002610 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002611 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002612 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002613 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002614 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002615 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002616 }
2617 })
2618 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002619
Michael Layzell734adb42017-06-07 16:58:31 -04002620 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002621 named2!(stmt_expr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002622 mut attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002623 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002624 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002625 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002626 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002627 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002628 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002629 })
David Tolnay939766a2016-09-23 23:48:12 -07002630 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002631
Michael Layzell734adb42017-06-07 16:58:31 -04002632 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002633 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002634 named!(parse -> Self, alt!(
2635 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2636 |
2637 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2638 |
2639 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2640 |
2641 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2642 |
2643 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2644 |
David Tolnay323279a2017-12-29 11:26:32 -05002645 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002646 |
2647 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2648 |
2649 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2650 |
2651 syn!(PatPath) => { Pat::Path }
2652 |
2653 syn!(PatTuple) => { Pat::Tuple }
2654 |
2655 syn!(PatRef) => { Pat::Ref }
2656 |
2657 syn!(PatSlice) => { Pat::Slice }
2658 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002659 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002660
Michael Layzell734adb42017-06-07 16:58:31 -04002661 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002662 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002663 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002664 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002665 |u| PatWild { underscore_token: u }
2666 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002667 }
David Tolnay84aa0752016-10-02 23:01:13 -07002668
Michael Layzell734adb42017-06-07 16:58:31 -04002669 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002670 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002671 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002672 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002673 pat: syn!(Pat) >>
2674 (PatBox {
2675 pat: Box::new(pat),
2676 box_token: boxed,
2677 })
2678 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002679 }
2680
Michael Layzell734adb42017-06-07 16:58:31 -04002681 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002682 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002683 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002684 by_ref: option!(keyword!(ref)) >>
2685 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002686 name: alt!(
2687 syn!(Ident)
2688 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002689 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002690 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002691 not!(punct!(<)) >>
2692 not!(punct!(::)) >>
2693 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002694 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002695 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002696 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002697 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002698 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002699 })
2700 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002701 }
2702
Michael Layzell734adb42017-06-07 16:58:31 -04002703 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002704 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002705 named!(parse -> Self, do_parse!(
2706 path: syn!(Path) >>
2707 tuple: syn!(PatTuple) >>
2708 (PatTupleStruct {
2709 path: path,
2710 pat: tuple,
2711 })
2712 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002713 }
2714
Michael Layzell734adb42017-06-07 16:58:31 -04002715 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002716 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002717 named!(parse -> Self, do_parse!(
2718 path: syn!(Path) >>
2719 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002720 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002721 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002722 (fields, base)
2723 )) >>
2724 (PatStruct {
2725 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002726 fields: (data.1).0,
2727 brace_token: data.0,
2728 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002729 })
2730 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002731 }
2732
Michael Layzell734adb42017-06-07 16:58:31 -04002733 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002734 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002735 named!(parse -> Self, alt!(
2736 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002737 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002738 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002739 pat: syn!(Pat) >>
2740 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002741 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002742 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002743 attrs: Vec::new(),
2744 colon_token: Some(colon),
2745 })
2746 )
2747 |
2748 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002749 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002750 by_ref: option!(keyword!(ref)) >>
2751 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002752 ident: syn!(Ident) >>
2753 ({
2754 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002755 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002756 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002757 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002758 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002759 }.into();
2760 if let Some(boxed) = boxed {
2761 pat = PatBox {
2762 pat: Box::new(pat),
2763 box_token: boxed,
2764 }.into();
2765 }
2766 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002767 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002768 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002769 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002770 colon_token: None,
2771 }
2772 })
2773 )
2774 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002775 }
2776
David Tolnay85b69a42017-12-27 20:43:10 -05002777 impl Synom for Member {
2778 named!(parse -> Self, alt!(
2779 syn!(Ident) => { Member::Named }
2780 |
2781 syn!(Index) => { Member::Unnamed }
2782 ));
2783 }
2784
David Tolnay85b69a42017-12-27 20:43:10 -05002785 impl Synom for Index {
2786 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002787 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002788 ({
David Tolnay360efd22018-01-04 23:35:26 -08002789 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002790 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002791 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002792 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002793 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002794 })
David Tolnay85b69a42017-12-27 20:43:10 -05002795 ));
2796 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002797
Michael Layzell734adb42017-06-07 16:58:31 -04002798 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002799 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002800 named!(parse -> Self, map!(
2801 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002802 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002803 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002804 }
David Tolnay9636c052016-10-02 17:11:17 -07002805
Michael Layzell734adb42017-06-07 16:58:31 -04002806 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002807 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002808 named!(parse -> Self, do_parse!(
2809 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002810 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002811 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2812 tuple!(punct!(..), option!(punct!(,)))
2813 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002814 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002815 Some((_, Some(_))) => true,
2816 _ => false,
2817 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002818 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002819 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002820 )) >>
2821 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002822 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002823 let (dotdot, trailing) = match dotdot {
2824 Some((a, b)) => (Some(a), Some(b)),
2825 None => (None, None),
2826 };
2827 PatTuple {
2828 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002829 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002830 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002831 comma_token: trailing.unwrap_or_default(),
2832 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002833 }
2834 })
2835 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002836 }
David Tolnayfbb73232016-10-03 01:00:06 -07002837
Michael Layzell734adb42017-06-07 16:58:31 -04002838 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002839 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002840 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002841 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002842 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002843 pat: syn!(Pat) >>
2844 (PatRef {
2845 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002846 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002847 and_token: and,
2848 })
2849 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002850 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002851
Michael Layzell734adb42017-06-07 16:58:31 -04002852 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002853 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002854 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002855 lit: shim!(pat_lit_expr) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002856 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002857 return parse_error(); // these need to be parsed by pat_path
2858 } else {
2859 PatLit {
2860 expr: Box::new(lit),
2861 }
2862 })
2863 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002864 }
David Tolnaye1310902016-10-29 23:40:00 -07002865
Michael Layzell734adb42017-06-07 16:58:31 -04002866 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002867 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002868 named!(parse -> Self, do_parse!(
David Tolnay9389c382018-08-27 09:13:37 -07002869 lo: shim!(pat_lit_expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002870 limits: syn!(RangeLimits) >>
David Tolnay9389c382018-08-27 09:13:37 -07002871 hi: shim!(pat_lit_expr) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002872 (PatRange {
2873 lo: Box::new(lo),
2874 hi: Box::new(hi),
2875 limits: limits,
2876 })
2877 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002878 }
David Tolnaye1310902016-10-29 23:40:00 -07002879
Michael Layzell734adb42017-06-07 16:58:31 -04002880 #[cfg(feature = "full")]
David Tolnay9389c382018-08-27 09:13:37 -07002881 named2!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002882 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002883 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002884 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002885 |
David Tolnay8c91b882017-12-28 23:04:32 -05002886 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002887 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002888 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002889 Expr::Unary(ExprUnary {
2890 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002891 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002892 expr: Box::new(v)
2893 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002894 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002895 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002896 })
2897 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002898
Michael Layzell734adb42017-06-07 16:58:31 -04002899 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002900 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002901 named!(parse -> Self, map!(
2902 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002903 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002904 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002905 dots: punct!(..) >>
2906 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002907 (dots, trailing)
2908 )) >>
2909 after: cond!(
2910 match middle {
2911 Some((_, ref trailing)) => trailing.is_some(),
2912 _ => false,
2913 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002914 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002915 ) >>
2916 (before, middle, after)
2917 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002918 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002919 let mut before: Punctuated<Pat, Token![,]> = before;
2920 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002921 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002922 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04002923 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04002924 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04002925 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04002926 }),
2927 bracket_token: brackets,
2928 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002929 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002930 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002931 } else {
David Tolnay56080682018-01-06 14:01:52 -08002932 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002933 }
2934 }),
2935 front: before,
2936 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002937 }
Alex Crichton954046c2017-05-30 21:49:42 -07002938 }
Michael Layzell92639a52017-06-01 00:07:44 -04002939 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002940 }
David Tolnay323279a2017-12-29 11:26:32 -05002941
2942 #[cfg(feature = "full")]
2943 impl Synom for PatMacro {
2944 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
2945 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002946}
2947
David Tolnayf4bbbd92016-09-23 14:41:55 -07002948#[cfg(feature = "printing")]
2949mod printing {
2950 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002951 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002952 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07002953 use proc_macro2::{Literal, TokenStream};
2954 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002955
David Tolnaybcf26022017-12-25 22:10:52 -05002956 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07002957 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002958 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07002959 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002960 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002961 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002962 e.to_tokens(tokens);
2963 });
2964 } else {
2965 e.to_tokens(tokens);
2966 }
2967 }
2968
David Tolnay8c91b882017-12-28 23:04:32 -05002969 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07002970 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05002971 tokens.append_all(attrs.outer());
2972 }
Michael Layzell734adb42017-06-07 16:58:31 -04002973
David Tolnayd997aef2018-07-21 18:42:31 -07002974 #[cfg(feature = "full")]
2975 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
2976 tokens.append_all(attrs.inner());
2977 }
2978
David Tolnay8c91b882017-12-28 23:04:32 -05002979 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07002980 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
2981
2982 #[cfg(not(feature = "full"))]
2983 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002984
Michael Layzell734adb42017-06-07 16:58:31 -04002985 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002986 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002987 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07002988 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002989 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002990 self.expr.to_tokens(tokens);
2991 }
2992 }
2993
Michael Layzell734adb42017-06-07 16:58:31 -04002994 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002995 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07002996 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07002997 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05002998 self.place.to_tokens(tokens);
2999 self.arrow_token.to_tokens(tokens);
3000 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003001 }
3002 }
3003
Michael Layzell734adb42017-06-07 16:58:31 -04003004 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003005 impl ToTokens for ExprArray {
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.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003009 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003010 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003012 }
3013 }
3014
3015 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003016 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003017 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003018 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003019 self.paren_token.surround(tokens, |tokens| {
3020 self.args.to_tokens(tokens);
3021 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003022 }
3023 }
3024
Michael Layzell734adb42017-06-07 16:58:31 -04003025 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003026 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003027 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003028 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003029 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003030 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003031 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003032 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003033 self.paren_token.surround(tokens, |tokens| {
3034 self.args.to_tokens(tokens);
3035 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003036 }
3037 }
3038
Michael Layzell734adb42017-06-07 16:58:31 -04003039 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003040 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003041 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003042 self.colon2_token.to_tokens(tokens);
3043 self.lt_token.to_tokens(tokens);
3044 self.args.to_tokens(tokens);
3045 self.gt_token.to_tokens(tokens);
3046 }
3047 }
3048
3049 #[cfg(feature = "full")]
3050 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003051 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003052 match *self {
3053 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3054 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3055 }
3056 }
3057 }
3058
3059 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003060 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003061 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003062 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003063 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003064 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003065 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003066 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003067 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003068 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003069 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003070 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003071 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003072 }
3073 }
3074
3075 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003076 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003077 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003078 self.left.to_tokens(tokens);
3079 self.op.to_tokens(tokens);
3080 self.right.to_tokens(tokens);
3081 }
3082 }
3083
3084 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003085 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003086 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003087 self.op.to_tokens(tokens);
3088 self.expr.to_tokens(tokens);
3089 }
3090 }
3091
David Tolnay8c91b882017-12-28 23:04:32 -05003092 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003093 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003094 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003095 self.lit.to_tokens(tokens);
3096 }
3097 }
3098
Alex Crichton62a0a592017-05-22 13:58:53 -07003099 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003100 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003101 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003102 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003103 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003104 self.ty.to_tokens(tokens);
3105 }
3106 }
3107
David Tolnay0cf94f22017-12-28 23:46:26 -05003108 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003109 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003110 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003111 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003112 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003113 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003114 self.ty.to_tokens(tokens);
3115 }
3116 }
3117
Michael Layzell734adb42017-06-07 16:58:31 -04003118 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003119 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003120 if let Some((ref else_token, ref else_)) = *else_ {
3121 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003122
3123 // If we are not one of the valid expressions to exist in an else
3124 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003125 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003126 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003127 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003128 }
3129 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003130 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003131 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003132 });
3133 }
3134 }
3135 }
3136 }
3137
3138 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003139 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003140 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003141 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003142 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003143 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003144 self.then_branch.to_tokens(tokens);
3145 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003146 }
3147 }
3148
Michael Layzell734adb42017-06-07 16:58:31 -04003149 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003150 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003151 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003152 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003153 self.if_token.to_tokens(tokens);
3154 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003155 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003156 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003157 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003158 self.then_branch.to_tokens(tokens);
3159 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003160 }
3161 }
3162
Michael Layzell734adb42017-06-07 16:58:31 -04003163 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003164 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003165 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003166 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003167 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003168 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003169 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003170 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003171 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003172 tokens.append_all(&self.body.stmts);
3173 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003174 }
3175 }
3176
Michael Layzell734adb42017-06-07 16:58:31 -04003177 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003178 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003179 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003180 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003181 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003182 self.while_token.to_tokens(tokens);
3183 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003184 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003185 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003186 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003187 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003188 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003189 tokens.append_all(&self.body.stmts);
3190 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003191 }
3192 }
3193
Michael Layzell734adb42017-06-07 16:58:31 -04003194 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003195 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003196 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003197 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003198 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003199 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003200 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003201 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003202 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003203 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003204 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003205 tokens.append_all(&self.body.stmts);
3206 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003207 }
3208 }
3209
Michael Layzell734adb42017-06-07 16:58:31 -04003210 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003211 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003212 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003213 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003214 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003215 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003216 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003217 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003218 tokens.append_all(&self.body.stmts);
3219 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003220 }
3221 }
3222
Michael Layzell734adb42017-06-07 16:58:31 -04003223 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003224 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003225 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003226 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003227 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003228 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003229 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003230 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003231 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003232 arm.to_tokens(tokens);
3233 // Ensure that we have a comma after a non-block arm, except
3234 // for the last one.
3235 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003236 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003237 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003238 }
3239 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003240 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003241 }
3242 }
3243
Michael Layzell734adb42017-06-07 16:58:31 -04003244 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003245 impl ToTokens for ExprAsync {
3246 fn to_tokens(&self, tokens: &mut TokenStream) {
3247 outer_attrs_to_tokens(&self.attrs, tokens);
3248 self.async_token.to_tokens(tokens);
3249 self.capture.to_tokens(tokens);
3250 self.block.to_tokens(tokens);
3251 }
3252 }
3253
3254 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003255 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003256 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003257 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003258 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003259 self.block.to_tokens(tokens);
3260 }
3261 }
3262
Michael Layzell734adb42017-06-07 16:58:31 -04003263 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003264 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003265 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003266 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003267 self.yield_token.to_tokens(tokens);
3268 self.expr.to_tokens(tokens);
3269 }
3270 }
3271
3272 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003273 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003274 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003275 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003276 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003277 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003278 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003279 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003280 for input in self.inputs.pairs() {
3281 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003282 FnArg::Captured(ArgCaptured {
3283 ref pat,
3284 ty: Type::Infer(_),
3285 ..
3286 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003287 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003288 }
David Tolnay56080682018-01-06 14:01:52 -08003289 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003290 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003291 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003292 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003293 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003294 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003295 self.body.to_tokens(tokens);
3296 }
3297 }
3298
Michael Layzell734adb42017-06-07 16:58:31 -04003299 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003300 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003301 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003302 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003303 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003304 self.block.brace_token.surround(tokens, |tokens| {
3305 inner_attrs_to_tokens(&self.attrs, tokens);
3306 tokens.append_all(&self.block.stmts);
3307 });
Nika Layzell640832a2017-12-04 13:37:09 -05003308 }
3309 }
3310
3311 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003312 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003313 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003314 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003315 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003316 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003317 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003318 tokens.append_all(&self.block.stmts);
3319 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003320 }
3321 }
3322
Michael Layzell734adb42017-06-07 16:58:31 -04003323 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003324 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003325 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003326 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003327 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003328 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003329 self.right.to_tokens(tokens);
3330 }
3331 }
3332
Michael Layzell734adb42017-06-07 16:58:31 -04003333 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003334 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003335 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003336 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003337 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003338 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003339 self.right.to_tokens(tokens);
3340 }
3341 }
3342
3343 impl ToTokens for ExprField {
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);
David Tolnay85b69a42017-12-27 20:43:10 -05003346 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003347 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003348 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003349 }
3350 }
3351
David Tolnay85b69a42017-12-27 20:43:10 -05003352 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003353 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003354 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003355 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003356 Member::Unnamed(ref index) => index.to_tokens(tokens),
3357 }
3358 }
3359 }
3360
David Tolnay85b69a42017-12-27 20:43:10 -05003361 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003362 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003363 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3364 lit.set_span(self.span);
3365 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003366 }
3367 }
3368
3369 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003370 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003371 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003372 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003373 self.bracket_token.surround(tokens, |tokens| {
3374 self.index.to_tokens(tokens);
3375 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003376 }
3377 }
3378
Michael Layzell734adb42017-06-07 16:58:31 -04003379 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003380 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003381 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003382 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003383 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003384 match self.limits {
3385 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3386 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3387 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003388 self.to.to_tokens(tokens);
3389 }
3390 }
3391
3392 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003393 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003394 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003395 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003396 }
3397 }
3398
Michael Layzell734adb42017-06-07 16:58:31 -04003399 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003400 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003401 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003402 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003403 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003404 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003405 self.expr.to_tokens(tokens);
3406 }
3407 }
3408
Michael Layzell734adb42017-06-07 16:58:31 -04003409 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003410 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003411 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003412 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003413 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003414 self.label.to_tokens(tokens);
3415 self.expr.to_tokens(tokens);
3416 }
3417 }
3418
Michael Layzell734adb42017-06-07 16:58:31 -04003419 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003420 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003421 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003422 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003423 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003424 self.label.to_tokens(tokens);
3425 }
3426 }
3427
Michael Layzell734adb42017-06-07 16:58:31 -04003428 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003429 impl ToTokens for ExprReturn {
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.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003433 self.expr.to_tokens(tokens);
3434 }
3435 }
3436
Michael Layzell734adb42017-06-07 16:58:31 -04003437 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003438 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003439 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003440 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003441 self.mac.to_tokens(tokens);
3442 }
3443 }
3444
3445 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003446 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003447 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003448 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003449 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003450 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003451 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003452 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003453 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003454 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003455 self.rest.to_tokens(tokens);
3456 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003457 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003458 }
3459 }
3460
Michael Layzell734adb42017-06-07 16:58:31 -04003461 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003462 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003463 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003464 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003465 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003466 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003467 self.expr.to_tokens(tokens);
3468 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003469 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003470 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003471 }
3472 }
3473
David Tolnaye98775f2017-12-28 23:17:00 -05003474 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003475 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003476 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003477 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003478 self.group_token.surround(tokens, |tokens| {
3479 self.expr.to_tokens(tokens);
3480 });
3481 }
3482 }
3483
Alex Crichton62a0a592017-05-22 13:58:53 -07003484 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003485 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003486 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003487 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003488 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003489 self.expr.to_tokens(tokens);
3490 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003491 }
3492 }
3493
Michael Layzell734adb42017-06-07 16:58:31 -04003494 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003495 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003496 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003497 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003498 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003499 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003500 }
3501 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003502
David Tolnay2ae520a2017-12-29 11:19:50 -05003503 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003504 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003505 self.tts.to_tokens(tokens);
3506 }
3507 }
3508
Michael Layzell734adb42017-06-07 16:58:31 -04003509 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003510 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003511 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003512 self.name.to_tokens(tokens);
3513 self.colon_token.to_tokens(tokens);
3514 }
3515 }
3516
3517 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003518 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003519 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003520 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003521 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003522 if let Some(ref colon_token) = self.colon_token {
3523 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003524 self.expr.to_tokens(tokens);
3525 }
David Tolnay055a7042016-10-02 19:23:54 -07003526 }
3527 }
3528
Michael Layzell734adb42017-06-07 16:58:31 -04003529 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003530 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003531 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003532 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003533 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003534 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003535 if let Some((ref if_token, ref guard)) = self.guard {
3536 if_token.to_tokens(tokens);
3537 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003538 }
David Tolnaydfb91432018-03-31 19:19:44 +02003539 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003540 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003541 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003542 }
3543 }
3544
Michael Layzell734adb42017-06-07 16:58:31 -04003545 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003546 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003547 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003548 self.underscore_token.to_tokens(tokens);
3549 }
3550 }
3551
Michael Layzell734adb42017-06-07 16:58:31 -04003552 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003553 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003554 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003555 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003556 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003557 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003558 if let Some((ref at_token, ref subpat)) = self.subpat {
3559 at_token.to_tokens(tokens);
3560 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003561 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003562 }
3563 }
3564
Michael Layzell734adb42017-06-07 16:58:31 -04003565 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003566 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003567 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003568 self.path.to_tokens(tokens);
3569 self.brace_token.surround(tokens, |tokens| {
3570 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003571 // NOTE: We need a comma before the dot2 token if it is present.
3572 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003573 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003574 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003575 self.dot2_token.to_tokens(tokens);
3576 });
3577 }
3578 }
3579
Michael Layzell734adb42017-06-07 16:58:31 -04003580 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003581 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003582 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003583 self.path.to_tokens(tokens);
3584 self.pat.to_tokens(tokens);
3585 }
3586 }
3587
Michael Layzell734adb42017-06-07 16:58:31 -04003588 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003589 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003590 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003591 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3592 }
3593 }
3594
Michael Layzell734adb42017-06-07 16:58:31 -04003595 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003596 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003597 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003598 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003599 self.front.to_tokens(tokens);
3600 if let Some(ref dot2_token) = self.dot2_token {
3601 if !self.front.empty_or_trailing() {
3602 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003603 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003604 }
David Tolnay41871922017-12-29 01:53:45 -05003605 dot2_token.to_tokens(tokens);
3606 self.comma_token.to_tokens(tokens);
3607 if self.comma_token.is_none() && !self.back.is_empty() {
3608 // Ensure there is a comma after the .. token.
3609 <Token![,]>::default().to_tokens(tokens);
3610 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003611 }
David Tolnay41871922017-12-29 01:53:45 -05003612 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003613 });
3614 }
3615 }
3616
Michael Layzell734adb42017-06-07 16:58:31 -04003617 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003618 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003619 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003620 self.box_token.to_tokens(tokens);
3621 self.pat.to_tokens(tokens);
3622 }
3623 }
3624
Michael Layzell734adb42017-06-07 16:58:31 -04003625 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003626 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003627 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003628 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003629 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003630 self.pat.to_tokens(tokens);
3631 }
3632 }
3633
Michael Layzell734adb42017-06-07 16:58:31 -04003634 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003635 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003636 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003637 self.expr.to_tokens(tokens);
3638 }
3639 }
3640
Michael Layzell734adb42017-06-07 16:58:31 -04003641 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003642 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003643 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003644 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003645 match self.limits {
3646 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003647 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003648 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003649 self.hi.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 PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003655 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003656 // XXX: This is a mess, and it will be so easy to screw it up. How
3657 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003658 self.bracket_token.surround(tokens, |tokens| {
3659 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003660
3661 // If we need a comma before the middle or standalone .. token,
3662 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003663 if !self.front.empty_or_trailing()
3664 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003665 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003666 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003667 }
3668
3669 // If we have an identifier, we always need a .. token.
3670 if self.middle.is_some() {
3671 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003672 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003673 } else if self.dot2_token.is_some() {
3674 self.dot2_token.to_tokens(tokens);
3675 }
3676
3677 // Make sure we have a comma before the back half.
3678 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003679 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003680 self.back.to_tokens(tokens);
3681 } else {
3682 self.comma_token.to_tokens(tokens);
3683 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003684 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003685 }
3686 }
3687
Michael Layzell734adb42017-06-07 16:58:31 -04003688 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003689 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003690 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003691 self.mac.to_tokens(tokens);
3692 }
3693 }
3694
3695 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003696 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003697 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003698 self.tts.to_tokens(tokens);
3699 }
3700 }
3701
3702 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003703 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003704 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003705 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003706 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003707 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003708 }
3709 self.pat.to_tokens(tokens);
3710 }
3711 }
3712
Michael Layzell734adb42017-06-07 16:58:31 -04003713 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003714 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003715 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003716 self.brace_token.surround(tokens, |tokens| {
3717 tokens.append_all(&self.stmts);
3718 });
David Tolnay42602292016-10-01 22:25:45 -07003719 }
3720 }
3721
Michael Layzell734adb42017-06-07 16:58:31 -04003722 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003723 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003724 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003725 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003726 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003727 Stmt::Item(ref item) => item.to_tokens(tokens),
3728 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003729 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003730 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003731 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003732 }
David Tolnay42602292016-10-01 22:25:45 -07003733 }
3734 }
3735 }
David Tolnay191e0582016-10-02 18:31:09 -07003736
Michael Layzell734adb42017-06-07 16:58:31 -04003737 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003738 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003739 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003740 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003741 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003742 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003743 if let Some((ref colon_token, ref ty)) = self.ty {
3744 colon_token.to_tokens(tokens);
3745 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003746 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003747 if let Some((ref eq_token, ref init)) = self.init {
3748 eq_token.to_tokens(tokens);
3749 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003750 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003751 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003752 }
3753 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003754}