blob: 1f99ea291c88a0fd63699dc2b6a4557c57507a44 [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 Tolnay02a9c6f2018-08-24 18:58:45 -0400512 /// An async block: `async { ... }`.
513 ///
514 /// *This type is available if Syn is built with the `"full"` feature.*
515 pub Async(ExprAsync #full {
516 pub attrs: Vec<Attribute>,
517 pub async_token: Token![async],
518 pub capture: Option<Token![move]>,
519 pub block: Block,
520 }),
521
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400522 /// A try block: `try { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800523 ///
524 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400525 pub TryBlock(ExprTryBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500526 pub attrs: Vec<Attribute>,
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400527 pub try_token: Token![try],
Alex Crichton62a0a592017-05-22 13:58:53 -0700528 pub block: Block,
529 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700530
David Tolnaya454c8f2018-01-07 01:01:10 -0800531 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800532 ///
533 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700534 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500535 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800536 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700537 pub expr: Option<Box<Expr>>,
538 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500539
David Tolnaya454c8f2018-01-07 01:01:10 -0800540 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800541 ///
542 /// *This type is available if Syn is built with the `"derive"` or
543 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500544 pub Verbatim(ExprVerbatim #manual_extra_traits {
545 pub tts: TokenStream,
546 }),
547 }
548}
549
550#[cfg(feature = "extra-traits")]
551impl Eq for ExprVerbatim {}
552
553#[cfg(feature = "extra-traits")]
554impl PartialEq for ExprVerbatim {
555 fn eq(&self, other: &Self) -> bool {
556 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
557 }
558}
559
560#[cfg(feature = "extra-traits")]
561impl Hash for ExprVerbatim {
562 fn hash<H>(&self, state: &mut H)
563 where
564 H: Hasher,
565 {
566 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700567 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700568}
569
David Tolnay8c91b882017-12-28 23:04:32 -0500570impl Expr {
571 // Not public API.
572 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500573 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500574 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500575 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800576 Expr::Box(ExprBox { ref mut attrs, .. })
577 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
578 | Expr::Array(ExprArray { ref mut attrs, .. })
579 | Expr::Call(ExprCall { ref mut attrs, .. })
580 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
581 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
582 | Expr::Binary(ExprBinary { ref mut attrs, .. })
583 | Expr::Unary(ExprUnary { ref mut attrs, .. })
584 | Expr::Lit(ExprLit { ref mut attrs, .. })
585 | Expr::Cast(ExprCast { ref mut attrs, .. })
586 | Expr::Type(ExprType { ref mut attrs, .. })
587 | Expr::If(ExprIf { ref mut attrs, .. })
588 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
589 | Expr::While(ExprWhile { ref mut attrs, .. })
590 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
591 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
592 | Expr::Loop(ExprLoop { ref mut attrs, .. })
593 | Expr::Match(ExprMatch { ref mut attrs, .. })
594 | Expr::Closure(ExprClosure { ref mut attrs, .. })
595 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
596 | Expr::Block(ExprBlock { ref mut attrs, .. })
597 | Expr::Assign(ExprAssign { ref mut attrs, .. })
598 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
599 | Expr::Field(ExprField { ref mut attrs, .. })
600 | Expr::Index(ExprIndex { ref mut attrs, .. })
601 | Expr::Range(ExprRange { ref mut attrs, .. })
602 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200603 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800604 | Expr::Break(ExprBreak { ref mut attrs, .. })
605 | Expr::Continue(ExprContinue { ref mut attrs, .. })
606 | Expr::Return(ExprReturn { ref mut attrs, .. })
607 | Expr::Macro(ExprMacro { ref mut attrs, .. })
608 | Expr::Struct(ExprStruct { ref mut attrs, .. })
609 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
610 | Expr::Paren(ExprParen { ref mut attrs, .. })
611 | Expr::Group(ExprGroup { ref mut attrs, .. })
612 | Expr::Try(ExprTry { ref mut attrs, .. })
David Tolnay02a9c6f2018-08-24 18:58:45 -0400613 | Expr::Async(ExprAsync { ref mut attrs, .. })
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400614 | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800615 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500616 Expr::Verbatim(_) => {
617 // TODO
618 Vec::new()
619 }
David Tolnay8c91b882017-12-28 23:04:32 -0500620 }
621 }
622}
623
David Tolnay85b69a42017-12-27 20:43:10 -0500624ast_enum! {
625 /// A struct or tuple struct field accessed in a struct literal or field
626 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800627 ///
628 /// *This type is available if Syn is built with the `"derive"` or `"full"`
629 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500630 pub enum Member {
631 /// A named field like `self.x`.
632 Named(Ident),
633 /// An unnamed field like `self.0`.
634 Unnamed(Index),
635 }
636}
637
David Tolnay85b69a42017-12-27 20:43:10 -0500638ast_struct! {
639 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800640 ///
641 /// *This type is available if Syn is built with the `"derive"` or `"full"`
642 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500643 pub struct Index #manual_extra_traits {
644 pub index: u32,
645 pub span: Span,
646 }
647}
648
David Tolnay14982012017-12-29 00:49:51 -0500649impl From<usize> for Index {
650 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700651 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500652 Index {
653 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700654 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500655 }
656 }
657}
658
659#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500660impl Eq for Index {}
661
David Tolnay14982012017-12-29 00:49:51 -0500662#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500663impl PartialEq for Index {
664 fn eq(&self, other: &Self) -> bool {
665 self.index == other.index
666 }
667}
668
David Tolnay14982012017-12-29 00:49:51 -0500669#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500670impl Hash for Index {
671 fn hash<H: Hasher>(&self, state: &mut H) {
672 self.index.hash(state);
673 }
674}
675
676#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700677ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800678 /// The `::<>` explicit type parameters passed to a method call:
679 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800680 ///
681 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500682 pub struct MethodTurbofish {
683 pub colon2_token: Token![::],
684 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500685 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500686 pub gt_token: Token![>],
687 }
688}
689
690#[cfg(feature = "full")]
691ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800692 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800693 ///
694 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500695 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800696 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500697 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800698 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500699 ///
700 /// NOTE: Identity expressions are represented as Type arguments, as
701 /// they are indistinguishable syntactically.
702 Const(Expr),
703 }
704}
705
706#[cfg(feature = "full")]
707ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700708 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800709 ///
710 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700711 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500712 /// Attributes tagged on the field.
713 pub attrs: Vec<Attribute>,
714
715 /// Name or index of the field.
716 pub member: Member,
717
David Tolnay5d7098a2017-12-29 01:35:24 -0500718 /// The colon in `Struct { x: x }`. If written in shorthand like
719 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500720 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500721
Alex Crichton62a0a592017-05-22 13:58:53 -0700722 /// Value of the field.
723 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700724 }
David Tolnay055a7042016-10-02 19:23:54 -0700725}
726
Michael Layzell734adb42017-06-07 16:58:31 -0400727#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700728ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800729 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500732 pub struct Label {
733 pub name: Lifetime,
734 pub colon_token: Token![:],
735 }
736}
737
738#[cfg(feature = "full")]
739ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800740 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800741 ///
742 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700743 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500744 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700745 /// Statements in a block
746 pub stmts: Vec<Stmt>,
747 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700748}
749
Michael Layzell734adb42017-06-07 16:58:31 -0400750#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700751ast_enum! {
752 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800753 ///
754 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700755 pub enum Stmt {
756 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800757 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700758
Alex Crichton62a0a592017-05-22 13:58:53 -0700759 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800760 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700761
Alex Crichton62a0a592017-05-22 13:58:53 -0700762 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800763 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700764
David Tolnaya454c8f2018-01-07 01:01:10 -0800765 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800766 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700767 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700768}
769
Michael Layzell734adb42017-06-07 16:58:31 -0400770#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700771ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800772 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800773 ///
774 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700775 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500776 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800777 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200778 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500779 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500780 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500781 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700782 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700783}
784
Michael Layzell734adb42017-06-07 16:58:31 -0400785#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700786ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800787 /// A pattern in a local binding, function signature, match expression, or
788 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800789 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800790 /// *This type is available if Syn is built with the `"full"` feature.*
791 ///
David Tolnay614a0142018-01-07 10:25:43 -0800792 /// # Syntax tree enum
793 ///
794 /// This type is a [syntax tree enum].
795 ///
796 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700797 // Clippy false positive
798 // https://github.com/Manishearth/rust-clippy/issues/1241
799 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
800 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800801 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800802 ///
803 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700804 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800805 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700806 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700807
David Tolnaya454c8f2018-01-07 01:01:10 -0800808 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800809 ///
810 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700811 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500812 pub by_ref: Option<Token![ref]>,
813 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700814 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500815 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700816 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700817
David Tolnaya454c8f2018-01-07 01:01:10 -0800818 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800819 ///
820 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700821 pub Struct(PatStruct {
822 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500823 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500824 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800825 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700826 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700827
David Tolnaya454c8f2018-01-07 01:01:10 -0800828 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800829 ///
830 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700831 pub TupleStruct(PatTupleStruct {
832 pub path: Path,
833 pub pat: PatTuple,
834 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700835
David Tolnaya454c8f2018-01-07 01:01:10 -0800836 /// A path pattern like `Color::Red`, optionally qualified with a
837 /// self-type.
838 ///
839 /// Unquailfied path patterns can legally refer to variants, structs,
840 /// constants or associated constants. Quailfied path patterns like
841 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
842 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800843 ///
844 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700845 pub Path(PatPath {
846 pub qself: Option<QSelf>,
847 pub path: Path,
848 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700849
David Tolnaya454c8f2018-01-07 01:01:10 -0800850 /// A tuple pattern: `(a, b)`.
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 Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500854 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500855 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500856 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500857 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500858 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700859 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800860
861 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800862 ///
863 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700864 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800865 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500866 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700867 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800868
869 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800870 ///
871 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700872 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800873 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500874 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500875 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700876 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800877
878 /// A literal pattern: `0`.
879 ///
880 /// This holds an `Expr` rather than a `Lit` because negative numbers
881 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800882 ///
883 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700884 pub Lit(PatLit {
885 pub expr: Box<Expr>,
886 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800887
888 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800889 ///
890 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 pub Range(PatRange {
892 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700893 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500894 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700895 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800896
897 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800898 ///
899 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500901 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500902 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700903 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500904 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500905 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500906 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700907 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800908
909 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800910 ///
911 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500912 pub Macro(PatMacro {
913 pub mac: Macro,
914 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800915
916 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800917 ///
918 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500919 pub Verbatim(PatVerbatim #manual_extra_traits {
920 pub tts: TokenStream,
921 }),
922 }
923}
924
David Tolnayc43b44e2017-12-30 23:55:54 -0500925#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500926impl Eq for PatVerbatim {}
927
David Tolnayc43b44e2017-12-30 23:55:54 -0500928#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500929impl PartialEq for PatVerbatim {
930 fn eq(&self, other: &Self) -> bool {
931 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
932 }
933}
934
David Tolnayc43b44e2017-12-30 23:55:54 -0500935#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500936impl Hash for PatVerbatim {
937 fn hash<H>(&self, state: &mut H)
938 where
939 H: Hasher,
940 {
941 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700942 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700943}
944
Michael Layzell734adb42017-06-07 16:58:31 -0400945#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700946ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800947 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700948 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800949 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700950 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500951 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800952 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500953 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800955 /// 0...10 => {
956 /// return true;
957 /// }
958 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500959 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700960 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800961 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500962 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700963 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800964 ///
965 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700966 pub struct Arm {
967 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200968 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500969 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500970 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200971 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700972 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800973 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700974 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700975}
976
Michael Layzell734adb42017-06-07 16:58:31 -0400977#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700978ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800979 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800980 ///
981 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700982 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700983 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800984 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800985 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800986 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800987 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700988 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700989}
990
Michael Layzell734adb42017-06-07 16:58:31 -0400991#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700992ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800993 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700994 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800995 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
996 /// 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 -0800997 ///
998 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700999 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -05001000 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -05001001 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -05001002 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001003 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001004 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001005}
1006
Michael Layzell3936ceb2017-07-08 00:28:36 -04001007#[cfg(any(feature = "parsing", feature = "printing"))]
1008#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -07001009fn arm_expr_requires_comma(expr: &Expr) -> bool {
1010 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1011 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001012 match *expr {
1013 Expr::Unsafe(..)
1014 | Expr::Block(..)
1015 | Expr::If(..)
1016 | Expr::IfLet(..)
1017 | Expr::Match(..)
1018 | Expr::While(..)
1019 | Expr::WhileLet(..)
1020 | Expr::Loop(..)
1021 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001022 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001023 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001024 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001025 }
1026}
1027
David Tolnayb9c8e322016-09-23 20:48:37 -07001028#[cfg(feature = "parsing")]
1029pub mod parsing {
1030 use super::*;
David Tolnay9cc2f092018-08-24 15:51:37 -04001031 use path::parsing::mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001032 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001033 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001034
David Tolnaydfc886b2018-01-06 08:03:09 -08001035 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001036 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001037 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001038 #[cfg(feature = "full")]
1039 use proc_macro2::TokenStream;
David Tolnay203557a2017-12-27 23:59:33 -05001040 use synom::PResult;
David Tolnay94d2b792018-04-29 12:26:10 -07001041 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // When we're parsing expressions which occur before blocks, like in an if
1044 // statement's condition, we cannot parse a struct literal.
1045 //
1046 // Struct literals are ambiguous in certain positions
1047 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001048 macro_rules! ambiguous_expr {
1049 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001050 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001051 };
1052 }
1053
David Tolnaybcf26022017-12-25 22:10:52 -05001054 // When we are parsing an optional suffix expression, we cannot allow blocks
1055 // if structs are not allowed.
1056 //
1057 // Example:
1058 //
1059 // if break {} {}
1060 //
1061 // is ambiguous between:
1062 //
1063 // if (break {}) {}
1064 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001065 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066 macro_rules! opt_ambiguous_expr {
1067 ($i:expr, $allow_struct:ident) => {
1068 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1069 };
1070 }
1071
Alex Crichton954046c2017-05-30 21:49:42 -07001072 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001073 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001074
1075 fn description() -> Option<&'static str> {
1076 Some("expression")
1077 }
1078 }
1079
Michael Layzell734adb42017-06-07 16:58:31 -04001080 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001081 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1082
David Tolnaybcf26022017-12-25 22:10:52 -05001083 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001084 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001085 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001086 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001087 }
1088
Michael Layzell734adb42017-06-07 16:58:31 -04001089 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001090 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001091 // NOTE: We intentionally skip assign_expr, placement_expr, and
1092 // range_expr, as they are not parsed in non-full mode.
1093 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001094 }
1095
David Tolnaybcf26022017-12-25 22:10:52 -05001096 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001097 macro_rules! binop {
1098 (
1099 $name: ident,
1100 $next: ident,
1101 $submac: ident!( $($args:tt)* )
1102 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001103 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001104 mut e: call!($next, allow_struct, allow_block) >>
1105 many0!(do_parse!(
1106 op: $submac!($($args)*) >>
1107 rhs: call!($next, allow_struct, true) >>
1108 ({
1109 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001110 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001111 left: Box::new(e.into()),
1112 op: op,
1113 right: Box::new(rhs.into()),
1114 }.into();
1115 })
1116 )) >>
1117 (e)
1118 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001119 }
David Tolnay54e854d2016-10-24 12:03:30 -07001120 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001121
David Tolnaybcf26022017-12-25 22:10:52 -05001122 // <placement> = <placement> ..
1123 // <placement> += <placement> ..
1124 // <placement> -= <placement> ..
1125 // <placement> *= <placement> ..
1126 // <placement> /= <placement> ..
1127 // <placement> %= <placement> ..
1128 // <placement> ^= <placement> ..
1129 // <placement> &= <placement> ..
1130 // <placement> |= <placement> ..
1131 // <placement> <<= <placement> ..
1132 // <placement> >>= <placement> ..
1133 //
1134 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001135 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001136 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001137 mut e: call!(placement_expr, allow_struct, allow_block) >>
1138 alt!(
1139 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001140 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001141 // Recurse into self to parse right-associative operator.
1142 rhs: call!(assign_expr, allow_struct, true) >>
1143 ({
1144 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001145 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001146 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001147 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001148 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001149 }.into();
1150 })
1151 )
1152 |
1153 do_parse!(
1154 op: call!(BinOp::parse_assign_op) >>
1155 // Recurse into self to parse right-associative operator.
1156 rhs: call!(assign_expr, allow_struct, true) >>
1157 ({
1158 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001159 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001160 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001161 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001162 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001163 }.into();
1164 })
1165 )
1166 |
1167 epsilon!()
1168 ) >>
1169 (e)
1170 ));
1171
David Tolnaybcf26022017-12-25 22:10:52 -05001172 // <range> <- <range> ..
1173 //
1174 // NOTE: The `in place { expr }` version of this syntax is parsed in
1175 // `atom_expr`, not here.
1176 //
1177 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001178 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001179 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001180 mut e: call!(range_expr, allow_struct, allow_block) >>
1181 alt!(
1182 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001183 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 // Recurse into self to parse right-associative operator.
1185 rhs: call!(placement_expr, allow_struct, true) >>
1186 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001187 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001188 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001189 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001190 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001191 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001192 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001193 }.into();
1194 })
1195 )
1196 |
1197 epsilon!()
1198 ) >>
1199 (e)
1200 ));
1201
David Tolnaybcf26022017-12-25 22:10:52 -05001202 // <or> ... <or> ..
1203 // <or> .. <or> ..
1204 // <or> ..
1205 //
1206 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1207 // rules are for parsing these expressions are, but this is not correct.
1208 // For example, `a .. b .. c` is not a legal expression. It should not
1209 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1210 //
1211 // NOTE: The form of ranges which don't include a preceding expression are
1212 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001213 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001214 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001215 mut e: call!(or_expr, allow_struct, allow_block) >>
1216 many0!(do_parse!(
1217 limits: syn!(RangeLimits) >>
1218 // We don't want to allow blocks here if we don't allow structs. See
1219 // the reasoning for `opt_ambiguous_expr!` above.
1220 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1221 ({
1222 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001223 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001224 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001225 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001226 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001227 }.into();
1228 })
1229 )) >>
1230 (e)
1231 ));
1232
David Tolnaybcf26022017-12-25 22:10:52 -05001233 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001234 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001235
David Tolnaybcf26022017-12-25 22:10:52 -05001236 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001237 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001238
David Tolnaybcf26022017-12-25 22:10:52 -05001239 // <bitor> == <bitor> ...
1240 // <bitor> != <bitor> ...
1241 // <bitor> >= <bitor> ...
1242 // <bitor> <= <bitor> ...
1243 // <bitor> > <bitor> ...
1244 // <bitor> < <bitor> ...
1245 //
1246 // NOTE: This operator appears to be parsed as left-associative, but errors
1247 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001248 binop!(
1249 compare_expr,
1250 bitor_expr,
1251 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001252 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001253 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001254 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001255 |
1256 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001258 |
1259 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001260 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001261 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001262 do_parse!(
1263 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001264 not!(punct!(<-)) >>
1265 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001266 (BinOp::Lt(t))
1267 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001268 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001269 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001270 )
1271 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001272
David Tolnaybcf26022017-12-25 22:10:52 -05001273 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001274 binop!(
1275 bitor_expr,
1276 bitxor_expr,
1277 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1278 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001279
David Tolnaybcf26022017-12-25 22:10:52 -05001280 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001281 binop!(
1282 bitxor_expr,
1283 bitand_expr,
1284 do_parse!(
1285 // NOTE: Make sure we aren't looking at ^=.
1286 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1287 )
1288 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001289
David Tolnaybcf26022017-12-25 22:10:52 -05001290 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001291 binop!(
1292 bitand_expr,
1293 shift_expr,
1294 do_parse!(
1295 // NOTE: Make sure we aren't looking at && or &=.
1296 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1297 )
1298 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001299
David Tolnaybcf26022017-12-25 22:10:52 -05001300 // <arith> << <arith> ...
1301 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001302 binop!(
1303 shift_expr,
1304 arith_expr,
1305 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001306 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001307 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001308 punct!(>>) => { BinOp::Shr }
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 // <term> + <term> ...
1313 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001314 binop!(
1315 arith_expr,
1316 term_expr,
1317 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001318 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001319 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001320 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001321 )
1322 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001323
David Tolnaybcf26022017-12-25 22:10:52 -05001324 // <cast> * <cast> ...
1325 // <cast> / <cast> ...
1326 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001327 binop!(
1328 term_expr,
1329 cast_expr,
1330 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001331 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001333 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001335 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001336 )
1337 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338
David Tolnaybcf26022017-12-25 22:10:52 -05001339 // <unary> as <ty>
1340 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001341 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001342 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001343 mut e: call!(unary_expr, allow_struct, allow_block) >>
1344 many0!(alt!(
1345 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001346 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001347 // We can't accept `A + B` in cast expressions, as it's
1348 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001349 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001350 ({
1351 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001352 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001353 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001354 as_token: as_,
1355 ty: Box::new(ty),
1356 }.into();
1357 })
1358 )
1359 |
1360 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001361 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001362 // We can't accept `A + B` in cast expressions, as it's
1363 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001364 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001365 ({
1366 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001367 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001368 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001369 colon_token: colon,
1370 ty: Box::new(ty),
1371 }.into();
1372 })
1373 )
1374 )) >>
1375 (e)
1376 ));
1377
David Tolnay0cf94f22017-12-28 23:46:26 -05001378 // <unary> as <ty>
1379 #[cfg(not(feature = "full"))]
1380 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1381 mut e: call!(unary_expr, allow_struct, allow_block) >>
1382 many0!(do_parse!(
1383 as_: keyword!(as) >>
1384 // We can't accept `A + B` in cast expressions, as it's
1385 // ambiguous with the + expression.
1386 ty: call!(Type::without_plus) >>
1387 ({
1388 e = ExprCast {
1389 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001390 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001391 as_token: as_,
1392 ty: Box::new(ty),
1393 }.into();
1394 })
1395 )) >>
1396 (e)
1397 ));
1398
David Tolnaybcf26022017-12-25 22:10:52 -05001399 // <UnOp> <trailer>
1400 // & <trailer>
1401 // &mut <trailer>
1402 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001403 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001404 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001405 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001406 attrs: many0!(Attribute::parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001407 op: syn!(UnOp) >>
1408 expr: call!(unary_expr, allow_struct, true) >>
1409 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001410 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001411 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001412 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001413 }.into())
1414 )
1415 |
1416 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001417 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001418 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001419 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001420 expr: call!(unary_expr, allow_struct, true) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001421 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001422 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001423 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001424 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001425 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001426 }.into())
1427 )
1428 |
1429 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001430 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001431 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001432 expr: call!(unary_expr, allow_struct, true) >>
1433 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001434 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001435 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001436 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001437 }.into())
1438 )
1439 |
1440 call!(trailer_expr, allow_struct, allow_block)
1441 ));
1442
Michael Layzell734adb42017-06-07 16:58:31 -04001443 // XXX: This duplication is ugly
1444 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001445 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001446 do_parse!(
1447 op: syn!(UnOp) >>
1448 expr: call!(unary_expr, allow_struct, true) >>
1449 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001450 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001451 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001452 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001453 }.into())
1454 )
1455 |
1456 call!(trailer_expr, allow_struct, allow_block)
1457 ));
1458
David Tolnayd997aef2018-07-21 18:42:31 -07001459 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001460 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1461 let mut outer = Vec::new();
1462 let mut inner = Vec::new();
1463 for attr in mem::replace(attrs, Vec::new()) {
1464 match attr.style {
1465 AttrStyle::Outer => outer.push(attr),
1466 AttrStyle::Inner(_) => inner.push(attr),
1467 }
1468 }
1469 *attrs = inner;
1470 outer
1471 }
1472
David Tolnaybcf26022017-12-25 22:10:52 -05001473 // <atom> (..<args>) ...
1474 // <atom> . <ident> (..<args>) ...
1475 // <atom> . <ident> ...
1476 // <atom> . <lit> ...
1477 // <atom> [ <expr> ] ...
1478 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001479 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001480 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001481 mut e: call!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001482 outer_attrs: value!({
1483 let mut attrs = e.replace_attrs(Vec::new());
1484 let outer_attrs = take_outer(&mut attrs);
1485 e.replace_attrs(attrs);
1486 outer_attrs
1487 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001488 many0!(alt!(
1489 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001490 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001491 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001492 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001493 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001494 args: args,
1495 paren_token: paren,
1496 }.into();
1497 })
1498 |
1499 tap!(more: and_method_call => {
1500 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001501 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001502 e = call.into();
1503 })
1504 |
1505 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001506 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001507 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001508 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001509 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001510 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001511 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001512 }.into();
1513 })
1514 |
1515 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001516 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001517 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001518 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001519 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001520 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001521 index: Box::new(i),
1522 }.into();
1523 })
1524 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001525 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001526 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001527 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001528 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001529 question_token: question,
1530 }.into();
1531 })
1532 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001533 ({
1534 let mut attrs = outer_attrs;
1535 attrs.extend(e.replace_attrs(Vec::new()));
1536 e.replace_attrs(attrs);
1537 e
1538 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001539 ));
1540
Michael Layzell734adb42017-06-07 16:58:31 -04001541 // XXX: Duplication == ugly
1542 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001543 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001544 mut e: call!(atom_expr, allow_struct, allow_block) >>
1545 many0!(alt!(
1546 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001547 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001548 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001549 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001550 paren_token: args.0,
1551 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001552 }.into();
1553 })
1554 |
David Tolnayd5147742018-06-30 10:09:52 -07001555 tap!(field: and_field => {
1556 let (token, member) = field;
1557 e = ExprField {
1558 attrs: Vec::new(),
1559 base: Box::new(e),
1560 dot_token: token,
1561 member: member,
1562 }.into();
1563 })
1564 |
Michael Layzell734adb42017-06-07 16:58:31 -04001565 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001566 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001567 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001568 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001569 bracket_token: i.0,
1570 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001571 }.into();
1572 })
1573 )) >>
1574 (e)
1575 ));
1576
David Tolnaya454c8f2018-01-07 01:01:10 -08001577 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001578 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001579 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001580 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1581 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001582 |
David Tolnay8c91b882017-12-28 23:04:32 -05001583 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001584 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001585 // must be before ExprStruct
David Tolnay02a9c6f2018-08-24 18:58:45 -04001586 syn!(ExprAsync) => { Expr::Async }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001587 |
David Tolnayf7177052018-08-24 15:31:50 -04001588 // must be before ExprStruct
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001589 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnayf7177052018-08-24 15:31:50 -04001590 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001591 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001592 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001593 |
David Tolnay8c91b882017-12-28 23:04:32 -05001594 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001595 |
David Tolnay8c91b882017-12-28 23:04:32 -05001596 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001597 |
1598 call!(expr_break, allow_struct) // must be before expr_path
1599 |
David Tolnay8c91b882017-12-28 23:04:32 -05001600 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001601 |
1602 call!(expr_ret, allow_struct) // must be before expr_path
1603 |
David Tolnay8c91b882017-12-28 23:04:32 -05001604 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001605 |
David Tolnay8c91b882017-12-28 23:04:32 -05001606 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001607 |
David Tolnay8c91b882017-12-28 23:04:32 -05001608 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001609 |
David Tolnay8c91b882017-12-28 23:04:32 -05001610 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001611 |
David Tolnay8c91b882017-12-28 23:04:32 -05001612 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001613 |
David Tolnay8c91b882017-12-28 23:04:32 -05001614 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001615 |
David Tolnay8c91b882017-12-28 23:04:32 -05001616 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001617 |
David Tolnay8c91b882017-12-28 23:04:32 -05001618 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001619 |
David Tolnay8c91b882017-12-28 23:04:32 -05001620 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001621 |
David Tolnay8c91b882017-12-28 23:04:32 -05001622 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001623 |
David Tolnay8c91b882017-12-28 23:04:32 -05001624 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001625 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001626 call!(expr_closure, allow_struct)
1627 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001628 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001629 |
David Tolnay5d08ae62018-08-01 00:08:48 -07001630 call!(unstable_labeled_block) => { Expr::Verbatim }
1631 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001632 // NOTE: This is the prefix-form of range
1633 call!(expr_range, allow_struct)
1634 |
David Tolnay8c91b882017-12-28 23:04:32 -05001635 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001636 |
David Tolnay8c91b882017-12-28 23:04:32 -05001637 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001638 ));
1639
Michael Layzell734adb42017-06-07 16:58:31 -04001640 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001641 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001642 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001643 |
David Tolnay9374bc02018-01-27 18:49:36 -08001644 syn!(ExprParen) => { Expr::Paren }
1645 |
David Tolnay8c91b882017-12-28 23:04:32 -05001646 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001647 ));
1648
Michael Layzell734adb42017-06-07 16:58:31 -04001649 #[cfg(feature = "full")]
David Tolnay313a36f2018-04-29 20:13:04 -07001650 named!(expr_nosemi -> Expr, do_parse!(
1651 nosemi: alt!(
1652 syn!(ExprIf) => { Expr::If }
1653 |
1654 syn!(ExprIfLet) => { Expr::IfLet }
1655 |
1656 syn!(ExprWhile) => { Expr::While }
1657 |
1658 syn!(ExprWhileLet) => { Expr::WhileLet }
1659 |
1660 syn!(ExprForLoop) => { Expr::ForLoop }
1661 |
1662 syn!(ExprLoop) => { Expr::Loop }
1663 |
1664 syn!(ExprMatch) => { Expr::Match }
1665 |
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001666 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnay313a36f2018-04-29 20:13:04 -07001667 |
1668 syn!(ExprYield) => { Expr::Yield }
1669 |
1670 syn!(ExprUnsafe) => { Expr::Unsafe }
1671 |
1672 syn!(ExprBlock) => { Expr::Block }
David Tolnay5d08ae62018-08-01 00:08:48 -07001673 |
1674 call!(unstable_labeled_block) => { Expr::Verbatim }
David Tolnay313a36f2018-04-29 20:13:04 -07001675 ) >>
1676 // If the next token is a `.` or a `?` it is special-cased to parse
1677 // as an expression instead of a blockexpression.
1678 not!(punct!(.)) >>
1679 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001680 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001681 ));
Michael Layzell35418782017-06-07 09:20:25 -04001682
David Tolnay8c91b882017-12-28 23:04:32 -05001683 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001684 #[cfg(not(feature = "full"))]
1685 named!(parse -> Self, do_parse!(
1686 lit: syn!(Lit) >>
1687 (ExprLit {
1688 attrs: Vec::new(),
1689 lit: lit,
1690 })
1691 ));
1692
1693 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001694 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07001695 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001696 lit: syn!(Lit) >>
1697 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001698 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001699 lit: lit,
1700 })
1701 ));
David Tolnay79777332018-01-07 10:04:42 -08001702
1703 fn description() -> Option<&'static str> {
1704 Some("literal")
1705 }
David Tolnay8c91b882017-12-28 23:04:32 -05001706 }
1707
1708 #[cfg(feature = "full")]
1709 impl Synom for ExprMacro {
1710 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001711 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001712 mac: syn!(Macro) >>
1713 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001714 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001715 mac: mac,
1716 })
1717 ));
David Tolnay79777332018-01-07 10:04:42 -08001718
1719 fn description() -> Option<&'static str> {
1720 Some("macro invocation expression")
1721 }
David Tolnay8c91b882017-12-28 23:04:32 -05001722 }
1723
David Tolnaye98775f2017-12-28 23:17:00 -05001724 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001725 impl Synom for ExprGroup {
1726 named!(parse -> Self, do_parse!(
1727 e: grouped!(syn!(Expr)) >>
1728 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001729 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001730 expr: Box::new(e.1),
1731 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001732 })
Michael Layzell93c36282017-06-04 20:43:14 -04001733 ));
David Tolnay79777332018-01-07 10:04:42 -08001734
1735 fn description() -> Option<&'static str> {
1736 Some("expression surrounded by invisible delimiters")
1737 }
Michael Layzell93c36282017-06-04 20:43:14 -04001738 }
1739
Alex Crichton954046c2017-05-30 21:49:42 -07001740 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001741 #[cfg(not(feature = "full"))]
1742 named!(parse -> Self, do_parse!(
1743 e: parens!(syn!(Expr)) >>
1744 (ExprParen {
1745 attrs: Vec::new(),
1746 paren_token: e.0,
1747 expr: Box::new(e.1),
1748 })
1749 ));
1750
1751 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001752 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001753 outer_attrs: many0!(Attribute::parse_outer) >>
1754 e: parens!(tuple!(
1755 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001756 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001757 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001758 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001759 attrs: {
1760 let mut attrs = outer_attrs;
1761 attrs.extend((e.1).0);
1762 attrs
1763 },
David Tolnay8875fca2017-12-31 13:52:37 -05001764 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001765 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001766 })
Michael Layzell92639a52017-06-01 00:07:44 -04001767 ));
David Tolnay79777332018-01-07 10:04:42 -08001768
1769 fn description() -> Option<&'static str> {
1770 Some("parenthesized expression")
1771 }
Alex Crichton954046c2017-05-30 21:49:42 -07001772 }
David Tolnay89e05672016-10-02 14:39:42 -07001773
Michael Layzell734adb42017-06-07 16:58:31 -04001774 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001775 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001776 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001777 outer_attrs: many0!(Attribute::parse_outer) >>
1778 elems: brackets!(tuple!(
1779 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001780 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001781 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001782 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001783 attrs: {
1784 let mut attrs = outer_attrs;
1785 attrs.extend((elems.1).0);
1786 attrs
1787 },
David Tolnay8875fca2017-12-31 13:52:37 -05001788 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001789 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001790 })
1791 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001792
1793 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001794 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001795 }
Alex Crichton954046c2017-05-30 21:49:42 -07001796 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001797
David Tolnayf2cfd722017-12-31 18:02:51 -05001798 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001799 parens!(Punctuated::parse_terminated)
1800 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001801
Michael Layzell734adb42017-06-07 16:58:31 -04001802 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001803 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001804 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001805 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001806 turbofish: option!(tuple!(
1807 punct!(::),
1808 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001809 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001810 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001811 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001812 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001813 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001814 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001815 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001816 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001817 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001818 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001819 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001820
Alex Crichton954046c2017-05-30 21:49:42 -07001821 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001822 turbofish: turbofish.map(|fish| MethodTurbofish {
1823 colon2_token: fish.0,
1824 lt_token: fish.1,
1825 args: fish.2,
1826 gt_token: fish.3,
1827 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001828 args: args.1,
1829 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001830 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001831 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001832 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001833 ));
1834
Michael Layzell734adb42017-06-07 16:58:31 -04001835 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001836 impl Synom for GenericMethodArgument {
1837 // TODO parse const generics as well
1838 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001839
1840 fn description() -> Option<&'static str> {
1841 Some("generic method argument")
1842 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001843 }
1844
1845 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001846 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001847 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001848 outer_attrs: many0!(Attribute::parse_outer) >>
1849 elems: parens!(tuple!(
1850 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001851 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001852 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001853 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001854 attrs: {
1855 let mut attrs = outer_attrs;
1856 attrs.extend((elems.1).0);
1857 attrs
1858 },
1859 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001860 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001861 })
1862 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001863
1864 fn description() -> Option<&'static str> {
1865 Some("tuple")
1866 }
Alex Crichton954046c2017-05-30 21:49:42 -07001867 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001868
Michael Layzell734adb42017-06-07 16:58:31 -04001869 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001870 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001871 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001872 if_: keyword!(if) >>
1873 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001874 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001875 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001876 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001877 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001878 else_block: option!(else_block) >>
1879 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001880 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001881 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001882 let_token: let_,
1883 eq_token: eq,
1884 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001885 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001886 brace_token: then_block.0,
1887 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001888 },
1889 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001890 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001891 })
1892 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001893
1894 fn description() -> Option<&'static str> {
1895 Some("`if let` expression")
1896 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001897 }
1898
Michael Layzell734adb42017-06-07 16:58:31 -04001899 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001900 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001901 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001902 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001903 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001904 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001905 else_block: option!(else_block) >>
1906 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001907 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001908 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001909 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001910 brace_token: then_block.0,
1911 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001912 },
1913 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001914 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001915 })
1916 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001917
1918 fn description() -> Option<&'static str> {
1919 Some("`if` expression")
1920 }
Alex Crichton954046c2017-05-30 21:49:42 -07001921 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001922
Michael Layzell734adb42017-06-07 16:58:31 -04001923 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001924 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001925 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001926 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001927 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001928 |
David Tolnay8c91b882017-12-28 23:04:32 -05001929 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001930 |
1931 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001932 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001933 (Expr::Block(ExprBlock {
1934 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001935 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001936 brace_token: else_block.0,
1937 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001938 },
1939 }))
David Tolnay939766a2016-09-23 23:48:12 -07001940 )
Alex Crichton954046c2017-05-30 21:49:42 -07001941 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001942 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001943 ));
1944
Michael Layzell734adb42017-06-07 16:58:31 -04001945 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001946 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001947 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001948 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001949 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001950 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001951 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001952 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001953 expr: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001954 block: braces!(tuple!(
1955 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001956 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001957 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001958 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001959 attrs: {
1960 let mut attrs = outer_attrs;
1961 attrs.extend((block.1).0);
1962 attrs
1963 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001964 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001965 for_token: for_,
1966 pat: Box::new(pat),
1967 in_token: in_,
1968 expr: Box::new(expr),
1969 body: Block {
1970 brace_token: block.0,
1971 stmts: (block.1).1,
1972 },
Michael Layzell92639a52017-06-01 00:07:44 -04001973 })
1974 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001975
1976 fn description() -> Option<&'static str> {
1977 Some("`for` loop")
1978 }
Alex Crichton954046c2017-05-30 21:49:42 -07001979 }
Gregory Katze5f35682016-09-27 14:20:55 -04001980
Michael Layzell734adb42017-06-07 16:58:31 -04001981 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001982 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001983 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001984 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001985 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001986 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001987 block: braces!(tuple!(
1988 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001989 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001990 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001991 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001992 attrs: {
1993 let mut attrs = outer_attrs;
1994 attrs.extend((block.1).0);
1995 attrs
1996 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001997 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001998 loop_token: loop_,
1999 body: Block {
2000 brace_token: block.0,
2001 stmts: (block.1).1,
2002 },
Michael Layzell92639a52017-06-01 00:07:44 -04002003 })
2004 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002005
2006 fn description() -> Option<&'static str> {
2007 Some("`loop`")
2008 }
Alex Crichton954046c2017-05-30 21:49:42 -07002009 }
2010
Michael Layzell734adb42017-06-07 16:58:31 -04002011 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002012 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002013 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002014 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002015 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002016 obj: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002017 braced_content: braces!(tuple!(
2018 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002019 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07002020 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05002021 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002022 attrs: {
2023 let mut attrs = outer_attrs;
2024 attrs.extend((braced_content.1).0);
2025 attrs
2026 },
David Tolnay8875fca2017-12-31 13:52:37 -05002027 expr: Box::new(obj),
2028 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07002029 brace_token: braced_content.0,
2030 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002031 })
2032 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002033
2034 fn description() -> Option<&'static str> {
2035 Some("`match` expression")
2036 }
Alex Crichton954046c2017-05-30 21:49:42 -07002037 }
David Tolnay1978c672016-10-27 22:05:52 -07002038
Michael Layzell734adb42017-06-07 16:58:31 -04002039 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002040 impl Synom for ExprTryBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002041 named!(parse -> Self, do_parse!(
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002042 try_token: keyword!(try) >>
2043 block: syn!(Block) >>
2044 (ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002045 attrs: Vec::new(),
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002046 try_token: try_token,
2047 block: block,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002048 })
Michael Layzell92639a52017-06-01 00:07:44 -04002049 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002050
2051 fn description() -> Option<&'static str> {
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002052 Some("`try` block")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002053 }
Alex Crichton954046c2017-05-30 21:49:42 -07002054 }
Arnavion02ef13f2017-04-25 00:54:31 -07002055
Michael Layzell734adb42017-06-07 16:58:31 -04002056 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002057 impl Synom for ExprYield {
2058 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002059 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002060 expr: option!(syn!(Expr)) >>
2061 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002062 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002063 yield_token: yield_,
2064 expr: expr.map(Box::new),
2065 })
2066 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002067
2068 fn description() -> Option<&'static str> {
2069 Some("`yield` expression")
2070 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002071 }
2072
2073 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002074 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002075 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002076 attrs: many0!(Attribute::parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002077 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002078 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002079 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002080 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002081 body: do_parse!(
2082 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002083 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2084 true => alt!(
2085 input_end!() => { |_| None }
2086 |
2087 punct!(,) => { Some }
2088 )
Alex Crichton03b30272017-08-28 09:35:24 -07002089 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002090 false => option!(punct!(,))
2091 ) >>
2092 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002093 ) >>
2094 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002095 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002096 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002097 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002098 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002099 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002100 body: Box::new(body.0),
2101 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002102 })
2103 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002104
2105 fn description() -> Option<&'static str> {
2106 Some("`match` arm")
2107 }
Alex Crichton954046c2017-05-30 21:49:42 -07002108 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002109
Michael Layzell734adb42017-06-07 16:58:31 -04002110 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002111 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnay713a6722018-07-21 15:49:40 -07002112 attrs: many0!(Attribute::parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002113 asyncness: option!(keyword!(async)) >>
2114 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002115 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002116 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002117 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002118 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002119 ret_and_body: alt!(
2120 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002121 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002122 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002123 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002124 (
2125 ReturnType::Type(arrow, Box::new(ty)),
2126 Expr::Block(ExprBlock {
2127 attrs: Vec::new(),
2128 block: body,
2129 },
2130 ))
David Tolnay89e05672016-10-02 14:39:42 -07002131 )
2132 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002133 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002134 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002135 (Expr::Closure(ExprClosure {
2136 attrs: attrs,
2137 asyncness: asyncness,
2138 movability: movability,
2139 capture: capture,
2140 or1_token: or1,
2141 inputs: inputs,
2142 or2_token: or2,
2143 output: ret_and_body.0,
2144 body: Box::new(ret_and_body.1),
2145 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002146 ));
2147
2148 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04002149 impl Synom for ExprAsync {
2150 named!(parse -> Self, do_parse!(
2151 attrs: many0!(Attribute::parse_outer) >>
2152 async_token: keyword!(async) >>
2153 capture: option!(keyword!(move)) >>
2154 block: syn!(Block) >>
2155 (ExprAsync {
2156 attrs: attrs,
2157 async_token: async_token,
2158 capture: capture,
2159 block: block,
2160 })
2161 ));
2162 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002163
2164 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002165 named!(fn_arg -> FnArg, do_parse!(
2166 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002167 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002168 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002169 if let Some((colon, ty)) = ty {
2170 FnArg::Captured(ArgCaptured {
2171 pat: pat,
2172 colon_token: colon,
2173 ty: ty,
2174 })
2175 } else {
2176 FnArg::Inferred(pat)
2177 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002178 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002179 ));
2180
Michael Layzell734adb42017-06-07 16:58:31 -04002181 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002182 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002183 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002184 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002185 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002186 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002187 cond: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002188 block: braces!(tuple!(
2189 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002190 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002191 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002192 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002193 attrs: {
2194 let mut attrs = outer_attrs;
2195 attrs.extend((block.1).0);
2196 attrs
2197 },
2198 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002199 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002200 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002201 body: Block {
2202 brace_token: block.0,
2203 stmts: (block.1).1,
2204 },
Michael Layzell92639a52017-06-01 00:07:44 -04002205 })
2206 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002207
2208 fn description() -> Option<&'static str> {
2209 Some("`while` expression")
2210 }
Alex Crichton954046c2017-05-30 21:49:42 -07002211 }
2212
Michael Layzell734adb42017-06-07 16:58:31 -04002213 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002214 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002215 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002216 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002217 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002218 while_: keyword!(while) >>
2219 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002220 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002221 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002222 value: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002223 block: braces!(tuple!(
2224 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002225 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002226 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002227 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002228 attrs: {
2229 let mut attrs = outer_attrs;
2230 attrs.extend((block.1).0);
2231 attrs
2232 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002233 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002234 while_token: while_,
2235 let_token: let_,
2236 pats: pats,
2237 eq_token: eq,
2238 expr: Box::new(value),
2239 body: Block {
2240 brace_token: block.0,
2241 stmts: (block.1).1,
2242 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002243 })
2244 ));
David Tolnay79777332018-01-07 10:04:42 -08002245
2246 fn description() -> Option<&'static str> {
2247 Some("`while let` expression")
2248 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002249 }
2250
2251 #[cfg(feature = "full")]
2252 impl Synom for Label {
2253 named!(parse -> Self, do_parse!(
2254 name: syn!(Lifetime) >>
2255 colon: punct!(:) >>
2256 (Label {
2257 name: name,
2258 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002259 })
2260 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002261
2262 fn description() -> Option<&'static str> {
2263 Some("`while let` expression")
2264 }
Alex Crichton954046c2017-05-30 21:49:42 -07002265 }
2266
Michael Layzell734adb42017-06-07 16:58:31 -04002267 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002268 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002269 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002270 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002271 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002272 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002273 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002274 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002275 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002276 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002277 })
2278 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002279
2280 fn description() -> Option<&'static str> {
2281 Some("`continue`")
2282 }
Alex Crichton954046c2017-05-30 21:49:42 -07002283 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002284
Michael Layzell734adb42017-06-07 16:58:31 -04002285 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002286 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002287 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002288 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002289 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002290 // We can't allow blocks after a `break` expression when we wouldn't
2291 // allow structs, as this expression is ambiguous.
2292 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002293 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002294 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002295 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002296 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002297 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002298 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002299 ));
2300
Michael Layzell734adb42017-06-07 16:58:31 -04002301 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002302 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002303 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002304 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002305 // NOTE: return is greedy and eats blocks after it even when in a
2306 // position where structs are not allowed, such as in if statement
2307 // conditions. For example:
2308 //
David Tolnaybcf26022017-12-25 22:10:52 -05002309 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002310 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002311 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002312 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002313 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002314 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002315 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002316 ));
2317
Michael Layzell734adb42017-06-07 16:58:31 -04002318 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002319 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002320 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002321 outer_attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002322 path: syn!(Path) >>
2323 data: braces!(do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002324 inner_attrs: many0!(Attribute::parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002325 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002326 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2327 dots: punct!(..) >>
2328 base: syn!(Expr) >>
2329 (dots, base)
2330 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002331 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002332 )) >>
2333 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002334 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002335 let (dots, rest) = match base.and_then(|b| b) {
2336 Some((dots, base)) => (Some(dots), Some(base)),
2337 None => (None, None),
2338 };
2339 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002340 attrs: {
2341 let mut attrs = outer_attrs;
2342 attrs.extend(inner_attrs);
2343 attrs
2344 },
Michael Layzell92639a52017-06-01 00:07:44 -04002345 brace_token: brace,
2346 path: path,
2347 fields: fields,
2348 dot2_token: dots,
2349 rest: rest.map(Box::new),
2350 }
2351 })
2352 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002353
2354 fn description() -> Option<&'static str> {
2355 Some("struct literal expression")
2356 }
Alex Crichton954046c2017-05-30 21:49:42 -07002357 }
2358
Michael Layzell734adb42017-06-07 16:58:31 -04002359 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002360 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002361 named!(parse -> Self, do_parse!(
2362 attrs: many0!(Attribute::parse_outer) >>
2363 field_value: alt!(
2364 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2365 |
2366 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002367 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002368 None,
2369 Expr::Path(ExprPath {
2370 attrs: Vec::new(),
2371 qself: None,
2372 path: name.into(),
2373 }),
2374 ))
2375 ) >>
2376 (FieldValue {
2377 attrs: attrs,
2378 member: field_value.0,
2379 colon_token: field_value.1,
2380 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002381 })
2382 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002383
2384 fn description() -> Option<&'static str> {
2385 Some("field-value pair: `field: value`")
2386 }
Alex Crichton954046c2017-05-30 21:49:42 -07002387 }
David Tolnay055a7042016-10-02 19:23:54 -07002388
Michael Layzell734adb42017-06-07 16:58:31 -04002389 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002390 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002391 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002392 outer_attrs: many0!(Attribute::parse_outer) >>
2393 data: brackets!(tuple!(
2394 many0!(Attribute::parse_inner),
2395 syn!(Expr),
2396 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002397 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002398 )) >>
2399 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002400 attrs: {
2401 let mut attrs = outer_attrs;
2402 attrs.extend((data.1).0);
2403 attrs
2404 },
2405 expr: Box::new((data.1).1),
2406 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002407 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002408 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002409 })
2410 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002411
2412 fn description() -> Option<&'static str> {
2413 Some("repeated array literal: `[val; N]`")
2414 }
Alex Crichton954046c2017-05-30 21:49:42 -07002415 }
David Tolnay055a7042016-10-02 19:23:54 -07002416
Michael Layzell734adb42017-06-07 16:58:31 -04002417 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002418 impl Synom for ExprUnsafe {
2419 named!(parse -> Self, do_parse!(
2420 unsafe_: keyword!(unsafe) >>
2421 b: syn!(Block) >>
2422 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002423 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002424 unsafe_token: unsafe_,
2425 block: b,
2426 })
2427 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002428
2429 fn description() -> Option<&'static str> {
2430 Some("unsafe block: `unsafe { .. }`")
2431 }
Nika Layzell640832a2017-12-04 13:37:09 -05002432 }
2433
2434 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002435 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002436 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002437 outer_attrs: many0!(Attribute::parse_outer) >>
2438 block: braces!(tuple!(
2439 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002440 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002441 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002442 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002443 attrs: {
2444 let mut attrs = outer_attrs;
2445 attrs.extend((block.1).0);
2446 attrs
2447 },
2448 block: Block {
2449 brace_token: block.0,
2450 stmts: (block.1).1,
2451 },
Michael Layzell92639a52017-06-01 00:07:44 -04002452 })
2453 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002454
2455 fn description() -> Option<&'static str> {
2456 Some("block: `{ .. }`")
2457 }
Alex Crichton954046c2017-05-30 21:49:42 -07002458 }
David Tolnay89e05672016-10-02 14:39:42 -07002459
Michael Layzell734adb42017-06-07 16:58:31 -04002460 #[cfg(feature = "full")]
David Tolnay5d08ae62018-08-01 00:08:48 -07002461 named!(unstable_labeled_block -> ExprVerbatim, do_parse!(
2462 begin: call!(verbatim::grab_cursor) >>
2463 many0!(Attribute::parse_outer) >>
David Tolnay61e15e52018-08-01 00:28:36 -07002464 syn!(Label) >>
David Tolnay5d08ae62018-08-01 00:08:48 -07002465 braces!(tuple!(
2466 many0!(Attribute::parse_inner),
2467 call!(Block::parse_within),
2468 )) >>
2469 end: call!(verbatim::grab_cursor) >>
2470 (ExprVerbatim {
2471 tts: verbatim::token_range(begin..end),
2472 })
2473 ));
2474
2475 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002476 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002477 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002478 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002479 (ExprRange {
2480 attrs: Vec::new(),
2481 from: None,
2482 to: hi.map(Box::new),
2483 limits: limits,
2484 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002485 ));
2486
Michael Layzell734adb42017-06-07 16:58:31 -04002487 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002488 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002489 named!(parse -> Self, alt!(
2490 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002491 punct!(..=) => { RangeLimits::Closed }
2492 |
2493 // Must come before Dot2
David Tolnay7ac699c2018-08-24 14:00:58 -04002494 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.spans)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002495 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002496 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002497 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002498
2499 fn description() -> Option<&'static str> {
2500 Some("range limit: `..`, `...` or `..=`")
2501 }
Alex Crichton954046c2017-05-30 21:49:42 -07002502 }
David Tolnay438c9052016-10-07 23:24:48 -07002503
Alex Crichton954046c2017-05-30 21:49:42 -07002504 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002505 #[cfg(not(feature = "full"))]
2506 named!(parse -> Self, do_parse!(
2507 pair: qpath >>
2508 (ExprPath {
2509 attrs: Vec::new(),
2510 qself: pair.0,
2511 path: pair.1,
2512 })
2513 ));
2514
2515 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002516 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07002517 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002518 pair: qpath >>
2519 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002520 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002521 qself: pair.0,
2522 path: pair.1,
2523 })
2524 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002525
2526 fn description() -> Option<&'static str> {
2527 Some("path: `a::b::c`")
2528 }
Alex Crichton954046c2017-05-30 21:49:42 -07002529 }
David Tolnay42602292016-10-01 22:25:45 -07002530
David Tolnay9cc2f092018-08-24 15:51:37 -04002531 named!(path -> Path, do_parse!(
2532 colon: option!(punct!(::)) >>
2533 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2534 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2535 (Path {
2536 leading_colon: colon,
2537 segments: segments,
2538 })
2539 ));
2540
2541 named!(path_segment -> PathSegment, alt!(
2542 do_parse!(
2543 ident: syn!(Ident) >>
2544 colon2: punct!(::) >>
2545 lt: punct!(<) >>
2546 args: call!(Punctuated::parse_terminated) >>
2547 gt: punct!(>) >>
2548 (PathSegment {
2549 ident: ident,
2550 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2551 colon2_token: Some(colon2),
2552 lt_token: lt,
2553 args: args,
2554 gt_token: gt,
2555 }),
2556 })
2557 )
2558 |
2559 mod_style_path_segment
2560 ));
2561
2562 named!(qpath -> (Option<QSelf>, Path), alt!(
2563 map!(path, |p| (None, p))
2564 |
2565 do_parse!(
2566 lt: punct!(<) >>
2567 this: syn!(Type) >>
2568 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2569 gt: punct!(>) >>
2570 colon2: punct!(::) >>
2571 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2572 ({
2573 let (pos, as_, path) = match path {
2574 Some((as_, mut path)) => {
2575 let pos = path.segments.len();
2576 path.segments.push_punct(colon2);
2577 path.segments.extend(rest.into_pairs());
2578 (pos, Some(as_), path)
2579 }
2580 None => {
2581 (0, None, Path {
2582 leading_colon: Some(colon2),
2583 segments: rest,
2584 })
2585 }
2586 };
2587 (Some(QSelf {
2588 lt_token: lt,
2589 ty: Box::new(this),
2590 position: pos,
2591 as_token: as_,
2592 gt_token: gt,
2593 }), path)
2594 })
2595 )
2596 |
2597 map!(keyword!(self), |s| (None, s.into()))
2598 ));
2599
David Tolnay85b69a42017-12-27 20:43:10 -05002600 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002601
David Tolnay8875fca2017-12-31 13:52:37 -05002602 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002603
Michael Layzell734adb42017-06-07 16:58:31 -04002604 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002605 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002606 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002607 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002608 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002609 brace_token: stmts.0,
2610 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002611 })
2612 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002613
2614 fn description() -> Option<&'static str> {
2615 Some("block: `{ .. }`")
2616 }
Alex Crichton954046c2017-05-30 21:49:42 -07002617 }
David Tolnay939766a2016-09-23 23:48:12 -07002618
Michael Layzell734adb42017-06-07 16:58:31 -04002619 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002620 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002621 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002622 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002623 mut standalone: many0!(do_parse!(
2624 stmt: syn!(Stmt) >>
2625 many0!(punct!(;)) >>
2626 (stmt)
2627 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002628 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002629 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002630 mut e: syn!(Expr) >>
2631 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002632 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002633 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002634 })
2635 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002636 (match last {
2637 None => standalone,
2638 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002639 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002640 standalone
2641 }
2642 })
2643 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002644 }
2645
Michael Layzell734adb42017-06-07 16:58:31 -04002646 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002647 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002648 named!(parse -> Self, alt!(
2649 stmt_mac
2650 |
2651 stmt_local
2652 |
2653 stmt_item
2654 |
Michael Layzell35418782017-06-07 09:20:25 -04002655 stmt_blockexpr
2656 |
Michael Layzell92639a52017-06-01 00:07:44 -04002657 stmt_expr
2658 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002659
2660 fn description() -> Option<&'static str> {
2661 Some("statement")
2662 }
Alex Crichton954046c2017-05-30 21:49:42 -07002663 }
David Tolnay939766a2016-09-23 23:48:12 -07002664
Michael Layzell734adb42017-06-07 16:58:31 -04002665 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002666 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002667 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002668 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002669 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002670 // Only parse braces here; paren and bracket will get parsed as
2671 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002672 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002673 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002674 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002675 attrs: attrs,
2676 ident: None,
2677 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002678 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002679 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002680 delimiter: MacroDelimiter::Brace(data.0),
2681 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002682 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002683 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002684 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002685 ));
2686
Michael Layzell734adb42017-06-07 16:58:31 -04002687 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002688 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002689 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002690 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002691 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002692 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002693 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2694 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002695 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002696 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002697 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002698 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002699 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2700 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2701 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002702 }))
David Tolnay191e0582016-10-02 18:31:09 -07002703 ));
2704
Michael Layzell734adb42017-06-07 16:58:31 -04002705 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002706 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002707
Michael Layzell734adb42017-06-07 16:58:31 -04002708 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002709 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002710 mut attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002711 mut e: expr_nosemi >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002712 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002713 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002714 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002715 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002716 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002717 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002718 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002719 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002720 }
2721 })
2722 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002723
Michael Layzell734adb42017-06-07 16:58:31 -04002724 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002725 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002726 mut attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002727 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002728 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002729 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002730 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002731 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002732 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002733 })
David Tolnay939766a2016-09-23 23:48:12 -07002734 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002735
Michael Layzell734adb42017-06-07 16:58:31 -04002736 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002737 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002738 named!(parse -> Self, alt!(
2739 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2740 |
2741 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2742 |
2743 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2744 |
2745 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2746 |
2747 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2748 |
David Tolnay323279a2017-12-29 11:26:32 -05002749 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002750 |
2751 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2752 |
2753 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2754 |
2755 syn!(PatPath) => { Pat::Path }
2756 |
2757 syn!(PatTuple) => { Pat::Tuple }
2758 |
2759 syn!(PatRef) => { Pat::Ref }
2760 |
2761 syn!(PatSlice) => { Pat::Slice }
2762 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002763
2764 fn description() -> Option<&'static str> {
2765 Some("pattern")
2766 }
Alex Crichton954046c2017-05-30 21:49:42 -07002767 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002768
Michael Layzell734adb42017-06-07 16:58:31 -04002769 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002770 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002771 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002772 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002773 |u| PatWild { underscore_token: u }
2774 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002775
2776 fn description() -> Option<&'static str> {
2777 Some("wild pattern: `_`")
2778 }
Alex Crichton954046c2017-05-30 21:49:42 -07002779 }
David Tolnay84aa0752016-10-02 23:01:13 -07002780
Michael Layzell734adb42017-06-07 16:58:31 -04002781 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002782 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002783 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002784 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002785 pat: syn!(Pat) >>
2786 (PatBox {
2787 pat: Box::new(pat),
2788 box_token: boxed,
2789 })
2790 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002791
2792 fn description() -> Option<&'static str> {
2793 Some("box pattern")
2794 }
Alex Crichton954046c2017-05-30 21:49:42 -07002795 }
2796
Michael Layzell734adb42017-06-07 16:58:31 -04002797 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002798 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002799 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002800 by_ref: option!(keyword!(ref)) >>
2801 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002802 name: alt!(
2803 syn!(Ident)
2804 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002805 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002806 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002807 not!(punct!(<)) >>
2808 not!(punct!(::)) >>
2809 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002810 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002811 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002812 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002813 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002814 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002815 })
2816 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002817
2818 fn description() -> Option<&'static str> {
2819 Some("pattern identifier binding")
2820 }
Alex Crichton954046c2017-05-30 21:49:42 -07002821 }
2822
Michael Layzell734adb42017-06-07 16:58:31 -04002823 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002824 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002825 named!(parse -> Self, do_parse!(
2826 path: syn!(Path) >>
2827 tuple: syn!(PatTuple) >>
2828 (PatTupleStruct {
2829 path: path,
2830 pat: tuple,
2831 })
2832 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002833
2834 fn description() -> Option<&'static str> {
2835 Some("tuple struct pattern")
2836 }
Alex Crichton954046c2017-05-30 21:49:42 -07002837 }
2838
Michael Layzell734adb42017-06-07 16:58:31 -04002839 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002840 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002841 named!(parse -> Self, do_parse!(
2842 path: syn!(Path) >>
2843 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002844 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002845 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002846 (fields, base)
2847 )) >>
2848 (PatStruct {
2849 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002850 fields: (data.1).0,
2851 brace_token: data.0,
2852 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002853 })
2854 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002855
2856 fn description() -> Option<&'static str> {
2857 Some("struct pattern")
2858 }
Alex Crichton954046c2017-05-30 21:49:42 -07002859 }
2860
Michael Layzell734adb42017-06-07 16:58:31 -04002861 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002862 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002863 named!(parse -> Self, alt!(
2864 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002865 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002866 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002867 pat: syn!(Pat) >>
2868 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002869 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002870 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002871 attrs: Vec::new(),
2872 colon_token: Some(colon),
2873 })
2874 )
2875 |
2876 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002877 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002878 by_ref: option!(keyword!(ref)) >>
2879 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002880 ident: syn!(Ident) >>
2881 ({
2882 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002883 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002884 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002885 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002886 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002887 }.into();
2888 if let Some(boxed) = boxed {
2889 pat = PatBox {
2890 pat: Box::new(pat),
2891 box_token: boxed,
2892 }.into();
2893 }
2894 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002895 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002896 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002897 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002898 colon_token: None,
2899 }
2900 })
2901 )
2902 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002903
2904 fn description() -> Option<&'static str> {
2905 Some("field pattern")
2906 }
Alex Crichton954046c2017-05-30 21:49:42 -07002907 }
2908
David Tolnay85b69a42017-12-27 20:43:10 -05002909 impl Synom for Member {
2910 named!(parse -> Self, alt!(
2911 syn!(Ident) => { Member::Named }
2912 |
2913 syn!(Index) => { Member::Unnamed }
2914 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002915
2916 fn description() -> Option<&'static str> {
2917 Some("field member")
2918 }
David Tolnay85b69a42017-12-27 20:43:10 -05002919 }
2920
David Tolnay85b69a42017-12-27 20:43:10 -05002921 impl Synom for Index {
2922 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002923 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002924 ({
David Tolnay360efd22018-01-04 23:35:26 -08002925 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002926 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002927 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002928 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002929 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002930 })
David Tolnay85b69a42017-12-27 20:43:10 -05002931 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002932
2933 fn description() -> Option<&'static str> {
2934 Some("field index")
2935 }
David Tolnay85b69a42017-12-27 20:43:10 -05002936 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002937
Michael Layzell734adb42017-06-07 16:58:31 -04002938 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002939 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002940 named!(parse -> Self, map!(
2941 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002942 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002943 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002944
2945 fn description() -> Option<&'static str> {
2946 Some("path pattern")
2947 }
Alex Crichton954046c2017-05-30 21:49:42 -07002948 }
David Tolnay9636c052016-10-02 17:11:17 -07002949
Michael Layzell734adb42017-06-07 16:58:31 -04002950 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002951 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002952 named!(parse -> Self, do_parse!(
2953 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002954 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002955 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2956 tuple!(punct!(..), option!(punct!(,)))
2957 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002958 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002959 Some((_, Some(_))) => true,
2960 _ => false,
2961 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002962 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002963 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002964 )) >>
2965 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002966 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002967 let (dotdot, trailing) = match dotdot {
2968 Some((a, b)) => (Some(a), Some(b)),
2969 None => (None, None),
2970 };
2971 PatTuple {
2972 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002973 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002974 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002975 comma_token: trailing.unwrap_or_default(),
2976 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002977 }
2978 })
2979 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002980
2981 fn description() -> Option<&'static str> {
2982 Some("tuple pattern")
2983 }
Alex Crichton954046c2017-05-30 21:49:42 -07002984 }
David Tolnayfbb73232016-10-03 01:00:06 -07002985
Michael Layzell734adb42017-06-07 16:58:31 -04002986 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002987 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002988 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002989 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002990 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002991 pat: syn!(Pat) >>
2992 (PatRef {
2993 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002994 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002995 and_token: and,
2996 })
2997 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002998
2999 fn description() -> Option<&'static str> {
3000 Some("reference pattern")
3001 }
Alex Crichton954046c2017-05-30 21:49:42 -07003002 }
David Tolnayffdb97f2016-10-03 01:28:33 -07003003
Michael Layzell734adb42017-06-07 16:58:31 -04003004 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003005 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04003006 named!(parse -> Self, do_parse!(
3007 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05003008 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04003009 return parse_error(); // these need to be parsed by pat_path
3010 } else {
3011 PatLit {
3012 expr: Box::new(lit),
3013 }
3014 })
3015 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003016
3017 fn description() -> Option<&'static str> {
3018 Some("literal pattern")
3019 }
Alex Crichton954046c2017-05-30 21:49:42 -07003020 }
David Tolnaye1310902016-10-29 23:40:00 -07003021
Michael Layzell734adb42017-06-07 16:58:31 -04003022 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003023 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04003024 named!(parse -> Self, do_parse!(
3025 lo: pat_lit_expr >>
3026 limits: syn!(RangeLimits) >>
3027 hi: pat_lit_expr >>
3028 (PatRange {
3029 lo: Box::new(lo),
3030 hi: Box::new(hi),
3031 limits: limits,
3032 })
3033 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003034
3035 fn description() -> Option<&'static str> {
3036 Some("range pattern")
3037 }
Alex Crichton954046c2017-05-30 21:49:42 -07003038 }
David Tolnaye1310902016-10-29 23:40:00 -07003039
Michael Layzell734adb42017-06-07 16:58:31 -04003040 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07003041 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003042 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07003043 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05003044 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07003045 |
David Tolnay8c91b882017-12-28 23:04:32 -05003046 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07003047 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05003048 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003049 Expr::Unary(ExprUnary {
3050 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003051 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05003052 expr: Box::new(v)
3053 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003054 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05003055 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003056 })
3057 ));
David Tolnay8b308c22016-10-03 01:24:10 -07003058
Michael Layzell734adb42017-06-07 16:58:31 -04003059 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003060 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04003061 named!(parse -> Self, map!(
3062 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05003063 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003064 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003065 dots: punct!(..) >>
3066 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003067 (dots, trailing)
3068 )) >>
3069 after: cond!(
3070 match middle {
3071 Some((_, ref trailing)) => trailing.is_some(),
3072 _ => false,
3073 },
David Tolnayf2cfd722017-12-31 18:02:51 -05003074 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04003075 ) >>
3076 (before, middle, after)
3077 )),
David Tolnay8875fca2017-12-31 13:52:37 -05003078 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05003079 let mut before: Punctuated<Pat, Token![,]> = before;
3080 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08003081 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04003082 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04003083 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04003084 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04003085 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04003086 }),
3087 bracket_token: brackets,
3088 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05003089 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04003090 None
David Tolnaydc03aec2017-12-30 01:54:18 -05003091 } else {
David Tolnay56080682018-01-06 14:01:52 -08003092 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04003093 }
3094 }),
3095 front: before,
3096 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07003097 }
Alex Crichton954046c2017-05-30 21:49:42 -07003098 }
Michael Layzell92639a52017-06-01 00:07:44 -04003099 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003100
3101 fn description() -> Option<&'static str> {
3102 Some("slice pattern")
3103 }
Alex Crichton954046c2017-05-30 21:49:42 -07003104 }
David Tolnay323279a2017-12-29 11:26:32 -05003105
3106 #[cfg(feature = "full")]
3107 impl Synom for PatMacro {
3108 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003109
3110 fn description() -> Option<&'static str> {
3111 Some("macro pattern")
3112 }
David Tolnay323279a2017-12-29 11:26:32 -05003113 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003114}
3115
David Tolnayf4bbbd92016-09-23 14:41:55 -07003116#[cfg(feature = "printing")]
3117mod printing {
3118 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003119 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003120 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003121 use proc_macro2::{Literal, TokenStream};
3122 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003123
David Tolnaybcf26022017-12-25 22:10:52 -05003124 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003125 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003126 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003127 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003128 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003129 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003130 e.to_tokens(tokens);
3131 });
3132 } else {
3133 e.to_tokens(tokens);
3134 }
3135 }
3136
David Tolnay8c91b882017-12-28 23:04:32 -05003137 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003138 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003139 tokens.append_all(attrs.outer());
3140 }
Michael Layzell734adb42017-06-07 16:58:31 -04003141
David Tolnayd997aef2018-07-21 18:42:31 -07003142 #[cfg(feature = "full")]
3143 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3144 tokens.append_all(attrs.inner());
3145 }
3146
David Tolnay8c91b882017-12-28 23:04:32 -05003147 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003148 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3149
3150 #[cfg(not(feature = "full"))]
3151 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003152
Michael Layzell734adb42017-06-07 16:58:31 -04003153 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003154 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003155 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003156 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003157 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003158 self.expr.to_tokens(tokens);
3159 }
3160 }
3161
Michael Layzell734adb42017-06-07 16:58:31 -04003162 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003163 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003164 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003165 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003166 self.place.to_tokens(tokens);
3167 self.arrow_token.to_tokens(tokens);
3168 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003169 }
3170 }
3171
Michael Layzell734adb42017-06-07 16:58:31 -04003172 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003173 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003174 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003175 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003176 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003177 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003178 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003179 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003180 }
3181 }
3182
3183 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003184 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003185 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003186 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003187 self.paren_token.surround(tokens, |tokens| {
3188 self.args.to_tokens(tokens);
3189 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003190 }
3191 }
3192
Michael Layzell734adb42017-06-07 16:58:31 -04003193 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003194 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003195 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003196 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003197 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003198 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003199 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003200 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003201 self.paren_token.surround(tokens, |tokens| {
3202 self.args.to_tokens(tokens);
3203 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003204 }
3205 }
3206
Michael Layzell734adb42017-06-07 16:58:31 -04003207 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003208 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003209 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003210 self.colon2_token.to_tokens(tokens);
3211 self.lt_token.to_tokens(tokens);
3212 self.args.to_tokens(tokens);
3213 self.gt_token.to_tokens(tokens);
3214 }
3215 }
3216
3217 #[cfg(feature = "full")]
3218 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003219 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003220 match *self {
3221 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3222 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3223 }
3224 }
3225 }
3226
3227 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003228 impl ToTokens for ExprTuple {
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 Crichtonccbb45d2017-05-23 10:58:24 -07003231 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003232 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003233 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003234 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003235 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003236 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003237 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003238 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003239 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003240 }
3241 }
3242
3243 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003244 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003245 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003246 self.left.to_tokens(tokens);
3247 self.op.to_tokens(tokens);
3248 self.right.to_tokens(tokens);
3249 }
3250 }
3251
3252 impl ToTokens for ExprUnary {
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.op.to_tokens(tokens);
3256 self.expr.to_tokens(tokens);
3257 }
3258 }
3259
David Tolnay8c91b882017-12-28 23:04:32 -05003260 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003261 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003262 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003263 self.lit.to_tokens(tokens);
3264 }
3265 }
3266
Alex Crichton62a0a592017-05-22 13:58:53 -07003267 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003268 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003269 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003270 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003271 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003272 self.ty.to_tokens(tokens);
3273 }
3274 }
3275
David Tolnay0cf94f22017-12-28 23:46:26 -05003276 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003277 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003278 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003279 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003280 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003281 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003282 self.ty.to_tokens(tokens);
3283 }
3284 }
3285
Michael Layzell734adb42017-06-07 16:58:31 -04003286 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003287 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003288 if let Some((ref else_token, ref else_)) = *else_ {
3289 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003290
3291 // If we are not one of the valid expressions to exist in an else
3292 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003293 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003294 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003295 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003296 }
3297 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003298 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003299 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003300 });
3301 }
3302 }
3303 }
3304 }
3305
3306 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003307 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003308 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003309 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003310 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003311 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003312 self.then_branch.to_tokens(tokens);
3313 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003314 }
3315 }
3316
Michael Layzell734adb42017-06-07 16:58:31 -04003317 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003318 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003319 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003320 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003321 self.if_token.to_tokens(tokens);
3322 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003323 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003324 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003325 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003326 self.then_branch.to_tokens(tokens);
3327 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003328 }
3329 }
3330
Michael Layzell734adb42017-06-07 16:58:31 -04003331 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003332 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003333 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003334 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003335 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003336 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003337 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003338 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003339 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003340 tokens.append_all(&self.body.stmts);
3341 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003342 }
3343 }
3344
Michael Layzell734adb42017-06-07 16:58:31 -04003345 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003346 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003347 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003348 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003349 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003350 self.while_token.to_tokens(tokens);
3351 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003352 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003353 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003354 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003355 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003356 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003357 tokens.append_all(&self.body.stmts);
3358 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003359 }
3360 }
3361
Michael Layzell734adb42017-06-07 16:58:31 -04003362 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003363 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003364 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003365 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003366 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003367 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003368 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003369 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003370 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003371 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003372 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003373 tokens.append_all(&self.body.stmts);
3374 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003375 }
3376 }
3377
Michael Layzell734adb42017-06-07 16:58:31 -04003378 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003379 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003380 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003381 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003382 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003383 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003384 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003385 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003386 tokens.append_all(&self.body.stmts);
3387 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003388 }
3389 }
3390
Michael Layzell734adb42017-06-07 16:58:31 -04003391 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003392 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003393 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003394 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003395 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003396 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003397 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003398 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003399 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003400 arm.to_tokens(tokens);
3401 // Ensure that we have a comma after a non-block arm, except
3402 // for the last one.
3403 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003404 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003405 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003406 }
3407 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003408 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003409 }
3410 }
3411
Michael Layzell734adb42017-06-07 16:58:31 -04003412 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003413 impl ToTokens for ExprAsync {
3414 fn to_tokens(&self, tokens: &mut TokenStream) {
3415 outer_attrs_to_tokens(&self.attrs, tokens);
3416 self.async_token.to_tokens(tokens);
3417 self.capture.to_tokens(tokens);
3418 self.block.to_tokens(tokens);
3419 }
3420 }
3421
3422 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003423 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003424 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003425 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003426 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003427 self.block.to_tokens(tokens);
3428 }
3429 }
3430
Michael Layzell734adb42017-06-07 16:58:31 -04003431 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003432 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003433 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003434 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003435 self.yield_token.to_tokens(tokens);
3436 self.expr.to_tokens(tokens);
3437 }
3438 }
3439
3440 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003441 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003442 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003443 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003444 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003445 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003446 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003447 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003448 for input in self.inputs.pairs() {
3449 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003450 FnArg::Captured(ArgCaptured {
3451 ref pat,
3452 ty: Type::Infer(_),
3453 ..
3454 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003455 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003456 }
David Tolnay56080682018-01-06 14:01:52 -08003457 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003458 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003459 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003460 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003461 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003462 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003463 self.body.to_tokens(tokens);
3464 }
3465 }
3466
Michael Layzell734adb42017-06-07 16:58:31 -04003467 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003468 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003469 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003470 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003471 self.unsafe_token.to_tokens(tokens);
3472 self.block.to_tokens(tokens);
3473 }
3474 }
3475
3476 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003477 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003478 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003479 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003480 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003481 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003482 tokens.append_all(&self.block.stmts);
3483 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003484 }
3485 }
3486
Michael Layzell734adb42017-06-07 16:58:31 -04003487 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003488 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003489 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003490 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003491 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003492 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003493 self.right.to_tokens(tokens);
3494 }
3495 }
3496
Michael Layzell734adb42017-06-07 16:58:31 -04003497 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003498 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003499 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003500 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003501 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003502 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003503 self.right.to_tokens(tokens);
3504 }
3505 }
3506
3507 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003508 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003509 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003510 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003511 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003512 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003513 }
3514 }
3515
David Tolnay85b69a42017-12-27 20:43:10 -05003516 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003517 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003518 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003519 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003520 Member::Unnamed(ref index) => index.to_tokens(tokens),
3521 }
3522 }
3523 }
3524
David Tolnay85b69a42017-12-27 20:43:10 -05003525 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003526 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003527 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3528 lit.set_span(self.span);
3529 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003530 }
3531 }
3532
3533 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003534 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003535 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003536 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003537 self.bracket_token.surround(tokens, |tokens| {
3538 self.index.to_tokens(tokens);
3539 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003540 }
3541 }
3542
Michael Layzell734adb42017-06-07 16:58:31 -04003543 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003544 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003545 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003546 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003547 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003548 match self.limits {
3549 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3550 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3551 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003552 self.to.to_tokens(tokens);
3553 }
3554 }
3555
3556 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003557 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003558 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003559 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003560 }
3561 }
3562
Michael Layzell734adb42017-06-07 16:58:31 -04003563 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003564 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003565 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003566 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003567 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003568 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003569 self.expr.to_tokens(tokens);
3570 }
3571 }
3572
Michael Layzell734adb42017-06-07 16:58:31 -04003573 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003574 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003575 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003576 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003577 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003578 self.label.to_tokens(tokens);
3579 self.expr.to_tokens(tokens);
3580 }
3581 }
3582
Michael Layzell734adb42017-06-07 16:58:31 -04003583 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003584 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003585 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003586 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003587 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003588 self.label.to_tokens(tokens);
3589 }
3590 }
3591
Michael Layzell734adb42017-06-07 16:58:31 -04003592 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003593 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003594 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003595 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003596 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003597 self.expr.to_tokens(tokens);
3598 }
3599 }
3600
Michael Layzell734adb42017-06-07 16:58:31 -04003601 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003602 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003603 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003604 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003605 self.mac.to_tokens(tokens);
3606 }
3607 }
3608
3609 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003610 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003611 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003612 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003613 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003614 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003615 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003616 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003617 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003618 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003619 self.rest.to_tokens(tokens);
3620 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003621 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003622 }
3623 }
3624
Michael Layzell734adb42017-06-07 16:58:31 -04003625 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003626 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003627 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003628 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003629 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003630 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003631 self.expr.to_tokens(tokens);
3632 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003633 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003634 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003635 }
3636 }
3637
David Tolnaye98775f2017-12-28 23:17:00 -05003638 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003639 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003640 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003641 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003642 self.group_token.surround(tokens, |tokens| {
3643 self.expr.to_tokens(tokens);
3644 });
3645 }
3646 }
3647
Alex Crichton62a0a592017-05-22 13:58:53 -07003648 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003649 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003650 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003651 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003652 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003653 self.expr.to_tokens(tokens);
3654 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003655 }
3656 }
3657
Michael Layzell734adb42017-06-07 16:58:31 -04003658 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003659 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003660 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003661 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003662 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003663 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003664 }
3665 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003666
David Tolnay2ae520a2017-12-29 11:19:50 -05003667 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003668 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003669 self.tts.to_tokens(tokens);
3670 }
3671 }
3672
Michael Layzell734adb42017-06-07 16:58:31 -04003673 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003674 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003675 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003676 self.name.to_tokens(tokens);
3677 self.colon_token.to_tokens(tokens);
3678 }
3679 }
3680
3681 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003682 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003683 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003684 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003685 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003686 if let Some(ref colon_token) = self.colon_token {
3687 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003688 self.expr.to_tokens(tokens);
3689 }
David Tolnay055a7042016-10-02 19:23:54 -07003690 }
3691 }
3692
Michael Layzell734adb42017-06-07 16:58:31 -04003693 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003694 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003695 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003696 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003697 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003698 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003699 if let Some((ref if_token, ref guard)) = self.guard {
3700 if_token.to_tokens(tokens);
3701 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003702 }
David Tolnaydfb91432018-03-31 19:19:44 +02003703 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003704 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003705 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003706 }
3707 }
3708
Michael Layzell734adb42017-06-07 16:58:31 -04003709 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003710 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003711 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003712 self.underscore_token.to_tokens(tokens);
3713 }
3714 }
3715
Michael Layzell734adb42017-06-07 16:58:31 -04003716 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003717 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003718 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003719 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003720 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003721 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003722 if let Some((ref at_token, ref subpat)) = self.subpat {
3723 at_token.to_tokens(tokens);
3724 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003725 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003726 }
3727 }
3728
Michael Layzell734adb42017-06-07 16:58:31 -04003729 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003730 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003731 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003732 self.path.to_tokens(tokens);
3733 self.brace_token.surround(tokens, |tokens| {
3734 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003735 // NOTE: We need a comma before the dot2 token if it is present.
3736 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003737 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003738 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003739 self.dot2_token.to_tokens(tokens);
3740 });
3741 }
3742 }
3743
Michael Layzell734adb42017-06-07 16:58:31 -04003744 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003745 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003746 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003747 self.path.to_tokens(tokens);
3748 self.pat.to_tokens(tokens);
3749 }
3750 }
3751
Michael Layzell734adb42017-06-07 16:58:31 -04003752 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003753 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003754 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003755 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3756 }
3757 }
3758
Michael Layzell734adb42017-06-07 16:58:31 -04003759 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003760 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003761 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003762 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003763 self.front.to_tokens(tokens);
3764 if let Some(ref dot2_token) = self.dot2_token {
3765 if !self.front.empty_or_trailing() {
3766 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003767 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003768 }
David Tolnay41871922017-12-29 01:53:45 -05003769 dot2_token.to_tokens(tokens);
3770 self.comma_token.to_tokens(tokens);
3771 if self.comma_token.is_none() && !self.back.is_empty() {
3772 // Ensure there is a comma after the .. token.
3773 <Token![,]>::default().to_tokens(tokens);
3774 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003775 }
David Tolnay41871922017-12-29 01:53:45 -05003776 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003777 });
3778 }
3779 }
3780
Michael Layzell734adb42017-06-07 16:58:31 -04003781 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003782 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003783 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003784 self.box_token.to_tokens(tokens);
3785 self.pat.to_tokens(tokens);
3786 }
3787 }
3788
Michael Layzell734adb42017-06-07 16:58:31 -04003789 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003790 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003791 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003792 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003793 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003794 self.pat.to_tokens(tokens);
3795 }
3796 }
3797
Michael Layzell734adb42017-06-07 16:58:31 -04003798 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003799 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003800 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003801 self.expr.to_tokens(tokens);
3802 }
3803 }
3804
Michael Layzell734adb42017-06-07 16:58:31 -04003805 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003806 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003807 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003808 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003809 match self.limits {
3810 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003811 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003812 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003813 self.hi.to_tokens(tokens);
3814 }
3815 }
3816
Michael Layzell734adb42017-06-07 16:58:31 -04003817 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003818 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003819 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003820 // XXX: This is a mess, and it will be so easy to screw it up. How
3821 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003822 self.bracket_token.surround(tokens, |tokens| {
3823 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003824
3825 // If we need a comma before the middle or standalone .. token,
3826 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003827 if !self.front.empty_or_trailing()
3828 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003829 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003830 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003831 }
3832
3833 // If we have an identifier, we always need a .. token.
3834 if self.middle.is_some() {
3835 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003836 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003837 } else if self.dot2_token.is_some() {
3838 self.dot2_token.to_tokens(tokens);
3839 }
3840
3841 // Make sure we have a comma before the back half.
3842 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003843 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003844 self.back.to_tokens(tokens);
3845 } else {
3846 self.comma_token.to_tokens(tokens);
3847 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003848 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003849 }
3850 }
3851
Michael Layzell734adb42017-06-07 16:58:31 -04003852 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003853 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003854 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003855 self.mac.to_tokens(tokens);
3856 }
3857 }
3858
3859 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003860 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003861 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003862 self.tts.to_tokens(tokens);
3863 }
3864 }
3865
3866 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003867 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003868 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003869 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003870 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003871 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003872 }
3873 self.pat.to_tokens(tokens);
3874 }
3875 }
3876
Michael Layzell734adb42017-06-07 16:58:31 -04003877 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003878 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003879 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003880 self.brace_token.surround(tokens, |tokens| {
3881 tokens.append_all(&self.stmts);
3882 });
David Tolnay42602292016-10-01 22:25:45 -07003883 }
3884 }
3885
Michael Layzell734adb42017-06-07 16:58:31 -04003886 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003887 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003888 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003889 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003890 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003891 Stmt::Item(ref item) => item.to_tokens(tokens),
3892 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003893 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003894 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003895 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003896 }
David Tolnay42602292016-10-01 22:25:45 -07003897 }
3898 }
3899 }
David Tolnay191e0582016-10-02 18:31:09 -07003900
Michael Layzell734adb42017-06-07 16:58:31 -04003901 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003902 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003903 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003904 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003905 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003906 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003907 if let Some((ref colon_token, ref ty)) = self.ty {
3908 colon_token.to_tokens(tokens);
3909 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003910 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003911 if let Some((ref eq_token, ref init)) = self.init {
3912 eq_token.to_tokens(tokens);
3913 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003914 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003915 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003916 }
3917 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003918}