blob: 2be23fda2ce756e1371d20b45010b32ca74b195d [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnaye303b7c2018-05-20 16:46:35 -070010use proc_macro2::{Span, TokenStream};
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay14982012017-12-29 00:49:51 -050012#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -050013use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -050014#[cfg(feature = "full")]
15use std::mem;
David Tolnay94d2b792018-04-29 12:26:10 -070016#[cfg(feature = "extra-traits")]
17use tt::TokenStreamHelper;
David Tolnayf4bbbd92016-09-23 14:41:55 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -080020 /// A Rust expression.
David Tolnay614a0142018-01-07 10:25:43 -080021 ///
David Tolnay461d98e2018-01-07 11:07:19 -080022 /// *This type is available if Syn is built with the `"derive"` or `"full"`
23 /// feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enums
26 ///
27 /// This type is a syntax tree enum. In Syn this and other syntax tree enums
28 /// are designed to be traversed using the following rebinding idiom.
29 ///
30 /// ```
31 /// # use syn::Expr;
32 /// #
33 /// # fn example(expr: Expr) {
34 /// # const IGNORE: &str = stringify! {
35 /// let expr: Expr = /* ... */;
36 /// # };
37 /// match expr {
38 /// Expr::MethodCall(expr) => {
39 /// /* ... */
40 /// }
41 /// Expr::Cast(expr) => {
42 /// /* ... */
43 /// }
44 /// Expr::IfLet(expr) => {
45 /// /* ... */
46 /// }
47 /// /* ... */
48 /// # _ => {}
49 /// }
50 /// # }
51 /// ```
52 ///
53 /// We begin with a variable `expr` of type `Expr` that has no fields
54 /// (because it is an enum), and by matching on it and rebinding a variable
55 /// with the same name `expr` we effectively imbue our variable with all of
56 /// the data fields provided by the variant that it turned out to be. So for
57 /// example above if we ended up in the `MethodCall` case then we get to use
58 /// `expr.receiver`, `expr.args` etc; if we ended up in the `IfLet` case we
59 /// get to use `expr.pat`, `expr.then_branch`, `expr.else_branch`.
60 ///
61 /// The pattern is similar if the input expression is borrowed:
62 ///
63 /// ```
64 /// # use syn::Expr;
65 /// #
66 /// # fn example(expr: &Expr) {
67 /// match *expr {
68 /// Expr::MethodCall(ref expr) => {
69 /// # }
70 /// # _ => {}
71 /// # }
72 /// # }
73 /// ```
74 ///
75 /// This approach avoids repeating the variant names twice on every line.
76 ///
77 /// ```
78 /// # use syn::{Expr, ExprMethodCall};
79 /// #
80 /// # fn example(expr: Expr) {
81 /// # match expr {
82 /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive
83 /// # }
84 /// # _ => {}
85 /// # }
86 /// # }
87 /// ```
88 ///
89 /// In general, the name to which a syntax tree enum variant is bound should
90 /// be a suitable name for the complete syntax tree enum type.
91 ///
92 /// ```
93 /// # use syn::{Expr, ExprField};
94 /// #
95 /// # fn example(discriminant: &ExprField) {
96 /// // Binding is called `base` which is the name I would use if I were
97 /// // assigning `*discriminant.base` without an `if let`.
98 /// if let Expr::Tuple(ref base) = *discriminant.base {
99 /// # }
100 /// # }
101 /// ```
102 ///
103 /// A sign that you may not be choosing the right variable names is if you
104 /// see names getting repeated in your code, like accessing
105 /// `receiver.receiver` or `pat.pat` or `cond.cond`.
David Tolnay8c91b882017-12-28 23:04:32 -0500106 pub enum Expr {
David Tolnaya454c8f2018-01-07 01:01:10 -0800107 /// A box expression: `box f`.
David Tolnay461d98e2018-01-07 11:07:19 -0800108 ///
109 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400110 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500111 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500113 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500115
David Tolnaya454c8f2018-01-07 01:01:10 -0800116 /// A placement expression: `place <- value`.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -0500122 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 pub value: Box<Expr>,
124 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500125
David Tolnaya454c8f2018-01-07 01:01:10 -0800126 /// A slice literal expression: `[a, b, c, d]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400129 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500130 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500131 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500134
David Tolnaya454c8f2018-01-07 01:01:10 -0800135 /// A function call expression: `invoke(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -0500140 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700141 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500142 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500143 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500145
David Tolnaya454c8f2018-01-07 01:01:10 -0800146 /// A method call expression: `x.foo::<T>(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800147 ///
148 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -0500151 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -0500154 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500156 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500158
David Tolnaya454c8f2018-01-07 01:01:10 -0800159 /// A tuple expression: `(a, b, c, d)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800160 ///
161 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -0500162 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500163 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500164 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500165 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500167
David Tolnaya454c8f2018-01-07 01:01:10 -0800168 /// A binary operation: `a + b`, `a * b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800169 ///
170 /// *This type is available if Syn is built with the `"derive"` or
171 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500173 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500175 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 pub right: Box<Expr>,
177 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500178
David Tolnaya454c8f2018-01-07 01:01:10 -0800179 /// A unary operation: `!x`, `*x`.
David Tolnay461d98e2018-01-07 11:07:19 -0800180 ///
181 /// *This type is available if Syn is built with the `"derive"` or
182 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 pub op: UnOp,
186 pub expr: Box<Expr>,
187 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500188
David Tolnaya454c8f2018-01-07 01:01:10 -0800189 /// A literal in place of an expression: `1`, `"foo"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800190 ///
191 /// *This type is available if Syn is built with the `"derive"` or
192 /// `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500193 pub Lit(ExprLit {
194 pub attrs: Vec<Attribute>,
195 pub lit: Lit,
196 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500197
David Tolnaya454c8f2018-01-07 01:01:10 -0800198 /// A cast expression: `foo as f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800199 ///
200 /// *This type is available if Syn is built with the `"derive"` or
201 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -0500203 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500208
David Tolnaya454c8f2018-01-07 01:01:10 -0800209 /// A type ascription expression: `foo: f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800210 ///
211 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay0cf94f22017-12-28 23:46:26 -0500212 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500213 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700214 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800215 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800216 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500218
David Tolnaya454c8f2018-01-07 01:01:10 -0800219 /// An `if` expression with an optional `else` block: `if expr { ... }
220 /// else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800222 /// The `else` branch expression may only be an `If`, `IfLet`, or
223 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800224 ///
225 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500228 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500230 pub then_branch: Block,
231 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500233
David Tolnaya454c8f2018-01-07 01:01:10 -0800234 /// An `if let` expression with an optional `else` block: `if let pat =
235 /// expr { ... } else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800237 /// The `else` branch expression may only be an `If`, `IfLet`, or
238 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800243 pub if_token: Token![if],
244 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200245 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500248 pub then_branch: Block,
249 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500251
David Tolnaya454c8f2018-01-07 01:01:10 -0800252 /// A while loop: `while expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400255 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500256 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500257 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500259 pub cond: Box<Expr>,
260 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500262
David Tolnaya454c8f2018-01-07 01:01:10 -0800263 /// A while-let loop: `while let pat = expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800264 ///
265 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400266 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500267 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500268 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800269 pub while_token: Token![while],
270 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200271 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500273 pub expr: Box<Expr>,
274 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
David Tolnaya454c8f2018-01-07 01:01:10 -0800277 /// A for loop: `for pat in expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800278 ///
279 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400280 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500281 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500282 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500283 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500285 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 pub expr: Box<Expr>,
287 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500289
David Tolnaya454c8f2018-01-07 01:01:10 -0800290 /// Conditionless loop: `loop { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800291 ///
292 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400293 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500295 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub loop_token: Token![loop],
297 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500299
David Tolnaya454c8f2018-01-07 01:01:10 -0800300 /// A `match` expression: `match n { Some(n) => {}, None => {} }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800301 ///
302 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400303 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500304 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800305 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500307 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 pub arms: Vec<Arm>,
309 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500310
David Tolnaya454c8f2018-01-07 01:01:10 -0800311 /// A closure expression: `|a, b| a + b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800312 ///
313 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400314 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500315 pub attrs: Vec<Attribute>,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +0900316 pub asyncness: Option<Token![async]>,
David Tolnay13d4c0e2018-03-31 20:53:59 +0200317 pub movability: Option<Token![static]>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500318 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500320 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800321 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500322 pub output: ReturnType,
323 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500325
David Tolnaya454c8f2018-01-07 01:01:10 -0800326 /// An unsafe block: `unsafe { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800327 ///
328 /// *This type is available if Syn is built with the `"full"` feature.*
Nika Layzell640832a2017-12-04 13:37:09 -0500329 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500330 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500331 pub unsafe_token: Token![unsafe],
332 pub block: Block,
333 }),
334
David Tolnaya454c8f2018-01-07 01:01:10 -0800335 /// A blocked scope: `{ ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800336 ///
337 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400338 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500339 pub attrs: Vec<Attribute>,
David Tolnay1d8e9962018-08-24 19:04:20 -0400340 pub label: Option<Label>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 pub block: Block,
342 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700343
David Tolnaya454c8f2018-01-07 01:01:10 -0800344 /// An assignment expression: `a = compute()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800345 ///
346 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400347 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500348 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800350 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500351 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500353
David Tolnaya454c8f2018-01-07 01:01:10 -0800354 /// A compound assignment expression: `counter += 1`.
David Tolnay461d98e2018-01-07 11:07:19 -0800355 ///
356 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400357 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500358 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500360 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 pub right: Box<Expr>,
362 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500363
David Tolnaya454c8f2018-01-07 01:01:10 -0800364 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500365 /// field (`obj.0`).
David Tolnay461d98e2018-01-07 11:07:19 -0800366 ///
367 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd5147742018-06-30 10:09:52 -0700368 pub Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -0500369 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500370 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800371 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500372 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500374
David Tolnay05658502018-01-07 09:56:37 -0800375 /// A square bracketed indexing expression: `vector[2]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800376 ///
377 /// *This type is available if Syn is built with the `"derive"` or
378 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500380 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500382 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500383 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700384 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500385
David Tolnaya454c8f2018-01-07 01:01:10 -0800386 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800387 ///
388 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400389 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500390 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500393 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700394 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700395
David Tolnaya454c8f2018-01-07 01:01:10 -0800396 /// A path like `std::mem::replace` possibly containing generic
397 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800399 /// A plain identifier like `x` is a path of length 1.
David Tolnay461d98e2018-01-07 11:07:19 -0800400 ///
401 /// *This type is available if Syn is built with the `"derive"` or
402 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500404 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 pub qself: Option<QSelf>,
406 pub path: Path,
407 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700408
David Tolnaya454c8f2018-01-07 01:01:10 -0800409 /// A referencing operation: `&a` or `&mut a`.
David Tolnay461d98e2018-01-07 11:07:19 -0800410 ///
411 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay00674ba2018-03-31 18:14:11 +0200412 pub Reference(ExprReference #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500413 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800414 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500415 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700416 pub expr: Box<Expr>,
417 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500418
David Tolnaya454c8f2018-01-07 01:01:10 -0800419 /// A `break`, with an optional label to break and an optional
420 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800421 ///
422 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400423 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500424 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500425 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700426 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 pub expr: Option<Box<Expr>>,
428 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500429
David Tolnaya454c8f2018-01-07 01:01:10 -0800430 /// A `continue`, with an optional label.
David Tolnay461d98e2018-01-07 11:07:19 -0800431 ///
432 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400433 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500434 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800435 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500436 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700437 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500438
David Tolnaya454c8f2018-01-07 01:01:10 -0800439 /// A `return`, with an optional value to be returned.
David Tolnay461d98e2018-01-07 11:07:19 -0800440 ///
441 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayc246cd32017-12-28 23:14:32 -0500442 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500443 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800444 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500445 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700446 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700447
David Tolnaya454c8f2018-01-07 01:01:10 -0800448 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800449 ///
450 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500451 pub Macro(ExprMacro #full {
452 pub attrs: Vec<Attribute>,
453 pub mac: Macro,
454 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700455
David Tolnaya454c8f2018-01-07 01:01:10 -0800456 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700457 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800458 /// The `rest` provides the value of the remaining fields as in `S { a:
459 /// 1, b: 1, ..rest }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800460 ///
461 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400462 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500463 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500465 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500466 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500467 pub dot2_token: Option<Token![..]>,
468 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700469 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700470
David Tolnaya454c8f2018-01-07 01:01:10 -0800471 /// An array literal constructed from one repeated element: `[0u8; N]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800472 ///
473 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400474 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500475 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500476 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700477 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500478 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800479 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700480 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700481
David Tolnaya454c8f2018-01-07 01:01:10 -0800482 /// A parenthesized expression: `(a + b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800483 ///
484 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9374bc02018-01-27 18:49:36 -0800485 pub Paren(ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -0500486 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500487 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500488 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700489 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700490
David Tolnaya454c8f2018-01-07 01:01:10 -0800491 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400492 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800493 /// This variant is important for faithfully representing the precedence
494 /// of expressions and is related to `None`-delimited spans in a
495 /// `TokenStream`.
David Tolnay461d98e2018-01-07 11:07:19 -0800496 ///
497 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye98775f2017-12-28 23:17:00 -0500498 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500499 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500500 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500501 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400502 }),
503
David Tolnaya454c8f2018-01-07 01:01:10 -0800504 /// A try-expression: `expr?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800505 ///
506 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400507 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500508 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800510 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700512
David Tolnay02a9c6f2018-08-24 18:58:45 -0400513 /// An async block: `async { ... }`.
514 ///
515 /// *This type is available if Syn is built with the `"full"` feature.*
516 pub Async(ExprAsync #full {
517 pub attrs: Vec<Attribute>,
518 pub async_token: Token![async],
519 pub capture: Option<Token![move]>,
520 pub block: Block,
521 }),
522
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400523 /// A try block: `try { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800524 ///
525 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400526 pub TryBlock(ExprTryBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500527 pub attrs: Vec<Attribute>,
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400528 pub try_token: Token![try],
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 pub block: Block,
530 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700531
David Tolnaya454c8f2018-01-07 01:01:10 -0800532 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800533 ///
534 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700535 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500536 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800537 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700538 pub expr: Option<Box<Expr>>,
539 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500540
David Tolnaya454c8f2018-01-07 01:01:10 -0800541 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800542 ///
543 /// *This type is available if Syn is built with the `"derive"` or
544 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500545 pub Verbatim(ExprVerbatim #manual_extra_traits {
546 pub tts: TokenStream,
547 }),
548 }
549}
550
551#[cfg(feature = "extra-traits")]
552impl Eq for ExprVerbatim {}
553
554#[cfg(feature = "extra-traits")]
555impl PartialEq for ExprVerbatim {
556 fn eq(&self, other: &Self) -> bool {
557 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
558 }
559}
560
561#[cfg(feature = "extra-traits")]
562impl Hash for ExprVerbatim {
563 fn hash<H>(&self, state: &mut H)
564 where
565 H: Hasher,
566 {
567 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700568 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700569}
570
David Tolnay8c91b882017-12-28 23:04:32 -0500571impl Expr {
572 // Not public API.
573 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500574 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500575 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500576 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800577 Expr::Box(ExprBox { ref mut attrs, .. })
578 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
579 | Expr::Array(ExprArray { ref mut attrs, .. })
580 | Expr::Call(ExprCall { ref mut attrs, .. })
581 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
582 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
583 | Expr::Binary(ExprBinary { ref mut attrs, .. })
584 | Expr::Unary(ExprUnary { ref mut attrs, .. })
585 | Expr::Lit(ExprLit { ref mut attrs, .. })
586 | Expr::Cast(ExprCast { ref mut attrs, .. })
587 | Expr::Type(ExprType { ref mut attrs, .. })
588 | Expr::If(ExprIf { ref mut attrs, .. })
589 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
590 | Expr::While(ExprWhile { ref mut attrs, .. })
591 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
592 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
593 | Expr::Loop(ExprLoop { ref mut attrs, .. })
594 | Expr::Match(ExprMatch { ref mut attrs, .. })
595 | Expr::Closure(ExprClosure { ref mut attrs, .. })
596 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
597 | Expr::Block(ExprBlock { ref mut attrs, .. })
598 | Expr::Assign(ExprAssign { ref mut attrs, .. })
599 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
600 | Expr::Field(ExprField { ref mut attrs, .. })
601 | Expr::Index(ExprIndex { ref mut attrs, .. })
602 | Expr::Range(ExprRange { ref mut attrs, .. })
603 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200604 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800605 | Expr::Break(ExprBreak { ref mut attrs, .. })
606 | Expr::Continue(ExprContinue { ref mut attrs, .. })
607 | Expr::Return(ExprReturn { ref mut attrs, .. })
608 | Expr::Macro(ExprMacro { ref mut attrs, .. })
609 | Expr::Struct(ExprStruct { ref mut attrs, .. })
610 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
611 | Expr::Paren(ExprParen { ref mut attrs, .. })
612 | Expr::Group(ExprGroup { ref mut attrs, .. })
613 | Expr::Try(ExprTry { ref mut attrs, .. })
David Tolnay02a9c6f2018-08-24 18:58:45 -0400614 | Expr::Async(ExprAsync { ref mut attrs, .. })
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400615 | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800616 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500617 Expr::Verbatim(_) => {
618 // TODO
619 Vec::new()
620 }
David Tolnay8c91b882017-12-28 23:04:32 -0500621 }
622 }
623}
624
David Tolnay85b69a42017-12-27 20:43:10 -0500625ast_enum! {
626 /// A struct or tuple struct field accessed in a struct literal or field
627 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800628 ///
629 /// *This type is available if Syn is built with the `"derive"` or `"full"`
630 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500631 pub enum Member {
632 /// A named field like `self.x`.
633 Named(Ident),
634 /// An unnamed field like `self.0`.
635 Unnamed(Index),
636 }
637}
638
David Tolnay85b69a42017-12-27 20:43:10 -0500639ast_struct! {
640 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800641 ///
642 /// *This type is available if Syn is built with the `"derive"` or `"full"`
643 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500644 pub struct Index #manual_extra_traits {
645 pub index: u32,
646 pub span: Span,
647 }
648}
649
David Tolnay14982012017-12-29 00:49:51 -0500650impl From<usize> for Index {
651 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700652 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500653 Index {
654 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700655 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500656 }
657 }
658}
659
660#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500661impl Eq for Index {}
662
David Tolnay14982012017-12-29 00:49:51 -0500663#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500664impl PartialEq for Index {
665 fn eq(&self, other: &Self) -> bool {
666 self.index == other.index
667 }
668}
669
David Tolnay14982012017-12-29 00:49:51 -0500670#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500671impl Hash for Index {
672 fn hash<H: Hasher>(&self, state: &mut H) {
673 self.index.hash(state);
674 }
675}
676
677#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700678ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800679 /// The `::<>` explicit type parameters passed to a method call:
680 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800681 ///
682 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500683 pub struct MethodTurbofish {
684 pub colon2_token: Token![::],
685 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500686 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500687 pub gt_token: Token![>],
688 }
689}
690
691#[cfg(feature = "full")]
692ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800693 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800694 ///
695 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500696 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800697 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500698 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800699 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500700 ///
701 /// NOTE: Identity expressions are represented as Type arguments, as
702 /// they are indistinguishable syntactically.
703 Const(Expr),
704 }
705}
706
707#[cfg(feature = "full")]
708ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800710 ///
711 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500713 /// Attributes tagged on the field.
714 pub attrs: Vec<Attribute>,
715
716 /// Name or index of the field.
717 pub member: Member,
718
David Tolnay5d7098a2017-12-29 01:35:24 -0500719 /// The colon in `Struct { x: x }`. If written in shorthand like
720 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500721 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500722
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 /// Value of the field.
724 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 }
David Tolnay055a7042016-10-02 19:23:54 -0700726}
727
Michael Layzell734adb42017-06-07 16:58:31 -0400728#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700729ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800730 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800731 ///
732 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500733 pub struct Label {
734 pub name: Lifetime,
735 pub colon_token: Token![:],
736 }
737}
738
739#[cfg(feature = "full")]
740ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800741 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800742 ///
743 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500745 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 /// Statements in a block
747 pub stmts: Vec<Stmt>,
748 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700749}
750
Michael Layzell734adb42017-06-07 16:58:31 -0400751#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700752ast_enum! {
753 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800754 ///
755 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700756 pub enum Stmt {
757 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800758 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700759
Alex Crichton62a0a592017-05-22 13:58:53 -0700760 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800761 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700762
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800764 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700765
David Tolnaya454c8f2018-01-07 01:01:10 -0800766 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800767 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700768 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700769}
770
Michael Layzell734adb42017-06-07 16:58:31 -0400771#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700772ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800773 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800774 ///
775 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700776 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500777 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800778 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200779 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500780 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500781 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500782 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700783 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700784}
785
Michael Layzell734adb42017-06-07 16:58:31 -0400786#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700787ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800788 /// A pattern in a local binding, function signature, match expression, or
789 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800790 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800791 /// *This type is available if Syn is built with the `"full"` feature.*
792 ///
David Tolnay614a0142018-01-07 10:25:43 -0800793 /// # Syntax tree enum
794 ///
795 /// This type is a [syntax tree enum].
796 ///
797 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700798 // Clippy false positive
799 // https://github.com/Manishearth/rust-clippy/issues/1241
800 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
801 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800802 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800803 ///
804 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800806 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700807 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700808
David Tolnaya454c8f2018-01-07 01:01:10 -0800809 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800810 ///
811 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700812 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500813 pub by_ref: Option<Token![ref]>,
814 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700815 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500816 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700817 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700818
David Tolnaya454c8f2018-01-07 01:01:10 -0800819 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800820 ///
821 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700822 pub Struct(PatStruct {
823 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500824 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500825 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800826 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700828
David Tolnaya454c8f2018-01-07 01:01:10 -0800829 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800830 ///
831 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700832 pub TupleStruct(PatTupleStruct {
833 pub path: Path,
834 pub pat: PatTuple,
835 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700836
David Tolnaya454c8f2018-01-07 01:01:10 -0800837 /// A path pattern like `Color::Red`, optionally qualified with a
838 /// self-type.
839 ///
840 /// Unquailfied path patterns can legally refer to variants, structs,
841 /// constants or associated constants. Quailfied path patterns like
842 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
843 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800844 ///
845 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700846 pub Path(PatPath {
847 pub qself: Option<QSelf>,
848 pub path: Path,
849 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700850
David Tolnaya454c8f2018-01-07 01:01:10 -0800851 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800852 ///
853 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700854 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500855 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500856 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500857 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500858 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500859 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700860 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800861
862 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800863 ///
864 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700865 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800866 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500867 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700868 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800869
870 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800871 ///
872 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800874 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500875 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500876 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700877 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800878
879 /// A literal pattern: `0`.
880 ///
881 /// This holds an `Expr` rather than a `Lit` because negative numbers
882 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800883 ///
884 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700885 pub Lit(PatLit {
886 pub expr: Box<Expr>,
887 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800888
889 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800890 ///
891 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 pub Range(PatRange {
893 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700894 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500895 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800897
898 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800899 ///
900 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700901 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500902 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500903 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700904 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500905 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500906 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500907 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700908 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800909
910 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800911 ///
912 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500913 pub Macro(PatMacro {
914 pub mac: Macro,
915 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800916
917 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800918 ///
919 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500920 pub Verbatim(PatVerbatim #manual_extra_traits {
921 pub tts: TokenStream,
922 }),
923 }
924}
925
David Tolnayc43b44e2017-12-30 23:55:54 -0500926#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500927impl Eq for PatVerbatim {}
928
David Tolnayc43b44e2017-12-30 23:55:54 -0500929#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500930impl PartialEq for PatVerbatim {
931 fn eq(&self, other: &Self) -> bool {
932 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
933 }
934}
935
David Tolnayc43b44e2017-12-30 23:55:54 -0500936#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500937impl Hash for PatVerbatim {
938 fn hash<H>(&self, state: &mut H)
939 where
940 H: Hasher,
941 {
942 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700944}
945
Michael Layzell734adb42017-06-07 16:58:31 -0400946#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700947ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800948 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800950 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500952 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800953 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500954 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800956 /// 0...10 => {
957 /// return true;
958 /// }
959 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500960 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800962 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500963 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800965 ///
966 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700967 pub struct Arm {
968 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200969 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500970 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500971 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200972 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700973 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700976}
977
Michael Layzell734adb42017-06-07 16:58:31 -0400978#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700979ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800980 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800981 ///
982 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700983 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800985 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800986 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800987 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800988 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700989 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700990}
991
Michael Layzell734adb42017-06-07 16:58:31 -0400992#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700993ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800994 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700995 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800996 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
997 /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token.
David Tolnay461d98e2018-01-07 11:07:19 -0800998 ///
999 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -05001001 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -05001002 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -05001003 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001004 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001005 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001006}
1007
Michael Layzell3936ceb2017-07-08 00:28:36 -04001008#[cfg(any(feature = "parsing", feature = "printing"))]
1009#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -07001010fn arm_expr_requires_comma(expr: &Expr) -> bool {
1011 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1012 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001013 match *expr {
1014 Expr::Unsafe(..)
1015 | Expr::Block(..)
1016 | Expr::If(..)
1017 | Expr::IfLet(..)
1018 | Expr::Match(..)
1019 | Expr::While(..)
1020 | Expr::WhileLet(..)
1021 | Expr::Loop(..)
1022 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001023 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001024 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001025 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001026 }
1027}
1028
David Tolnayb9c8e322016-09-23 20:48:37 -07001029#[cfg(feature = "parsing")]
1030pub mod parsing {
1031 use super::*;
David Tolnaya7d69fc2018-08-26 13:30:24 -04001032 use path::parsing::old_mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001033 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001034 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001035
David Tolnaydfc886b2018-01-06 08:03:09 -08001036 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001037 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001038 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001039 #[cfg(feature = "full")]
1040 use proc_macro2::TokenStream;
David Tolnay203557a2017-12-27 23:59:33 -05001041 use synom::PResult;
David Tolnay94d2b792018-04-29 12:26:10 -07001042 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001043
David Tolnaybcf26022017-12-25 22:10:52 -05001044 // When we're parsing expressions which occur before blocks, like in an if
1045 // statement's condition, we cannot parse a struct literal.
1046 //
1047 // Struct literals are ambiguous in certain positions
1048 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001049 macro_rules! ambiguous_expr {
1050 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001051 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001052 };
1053 }
1054
David Tolnaybcf26022017-12-25 22:10:52 -05001055 // When we are parsing an optional suffix expression, we cannot allow blocks
1056 // if structs are not allowed.
1057 //
1058 // Example:
1059 //
1060 // if break {} {}
1061 //
1062 // is ambiguous between:
1063 //
1064 // if (break {}) {}
1065 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001066 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001067 macro_rules! opt_ambiguous_expr {
1068 ($i:expr, $allow_struct:ident) => {
1069 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1070 };
1071 }
1072
Alex Crichton954046c2017-05-30 21:49:42 -07001073 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001074 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001075
1076 fn description() -> Option<&'static str> {
1077 Some("expression")
1078 }
1079 }
1080
Michael Layzell734adb42017-06-07 16:58:31 -04001081 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001082 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1083
David Tolnaybcf26022017-12-25 22:10:52 -05001084 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001085 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001086 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001087 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001088 }
1089
Michael Layzell734adb42017-06-07 16:58:31 -04001090 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001091 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001092 // NOTE: We intentionally skip assign_expr, placement_expr, and
1093 // range_expr, as they are not parsed in non-full mode.
1094 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001095 }
1096
David Tolnaybcf26022017-12-25 22:10:52 -05001097 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001098 macro_rules! binop {
1099 (
1100 $name: ident,
1101 $next: ident,
1102 $submac: ident!( $($args:tt)* )
1103 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001104 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001105 mut e: call!($next, allow_struct, allow_block) >>
1106 many0!(do_parse!(
1107 op: $submac!($($args)*) >>
1108 rhs: call!($next, allow_struct, true) >>
1109 ({
1110 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001111 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001112 left: Box::new(e.into()),
1113 op: op,
1114 right: Box::new(rhs.into()),
1115 }.into();
1116 })
1117 )) >>
1118 (e)
1119 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001120 }
David Tolnay54e854d2016-10-24 12:03:30 -07001121 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001122
David Tolnaybcf26022017-12-25 22:10:52 -05001123 // <placement> = <placement> ..
1124 // <placement> += <placement> ..
1125 // <placement> -= <placement> ..
1126 // <placement> *= <placement> ..
1127 // <placement> /= <placement> ..
1128 // <placement> %= <placement> ..
1129 // <placement> ^= <placement> ..
1130 // <placement> &= <placement> ..
1131 // <placement> |= <placement> ..
1132 // <placement> <<= <placement> ..
1133 // <placement> >>= <placement> ..
1134 //
1135 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001136 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001137 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001138 mut e: call!(placement_expr, allow_struct, allow_block) >>
1139 alt!(
1140 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001141 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001142 // Recurse into self to parse right-associative operator.
1143 rhs: call!(assign_expr, allow_struct, true) >>
1144 ({
1145 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001146 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001147 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001148 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001149 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001150 }.into();
1151 })
1152 )
1153 |
1154 do_parse!(
David Tolnay2a54cfb2018-08-26 18:54:19 -04001155 op: shim!(BinOp::parse_assign_op) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001156 // Recurse into self to parse right-associative operator.
1157 rhs: call!(assign_expr, allow_struct, true) >>
1158 ({
1159 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001160 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001161 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001162 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001163 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001164 }.into();
1165 })
1166 )
1167 |
1168 epsilon!()
1169 ) >>
1170 (e)
1171 ));
1172
David Tolnaybcf26022017-12-25 22:10:52 -05001173 // <range> <- <range> ..
1174 //
1175 // NOTE: The `in place { expr }` version of this syntax is parsed in
1176 // `atom_expr`, not here.
1177 //
1178 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001179 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001180 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 mut e: call!(range_expr, allow_struct, allow_block) >>
1182 alt!(
1183 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001184 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001185 // Recurse into self to parse right-associative operator.
1186 rhs: call!(placement_expr, allow_struct, true) >>
1187 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001189 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001190 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001191 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001192 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001193 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001194 }.into();
1195 })
1196 )
1197 |
1198 epsilon!()
1199 ) >>
1200 (e)
1201 ));
1202
David Tolnaybcf26022017-12-25 22:10:52 -05001203 // <or> ... <or> ..
1204 // <or> .. <or> ..
1205 // <or> ..
1206 //
1207 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1208 // rules are for parsing these expressions are, but this is not correct.
1209 // For example, `a .. b .. c` is not a legal expression. It should not
1210 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1211 //
1212 // NOTE: The form of ranges which don't include a preceding expression are
1213 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001214 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001215 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001216 mut e: call!(or_expr, allow_struct, allow_block) >>
1217 many0!(do_parse!(
1218 limits: syn!(RangeLimits) >>
1219 // We don't want to allow blocks here if we don't allow structs. See
1220 // the reasoning for `opt_ambiguous_expr!` above.
1221 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1222 ({
1223 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001224 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001225 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001226 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001227 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001228 }.into();
1229 })
1230 )) >>
1231 (e)
1232 ));
1233
David Tolnaybcf26022017-12-25 22:10:52 -05001234 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001235 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001236
David Tolnaybcf26022017-12-25 22:10:52 -05001237 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001238 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001239
David Tolnaybcf26022017-12-25 22:10:52 -05001240 // <bitor> == <bitor> ...
1241 // <bitor> != <bitor> ...
1242 // <bitor> >= <bitor> ...
1243 // <bitor> <= <bitor> ...
1244 // <bitor> > <bitor> ...
1245 // <bitor> < <bitor> ...
1246 //
1247 // NOTE: This operator appears to be parsed as left-associative, but errors
1248 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001249 binop!(
1250 compare_expr,
1251 bitor_expr,
1252 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001253 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001254 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001255 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 |
1257 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001258 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 |
1260 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001261 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001262 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001263 do_parse!(
1264 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001265 not!(punct!(<-)) >>
1266 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001267 (BinOp::Lt(t))
1268 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001269 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001270 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001271 )
1272 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001273
David Tolnaybcf26022017-12-25 22:10:52 -05001274 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001275 binop!(
1276 bitor_expr,
1277 bitxor_expr,
1278 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1279 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001280
David Tolnaybcf26022017-12-25 22:10:52 -05001281 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001282 binop!(
1283 bitxor_expr,
1284 bitand_expr,
1285 do_parse!(
1286 // NOTE: Make sure we aren't looking at ^=.
1287 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1288 )
1289 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001290
David Tolnaybcf26022017-12-25 22:10:52 -05001291 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001292 binop!(
1293 bitand_expr,
1294 shift_expr,
1295 do_parse!(
1296 // NOTE: Make sure we aren't looking at && or &=.
1297 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1298 )
1299 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001300
David Tolnaybcf26022017-12-25 22:10:52 -05001301 // <arith> << <arith> ...
1302 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001303 binop!(
1304 shift_expr,
1305 arith_expr,
1306 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001307 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001308 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001309 punct!(>>) => { BinOp::Shr }
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 // <term> + <term> ...
1314 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001315 binop!(
1316 arith_expr,
1317 term_expr,
1318 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001319 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001321 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001322 )
1323 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001324
David Tolnaybcf26022017-12-25 22:10:52 -05001325 // <cast> * <cast> ...
1326 // <cast> / <cast> ...
1327 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001328 binop!(
1329 term_expr,
1330 cast_expr,
1331 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001332 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001333 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001334 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001335 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001336 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001337 )
1338 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001339
David Tolnaybcf26022017-12-25 22:10:52 -05001340 // <unary> as <ty>
1341 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001342 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001343 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001344 mut e: call!(unary_expr, allow_struct, allow_block) >>
1345 many0!(alt!(
1346 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001347 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001348 // We can't accept `A + B` in cast expressions, as it's
1349 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001350 ty: shim!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001351 ({
1352 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001353 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001354 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001355 as_token: as_,
1356 ty: Box::new(ty),
1357 }.into();
1358 })
1359 )
1360 |
1361 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001362 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001363 // We can't accept `A + B` in cast expressions, as it's
1364 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001365 ty: shim!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001366 ({
1367 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001368 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001369 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001370 colon_token: colon,
1371 ty: Box::new(ty),
1372 }.into();
1373 })
1374 )
1375 )) >>
1376 (e)
1377 ));
1378
David Tolnay0cf94f22017-12-28 23:46:26 -05001379 // <unary> as <ty>
1380 #[cfg(not(feature = "full"))]
1381 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1382 mut e: call!(unary_expr, allow_struct, allow_block) >>
1383 many0!(do_parse!(
1384 as_: keyword!(as) >>
1385 // We can't accept `A + B` in cast expressions, as it's
1386 // ambiguous with the + expression.
David Tolnaya7d69fc2018-08-26 13:30:24 -04001387 ty: shim!(Type::without_plus) >>
David Tolnay0cf94f22017-12-28 23:46:26 -05001388 ({
1389 e = ExprCast {
1390 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001391 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001392 as_token: as_,
1393 ty: Box::new(ty),
1394 }.into();
1395 })
1396 )) >>
1397 (e)
1398 ));
1399
David Tolnaybcf26022017-12-25 22:10:52 -05001400 // <UnOp> <trailer>
1401 // & <trailer>
1402 // &mut <trailer>
1403 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001404 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001405 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001406 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001407 attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001408 op: syn!(UnOp) >>
1409 expr: call!(unary_expr, allow_struct, true) >>
1410 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001411 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001412 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001413 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001414 }.into())
1415 )
1416 |
1417 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001418 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001419 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001420 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001421 expr: call!(unary_expr, allow_struct, true) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001422 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001423 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001424 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001425 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001426 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001427 }.into())
1428 )
1429 |
1430 do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001431 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001432 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001433 expr: call!(unary_expr, allow_struct, true) >>
1434 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001435 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001436 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001437 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001438 }.into())
1439 )
1440 |
1441 call!(trailer_expr, allow_struct, allow_block)
1442 ));
1443
Michael Layzell734adb42017-06-07 16:58:31 -04001444 // XXX: This duplication is ugly
1445 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001446 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001447 do_parse!(
1448 op: syn!(UnOp) >>
1449 expr: call!(unary_expr, allow_struct, true) >>
1450 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001451 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001452 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001453 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001454 }.into())
1455 )
1456 |
1457 call!(trailer_expr, allow_struct, allow_block)
1458 ));
1459
David Tolnayd997aef2018-07-21 18:42:31 -07001460 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001461 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1462 let mut outer = Vec::new();
1463 let mut inner = Vec::new();
1464 for attr in mem::replace(attrs, Vec::new()) {
1465 match attr.style {
1466 AttrStyle::Outer => outer.push(attr),
1467 AttrStyle::Inner(_) => inner.push(attr),
1468 }
1469 }
1470 *attrs = inner;
1471 outer
1472 }
1473
David Tolnaybcf26022017-12-25 22:10:52 -05001474 // <atom> (..<args>) ...
1475 // <atom> . <ident> (..<args>) ...
1476 // <atom> . <ident> ...
1477 // <atom> . <lit> ...
1478 // <atom> [ <expr> ] ...
1479 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001480 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001481 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001482 mut e: call!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001483 outer_attrs: value!({
1484 let mut attrs = e.replace_attrs(Vec::new());
1485 let outer_attrs = take_outer(&mut attrs);
1486 e.replace_attrs(attrs);
1487 outer_attrs
1488 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001489 many0!(alt!(
1490 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001491 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001492 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001493 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001494 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001495 args: args,
1496 paren_token: paren,
1497 }.into();
1498 })
1499 |
1500 tap!(more: and_method_call => {
1501 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001502 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001503 e = call.into();
1504 })
1505 |
1506 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001507 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001508 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001509 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001510 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001511 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001512 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001513 }.into();
1514 })
1515 |
1516 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001517 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001518 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001519 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001520 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001521 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001522 index: Box::new(i),
1523 }.into();
1524 })
1525 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001526 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001527 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001528 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001529 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001530 question_token: question,
1531 }.into();
1532 })
1533 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001534 ({
1535 let mut attrs = outer_attrs;
1536 attrs.extend(e.replace_attrs(Vec::new()));
1537 e.replace_attrs(attrs);
1538 e
1539 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001540 ));
1541
Michael Layzell734adb42017-06-07 16:58:31 -04001542 // XXX: Duplication == ugly
1543 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001544 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001545 mut e: call!(atom_expr, allow_struct, allow_block) >>
1546 many0!(alt!(
1547 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001548 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001549 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001550 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001551 paren_token: args.0,
1552 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001553 }.into();
1554 })
1555 |
David Tolnayd5147742018-06-30 10:09:52 -07001556 tap!(field: and_field => {
1557 let (token, member) = field;
1558 e = ExprField {
1559 attrs: Vec::new(),
1560 base: Box::new(e),
1561 dot_token: token,
1562 member: member,
1563 }.into();
1564 })
1565 |
Michael Layzell734adb42017-06-07 16:58:31 -04001566 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001567 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001568 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001569 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001570 bracket_token: i.0,
1571 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001572 }.into();
1573 })
1574 )) >>
1575 (e)
1576 ));
1577
David Tolnaya454c8f2018-01-07 01:01:10 -08001578 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001579 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001580 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001581 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1582 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001583 |
David Tolnay8c91b882017-12-28 23:04:32 -05001584 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001585 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001586 // must be before ExprStruct
David Tolnay02a9c6f2018-08-24 18:58:45 -04001587 syn!(ExprAsync) => { Expr::Async }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001588 |
David Tolnayf7177052018-08-24 15:31:50 -04001589 // must be before ExprStruct
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001590 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnayf7177052018-08-24 15:31:50 -04001591 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001592 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001593 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001594 |
David Tolnay8c91b882017-12-28 23:04:32 -05001595 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001596 |
David Tolnay8c91b882017-12-28 23:04:32 -05001597 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001598 |
1599 call!(expr_break, allow_struct) // must be before expr_path
1600 |
David Tolnay8c91b882017-12-28 23:04:32 -05001601 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001602 |
1603 call!(expr_ret, allow_struct) // must be before expr_path
1604 |
David Tolnay8c91b882017-12-28 23:04:32 -05001605 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001606 |
David Tolnay8c91b882017-12-28 23:04:32 -05001607 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001608 |
David Tolnay8c91b882017-12-28 23:04:32 -05001609 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001610 |
David Tolnay8c91b882017-12-28 23:04:32 -05001611 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001612 |
David Tolnay8c91b882017-12-28 23:04:32 -05001613 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001614 |
David Tolnay8c91b882017-12-28 23:04:32 -05001615 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001616 |
David Tolnay8c91b882017-12-28 23:04:32 -05001617 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001618 |
David Tolnay8c91b882017-12-28 23:04:32 -05001619 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001620 |
David Tolnay8c91b882017-12-28 23:04:32 -05001621 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001622 |
David Tolnay8c91b882017-12-28 23:04:32 -05001623 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001624 |
David Tolnay8c91b882017-12-28 23:04:32 -05001625 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001626 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001627 call!(expr_closure, allow_struct)
1628 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001629 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001630 |
1631 // NOTE: This is the prefix-form of range
1632 call!(expr_range, allow_struct)
1633 |
David Tolnay8c91b882017-12-28 23:04:32 -05001634 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001635 |
David Tolnay8c91b882017-12-28 23:04:32 -05001636 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001637 ));
1638
Michael Layzell734adb42017-06-07 16:58:31 -04001639 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001640 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001641 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001642 |
David Tolnay9374bc02018-01-27 18:49:36 -08001643 syn!(ExprParen) => { Expr::Paren }
1644 |
David Tolnay8c91b882017-12-28 23:04:32 -05001645 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001646 ));
1647
Michael Layzell734adb42017-06-07 16:58:31 -04001648 #[cfg(feature = "full")]
David Tolnay313a36f2018-04-29 20:13:04 -07001649 named!(expr_nosemi -> Expr, do_parse!(
1650 nosemi: alt!(
1651 syn!(ExprIf) => { Expr::If }
1652 |
1653 syn!(ExprIfLet) => { Expr::IfLet }
1654 |
1655 syn!(ExprWhile) => { Expr::While }
1656 |
1657 syn!(ExprWhileLet) => { Expr::WhileLet }
1658 |
1659 syn!(ExprForLoop) => { Expr::ForLoop }
1660 |
1661 syn!(ExprLoop) => { Expr::Loop }
1662 |
1663 syn!(ExprMatch) => { Expr::Match }
1664 |
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001665 syn!(ExprTryBlock) => { Expr::TryBlock }
David Tolnay313a36f2018-04-29 20:13:04 -07001666 |
1667 syn!(ExprYield) => { Expr::Yield }
1668 |
1669 syn!(ExprUnsafe) => { Expr::Unsafe }
1670 |
1671 syn!(ExprBlock) => { Expr::Block }
1672 ) >>
1673 // If the next token is a `.` or a `?` it is special-cased to parse
1674 // as an expression instead of a blockexpression.
1675 not!(punct!(.)) >>
1676 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001677 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001678 ));
Michael Layzell35418782017-06-07 09:20:25 -04001679
David Tolnay8c91b882017-12-28 23:04:32 -05001680 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001681 #[cfg(not(feature = "full"))]
1682 named!(parse -> Self, do_parse!(
1683 lit: syn!(Lit) >>
1684 (ExprLit {
1685 attrs: Vec::new(),
1686 lit: lit,
1687 })
1688 ));
1689
1690 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001691 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001692 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001693 lit: syn!(Lit) >>
1694 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001695 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001696 lit: lit,
1697 })
1698 ));
David Tolnay79777332018-01-07 10:04:42 -08001699
1700 fn description() -> Option<&'static str> {
1701 Some("literal")
1702 }
David Tolnay8c91b882017-12-28 23:04:32 -05001703 }
1704
1705 #[cfg(feature = "full")]
1706 impl Synom for ExprMacro {
1707 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001708 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001709 mac: syn!(Macro) >>
1710 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001711 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001712 mac: mac,
1713 })
1714 ));
David Tolnay79777332018-01-07 10:04:42 -08001715
1716 fn description() -> Option<&'static str> {
1717 Some("macro invocation expression")
1718 }
David Tolnay8c91b882017-12-28 23:04:32 -05001719 }
1720
David Tolnaye98775f2017-12-28 23:17:00 -05001721 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001722 impl Synom for ExprGroup {
1723 named!(parse -> Self, do_parse!(
David Tolnaya7d69fc2018-08-26 13:30:24 -04001724 e: old_grouped!(syn!(Expr)) >>
Michael Layzell93c36282017-06-04 20:43:14 -04001725 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001726 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001727 expr: Box::new(e.1),
1728 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001729 })
Michael Layzell93c36282017-06-04 20:43:14 -04001730 ));
David Tolnay79777332018-01-07 10:04:42 -08001731
1732 fn description() -> Option<&'static str> {
1733 Some("expression surrounded by invisible delimiters")
1734 }
Michael Layzell93c36282017-06-04 20:43:14 -04001735 }
1736
Alex Crichton954046c2017-05-30 21:49:42 -07001737 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001738 #[cfg(not(feature = "full"))]
1739 named!(parse -> Self, do_parse!(
1740 e: parens!(syn!(Expr)) >>
1741 (ExprParen {
1742 attrs: Vec::new(),
1743 paren_token: e.0,
1744 expr: Box::new(e.1),
1745 })
1746 ));
1747
1748 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001749 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001750 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001751 e: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001752 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001753 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001754 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001755 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001756 attrs: {
1757 let mut attrs = outer_attrs;
1758 attrs.extend((e.1).0);
1759 attrs
1760 },
David Tolnay8875fca2017-12-31 13:52:37 -05001761 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001762 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001763 })
Michael Layzell92639a52017-06-01 00:07:44 -04001764 ));
David Tolnay79777332018-01-07 10:04:42 -08001765
1766 fn description() -> Option<&'static str> {
1767 Some("parenthesized expression")
1768 }
Alex Crichton954046c2017-05-30 21:49:42 -07001769 }
David Tolnay89e05672016-10-02 14:39:42 -07001770
Michael Layzell734adb42017-06-07 16:58:31 -04001771 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001772 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001773 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001774 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001775 elems: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001776 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001777 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001778 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001779 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001780 attrs: {
1781 let mut attrs = outer_attrs;
1782 attrs.extend((elems.1).0);
1783 attrs
1784 },
David Tolnay8875fca2017-12-31 13:52:37 -05001785 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001786 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001787 })
1788 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001789
1790 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001791 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001792 }
Alex Crichton954046c2017-05-30 21:49:42 -07001793 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001794
David Tolnayf2cfd722017-12-31 18:02:51 -05001795 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001796 parens!(Punctuated::parse_terminated)
1797 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001798
Michael Layzell734adb42017-06-07 16:58:31 -04001799 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001800 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001801 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001802 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001803 turbofish: option!(tuple!(
1804 punct!(::),
1805 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001806 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001807 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001808 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001809 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001810 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001811 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001812 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001813 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001814 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001815 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001816 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817
Alex Crichton954046c2017-05-30 21:49:42 -07001818 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001819 turbofish: turbofish.map(|fish| MethodTurbofish {
1820 colon2_token: fish.0,
1821 lt_token: fish.1,
1822 args: fish.2,
1823 gt_token: fish.3,
1824 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001825 args: args.1,
1826 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001827 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001828 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001829 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001830 ));
1831
Michael Layzell734adb42017-06-07 16:58:31 -04001832 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001833 impl Synom for GenericMethodArgument {
1834 // TODO parse const generics as well
1835 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001836
1837 fn description() -> Option<&'static str> {
1838 Some("generic method argument")
1839 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001840 }
1841
1842 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001843 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001844 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001845 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001846 elems: parens!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001847 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001848 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001849 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001850 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001851 attrs: {
1852 let mut attrs = outer_attrs;
1853 attrs.extend((elems.1).0);
1854 attrs
1855 },
1856 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001857 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001858 })
1859 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001860
1861 fn description() -> Option<&'static str> {
1862 Some("tuple")
1863 }
Alex Crichton954046c2017-05-30 21:49:42 -07001864 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001865
Michael Layzell734adb42017-06-07 16:58:31 -04001866 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001867 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001868 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001869 if_: keyword!(if) >>
1870 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001871 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001872 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001873 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001874 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001875 else_block: option!(else_block) >>
1876 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001877 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001878 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001879 let_token: let_,
1880 eq_token: eq,
1881 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001882 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001883 brace_token: then_block.0,
1884 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001885 },
1886 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001887 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001888 })
1889 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001890
1891 fn description() -> Option<&'static str> {
1892 Some("`if let` expression")
1893 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001894 }
1895
Michael Layzell734adb42017-06-07 16:58:31 -04001896 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001897 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001898 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001899 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001900 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001901 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001902 else_block: option!(else_block) >>
1903 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001904 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001905 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001906 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001907 brace_token: then_block.0,
1908 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001909 },
1910 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001911 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001912 })
1913 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001914
1915 fn description() -> Option<&'static str> {
1916 Some("`if` expression")
1917 }
Alex Crichton954046c2017-05-30 21:49:42 -07001918 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001919
Michael Layzell734adb42017-06-07 16:58:31 -04001920 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001921 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001922 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001923 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001924 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001925 |
David Tolnay8c91b882017-12-28 23:04:32 -05001926 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001927 |
1928 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001929 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001930 (Expr::Block(ExprBlock {
1931 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04001932 label: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001933 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001934 brace_token: else_block.0,
1935 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001936 },
1937 }))
David Tolnay939766a2016-09-23 23:48:12 -07001938 )
Alex Crichton954046c2017-05-30 21:49:42 -07001939 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001940 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001941 ));
1942
Michael Layzell734adb42017-06-07 16:58:31 -04001943 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001944 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001945 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001946 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001947 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001948 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001949 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001950 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001951 expr: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001952 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001953 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001954 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001955 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001956 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001957 attrs: {
1958 let mut attrs = outer_attrs;
1959 attrs.extend((block.1).0);
1960 attrs
1961 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001962 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001963 for_token: for_,
1964 pat: Box::new(pat),
1965 in_token: in_,
1966 expr: Box::new(expr),
1967 body: Block {
1968 brace_token: block.0,
1969 stmts: (block.1).1,
1970 },
Michael Layzell92639a52017-06-01 00:07:44 -04001971 })
1972 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001973
1974 fn description() -> Option<&'static str> {
1975 Some("`for` loop")
1976 }
Alex Crichton954046c2017-05-30 21:49:42 -07001977 }
Gregory Katze5f35682016-09-27 14:20:55 -04001978
Michael Layzell734adb42017-06-07 16:58:31 -04001979 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001980 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001981 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04001982 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001983 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001984 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001985 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04001986 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001987 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001988 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001989 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001990 attrs: {
1991 let mut attrs = outer_attrs;
1992 attrs.extend((block.1).0);
1993 attrs
1994 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001995 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001996 loop_token: loop_,
1997 body: Block {
1998 brace_token: block.0,
1999 stmts: (block.1).1,
2000 },
Michael Layzell92639a52017-06-01 00:07:44 -04002001 })
2002 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002003
2004 fn description() -> Option<&'static str> {
2005 Some("`loop`")
2006 }
Alex Crichton954046c2017-05-30 21:49:42 -07002007 }
2008
Michael Layzell734adb42017-06-07 16:58:31 -04002009 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002010 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002011 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002012 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002013 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002014 obj: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002015 braced_content: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002016 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002017 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07002018 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05002019 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002020 attrs: {
2021 let mut attrs = outer_attrs;
2022 attrs.extend((braced_content.1).0);
2023 attrs
2024 },
David Tolnay8875fca2017-12-31 13:52:37 -05002025 expr: Box::new(obj),
2026 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07002027 brace_token: braced_content.0,
2028 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002029 })
2030 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002031
2032 fn description() -> Option<&'static str> {
2033 Some("`match` expression")
2034 }
Alex Crichton954046c2017-05-30 21:49:42 -07002035 }
David Tolnay1978c672016-10-27 22:05:52 -07002036
Michael Layzell734adb42017-06-07 16:58:31 -04002037 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002038 impl Synom for ExprTryBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002039 named!(parse -> Self, do_parse!(
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002040 try_token: keyword!(try) >>
2041 block: syn!(Block) >>
2042 (ExprTryBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002043 attrs: Vec::new(),
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002044 try_token: try_token,
2045 block: block,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002046 })
Michael Layzell92639a52017-06-01 00:07:44 -04002047 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002048
2049 fn description() -> Option<&'static str> {
David Tolnayfb2dd4b2018-08-24 16:45:34 -04002050 Some("`try` block")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002051 }
Alex Crichton954046c2017-05-30 21:49:42 -07002052 }
Arnavion02ef13f2017-04-25 00:54:31 -07002053
Michael Layzell734adb42017-06-07 16:58:31 -04002054 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002055 impl Synom for ExprYield {
2056 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002057 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002058 expr: option!(syn!(Expr)) >>
2059 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002060 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002061 yield_token: yield_,
2062 expr: expr.map(Box::new),
2063 })
2064 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002065
2066 fn description() -> Option<&'static str> {
2067 Some("`yield` expression")
2068 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002069 }
2070
2071 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002072 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002073 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002074 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002075 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002076 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002077 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002078 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002079 body: do_parse!(
2080 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002081 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2082 true => alt!(
2083 input_end!() => { |_| None }
2084 |
2085 punct!(,) => { Some }
2086 )
Alex Crichton03b30272017-08-28 09:35:24 -07002087 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002088 false => option!(punct!(,))
2089 ) >>
2090 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002091 ) >>
2092 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002093 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002094 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002095 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002096 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002097 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002098 body: Box::new(body.0),
2099 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002100 })
2101 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002102
2103 fn description() -> Option<&'static str> {
2104 Some("`match` arm")
2105 }
Alex Crichton954046c2017-05-30 21:49:42 -07002106 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002107
Michael Layzell734adb42017-06-07 16:58:31 -04002108 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002109 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002110 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002111 asyncness: option!(keyword!(async)) >>
2112 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002113 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002114 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002115 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002116 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002117 ret_and_body: alt!(
2118 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002119 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002120 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002121 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002122 (
2123 ReturnType::Type(arrow, Box::new(ty)),
2124 Expr::Block(ExprBlock {
2125 attrs: Vec::new(),
David Tolnay1d8e9962018-08-24 19:04:20 -04002126 label: None,
David Tolnay76178be2018-07-31 23:06:15 -07002127 block: body,
2128 },
2129 ))
David Tolnay89e05672016-10-02 14:39:42 -07002130 )
2131 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002132 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002133 ) >>
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002134 (Expr::Closure(ExprClosure {
2135 attrs: attrs,
2136 asyncness: asyncness,
2137 movability: movability,
2138 capture: capture,
2139 or1_token: or1,
2140 inputs: inputs,
2141 or2_token: or2,
2142 output: ret_and_body.0,
2143 body: Box::new(ret_and_body.1),
2144 }))
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002145 ));
2146
2147 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04002148 impl Synom for ExprAsync {
2149 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002150 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay02a9c6f2018-08-24 18:58:45 -04002151 async_token: keyword!(async) >>
2152 capture: option!(keyword!(move)) >>
2153 block: syn!(Block) >>
2154 (ExprAsync {
2155 attrs: attrs,
2156 async_token: async_token,
2157 capture: capture,
2158 block: block,
2159 })
2160 ));
2161 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002162
2163 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002164 named!(fn_arg -> FnArg, do_parse!(
2165 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002166 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002167 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002168 if let Some((colon, ty)) = ty {
2169 FnArg::Captured(ArgCaptured {
2170 pat: pat,
2171 colon_token: colon,
2172 ty: ty,
2173 })
2174 } else {
2175 FnArg::Inferred(pat)
2176 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002177 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002178 ));
2179
Michael Layzell734adb42017-06-07 16:58:31 -04002180 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002181 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002182 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002183 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002184 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002185 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002186 cond: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002187 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002188 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002189 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002190 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002191 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002192 attrs: {
2193 let mut attrs = outer_attrs;
2194 attrs.extend((block.1).0);
2195 attrs
2196 },
2197 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002198 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002199 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002200 body: Block {
2201 brace_token: block.0,
2202 stmts: (block.1).1,
2203 },
Michael Layzell92639a52017-06-01 00:07:44 -04002204 })
2205 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002206
2207 fn description() -> Option<&'static str> {
2208 Some("`while` expression")
2209 }
Alex Crichton954046c2017-05-30 21:49:42 -07002210 }
2211
Michael Layzell734adb42017-06-07 16:58:31 -04002212 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002213 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002214 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002215 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002216 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002217 while_: keyword!(while) >>
2218 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002219 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002220 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002221 value: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002222 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002223 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002224 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002225 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002226 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002227 attrs: {
2228 let mut attrs = outer_attrs;
2229 attrs.extend((block.1).0);
2230 attrs
2231 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002232 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002233 while_token: while_,
2234 let_token: let_,
2235 pats: pats,
2236 eq_token: eq,
2237 expr: Box::new(value),
2238 body: Block {
2239 brace_token: block.0,
2240 stmts: (block.1).1,
2241 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002242 })
2243 ));
David Tolnay79777332018-01-07 10:04:42 -08002244
2245 fn description() -> Option<&'static str> {
2246 Some("`while let` expression")
2247 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002248 }
2249
2250 #[cfg(feature = "full")]
2251 impl Synom for Label {
2252 named!(parse -> Self, do_parse!(
2253 name: syn!(Lifetime) >>
2254 colon: punct!(:) >>
2255 (Label {
2256 name: name,
2257 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002258 })
2259 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002260
2261 fn description() -> Option<&'static str> {
2262 Some("`while let` expression")
2263 }
Alex Crichton954046c2017-05-30 21:49:42 -07002264 }
2265
Michael Layzell734adb42017-06-07 16:58:31 -04002266 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002267 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002268 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002269 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002270 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002271 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002272 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002273 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002274 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002275 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002276 })
2277 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002278
2279 fn description() -> Option<&'static str> {
2280 Some("`continue`")
2281 }
Alex Crichton954046c2017-05-30 21:49:42 -07002282 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002283
Michael Layzell734adb42017-06-07 16:58:31 -04002284 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002285 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002286 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002287 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002288 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002289 // We can't allow blocks after a `break` expression when we wouldn't
2290 // allow structs, as this expression is ambiguous.
2291 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002292 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002293 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002294 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002295 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002296 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002297 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002298 ));
2299
Michael Layzell734adb42017-06-07 16:58:31 -04002300 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002301 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002302 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002303 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002304 // NOTE: return is greedy and eats blocks after it even when in a
2305 // position where structs are not allowed, such as in if statement
2306 // conditions. For example:
2307 //
David Tolnaybcf26022017-12-25 22:10:52 -05002308 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002309 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002310 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002311 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002312 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002313 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002314 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002315 ));
2316
Michael Layzell734adb42017-06-07 16:58:31 -04002317 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002318 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002319 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002320 outer_attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002321 path: syn!(Path) >>
2322 data: braces!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002323 inner_attrs: many0!(Attribute::old_parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002324 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002325 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2326 dots: punct!(..) >>
2327 base: syn!(Expr) >>
2328 (dots, base)
2329 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002330 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002331 )) >>
2332 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002333 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002334 let (dots, rest) = match base.and_then(|b| b) {
2335 Some((dots, base)) => (Some(dots), Some(base)),
2336 None => (None, None),
2337 };
2338 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002339 attrs: {
2340 let mut attrs = outer_attrs;
2341 attrs.extend(inner_attrs);
2342 attrs
2343 },
Michael Layzell92639a52017-06-01 00:07:44 -04002344 brace_token: brace,
2345 path: path,
2346 fields: fields,
2347 dot2_token: dots,
2348 rest: rest.map(Box::new),
2349 }
2350 })
2351 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002352
2353 fn description() -> Option<&'static str> {
2354 Some("struct literal expression")
2355 }
Alex Crichton954046c2017-05-30 21:49:42 -07002356 }
2357
Michael Layzell734adb42017-06-07 16:58:31 -04002358 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002359 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002360 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002361 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayc42b90a2018-01-18 23:11:37 -08002362 field_value: alt!(
2363 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2364 |
2365 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002366 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002367 None,
2368 Expr::Path(ExprPath {
2369 attrs: Vec::new(),
2370 qself: None,
2371 path: name.into(),
2372 }),
2373 ))
2374 ) >>
2375 (FieldValue {
2376 attrs: attrs,
2377 member: field_value.0,
2378 colon_token: field_value.1,
2379 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002380 })
2381 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002382
2383 fn description() -> Option<&'static str> {
2384 Some("field-value pair: `field: value`")
2385 }
Alex Crichton954046c2017-05-30 21:49:42 -07002386 }
David Tolnay055a7042016-10-02 19:23:54 -07002387
Michael Layzell734adb42017-06-07 16:58:31 -04002388 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002389 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002390 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002391 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002392 data: brackets!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002393 many0!(Attribute::old_parse_inner),
David Tolnay5d314dc2018-07-21 16:40:01 -07002394 syn!(Expr),
2395 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002396 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002397 )) >>
2398 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002399 attrs: {
2400 let mut attrs = outer_attrs;
2401 attrs.extend((data.1).0);
2402 attrs
2403 },
2404 expr: Box::new((data.1).1),
2405 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002406 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002407 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002408 })
2409 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002410
2411 fn description() -> Option<&'static str> {
2412 Some("repeated array literal: `[val; N]`")
2413 }
Alex Crichton954046c2017-05-30 21:49:42 -07002414 }
David Tolnay055a7042016-10-02 19:23:54 -07002415
Michael Layzell734adb42017-06-07 16:58:31 -04002416 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002417 impl Synom for ExprUnsafe {
2418 named!(parse -> Self, do_parse!(
David Tolnay8b493772018-08-27 06:30:18 -07002419 outer_attrs: many0!(Attribute::old_parse_outer) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002420 unsafe_: keyword!(unsafe) >>
David Tolnayc4be3512018-08-27 06:25:44 -07002421 block: braces!(tuple!(
David Tolnay8b493772018-08-27 06:30:18 -07002422 many0!(Attribute::old_parse_inner),
David Tolnayc4be3512018-08-27 06:25:44 -07002423 call!(Block::parse_within),
2424 )) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002425 (ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002426 attrs: {
2427 let mut attrs = outer_attrs;
2428 attrs.extend((block.1).0);
2429 attrs
2430 },
Nika Layzell640832a2017-12-04 13:37:09 -05002431 unsafe_token: unsafe_,
David Tolnayc4be3512018-08-27 06:25:44 -07002432 block: Block {
2433 brace_token: block.0,
2434 stmts: (block.1).1,
2435 },
Nika Layzell640832a2017-12-04 13:37:09 -05002436 })
2437 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002438
2439 fn description() -> Option<&'static str> {
2440 Some("unsafe block: `unsafe { .. }`")
2441 }
Nika Layzell640832a2017-12-04 13:37:09 -05002442 }
2443
2444 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002445 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002446 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002447 outer_attrs: many0!(Attribute::old_parse_outer) >>
David Tolnay1d8e9962018-08-24 19:04:20 -04002448 label: option!(syn!(Label)) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002449 block: braces!(tuple!(
David Tolnayf8106f82018-08-25 21:17:45 -04002450 many0!(Attribute::old_parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002451 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002452 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002453 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002454 attrs: {
2455 let mut attrs = outer_attrs;
2456 attrs.extend((block.1).0);
2457 attrs
2458 },
David Tolnay1d8e9962018-08-24 19:04:20 -04002459 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002460 block: Block {
2461 brace_token: block.0,
2462 stmts: (block.1).1,
2463 },
Michael Layzell92639a52017-06-01 00:07:44 -04002464 })
2465 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002466
2467 fn description() -> Option<&'static str> {
2468 Some("block: `{ .. }`")
2469 }
Alex Crichton954046c2017-05-30 21:49:42 -07002470 }
David Tolnay89e05672016-10-02 14:39:42 -07002471
Michael Layzell734adb42017-06-07 16:58:31 -04002472 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002473 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002474 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002475 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002476 (ExprRange {
2477 attrs: Vec::new(),
2478 from: None,
2479 to: hi.map(Box::new),
2480 limits: limits,
2481 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002482 ));
2483
Michael Layzell734adb42017-06-07 16:58:31 -04002484 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002485 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002486 named!(parse -> Self, alt!(
2487 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002488 punct!(..=) => { RangeLimits::Closed }
2489 |
2490 // Must come before Dot2
David Tolnay7ac699c2018-08-24 14:00:58 -04002491 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.spans)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002492 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002493 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002494 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002495
2496 fn description() -> Option<&'static str> {
2497 Some("range limit: `..`, `...` or `..=`")
2498 }
Alex Crichton954046c2017-05-30 21:49:42 -07002499 }
David Tolnay438c9052016-10-07 23:24:48 -07002500
Alex Crichton954046c2017-05-30 21:49:42 -07002501 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002502 #[cfg(not(feature = "full"))]
2503 named!(parse -> Self, do_parse!(
2504 pair: qpath >>
2505 (ExprPath {
2506 attrs: Vec::new(),
2507 qself: pair.0,
2508 path: pair.1,
2509 })
2510 ));
2511
2512 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002513 named!(parse -> Self, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002514 attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002515 pair: qpath >>
2516 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002517 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002518 qself: pair.0,
2519 path: pair.1,
2520 })
2521 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002522
2523 fn description() -> Option<&'static str> {
2524 Some("path: `a::b::c`")
2525 }
Alex Crichton954046c2017-05-30 21:49:42 -07002526 }
David Tolnay42602292016-10-01 22:25:45 -07002527
David Tolnay9cc2f092018-08-24 15:51:37 -04002528 named!(path -> Path, do_parse!(
2529 colon: option!(punct!(::)) >>
2530 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2531 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2532 (Path {
2533 leading_colon: colon,
2534 segments: segments,
2535 })
2536 ));
2537
2538 named!(path_segment -> PathSegment, alt!(
2539 do_parse!(
2540 ident: syn!(Ident) >>
2541 colon2: punct!(::) >>
2542 lt: punct!(<) >>
2543 args: call!(Punctuated::parse_terminated) >>
2544 gt: punct!(>) >>
2545 (PathSegment {
2546 ident: ident,
2547 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2548 colon2_token: Some(colon2),
2549 lt_token: lt,
2550 args: args,
2551 gt_token: gt,
2552 }),
2553 })
2554 )
2555 |
David Tolnaya7d69fc2018-08-26 13:30:24 -04002556 old_mod_style_path_segment
David Tolnay9cc2f092018-08-24 15:51:37 -04002557 ));
2558
2559 named!(qpath -> (Option<QSelf>, Path), alt!(
2560 map!(path, |p| (None, p))
2561 |
2562 do_parse!(
2563 lt: punct!(<) >>
2564 this: syn!(Type) >>
2565 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2566 gt: punct!(>) >>
2567 colon2: punct!(::) >>
2568 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2569 ({
2570 let (pos, as_, path) = match path {
2571 Some((as_, mut path)) => {
2572 let pos = path.segments.len();
2573 path.segments.push_punct(colon2);
2574 path.segments.extend(rest.into_pairs());
2575 (pos, Some(as_), path)
2576 }
2577 None => {
2578 (0, None, Path {
2579 leading_colon: Some(colon2),
2580 segments: rest,
2581 })
2582 }
2583 };
2584 (Some(QSelf {
2585 lt_token: lt,
2586 ty: Box::new(this),
2587 position: pos,
2588 as_token: as_,
2589 gt_token: gt,
2590 }), path)
2591 })
2592 )
2593 |
2594 map!(keyword!(self), |s| (None, s.into()))
2595 ));
2596
David Tolnay85b69a42017-12-27 20:43:10 -05002597 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002598
David Tolnay8875fca2017-12-31 13:52:37 -05002599 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002600
Michael Layzell734adb42017-06-07 16:58:31 -04002601 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002602 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002603 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002604 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002605 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002606 brace_token: stmts.0,
2607 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002608 })
2609 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002610
2611 fn description() -> Option<&'static str> {
2612 Some("block: `{ .. }`")
2613 }
Alex Crichton954046c2017-05-30 21:49:42 -07002614 }
David Tolnay939766a2016-09-23 23:48:12 -07002615
Michael Layzell734adb42017-06-07 16:58:31 -04002616 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002617 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002618 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002619 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002620 mut standalone: many0!(do_parse!(
2621 stmt: syn!(Stmt) >>
2622 many0!(punct!(;)) >>
2623 (stmt)
2624 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002625 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002626 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002627 mut e: syn!(Expr) >>
2628 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002629 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002630 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002631 })
2632 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002633 (match last {
2634 None => standalone,
2635 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002636 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002637 standalone
2638 }
2639 })
2640 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002641 }
2642
Michael Layzell734adb42017-06-07 16:58:31 -04002643 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002644 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002645 named!(parse -> Self, alt!(
2646 stmt_mac
2647 |
2648 stmt_local
2649 |
2650 stmt_item
2651 |
Michael Layzell35418782017-06-07 09:20:25 -04002652 stmt_blockexpr
2653 |
Michael Layzell92639a52017-06-01 00:07:44 -04002654 stmt_expr
2655 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002656
2657 fn description() -> Option<&'static str> {
2658 Some("statement")
2659 }
Alex Crichton954046c2017-05-30 21:49:42 -07002660 }
David Tolnay939766a2016-09-23 23:48:12 -07002661
Michael Layzell734adb42017-06-07 16:58:31 -04002662 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002663 named!(stmt_mac -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002664 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaya7d69fc2018-08-26 13:30:24 -04002665 what: call!(Path::old_parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002666 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002667 // Only parse braces here; paren and bracket will get parsed as
2668 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002669 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002670 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002671 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002672 attrs: attrs,
2673 ident: None,
2674 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002675 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002676 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002677 delimiter: MacroDelimiter::Brace(data.0),
2678 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002679 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002680 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002681 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002682 ));
2683
Michael Layzell734adb42017-06-07 16:58:31 -04002684 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002685 named!(stmt_local -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002686 attrs: many0!(Attribute::old_parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002687 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002688 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002689 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002690 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2691 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002692 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002693 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002694 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002695 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002696 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2697 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2698 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002699 }))
David Tolnay191e0582016-10-02 18:31:09 -07002700 ));
2701
Michael Layzell734adb42017-06-07 16:58:31 -04002702 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002703 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002704
Michael Layzell734adb42017-06-07 16:58:31 -04002705 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002706 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002707 mut attrs: many0!(Attribute::old_parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002708 mut e: expr_nosemi >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002709 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002710 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002711 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002712 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002713 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002714 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002715 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002716 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002717 }
2718 })
2719 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002720
Michael Layzell734adb42017-06-07 16:58:31 -04002721 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002722 named!(stmt_expr -> Stmt, do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002723 mut attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002724 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002725 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002726 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002727 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002728 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002729 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002730 })
David Tolnay939766a2016-09-23 23:48:12 -07002731 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002732
Michael Layzell734adb42017-06-07 16:58:31 -04002733 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002734 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002735 named!(parse -> Self, alt!(
2736 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2737 |
2738 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2739 |
2740 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2741 |
2742 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2743 |
2744 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2745 |
David Tolnay323279a2017-12-29 11:26:32 -05002746 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002747 |
2748 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2749 |
2750 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2751 |
2752 syn!(PatPath) => { Pat::Path }
2753 |
2754 syn!(PatTuple) => { Pat::Tuple }
2755 |
2756 syn!(PatRef) => { Pat::Ref }
2757 |
2758 syn!(PatSlice) => { Pat::Slice }
2759 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002760
2761 fn description() -> Option<&'static str> {
2762 Some("pattern")
2763 }
Alex Crichton954046c2017-05-30 21:49:42 -07002764 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002765
Michael Layzell734adb42017-06-07 16:58:31 -04002766 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002767 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002768 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002769 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002770 |u| PatWild { underscore_token: u }
2771 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002772
2773 fn description() -> Option<&'static str> {
2774 Some("wild pattern: `_`")
2775 }
Alex Crichton954046c2017-05-30 21:49:42 -07002776 }
David Tolnay84aa0752016-10-02 23:01:13 -07002777
Michael Layzell734adb42017-06-07 16:58:31 -04002778 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002779 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002780 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002781 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002782 pat: syn!(Pat) >>
2783 (PatBox {
2784 pat: Box::new(pat),
2785 box_token: boxed,
2786 })
2787 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002788
2789 fn description() -> Option<&'static str> {
2790 Some("box pattern")
2791 }
Alex Crichton954046c2017-05-30 21:49:42 -07002792 }
2793
Michael Layzell734adb42017-06-07 16:58:31 -04002794 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002795 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002796 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002797 by_ref: option!(keyword!(ref)) >>
2798 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002799 name: alt!(
2800 syn!(Ident)
2801 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002802 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002803 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002804 not!(punct!(<)) >>
2805 not!(punct!(::)) >>
2806 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002807 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002808 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002809 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002810 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002811 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002812 })
2813 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002814
2815 fn description() -> Option<&'static str> {
2816 Some("pattern identifier binding")
2817 }
Alex Crichton954046c2017-05-30 21:49:42 -07002818 }
2819
Michael Layzell734adb42017-06-07 16:58:31 -04002820 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002821 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002822 named!(parse -> Self, do_parse!(
2823 path: syn!(Path) >>
2824 tuple: syn!(PatTuple) >>
2825 (PatTupleStruct {
2826 path: path,
2827 pat: tuple,
2828 })
2829 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002830
2831 fn description() -> Option<&'static str> {
2832 Some("tuple struct pattern")
2833 }
Alex Crichton954046c2017-05-30 21:49:42 -07002834 }
2835
Michael Layzell734adb42017-06-07 16:58:31 -04002836 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002837 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002838 named!(parse -> Self, do_parse!(
2839 path: syn!(Path) >>
2840 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002841 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002842 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002843 (fields, base)
2844 )) >>
2845 (PatStruct {
2846 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002847 fields: (data.1).0,
2848 brace_token: data.0,
2849 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002850 })
2851 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002852
2853 fn description() -> Option<&'static str> {
2854 Some("struct pattern")
2855 }
Alex Crichton954046c2017-05-30 21:49:42 -07002856 }
2857
Michael Layzell734adb42017-06-07 16:58:31 -04002858 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002859 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002860 named!(parse -> Self, alt!(
2861 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002862 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002863 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002864 pat: syn!(Pat) >>
2865 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002866 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002867 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002868 attrs: Vec::new(),
2869 colon_token: Some(colon),
2870 })
2871 )
2872 |
2873 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002874 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002875 by_ref: option!(keyword!(ref)) >>
2876 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002877 ident: syn!(Ident) >>
2878 ({
2879 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002880 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002881 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002882 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002883 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002884 }.into();
2885 if let Some(boxed) = boxed {
2886 pat = PatBox {
2887 pat: Box::new(pat),
2888 box_token: boxed,
2889 }.into();
2890 }
2891 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002892 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002893 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002894 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002895 colon_token: None,
2896 }
2897 })
2898 )
2899 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002900
2901 fn description() -> Option<&'static str> {
2902 Some("field pattern")
2903 }
Alex Crichton954046c2017-05-30 21:49:42 -07002904 }
2905
David Tolnay85b69a42017-12-27 20:43:10 -05002906 impl Synom for Member {
2907 named!(parse -> Self, alt!(
2908 syn!(Ident) => { Member::Named }
2909 |
2910 syn!(Index) => { Member::Unnamed }
2911 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002912
2913 fn description() -> Option<&'static str> {
2914 Some("field member")
2915 }
David Tolnay85b69a42017-12-27 20:43:10 -05002916 }
2917
David Tolnay85b69a42017-12-27 20:43:10 -05002918 impl Synom for Index {
2919 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002920 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002921 ({
David Tolnay360efd22018-01-04 23:35:26 -08002922 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002923 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002924 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002925 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002926 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002927 })
David Tolnay85b69a42017-12-27 20:43:10 -05002928 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002929
2930 fn description() -> Option<&'static str> {
2931 Some("field index")
2932 }
David Tolnay85b69a42017-12-27 20:43:10 -05002933 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002934
Michael Layzell734adb42017-06-07 16:58:31 -04002935 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002936 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002937 named!(parse -> Self, map!(
2938 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002939 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002940 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002941
2942 fn description() -> Option<&'static str> {
2943 Some("path pattern")
2944 }
Alex Crichton954046c2017-05-30 21:49:42 -07002945 }
David Tolnay9636c052016-10-02 17:11:17 -07002946
Michael Layzell734adb42017-06-07 16:58:31 -04002947 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002948 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002949 named!(parse -> Self, do_parse!(
2950 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002951 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002952 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2953 tuple!(punct!(..), option!(punct!(,)))
2954 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002955 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002956 Some((_, Some(_))) => true,
2957 _ => false,
2958 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002959 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002960 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002961 )) >>
2962 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002963 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002964 let (dotdot, trailing) = match dotdot {
2965 Some((a, b)) => (Some(a), Some(b)),
2966 None => (None, None),
2967 };
2968 PatTuple {
2969 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002970 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002971 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002972 comma_token: trailing.unwrap_or_default(),
2973 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002974 }
2975 })
2976 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002977
2978 fn description() -> Option<&'static str> {
2979 Some("tuple pattern")
2980 }
Alex Crichton954046c2017-05-30 21:49:42 -07002981 }
David Tolnayfbb73232016-10-03 01:00:06 -07002982
Michael Layzell734adb42017-06-07 16:58:31 -04002983 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002984 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002985 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002986 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002987 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002988 pat: syn!(Pat) >>
2989 (PatRef {
2990 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002991 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002992 and_token: and,
2993 })
2994 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002995
2996 fn description() -> Option<&'static str> {
2997 Some("reference pattern")
2998 }
Alex Crichton954046c2017-05-30 21:49:42 -07002999 }
David Tolnayffdb97f2016-10-03 01:28:33 -07003000
Michael Layzell734adb42017-06-07 16:58:31 -04003001 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003002 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04003003 named!(parse -> Self, do_parse!(
3004 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05003005 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04003006 return parse_error(); // these need to be parsed by pat_path
3007 } else {
3008 PatLit {
3009 expr: Box::new(lit),
3010 }
3011 })
3012 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003013
3014 fn description() -> Option<&'static str> {
3015 Some("literal pattern")
3016 }
Alex Crichton954046c2017-05-30 21:49:42 -07003017 }
David Tolnaye1310902016-10-29 23:40:00 -07003018
Michael Layzell734adb42017-06-07 16:58:31 -04003019 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003020 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04003021 named!(parse -> Self, do_parse!(
3022 lo: pat_lit_expr >>
3023 limits: syn!(RangeLimits) >>
3024 hi: pat_lit_expr >>
3025 (PatRange {
3026 lo: Box::new(lo),
3027 hi: Box::new(hi),
3028 limits: limits,
3029 })
3030 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003031
3032 fn description() -> Option<&'static str> {
3033 Some("range pattern")
3034 }
Alex Crichton954046c2017-05-30 21:49:42 -07003035 }
David Tolnaye1310902016-10-29 23:40:00 -07003036
Michael Layzell734adb42017-06-07 16:58:31 -04003037 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07003038 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003039 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07003040 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05003041 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07003042 |
David Tolnay8c91b882017-12-28 23:04:32 -05003043 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07003044 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05003045 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003046 Expr::Unary(ExprUnary {
3047 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003048 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05003049 expr: Box::new(v)
3050 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003051 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05003052 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003053 })
3054 ));
David Tolnay8b308c22016-10-03 01:24:10 -07003055
Michael Layzell734adb42017-06-07 16:58:31 -04003056 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003057 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04003058 named!(parse -> Self, map!(
3059 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05003060 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003061 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003062 dots: punct!(..) >>
3063 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003064 (dots, trailing)
3065 )) >>
3066 after: cond!(
3067 match middle {
3068 Some((_, ref trailing)) => trailing.is_some(),
3069 _ => false,
3070 },
David Tolnayf2cfd722017-12-31 18:02:51 -05003071 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04003072 ) >>
3073 (before, middle, after)
3074 )),
David Tolnay8875fca2017-12-31 13:52:37 -05003075 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05003076 let mut before: Punctuated<Pat, Token![,]> = before;
3077 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08003078 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04003079 PatSlice {
David Tolnay7ac699c2018-08-24 14:00:58 -04003080 dot2_token: middle.as_ref().map(|m| Token![..](m.0.spans)),
Michael Layzell92639a52017-06-01 00:07:44 -04003081 comma_token: middle.as_ref().and_then(|m| {
David Tolnay7ac699c2018-08-24 14:00:58 -04003082 m.1.as_ref().map(|m| Token![,](m.spans))
Michael Layzell92639a52017-06-01 00:07:44 -04003083 }),
3084 bracket_token: brackets,
3085 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05003086 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04003087 None
David Tolnaydc03aec2017-12-30 01:54:18 -05003088 } else {
David Tolnay56080682018-01-06 14:01:52 -08003089 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04003090 }
3091 }),
3092 front: before,
3093 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07003094 }
Alex Crichton954046c2017-05-30 21:49:42 -07003095 }
Michael Layzell92639a52017-06-01 00:07:44 -04003096 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003097
3098 fn description() -> Option<&'static str> {
3099 Some("slice pattern")
3100 }
Alex Crichton954046c2017-05-30 21:49:42 -07003101 }
David Tolnay323279a2017-12-29 11:26:32 -05003102
3103 #[cfg(feature = "full")]
3104 impl Synom for PatMacro {
3105 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003106
3107 fn description() -> Option<&'static str> {
3108 Some("macro pattern")
3109 }
David Tolnay323279a2017-12-29 11:26:32 -05003110 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003111}
3112
David Tolnayf4bbbd92016-09-23 14:41:55 -07003113#[cfg(feature = "printing")]
3114mod printing {
3115 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003116 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003117 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003118 use proc_macro2::{Literal, TokenStream};
3119 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003120
David Tolnaybcf26022017-12-25 22:10:52 -05003121 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003122 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003123 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003124 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003125 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003126 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003127 e.to_tokens(tokens);
3128 });
3129 } else {
3130 e.to_tokens(tokens);
3131 }
3132 }
3133
David Tolnay8c91b882017-12-28 23:04:32 -05003134 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003135 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003136 tokens.append_all(attrs.outer());
3137 }
Michael Layzell734adb42017-06-07 16:58:31 -04003138
David Tolnayd997aef2018-07-21 18:42:31 -07003139 #[cfg(feature = "full")]
3140 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3141 tokens.append_all(attrs.inner());
3142 }
3143
David Tolnay8c91b882017-12-28 23:04:32 -05003144 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003145 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3146
3147 #[cfg(not(feature = "full"))]
3148 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003149
Michael Layzell734adb42017-06-07 16:58:31 -04003150 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003151 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003152 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003153 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003154 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003155 self.expr.to_tokens(tokens);
3156 }
3157 }
3158
Michael Layzell734adb42017-06-07 16:58:31 -04003159 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003160 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003161 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003162 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003163 self.place.to_tokens(tokens);
3164 self.arrow_token.to_tokens(tokens);
3165 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003166 }
3167 }
3168
Michael Layzell734adb42017-06-07 16:58:31 -04003169 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003170 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003171 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003172 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003173 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003174 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003175 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003176 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003177 }
3178 }
3179
3180 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003181 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003182 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003183 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003184 self.paren_token.surround(tokens, |tokens| {
3185 self.args.to_tokens(tokens);
3186 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003187 }
3188 }
3189
Michael Layzell734adb42017-06-07 16:58:31 -04003190 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003191 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003192 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003193 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003194 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003195 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003196 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003197 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003198 self.paren_token.surround(tokens, |tokens| {
3199 self.args.to_tokens(tokens);
3200 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003201 }
3202 }
3203
Michael Layzell734adb42017-06-07 16:58:31 -04003204 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003205 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003206 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003207 self.colon2_token.to_tokens(tokens);
3208 self.lt_token.to_tokens(tokens);
3209 self.args.to_tokens(tokens);
3210 self.gt_token.to_tokens(tokens);
3211 }
3212 }
3213
3214 #[cfg(feature = "full")]
3215 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003216 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003217 match *self {
3218 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3219 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3220 }
3221 }
3222 }
3223
3224 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003225 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003226 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003227 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003228 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003229 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003230 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003231 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003232 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003233 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003234 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003235 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003236 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003237 }
3238 }
3239
3240 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003241 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003242 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003243 self.left.to_tokens(tokens);
3244 self.op.to_tokens(tokens);
3245 self.right.to_tokens(tokens);
3246 }
3247 }
3248
3249 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003250 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003251 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003252 self.op.to_tokens(tokens);
3253 self.expr.to_tokens(tokens);
3254 }
3255 }
3256
David Tolnay8c91b882017-12-28 23:04:32 -05003257 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003258 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003259 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003260 self.lit.to_tokens(tokens);
3261 }
3262 }
3263
Alex Crichton62a0a592017-05-22 13:58:53 -07003264 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003265 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003266 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003267 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003268 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003269 self.ty.to_tokens(tokens);
3270 }
3271 }
3272
David Tolnay0cf94f22017-12-28 23:46:26 -05003273 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003274 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003275 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003276 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003277 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003278 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003279 self.ty.to_tokens(tokens);
3280 }
3281 }
3282
Michael Layzell734adb42017-06-07 16:58:31 -04003283 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003284 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003285 if let Some((ref else_token, ref else_)) = *else_ {
3286 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003287
3288 // If we are not one of the valid expressions to exist in an else
3289 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003290 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003291 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003292 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003293 }
3294 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003295 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003296 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003297 });
3298 }
3299 }
3300 }
3301 }
3302
3303 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003304 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003305 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003306 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003307 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003308 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003309 self.then_branch.to_tokens(tokens);
3310 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003311 }
3312 }
3313
Michael Layzell734adb42017-06-07 16:58:31 -04003314 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003315 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003316 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003317 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003318 self.if_token.to_tokens(tokens);
3319 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003320 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003321 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003322 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003323 self.then_branch.to_tokens(tokens);
3324 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003325 }
3326 }
3327
Michael Layzell734adb42017-06-07 16:58:31 -04003328 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003329 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003330 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003331 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003332 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003333 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003334 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003335 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003336 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003337 tokens.append_all(&self.body.stmts);
3338 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003339 }
3340 }
3341
Michael Layzell734adb42017-06-07 16:58:31 -04003342 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003343 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003344 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003345 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003346 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003347 self.while_token.to_tokens(tokens);
3348 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003349 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003350 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003351 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003352 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003353 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003354 tokens.append_all(&self.body.stmts);
3355 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003356 }
3357 }
3358
Michael Layzell734adb42017-06-07 16:58:31 -04003359 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003360 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003361 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003362 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003363 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003364 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003365 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003366 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003367 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003368 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003369 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003370 tokens.append_all(&self.body.stmts);
3371 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003372 }
3373 }
3374
Michael Layzell734adb42017-06-07 16:58:31 -04003375 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003376 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003377 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003378 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003379 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003380 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003381 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003382 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003383 tokens.append_all(&self.body.stmts);
3384 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003385 }
3386 }
3387
Michael Layzell734adb42017-06-07 16:58:31 -04003388 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003389 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003390 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003391 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003392 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003393 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003394 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003395 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003396 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003397 arm.to_tokens(tokens);
3398 // Ensure that we have a comma after a non-block arm, except
3399 // for the last one.
3400 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003401 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003402 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003403 }
3404 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003405 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003406 }
3407 }
3408
Michael Layzell734adb42017-06-07 16:58:31 -04003409 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003410 impl ToTokens for ExprAsync {
3411 fn to_tokens(&self, tokens: &mut TokenStream) {
3412 outer_attrs_to_tokens(&self.attrs, tokens);
3413 self.async_token.to_tokens(tokens);
3414 self.capture.to_tokens(tokens);
3415 self.block.to_tokens(tokens);
3416 }
3417 }
3418
3419 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003420 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003421 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003422 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003423 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003424 self.block.to_tokens(tokens);
3425 }
3426 }
3427
Michael Layzell734adb42017-06-07 16:58:31 -04003428 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003429 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003430 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003431 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003432 self.yield_token.to_tokens(tokens);
3433 self.expr.to_tokens(tokens);
3434 }
3435 }
3436
3437 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003438 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003439 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003440 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003441 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003442 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003443 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003444 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003445 for input in self.inputs.pairs() {
3446 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003447 FnArg::Captured(ArgCaptured {
3448 ref pat,
3449 ty: Type::Infer(_),
3450 ..
3451 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003452 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003453 }
David Tolnay56080682018-01-06 14:01:52 -08003454 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003455 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003456 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003457 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003458 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003459 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003460 self.body.to_tokens(tokens);
3461 }
3462 }
3463
Michael Layzell734adb42017-06-07 16:58:31 -04003464 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003465 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003466 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003467 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003468 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003469 self.block.brace_token.surround(tokens, |tokens| {
3470 inner_attrs_to_tokens(&self.attrs, tokens);
3471 tokens.append_all(&self.block.stmts);
3472 });
Nika Layzell640832a2017-12-04 13:37:09 -05003473 }
3474 }
3475
3476 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003477 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003478 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003479 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003480 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003481 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003482 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003483 tokens.append_all(&self.block.stmts);
3484 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003485 }
3486 }
3487
Michael Layzell734adb42017-06-07 16:58:31 -04003488 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003489 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003490 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003491 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003492 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003493 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003494 self.right.to_tokens(tokens);
3495 }
3496 }
3497
Michael Layzell734adb42017-06-07 16:58:31 -04003498 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003499 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003500 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003501 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003502 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003503 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003504 self.right.to_tokens(tokens);
3505 }
3506 }
3507
3508 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003509 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003510 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003511 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003512 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003513 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003514 }
3515 }
3516
David Tolnay85b69a42017-12-27 20:43:10 -05003517 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003518 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003519 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003520 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003521 Member::Unnamed(ref index) => index.to_tokens(tokens),
3522 }
3523 }
3524 }
3525
David Tolnay85b69a42017-12-27 20:43:10 -05003526 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003527 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003528 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3529 lit.set_span(self.span);
3530 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003531 }
3532 }
3533
3534 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003535 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003536 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003537 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003538 self.bracket_token.surround(tokens, |tokens| {
3539 self.index.to_tokens(tokens);
3540 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003541 }
3542 }
3543
Michael Layzell734adb42017-06-07 16:58:31 -04003544 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003545 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003546 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003547 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003548 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003549 match self.limits {
3550 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3551 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3552 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003553 self.to.to_tokens(tokens);
3554 }
3555 }
3556
3557 impl ToTokens for ExprPath {
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 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003561 }
3562 }
3563
Michael Layzell734adb42017-06-07 16:58:31 -04003564 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003565 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003566 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003567 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003568 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003569 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003570 self.expr.to_tokens(tokens);
3571 }
3572 }
3573
Michael Layzell734adb42017-06-07 16:58:31 -04003574 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003575 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003576 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003577 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003578 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003579 self.label.to_tokens(tokens);
3580 self.expr.to_tokens(tokens);
3581 }
3582 }
3583
Michael Layzell734adb42017-06-07 16:58:31 -04003584 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003585 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003586 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003587 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003588 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003589 self.label.to_tokens(tokens);
3590 }
3591 }
3592
Michael Layzell734adb42017-06-07 16:58:31 -04003593 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003594 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003595 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003596 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003597 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003598 self.expr.to_tokens(tokens);
3599 }
3600 }
3601
Michael Layzell734adb42017-06-07 16:58:31 -04003602 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003603 impl ToTokens for ExprMacro {
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);
David Tolnay8c91b882017-12-28 23:04:32 -05003606 self.mac.to_tokens(tokens);
3607 }
3608 }
3609
3610 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003611 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003612 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003613 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003614 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003615 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003616 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003617 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003618 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003619 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003620 self.rest.to_tokens(tokens);
3621 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003622 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003623 }
3624 }
3625
Michael Layzell734adb42017-06-07 16:58:31 -04003626 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003627 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003628 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003629 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003630 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003631 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003632 self.expr.to_tokens(tokens);
3633 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003634 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003635 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003636 }
3637 }
3638
David Tolnaye98775f2017-12-28 23:17:00 -05003639 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003640 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003641 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003642 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003643 self.group_token.surround(tokens, |tokens| {
3644 self.expr.to_tokens(tokens);
3645 });
3646 }
3647 }
3648
Alex Crichton62a0a592017-05-22 13:58:53 -07003649 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003650 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003651 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003652 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003653 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003654 self.expr.to_tokens(tokens);
3655 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003656 }
3657 }
3658
Michael Layzell734adb42017-06-07 16:58:31 -04003659 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003660 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003661 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003662 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003663 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003664 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003665 }
3666 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003667
David Tolnay2ae520a2017-12-29 11:19:50 -05003668 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003669 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003670 self.tts.to_tokens(tokens);
3671 }
3672 }
3673
Michael Layzell734adb42017-06-07 16:58:31 -04003674 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003675 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003676 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003677 self.name.to_tokens(tokens);
3678 self.colon_token.to_tokens(tokens);
3679 }
3680 }
3681
3682 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003683 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003684 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003685 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003686 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003687 if let Some(ref colon_token) = self.colon_token {
3688 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003689 self.expr.to_tokens(tokens);
3690 }
David Tolnay055a7042016-10-02 19:23:54 -07003691 }
3692 }
3693
Michael Layzell734adb42017-06-07 16:58:31 -04003694 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003695 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003696 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003697 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003698 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003699 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003700 if let Some((ref if_token, ref guard)) = self.guard {
3701 if_token.to_tokens(tokens);
3702 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003703 }
David Tolnaydfb91432018-03-31 19:19:44 +02003704 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003705 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003706 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003707 }
3708 }
3709
Michael Layzell734adb42017-06-07 16:58:31 -04003710 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003711 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003712 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003713 self.underscore_token.to_tokens(tokens);
3714 }
3715 }
3716
Michael Layzell734adb42017-06-07 16:58:31 -04003717 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003718 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003719 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003720 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003721 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003722 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003723 if let Some((ref at_token, ref subpat)) = self.subpat {
3724 at_token.to_tokens(tokens);
3725 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003726 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003727 }
3728 }
3729
Michael Layzell734adb42017-06-07 16:58:31 -04003730 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003731 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003732 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003733 self.path.to_tokens(tokens);
3734 self.brace_token.surround(tokens, |tokens| {
3735 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003736 // NOTE: We need a comma before the dot2 token if it is present.
3737 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003738 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003739 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003740 self.dot2_token.to_tokens(tokens);
3741 });
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 PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003747 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003748 self.path.to_tokens(tokens);
3749 self.pat.to_tokens(tokens);
3750 }
3751 }
3752
Michael Layzell734adb42017-06-07 16:58:31 -04003753 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003754 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003755 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003756 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3757 }
3758 }
3759
Michael Layzell734adb42017-06-07 16:58:31 -04003760 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003761 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003762 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003763 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003764 self.front.to_tokens(tokens);
3765 if let Some(ref dot2_token) = self.dot2_token {
3766 if !self.front.empty_or_trailing() {
3767 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003768 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003769 }
David Tolnay41871922017-12-29 01:53:45 -05003770 dot2_token.to_tokens(tokens);
3771 self.comma_token.to_tokens(tokens);
3772 if self.comma_token.is_none() && !self.back.is_empty() {
3773 // Ensure there is a comma after the .. token.
3774 <Token![,]>::default().to_tokens(tokens);
3775 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003776 }
David Tolnay41871922017-12-29 01:53:45 -05003777 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003778 });
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 PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003784 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003785 self.box_token.to_tokens(tokens);
3786 self.pat.to_tokens(tokens);
3787 }
3788 }
3789
Michael Layzell734adb42017-06-07 16:58:31 -04003790 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003791 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003792 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003793 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003794 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003795 self.pat.to_tokens(tokens);
3796 }
3797 }
3798
Michael Layzell734adb42017-06-07 16:58:31 -04003799 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003800 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003801 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003802 self.expr.to_tokens(tokens);
3803 }
3804 }
3805
Michael Layzell734adb42017-06-07 16:58:31 -04003806 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003807 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003808 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003809 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003810 match self.limits {
3811 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003812 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003813 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003814 self.hi.to_tokens(tokens);
3815 }
3816 }
3817
Michael Layzell734adb42017-06-07 16:58:31 -04003818 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003819 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003820 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003821 // XXX: This is a mess, and it will be so easy to screw it up. How
3822 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003823 self.bracket_token.surround(tokens, |tokens| {
3824 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003825
3826 // If we need a comma before the middle or standalone .. token,
3827 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003828 if !self.front.empty_or_trailing()
3829 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003830 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003831 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003832 }
3833
3834 // If we have an identifier, we always need a .. token.
3835 if self.middle.is_some() {
3836 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003837 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003838 } else if self.dot2_token.is_some() {
3839 self.dot2_token.to_tokens(tokens);
3840 }
3841
3842 // Make sure we have a comma before the back half.
3843 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003844 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003845 self.back.to_tokens(tokens);
3846 } else {
3847 self.comma_token.to_tokens(tokens);
3848 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003849 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003850 }
3851 }
3852
Michael Layzell734adb42017-06-07 16:58:31 -04003853 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003854 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003855 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003856 self.mac.to_tokens(tokens);
3857 }
3858 }
3859
3860 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003861 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003862 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003863 self.tts.to_tokens(tokens);
3864 }
3865 }
3866
3867 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003868 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003869 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003870 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003871 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003872 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003873 }
3874 self.pat.to_tokens(tokens);
3875 }
3876 }
3877
Michael Layzell734adb42017-06-07 16:58:31 -04003878 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003879 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003880 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003881 self.brace_token.surround(tokens, |tokens| {
3882 tokens.append_all(&self.stmts);
3883 });
David Tolnay42602292016-10-01 22:25:45 -07003884 }
3885 }
3886
Michael Layzell734adb42017-06-07 16:58:31 -04003887 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003888 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003889 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003890 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003891 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003892 Stmt::Item(ref item) => item.to_tokens(tokens),
3893 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003894 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003895 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003896 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003897 }
David Tolnay42602292016-10-01 22:25:45 -07003898 }
3899 }
3900 }
David Tolnay191e0582016-10-02 18:31:09 -07003901
Michael Layzell734adb42017-06-07 16:58:31 -04003902 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003903 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003904 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003905 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003906 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003907 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003908 if let Some((ref colon_token, ref ty)) = self.ty {
3909 colon_token.to_tokens(tokens);
3910 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003911 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003912 if let Some((ref eq_token, ref init)) = self.init {
3913 eq_token.to_tokens(tokens);
3914 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003915 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003916 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003917 }
3918 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003919}