blob: 748f4a5277485fd0a24f0fb8695e4b9e3683cda2 [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 Tolnaya454c8f2018-01-07 01:01:10 -0800512 /// A catch expression: `do catch { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800513 ///
514 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400515 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500516 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub do_token: Token![do],
518 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700519 pub block: Block,
520 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700521
David Tolnaya454c8f2018-01-07 01:01:10 -0800522 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800523 ///
524 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700525 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500526 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800527 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700528 pub expr: Option<Box<Expr>>,
529 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500530
David Tolnaya454c8f2018-01-07 01:01:10 -0800531 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800532 ///
533 /// *This type is available if Syn is built with the `"derive"` or
534 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500535 pub Verbatim(ExprVerbatim #manual_extra_traits {
536 pub tts: TokenStream,
537 }),
538 }
539}
540
541#[cfg(feature = "extra-traits")]
542impl Eq for ExprVerbatim {}
543
544#[cfg(feature = "extra-traits")]
545impl PartialEq for ExprVerbatim {
546 fn eq(&self, other: &Self) -> bool {
547 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
548 }
549}
550
551#[cfg(feature = "extra-traits")]
552impl Hash for ExprVerbatim {
553 fn hash<H>(&self, state: &mut H)
554 where
555 H: Hasher,
556 {
557 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700558 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700559}
560
David Tolnay8c91b882017-12-28 23:04:32 -0500561impl Expr {
562 // Not public API.
563 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500564 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500565 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500566 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800567 Expr::Box(ExprBox { ref mut attrs, .. })
568 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
569 | Expr::Array(ExprArray { ref mut attrs, .. })
570 | Expr::Call(ExprCall { ref mut attrs, .. })
571 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
572 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
573 | Expr::Binary(ExprBinary { ref mut attrs, .. })
574 | Expr::Unary(ExprUnary { ref mut attrs, .. })
575 | Expr::Lit(ExprLit { ref mut attrs, .. })
576 | Expr::Cast(ExprCast { ref mut attrs, .. })
577 | Expr::Type(ExprType { ref mut attrs, .. })
578 | Expr::If(ExprIf { ref mut attrs, .. })
579 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
580 | Expr::While(ExprWhile { ref mut attrs, .. })
581 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
582 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
583 | Expr::Loop(ExprLoop { ref mut attrs, .. })
584 | Expr::Match(ExprMatch { ref mut attrs, .. })
585 | Expr::Closure(ExprClosure { ref mut attrs, .. })
586 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
587 | Expr::Block(ExprBlock { ref mut attrs, .. })
588 | Expr::Assign(ExprAssign { ref mut attrs, .. })
589 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
590 | Expr::Field(ExprField { ref mut attrs, .. })
591 | Expr::Index(ExprIndex { ref mut attrs, .. })
592 | Expr::Range(ExprRange { ref mut attrs, .. })
593 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200594 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800595 | Expr::Break(ExprBreak { ref mut attrs, .. })
596 | Expr::Continue(ExprContinue { ref mut attrs, .. })
597 | Expr::Return(ExprReturn { ref mut attrs, .. })
598 | Expr::Macro(ExprMacro { ref mut attrs, .. })
599 | Expr::Struct(ExprStruct { ref mut attrs, .. })
600 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
601 | Expr::Paren(ExprParen { ref mut attrs, .. })
602 | Expr::Group(ExprGroup { ref mut attrs, .. })
603 | Expr::Try(ExprTry { ref mut attrs, .. })
604 | Expr::Catch(ExprCatch { ref mut attrs, .. })
605 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500606 Expr::Verbatim(_) => {
607 // TODO
608 Vec::new()
609 }
David Tolnay8c91b882017-12-28 23:04:32 -0500610 }
611 }
612}
613
David Tolnay85b69a42017-12-27 20:43:10 -0500614ast_enum! {
615 /// A struct or tuple struct field accessed in a struct literal or field
616 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800617 ///
618 /// *This type is available if Syn is built with the `"derive"` or `"full"`
619 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500620 pub enum Member {
621 /// A named field like `self.x`.
622 Named(Ident),
623 /// An unnamed field like `self.0`.
624 Unnamed(Index),
625 }
626}
627
David Tolnay85b69a42017-12-27 20:43:10 -0500628ast_struct! {
629 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800630 ///
631 /// *This type is available if Syn is built with the `"derive"` or `"full"`
632 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500633 pub struct Index #manual_extra_traits {
634 pub index: u32,
635 pub span: Span,
636 }
637}
638
David Tolnay14982012017-12-29 00:49:51 -0500639impl From<usize> for Index {
640 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700641 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500642 Index {
643 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700644 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500645 }
646 }
647}
648
649#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500650impl Eq for Index {}
651
David Tolnay14982012017-12-29 00:49:51 -0500652#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500653impl PartialEq for Index {
654 fn eq(&self, other: &Self) -> bool {
655 self.index == other.index
656 }
657}
658
David Tolnay14982012017-12-29 00:49:51 -0500659#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500660impl Hash for Index {
661 fn hash<H: Hasher>(&self, state: &mut H) {
662 self.index.hash(state);
663 }
664}
665
666#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700667ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800668 /// The `::<>` explicit type parameters passed to a method call:
669 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800670 ///
671 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500672 pub struct MethodTurbofish {
673 pub colon2_token: Token![::],
674 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500675 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500676 pub gt_token: Token![>],
677 }
678}
679
680#[cfg(feature = "full")]
681ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800682 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800683 ///
684 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500685 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800686 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500687 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800688 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500689 ///
690 /// NOTE: Identity expressions are represented as Type arguments, as
691 /// they are indistinguishable syntactically.
692 Const(Expr),
693 }
694}
695
696#[cfg(feature = "full")]
697ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700698 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800699 ///
700 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700701 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500702 /// Attributes tagged on the field.
703 pub attrs: Vec<Attribute>,
704
705 /// Name or index of the field.
706 pub member: Member,
707
David Tolnay5d7098a2017-12-29 01:35:24 -0500708 /// The colon in `Struct { x: x }`. If written in shorthand like
709 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500710 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500711
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 /// Value of the field.
713 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700714 }
David Tolnay055a7042016-10-02 19:23:54 -0700715}
716
Michael Layzell734adb42017-06-07 16:58:31 -0400717#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700718ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800719 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800720 ///
721 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500722 pub struct Label {
723 pub name: Lifetime,
724 pub colon_token: Token![:],
725 }
726}
727
728#[cfg(feature = "full")]
729ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800730 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800731 ///
732 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700733 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500734 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 /// Statements in a block
736 pub stmts: Vec<Stmt>,
737 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700738}
739
Michael Layzell734adb42017-06-07 16:58:31 -0400740#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700741ast_enum! {
742 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800743 ///
744 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700745 pub enum Stmt {
746 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800747 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700748
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800750 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700751
Alex Crichton62a0a592017-05-22 13:58:53 -0700752 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800753 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700754
David Tolnaya454c8f2018-01-07 01:01:10 -0800755 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800756 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700757 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700758}
759
Michael Layzell734adb42017-06-07 16:58:31 -0400760#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700761ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800762 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800763 ///
764 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700765 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500766 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800767 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200768 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500769 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500770 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500771 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700772 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700773}
774
Michael Layzell734adb42017-06-07 16:58:31 -0400775#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700776ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800777 /// A pattern in a local binding, function signature, match expression, or
778 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800779 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800780 /// *This type is available if Syn is built with the `"full"` feature.*
781 ///
David Tolnay614a0142018-01-07 10:25:43 -0800782 /// # Syntax tree enum
783 ///
784 /// This type is a [syntax tree enum].
785 ///
786 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700787 // Clippy false positive
788 // https://github.com/Manishearth/rust-clippy/issues/1241
789 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
790 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800791 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800792 ///
793 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700794 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800795 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700796 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700797
David Tolnaya454c8f2018-01-07 01:01:10 -0800798 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800799 ///
800 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700801 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500802 pub by_ref: Option<Token![ref]>,
803 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700804 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500805 pub subpat: Option<(Token![@], Box<Pat>)>,
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 struct or struct variant pattern: `Variant { x, y, .. }`.
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 Struct(PatStruct {
812 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500813 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500814 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800815 pub dot2_token: Option<Token![..]>,
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 tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
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 TupleStruct(PatTupleStruct {
822 pub path: Path,
823 pub pat: PatTuple,
824 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700825
David Tolnaya454c8f2018-01-07 01:01:10 -0800826 /// A path pattern like `Color::Red`, optionally qualified with a
827 /// self-type.
828 ///
829 /// Unquailfied path patterns can legally refer to variants, structs,
830 /// constants or associated constants. Quailfied path patterns like
831 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
832 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800833 ///
834 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700835 pub Path(PatPath {
836 pub qself: Option<QSelf>,
837 pub path: Path,
838 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700839
David Tolnaya454c8f2018-01-07 01:01:10 -0800840 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800841 ///
842 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700843 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500844 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500845 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500846 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500847 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500848 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700849 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800850
851 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800852 ///
853 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700854 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800855 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500856 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700857 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800858
859 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800860 ///
861 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700862 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800863 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500864 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500865 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700866 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800867
868 /// A literal pattern: `0`.
869 ///
870 /// This holds an `Expr` rather than a `Lit` because negative numbers
871 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800872 ///
873 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700874 pub Lit(PatLit {
875 pub expr: Box<Expr>,
876 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800877
878 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800879 ///
880 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700881 pub Range(PatRange {
882 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700883 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500884 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700885 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800886
887 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800888 ///
889 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700890 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500891 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500892 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700893 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500894 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500895 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500896 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700897 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800898
899 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800900 ///
901 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500902 pub Macro(PatMacro {
903 pub mac: Macro,
904 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800905
906 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800907 ///
908 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500909 pub Verbatim(PatVerbatim #manual_extra_traits {
910 pub tts: TokenStream,
911 }),
912 }
913}
914
David Tolnayc43b44e2017-12-30 23:55:54 -0500915#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500916impl Eq for PatVerbatim {}
917
David Tolnayc43b44e2017-12-30 23:55:54 -0500918#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500919impl PartialEq for PatVerbatim {
920 fn eq(&self, other: &Self) -> bool {
921 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
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 Hash for PatVerbatim {
927 fn hash<H>(&self, state: &mut H)
928 where
929 H: Hasher,
930 {
931 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700932 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700933}
934
Michael Layzell734adb42017-06-07 16:58:31 -0400935#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700936ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800937 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700938 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800939 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700940 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500941 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800942 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500943 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700944 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800945 /// 0...10 => {
946 /// return true;
947 /// }
948 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500949 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700950 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800951 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500952 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700953 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800954 ///
955 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700956 pub struct Arm {
957 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200958 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500959 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500960 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200961 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800963 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700965}
966
Michael Layzell734adb42017-06-07 16:58:31 -0400967#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700968ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800969 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800970 ///
971 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700972 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700973 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800974 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800975 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800976 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800977 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700978 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700979}
980
Michael Layzell734adb42017-06-07 16:58:31 -0400981#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700982ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800983 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800985 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
986 /// 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 -0800987 ///
988 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700989 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500990 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500991 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500992 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700993 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700994 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700995}
996
Michael Layzell3936ceb2017-07-08 00:28:36 -0400997#[cfg(any(feature = "parsing", feature = "printing"))]
998#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700999fn arm_expr_requires_comma(expr: &Expr) -> bool {
1000 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1001 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001002 match *expr {
1003 Expr::Unsafe(..)
1004 | Expr::Block(..)
1005 | Expr::If(..)
1006 | Expr::IfLet(..)
1007 | Expr::Match(..)
1008 | Expr::While(..)
1009 | Expr::WhileLet(..)
1010 | Expr::Loop(..)
1011 | Expr::ForLoop(..)
1012 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001013 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001014 }
1015}
1016
David Tolnayb9c8e322016-09-23 20:48:37 -07001017#[cfg(feature = "parsing")]
1018pub mod parsing {
1019 use super::*;
David Tolnay9cc2f092018-08-24 15:51:37 -04001020 use path::parsing::mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001021 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001022 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001023
David Tolnaydfc886b2018-01-06 08:03:09 -08001024 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001025 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001026 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001027 #[cfg(feature = "full")]
1028 use proc_macro2::TokenStream;
David Tolnay203557a2017-12-27 23:59:33 -05001029 use synom::PResult;
David Tolnay94d2b792018-04-29 12:26:10 -07001030 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001031
David Tolnaybcf26022017-12-25 22:10:52 -05001032 // When we're parsing expressions which occur before blocks, like in an if
1033 // statement's condition, we cannot parse a struct literal.
1034 //
1035 // Struct literals are ambiguous in certain positions
1036 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001037 macro_rules! ambiguous_expr {
1038 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001039 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001040 };
1041 }
1042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // When we are parsing an optional suffix expression, we cannot allow blocks
1044 // if structs are not allowed.
1045 //
1046 // Example:
1047 //
1048 // if break {} {}
1049 //
1050 // is ambiguous between:
1051 //
1052 // if (break {}) {}
1053 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001054 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001055 macro_rules! opt_ambiguous_expr {
1056 ($i:expr, $allow_struct:ident) => {
1057 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1058 };
1059 }
1060
Alex Crichton954046c2017-05-30 21:49:42 -07001061 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001062 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001063
1064 fn description() -> Option<&'static str> {
1065 Some("expression")
1066 }
1067 }
1068
Michael Layzell734adb42017-06-07 16:58:31 -04001069 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001070 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1071
David Tolnaybcf26022017-12-25 22:10:52 -05001072 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001073 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001074 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001075 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001076 }
1077
Michael Layzell734adb42017-06-07 16:58:31 -04001078 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001079 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001080 // NOTE: We intentionally skip assign_expr, placement_expr, and
1081 // range_expr, as they are not parsed in non-full mode.
1082 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001083 }
1084
David Tolnaybcf26022017-12-25 22:10:52 -05001085 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001086 macro_rules! binop {
1087 (
1088 $name: ident,
1089 $next: ident,
1090 $submac: ident!( $($args:tt)* )
1091 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001092 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001093 mut e: call!($next, allow_struct, allow_block) >>
1094 many0!(do_parse!(
1095 op: $submac!($($args)*) >>
1096 rhs: call!($next, allow_struct, true) >>
1097 ({
1098 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001099 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001100 left: Box::new(e.into()),
1101 op: op,
1102 right: Box::new(rhs.into()),
1103 }.into();
1104 })
1105 )) >>
1106 (e)
1107 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001108 }
David Tolnay54e854d2016-10-24 12:03:30 -07001109 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001110
David Tolnaybcf26022017-12-25 22:10:52 -05001111 // <placement> = <placement> ..
1112 // <placement> += <placement> ..
1113 // <placement> -= <placement> ..
1114 // <placement> *= <placement> ..
1115 // <placement> /= <placement> ..
1116 // <placement> %= <placement> ..
1117 // <placement> ^= <placement> ..
1118 // <placement> &= <placement> ..
1119 // <placement> |= <placement> ..
1120 // <placement> <<= <placement> ..
1121 // <placement> >>= <placement> ..
1122 //
1123 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001124 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001125 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001126 mut e: call!(placement_expr, allow_struct, allow_block) >>
1127 alt!(
1128 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001129 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001130 // Recurse into self to parse right-associative operator.
1131 rhs: call!(assign_expr, allow_struct, true) >>
1132 ({
1133 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001134 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001135 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001136 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001137 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001138 }.into();
1139 })
1140 )
1141 |
1142 do_parse!(
1143 op: call!(BinOp::parse_assign_op) >>
1144 // Recurse into self to parse right-associative operator.
1145 rhs: call!(assign_expr, allow_struct, true) >>
1146 ({
1147 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001148 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001149 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001150 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001151 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001152 }.into();
1153 })
1154 )
1155 |
1156 epsilon!()
1157 ) >>
1158 (e)
1159 ));
1160
David Tolnaybcf26022017-12-25 22:10:52 -05001161 // <range> <- <range> ..
1162 //
1163 // NOTE: The `in place { expr }` version of this syntax is parsed in
1164 // `atom_expr`, not here.
1165 //
1166 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001167 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001168 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001169 mut e: call!(range_expr, allow_struct, allow_block) >>
1170 alt!(
1171 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001172 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 // Recurse into self to parse right-associative operator.
1174 rhs: call!(placement_expr, allow_struct, true) >>
1175 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001176 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001177 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001178 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001179 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001180 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001181 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001182 }.into();
1183 })
1184 )
1185 |
1186 epsilon!()
1187 ) >>
1188 (e)
1189 ));
1190
David Tolnaybcf26022017-12-25 22:10:52 -05001191 // <or> ... <or> ..
1192 // <or> .. <or> ..
1193 // <or> ..
1194 //
1195 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1196 // rules are for parsing these expressions are, but this is not correct.
1197 // For example, `a .. b .. c` is not a legal expression. It should not
1198 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1199 //
1200 // NOTE: The form of ranges which don't include a preceding expression are
1201 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001202 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001203 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001204 mut e: call!(or_expr, allow_struct, allow_block) >>
1205 many0!(do_parse!(
1206 limits: syn!(RangeLimits) >>
1207 // We don't want to allow blocks here if we don't allow structs. See
1208 // the reasoning for `opt_ambiguous_expr!` above.
1209 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1210 ({
1211 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001212 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001213 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001214 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001215 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001216 }.into();
1217 })
1218 )) >>
1219 (e)
1220 ));
1221
David Tolnaybcf26022017-12-25 22:10:52 -05001222 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001223 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001224
David Tolnaybcf26022017-12-25 22:10:52 -05001225 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001226 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001227
David Tolnaybcf26022017-12-25 22:10:52 -05001228 // <bitor> == <bitor> ...
1229 // <bitor> != <bitor> ...
1230 // <bitor> >= <bitor> ...
1231 // <bitor> <= <bitor> ...
1232 // <bitor> > <bitor> ...
1233 // <bitor> < <bitor> ...
1234 //
1235 // NOTE: This operator appears to be parsed as left-associative, but errors
1236 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001237 binop!(
1238 compare_expr,
1239 bitor_expr,
1240 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001241 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001243 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001244 |
1245 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001246 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 |
1248 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001249 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001251 do_parse!(
1252 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001253 not!(punct!(<-)) >>
1254 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001255 (BinOp::Lt(t))
1256 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001257 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001258 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001259 )
1260 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001261
David Tolnaybcf26022017-12-25 22:10:52 -05001262 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001263 binop!(
1264 bitor_expr,
1265 bitxor_expr,
1266 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1267 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001268
David Tolnaybcf26022017-12-25 22:10:52 -05001269 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001270 binop!(
1271 bitxor_expr,
1272 bitand_expr,
1273 do_parse!(
1274 // NOTE: Make sure we aren't looking at ^=.
1275 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1276 )
1277 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001278
David Tolnaybcf26022017-12-25 22:10:52 -05001279 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001280 binop!(
1281 bitand_expr,
1282 shift_expr,
1283 do_parse!(
1284 // NOTE: Make sure we aren't looking at && or &=.
1285 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1286 )
1287 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001288
David Tolnaybcf26022017-12-25 22:10:52 -05001289 // <arith> << <arith> ...
1290 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001291 binop!(
1292 shift_expr,
1293 arith_expr,
1294 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001295 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001296 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001297 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001298 )
1299 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001300
David Tolnaybcf26022017-12-25 22:10:52 -05001301 // <term> + <term> ...
1302 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001303 binop!(
1304 arith_expr,
1305 term_expr,
1306 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001307 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001308 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001309 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001310 )
1311 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001312
David Tolnaybcf26022017-12-25 22:10:52 -05001313 // <cast> * <cast> ...
1314 // <cast> / <cast> ...
1315 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001316 binop!(
1317 term_expr,
1318 cast_expr,
1319 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001320 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001321 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001322 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001323 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001324 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001325 )
1326 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001327
David Tolnaybcf26022017-12-25 22:10:52 -05001328 // <unary> as <ty>
1329 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001330 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001331 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 mut e: call!(unary_expr, allow_struct, allow_block) >>
1333 many0!(alt!(
1334 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001335 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001336 // We can't accept `A + B` in cast expressions, as it's
1337 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001338 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001339 ({
1340 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001341 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001342 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001343 as_token: as_,
1344 ty: Box::new(ty),
1345 }.into();
1346 })
1347 )
1348 |
1349 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001350 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001351 // We can't accept `A + B` in cast expressions, as it's
1352 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001353 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001354 ({
1355 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001356 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001357 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001358 colon_token: colon,
1359 ty: Box::new(ty),
1360 }.into();
1361 })
1362 )
1363 )) >>
1364 (e)
1365 ));
1366
David Tolnay0cf94f22017-12-28 23:46:26 -05001367 // <unary> as <ty>
1368 #[cfg(not(feature = "full"))]
1369 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1370 mut e: call!(unary_expr, allow_struct, allow_block) >>
1371 many0!(do_parse!(
1372 as_: keyword!(as) >>
1373 // We can't accept `A + B` in cast expressions, as it's
1374 // ambiguous with the + expression.
1375 ty: call!(Type::without_plus) >>
1376 ({
1377 e = ExprCast {
1378 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001379 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001380 as_token: as_,
1381 ty: Box::new(ty),
1382 }.into();
1383 })
1384 )) >>
1385 (e)
1386 ));
1387
David Tolnaybcf26022017-12-25 22:10:52 -05001388 // <UnOp> <trailer>
1389 // & <trailer>
1390 // &mut <trailer>
1391 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001392 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001393 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001394 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001395 attrs: many0!(Attribute::parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001396 op: syn!(UnOp) >>
1397 expr: call!(unary_expr, allow_struct, true) >>
1398 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001399 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001400 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001401 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001402 }.into())
1403 )
1404 |
1405 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001406 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001407 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001408 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001409 expr: call!(unary_expr, allow_struct, true) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001410 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001411 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001412 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001413 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001414 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001415 }.into())
1416 )
1417 |
1418 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001419 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001420 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001421 expr: call!(unary_expr, allow_struct, true) >>
1422 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001423 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001424 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001425 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001426 }.into())
1427 )
1428 |
1429 call!(trailer_expr, allow_struct, allow_block)
1430 ));
1431
Michael Layzell734adb42017-06-07 16:58:31 -04001432 // XXX: This duplication is ugly
1433 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001434 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001435 do_parse!(
1436 op: syn!(UnOp) >>
1437 expr: call!(unary_expr, allow_struct, true) >>
1438 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001439 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001440 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001441 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001442 }.into())
1443 )
1444 |
1445 call!(trailer_expr, allow_struct, allow_block)
1446 ));
1447
David Tolnayd997aef2018-07-21 18:42:31 -07001448 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001449 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1450 let mut outer = Vec::new();
1451 let mut inner = Vec::new();
1452 for attr in mem::replace(attrs, Vec::new()) {
1453 match attr.style {
1454 AttrStyle::Outer => outer.push(attr),
1455 AttrStyle::Inner(_) => inner.push(attr),
1456 }
1457 }
1458 *attrs = inner;
1459 outer
1460 }
1461
David Tolnaybcf26022017-12-25 22:10:52 -05001462 // <atom> (..<args>) ...
1463 // <atom> . <ident> (..<args>) ...
1464 // <atom> . <ident> ...
1465 // <atom> . <lit> ...
1466 // <atom> [ <expr> ] ...
1467 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001468 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001469 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001470 mut e: call!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001471 outer_attrs: value!({
1472 let mut attrs = e.replace_attrs(Vec::new());
1473 let outer_attrs = take_outer(&mut attrs);
1474 e.replace_attrs(attrs);
1475 outer_attrs
1476 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001477 many0!(alt!(
1478 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001479 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001480 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001481 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001482 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001483 args: args,
1484 paren_token: paren,
1485 }.into();
1486 })
1487 |
1488 tap!(more: and_method_call => {
1489 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001490 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001491 e = call.into();
1492 })
1493 |
1494 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001495 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001496 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001497 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001498 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001499 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001500 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001501 }.into();
1502 })
1503 |
1504 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001505 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001506 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001507 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001508 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001509 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001510 index: Box::new(i),
1511 }.into();
1512 })
1513 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001514 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001515 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001516 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001517 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001518 question_token: question,
1519 }.into();
1520 })
1521 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001522 ({
1523 let mut attrs = outer_attrs;
1524 attrs.extend(e.replace_attrs(Vec::new()));
1525 e.replace_attrs(attrs);
1526 e
1527 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001528 ));
1529
Michael Layzell734adb42017-06-07 16:58:31 -04001530 // XXX: Duplication == ugly
1531 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001532 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001533 mut e: call!(atom_expr, allow_struct, allow_block) >>
1534 many0!(alt!(
1535 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001536 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001537 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001538 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001539 paren_token: args.0,
1540 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001541 }.into();
1542 })
1543 |
David Tolnayd5147742018-06-30 10:09:52 -07001544 tap!(field: and_field => {
1545 let (token, member) = field;
1546 e = ExprField {
1547 attrs: Vec::new(),
1548 base: Box::new(e),
1549 dot_token: token,
1550 member: member,
1551 }.into();
1552 })
1553 |
Michael Layzell734adb42017-06-07 16:58:31 -04001554 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001555 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001556 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001557 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001558 bracket_token: i.0,
1559 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001560 }.into();
1561 })
1562 )) >>
1563 (e)
1564 ));
1565
David Tolnaya454c8f2018-01-07 01:01:10 -08001566 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001567 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001568 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001569 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1570 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001571 |
David Tolnay8c91b882017-12-28 23:04:32 -05001572 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001573 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001574 // must be before ExprStruct
1575 call!(unstable_async_block) => { Expr::Verbatim }
1576 |
David Tolnayf7177052018-08-24 15:31:50 -04001577 // must be before ExprStruct
1578 call!(unstable_try_block) => { Expr::Verbatim }
1579 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001580 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001581 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001582 |
David Tolnay8c91b882017-12-28 23:04:32 -05001583 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001584 |
David Tolnay8c91b882017-12-28 23:04:32 -05001585 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001586 |
1587 call!(expr_break, allow_struct) // must be before expr_path
1588 |
David Tolnay8c91b882017-12-28 23:04:32 -05001589 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001590 |
1591 call!(expr_ret, allow_struct) // must be before expr_path
1592 |
David Tolnay8c91b882017-12-28 23:04:32 -05001593 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001594 |
David Tolnay8c91b882017-12-28 23:04:32 -05001595 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001596 |
David Tolnay8c91b882017-12-28 23:04:32 -05001597 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001598 |
David Tolnay8c91b882017-12-28 23:04:32 -05001599 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001600 |
David Tolnay8c91b882017-12-28 23:04:32 -05001601 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001602 |
David Tolnay8c91b882017-12-28 23:04:32 -05001603 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001604 |
David Tolnay8c91b882017-12-28 23:04:32 -05001605 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001606 |
David Tolnay8c91b882017-12-28 23:04:32 -05001607 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001608 |
David Tolnay8c91b882017-12-28 23:04:32 -05001609 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001610 |
David Tolnay8c91b882017-12-28 23:04:32 -05001611 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001612 |
David Tolnay8c91b882017-12-28 23:04:32 -05001613 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001614 |
David Tolnay8c91b882017-12-28 23:04:32 -05001615 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001616 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001617 call!(expr_closure, allow_struct)
1618 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001619 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001620 |
David Tolnay5d08ae62018-08-01 00:08:48 -07001621 call!(unstable_labeled_block) => { Expr::Verbatim }
1622 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001623 // NOTE: This is the prefix-form of range
1624 call!(expr_range, allow_struct)
1625 |
David Tolnay8c91b882017-12-28 23:04:32 -05001626 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001627 |
David Tolnay8c91b882017-12-28 23:04:32 -05001628 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001629 ));
1630
Michael Layzell734adb42017-06-07 16:58:31 -04001631 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001632 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001633 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001634 |
David Tolnay9374bc02018-01-27 18:49:36 -08001635 syn!(ExprParen) => { Expr::Paren }
1636 |
David Tolnay8c91b882017-12-28 23:04:32 -05001637 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001638 ));
1639
Michael Layzell734adb42017-06-07 16:58:31 -04001640 #[cfg(feature = "full")]
David Tolnay313a36f2018-04-29 20:13:04 -07001641 named!(expr_nosemi -> Expr, do_parse!(
1642 nosemi: alt!(
1643 syn!(ExprIf) => { Expr::If }
1644 |
1645 syn!(ExprIfLet) => { Expr::IfLet }
1646 |
1647 syn!(ExprWhile) => { Expr::While }
1648 |
1649 syn!(ExprWhileLet) => { Expr::WhileLet }
1650 |
1651 syn!(ExprForLoop) => { Expr::ForLoop }
1652 |
1653 syn!(ExprLoop) => { Expr::Loop }
1654 |
1655 syn!(ExprMatch) => { Expr::Match }
1656 |
1657 syn!(ExprCatch) => { Expr::Catch }
1658 |
1659 syn!(ExprYield) => { Expr::Yield }
1660 |
1661 syn!(ExprUnsafe) => { Expr::Unsafe }
1662 |
1663 syn!(ExprBlock) => { Expr::Block }
David Tolnay5d08ae62018-08-01 00:08:48 -07001664 |
1665 call!(unstable_labeled_block) => { Expr::Verbatim }
David Tolnay313a36f2018-04-29 20:13:04 -07001666 ) >>
1667 // If the next token is a `.` or a `?` it is special-cased to parse
1668 // as an expression instead of a blockexpression.
1669 not!(punct!(.)) >>
1670 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001671 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001672 ));
Michael Layzell35418782017-06-07 09:20:25 -04001673
David Tolnay8c91b882017-12-28 23:04:32 -05001674 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001675 #[cfg(not(feature = "full"))]
1676 named!(parse -> Self, do_parse!(
1677 lit: syn!(Lit) >>
1678 (ExprLit {
1679 attrs: Vec::new(),
1680 lit: lit,
1681 })
1682 ));
1683
1684 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001685 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07001686 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001687 lit: syn!(Lit) >>
1688 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001689 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001690 lit: lit,
1691 })
1692 ));
David Tolnay79777332018-01-07 10:04:42 -08001693
1694 fn description() -> Option<&'static str> {
1695 Some("literal")
1696 }
David Tolnay8c91b882017-12-28 23:04:32 -05001697 }
1698
1699 #[cfg(feature = "full")]
1700 impl Synom for ExprMacro {
1701 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001702 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001703 mac: syn!(Macro) >>
1704 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001705 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001706 mac: mac,
1707 })
1708 ));
David Tolnay79777332018-01-07 10:04:42 -08001709
1710 fn description() -> Option<&'static str> {
1711 Some("macro invocation expression")
1712 }
David Tolnay8c91b882017-12-28 23:04:32 -05001713 }
1714
David Tolnaye98775f2017-12-28 23:17:00 -05001715 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001716 impl Synom for ExprGroup {
1717 named!(parse -> Self, do_parse!(
1718 e: grouped!(syn!(Expr)) >>
1719 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001720 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001721 expr: Box::new(e.1),
1722 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001723 })
Michael Layzell93c36282017-06-04 20:43:14 -04001724 ));
David Tolnay79777332018-01-07 10:04:42 -08001725
1726 fn description() -> Option<&'static str> {
1727 Some("expression surrounded by invisible delimiters")
1728 }
Michael Layzell93c36282017-06-04 20:43:14 -04001729 }
1730
Alex Crichton954046c2017-05-30 21:49:42 -07001731 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001732 #[cfg(not(feature = "full"))]
1733 named!(parse -> Self, do_parse!(
1734 e: parens!(syn!(Expr)) >>
1735 (ExprParen {
1736 attrs: Vec::new(),
1737 paren_token: e.0,
1738 expr: Box::new(e.1),
1739 })
1740 ));
1741
1742 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001743 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001744 outer_attrs: many0!(Attribute::parse_outer) >>
1745 e: parens!(tuple!(
1746 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001747 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001748 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001749 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001750 attrs: {
1751 let mut attrs = outer_attrs;
1752 attrs.extend((e.1).0);
1753 attrs
1754 },
David Tolnay8875fca2017-12-31 13:52:37 -05001755 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001756 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001757 })
Michael Layzell92639a52017-06-01 00:07:44 -04001758 ));
David Tolnay79777332018-01-07 10:04:42 -08001759
1760 fn description() -> Option<&'static str> {
1761 Some("parenthesized expression")
1762 }
Alex Crichton954046c2017-05-30 21:49:42 -07001763 }
David Tolnay89e05672016-10-02 14:39:42 -07001764
Michael Layzell734adb42017-06-07 16:58:31 -04001765 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001766 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001767 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001768 outer_attrs: many0!(Attribute::parse_outer) >>
1769 elems: brackets!(tuple!(
1770 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001771 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001772 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001773 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001774 attrs: {
1775 let mut attrs = outer_attrs;
1776 attrs.extend((elems.1).0);
1777 attrs
1778 },
David Tolnay8875fca2017-12-31 13:52:37 -05001779 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001780 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001781 })
1782 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001783
1784 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001785 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001786 }
Alex Crichton954046c2017-05-30 21:49:42 -07001787 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001788
David Tolnayf2cfd722017-12-31 18:02:51 -05001789 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001790 parens!(Punctuated::parse_terminated)
1791 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001792
Michael Layzell734adb42017-06-07 16:58:31 -04001793 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001794 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001795 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001796 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001797 turbofish: option!(tuple!(
1798 punct!(::),
1799 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001800 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001801 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001802 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001803 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001804 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001805 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001806 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001807 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001808 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001809 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001810 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001811
Alex Crichton954046c2017-05-30 21:49:42 -07001812 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001813 turbofish: turbofish.map(|fish| MethodTurbofish {
1814 colon2_token: fish.0,
1815 lt_token: fish.1,
1816 args: fish.2,
1817 gt_token: fish.3,
1818 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001819 args: args.1,
1820 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001821 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001822 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001823 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001824 ));
1825
Michael Layzell734adb42017-06-07 16:58:31 -04001826 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001827 impl Synom for GenericMethodArgument {
1828 // TODO parse const generics as well
1829 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001830
1831 fn description() -> Option<&'static str> {
1832 Some("generic method argument")
1833 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001834 }
1835
1836 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001837 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001838 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001839 outer_attrs: many0!(Attribute::parse_outer) >>
1840 elems: parens!(tuple!(
1841 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001842 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001843 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001844 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001845 attrs: {
1846 let mut attrs = outer_attrs;
1847 attrs.extend((elems.1).0);
1848 attrs
1849 },
1850 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001851 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001852 })
1853 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001854
1855 fn description() -> Option<&'static str> {
1856 Some("tuple")
1857 }
Alex Crichton954046c2017-05-30 21:49:42 -07001858 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001859
Michael Layzell734adb42017-06-07 16:58:31 -04001860 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001861 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001862 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001863 if_: keyword!(if) >>
1864 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001865 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001866 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001867 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001868 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001869 else_block: option!(else_block) >>
1870 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001871 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001872 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001873 let_token: let_,
1874 eq_token: eq,
1875 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001876 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001877 brace_token: then_block.0,
1878 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001879 },
1880 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001881 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001882 })
1883 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001884
1885 fn description() -> Option<&'static str> {
1886 Some("`if let` expression")
1887 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001888 }
1889
Michael Layzell734adb42017-06-07 16:58:31 -04001890 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001891 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001892 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001893 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001894 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001895 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001896 else_block: option!(else_block) >>
1897 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001898 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001899 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001900 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001901 brace_token: then_block.0,
1902 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001903 },
1904 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001905 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001906 })
1907 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001908
1909 fn description() -> Option<&'static str> {
1910 Some("`if` expression")
1911 }
Alex Crichton954046c2017-05-30 21:49:42 -07001912 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001913
Michael Layzell734adb42017-06-07 16:58:31 -04001914 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001915 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001916 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001917 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001918 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001919 |
David Tolnay8c91b882017-12-28 23:04:32 -05001920 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001921 |
1922 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001923 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001924 (Expr::Block(ExprBlock {
1925 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001926 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001927 brace_token: else_block.0,
1928 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001929 },
1930 }))
David Tolnay939766a2016-09-23 23:48:12 -07001931 )
Alex Crichton954046c2017-05-30 21:49:42 -07001932 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001933 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001934 ));
1935
Michael Layzell734adb42017-06-07 16:58:31 -04001936 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001937 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001938 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001939 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001940 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001941 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001942 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001943 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001944 expr: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001945 block: braces!(tuple!(
1946 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001947 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001948 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001949 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001950 attrs: {
1951 let mut attrs = outer_attrs;
1952 attrs.extend((block.1).0);
1953 attrs
1954 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001955 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001956 for_token: for_,
1957 pat: Box::new(pat),
1958 in_token: in_,
1959 expr: Box::new(expr),
1960 body: Block {
1961 brace_token: block.0,
1962 stmts: (block.1).1,
1963 },
Michael Layzell92639a52017-06-01 00:07:44 -04001964 })
1965 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001966
1967 fn description() -> Option<&'static str> {
1968 Some("`for` loop")
1969 }
Alex Crichton954046c2017-05-30 21:49:42 -07001970 }
Gregory Katze5f35682016-09-27 14:20:55 -04001971
Michael Layzell734adb42017-06-07 16:58:31 -04001972 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001973 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001974 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001975 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001976 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001977 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001978 block: braces!(tuple!(
1979 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001980 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001981 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001982 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001983 attrs: {
1984 let mut attrs = outer_attrs;
1985 attrs.extend((block.1).0);
1986 attrs
1987 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001988 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001989 loop_token: loop_,
1990 body: Block {
1991 brace_token: block.0,
1992 stmts: (block.1).1,
1993 },
Michael Layzell92639a52017-06-01 00:07:44 -04001994 })
1995 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001996
1997 fn description() -> Option<&'static str> {
1998 Some("`loop`")
1999 }
Alex Crichton954046c2017-05-30 21:49:42 -07002000 }
2001
Michael Layzell734adb42017-06-07 16:58:31 -04002002 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002003 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002004 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002005 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002006 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002007 obj: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002008 braced_content: braces!(tuple!(
2009 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002010 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07002011 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05002012 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002013 attrs: {
2014 let mut attrs = outer_attrs;
2015 attrs.extend((braced_content.1).0);
2016 attrs
2017 },
David Tolnay8875fca2017-12-31 13:52:37 -05002018 expr: Box::new(obj),
2019 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07002020 brace_token: braced_content.0,
2021 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002022 })
2023 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002024
2025 fn description() -> Option<&'static str> {
2026 Some("`match` expression")
2027 }
Alex Crichton954046c2017-05-30 21:49:42 -07002028 }
David Tolnay1978c672016-10-27 22:05:52 -07002029
Michael Layzell734adb42017-06-07 16:58:31 -04002030 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002031 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002032 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002033 do_: keyword!(do) >>
2034 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002035 catch_block: syn!(Block) >>
2036 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05002037 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002038 block: catch_block,
2039 do_token: do_,
2040 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002041 })
Michael Layzell92639a52017-06-01 00:07:44 -04002042 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002043
2044 fn description() -> Option<&'static str> {
2045 Some("`catch` expression")
2046 }
Alex Crichton954046c2017-05-30 21:49:42 -07002047 }
Arnavion02ef13f2017-04-25 00:54:31 -07002048
Michael Layzell734adb42017-06-07 16:58:31 -04002049 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002050 impl Synom for ExprYield {
2051 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002052 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002053 expr: option!(syn!(Expr)) >>
2054 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002055 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002056 yield_token: yield_,
2057 expr: expr.map(Box::new),
2058 })
2059 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002060
2061 fn description() -> Option<&'static str> {
2062 Some("`yield` expression")
2063 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002064 }
2065
2066 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002067 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002068 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002069 attrs: many0!(Attribute::parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002070 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002071 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002072 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002073 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002074 body: do_parse!(
2075 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002076 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2077 true => alt!(
2078 input_end!() => { |_| None }
2079 |
2080 punct!(,) => { Some }
2081 )
Alex Crichton03b30272017-08-28 09:35:24 -07002082 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002083 false => option!(punct!(,))
2084 ) >>
2085 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002086 ) >>
2087 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002088 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002089 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002090 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002091 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002092 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002093 body: Box::new(body.0),
2094 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002095 })
2096 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002097
2098 fn description() -> Option<&'static str> {
2099 Some("`match` arm")
2100 }
Alex Crichton954046c2017-05-30 21:49:42 -07002101 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002102
Michael Layzell734adb42017-06-07 16:58:31 -04002103 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002104 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnay713a6722018-07-21 15:49:40 -07002105 attrs: many0!(Attribute::parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002106 asyncness: option!(keyword!(async)) >>
2107 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002108 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002109 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002110 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002111 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002112 ret_and_body: alt!(
2113 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002114 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002115 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002116 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002117 (
2118 ReturnType::Type(arrow, Box::new(ty)),
2119 Expr::Block(ExprBlock {
2120 attrs: Vec::new(),
2121 block: body,
2122 },
2123 ))
David Tolnay89e05672016-10-02 14:39:42 -07002124 )
2125 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002126 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002127 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002128 (Expr::Closure(ExprClosure {
2129 attrs: attrs,
2130 asyncness: asyncness,
2131 movability: movability,
2132 capture: capture,
2133 or1_token: or1,
2134 inputs: inputs,
2135 or2_token: or2,
2136 output: ret_and_body.0,
2137 body: Box::new(ret_and_body.1),
2138 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002139 ));
2140
2141 #[cfg(feature = "full")]
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002142 named!(unstable_async_block -> ExprVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07002143 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07002144 many0!(Attribute::parse_outer) >>
2145 keyword!(async) >>
2146 option!(keyword!(move)) >>
2147 syn!(Block) >>
David Tolnay9be32582018-07-31 22:37:26 -07002148 end: call!(verbatim::grab_cursor) >>
2149 (ExprVerbatim {
2150 tts: verbatim::token_range(begin..end),
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002151 })
2152 ));
2153
2154 #[cfg(feature = "full")]
David Tolnayf7177052018-08-24 15:31:50 -04002155 named!(unstable_try_block -> ExprVerbatim, do_parse!(
2156 begin: call!(verbatim::grab_cursor) >>
2157 many0!(Attribute::parse_outer) >>
2158 keyword!(try) >>
2159 syn!(Block) >>
2160 end: call!(verbatim::grab_cursor) >>
2161 (ExprVerbatim {
2162 tts: verbatim::token_range(begin..end),
2163 })
2164 ));
2165
2166 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002167 named!(fn_arg -> FnArg, do_parse!(
2168 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002169 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002170 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002171 if let Some((colon, ty)) = ty {
2172 FnArg::Captured(ArgCaptured {
2173 pat: pat,
2174 colon_token: colon,
2175 ty: ty,
2176 })
2177 } else {
2178 FnArg::Inferred(pat)
2179 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002180 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002181 ));
2182
Michael Layzell734adb42017-06-07 16:58:31 -04002183 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002184 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002185 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002186 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002187 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002188 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002189 cond: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002190 block: braces!(tuple!(
2191 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002192 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002193 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002194 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002195 attrs: {
2196 let mut attrs = outer_attrs;
2197 attrs.extend((block.1).0);
2198 attrs
2199 },
2200 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002201 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002202 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002203 body: Block {
2204 brace_token: block.0,
2205 stmts: (block.1).1,
2206 },
Michael Layzell92639a52017-06-01 00:07:44 -04002207 })
2208 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002209
2210 fn description() -> Option<&'static str> {
2211 Some("`while` expression")
2212 }
Alex Crichton954046c2017-05-30 21:49:42 -07002213 }
2214
Michael Layzell734adb42017-06-07 16:58:31 -04002215 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002216 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002217 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002218 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002219 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002220 while_: keyword!(while) >>
2221 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002222 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002223 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002224 value: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002225 block: braces!(tuple!(
2226 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002227 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002228 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002229 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002230 attrs: {
2231 let mut attrs = outer_attrs;
2232 attrs.extend((block.1).0);
2233 attrs
2234 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002235 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002236 while_token: while_,
2237 let_token: let_,
2238 pats: pats,
2239 eq_token: eq,
2240 expr: Box::new(value),
2241 body: Block {
2242 brace_token: block.0,
2243 stmts: (block.1).1,
2244 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002245 })
2246 ));
David Tolnay79777332018-01-07 10:04:42 -08002247
2248 fn description() -> Option<&'static str> {
2249 Some("`while let` expression")
2250 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002251 }
2252
2253 #[cfg(feature = "full")]
2254 impl Synom for Label {
2255 named!(parse -> Self, do_parse!(
2256 name: syn!(Lifetime) >>
2257 colon: punct!(:) >>
2258 (Label {
2259 name: name,
2260 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002261 })
2262 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002263
2264 fn description() -> Option<&'static str> {
2265 Some("`while let` expression")
2266 }
Alex Crichton954046c2017-05-30 21:49:42 -07002267 }
2268
Michael Layzell734adb42017-06-07 16:58:31 -04002269 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002270 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002271 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002272 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002273 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002274 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002275 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002276 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002277 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002278 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002279 })
2280 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002281
2282 fn description() -> Option<&'static str> {
2283 Some("`continue`")
2284 }
Alex Crichton954046c2017-05-30 21:49:42 -07002285 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002286
Michael Layzell734adb42017-06-07 16:58:31 -04002287 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002288 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002289 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002290 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002291 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002292 // We can't allow blocks after a `break` expression when we wouldn't
2293 // allow structs, as this expression is ambiguous.
2294 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002295 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002296 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002297 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002298 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002299 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002300 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002301 ));
2302
Michael Layzell734adb42017-06-07 16:58:31 -04002303 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002304 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002305 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002306 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002307 // NOTE: return is greedy and eats blocks after it even when in a
2308 // position where structs are not allowed, such as in if statement
2309 // conditions. For example:
2310 //
David Tolnaybcf26022017-12-25 22:10:52 -05002311 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002312 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002313 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002314 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002315 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002316 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002317 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002318 ));
2319
Michael Layzell734adb42017-06-07 16:58:31 -04002320 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002321 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002322 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002323 outer_attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002324 path: syn!(Path) >>
2325 data: braces!(do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002326 inner_attrs: many0!(Attribute::parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002327 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002328 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2329 dots: punct!(..) >>
2330 base: syn!(Expr) >>
2331 (dots, base)
2332 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002333 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002334 )) >>
2335 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002336 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002337 let (dots, rest) = match base.and_then(|b| b) {
2338 Some((dots, base)) => (Some(dots), Some(base)),
2339 None => (None, None),
2340 };
2341 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002342 attrs: {
2343 let mut attrs = outer_attrs;
2344 attrs.extend(inner_attrs);
2345 attrs
2346 },
Michael Layzell92639a52017-06-01 00:07:44 -04002347 brace_token: brace,
2348 path: path,
2349 fields: fields,
2350 dot2_token: dots,
2351 rest: rest.map(Box::new),
2352 }
2353 })
2354 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002355
2356 fn description() -> Option<&'static str> {
2357 Some("struct literal expression")
2358 }
Alex Crichton954046c2017-05-30 21:49:42 -07002359 }
2360
Michael Layzell734adb42017-06-07 16:58:31 -04002361 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002362 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002363 named!(parse -> Self, do_parse!(
2364 attrs: many0!(Attribute::parse_outer) >>
2365 field_value: alt!(
2366 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2367 |
2368 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002369 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002370 None,
2371 Expr::Path(ExprPath {
2372 attrs: Vec::new(),
2373 qself: None,
2374 path: name.into(),
2375 }),
2376 ))
2377 ) >>
2378 (FieldValue {
2379 attrs: attrs,
2380 member: field_value.0,
2381 colon_token: field_value.1,
2382 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002383 })
2384 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002385
2386 fn description() -> Option<&'static str> {
2387 Some("field-value pair: `field: value`")
2388 }
Alex Crichton954046c2017-05-30 21:49:42 -07002389 }
David Tolnay055a7042016-10-02 19:23:54 -07002390
Michael Layzell734adb42017-06-07 16:58:31 -04002391 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002392 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002393 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002394 outer_attrs: many0!(Attribute::parse_outer) >>
2395 data: brackets!(tuple!(
2396 many0!(Attribute::parse_inner),
2397 syn!(Expr),
2398 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002399 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002400 )) >>
2401 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002402 attrs: {
2403 let mut attrs = outer_attrs;
2404 attrs.extend((data.1).0);
2405 attrs
2406 },
2407 expr: Box::new((data.1).1),
2408 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002409 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002410 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002411 })
2412 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002413
2414 fn description() -> Option<&'static str> {
2415 Some("repeated array literal: `[val; N]`")
2416 }
Alex Crichton954046c2017-05-30 21:49:42 -07002417 }
David Tolnay055a7042016-10-02 19:23:54 -07002418
Michael Layzell734adb42017-06-07 16:58:31 -04002419 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002420 impl Synom for ExprUnsafe {
2421 named!(parse -> Self, do_parse!(
2422 unsafe_: keyword!(unsafe) >>
2423 b: syn!(Block) >>
2424 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002425 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002426 unsafe_token: unsafe_,
2427 block: b,
2428 })
2429 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002430
2431 fn description() -> Option<&'static str> {
2432 Some("unsafe block: `unsafe { .. }`")
2433 }
Nika Layzell640832a2017-12-04 13:37:09 -05002434 }
2435
2436 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002437 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002438 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002439 outer_attrs: many0!(Attribute::parse_outer) >>
2440 block: braces!(tuple!(
2441 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002442 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002443 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002444 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002445 attrs: {
2446 let mut attrs = outer_attrs;
2447 attrs.extend((block.1).0);
2448 attrs
2449 },
2450 block: Block {
2451 brace_token: block.0,
2452 stmts: (block.1).1,
2453 },
Michael Layzell92639a52017-06-01 00:07:44 -04002454 })
2455 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002456
2457 fn description() -> Option<&'static str> {
2458 Some("block: `{ .. }`")
2459 }
Alex Crichton954046c2017-05-30 21:49:42 -07002460 }
David Tolnay89e05672016-10-02 14:39:42 -07002461
Michael Layzell734adb42017-06-07 16:58:31 -04002462 #[cfg(feature = "full")]
David Tolnay5d08ae62018-08-01 00:08:48 -07002463 named!(unstable_labeled_block -> ExprVerbatim, do_parse!(
2464 begin: call!(verbatim::grab_cursor) >>
2465 many0!(Attribute::parse_outer) >>
David Tolnay61e15e52018-08-01 00:28:36 -07002466 syn!(Label) >>
David Tolnay5d08ae62018-08-01 00:08:48 -07002467 braces!(tuple!(
2468 many0!(Attribute::parse_inner),
2469 call!(Block::parse_within),
2470 )) >>
2471 end: call!(verbatim::grab_cursor) >>
2472 (ExprVerbatim {
2473 tts: verbatim::token_range(begin..end),
2474 })
2475 ));
2476
2477 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002478 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002479 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002480 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002481 (ExprRange {
2482 attrs: Vec::new(),
2483 from: None,
2484 to: hi.map(Box::new),
2485 limits: limits,
2486 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002487 ));
2488
Michael Layzell734adb42017-06-07 16:58:31 -04002489 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002490 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002491 named!(parse -> Self, alt!(
2492 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002493 punct!(..=) => { RangeLimits::Closed }
2494 |
2495 // Must come before Dot2
David Tolnay7ac699c2018-08-24 14:00:58 -04002496 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.spans)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002497 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002498 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002499 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002500
2501 fn description() -> Option<&'static str> {
2502 Some("range limit: `..`, `...` or `..=`")
2503 }
Alex Crichton954046c2017-05-30 21:49:42 -07002504 }
David Tolnay438c9052016-10-07 23:24:48 -07002505
Alex Crichton954046c2017-05-30 21:49:42 -07002506 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002507 #[cfg(not(feature = "full"))]
2508 named!(parse -> Self, do_parse!(
2509 pair: qpath >>
2510 (ExprPath {
2511 attrs: Vec::new(),
2512 qself: pair.0,
2513 path: pair.1,
2514 })
2515 ));
2516
2517 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002518 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07002519 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002520 pair: qpath >>
2521 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002522 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002523 qself: pair.0,
2524 path: pair.1,
2525 })
2526 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002527
2528 fn description() -> Option<&'static str> {
2529 Some("path: `a::b::c`")
2530 }
Alex Crichton954046c2017-05-30 21:49:42 -07002531 }
David Tolnay42602292016-10-01 22:25:45 -07002532
David Tolnay9cc2f092018-08-24 15:51:37 -04002533 named!(path -> Path, do_parse!(
2534 colon: option!(punct!(::)) >>
2535 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2536 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2537 (Path {
2538 leading_colon: colon,
2539 segments: segments,
2540 })
2541 ));
2542
2543 named!(path_segment -> PathSegment, alt!(
2544 do_parse!(
2545 ident: syn!(Ident) >>
2546 colon2: punct!(::) >>
2547 lt: punct!(<) >>
2548 args: call!(Punctuated::parse_terminated) >>
2549 gt: punct!(>) >>
2550 (PathSegment {
2551 ident: ident,
2552 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2553 colon2_token: Some(colon2),
2554 lt_token: lt,
2555 args: args,
2556 gt_token: gt,
2557 }),
2558 })
2559 )
2560 |
2561 mod_style_path_segment
2562 ));
2563
2564 named!(qpath -> (Option<QSelf>, Path), alt!(
2565 map!(path, |p| (None, p))
2566 |
2567 do_parse!(
2568 lt: punct!(<) >>
2569 this: syn!(Type) >>
2570 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2571 gt: punct!(>) >>
2572 colon2: punct!(::) >>
2573 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2574 ({
2575 let (pos, as_, path) = match path {
2576 Some((as_, mut path)) => {
2577 let pos = path.segments.len();
2578 path.segments.push_punct(colon2);
2579 path.segments.extend(rest.into_pairs());
2580 (pos, Some(as_), path)
2581 }
2582 None => {
2583 (0, None, Path {
2584 leading_colon: Some(colon2),
2585 segments: rest,
2586 })
2587 }
2588 };
2589 (Some(QSelf {
2590 lt_token: lt,
2591 ty: Box::new(this),
2592 position: pos,
2593 as_token: as_,
2594 gt_token: gt,
2595 }), path)
2596 })
2597 )
2598 |
2599 map!(keyword!(self), |s| (None, s.into()))
2600 ));
2601
David Tolnay85b69a42017-12-27 20:43:10 -05002602 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002603
David Tolnay8875fca2017-12-31 13:52:37 -05002604 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002605
Michael Layzell734adb42017-06-07 16:58:31 -04002606 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002607 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002608 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002609 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002610 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002611 brace_token: stmts.0,
2612 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002613 })
2614 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002615
2616 fn description() -> Option<&'static str> {
2617 Some("block: `{ .. }`")
2618 }
Alex Crichton954046c2017-05-30 21:49:42 -07002619 }
David Tolnay939766a2016-09-23 23:48:12 -07002620
Michael Layzell734adb42017-06-07 16:58:31 -04002621 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002622 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002623 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002624 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002625 mut standalone: many0!(do_parse!(
2626 stmt: syn!(Stmt) >>
2627 many0!(punct!(;)) >>
2628 (stmt)
2629 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002630 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002631 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002632 mut e: syn!(Expr) >>
2633 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002634 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002635 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002636 })
2637 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002638 (match last {
2639 None => standalone,
2640 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002641 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002642 standalone
2643 }
2644 })
2645 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002646 }
2647
Michael Layzell734adb42017-06-07 16:58:31 -04002648 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002649 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002650 named!(parse -> Self, alt!(
2651 stmt_mac
2652 |
2653 stmt_local
2654 |
2655 stmt_item
2656 |
Michael Layzell35418782017-06-07 09:20:25 -04002657 stmt_blockexpr
2658 |
Michael Layzell92639a52017-06-01 00:07:44 -04002659 stmt_expr
2660 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002661
2662 fn description() -> Option<&'static str> {
2663 Some("statement")
2664 }
Alex Crichton954046c2017-05-30 21:49:42 -07002665 }
David Tolnay939766a2016-09-23 23:48:12 -07002666
Michael Layzell734adb42017-06-07 16:58:31 -04002667 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002668 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002669 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002670 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002671 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002672 // Only parse braces here; paren and bracket will get parsed as
2673 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002674 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002675 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002676 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002677 attrs: attrs,
2678 ident: None,
2679 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002680 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002681 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002682 delimiter: MacroDelimiter::Brace(data.0),
2683 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002684 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002685 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002686 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002687 ));
2688
Michael Layzell734adb42017-06-07 16:58:31 -04002689 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002690 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002691 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002692 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002693 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002694 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002695 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2696 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002697 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002698 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002699 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002700 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002701 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2702 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2703 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002704 }))
David Tolnay191e0582016-10-02 18:31:09 -07002705 ));
2706
Michael Layzell734adb42017-06-07 16:58:31 -04002707 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002708 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002709
Michael Layzell734adb42017-06-07 16:58:31 -04002710 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002711 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002712 mut attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002713 mut e: expr_nosemi >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002714 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002715 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002716 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002717 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002718 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002719 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002720 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002721 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002722 }
2723 })
2724 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002725
Michael Layzell734adb42017-06-07 16:58:31 -04002726 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002727 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002728 mut attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002729 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002730 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002731 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002732 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002733 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002734 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002735 })
David Tolnay939766a2016-09-23 23:48:12 -07002736 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002737
Michael Layzell734adb42017-06-07 16:58:31 -04002738 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002739 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002740 named!(parse -> Self, alt!(
2741 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2742 |
2743 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2744 |
2745 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2746 |
2747 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2748 |
2749 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2750 |
David Tolnay323279a2017-12-29 11:26:32 -05002751 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002752 |
2753 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2754 |
2755 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2756 |
2757 syn!(PatPath) => { Pat::Path }
2758 |
2759 syn!(PatTuple) => { Pat::Tuple }
2760 |
2761 syn!(PatRef) => { Pat::Ref }
2762 |
2763 syn!(PatSlice) => { Pat::Slice }
2764 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002765
2766 fn description() -> Option<&'static str> {
2767 Some("pattern")
2768 }
Alex Crichton954046c2017-05-30 21:49:42 -07002769 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002770
Michael Layzell734adb42017-06-07 16:58:31 -04002771 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002772 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002773 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002774 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002775 |u| PatWild { underscore_token: u }
2776 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002777
2778 fn description() -> Option<&'static str> {
2779 Some("wild pattern: `_`")
2780 }
Alex Crichton954046c2017-05-30 21:49:42 -07002781 }
David Tolnay84aa0752016-10-02 23:01:13 -07002782
Michael Layzell734adb42017-06-07 16:58:31 -04002783 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002784 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002785 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002786 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002787 pat: syn!(Pat) >>
2788 (PatBox {
2789 pat: Box::new(pat),
2790 box_token: boxed,
2791 })
2792 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002793
2794 fn description() -> Option<&'static str> {
2795 Some("box pattern")
2796 }
Alex Crichton954046c2017-05-30 21:49:42 -07002797 }
2798
Michael Layzell734adb42017-06-07 16:58:31 -04002799 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002800 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002801 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002802 by_ref: option!(keyword!(ref)) >>
2803 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002804 name: alt!(
2805 syn!(Ident)
2806 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002807 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002808 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002809 not!(punct!(<)) >>
2810 not!(punct!(::)) >>
2811 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002812 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002813 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002814 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002815 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002816 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002817 })
2818 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002819
2820 fn description() -> Option<&'static str> {
2821 Some("pattern identifier binding")
2822 }
Alex Crichton954046c2017-05-30 21:49:42 -07002823 }
2824
Michael Layzell734adb42017-06-07 16:58:31 -04002825 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002826 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002827 named!(parse -> Self, do_parse!(
2828 path: syn!(Path) >>
2829 tuple: syn!(PatTuple) >>
2830 (PatTupleStruct {
2831 path: path,
2832 pat: tuple,
2833 })
2834 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002835
2836 fn description() -> Option<&'static str> {
2837 Some("tuple struct pattern")
2838 }
Alex Crichton954046c2017-05-30 21:49:42 -07002839 }
2840
Michael Layzell734adb42017-06-07 16:58:31 -04002841 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002842 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002843 named!(parse -> Self, do_parse!(
2844 path: syn!(Path) >>
2845 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002846 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002847 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002848 (fields, base)
2849 )) >>
2850 (PatStruct {
2851 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002852 fields: (data.1).0,
2853 brace_token: data.0,
2854 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002855 })
2856 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002857
2858 fn description() -> Option<&'static str> {
2859 Some("struct pattern")
2860 }
Alex Crichton954046c2017-05-30 21:49:42 -07002861 }
2862
Michael Layzell734adb42017-06-07 16:58:31 -04002863 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002864 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002865 named!(parse -> Self, alt!(
2866 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002867 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002868 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002869 pat: syn!(Pat) >>
2870 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002871 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002872 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002873 attrs: Vec::new(),
2874 colon_token: Some(colon),
2875 })
2876 )
2877 |
2878 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002879 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002880 by_ref: option!(keyword!(ref)) >>
2881 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002882 ident: syn!(Ident) >>
2883 ({
2884 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002885 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002886 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002887 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002888 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002889 }.into();
2890 if let Some(boxed) = boxed {
2891 pat = PatBox {
2892 pat: Box::new(pat),
2893 box_token: boxed,
2894 }.into();
2895 }
2896 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002897 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002898 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002899 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002900 colon_token: None,
2901 }
2902 })
2903 )
2904 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002905
2906 fn description() -> Option<&'static str> {
2907 Some("field pattern")
2908 }
Alex Crichton954046c2017-05-30 21:49:42 -07002909 }
2910
David Tolnay85b69a42017-12-27 20:43:10 -05002911 impl Synom for Member {
2912 named!(parse -> Self, alt!(
2913 syn!(Ident) => { Member::Named }
2914 |
2915 syn!(Index) => { Member::Unnamed }
2916 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002917
2918 fn description() -> Option<&'static str> {
2919 Some("field member")
2920 }
David Tolnay85b69a42017-12-27 20:43:10 -05002921 }
2922
David Tolnay85b69a42017-12-27 20:43:10 -05002923 impl Synom for Index {
2924 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002925 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002926 ({
David Tolnay360efd22018-01-04 23:35:26 -08002927 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002928 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002929 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002930 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002931 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002932 })
David Tolnay85b69a42017-12-27 20:43:10 -05002933 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002934
2935 fn description() -> Option<&'static str> {
2936 Some("field index")
2937 }
David Tolnay85b69a42017-12-27 20:43:10 -05002938 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002939
Michael Layzell734adb42017-06-07 16:58:31 -04002940 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002941 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002942 named!(parse -> Self, map!(
2943 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002944 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002945 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002946
2947 fn description() -> Option<&'static str> {
2948 Some("path pattern")
2949 }
Alex Crichton954046c2017-05-30 21:49:42 -07002950 }
David Tolnay9636c052016-10-02 17:11:17 -07002951
Michael Layzell734adb42017-06-07 16:58:31 -04002952 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002953 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002954 named!(parse -> Self, do_parse!(
2955 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002956 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002957 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2958 tuple!(punct!(..), option!(punct!(,)))
2959 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002960 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002961 Some((_, Some(_))) => true,
2962 _ => false,
2963 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002964 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002965 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002966 )) >>
2967 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002968 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002969 let (dotdot, trailing) = match dotdot {
2970 Some((a, b)) => (Some(a), Some(b)),
2971 None => (None, None),
2972 };
2973 PatTuple {
2974 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002975 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002976 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002977 comma_token: trailing.unwrap_or_default(),
2978 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002979 }
2980 })
2981 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002982
2983 fn description() -> Option<&'static str> {
2984 Some("tuple pattern")
2985 }
Alex Crichton954046c2017-05-30 21:49:42 -07002986 }
David Tolnayfbb73232016-10-03 01:00:06 -07002987
Michael Layzell734adb42017-06-07 16:58:31 -04002988 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002989 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002990 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002991 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002992 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002993 pat: syn!(Pat) >>
2994 (PatRef {
2995 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002996 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002997 and_token: and,
2998 })
2999 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003000
3001 fn description() -> Option<&'static str> {
3002 Some("reference pattern")
3003 }
Alex Crichton954046c2017-05-30 21:49:42 -07003004 }
David Tolnayffdb97f2016-10-03 01:28:33 -07003005
Michael Layzell734adb42017-06-07 16:58:31 -04003006 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003007 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04003008 named!(parse -> Self, do_parse!(
3009 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05003010 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04003011 return parse_error(); // these need to be parsed by pat_path
3012 } else {
3013 PatLit {
3014 expr: Box::new(lit),
3015 }
3016 })
3017 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003018
3019 fn description() -> Option<&'static str> {
3020 Some("literal pattern")
3021 }
Alex Crichton954046c2017-05-30 21:49:42 -07003022 }
David Tolnaye1310902016-10-29 23:40:00 -07003023
Michael Layzell734adb42017-06-07 16:58:31 -04003024 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003025 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04003026 named!(parse -> Self, do_parse!(
3027 lo: pat_lit_expr >>
3028 limits: syn!(RangeLimits) >>
3029 hi: pat_lit_expr >>
3030 (PatRange {
3031 lo: Box::new(lo),
3032 hi: Box::new(hi),
3033 limits: limits,
3034 })
3035 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003036
3037 fn description() -> Option<&'static str> {
3038 Some("range pattern")
3039 }
Alex Crichton954046c2017-05-30 21:49:42 -07003040 }
David Tolnaye1310902016-10-29 23:40:00 -07003041
Michael Layzell734adb42017-06-07 16:58:31 -04003042 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07003043 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003044 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07003045 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05003046 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07003047 |
David Tolnay8c91b882017-12-28 23:04:32 -05003048 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07003049 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05003050 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003051 Expr::Unary(ExprUnary {
3052 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003053 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05003054 expr: Box::new(v)
3055 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003056 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05003057 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003058 })
3059 ));
David Tolnay8b308c22016-10-03 01:24:10 -07003060
Michael Layzell734adb42017-06-07 16:58:31 -04003061 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003062 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04003063 named!(parse -> Self, map!(
3064 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05003065 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003066 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003067 dots: punct!(..) >>
3068 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003069 (dots, trailing)
3070 )) >>
3071 after: cond!(
3072 match middle {
3073 Some((_, ref trailing)) => trailing.is_some(),
3074 _ => false,
3075 },
David Tolnayf2cfd722017-12-31 18:02:51 -05003076 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04003077 ) >>
3078 (before, middle, after)
3079 )),
David Tolnay8875fca2017-12-31 13:52:37 -05003080 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05003081 let mut before: Punctuated<Pat, Token![,]> = before;
3082 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08003083 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04003084 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04003085 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04003086 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04003087 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04003088 }),
3089 bracket_token: brackets,
3090 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05003091 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04003092 None
David Tolnaydc03aec2017-12-30 01:54:18 -05003093 } else {
David Tolnay56080682018-01-06 14:01:52 -08003094 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04003095 }
3096 }),
3097 front: before,
3098 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07003099 }
Alex Crichton954046c2017-05-30 21:49:42 -07003100 }
Michael Layzell92639a52017-06-01 00:07:44 -04003101 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003102
3103 fn description() -> Option<&'static str> {
3104 Some("slice pattern")
3105 }
Alex Crichton954046c2017-05-30 21:49:42 -07003106 }
David Tolnay323279a2017-12-29 11:26:32 -05003107
3108 #[cfg(feature = "full")]
3109 impl Synom for PatMacro {
3110 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003111
3112 fn description() -> Option<&'static str> {
3113 Some("macro pattern")
3114 }
David Tolnay323279a2017-12-29 11:26:32 -05003115 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003116}
3117
David Tolnayf4bbbd92016-09-23 14:41:55 -07003118#[cfg(feature = "printing")]
3119mod printing {
3120 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003121 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003122 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003123 use proc_macro2::{Literal, TokenStream};
3124 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003125
David Tolnaybcf26022017-12-25 22:10:52 -05003126 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003127 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003128 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003129 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003130 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003131 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003132 e.to_tokens(tokens);
3133 });
3134 } else {
3135 e.to_tokens(tokens);
3136 }
3137 }
3138
David Tolnay8c91b882017-12-28 23:04:32 -05003139 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003140 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003141 tokens.append_all(attrs.outer());
3142 }
Michael Layzell734adb42017-06-07 16:58:31 -04003143
David Tolnayd997aef2018-07-21 18:42:31 -07003144 #[cfg(feature = "full")]
3145 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3146 tokens.append_all(attrs.inner());
3147 }
3148
David Tolnay8c91b882017-12-28 23:04:32 -05003149 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003150 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3151
3152 #[cfg(not(feature = "full"))]
3153 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003154
Michael Layzell734adb42017-06-07 16:58:31 -04003155 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003156 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003157 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003158 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003159 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003160 self.expr.to_tokens(tokens);
3161 }
3162 }
3163
Michael Layzell734adb42017-06-07 16:58:31 -04003164 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003165 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003166 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003167 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003168 self.place.to_tokens(tokens);
3169 self.arrow_token.to_tokens(tokens);
3170 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003171 }
3172 }
3173
Michael Layzell734adb42017-06-07 16:58:31 -04003174 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003175 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003176 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003177 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003178 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003179 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003180 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003181 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003182 }
3183 }
3184
3185 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003186 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003187 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003188 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003189 self.paren_token.surround(tokens, |tokens| {
3190 self.args.to_tokens(tokens);
3191 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003192 }
3193 }
3194
Michael Layzell734adb42017-06-07 16:58:31 -04003195 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003196 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003197 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003198 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003199 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003200 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003201 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003202 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003203 self.paren_token.surround(tokens, |tokens| {
3204 self.args.to_tokens(tokens);
3205 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003206 }
3207 }
3208
Michael Layzell734adb42017-06-07 16:58:31 -04003209 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003210 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003211 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003212 self.colon2_token.to_tokens(tokens);
3213 self.lt_token.to_tokens(tokens);
3214 self.args.to_tokens(tokens);
3215 self.gt_token.to_tokens(tokens);
3216 }
3217 }
3218
3219 #[cfg(feature = "full")]
3220 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003221 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003222 match *self {
3223 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3224 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3225 }
3226 }
3227 }
3228
3229 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003230 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003231 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003232 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003233 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003234 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003235 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003236 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003237 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003238 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003239 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003240 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003241 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003242 }
3243 }
3244
3245 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003246 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003247 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003248 self.left.to_tokens(tokens);
3249 self.op.to_tokens(tokens);
3250 self.right.to_tokens(tokens);
3251 }
3252 }
3253
3254 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003255 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003256 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003257 self.op.to_tokens(tokens);
3258 self.expr.to_tokens(tokens);
3259 }
3260 }
3261
David Tolnay8c91b882017-12-28 23:04:32 -05003262 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003263 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003264 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003265 self.lit.to_tokens(tokens);
3266 }
3267 }
3268
Alex Crichton62a0a592017-05-22 13:58:53 -07003269 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003270 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003271 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003272 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003273 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003274 self.ty.to_tokens(tokens);
3275 }
3276 }
3277
David Tolnay0cf94f22017-12-28 23:46:26 -05003278 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003279 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003280 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003281 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003282 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003283 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003284 self.ty.to_tokens(tokens);
3285 }
3286 }
3287
Michael Layzell734adb42017-06-07 16:58:31 -04003288 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003289 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003290 if let Some((ref else_token, ref else_)) = *else_ {
3291 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003292
3293 // If we are not one of the valid expressions to exist in an else
3294 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003295 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003296 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003297 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003298 }
3299 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003300 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003301 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003302 });
3303 }
3304 }
3305 }
3306 }
3307
3308 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003309 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003310 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003311 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003312 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003313 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003314 self.then_branch.to_tokens(tokens);
3315 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003316 }
3317 }
3318
Michael Layzell734adb42017-06-07 16:58:31 -04003319 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003320 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003321 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003322 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003323 self.if_token.to_tokens(tokens);
3324 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003325 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003326 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003327 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003328 self.then_branch.to_tokens(tokens);
3329 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003330 }
3331 }
3332
Michael Layzell734adb42017-06-07 16:58:31 -04003333 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003334 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003335 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003336 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003337 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003338 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003339 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003340 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003341 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003342 tokens.append_all(&self.body.stmts);
3343 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003344 }
3345 }
3346
Michael Layzell734adb42017-06-07 16:58:31 -04003347 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003348 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003349 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003350 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003351 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003352 self.while_token.to_tokens(tokens);
3353 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003354 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003355 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003356 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003357 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003358 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003359 tokens.append_all(&self.body.stmts);
3360 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003361 }
3362 }
3363
Michael Layzell734adb42017-06-07 16:58:31 -04003364 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003365 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003366 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003367 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003368 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003369 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003370 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003371 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003372 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003373 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003374 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003375 tokens.append_all(&self.body.stmts);
3376 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003377 }
3378 }
3379
Michael Layzell734adb42017-06-07 16:58:31 -04003380 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003381 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003382 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003383 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003384 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003385 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003386 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003387 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003388 tokens.append_all(&self.body.stmts);
3389 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003390 }
3391 }
3392
Michael Layzell734adb42017-06-07 16:58:31 -04003393 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003394 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003395 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003396 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003397 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003398 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003399 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003400 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003401 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003402 arm.to_tokens(tokens);
3403 // Ensure that we have a comma after a non-block arm, except
3404 // for the last one.
3405 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003406 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003407 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003408 }
3409 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003410 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003411 }
3412 }
3413
Michael Layzell734adb42017-06-07 16:58:31 -04003414 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003415 impl ToTokens for ExprCatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003416 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003417 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003418 self.do_token.to_tokens(tokens);
3419 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003420 self.block.to_tokens(tokens);
3421 }
3422 }
3423
Michael Layzell734adb42017-06-07 16:58:31 -04003424 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003425 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003426 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003427 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003428 self.yield_token.to_tokens(tokens);
3429 self.expr.to_tokens(tokens);
3430 }
3431 }
3432
3433 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003434 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003435 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003436 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003437 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003438 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003439 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003440 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003441 for input in self.inputs.pairs() {
3442 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003443 FnArg::Captured(ArgCaptured {
3444 ref pat,
3445 ty: Type::Infer(_),
3446 ..
3447 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003448 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003449 }
David Tolnay56080682018-01-06 14:01:52 -08003450 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003451 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003452 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003453 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003454 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003455 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003456 self.body.to_tokens(tokens);
3457 }
3458 }
3459
Michael Layzell734adb42017-06-07 16:58:31 -04003460 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003461 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003462 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003463 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003464 self.unsafe_token.to_tokens(tokens);
3465 self.block.to_tokens(tokens);
3466 }
3467 }
3468
3469 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003470 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003471 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003472 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003473 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003474 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003475 tokens.append_all(&self.block.stmts);
3476 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003477 }
3478 }
3479
Michael Layzell734adb42017-06-07 16:58:31 -04003480 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003481 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003482 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003483 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003484 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003485 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003486 self.right.to_tokens(tokens);
3487 }
3488 }
3489
Michael Layzell734adb42017-06-07 16:58:31 -04003490 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003491 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003492 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003493 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003494 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003495 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003496 self.right.to_tokens(tokens);
3497 }
3498 }
3499
3500 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003501 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003502 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003503 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003504 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003505 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003506 }
3507 }
3508
David Tolnay85b69a42017-12-27 20:43:10 -05003509 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003510 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003511 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003512 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003513 Member::Unnamed(ref index) => index.to_tokens(tokens),
3514 }
3515 }
3516 }
3517
David Tolnay85b69a42017-12-27 20:43:10 -05003518 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003519 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003520 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3521 lit.set_span(self.span);
3522 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003523 }
3524 }
3525
3526 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003527 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003528 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003529 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003530 self.bracket_token.surround(tokens, |tokens| {
3531 self.index.to_tokens(tokens);
3532 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003533 }
3534 }
3535
Michael Layzell734adb42017-06-07 16:58:31 -04003536 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003537 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003538 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003539 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003540 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003541 match self.limits {
3542 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3543 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3544 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003545 self.to.to_tokens(tokens);
3546 }
3547 }
3548
3549 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003550 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003551 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003552 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003553 }
3554 }
3555
Michael Layzell734adb42017-06-07 16:58:31 -04003556 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003557 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003558 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003559 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003560 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003561 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003562 self.expr.to_tokens(tokens);
3563 }
3564 }
3565
Michael Layzell734adb42017-06-07 16:58:31 -04003566 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003567 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003568 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003569 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003570 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003571 self.label.to_tokens(tokens);
3572 self.expr.to_tokens(tokens);
3573 }
3574 }
3575
Michael Layzell734adb42017-06-07 16:58:31 -04003576 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003577 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003578 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003579 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003580 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003581 self.label.to_tokens(tokens);
3582 }
3583 }
3584
Michael Layzell734adb42017-06-07 16:58:31 -04003585 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003586 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003587 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003588 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003589 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003590 self.expr.to_tokens(tokens);
3591 }
3592 }
3593
Michael Layzell734adb42017-06-07 16:58:31 -04003594 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003595 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003596 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003597 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003598 self.mac.to_tokens(tokens);
3599 }
3600 }
3601
3602 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003603 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003604 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003605 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003606 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003607 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003608 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003609 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003610 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003611 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003612 self.rest.to_tokens(tokens);
3613 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003614 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003615 }
3616 }
3617
Michael Layzell734adb42017-06-07 16:58:31 -04003618 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003619 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003620 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003621 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003622 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003623 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003624 self.expr.to_tokens(tokens);
3625 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003626 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003627 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003628 }
3629 }
3630
David Tolnaye98775f2017-12-28 23:17:00 -05003631 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003632 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003633 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003634 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003635 self.group_token.surround(tokens, |tokens| {
3636 self.expr.to_tokens(tokens);
3637 });
3638 }
3639 }
3640
Alex Crichton62a0a592017-05-22 13:58:53 -07003641 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003642 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003643 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003644 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003645 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003646 self.expr.to_tokens(tokens);
3647 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003648 }
3649 }
3650
Michael Layzell734adb42017-06-07 16:58:31 -04003651 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003652 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003653 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003654 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003655 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003656 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003657 }
3658 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003659
David Tolnay2ae520a2017-12-29 11:19:50 -05003660 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003661 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003662 self.tts.to_tokens(tokens);
3663 }
3664 }
3665
Michael Layzell734adb42017-06-07 16:58:31 -04003666 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003667 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003668 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003669 self.name.to_tokens(tokens);
3670 self.colon_token.to_tokens(tokens);
3671 }
3672 }
3673
3674 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003675 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003676 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003677 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003678 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003679 if let Some(ref colon_token) = self.colon_token {
3680 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003681 self.expr.to_tokens(tokens);
3682 }
David Tolnay055a7042016-10-02 19:23:54 -07003683 }
3684 }
3685
Michael Layzell734adb42017-06-07 16:58:31 -04003686 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003687 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003688 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003689 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003690 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003691 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003692 if let Some((ref if_token, ref guard)) = self.guard {
3693 if_token.to_tokens(tokens);
3694 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003695 }
David Tolnaydfb91432018-03-31 19:19:44 +02003696 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003697 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003698 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003699 }
3700 }
3701
Michael Layzell734adb42017-06-07 16:58:31 -04003702 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003703 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003704 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003705 self.underscore_token.to_tokens(tokens);
3706 }
3707 }
3708
Michael Layzell734adb42017-06-07 16:58:31 -04003709 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003710 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003711 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003712 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003713 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003714 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003715 if let Some((ref at_token, ref subpat)) = self.subpat {
3716 at_token.to_tokens(tokens);
3717 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003718 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003719 }
3720 }
3721
Michael Layzell734adb42017-06-07 16:58:31 -04003722 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003723 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003724 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003725 self.path.to_tokens(tokens);
3726 self.brace_token.surround(tokens, |tokens| {
3727 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003728 // NOTE: We need a comma before the dot2 token if it is present.
3729 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003730 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003731 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003732 self.dot2_token.to_tokens(tokens);
3733 });
3734 }
3735 }
3736
Michael Layzell734adb42017-06-07 16:58:31 -04003737 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003738 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003739 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003740 self.path.to_tokens(tokens);
3741 self.pat.to_tokens(tokens);
3742 }
3743 }
3744
Michael Layzell734adb42017-06-07 16:58:31 -04003745 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003746 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003747 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003748 ::PathTokens(&self.qself, &self.path).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 PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003754 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003755 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003756 self.front.to_tokens(tokens);
3757 if let Some(ref dot2_token) = self.dot2_token {
3758 if !self.front.empty_or_trailing() {
3759 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003760 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003761 }
David Tolnay41871922017-12-29 01:53:45 -05003762 dot2_token.to_tokens(tokens);
3763 self.comma_token.to_tokens(tokens);
3764 if self.comma_token.is_none() && !self.back.is_empty() {
3765 // Ensure there is a comma after the .. token.
3766 <Token![,]>::default().to_tokens(tokens);
3767 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003768 }
David Tolnay41871922017-12-29 01:53:45 -05003769 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003770 });
3771 }
3772 }
3773
Michael Layzell734adb42017-06-07 16:58:31 -04003774 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003775 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003776 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003777 self.box_token.to_tokens(tokens);
3778 self.pat.to_tokens(tokens);
3779 }
3780 }
3781
Michael Layzell734adb42017-06-07 16:58:31 -04003782 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003783 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003784 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003785 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003786 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003787 self.pat.to_tokens(tokens);
3788 }
3789 }
3790
Michael Layzell734adb42017-06-07 16:58:31 -04003791 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003792 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003793 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003794 self.expr.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 PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003800 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003801 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003802 match self.limits {
3803 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003804 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003805 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003806 self.hi.to_tokens(tokens);
3807 }
3808 }
3809
Michael Layzell734adb42017-06-07 16:58:31 -04003810 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003811 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003812 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003813 // XXX: This is a mess, and it will be so easy to screw it up. How
3814 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003815 self.bracket_token.surround(tokens, |tokens| {
3816 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003817
3818 // If we need a comma before the middle or standalone .. token,
3819 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003820 if !self.front.empty_or_trailing()
3821 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003822 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003823 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003824 }
3825
3826 // If we have an identifier, we always need a .. token.
3827 if self.middle.is_some() {
3828 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003829 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003830 } else if self.dot2_token.is_some() {
3831 self.dot2_token.to_tokens(tokens);
3832 }
3833
3834 // Make sure we have a comma before the back half.
3835 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003836 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003837 self.back.to_tokens(tokens);
3838 } else {
3839 self.comma_token.to_tokens(tokens);
3840 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003841 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003842 }
3843 }
3844
Michael Layzell734adb42017-06-07 16:58:31 -04003845 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003846 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003847 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003848 self.mac.to_tokens(tokens);
3849 }
3850 }
3851
3852 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003853 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003854 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003855 self.tts.to_tokens(tokens);
3856 }
3857 }
3858
3859 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003860 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003861 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003862 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003863 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003864 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003865 }
3866 self.pat.to_tokens(tokens);
3867 }
3868 }
3869
Michael Layzell734adb42017-06-07 16:58:31 -04003870 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003871 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003872 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003873 self.brace_token.surround(tokens, |tokens| {
3874 tokens.append_all(&self.stmts);
3875 });
David Tolnay42602292016-10-01 22:25:45 -07003876 }
3877 }
3878
Michael Layzell734adb42017-06-07 16:58:31 -04003879 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003880 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003881 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003882 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003883 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003884 Stmt::Item(ref item) => item.to_tokens(tokens),
3885 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003886 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003887 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003888 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003889 }
David Tolnay42602292016-10-01 22:25:45 -07003890 }
3891 }
3892 }
David Tolnay191e0582016-10-02 18:31:09 -07003893
Michael Layzell734adb42017-06-07 16:58:31 -04003894 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003895 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003896 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003897 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003898 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003899 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003900 if let Some((ref colon_token, ref ty)) = self.ty {
3901 colon_token.to_tokens(tokens);
3902 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003903 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003904 if let Some((ref eq_token, ref init)) = self.init {
3905 eq_token.to_tokens(tokens);
3906 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003907 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003908 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003909 }
3910 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003911}