blob: 2b7aebba929b5a59546e1908215be8f37a533034 [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>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 pub block: Block,
341 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700342
David Tolnaya454c8f2018-01-07 01:01:10 -0800343 /// An assignment expression: `a = compute()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800344 ///
345 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400346 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500347 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800349 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500350 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500352
David Tolnaya454c8f2018-01-07 01:01:10 -0800353 /// A compound assignment expression: `counter += 1`.
David Tolnay461d98e2018-01-07 11:07:19 -0800354 ///
355 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400356 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500357 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700358 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500359 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700360 pub right: Box<Expr>,
361 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500362
David Tolnaya454c8f2018-01-07 01:01:10 -0800363 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500364 /// field (`obj.0`).
David Tolnay461d98e2018-01-07 11:07:19 -0800365 ///
366 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd5147742018-06-30 10:09:52 -0700367 pub Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -0500368 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500369 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500371 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500373
David Tolnay05658502018-01-07 09:56:37 -0800374 /// A square bracketed indexing expression: `vector[2]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800375 ///
376 /// *This type is available if Syn is built with the `"derive"` or
377 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700378 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500379 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500381 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500382 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500384
David Tolnaya454c8f2018-01-07 01:01:10 -0800385 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800386 ///
387 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400388 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500389 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700390 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500392 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700393 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700394
David Tolnaya454c8f2018-01-07 01:01:10 -0800395 /// A path like `std::mem::replace` possibly containing generic
396 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800398 /// A plain identifier like `x` is a path of length 1.
David Tolnay461d98e2018-01-07 11:07:19 -0800399 ///
400 /// *This type is available if Syn is built with the `"derive"` or
401 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500403 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 pub qself: Option<QSelf>,
405 pub path: Path,
406 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700407
David Tolnaya454c8f2018-01-07 01:01:10 -0800408 /// A referencing operation: `&a` or `&mut a`.
David Tolnay461d98e2018-01-07 11:07:19 -0800409 ///
410 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay00674ba2018-03-31 18:14:11 +0200411 pub Reference(ExprReference #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500412 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800413 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500414 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub expr: Box<Expr>,
416 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500417
David Tolnaya454c8f2018-01-07 01:01:10 -0800418 /// A `break`, with an optional label to break and an optional
419 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800420 ///
421 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400422 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500423 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500424 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700425 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700426 pub expr: Option<Box<Expr>>,
427 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500428
David Tolnaya454c8f2018-01-07 01:01:10 -0800429 /// A `continue`, with an optional label.
David Tolnay461d98e2018-01-07 11:07:19 -0800430 ///
431 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400432 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500433 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800434 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500435 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700436 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500437
David Tolnaya454c8f2018-01-07 01:01:10 -0800438 /// A `return`, with an optional value to be returned.
David Tolnay461d98e2018-01-07 11:07:19 -0800439 ///
440 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayc246cd32017-12-28 23:14:32 -0500441 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500442 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800443 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500444 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700445 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700446
David Tolnaya454c8f2018-01-07 01:01:10 -0800447 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800448 ///
449 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500450 pub Macro(ExprMacro #full {
451 pub attrs: Vec<Attribute>,
452 pub mac: Macro,
453 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700454
David Tolnaya454c8f2018-01-07 01:01:10 -0800455 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700456 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800457 /// The `rest` provides the value of the remaining fields as in `S { a:
458 /// 1, b: 1, ..rest }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800459 ///
460 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400461 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500462 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700463 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500464 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500465 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500466 pub dot2_token: Option<Token![..]>,
467 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700468 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700469
David Tolnaya454c8f2018-01-07 01:01:10 -0800470 /// An array literal constructed from one repeated element: `[0u8; N]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800471 ///
472 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400473 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500474 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500475 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700476 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500477 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800478 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700479 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700480
David Tolnaya454c8f2018-01-07 01:01:10 -0800481 /// A parenthesized expression: `(a + b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800482 ///
483 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9374bc02018-01-27 18:49:36 -0800484 pub Paren(ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -0500485 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500486 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500487 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700488 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700489
David Tolnaya454c8f2018-01-07 01:01:10 -0800490 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400491 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800492 /// This variant is important for faithfully representing the precedence
493 /// of expressions and is related to `None`-delimited spans in a
494 /// `TokenStream`.
David Tolnay461d98e2018-01-07 11:07:19 -0800495 ///
496 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye98775f2017-12-28 23:17:00 -0500497 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500498 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500499 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500500 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400501 }),
502
David Tolnaya454c8f2018-01-07 01:01:10 -0800503 /// A try-expression: `expr?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800504 ///
505 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400506 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500507 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800509 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700510 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700511
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400512 /// A try block: `try { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800513 ///
514 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400515 pub TryBlock(ExprTryBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500516 pub attrs: Vec<Attribute>,
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400517 pub try_token: Token![try],
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 pub block: Block,
519 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700520
David Tolnaya454c8f2018-01-07 01:01:10 -0800521 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800522 ///
523 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700524 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500525 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800526 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700527 pub expr: Option<Box<Expr>>,
528 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500529
David Tolnaya454c8f2018-01-07 01:01:10 -0800530 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800531 ///
532 /// *This type is available if Syn is built with the `"derive"` or
533 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500534 pub Verbatim(ExprVerbatim #manual_extra_traits {
535 pub tts: TokenStream,
536 }),
537 }
538}
539
540#[cfg(feature = "extra-traits")]
541impl Eq for ExprVerbatim {}
542
543#[cfg(feature = "extra-traits")]
544impl PartialEq for ExprVerbatim {
545 fn eq(&self, other: &Self) -> bool {
546 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
547 }
548}
549
550#[cfg(feature = "extra-traits")]
551impl Hash for ExprVerbatim {
552 fn hash<H>(&self, state: &mut H)
553 where
554 H: Hasher,
555 {
556 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700558}
559
David Tolnay8c91b882017-12-28 23:04:32 -0500560impl Expr {
561 // Not public API.
562 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500563 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500564 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500565 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800566 Expr::Box(ExprBox { ref mut attrs, .. })
567 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
568 | Expr::Array(ExprArray { ref mut attrs, .. })
569 | Expr::Call(ExprCall { ref mut attrs, .. })
570 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
571 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
572 | Expr::Binary(ExprBinary { ref mut attrs, .. })
573 | Expr::Unary(ExprUnary { ref mut attrs, .. })
574 | Expr::Lit(ExprLit { ref mut attrs, .. })
575 | Expr::Cast(ExprCast { ref mut attrs, .. })
576 | Expr::Type(ExprType { ref mut attrs, .. })
577 | Expr::If(ExprIf { ref mut attrs, .. })
578 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
579 | Expr::While(ExprWhile { ref mut attrs, .. })
580 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
581 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
582 | Expr::Loop(ExprLoop { ref mut attrs, .. })
583 | Expr::Match(ExprMatch { ref mut attrs, .. })
584 | Expr::Closure(ExprClosure { ref mut attrs, .. })
585 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
586 | Expr::Block(ExprBlock { ref mut attrs, .. })
587 | Expr::Assign(ExprAssign { ref mut attrs, .. })
588 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
589 | Expr::Field(ExprField { ref mut attrs, .. })
590 | Expr::Index(ExprIndex { ref mut attrs, .. })
591 | Expr::Range(ExprRange { ref mut attrs, .. })
592 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200593 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800594 | Expr::Break(ExprBreak { ref mut attrs, .. })
595 | Expr::Continue(ExprContinue { ref mut attrs, .. })
596 | Expr::Return(ExprReturn { ref mut attrs, .. })
597 | Expr::Macro(ExprMacro { ref mut attrs, .. })
598 | Expr::Struct(ExprStruct { ref mut attrs, .. })
599 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
600 | Expr::Paren(ExprParen { ref mut attrs, .. })
601 | Expr::Group(ExprGroup { ref mut attrs, .. })
602 | Expr::Try(ExprTry { ref mut attrs, .. })
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400603 | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800604 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500605 Expr::Verbatim(_) => {
606 // TODO
607 Vec::new()
608 }
David Tolnay8c91b882017-12-28 23:04:32 -0500609 }
610 }
611}
612
David Tolnay85b69a42017-12-27 20:43:10 -0500613ast_enum! {
614 /// A struct or tuple struct field accessed in a struct literal or field
615 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800616 ///
617 /// *This type is available if Syn is built with the `"derive"` or `"full"`
618 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500619 pub enum Member {
620 /// A named field like `self.x`.
621 Named(Ident),
622 /// An unnamed field like `self.0`.
623 Unnamed(Index),
624 }
625}
626
David Tolnay85b69a42017-12-27 20:43:10 -0500627ast_struct! {
628 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800629 ///
630 /// *This type is available if Syn is built with the `"derive"` or `"full"`
631 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500632 pub struct Index #manual_extra_traits {
633 pub index: u32,
634 pub span: Span,
635 }
636}
637
David Tolnay14982012017-12-29 00:49:51 -0500638impl From<usize> for Index {
639 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700640 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500641 Index {
642 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700643 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500644 }
645 }
646}
647
648#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500649impl Eq for Index {}
650
David Tolnay14982012017-12-29 00:49:51 -0500651#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500652impl PartialEq for Index {
653 fn eq(&self, other: &Self) -> bool {
654 self.index == other.index
655 }
656}
657
David Tolnay14982012017-12-29 00:49:51 -0500658#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500659impl Hash for Index {
660 fn hash<H: Hasher>(&self, state: &mut H) {
661 self.index.hash(state);
662 }
663}
664
665#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700666ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800667 /// The `::<>` explicit type parameters passed to a method call:
668 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800669 ///
670 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500671 pub struct MethodTurbofish {
672 pub colon2_token: Token![::],
673 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500674 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500675 pub gt_token: Token![>],
676 }
677}
678
679#[cfg(feature = "full")]
680ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800681 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800682 ///
683 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500684 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800685 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500686 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800687 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500688 ///
689 /// NOTE: Identity expressions are represented as Type arguments, as
690 /// they are indistinguishable syntactically.
691 Const(Expr),
692 }
693}
694
695#[cfg(feature = "full")]
696ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700697 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800698 ///
699 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500701 /// Attributes tagged on the field.
702 pub attrs: Vec<Attribute>,
703
704 /// Name or index of the field.
705 pub member: Member,
706
David Tolnay5d7098a2017-12-29 01:35:24 -0500707 /// The colon in `Struct { x: x }`. If written in shorthand like
708 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500709 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500710
Alex Crichton62a0a592017-05-22 13:58:53 -0700711 /// Value of the field.
712 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700713 }
David Tolnay055a7042016-10-02 19:23:54 -0700714}
715
Michael Layzell734adb42017-06-07 16:58:31 -0400716#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700717ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800718 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500721 pub struct Label {
722 pub name: Lifetime,
723 pub colon_token: Token![:],
724 }
725}
726
727#[cfg(feature = "full")]
728ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800729 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500733 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700734 /// Statements in a block
735 pub stmts: Vec<Stmt>,
736 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700737}
738
Michael Layzell734adb42017-06-07 16:58:31 -0400739#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700740ast_enum! {
741 /// A statement, usually ending in a semicolon.
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 enum Stmt {
745 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800746 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700747
Alex Crichton62a0a592017-05-22 13:58:53 -0700748 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800749 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700750
Alex Crichton62a0a592017-05-22 13:58:53 -0700751 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800752 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700753
David Tolnaya454c8f2018-01-07 01:01:10 -0800754 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800755 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700756 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700757}
758
Michael Layzell734adb42017-06-07 16:58:31 -0400759#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700760ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800761 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800762 ///
763 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700764 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500765 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200767 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500768 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500769 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500770 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700771 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700772}
773
Michael Layzell734adb42017-06-07 16:58:31 -0400774#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700775ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800776 /// A pattern in a local binding, function signature, match expression, or
777 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800778 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800779 /// *This type is available if Syn is built with the `"full"` feature.*
780 ///
David Tolnay614a0142018-01-07 10:25:43 -0800781 /// # Syntax tree enum
782 ///
783 /// This type is a [syntax tree enum].
784 ///
785 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700786 // Clippy false positive
787 // https://github.com/Manishearth/rust-clippy/issues/1241
788 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
789 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800790 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800791 ///
792 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700793 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800794 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700796
David Tolnaya454c8f2018-01-07 01:01:10 -0800797 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800798 ///
799 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700800 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500801 pub by_ref: Option<Token![ref]>,
802 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700803 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500804 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700806
David Tolnaya454c8f2018-01-07 01:01:10 -0800807 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800808 ///
809 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700810 pub Struct(PatStruct {
811 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500812 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500813 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800814 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700815 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700816
David Tolnaya454c8f2018-01-07 01:01:10 -0800817 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800818 ///
819 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700820 pub TupleStruct(PatTupleStruct {
821 pub path: Path,
822 pub pat: PatTuple,
823 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700824
David Tolnaya454c8f2018-01-07 01:01:10 -0800825 /// A path pattern like `Color::Red`, optionally qualified with a
826 /// self-type.
827 ///
828 /// Unquailfied path patterns can legally refer to variants, structs,
829 /// constants or associated constants. Quailfied path patterns like
830 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
831 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800832 ///
833 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700834 pub Path(PatPath {
835 pub qself: Option<QSelf>,
836 pub path: Path,
837 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700838
David Tolnaya454c8f2018-01-07 01:01:10 -0800839 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800840 ///
841 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700842 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500843 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500844 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500845 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500846 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500847 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700848 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800849
850 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800851 ///
852 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700853 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800854 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500855 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700856 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800857
858 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800859 ///
860 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700861 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800862 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500863 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500864 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700865 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800866
867 /// A literal pattern: `0`.
868 ///
869 /// This holds an `Expr` rather than a `Lit` because negative numbers
870 /// are represented as an `Expr::Unary`.
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 Lit(PatLit {
874 pub expr: Box<Expr>,
875 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800876
877 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800878 ///
879 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 pub Range(PatRange {
881 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700882 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500883 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700884 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800885
886 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800887 ///
888 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700889 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500890 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500891 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500893 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500894 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500895 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800897
898 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800899 ///
900 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500901 pub Macro(PatMacro {
902 pub mac: Macro,
903 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800904
905 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800906 ///
907 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500908 pub Verbatim(PatVerbatim #manual_extra_traits {
909 pub tts: TokenStream,
910 }),
911 }
912}
913
David Tolnayc43b44e2017-12-30 23:55:54 -0500914#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500915impl Eq for PatVerbatim {}
916
David Tolnayc43b44e2017-12-30 23:55:54 -0500917#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500918impl PartialEq for PatVerbatim {
919 fn eq(&self, other: &Self) -> bool {
920 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
921 }
922}
923
David Tolnayc43b44e2017-12-30 23:55:54 -0500924#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500925impl Hash for PatVerbatim {
926 fn hash<H>(&self, state: &mut H)
927 where
928 H: Hasher,
929 {
930 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700931 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700932}
933
Michael Layzell734adb42017-06-07 16:58:31 -0400934#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700935ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800936 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700937 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800938 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700939 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500940 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800941 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500942 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800944 /// 0...10 => {
945 /// return true;
946 /// }
947 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500948 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800950 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500951 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700952 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800953 ///
954 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 pub struct Arm {
956 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200957 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500958 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500959 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200960 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700963 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700964}
965
Michael Layzell734adb42017-06-07 16:58:31 -0400966#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700967ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800968 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800969 ///
970 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700971 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700972 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800973 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800975 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800976 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700977 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700978}
979
Michael Layzell734adb42017-06-07 16:58:31 -0400980#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700981ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800982 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700983 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800984 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
985 /// 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 -0800986 ///
987 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700988 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500989 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500990 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500991 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700992 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700993 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700994}
995
Michael Layzell3936ceb2017-07-08 00:28:36 -0400996#[cfg(any(feature = "parsing", feature = "printing"))]
997#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700998fn arm_expr_requires_comma(expr: &Expr) -> bool {
999 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1000 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001001 match *expr {
1002 Expr::Unsafe(..)
1003 | Expr::Block(..)
1004 | Expr::If(..)
1005 | Expr::IfLet(..)
1006 | Expr::Match(..)
1007 | Expr::While(..)
1008 | Expr::WhileLet(..)
1009 | Expr::Loop(..)
1010 | Expr::ForLoop(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001011 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001012 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001013 }
1014}
1015
David Tolnayb9c8e322016-09-23 20:48:37 -07001016#[cfg(feature = "parsing")]
1017pub mod parsing {
1018 use super::*;
David Tolnay9cc2f092018-08-24 15:51:37 -04001019 use path::parsing::mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001020 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001021 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001022
David Tolnaydfc886b2018-01-06 08:03:09 -08001023 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001024 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001025 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001026 #[cfg(feature = "full")]
1027 use proc_macro2::TokenStream;
David Tolnay203557a2017-12-27 23:59:33 -05001028 use synom::PResult;
David Tolnay94d2b792018-04-29 12:26:10 -07001029 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001030
David Tolnaybcf26022017-12-25 22:10:52 -05001031 // When we're parsing expressions which occur before blocks, like in an if
1032 // statement's condition, we cannot parse a struct literal.
1033 //
1034 // Struct literals are ambiguous in certain positions
1035 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001036 macro_rules! ambiguous_expr {
1037 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001038 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001039 };
1040 }
1041
David Tolnaybcf26022017-12-25 22:10:52 -05001042 // When we are parsing an optional suffix expression, we cannot allow blocks
1043 // if structs are not allowed.
1044 //
1045 // Example:
1046 //
1047 // if break {} {}
1048 //
1049 // is ambiguous between:
1050 //
1051 // if (break {}) {}
1052 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001053 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001054 macro_rules! opt_ambiguous_expr {
1055 ($i:expr, $allow_struct:ident) => {
1056 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1057 };
1058 }
1059
Alex Crichton954046c2017-05-30 21:49:42 -07001060 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001061 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001062
1063 fn description() -> Option<&'static str> {
1064 Some("expression")
1065 }
1066 }
1067
Michael Layzell734adb42017-06-07 16:58:31 -04001068 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001069 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1070
David Tolnaybcf26022017-12-25 22:10:52 -05001071 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001072 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001073 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001074 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001075 }
1076
Michael Layzell734adb42017-06-07 16:58:31 -04001077 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001078 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001079 // NOTE: We intentionally skip assign_expr, placement_expr, and
1080 // range_expr, as they are not parsed in non-full mode.
1081 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001082 }
1083
David Tolnaybcf26022017-12-25 22:10:52 -05001084 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001085 macro_rules! binop {
1086 (
1087 $name: ident,
1088 $next: ident,
1089 $submac: ident!( $($args:tt)* )
1090 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001091 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001092 mut e: call!($next, allow_struct, allow_block) >>
1093 many0!(do_parse!(
1094 op: $submac!($($args)*) >>
1095 rhs: call!($next, allow_struct, true) >>
1096 ({
1097 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001098 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001099 left: Box::new(e.into()),
1100 op: op,
1101 right: Box::new(rhs.into()),
1102 }.into();
1103 })
1104 )) >>
1105 (e)
1106 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001107 }
David Tolnay54e854d2016-10-24 12:03:30 -07001108 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001109
David Tolnaybcf26022017-12-25 22:10:52 -05001110 // <placement> = <placement> ..
1111 // <placement> += <placement> ..
1112 // <placement> -= <placement> ..
1113 // <placement> *= <placement> ..
1114 // <placement> /= <placement> ..
1115 // <placement> %= <placement> ..
1116 // <placement> ^= <placement> ..
1117 // <placement> &= <placement> ..
1118 // <placement> |= <placement> ..
1119 // <placement> <<= <placement> ..
1120 // <placement> >>= <placement> ..
1121 //
1122 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001123 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001124 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001125 mut e: call!(placement_expr, allow_struct, allow_block) >>
1126 alt!(
1127 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001128 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001129 // Recurse into self to parse right-associative operator.
1130 rhs: call!(assign_expr, allow_struct, true) >>
1131 ({
1132 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001133 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001134 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001135 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001136 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001137 }.into();
1138 })
1139 )
1140 |
1141 do_parse!(
1142 op: call!(BinOp::parse_assign_op) >>
1143 // Recurse into self to parse right-associative operator.
1144 rhs: call!(assign_expr, allow_struct, true) >>
1145 ({
1146 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001147 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001148 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001149 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001150 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001151 }.into();
1152 })
1153 )
1154 |
1155 epsilon!()
1156 ) >>
1157 (e)
1158 ));
1159
David Tolnaybcf26022017-12-25 22:10:52 -05001160 // <range> <- <range> ..
1161 //
1162 // NOTE: The `in place { expr }` version of this syntax is parsed in
1163 // `atom_expr`, not here.
1164 //
1165 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001166 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001167 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 mut e: call!(range_expr, allow_struct, allow_block) >>
1169 alt!(
1170 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001171 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001172 // Recurse into self to parse right-associative operator.
1173 rhs: call!(placement_expr, allow_struct, true) >>
1174 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001175 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001176 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001177 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001178 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001179 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001180 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 }.into();
1182 })
1183 )
1184 |
1185 epsilon!()
1186 ) >>
1187 (e)
1188 ));
1189
David Tolnaybcf26022017-12-25 22:10:52 -05001190 // <or> ... <or> ..
1191 // <or> .. <or> ..
1192 // <or> ..
1193 //
1194 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1195 // rules are for parsing these expressions are, but this is not correct.
1196 // For example, `a .. b .. c` is not a legal expression. It should not
1197 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1198 //
1199 // NOTE: The form of ranges which don't include a preceding expression are
1200 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001201 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001202 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001203 mut e: call!(or_expr, allow_struct, allow_block) >>
1204 many0!(do_parse!(
1205 limits: syn!(RangeLimits) >>
1206 // We don't want to allow blocks here if we don't allow structs. See
1207 // the reasoning for `opt_ambiguous_expr!` above.
1208 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1209 ({
1210 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001211 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001212 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001213 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001214 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001215 }.into();
1216 })
1217 )) >>
1218 (e)
1219 ));
1220
David Tolnaybcf26022017-12-25 22:10:52 -05001221 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001222 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001223
David Tolnaybcf26022017-12-25 22:10:52 -05001224 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001225 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001226
David Tolnaybcf26022017-12-25 22:10:52 -05001227 // <bitor> == <bitor> ...
1228 // <bitor> != <bitor> ...
1229 // <bitor> >= <bitor> ...
1230 // <bitor> <= <bitor> ...
1231 // <bitor> > <bitor> ...
1232 // <bitor> < <bitor> ...
1233 //
1234 // NOTE: This operator appears to be parsed as left-associative, but errors
1235 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001236 binop!(
1237 compare_expr,
1238 bitor_expr,
1239 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001240 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001241 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001242 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001243 |
1244 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001245 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001246 |
1247 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001248 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001249 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001250 do_parse!(
1251 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001252 not!(punct!(<-)) >>
1253 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001254 (BinOp::Lt(t))
1255 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001258 )
1259 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001260
David Tolnaybcf26022017-12-25 22:10:52 -05001261 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001262 binop!(
1263 bitor_expr,
1264 bitxor_expr,
1265 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1266 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267
David Tolnaybcf26022017-12-25 22:10:52 -05001268 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001269 binop!(
1270 bitxor_expr,
1271 bitand_expr,
1272 do_parse!(
1273 // NOTE: Make sure we aren't looking at ^=.
1274 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1275 )
1276 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001277
David Tolnaybcf26022017-12-25 22:10:52 -05001278 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001279 binop!(
1280 bitand_expr,
1281 shift_expr,
1282 do_parse!(
1283 // NOTE: Make sure we aren't looking at && or &=.
1284 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1285 )
1286 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001287
David Tolnaybcf26022017-12-25 22:10:52 -05001288 // <arith> << <arith> ...
1289 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001290 binop!(
1291 shift_expr,
1292 arith_expr,
1293 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001295 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001296 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001297 )
1298 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001299
David Tolnaybcf26022017-12-25 22:10:52 -05001300 // <term> + <term> ...
1301 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001302 binop!(
1303 arith_expr,
1304 term_expr,
1305 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001306 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001307 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001308 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001309 )
1310 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001311
David Tolnaybcf26022017-12-25 22:10:52 -05001312 // <cast> * <cast> ...
1313 // <cast> / <cast> ...
1314 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001315 binop!(
1316 term_expr,
1317 cast_expr,
1318 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001319 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001321 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001322 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001323 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001324 )
1325 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326
David Tolnaybcf26022017-12-25 22:10:52 -05001327 // <unary> as <ty>
1328 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001329 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001330 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001331 mut e: call!(unary_expr, allow_struct, allow_block) >>
1332 many0!(alt!(
1333 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001334 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001335 // We can't accept `A + B` in cast expressions, as it's
1336 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001337 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338 ({
1339 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001340 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001341 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001342 as_token: as_,
1343 ty: Box::new(ty),
1344 }.into();
1345 })
1346 )
1347 |
1348 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001349 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001350 // We can't accept `A + B` in cast expressions, as it's
1351 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001352 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001353 ({
1354 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001355 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001356 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001357 colon_token: colon,
1358 ty: Box::new(ty),
1359 }.into();
1360 })
1361 )
1362 )) >>
1363 (e)
1364 ));
1365
David Tolnay0cf94f22017-12-28 23:46:26 -05001366 // <unary> as <ty>
1367 #[cfg(not(feature = "full"))]
1368 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1369 mut e: call!(unary_expr, allow_struct, allow_block) >>
1370 many0!(do_parse!(
1371 as_: keyword!(as) >>
1372 // We can't accept `A + B` in cast expressions, as it's
1373 // ambiguous with the + expression.
1374 ty: call!(Type::without_plus) >>
1375 ({
1376 e = ExprCast {
1377 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001378 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001379 as_token: as_,
1380 ty: Box::new(ty),
1381 }.into();
1382 })
1383 )) >>
1384 (e)
1385 ));
1386
David Tolnaybcf26022017-12-25 22:10:52 -05001387 // <UnOp> <trailer>
1388 // & <trailer>
1389 // &mut <trailer>
1390 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001391 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001392 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001393 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001394 attrs: many0!(Attribute::parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001395 op: syn!(UnOp) >>
1396 expr: call!(unary_expr, allow_struct, true) >>
1397 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001398 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001399 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001400 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001401 }.into())
1402 )
1403 |
1404 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001405 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001406 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001407 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001408 expr: call!(unary_expr, allow_struct, true) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001409 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001410 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001411 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001412 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001413 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001414 }.into())
1415 )
1416 |
1417 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001418 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001419 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001420 expr: call!(unary_expr, allow_struct, true) >>
1421 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001422 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001423 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001424 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001425 }.into())
1426 )
1427 |
1428 call!(trailer_expr, allow_struct, allow_block)
1429 ));
1430
Michael Layzell734adb42017-06-07 16:58:31 -04001431 // XXX: This duplication is ugly
1432 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001433 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001434 do_parse!(
1435 op: syn!(UnOp) >>
1436 expr: call!(unary_expr, allow_struct, true) >>
1437 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001438 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001439 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001440 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001441 }.into())
1442 )
1443 |
1444 call!(trailer_expr, allow_struct, allow_block)
1445 ));
1446
David Tolnayd997aef2018-07-21 18:42:31 -07001447 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001448 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1449 let mut outer = Vec::new();
1450 let mut inner = Vec::new();
1451 for attr in mem::replace(attrs, Vec::new()) {
1452 match attr.style {
1453 AttrStyle::Outer => outer.push(attr),
1454 AttrStyle::Inner(_) => inner.push(attr),
1455 }
1456 }
1457 *attrs = inner;
1458 outer
1459 }
1460
David Tolnaybcf26022017-12-25 22:10:52 -05001461 // <atom> (..<args>) ...
1462 // <atom> . <ident> (..<args>) ...
1463 // <atom> . <ident> ...
1464 // <atom> . <lit> ...
1465 // <atom> [ <expr> ] ...
1466 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001467 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001468 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001469 mut e: call!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001470 outer_attrs: value!({
1471 let mut attrs = e.replace_attrs(Vec::new());
1472 let outer_attrs = take_outer(&mut attrs);
1473 e.replace_attrs(attrs);
1474 outer_attrs
1475 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001476 many0!(alt!(
1477 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001478 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001479 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001480 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001481 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001482 args: args,
1483 paren_token: paren,
1484 }.into();
1485 })
1486 |
1487 tap!(more: and_method_call => {
1488 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001489 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001490 e = call.into();
1491 })
1492 |
1493 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001494 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001495 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001496 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001497 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001498 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001499 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001500 }.into();
1501 })
1502 |
1503 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001504 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001505 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001506 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001507 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001508 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001509 index: Box::new(i),
1510 }.into();
1511 })
1512 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001513 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001514 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001515 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001516 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001517 question_token: question,
1518 }.into();
1519 })
1520 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001521 ({
1522 let mut attrs = outer_attrs;
1523 attrs.extend(e.replace_attrs(Vec::new()));
1524 e.replace_attrs(attrs);
1525 e
1526 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001527 ));
1528
Michael Layzell734adb42017-06-07 16:58:31 -04001529 // XXX: Duplication == ugly
1530 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001531 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001532 mut e: call!(atom_expr, allow_struct, allow_block) >>
1533 many0!(alt!(
1534 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001535 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001536 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001537 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001538 paren_token: args.0,
1539 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001540 }.into();
1541 })
1542 |
David Tolnayd5147742018-06-30 10:09:52 -07001543 tap!(field: and_field => {
1544 let (token, member) = field;
1545 e = ExprField {
1546 attrs: Vec::new(),
1547 base: Box::new(e),
1548 dot_token: token,
1549 member: member,
1550 }.into();
1551 })
1552 |
Michael Layzell734adb42017-06-07 16:58:31 -04001553 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001554 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001555 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001556 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001557 bracket_token: i.0,
1558 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001559 }.into();
1560 })
1561 )) >>
1562 (e)
1563 ));
1564
David Tolnaya454c8f2018-01-07 01:01:10 -08001565 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001566 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001567 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001568 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1569 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001570 |
David Tolnay8c91b882017-12-28 23:04:32 -05001571 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001572 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001573 // must be before ExprStruct
1574 call!(unstable_async_block) => { Expr::Verbatim }
1575 |
David Tolnayf7177052018-08-24 15:31:50 -04001576 // must be before ExprStruct
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001577 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnayf7177052018-08-24 15:31:50 -04001578 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001579 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001580 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001581 |
David Tolnay8c91b882017-12-28 23:04:32 -05001582 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001583 |
David Tolnay8c91b882017-12-28 23:04:32 -05001584 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001585 |
1586 call!(expr_break, allow_struct) // must be before expr_path
1587 |
David Tolnay8c91b882017-12-28 23:04:32 -05001588 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001589 |
1590 call!(expr_ret, allow_struct) // must be before expr_path
1591 |
David Tolnay8c91b882017-12-28 23:04:32 -05001592 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001593 |
David Tolnay8c91b882017-12-28 23:04:32 -05001594 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001595 |
David Tolnay8c91b882017-12-28 23:04:32 -05001596 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001597 |
David Tolnay8c91b882017-12-28 23:04:32 -05001598 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001599 |
David Tolnay8c91b882017-12-28 23:04:32 -05001600 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001601 |
David Tolnay8c91b882017-12-28 23:04:32 -05001602 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001603 |
David Tolnay8c91b882017-12-28 23:04:32 -05001604 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001605 |
David Tolnay8c91b882017-12-28 23:04:32 -05001606 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001607 |
David Tolnay8c91b882017-12-28 23:04:32 -05001608 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001609 |
David Tolnay8c91b882017-12-28 23:04:32 -05001610 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001611 |
David Tolnay8c91b882017-12-28 23:04:32 -05001612 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001613 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001614 call!(expr_closure, allow_struct)
1615 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001616 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001617 |
David Tolnay5d08ae62018-08-01 00:08:48 -07001618 call!(unstable_labeled_block) => { Expr::Verbatim }
1619 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001620 // NOTE: This is the prefix-form of range
1621 call!(expr_range, allow_struct)
1622 |
David Tolnay8c91b882017-12-28 23:04:32 -05001623 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001624 |
David Tolnay8c91b882017-12-28 23:04:32 -05001625 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001626 ));
1627
Michael Layzell734adb42017-06-07 16:58:31 -04001628 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001629 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001630 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001631 |
David Tolnay9374bc02018-01-27 18:49:36 -08001632 syn!(ExprParen) => { Expr::Paren }
1633 |
David Tolnay8c91b882017-12-28 23:04:32 -05001634 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001635 ));
1636
Michael Layzell734adb42017-06-07 16:58:31 -04001637 #[cfg(feature = "full")]
David Tolnay313a36f2018-04-29 20:13:04 -07001638 named!(expr_nosemi -> Expr, do_parse!(
1639 nosemi: alt!(
1640 syn!(ExprIf) => { Expr::If }
1641 |
1642 syn!(ExprIfLet) => { Expr::IfLet }
1643 |
1644 syn!(ExprWhile) => { Expr::While }
1645 |
1646 syn!(ExprWhileLet) => { Expr::WhileLet }
1647 |
1648 syn!(ExprForLoop) => { Expr::ForLoop }
1649 |
1650 syn!(ExprLoop) => { Expr::Loop }
1651 |
1652 syn!(ExprMatch) => { Expr::Match }
1653 |
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001654 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnay313a36f2018-04-29 20:13:04 -07001655 |
1656 syn!(ExprYield) => { Expr::Yield }
1657 |
1658 syn!(ExprUnsafe) => { Expr::Unsafe }
1659 |
1660 syn!(ExprBlock) => { Expr::Block }
David Tolnay5d08ae62018-08-01 00:08:48 -07001661 |
1662 call!(unstable_labeled_block) => { Expr::Verbatim }
David Tolnay313a36f2018-04-29 20:13:04 -07001663 ) >>
1664 // If the next token is a `.` or a `?` it is special-cased to parse
1665 // as an expression instead of a blockexpression.
1666 not!(punct!(.)) >>
1667 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001668 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001669 ));
Michael Layzell35418782017-06-07 09:20:25 -04001670
David Tolnay8c91b882017-12-28 23:04:32 -05001671 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001672 #[cfg(not(feature = "full"))]
1673 named!(parse -> Self, do_parse!(
1674 lit: syn!(Lit) >>
1675 (ExprLit {
1676 attrs: Vec::new(),
1677 lit: lit,
1678 })
1679 ));
1680
1681 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001682 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07001683 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001684 lit: syn!(Lit) >>
1685 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001686 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001687 lit: lit,
1688 })
1689 ));
David Tolnay79777332018-01-07 10:04:42 -08001690
1691 fn description() -> Option<&'static str> {
1692 Some("literal")
1693 }
David Tolnay8c91b882017-12-28 23:04:32 -05001694 }
1695
1696 #[cfg(feature = "full")]
1697 impl Synom for ExprMacro {
1698 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001699 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001700 mac: syn!(Macro) >>
1701 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001702 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001703 mac: mac,
1704 })
1705 ));
David Tolnay79777332018-01-07 10:04:42 -08001706
1707 fn description() -> Option<&'static str> {
1708 Some("macro invocation expression")
1709 }
David Tolnay8c91b882017-12-28 23:04:32 -05001710 }
1711
David Tolnaye98775f2017-12-28 23:17:00 -05001712 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001713 impl Synom for ExprGroup {
1714 named!(parse -> Self, do_parse!(
1715 e: grouped!(syn!(Expr)) >>
1716 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001717 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001718 expr: Box::new(e.1),
1719 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001720 })
Michael Layzell93c36282017-06-04 20:43:14 -04001721 ));
David Tolnay79777332018-01-07 10:04:42 -08001722
1723 fn description() -> Option<&'static str> {
1724 Some("expression surrounded by invisible delimiters")
1725 }
Michael Layzell93c36282017-06-04 20:43:14 -04001726 }
1727
Alex Crichton954046c2017-05-30 21:49:42 -07001728 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001729 #[cfg(not(feature = "full"))]
1730 named!(parse -> Self, do_parse!(
1731 e: parens!(syn!(Expr)) >>
1732 (ExprParen {
1733 attrs: Vec::new(),
1734 paren_token: e.0,
1735 expr: Box::new(e.1),
1736 })
1737 ));
1738
1739 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001740 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001741 outer_attrs: many0!(Attribute::parse_outer) >>
1742 e: parens!(tuple!(
1743 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001744 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001745 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001746 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001747 attrs: {
1748 let mut attrs = outer_attrs;
1749 attrs.extend((e.1).0);
1750 attrs
1751 },
David Tolnay8875fca2017-12-31 13:52:37 -05001752 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001753 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001754 })
Michael Layzell92639a52017-06-01 00:07:44 -04001755 ));
David Tolnay79777332018-01-07 10:04:42 -08001756
1757 fn description() -> Option<&'static str> {
1758 Some("parenthesized expression")
1759 }
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 Tolnay5d314dc2018-07-21 16:40:01 -07001765 outer_attrs: many0!(Attribute::parse_outer) >>
1766 elems: brackets!(tuple!(
1767 many0!(Attribute::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 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001780
1781 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001782 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001783 }
Alex Crichton954046c2017-05-30 21:49:42 -07001784 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001785
David Tolnayf2cfd722017-12-31 18:02:51 -05001786 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001787 parens!(Punctuated::parse_terminated)
1788 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001789
Michael Layzell734adb42017-06-07 16:58:31 -04001790 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001791 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001792 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001793 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001794 turbofish: option!(tuple!(
1795 punct!(::),
1796 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001797 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001798 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001799 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001800 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001801 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001802 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001803 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001804 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001805 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001806 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001807 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001808
Alex Crichton954046c2017-05-30 21:49:42 -07001809 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001810 turbofish: turbofish.map(|fish| MethodTurbofish {
1811 colon2_token: fish.0,
1812 lt_token: fish.1,
1813 args: fish.2,
1814 gt_token: fish.3,
1815 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001816 args: args.1,
1817 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001818 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001819 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001820 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001821 ));
1822
Michael Layzell734adb42017-06-07 16:58:31 -04001823 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001824 impl Synom for GenericMethodArgument {
1825 // TODO parse const generics as well
1826 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001827
1828 fn description() -> Option<&'static str> {
1829 Some("generic method argument")
1830 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001831 }
1832
1833 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001834 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001835 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001836 outer_attrs: many0!(Attribute::parse_outer) >>
1837 elems: parens!(tuple!(
1838 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001839 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001840 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001841 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001842 attrs: {
1843 let mut attrs = outer_attrs;
1844 attrs.extend((elems.1).0);
1845 attrs
1846 },
1847 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001848 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001849 })
1850 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001851
1852 fn description() -> Option<&'static str> {
1853 Some("tuple")
1854 }
Alex Crichton954046c2017-05-30 21:49:42 -07001855 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001856
Michael Layzell734adb42017-06-07 16:58:31 -04001857 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001858 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001859 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001860 if_: keyword!(if) >>
1861 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001862 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001863 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001864 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001865 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001866 else_block: option!(else_block) >>
1867 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001868 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001869 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001870 let_token: let_,
1871 eq_token: eq,
1872 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001873 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001874 brace_token: then_block.0,
1875 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001876 },
1877 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001878 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001879 })
1880 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001881
1882 fn description() -> Option<&'static str> {
1883 Some("`if let` expression")
1884 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001885 }
1886
Michael Layzell734adb42017-06-07 16:58:31 -04001887 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001888 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001889 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001890 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001891 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001892 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001893 else_block: option!(else_block) >>
1894 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001895 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001896 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001897 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001898 brace_token: then_block.0,
1899 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001900 },
1901 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001902 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001903 })
1904 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001905
1906 fn description() -> Option<&'static str> {
1907 Some("`if` expression")
1908 }
Alex Crichton954046c2017-05-30 21:49:42 -07001909 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001910
Michael Layzell734adb42017-06-07 16:58:31 -04001911 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001912 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001913 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001914 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001915 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001916 |
David Tolnay8c91b882017-12-28 23:04:32 -05001917 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001918 |
1919 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001920 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001921 (Expr::Block(ExprBlock {
1922 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001923 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001924 brace_token: else_block.0,
1925 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001926 },
1927 }))
David Tolnay939766a2016-09-23 23:48:12 -07001928 )
Alex Crichton954046c2017-05-30 21:49:42 -07001929 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001930 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001931 ));
1932
Michael Layzell734adb42017-06-07 16:58:31 -04001933 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001934 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001935 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001936 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001937 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001938 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001939 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001940 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001941 expr: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001942 block: braces!(tuple!(
1943 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001944 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001945 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001946 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001947 attrs: {
1948 let mut attrs = outer_attrs;
1949 attrs.extend((block.1).0);
1950 attrs
1951 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001952 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001953 for_token: for_,
1954 pat: Box::new(pat),
1955 in_token: in_,
1956 expr: Box::new(expr),
1957 body: Block {
1958 brace_token: block.0,
1959 stmts: (block.1).1,
1960 },
Michael Layzell92639a52017-06-01 00:07:44 -04001961 })
1962 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001963
1964 fn description() -> Option<&'static str> {
1965 Some("`for` loop")
1966 }
Alex Crichton954046c2017-05-30 21:49:42 -07001967 }
Gregory Katze5f35682016-09-27 14:20:55 -04001968
Michael Layzell734adb42017-06-07 16:58:31 -04001969 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001970 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001971 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001972 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001973 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001974 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001975 block: braces!(tuple!(
1976 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001977 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001978 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001979 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001980 attrs: {
1981 let mut attrs = outer_attrs;
1982 attrs.extend((block.1).0);
1983 attrs
1984 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001985 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001986 loop_token: loop_,
1987 body: Block {
1988 brace_token: block.0,
1989 stmts: (block.1).1,
1990 },
Michael Layzell92639a52017-06-01 00:07:44 -04001991 })
1992 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001993
1994 fn description() -> Option<&'static str> {
1995 Some("`loop`")
1996 }
Alex Crichton954046c2017-05-30 21:49:42 -07001997 }
1998
Michael Layzell734adb42017-06-07 16:58:31 -04001999 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002000 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002001 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002002 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002003 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002004 obj: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002005 braced_content: braces!(tuple!(
2006 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002007 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07002008 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05002009 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002010 attrs: {
2011 let mut attrs = outer_attrs;
2012 attrs.extend((braced_content.1).0);
2013 attrs
2014 },
David Tolnay8875fca2017-12-31 13:52:37 -05002015 expr: Box::new(obj),
2016 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07002017 brace_token: braced_content.0,
2018 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002019 })
2020 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002021
2022 fn description() -> Option<&'static str> {
2023 Some("`match` expression")
2024 }
Alex Crichton954046c2017-05-30 21:49:42 -07002025 }
David Tolnay1978c672016-10-27 22:05:52 -07002026
Michael Layzell734adb42017-06-07 16:58:31 -04002027 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002028 impl Synom for ExprTryBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002029 named!(parse -> Self, do_parse!(
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002030 try_token: keyword!(try) >>
2031 block: syn!(Block) >>
2032 (ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002033 attrs: Vec::new(),
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002034 try_token: try_token,
2035 block: block,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002036 })
Michael Layzell92639a52017-06-01 00:07:44 -04002037 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002038
2039 fn description() -> Option<&'static str> {
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002040 Some("`try` block")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002041 }
Alex Crichton954046c2017-05-30 21:49:42 -07002042 }
Arnavion02ef13f2017-04-25 00:54:31 -07002043
Michael Layzell734adb42017-06-07 16:58:31 -04002044 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002045 impl Synom for ExprYield {
2046 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002047 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002048 expr: option!(syn!(Expr)) >>
2049 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002050 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002051 yield_token: yield_,
2052 expr: expr.map(Box::new),
2053 })
2054 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002055
2056 fn description() -> Option<&'static str> {
2057 Some("`yield` expression")
2058 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002059 }
2060
2061 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002062 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002063 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002064 attrs: many0!(Attribute::parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002065 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002066 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002067 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002068 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002069 body: do_parse!(
2070 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002071 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2072 true => alt!(
2073 input_end!() => { |_| None }
2074 |
2075 punct!(,) => { Some }
2076 )
Alex Crichton03b30272017-08-28 09:35:24 -07002077 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002078 false => option!(punct!(,))
2079 ) >>
2080 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002081 ) >>
2082 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002083 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002084 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002085 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002086 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002087 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002088 body: Box::new(body.0),
2089 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002090 })
2091 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002092
2093 fn description() -> Option<&'static str> {
2094 Some("`match` arm")
2095 }
Alex Crichton954046c2017-05-30 21:49:42 -07002096 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002097
Michael Layzell734adb42017-06-07 16:58:31 -04002098 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002099 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnay713a6722018-07-21 15:49:40 -07002100 attrs: many0!(Attribute::parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002101 asyncness: option!(keyword!(async)) >>
2102 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002103 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002104 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002105 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002106 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002107 ret_and_body: alt!(
2108 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002109 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002110 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002111 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002112 (
2113 ReturnType::Type(arrow, Box::new(ty)),
2114 Expr::Block(ExprBlock {
2115 attrs: Vec::new(),
2116 block: body,
2117 },
2118 ))
David Tolnay89e05672016-10-02 14:39:42 -07002119 )
2120 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002121 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002122 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002123 (Expr::Closure(ExprClosure {
2124 attrs: attrs,
2125 asyncness: asyncness,
2126 movability: movability,
2127 capture: capture,
2128 or1_token: or1,
2129 inputs: inputs,
2130 or2_token: or2,
2131 output: ret_and_body.0,
2132 body: Box::new(ret_and_body.1),
2133 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002134 ));
2135
2136 #[cfg(feature = "full")]
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002137 named!(unstable_async_block -> ExprVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07002138 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07002139 many0!(Attribute::parse_outer) >>
2140 keyword!(async) >>
2141 option!(keyword!(move)) >>
2142 syn!(Block) >>
David Tolnay9be32582018-07-31 22:37:26 -07002143 end: call!(verbatim::grab_cursor) >>
2144 (ExprVerbatim {
2145 tts: verbatim::token_range(begin..end),
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002146 })
2147 ));
2148
2149 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002150 named!(fn_arg -> FnArg, do_parse!(
2151 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002152 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002153 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002154 if let Some((colon, ty)) = ty {
2155 FnArg::Captured(ArgCaptured {
2156 pat: pat,
2157 colon_token: colon,
2158 ty: ty,
2159 })
2160 } else {
2161 FnArg::Inferred(pat)
2162 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002163 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002164 ));
2165
Michael Layzell734adb42017-06-07 16:58:31 -04002166 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002167 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002168 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002169 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002170 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002171 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002172 cond: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002173 block: braces!(tuple!(
2174 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002175 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002176 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002177 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002178 attrs: {
2179 let mut attrs = outer_attrs;
2180 attrs.extend((block.1).0);
2181 attrs
2182 },
2183 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002184 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002185 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002186 body: Block {
2187 brace_token: block.0,
2188 stmts: (block.1).1,
2189 },
Michael Layzell92639a52017-06-01 00:07:44 -04002190 })
2191 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002192
2193 fn description() -> Option<&'static str> {
2194 Some("`while` expression")
2195 }
Alex Crichton954046c2017-05-30 21:49:42 -07002196 }
2197
Michael Layzell734adb42017-06-07 16:58:31 -04002198 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002199 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002200 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002201 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002202 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002203 while_: keyword!(while) >>
2204 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002205 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002206 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002207 value: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002208 block: braces!(tuple!(
2209 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002210 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002211 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002212 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002213 attrs: {
2214 let mut attrs = outer_attrs;
2215 attrs.extend((block.1).0);
2216 attrs
2217 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002218 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002219 while_token: while_,
2220 let_token: let_,
2221 pats: pats,
2222 eq_token: eq,
2223 expr: Box::new(value),
2224 body: Block {
2225 brace_token: block.0,
2226 stmts: (block.1).1,
2227 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002228 })
2229 ));
David Tolnay79777332018-01-07 10:04:42 -08002230
2231 fn description() -> Option<&'static str> {
2232 Some("`while let` expression")
2233 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002234 }
2235
2236 #[cfg(feature = "full")]
2237 impl Synom for Label {
2238 named!(parse -> Self, do_parse!(
2239 name: syn!(Lifetime) >>
2240 colon: punct!(:) >>
2241 (Label {
2242 name: name,
2243 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002244 })
2245 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002246
2247 fn description() -> Option<&'static str> {
2248 Some("`while let` expression")
2249 }
Alex Crichton954046c2017-05-30 21:49:42 -07002250 }
2251
Michael Layzell734adb42017-06-07 16:58:31 -04002252 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002253 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002254 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002255 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002256 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002257 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002258 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002259 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002260 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002261 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002262 })
2263 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002264
2265 fn description() -> Option<&'static str> {
2266 Some("`continue`")
2267 }
Alex Crichton954046c2017-05-30 21:49:42 -07002268 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002269
Michael Layzell734adb42017-06-07 16:58:31 -04002270 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002271 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002272 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002273 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002274 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002275 // We can't allow blocks after a `break` expression when we wouldn't
2276 // allow structs, as this expression is ambiguous.
2277 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002278 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002279 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002280 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002281 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002282 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002283 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002284 ));
2285
Michael Layzell734adb42017-06-07 16:58:31 -04002286 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002287 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002288 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002289 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002290 // NOTE: return is greedy and eats blocks after it even when in a
2291 // position where structs are not allowed, such as in if statement
2292 // conditions. For example:
2293 //
David Tolnaybcf26022017-12-25 22:10:52 -05002294 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002295 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002296 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002297 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002298 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002299 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002300 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002301 ));
2302
Michael Layzell734adb42017-06-07 16:58:31 -04002303 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002304 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002305 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002306 outer_attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002307 path: syn!(Path) >>
2308 data: braces!(do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002309 inner_attrs: many0!(Attribute::parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002310 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002311 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2312 dots: punct!(..) >>
2313 base: syn!(Expr) >>
2314 (dots, base)
2315 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002316 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002317 )) >>
2318 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002319 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002320 let (dots, rest) = match base.and_then(|b| b) {
2321 Some((dots, base)) => (Some(dots), Some(base)),
2322 None => (None, None),
2323 };
2324 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002325 attrs: {
2326 let mut attrs = outer_attrs;
2327 attrs.extend(inner_attrs);
2328 attrs
2329 },
Michael Layzell92639a52017-06-01 00:07:44 -04002330 brace_token: brace,
2331 path: path,
2332 fields: fields,
2333 dot2_token: dots,
2334 rest: rest.map(Box::new),
2335 }
2336 })
2337 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002338
2339 fn description() -> Option<&'static str> {
2340 Some("struct literal expression")
2341 }
Alex Crichton954046c2017-05-30 21:49:42 -07002342 }
2343
Michael Layzell734adb42017-06-07 16:58:31 -04002344 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002345 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002346 named!(parse -> Self, do_parse!(
2347 attrs: many0!(Attribute::parse_outer) >>
2348 field_value: alt!(
2349 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2350 |
2351 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002352 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002353 None,
2354 Expr::Path(ExprPath {
2355 attrs: Vec::new(),
2356 qself: None,
2357 path: name.into(),
2358 }),
2359 ))
2360 ) >>
2361 (FieldValue {
2362 attrs: attrs,
2363 member: field_value.0,
2364 colon_token: field_value.1,
2365 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002366 })
2367 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002368
2369 fn description() -> Option<&'static str> {
2370 Some("field-value pair: `field: value`")
2371 }
Alex Crichton954046c2017-05-30 21:49:42 -07002372 }
David Tolnay055a7042016-10-02 19:23:54 -07002373
Michael Layzell734adb42017-06-07 16:58:31 -04002374 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002375 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002376 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002377 outer_attrs: many0!(Attribute::parse_outer) >>
2378 data: brackets!(tuple!(
2379 many0!(Attribute::parse_inner),
2380 syn!(Expr),
2381 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002382 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002383 )) >>
2384 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002385 attrs: {
2386 let mut attrs = outer_attrs;
2387 attrs.extend((data.1).0);
2388 attrs
2389 },
2390 expr: Box::new((data.1).1),
2391 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002392 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002393 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002394 })
2395 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002396
2397 fn description() -> Option<&'static str> {
2398 Some("repeated array literal: `[val; N]`")
2399 }
Alex Crichton954046c2017-05-30 21:49:42 -07002400 }
David Tolnay055a7042016-10-02 19:23:54 -07002401
Michael Layzell734adb42017-06-07 16:58:31 -04002402 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002403 impl Synom for ExprUnsafe {
2404 named!(parse -> Self, do_parse!(
2405 unsafe_: keyword!(unsafe) >>
2406 b: syn!(Block) >>
2407 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002408 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002409 unsafe_token: unsafe_,
2410 block: b,
2411 })
2412 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002413
2414 fn description() -> Option<&'static str> {
2415 Some("unsafe block: `unsafe { .. }`")
2416 }
Nika Layzell640832a2017-12-04 13:37:09 -05002417 }
2418
2419 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002420 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002421 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002422 outer_attrs: many0!(Attribute::parse_outer) >>
2423 block: braces!(tuple!(
2424 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002425 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002426 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002427 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002428 attrs: {
2429 let mut attrs = outer_attrs;
2430 attrs.extend((block.1).0);
2431 attrs
2432 },
2433 block: Block {
2434 brace_token: block.0,
2435 stmts: (block.1).1,
2436 },
Michael Layzell92639a52017-06-01 00:07:44 -04002437 })
2438 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002439
2440 fn description() -> Option<&'static str> {
2441 Some("block: `{ .. }`")
2442 }
Alex Crichton954046c2017-05-30 21:49:42 -07002443 }
David Tolnay89e05672016-10-02 14:39:42 -07002444
Michael Layzell734adb42017-06-07 16:58:31 -04002445 #[cfg(feature = "full")]
David Tolnay5d08ae62018-08-01 00:08:48 -07002446 named!(unstable_labeled_block -> ExprVerbatim, do_parse!(
2447 begin: call!(verbatim::grab_cursor) >>
2448 many0!(Attribute::parse_outer) >>
David Tolnay61e15e52018-08-01 00:28:36 -07002449 syn!(Label) >>
David Tolnay5d08ae62018-08-01 00:08:48 -07002450 braces!(tuple!(
2451 many0!(Attribute::parse_inner),
2452 call!(Block::parse_within),
2453 )) >>
2454 end: call!(verbatim::grab_cursor) >>
2455 (ExprVerbatim {
2456 tts: verbatim::token_range(begin..end),
2457 })
2458 ));
2459
2460 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002461 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002462 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002463 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002464 (ExprRange {
2465 attrs: Vec::new(),
2466 from: None,
2467 to: hi.map(Box::new),
2468 limits: limits,
2469 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002470 ));
2471
Michael Layzell734adb42017-06-07 16:58:31 -04002472 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002473 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002474 named!(parse -> Self, alt!(
2475 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002476 punct!(..=) => { RangeLimits::Closed }
2477 |
2478 // Must come before Dot2
David Tolnay7ac699c2018-08-24 14:00:58 -04002479 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.spans)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002480 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002481 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002482 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002483
2484 fn description() -> Option<&'static str> {
2485 Some("range limit: `..`, `...` or `..=`")
2486 }
Alex Crichton954046c2017-05-30 21:49:42 -07002487 }
David Tolnay438c9052016-10-07 23:24:48 -07002488
Alex Crichton954046c2017-05-30 21:49:42 -07002489 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002490 #[cfg(not(feature = "full"))]
2491 named!(parse -> Self, do_parse!(
2492 pair: qpath >>
2493 (ExprPath {
2494 attrs: Vec::new(),
2495 qself: pair.0,
2496 path: pair.1,
2497 })
2498 ));
2499
2500 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002501 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07002502 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002503 pair: qpath >>
2504 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002505 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002506 qself: pair.0,
2507 path: pair.1,
2508 })
2509 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002510
2511 fn description() -> Option<&'static str> {
2512 Some("path: `a::b::c`")
2513 }
Alex Crichton954046c2017-05-30 21:49:42 -07002514 }
David Tolnay42602292016-10-01 22:25:45 -07002515
David Tolnay9cc2f092018-08-24 15:51:37 -04002516 named!(path -> Path, do_parse!(
2517 colon: option!(punct!(::)) >>
2518 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2519 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2520 (Path {
2521 leading_colon: colon,
2522 segments: segments,
2523 })
2524 ));
2525
2526 named!(path_segment -> PathSegment, alt!(
2527 do_parse!(
2528 ident: syn!(Ident) >>
2529 colon2: punct!(::) >>
2530 lt: punct!(<) >>
2531 args: call!(Punctuated::parse_terminated) >>
2532 gt: punct!(>) >>
2533 (PathSegment {
2534 ident: ident,
2535 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2536 colon2_token: Some(colon2),
2537 lt_token: lt,
2538 args: args,
2539 gt_token: gt,
2540 }),
2541 })
2542 )
2543 |
2544 mod_style_path_segment
2545 ));
2546
2547 named!(qpath -> (Option<QSelf>, Path), alt!(
2548 map!(path, |p| (None, p))
2549 |
2550 do_parse!(
2551 lt: punct!(<) >>
2552 this: syn!(Type) >>
2553 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2554 gt: punct!(>) >>
2555 colon2: punct!(::) >>
2556 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2557 ({
2558 let (pos, as_, path) = match path {
2559 Some((as_, mut path)) => {
2560 let pos = path.segments.len();
2561 path.segments.push_punct(colon2);
2562 path.segments.extend(rest.into_pairs());
2563 (pos, Some(as_), path)
2564 }
2565 None => {
2566 (0, None, Path {
2567 leading_colon: Some(colon2),
2568 segments: rest,
2569 })
2570 }
2571 };
2572 (Some(QSelf {
2573 lt_token: lt,
2574 ty: Box::new(this),
2575 position: pos,
2576 as_token: as_,
2577 gt_token: gt,
2578 }), path)
2579 })
2580 )
2581 |
2582 map!(keyword!(self), |s| (None, s.into()))
2583 ));
2584
David Tolnay85b69a42017-12-27 20:43:10 -05002585 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002586
David Tolnay8875fca2017-12-31 13:52:37 -05002587 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002588
Michael Layzell734adb42017-06-07 16:58:31 -04002589 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002590 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002591 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002592 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002593 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002594 brace_token: stmts.0,
2595 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002596 })
2597 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002598
2599 fn description() -> Option<&'static str> {
2600 Some("block: `{ .. }`")
2601 }
Alex Crichton954046c2017-05-30 21:49:42 -07002602 }
David Tolnay939766a2016-09-23 23:48:12 -07002603
Michael Layzell734adb42017-06-07 16:58:31 -04002604 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002605 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002606 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002607 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002608 mut standalone: many0!(do_parse!(
2609 stmt: syn!(Stmt) >>
2610 many0!(punct!(;)) >>
2611 (stmt)
2612 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002613 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002614 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002615 mut e: syn!(Expr) >>
2616 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002617 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002618 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002619 })
2620 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002621 (match last {
2622 None => standalone,
2623 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002624 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002625 standalone
2626 }
2627 })
2628 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002629 }
2630
Michael Layzell734adb42017-06-07 16:58:31 -04002631 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002632 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002633 named!(parse -> Self, alt!(
2634 stmt_mac
2635 |
2636 stmt_local
2637 |
2638 stmt_item
2639 |
Michael Layzell35418782017-06-07 09:20:25 -04002640 stmt_blockexpr
2641 |
Michael Layzell92639a52017-06-01 00:07:44 -04002642 stmt_expr
2643 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002644
2645 fn description() -> Option<&'static str> {
2646 Some("statement")
2647 }
Alex Crichton954046c2017-05-30 21:49:42 -07002648 }
David Tolnay939766a2016-09-23 23:48:12 -07002649
Michael Layzell734adb42017-06-07 16:58:31 -04002650 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002651 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002652 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002653 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002654 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002655 // Only parse braces here; paren and bracket will get parsed as
2656 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002657 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002658 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002659 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002660 attrs: attrs,
2661 ident: None,
2662 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002663 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002664 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002665 delimiter: MacroDelimiter::Brace(data.0),
2666 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002667 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002668 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002669 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002670 ));
2671
Michael Layzell734adb42017-06-07 16:58:31 -04002672 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002673 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002674 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002675 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002676 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002677 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002678 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2679 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002680 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002681 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002682 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002683 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002684 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2685 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2686 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002687 }))
David Tolnay191e0582016-10-02 18:31:09 -07002688 ));
2689
Michael Layzell734adb42017-06-07 16:58:31 -04002690 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002691 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002692
Michael Layzell734adb42017-06-07 16:58:31 -04002693 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002694 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002695 mut attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002696 mut e: expr_nosemi >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002697 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002698 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002699 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002700 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002701 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002702 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002703 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002704 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002705 }
2706 })
2707 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002708
Michael Layzell734adb42017-06-07 16:58:31 -04002709 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002710 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002711 mut attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002712 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002713 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002714 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002715 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002716 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002717 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002718 })
David Tolnay939766a2016-09-23 23:48:12 -07002719 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002720
Michael Layzell734adb42017-06-07 16:58:31 -04002721 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002722 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002723 named!(parse -> Self, alt!(
2724 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2725 |
2726 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2727 |
2728 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2729 |
2730 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2731 |
2732 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2733 |
David Tolnay323279a2017-12-29 11:26:32 -05002734 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002735 |
2736 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2737 |
2738 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2739 |
2740 syn!(PatPath) => { Pat::Path }
2741 |
2742 syn!(PatTuple) => { Pat::Tuple }
2743 |
2744 syn!(PatRef) => { Pat::Ref }
2745 |
2746 syn!(PatSlice) => { Pat::Slice }
2747 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002748
2749 fn description() -> Option<&'static str> {
2750 Some("pattern")
2751 }
Alex Crichton954046c2017-05-30 21:49:42 -07002752 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002753
Michael Layzell734adb42017-06-07 16:58:31 -04002754 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002755 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002756 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002757 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002758 |u| PatWild { underscore_token: u }
2759 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002760
2761 fn description() -> Option<&'static str> {
2762 Some("wild pattern: `_`")
2763 }
Alex Crichton954046c2017-05-30 21:49:42 -07002764 }
David Tolnay84aa0752016-10-02 23:01:13 -07002765
Michael Layzell734adb42017-06-07 16:58:31 -04002766 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002767 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002768 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002769 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002770 pat: syn!(Pat) >>
2771 (PatBox {
2772 pat: Box::new(pat),
2773 box_token: boxed,
2774 })
2775 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002776
2777 fn description() -> Option<&'static str> {
2778 Some("box pattern")
2779 }
Alex Crichton954046c2017-05-30 21:49:42 -07002780 }
2781
Michael Layzell734adb42017-06-07 16:58:31 -04002782 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002783 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002784 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002785 by_ref: option!(keyword!(ref)) >>
2786 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002787 name: alt!(
2788 syn!(Ident)
2789 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002790 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002791 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002792 not!(punct!(<)) >>
2793 not!(punct!(::)) >>
2794 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002795 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002796 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002797 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002798 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002799 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002800 })
2801 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002802
2803 fn description() -> Option<&'static str> {
2804 Some("pattern identifier binding")
2805 }
Alex Crichton954046c2017-05-30 21:49:42 -07002806 }
2807
Michael Layzell734adb42017-06-07 16:58:31 -04002808 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002809 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002810 named!(parse -> Self, do_parse!(
2811 path: syn!(Path) >>
2812 tuple: syn!(PatTuple) >>
2813 (PatTupleStruct {
2814 path: path,
2815 pat: tuple,
2816 })
2817 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002818
2819 fn description() -> Option<&'static str> {
2820 Some("tuple struct pattern")
2821 }
Alex Crichton954046c2017-05-30 21:49:42 -07002822 }
2823
Michael Layzell734adb42017-06-07 16:58:31 -04002824 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002825 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002826 named!(parse -> Self, do_parse!(
2827 path: syn!(Path) >>
2828 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002829 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002830 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002831 (fields, base)
2832 )) >>
2833 (PatStruct {
2834 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002835 fields: (data.1).0,
2836 brace_token: data.0,
2837 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002838 })
2839 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002840
2841 fn description() -> Option<&'static str> {
2842 Some("struct pattern")
2843 }
Alex Crichton954046c2017-05-30 21:49:42 -07002844 }
2845
Michael Layzell734adb42017-06-07 16:58:31 -04002846 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002847 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002848 named!(parse -> Self, alt!(
2849 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002850 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002851 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002852 pat: syn!(Pat) >>
2853 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002854 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002855 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002856 attrs: Vec::new(),
2857 colon_token: Some(colon),
2858 })
2859 )
2860 |
2861 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002862 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002863 by_ref: option!(keyword!(ref)) >>
2864 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002865 ident: syn!(Ident) >>
2866 ({
2867 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002868 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002869 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002870 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002871 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002872 }.into();
2873 if let Some(boxed) = boxed {
2874 pat = PatBox {
2875 pat: Box::new(pat),
2876 box_token: boxed,
2877 }.into();
2878 }
2879 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002880 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002881 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002882 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002883 colon_token: None,
2884 }
2885 })
2886 )
2887 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002888
2889 fn description() -> Option<&'static str> {
2890 Some("field pattern")
2891 }
Alex Crichton954046c2017-05-30 21:49:42 -07002892 }
2893
David Tolnay85b69a42017-12-27 20:43:10 -05002894 impl Synom for Member {
2895 named!(parse -> Self, alt!(
2896 syn!(Ident) => { Member::Named }
2897 |
2898 syn!(Index) => { Member::Unnamed }
2899 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002900
2901 fn description() -> Option<&'static str> {
2902 Some("field member")
2903 }
David Tolnay85b69a42017-12-27 20:43:10 -05002904 }
2905
David Tolnay85b69a42017-12-27 20:43:10 -05002906 impl Synom for Index {
2907 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002908 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002909 ({
David Tolnay360efd22018-01-04 23:35:26 -08002910 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002911 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002912 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002913 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002914 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002915 })
David Tolnay85b69a42017-12-27 20:43:10 -05002916 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002917
2918 fn description() -> Option<&'static str> {
2919 Some("field index")
2920 }
David Tolnay85b69a42017-12-27 20:43:10 -05002921 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002922
Michael Layzell734adb42017-06-07 16:58:31 -04002923 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002924 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002925 named!(parse -> Self, map!(
2926 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002927 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002928 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002929
2930 fn description() -> Option<&'static str> {
2931 Some("path pattern")
2932 }
Alex Crichton954046c2017-05-30 21:49:42 -07002933 }
David Tolnay9636c052016-10-02 17:11:17 -07002934
Michael Layzell734adb42017-06-07 16:58:31 -04002935 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002936 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002937 named!(parse -> Self, do_parse!(
2938 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002939 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002940 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2941 tuple!(punct!(..), option!(punct!(,)))
2942 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002943 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002944 Some((_, Some(_))) => true,
2945 _ => false,
2946 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002947 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002948 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002949 )) >>
2950 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002951 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002952 let (dotdot, trailing) = match dotdot {
2953 Some((a, b)) => (Some(a), Some(b)),
2954 None => (None, None),
2955 };
2956 PatTuple {
2957 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002958 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002959 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002960 comma_token: trailing.unwrap_or_default(),
2961 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002962 }
2963 })
2964 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002965
2966 fn description() -> Option<&'static str> {
2967 Some("tuple pattern")
2968 }
Alex Crichton954046c2017-05-30 21:49:42 -07002969 }
David Tolnayfbb73232016-10-03 01:00:06 -07002970
Michael Layzell734adb42017-06-07 16:58:31 -04002971 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002972 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002973 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002974 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002975 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002976 pat: syn!(Pat) >>
2977 (PatRef {
2978 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002979 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002980 and_token: and,
2981 })
2982 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002983
2984 fn description() -> Option<&'static str> {
2985 Some("reference pattern")
2986 }
Alex Crichton954046c2017-05-30 21:49:42 -07002987 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002988
Michael Layzell734adb42017-06-07 16:58:31 -04002989 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002990 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002991 named!(parse -> Self, do_parse!(
2992 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002993 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002994 return parse_error(); // these need to be parsed by pat_path
2995 } else {
2996 PatLit {
2997 expr: Box::new(lit),
2998 }
2999 })
3000 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003001
3002 fn description() -> Option<&'static str> {
3003 Some("literal pattern")
3004 }
Alex Crichton954046c2017-05-30 21:49:42 -07003005 }
David Tolnaye1310902016-10-29 23:40:00 -07003006
Michael Layzell734adb42017-06-07 16:58:31 -04003007 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003008 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04003009 named!(parse -> Self, do_parse!(
3010 lo: pat_lit_expr >>
3011 limits: syn!(RangeLimits) >>
3012 hi: pat_lit_expr >>
3013 (PatRange {
3014 lo: Box::new(lo),
3015 hi: Box::new(hi),
3016 limits: limits,
3017 })
3018 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003019
3020 fn description() -> Option<&'static str> {
3021 Some("range pattern")
3022 }
Alex Crichton954046c2017-05-30 21:49:42 -07003023 }
David Tolnaye1310902016-10-29 23:40:00 -07003024
Michael Layzell734adb42017-06-07 16:58:31 -04003025 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07003026 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003027 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07003028 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05003029 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07003030 |
David Tolnay8c91b882017-12-28 23:04:32 -05003031 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07003032 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05003033 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003034 Expr::Unary(ExprUnary {
3035 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003036 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05003037 expr: Box::new(v)
3038 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003039 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05003040 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003041 })
3042 ));
David Tolnay8b308c22016-10-03 01:24:10 -07003043
Michael Layzell734adb42017-06-07 16:58:31 -04003044 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003045 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04003046 named!(parse -> Self, map!(
3047 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05003048 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003049 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003050 dots: punct!(..) >>
3051 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003052 (dots, trailing)
3053 )) >>
3054 after: cond!(
3055 match middle {
3056 Some((_, ref trailing)) => trailing.is_some(),
3057 _ => false,
3058 },
David Tolnayf2cfd722017-12-31 18:02:51 -05003059 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04003060 ) >>
3061 (before, middle, after)
3062 )),
David Tolnay8875fca2017-12-31 13:52:37 -05003063 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05003064 let mut before: Punctuated<Pat, Token![,]> = before;
3065 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08003066 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04003067 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04003068 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04003069 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04003070 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04003071 }),
3072 bracket_token: brackets,
3073 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05003074 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04003075 None
David Tolnaydc03aec2017-12-30 01:54:18 -05003076 } else {
David Tolnay56080682018-01-06 14:01:52 -08003077 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04003078 }
3079 }),
3080 front: before,
3081 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07003082 }
Alex Crichton954046c2017-05-30 21:49:42 -07003083 }
Michael Layzell92639a52017-06-01 00:07:44 -04003084 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003085
3086 fn description() -> Option<&'static str> {
3087 Some("slice pattern")
3088 }
Alex Crichton954046c2017-05-30 21:49:42 -07003089 }
David Tolnay323279a2017-12-29 11:26:32 -05003090
3091 #[cfg(feature = "full")]
3092 impl Synom for PatMacro {
3093 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003094
3095 fn description() -> Option<&'static str> {
3096 Some("macro pattern")
3097 }
David Tolnay323279a2017-12-29 11:26:32 -05003098 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003099}
3100
David Tolnayf4bbbd92016-09-23 14:41:55 -07003101#[cfg(feature = "printing")]
3102mod printing {
3103 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003104 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003105 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003106 use proc_macro2::{Literal, TokenStream};
3107 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003108
David Tolnaybcf26022017-12-25 22:10:52 -05003109 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003110 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003111 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003112 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003113 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003114 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003115 e.to_tokens(tokens);
3116 });
3117 } else {
3118 e.to_tokens(tokens);
3119 }
3120 }
3121
David Tolnay8c91b882017-12-28 23:04:32 -05003122 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003123 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003124 tokens.append_all(attrs.outer());
3125 }
Michael Layzell734adb42017-06-07 16:58:31 -04003126
David Tolnayd997aef2018-07-21 18:42:31 -07003127 #[cfg(feature = "full")]
3128 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3129 tokens.append_all(attrs.inner());
3130 }
3131
David Tolnay8c91b882017-12-28 23:04:32 -05003132 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003133 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3134
3135 #[cfg(not(feature = "full"))]
3136 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003137
Michael Layzell734adb42017-06-07 16:58:31 -04003138 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003139 impl ToTokens for ExprBox {
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.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003143 self.expr.to_tokens(tokens);
3144 }
3145 }
3146
Michael Layzell734adb42017-06-07 16:58:31 -04003147 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003148 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003149 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003150 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003151 self.place.to_tokens(tokens);
3152 self.arrow_token.to_tokens(tokens);
3153 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003154 }
3155 }
3156
Michael Layzell734adb42017-06-07 16:58:31 -04003157 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003158 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003159 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003160 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003162 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003163 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003164 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003165 }
3166 }
3167
3168 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003169 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003170 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003171 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003172 self.paren_token.surround(tokens, |tokens| {
3173 self.args.to_tokens(tokens);
3174 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003175 }
3176 }
3177
Michael Layzell734adb42017-06-07 16:58:31 -04003178 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003179 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003180 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003181 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003182 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003183 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003184 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003185 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003186 self.paren_token.surround(tokens, |tokens| {
3187 self.args.to_tokens(tokens);
3188 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003189 }
3190 }
3191
Michael Layzell734adb42017-06-07 16:58:31 -04003192 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003193 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003194 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003195 self.colon2_token.to_tokens(tokens);
3196 self.lt_token.to_tokens(tokens);
3197 self.args.to_tokens(tokens);
3198 self.gt_token.to_tokens(tokens);
3199 }
3200 }
3201
3202 #[cfg(feature = "full")]
3203 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003204 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003205 match *self {
3206 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3207 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3208 }
3209 }
3210 }
3211
3212 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003213 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003214 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003215 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003216 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003217 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003218 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003219 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003220 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003221 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003222 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003223 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003224 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003225 }
3226 }
3227
3228 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003229 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003230 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003231 self.left.to_tokens(tokens);
3232 self.op.to_tokens(tokens);
3233 self.right.to_tokens(tokens);
3234 }
3235 }
3236
3237 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003238 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003239 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003240 self.op.to_tokens(tokens);
3241 self.expr.to_tokens(tokens);
3242 }
3243 }
3244
David Tolnay8c91b882017-12-28 23:04:32 -05003245 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003246 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003247 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003248 self.lit.to_tokens(tokens);
3249 }
3250 }
3251
Alex Crichton62a0a592017-05-22 13:58:53 -07003252 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003253 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003254 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003255 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003256 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003257 self.ty.to_tokens(tokens);
3258 }
3259 }
3260
David Tolnay0cf94f22017-12-28 23:46:26 -05003261 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003262 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003263 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003264 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003265 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003266 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003267 self.ty.to_tokens(tokens);
3268 }
3269 }
3270
Michael Layzell734adb42017-06-07 16:58:31 -04003271 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003272 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003273 if let Some((ref else_token, ref else_)) = *else_ {
3274 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003275
3276 // If we are not one of the valid expressions to exist in an else
3277 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003278 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003279 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003280 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003281 }
3282 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003283 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003284 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003285 });
3286 }
3287 }
3288 }
3289 }
3290
3291 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003292 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003293 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003294 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003295 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003296 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003297 self.then_branch.to_tokens(tokens);
3298 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003299 }
3300 }
3301
Michael Layzell734adb42017-06-07 16:58:31 -04003302 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003303 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003304 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003305 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003306 self.if_token.to_tokens(tokens);
3307 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003308 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003309 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003310 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003311 self.then_branch.to_tokens(tokens);
3312 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003313 }
3314 }
3315
Michael Layzell734adb42017-06-07 16:58:31 -04003316 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003317 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003318 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003319 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003320 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003321 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003322 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003323 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003324 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003325 tokens.append_all(&self.body.stmts);
3326 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003327 }
3328 }
3329
Michael Layzell734adb42017-06-07 16:58:31 -04003330 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003331 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003332 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003333 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003334 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003335 self.while_token.to_tokens(tokens);
3336 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003337 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003338 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003339 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003340 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003341 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003342 tokens.append_all(&self.body.stmts);
3343 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003344 }
3345 }
3346
Michael Layzell734adb42017-06-07 16:58:31 -04003347 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003348 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003349 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003350 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003351 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003352 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003353 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003354 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003355 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003356 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003357 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003358 tokens.append_all(&self.body.stmts);
3359 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003360 }
3361 }
3362
Michael Layzell734adb42017-06-07 16:58:31 -04003363 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003364 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003365 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003366 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003367 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003368 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003369 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003370 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003371 tokens.append_all(&self.body.stmts);
3372 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003373 }
3374 }
3375
Michael Layzell734adb42017-06-07 16:58:31 -04003376 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003377 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003378 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003379 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003380 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003381 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003382 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003383 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003384 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003385 arm.to_tokens(tokens);
3386 // Ensure that we have a comma after a non-block arm, except
3387 // for the last one.
3388 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003389 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003390 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003391 }
3392 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003393 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003394 }
3395 }
3396
Michael Layzell734adb42017-06-07 16:58:31 -04003397 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003398 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003399 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003400 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003401 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003402 self.block.to_tokens(tokens);
3403 }
3404 }
3405
Michael Layzell734adb42017-06-07 16:58:31 -04003406 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003407 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003408 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003409 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003410 self.yield_token.to_tokens(tokens);
3411 self.expr.to_tokens(tokens);
3412 }
3413 }
3414
3415 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003416 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003417 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003418 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003419 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003420 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003421 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003422 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003423 for input in self.inputs.pairs() {
3424 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003425 FnArg::Captured(ArgCaptured {
3426 ref pat,
3427 ty: Type::Infer(_),
3428 ..
3429 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003430 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003431 }
David Tolnay56080682018-01-06 14:01:52 -08003432 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003433 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003434 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003435 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003436 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003437 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003438 self.body.to_tokens(tokens);
3439 }
3440 }
3441
Michael Layzell734adb42017-06-07 16:58:31 -04003442 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003443 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003444 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003445 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003446 self.unsafe_token.to_tokens(tokens);
3447 self.block.to_tokens(tokens);
3448 }
3449 }
3450
3451 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003452 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003453 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003454 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003455 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003456 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003457 tokens.append_all(&self.block.stmts);
3458 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003459 }
3460 }
3461
Michael Layzell734adb42017-06-07 16:58:31 -04003462 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003463 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003464 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003465 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003466 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003467 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003468 self.right.to_tokens(tokens);
3469 }
3470 }
3471
Michael Layzell734adb42017-06-07 16:58:31 -04003472 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003473 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003474 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003475 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003476 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003477 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003478 self.right.to_tokens(tokens);
3479 }
3480 }
3481
3482 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003483 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003484 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003485 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003486 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003487 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003488 }
3489 }
3490
David Tolnay85b69a42017-12-27 20:43:10 -05003491 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003492 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003493 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003494 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003495 Member::Unnamed(ref index) => index.to_tokens(tokens),
3496 }
3497 }
3498 }
3499
David Tolnay85b69a42017-12-27 20:43:10 -05003500 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003501 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003502 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3503 lit.set_span(self.span);
3504 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003505 }
3506 }
3507
3508 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003509 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003510 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003511 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003512 self.bracket_token.surround(tokens, |tokens| {
3513 self.index.to_tokens(tokens);
3514 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003515 }
3516 }
3517
Michael Layzell734adb42017-06-07 16:58:31 -04003518 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003519 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003520 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003521 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003522 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003523 match self.limits {
3524 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3525 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3526 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003527 self.to.to_tokens(tokens);
3528 }
3529 }
3530
3531 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003532 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003533 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003534 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003535 }
3536 }
3537
Michael Layzell734adb42017-06-07 16:58:31 -04003538 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003539 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003540 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003541 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003542 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003543 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003544 self.expr.to_tokens(tokens);
3545 }
3546 }
3547
Michael Layzell734adb42017-06-07 16:58:31 -04003548 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003549 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003550 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003551 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003552 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003553 self.label.to_tokens(tokens);
3554 self.expr.to_tokens(tokens);
3555 }
3556 }
3557
Michael Layzell734adb42017-06-07 16:58:31 -04003558 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003559 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003560 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003561 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003562 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003563 self.label.to_tokens(tokens);
3564 }
3565 }
3566
Michael Layzell734adb42017-06-07 16:58:31 -04003567 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003568 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003569 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003570 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003571 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003572 self.expr.to_tokens(tokens);
3573 }
3574 }
3575
Michael Layzell734adb42017-06-07 16:58:31 -04003576 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003577 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003578 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003579 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003580 self.mac.to_tokens(tokens);
3581 }
3582 }
3583
3584 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003585 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003586 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003587 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003588 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003589 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003590 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003591 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003592 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003593 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003594 self.rest.to_tokens(tokens);
3595 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003596 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003597 }
3598 }
3599
Michael Layzell734adb42017-06-07 16:58:31 -04003600 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003601 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003602 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003603 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003604 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003605 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003606 self.expr.to_tokens(tokens);
3607 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003608 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003609 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003610 }
3611 }
3612
David Tolnaye98775f2017-12-28 23:17:00 -05003613 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003614 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003615 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003616 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003617 self.group_token.surround(tokens, |tokens| {
3618 self.expr.to_tokens(tokens);
3619 });
3620 }
3621 }
3622
Alex Crichton62a0a592017-05-22 13:58:53 -07003623 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003624 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003625 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003626 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003627 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003628 self.expr.to_tokens(tokens);
3629 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003630 }
3631 }
3632
Michael Layzell734adb42017-06-07 16:58:31 -04003633 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003634 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003635 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003636 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003637 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003638 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003639 }
3640 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003641
David Tolnay2ae520a2017-12-29 11:19:50 -05003642 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003643 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003644 self.tts.to_tokens(tokens);
3645 }
3646 }
3647
Michael Layzell734adb42017-06-07 16:58:31 -04003648 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003649 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003650 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003651 self.name.to_tokens(tokens);
3652 self.colon_token.to_tokens(tokens);
3653 }
3654 }
3655
3656 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003657 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003658 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003659 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003660 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003661 if let Some(ref colon_token) = self.colon_token {
3662 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003663 self.expr.to_tokens(tokens);
3664 }
David Tolnay055a7042016-10-02 19:23:54 -07003665 }
3666 }
3667
Michael Layzell734adb42017-06-07 16:58:31 -04003668 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003669 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003670 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003671 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003672 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003673 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003674 if let Some((ref if_token, ref guard)) = self.guard {
3675 if_token.to_tokens(tokens);
3676 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003677 }
David Tolnaydfb91432018-03-31 19:19:44 +02003678 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003679 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003680 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003681 }
3682 }
3683
Michael Layzell734adb42017-06-07 16:58:31 -04003684 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003685 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003686 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003687 self.underscore_token.to_tokens(tokens);
3688 }
3689 }
3690
Michael Layzell734adb42017-06-07 16:58:31 -04003691 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003692 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003693 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003694 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003695 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003696 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003697 if let Some((ref at_token, ref subpat)) = self.subpat {
3698 at_token.to_tokens(tokens);
3699 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003700 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003701 }
3702 }
3703
Michael Layzell734adb42017-06-07 16:58:31 -04003704 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003705 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003706 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003707 self.path.to_tokens(tokens);
3708 self.brace_token.surround(tokens, |tokens| {
3709 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003710 // NOTE: We need a comma before the dot2 token if it is present.
3711 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003712 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003713 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003714 self.dot2_token.to_tokens(tokens);
3715 });
3716 }
3717 }
3718
Michael Layzell734adb42017-06-07 16:58:31 -04003719 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003720 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003721 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003722 self.path.to_tokens(tokens);
3723 self.pat.to_tokens(tokens);
3724 }
3725 }
3726
Michael Layzell734adb42017-06-07 16:58:31 -04003727 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003728 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003729 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003730 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3731 }
3732 }
3733
Michael Layzell734adb42017-06-07 16:58:31 -04003734 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003735 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003736 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003737 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003738 self.front.to_tokens(tokens);
3739 if let Some(ref dot2_token) = self.dot2_token {
3740 if !self.front.empty_or_trailing() {
3741 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003742 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003743 }
David Tolnay41871922017-12-29 01:53:45 -05003744 dot2_token.to_tokens(tokens);
3745 self.comma_token.to_tokens(tokens);
3746 if self.comma_token.is_none() && !self.back.is_empty() {
3747 // Ensure there is a comma after the .. token.
3748 <Token![,]>::default().to_tokens(tokens);
3749 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003750 }
David Tolnay41871922017-12-29 01:53:45 -05003751 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003752 });
3753 }
3754 }
3755
Michael Layzell734adb42017-06-07 16:58:31 -04003756 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003757 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003758 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003759 self.box_token.to_tokens(tokens);
3760 self.pat.to_tokens(tokens);
3761 }
3762 }
3763
Michael Layzell734adb42017-06-07 16:58:31 -04003764 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003765 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003766 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003767 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003768 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003769 self.pat.to_tokens(tokens);
3770 }
3771 }
3772
Michael Layzell734adb42017-06-07 16:58:31 -04003773 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003774 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003775 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003776 self.expr.to_tokens(tokens);
3777 }
3778 }
3779
Michael Layzell734adb42017-06-07 16:58:31 -04003780 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003781 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003782 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003783 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003784 match self.limits {
3785 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003786 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003787 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003788 self.hi.to_tokens(tokens);
3789 }
3790 }
3791
Michael Layzell734adb42017-06-07 16:58:31 -04003792 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003793 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003794 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003795 // XXX: This is a mess, and it will be so easy to screw it up. How
3796 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003797 self.bracket_token.surround(tokens, |tokens| {
3798 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003799
3800 // If we need a comma before the middle or standalone .. token,
3801 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003802 if !self.front.empty_or_trailing()
3803 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003804 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003805 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003806 }
3807
3808 // If we have an identifier, we always need a .. token.
3809 if self.middle.is_some() {
3810 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003811 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003812 } else if self.dot2_token.is_some() {
3813 self.dot2_token.to_tokens(tokens);
3814 }
3815
3816 // Make sure we have a comma before the back half.
3817 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003818 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003819 self.back.to_tokens(tokens);
3820 } else {
3821 self.comma_token.to_tokens(tokens);
3822 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003823 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003824 }
3825 }
3826
Michael Layzell734adb42017-06-07 16:58:31 -04003827 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003828 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003829 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003830 self.mac.to_tokens(tokens);
3831 }
3832 }
3833
3834 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003835 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003836 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003837 self.tts.to_tokens(tokens);
3838 }
3839 }
3840
3841 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003842 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003843 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003844 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003845 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003846 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003847 }
3848 self.pat.to_tokens(tokens);
3849 }
3850 }
3851
Michael Layzell734adb42017-06-07 16:58:31 -04003852 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003853 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003854 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003855 self.brace_token.surround(tokens, |tokens| {
3856 tokens.append_all(&self.stmts);
3857 });
David Tolnay42602292016-10-01 22:25:45 -07003858 }
3859 }
3860
Michael Layzell734adb42017-06-07 16:58:31 -04003861 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003862 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003863 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003864 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003865 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003866 Stmt::Item(ref item) => item.to_tokens(tokens),
3867 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003868 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003869 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003870 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003871 }
David Tolnay42602292016-10-01 22:25:45 -07003872 }
3873 }
3874 }
David Tolnay191e0582016-10-02 18:31:09 -07003875
Michael Layzell734adb42017-06-07 16:58:31 -04003876 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003877 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003878 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003879 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003880 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003881 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003882 if let Some((ref colon_token, ref ty)) = self.ty {
3883 colon_token.to_tokens(tokens);
3884 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003885 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003886 if let Some((ref eq_token, ref init)) = self.init {
3887 eq_token.to_tokens(tokens);
3888 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003889 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003890 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003891 }
3892 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003893}