blob: bbaaee749102ab07eb4fa375eb676d3f7456e70f [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 {
David Tolnay01218d12018-08-29 18:13:07 -07001011 // see https://github.com/rust-lang/rust/blob/eb8f2586e/src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001012 match *expr {
1013 Expr::Unsafe(..)
1014 | Expr::Block(..)
1015 | Expr::If(..)
1016 | Expr::IfLet(..)
1017 | Expr::Match(..)
1018 | Expr::While(..)
1019 | Expr::WhileLet(..)
1020 | Expr::Loop(..)
1021 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001022 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001023 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001024 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001025 }
1026}
1027
David Tolnayb9c8e322016-09-23 20:48:37 -07001028#[cfg(feature = "parsing")]
1029pub mod parsing {
1030 use super::*;
David Tolnay60291082018-08-28 09:54:49 -07001031 use path;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001032 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001033 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001034
David Tolnay9389c382018-08-27 09:13:37 -07001035 use parse::{Parse, ParseStream, Result};
Michael Layzell734adb42017-06-07 16:58:31 -04001036 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001037 use synom::ext::IdentExt;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001038
David Tolnaybcf26022017-12-25 22:10:52 -05001039 // When we're parsing expressions which occur before blocks, like in an if
1040 // statement's condition, we cannot parse a struct literal.
1041 //
1042 // Struct literals are ambiguous in certain positions
1043 // https://github.com/rust-lang/rfcs/pull/92
David Tolnay9389c382018-08-27 09:13:37 -07001044 #[derive(Copy, Clone)]
1045 pub struct AllowStruct(bool);
1046
David Tolnay01218d12018-08-29 18:13:07 -07001047 #[derive(Copy, Clone, PartialEq, PartialOrd)]
1048 enum Precedence {
1049 Any,
1050 Assign,
1051 Placement,
1052 Range,
1053 Or,
1054 And,
1055 Compare,
1056 BitOr,
1057 BitXor,
1058 BitAnd,
1059 Shift,
1060 Arithmetic,
1061 Term,
1062 Cast,
1063 }
1064
1065 impl Precedence {
1066 fn of(op: &BinOp) -> Self {
1067 match *op {
1068 BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic,
1069 BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term,
1070 BinOp::And(_) => Precedence::And,
1071 BinOp::Or(_) => Precedence::Or,
1072 BinOp::BitXor(_) => Precedence::BitXor,
1073 BinOp::BitAnd(_) => Precedence::BitAnd,
1074 BinOp::BitOr(_) => Precedence::BitOr,
1075 BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift,
1076 BinOp::Eq(_) | BinOp::Lt(_) | BinOp::Le(_) | BinOp::Ne(_) | BinOp::Ge(_) | BinOp::Gt(_) => Precedence::Compare,
1077 BinOp::AddEq(_) | BinOp::SubEq(_) | BinOp::MulEq(_) | BinOp::DivEq(_) | BinOp::RemEq(_) | BinOp::BitXorEq(_) | BinOp::BitAndEq(_) | BinOp::BitOrEq(_) | BinOp::ShlEq(_) | BinOp::ShrEq(_) => Precedence::Assign,
1078 }
1079 }
1080 }
1081
David Tolnay9389c382018-08-27 09:13:37 -07001082 impl Parse for Expr {
1083 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001084 ambiguous_expr(input, AllowStruct(true))
Alex Crichton954046c2017-05-30 21:49:42 -07001085 }
1086 }
1087
Michael Layzell734adb42017-06-07 16:58:31 -04001088 #[cfg(feature = "full")]
David Tolnay9fb0aed2018-08-27 10:23:12 -07001089 fn expr_no_struct(input: ParseStream) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001090 ambiguous_expr(input, AllowStruct(false))
David Tolnay9fb0aed2018-08-27 10:23:12 -07001091 }
David Tolnayaf2557e2016-10-24 11:52:21 -07001092
David Tolnay01218d12018-08-29 18:13:07 -07001093 #[cfg(feature = "full")]
David Tolnay7d2e1db2018-08-30 11:49:04 -07001094 fn parse_expr(input: ParseStream, mut lhs: Expr, allow_struct: AllowStruct, base: Precedence) -> Result<Expr> {
David Tolnay01218d12018-08-29 18:13:07 -07001095 loop {
1096 if input.fork().parse::<BinOp>().ok().map_or(false, |op| Precedence::of(&op) >= base) {
1097 let op: BinOp = input.parse()?;
1098 let precedence = Precedence::of(&op);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001099 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001100 loop {
1101 let next = peek_precedence(input);
1102 if next > precedence || next == precedence && precedence == Precedence::Assign {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001103 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001104 } else {
1105 break;
1106 }
1107 }
1108 lhs = Expr::Binary(ExprBinary {
1109 attrs: Vec::new(),
1110 left: Box::new(lhs),
1111 op: op,
1112 right: Box::new(rhs),
1113 });
1114 } else if Precedence::Assign >= base && input.peek(Token![=]) && !input.peek(Token![==]) && !input.peek(Token![=>]) {
1115 let eq_token: Token![=] = input.parse()?;
David Tolnay7d2e1db2018-08-30 11:49:04 -07001116 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001117 loop {
1118 let next = peek_precedence(input);
1119 if next >= Precedence::Assign {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001120 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001121 } else {
1122 break;
1123 }
1124 }
1125 lhs = Expr::Assign(ExprAssign {
1126 attrs: Vec::new(),
1127 left: Box::new(lhs),
1128 eq_token: eq_token,
1129 right: Box::new(rhs),
1130 });
1131 } else if Precedence::Placement >= base && input.peek(Token![<-]) {
1132 let arrow_token: Token![<-] = input.parse()?;
David Tolnay7d2e1db2018-08-30 11:49:04 -07001133 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001134 loop {
1135 let next = peek_precedence(input);
1136 if next > Precedence::Placement {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001137 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001138 } else {
1139 break;
1140 }
1141 }
1142 lhs = Expr::InPlace(ExprInPlace {
1143 attrs: Vec::new(),
1144 place: Box::new(lhs),
1145 arrow_token: arrow_token,
1146 value: Box::new(rhs),
1147 });
1148 } else if Precedence::Range >= base && input.peek(Token![..]) {
1149 let limits: RangeLimits = input.parse()?;
1150 let rhs = if input.is_empty()
1151 || input.peek(Token![,])
1152 || input.peek(Token![;])
1153 || !allow_struct.0 && input.peek(token::Brace)
1154 {
1155 None
1156 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001157 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001158 loop {
1159 let next = peek_precedence(input);
1160 if next > Precedence::Range {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001161 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001162 } else {
1163 break;
1164 }
1165 }
1166 Some(rhs)
1167 };
1168 lhs = Expr::Range(ExprRange {
1169 attrs: Vec::new(),
1170 from: Some(Box::new(lhs)),
1171 limits: limits,
1172 to: rhs.map(Box::new),
1173 });
1174 } else if Precedence::Cast >= base && input.peek(Token![as]) {
1175 let as_token: Token![as] = input.parse()?;
1176 let ty = input.call(Type::without_plus)?;
1177 lhs = Expr::Cast(ExprCast {
1178 attrs: Vec::new(),
1179 expr: Box::new(lhs),
1180 as_token: as_token,
1181 ty: Box::new(ty),
1182 });
1183 } else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) {
1184 let colon_token: Token![:] = input.parse()?;
1185 let ty = input.call(Type::without_plus)?;
1186 lhs = Expr::Type(ExprType {
1187 attrs: Vec::new(),
1188 expr: Box::new(lhs),
1189 colon_token: colon_token,
1190 ty: Box::new(ty),
1191 });
1192 } else {
1193 break;
1194 }
1195 }
1196 Ok(lhs)
1197 }
1198
David Tolnay3e541292018-08-30 11:42:15 -07001199 #[cfg(not(feature = "full"))]
David Tolnay7d2e1db2018-08-30 11:49:04 -07001200 fn parse_expr(input: ParseStream, mut lhs: Expr, allow_struct: AllowStruct, base: Precedence) -> Result<Expr> {
David Tolnay3e541292018-08-30 11:42:15 -07001201 loop {
1202 if input.fork().parse::<BinOp>().ok().map_or(false, |op| Precedence::of(&op) >= base) {
1203 let op: BinOp = input.parse()?;
1204 let precedence = Precedence::of(&op);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001205 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay3e541292018-08-30 11:42:15 -07001206 loop {
1207 let next = peek_precedence(input);
1208 if next > precedence || next == precedence && precedence == Precedence::Assign {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001209 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay3e541292018-08-30 11:42:15 -07001210 } else {
1211 break;
1212 }
1213 }
1214 lhs = Expr::Binary(ExprBinary {
1215 attrs: Vec::new(),
1216 left: Box::new(lhs),
1217 op: op,
1218 right: Box::new(rhs),
1219 });
1220 } else if Precedence::Cast >= base && input.peek(Token![as]) {
1221 let as_token: Token![as] = input.parse()?;
1222 let ty = input.call(Type::without_plus)?;
1223 lhs = Expr::Cast(ExprCast {
1224 attrs: Vec::new(),
1225 expr: Box::new(lhs),
1226 as_token: as_token,
1227 ty: Box::new(ty),
1228 });
1229 } else {
1230 break;
1231 }
1232 }
1233 Ok(lhs)
1234 }
1235
David Tolnay01218d12018-08-29 18:13:07 -07001236 fn peek_precedence(input: ParseStream) -> Precedence {
1237 if let Ok(op) = input.fork().parse() {
1238 Precedence::of(&op)
David Tolnay3e541292018-08-30 11:42:15 -07001239 } else if input.peek(Token![=]) && !input.peek(Token![=>]) {
David Tolnay01218d12018-08-29 18:13:07 -07001240 Precedence::Assign
1241 } else if input.peek(Token![<-]) {
1242 Precedence::Placement
1243 } else if input.peek(Token![..]) {
1244 Precedence::Range
1245 } else if input.peek(Token![as]) || input.peek(Token![:]) && !input.peek(Token![::]) {
1246 Precedence::Cast
1247 } else {
1248 Precedence::Any
1249 }
1250 }
1251
David Tolnaybcf26022017-12-25 22:10:52 -05001252 // Parse an arbitrary expression.
David Tolnay60291082018-08-28 09:54:49 -07001253 fn ambiguous_expr(
1254 input: ParseStream,
1255 allow_struct: AllowStruct,
David Tolnay60291082018-08-28 09:54:49 -07001256 ) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001257 let lhs = unary_expr(input, allow_struct)?;
1258 parse_expr(input, lhs, allow_struct, Precedence::Any)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 }
1260
David Tolnaybcf26022017-12-25 22:10:52 -05001261 // <UnOp> <trailer>
1262 // & <trailer>
1263 // &mut <trailer>
1264 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001265 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001266 fn unary_expr(
1267 input: ParseStream,
1268 allow_struct: AllowStruct,
David Tolnay60291082018-08-28 09:54:49 -07001269 ) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001270 let ahead = input.fork();
1271 ahead.call(Attribute::parse_outer)?;
1272 if ahead.peek(Token![&])
1273 || ahead.peek(Token![box])
1274 || ahead.peek(Token![*])
1275 || ahead.peek(Token![!])
1276 || ahead.peek(Token![-])
1277 {
1278 let attrs = input.call(Attribute::parse_outer)?;
1279 if input.peek(Token![&]) {
1280 Ok(Expr::Reference(ExprReference {
1281 attrs: attrs,
1282 and_token: input.parse()?,
1283 mutability: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001284 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001285 }))
1286 } else if input.peek(Token![box]) {
1287 Ok(Expr::Box(ExprBox {
1288 attrs: attrs,
1289 box_token: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001290 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001291 }))
1292 } else {
1293 Ok(Expr::Unary(ExprUnary {
1294 attrs: attrs,
1295 op: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001296 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001297 }))
1298 }
1299 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001300 trailer_expr(input, allow_struct)
David Tolnay377263f2018-08-27 13:48:30 -07001301 }
1302 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001303
Michael Layzell734adb42017-06-07 16:58:31 -04001304 // XXX: This duplication is ugly
1305 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001306 fn unary_expr(
1307 input: ParseStream,
1308 allow_struct: AllowStruct,
David Tolnay60291082018-08-28 09:54:49 -07001309 ) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001310 let ahead = input.fork();
1311 ahead.call(Attribute::parse_outer)?;
1312 if ahead.peek(Token![*]) || ahead.peek(Token![!]) || ahead.peek(Token![-]) {
1313 Ok(Expr::Unary(ExprUnary {
1314 attrs: input.call(Attribute::parse_outer)?,
1315 op: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001316 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001317 }))
1318 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001319 trailer_expr(input, allow_struct)
David Tolnay377263f2018-08-27 13:48:30 -07001320 }
1321 }
Michael Layzell734adb42017-06-07 16:58:31 -04001322
David Tolnayd997aef2018-07-21 18:42:31 -07001323 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001324 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1325 let mut outer = Vec::new();
1326 let mut inner = Vec::new();
1327 for attr in mem::replace(attrs, Vec::new()) {
1328 match attr.style {
1329 AttrStyle::Outer => outer.push(attr),
1330 AttrStyle::Inner(_) => inner.push(attr),
1331 }
1332 }
1333 *attrs = inner;
1334 outer
1335 }
1336
David Tolnaybcf26022017-12-25 22:10:52 -05001337 // <atom> (..<args>) ...
1338 // <atom> . <ident> (..<args>) ...
1339 // <atom> . <ident> ...
1340 // <atom> . <lit> ...
1341 // <atom> [ <expr> ] ...
1342 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001343 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001344 fn trailer_expr(
1345 input: ParseStream,
1346 allow_struct: AllowStruct,
David Tolnay60291082018-08-28 09:54:49 -07001347 ) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001348 let mut e = atom_expr(input, allow_struct)?;
David Tolnay1501f7e2018-08-27 14:21:03 -07001349
1350 let mut attrs = e.replace_attrs(Vec::new());
1351 let outer_attrs = take_outer(&mut attrs);
1352 e.replace_attrs(attrs);
1353
David Tolnay01218d12018-08-29 18:13:07 -07001354 e = trailer_helper(input, e)?;
1355
1356 let mut attrs = outer_attrs;
1357 attrs.extend(e.replace_attrs(Vec::new()));
1358 e.replace_attrs(attrs);
1359 Ok(e)
1360 }
1361
1362 #[cfg(feature = "full")]
1363 fn trailer_helper(input: ParseStream, mut e: Expr) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001364 loop {
1365 if input.peek(token::Paren) {
1366 let content;
1367 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001368 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001369 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001370 paren_token: parenthesized!(content in input),
1371 args: content.parse_terminated(<Expr as Parse>::parse)?,
1372 });
1373 } else if input.peek(Token![.]) && !input.peek(Token![..]) {
1374 let dot_token: Token![.] = input.parse()?;
1375 let member: Member = input.parse()?;
1376 let turbofish = if member.is_named() && input.peek(Token![::]) {
1377 Some(MethodTurbofish {
1378 colon2_token: input.parse()?,
1379 lt_token: input.parse()?,
1380 args: {
1381 let mut args = Punctuated::new();
1382 loop {
1383 if input.peek(Token![>]) {
1384 break;
1385 }
David Tolnay310b3262018-08-30 15:33:00 -07001386 let value = input.call(generic_method_argument)?;
David Tolnay1501f7e2018-08-27 14:21:03 -07001387 args.push_value(value);
1388 if input.peek(Token![>]) {
1389 break;
1390 }
1391 let punct = input.parse()?;
1392 args.push_punct(punct);
1393 }
1394 args
1395 },
1396 gt_token: input.parse()?,
1397 })
1398 } else {
1399 None
1400 };
1401
1402 if turbofish.is_some() || input.peek(token::Paren) {
1403 if let Member::Named(method) = member {
1404 let content;
1405 e = Expr::MethodCall(ExprMethodCall {
1406 attrs: Vec::new(),
1407 receiver: Box::new(e),
1408 dot_token: dot_token,
1409 method: method,
1410 turbofish: turbofish,
1411 paren_token: parenthesized!(content in input),
1412 args: content.parse_terminated(<Expr as Parse>::parse)?,
1413 });
1414 continue;
1415 }
1416 }
1417
1418 e = Expr::Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001419 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001420 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001421 dot_token: dot_token,
David Tolnay85b69a42017-12-27 20:43:10 -05001422 member: member,
David Tolnay1501f7e2018-08-27 14:21:03 -07001423 });
1424 } else if input.peek(token::Bracket) {
1425 let content;
1426 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001427 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001428 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001429 bracket_token: bracketed!(content in input),
1430 index: content.parse()?,
1431 });
1432 } else if input.peek(Token![?]) {
1433 e = Expr::Try(ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001434 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001435 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001436 question_token: input.parse()?,
1437 });
1438 } else {
1439 break;
1440 }
1441 }
David Tolnay1501f7e2018-08-27 14:21:03 -07001442 Ok(e)
1443 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001444
Michael Layzell734adb42017-06-07 16:58:31 -04001445 // XXX: Duplication == ugly
1446 #[cfg(not(feature = "full"))]
David Tolnay60291082018-08-28 09:54:49 -07001447 fn trailer_expr(
1448 input: ParseStream,
1449 allow_struct: AllowStruct,
David Tolnay60291082018-08-28 09:54:49 -07001450 ) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001451 let mut e = atom_expr(input, allow_struct)?;
David Tolnay1501f7e2018-08-27 14:21:03 -07001452
1453 loop {
1454 if input.peek(token::Paren) {
1455 let content;
1456 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001457 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001458 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001459 paren_token: parenthesized!(content in input),
1460 args: content.parse_terminated(<Expr as Parse>::parse)?,
1461 });
1462 } else if input.peek(Token![.]) {
1463 e = Expr::Field(ExprField {
David Tolnayd5147742018-06-30 10:09:52 -07001464 attrs: Vec::new(),
1465 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001466 dot_token: input.parse()?,
1467 member: input.parse()?,
1468 });
1469 } else if input.peek(token::Bracket) {
1470 let content;
1471 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001472 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001473 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001474 bracket_token: bracketed!(content in input),
1475 index: content.parse()?,
1476 });
1477 } else {
1478 break;
1479 }
1480 }
1481
1482 Ok(e)
1483 }
Michael Layzell734adb42017-06-07 16:58:31 -04001484
David Tolnaya454c8f2018-01-07 01:01:10 -08001485 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001486 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001487 #[cfg(feature = "full")]
David Tolnay7d2e1db2018-08-30 11:49:04 -07001488 fn atom_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay6e1e5052018-08-30 10:21:48 -07001489 if input.peek(token::Group) {
David Tolnay310b3262018-08-30 15:33:00 -07001490 return input.call(expr_group).map(Expr::Group);
David Tolnay6e1e5052018-08-30 10:21:48 -07001491 }
1492
1493 let mut attrs = input.call(Attribute::parse_outer)?;
1494
1495 let mut expr = if input.peek(token::Group) {
David Tolnay310b3262018-08-30 15:33:00 -07001496 Expr::Group(input.call(expr_group)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001497 } else if input.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -07001498 Expr::Lit(input.call(expr_lit)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001499 } else if input.peek(Token![async])
1500 && (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace))
1501 {
David Tolnay310b3262018-08-30 15:33:00 -07001502 Expr::Async(input.call(expr_async)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001503 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001504 Expr::TryBlock(input.call(expr_try_block)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001505 } else if input.peek(Token![|])
1506 || input.peek(Token![async]) && (input.peek2(Token![|]) || input.peek2(Token![move]))
1507 || input.peek(Token![static])
1508 || input.peek(Token![move])
1509 {
1510 Expr::Closure(expr_closure(input, allow_struct)?)
1511 } else if input.peek(Ident)
1512 || input.peek(Token![::])
1513 || input.peek(Token![<])
1514 || input.peek(Token![self])
1515 || input.peek(Token![Self])
1516 || input.peek(Token![super])
1517 || input.peek(Token![extern])
1518 || input.peek(Token![crate])
1519 {
1520 path_or_macro_or_struct(input, allow_struct)?
1521 } else if input.peek(token::Paren) {
1522 paren_or_tuple(input)?
1523 } else if input.peek(Token![break]) {
1524 Expr::Break(expr_break(input, allow_struct)?)
1525 } else if input.peek(Token![continue]) {
David Tolnay310b3262018-08-30 15:33:00 -07001526 Expr::Continue(input.call(expr_continue)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001527 } else if input.peek(Token![return]) {
1528 Expr::Return(expr_ret(input, allow_struct)?)
1529 } else if input.peek(token::Bracket) {
1530 array_or_repeat(input)?
1531 } else if input.peek(Token![if]) {
1532 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001533 Expr::IfLet(input.call(expr_if_let)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001534 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001535 Expr::If(input.call(expr_if)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001536 }
1537 } else if input.peek(Token![while]) {
1538 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001539 Expr::WhileLet(input.call(expr_while_let)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001540 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001541 Expr::While(input.call(expr_while)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001542 }
1543 } else if input.peek(Token![for]) {
David Tolnay310b3262018-08-30 15:33:00 -07001544 Expr::ForLoop(input.call(expr_for_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001545 } else if input.peek(Token![loop]) {
David Tolnay310b3262018-08-30 15:33:00 -07001546 Expr::Loop(input.call(expr_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001547 } else if input.peek(Token![match]) {
David Tolnay310b3262018-08-30 15:33:00 -07001548 Expr::Match(input.call(expr_match)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001549 } else if input.peek(Token![yield]) {
David Tolnay310b3262018-08-30 15:33:00 -07001550 Expr::Yield(input.call(expr_yield)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001551 } else if input.peek(Token![unsafe]) {
David Tolnay310b3262018-08-30 15:33:00 -07001552 Expr::Unsafe(input.call(expr_unsafe)?)
David Tolnay7d2e1db2018-08-30 11:49:04 -07001553 } else if input.peek(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001554 Expr::Block(input.call(expr_block)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001555 } else if input.peek(Token![..]) {
1556 Expr::Range(expr_range(input, allow_struct)?)
1557 } else if input.peek(Lifetime) {
1558 let the_label: Label = input.parse()?;
1559 let mut expr = if input.peek(Token![while]) {
1560 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001561 Expr::WhileLet(input.call(expr_while_let)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001562 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001563 Expr::While(input.call(expr_while)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001564 }
1565 } else if input.peek(Token![for]) {
David Tolnay310b3262018-08-30 15:33:00 -07001566 Expr::ForLoop(input.call(expr_for_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001567 } else if input.peek(Token![loop]) {
David Tolnay310b3262018-08-30 15:33:00 -07001568 Expr::Loop(input.call(expr_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001569 } else if input.peek(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001570 Expr::Block(input.call(expr_block)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001571 } else {
1572 return Err(input.error("expected loop or block expression"));
1573 };
1574 match expr {
1575 Expr::WhileLet(ExprWhileLet { ref mut label, .. }) |
1576 Expr::While(ExprWhile { ref mut label, .. }) |
1577 Expr::ForLoop(ExprForLoop { ref mut label, .. }) |
1578 Expr::Loop(ExprLoop { ref mut label, .. }) |
1579 Expr::Block(ExprBlock { ref mut label, .. }) => *label = Some(the_label),
1580 _ => unreachable!(),
1581 }
1582 expr
1583 } else {
1584 return Err(input.error("expected expression"));
1585 };
1586
1587 attrs.extend(expr.replace_attrs(Vec::new()));
1588 expr.replace_attrs(attrs);
1589 Ok(expr)
1590 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001591
Michael Layzell734adb42017-06-07 16:58:31 -04001592 #[cfg(not(feature = "full"))]
David Tolnay7d2e1db2018-08-30 11:49:04 -07001593 fn atom_expr(input: ParseStream, _allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay6e1e5052018-08-30 10:21:48 -07001594 if input.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -07001595 input.call(expr_lit).map(Expr::Lit)
David Tolnay6e1e5052018-08-30 10:21:48 -07001596 } else if input.peek(token::Paren) {
David Tolnay310b3262018-08-30 15:33:00 -07001597 input.call(expr_paren).map(Expr::Paren)
David Tolnay6e1e5052018-08-30 10:21:48 -07001598 } else if input.peek(Ident)
1599 || input.peek(Token![::])
1600 || input.peek(Token![<])
1601 || input.peek(Token![self])
1602 || input.peek(Token![Self])
1603 || input.peek(Token![super])
1604 || input.peek(Token![extern])
1605 || input.peek(Token![crate])
1606 {
1607 input.parse().map(Expr::Path)
1608 } else {
1609 Err(input.error("unsupported expression; enable syn's features=[\"full\"]"))
1610 }
1611 }
1612
1613 #[cfg(feature = "full")]
1614 fn path_or_macro_or_struct(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
1615 let expr: ExprPath = input.parse()?;
1616 if expr.qself.is_some() {
1617 return Ok(Expr::Path(expr));
1618 }
1619
1620 if input.peek(Token![!]) && !input.peek(Token![!=]) {
1621 let mut contains_arguments = false;
1622 for segment in &expr.path.segments {
1623 match segment.arguments {
1624 PathArguments::None => {}
1625 PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
1626 contains_arguments = true;
1627 }
1628 }
1629 }
1630
1631 if !contains_arguments {
1632 let bang_token: Token![!] = input.parse()?;
1633 let (delimiter, tts) = mac::parse_delimiter(input)?;
1634 return Ok(Expr::Macro(ExprMacro {
1635 attrs: Vec::new(),
1636 mac: Macro {
1637 path: expr.path,
1638 bang_token: bang_token,
1639 delimiter: delimiter,
1640 tts: tts,
1641 },
1642 }));
1643 }
1644 }
1645
1646 if allow_struct.0 && input.peek(token::Brace) {
1647 let outer_attrs = Vec::new();
1648 expr_struct_helper(input, outer_attrs, expr.path).map(Expr::Struct)
1649 } else {
1650 Ok(Expr::Path(expr))
1651 }
1652 }
1653
1654 #[cfg(feature = "full")]
1655 fn paren_or_tuple(input: ParseStream) -> Result<Expr> {
1656 let content;
1657 let paren_token = parenthesized!(content in input);
1658 let inner_attrs = content.call(Attribute::parse_inner)?;
1659 if content.is_empty() {
1660 return Ok(Expr::Tuple(ExprTuple {
1661 attrs: inner_attrs,
1662 paren_token: paren_token,
1663 elems: Punctuated::new(),
1664 }));
1665 }
1666
1667 let first: Expr = content.parse()?;
1668 if content.is_empty() {
1669 return Ok(Expr::Paren(ExprParen {
1670 attrs: inner_attrs,
1671 paren_token: paren_token,
1672 expr: Box::new(first),
1673 }));
1674 }
1675
1676 let mut elems = Punctuated::new();
1677 elems.push_value(first);
1678 while !content.is_empty() {
1679 let punct = content.parse()?;
1680 elems.push_punct(punct);
1681 if content.is_empty() {
1682 break;
1683 }
1684 let value = content.parse()?;
1685 elems.push_value(value);
1686 }
1687 Ok(Expr::Tuple(ExprTuple {
1688 attrs: inner_attrs,
1689 paren_token: paren_token,
1690 elems: elems,
1691 }))
1692 }
1693
1694 #[cfg(feature = "full")]
1695 fn array_or_repeat(input: ParseStream) -> Result<Expr> {
1696 let content;
1697 let bracket_token = bracketed!(content in input);
1698 let inner_attrs = content.call(Attribute::parse_inner)?;
1699 if content.is_empty() {
1700 return Ok(Expr::Array(ExprArray {
1701 attrs: inner_attrs,
1702 bracket_token: bracket_token,
1703 elems: Punctuated::new(),
1704 }));
1705 }
1706
1707 let first: Expr = content.parse()?;
1708 if content.is_empty() || content.peek(Token![,]) {
1709 let mut elems = Punctuated::new();
1710 elems.push_value(first);
1711 while !content.is_empty() {
1712 let punct = content.parse()?;
1713 elems.push_punct(punct);
1714 if content.is_empty() {
1715 break;
1716 }
1717 let value = content.parse()?;
1718 elems.push_value(value);
1719 }
1720 Ok(Expr::Array(ExprArray {
1721 attrs: inner_attrs,
1722 bracket_token: bracket_token,
1723 elems: elems,
1724 }))
1725 } else if content.peek(Token![;]) {
1726 let semi_token: Token![;] = content.parse()?;
1727 let len: Expr = content.parse()?;
1728 Ok(Expr::Repeat(ExprRepeat {
1729 attrs: inner_attrs,
1730 bracket_token: bracket_token,
1731 expr: Box::new(first),
1732 semi_token: semi_token,
1733 len: Box::new(len),
1734 }))
1735 } else {
1736 Err(content.error("expected `,` or `;`"))
1737 }
1738 }
Michael Layzell734adb42017-06-07 16:58:31 -04001739
Michael Layzell734adb42017-06-07 16:58:31 -04001740 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07001741 fn expr_early(input: ParseStream) -> Result<Expr> {
1742 let mut attrs = input.call(Attribute::parse_outer)?;
1743 let mut expr = if input.peek(Token![if]) {
1744 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001745 Expr::IfLet(input.call(expr_if_let)?)
David Tolnay01218d12018-08-29 18:13:07 -07001746 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001747 Expr::If(input.call(expr_if)?)
David Tolnay01218d12018-08-29 18:13:07 -07001748 }
1749 } else if input.peek(Token![while]) {
1750 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001751 Expr::WhileLet(input.call(expr_while_let)?)
David Tolnay01218d12018-08-29 18:13:07 -07001752 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001753 Expr::While(input.call(expr_while)?)
David Tolnay01218d12018-08-29 18:13:07 -07001754 }
1755 } else if input.peek(Token![for]) {
David Tolnay310b3262018-08-30 15:33:00 -07001756 Expr::ForLoop(input.call(expr_for_loop)?)
David Tolnay01218d12018-08-29 18:13:07 -07001757 } else if input.peek(Token![loop]) {
David Tolnay310b3262018-08-30 15:33:00 -07001758 Expr::Loop(input.call(expr_loop)?)
David Tolnay01218d12018-08-29 18:13:07 -07001759 } else if input.peek(Token![match]) {
David Tolnay310b3262018-08-30 15:33:00 -07001760 Expr::Match(input.call(expr_match)?)
David Tolnay01218d12018-08-29 18:13:07 -07001761 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001762 Expr::TryBlock(input.call(expr_try_block)?)
David Tolnay01218d12018-08-29 18:13:07 -07001763 } else if input.peek(Token![unsafe]) {
David Tolnay310b3262018-08-30 15:33:00 -07001764 Expr::Unsafe(input.call(expr_unsafe)?)
David Tolnay01218d12018-08-29 18:13:07 -07001765 } else if input.peek(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001766 Expr::Block(input.call(expr_block)?)
David Tolnay01218d12018-08-29 18:13:07 -07001767 } else {
1768 let allow_struct = AllowStruct(true);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001769 let mut expr = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001770
1771 attrs.extend(expr.replace_attrs(Vec::new()));
1772 expr.replace_attrs(attrs);
1773
David Tolnay7d2e1db2018-08-30 11:49:04 -07001774 return parse_expr(input, expr, allow_struct, Precedence::Any);
David Tolnay01218d12018-08-29 18:13:07 -07001775 };
1776
1777 if input.peek(Token![.]) || input.peek(Token![?]) {
1778 expr = trailer_helper(input, expr)?;
1779
1780 attrs.extend(expr.replace_attrs(Vec::new()));
1781 expr.replace_attrs(attrs);
1782
1783 let allow_struct = AllowStruct(true);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001784 return parse_expr(input, expr, allow_struct, Precedence::Any);
David Tolnay01218d12018-08-29 18:13:07 -07001785 }
1786
1787 attrs.extend(expr.replace_attrs(Vec::new()));
1788 expr.replace_attrs(attrs);
1789 Ok(expr)
1790 }
Michael Layzell35418782017-06-07 09:20:25 -04001791
David Tolnay310b3262018-08-30 15:33:00 -07001792 pub fn expr_lit(input: ParseStream) -> Result<ExprLit> {
1793 Ok(ExprLit {
1794 attrs: Vec::new(),
1795 lit: input.parse()?,
1796 })
David Tolnay8c91b882017-12-28 23:04:32 -05001797 }
1798
1799 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001800 fn expr_group(input: ParseStream) -> Result<ExprGroup> {
1801 let content;
1802 Ok(ExprGroup {
1803 attrs: Vec::new(),
1804 group_token: grouped!(content in input),
1805 expr: content.parse()?,
1806 })
1807 }
1808
1809 #[cfg(not(feature = "full"))]
1810 fn expr_paren(input: ParseStream) -> Result<ExprParen> {
1811 let content;
1812 Ok(ExprParen {
1813 attrs: Vec::new(),
1814 paren_token: parenthesized!(content in input),
1815 expr: content.parse()?,
1816 })
David Tolnay8c91b882017-12-28 23:04:32 -05001817 }
1818
David Tolnaye98775f2017-12-28 23:17:00 -05001819 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001820 fn generic_method_argument(input: ParseStream) -> Result<GenericMethodArgument> {
David Tolnayd60cfec2017-12-29 00:21:38 -05001821 // TODO parse const generics as well
David Tolnay310b3262018-08-30 15:33:00 -07001822 input
1823 .parse_synom(ty_no_eq_after)
1824 .map(GenericMethodArgument::Type)
David Tolnayd60cfec2017-12-29 00:21:38 -05001825 }
1826
1827 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001828 fn expr_if_let(input: ParseStream) -> Result<ExprIfLet> {
1829 Ok(ExprIfLet {
1830 attrs: Vec::new(),
1831 if_token: input.parse()?,
1832 let_token: input.parse()?,
1833 pats: {
1834 let mut pats = Punctuated::new();
1835 let value: Pat = input.parse()?;
1836 pats.push_value(value);
1837 while input.peek(Token![|])
1838 && !input.peek(Token![||])
1839 && !input.peek(Token![|=])
1840 {
1841 let punct = input.parse()?;
1842 pats.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07001843 let value: Pat = input.parse()?;
1844 pats.push_value(value);
David Tolnay310b3262018-08-30 15:33:00 -07001845 }
1846 pats
1847 },
1848 eq_token: input.parse()?,
1849 expr: Box::new(input.call(expr_no_struct)?),
1850 then_branch: input.parse()?,
1851 else_branch: {
1852 if input.peek(Token![else]) {
1853 Some(input.call(else_block)?)
1854 } else {
1855 None
1856 }
1857 },
1858 })
David Tolnay29f9ce12016-10-02 20:58:40 -07001859 }
1860
Michael Layzell734adb42017-06-07 16:58:31 -04001861 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001862 fn expr_if(input: ParseStream) -> Result<ExprIf> {
1863 Ok(ExprIf {
1864 attrs: Vec::new(),
1865 if_token: input.parse()?,
1866 cond: Box::new(input.call(expr_no_struct)?),
1867 then_branch: input.parse()?,
1868 else_branch: {
1869 if input.peek(Token![else]) {
1870 Some(input.call(else_block)?)
1871 } else {
1872 None
1873 }
1874 },
1875 })
Alex Crichton954046c2017-05-30 21:49:42 -07001876 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001877
Michael Layzell734adb42017-06-07 16:58:31 -04001878 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001879 fn else_block(input: ParseStream) -> Result<(Token![else], Box<Expr>)> {
1880 let else_token: Token![else] = input.parse()?;
1881
1882 let lookahead = input.lookahead1();
1883 let else_branch = if input.peek(Token![if]) {
1884 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001885 input.call(expr_if_let).map(Expr::IfLet)?
David Tolnay60291082018-08-28 09:54:49 -07001886 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001887 input.call(expr_if).map(Expr::If)?
David Tolnay60291082018-08-28 09:54:49 -07001888 }
1889 } else if input.peek(token::Brace) {
1890 Expr::Block(ExprBlock {
1891 attrs: Vec::new(),
1892 label: None,
1893 block: input.parse()?,
1894 })
1895 } else {
1896 return Err(lookahead.error());
1897 };
1898
1899 Ok((else_token, Box::new(else_branch)))
1900 }
David Tolnay939766a2016-09-23 23:48:12 -07001901
Michael Layzell734adb42017-06-07 16:58:31 -04001902 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001903 fn expr_for_loop(input: ParseStream) -> Result<ExprForLoop> {
1904 let label: Option<Label> = input.parse()?;
1905 let for_token: Token![for] = input.parse()?;
1906 let pat: Pat = input.parse()?;
1907 let in_token: Token![in] = input.parse()?;
1908 let expr: Expr = input.call(expr_no_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07001909
David Tolnay310b3262018-08-30 15:33:00 -07001910 let content;
1911 let brace_token = braced!(content in input);
1912 let inner_attrs = content.call(Attribute::parse_inner)?;
1913 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07001914
David Tolnay310b3262018-08-30 15:33:00 -07001915 Ok(ExprForLoop {
1916 attrs: inner_attrs,
1917 label: label,
1918 for_token: for_token,
1919 pat: Box::new(pat),
1920 in_token: in_token,
1921 expr: Box::new(expr),
1922 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07001923 brace_token: brace_token,
David Tolnay310b3262018-08-30 15:33:00 -07001924 stmts: stmts,
1925 },
1926 })
Alex Crichton954046c2017-05-30 21:49:42 -07001927 }
David Tolnay1978c672016-10-27 22:05:52 -07001928
Michael Layzell734adb42017-06-07 16:58:31 -04001929 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001930 fn expr_loop(input: ParseStream) -> Result<ExprLoop> {
1931 let label: Option<Label> = input.parse()?;
1932 let loop_token: Token![loop] = input.parse()?;
1933
1934 let content;
1935 let brace_token = braced!(content in input);
1936 let inner_attrs = content.call(Attribute::parse_inner)?;
1937 let stmts = content.call(Block::parse_within)?;
1938
1939 Ok(ExprLoop {
1940 attrs: inner_attrs,
1941 label: label,
1942 loop_token: loop_token,
1943 body: Block {
1944 brace_token: brace_token,
1945 stmts: stmts,
1946 },
1947 })
Alex Crichton954046c2017-05-30 21:49:42 -07001948 }
Arnavion02ef13f2017-04-25 00:54:31 -07001949
Michael Layzell734adb42017-06-07 16:58:31 -04001950 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001951 fn expr_match(input: ParseStream) -> Result<ExprMatch> {
1952 let match_token: Token![match] = input.parse()?;
1953 let expr = expr_no_struct(input)?;
1954
1955 let content;
1956 let brace_token = braced!(content in input);
1957 let inner_attrs = content.call(Attribute::parse_inner)?;
1958
1959 let mut arms = Vec::new();
1960 while !content.is_empty() {
1961 arms.push(content.call(match_arm)?);
1962 }
1963
1964 Ok(ExprMatch {
1965 attrs: inner_attrs,
1966 match_token: match_token,
1967 expr: Box::new(expr),
1968 brace_token: brace_token,
1969 arms: arms,
1970 })
1971 }
1972
1973 #[cfg(feature = "full")]
1974 fn expr_try_block(input: ParseStream) -> Result<ExprTryBlock> {
1975 Ok(ExprTryBlock {
1976 attrs: Vec::new(),
1977 try_token: input.parse()?,
1978 block: input.parse()?,
1979 })
1980 }
1981
1982 #[cfg(feature = "full")]
1983 fn expr_yield(input: ParseStream) -> Result<ExprYield> {
1984 Ok(ExprYield {
1985 attrs: Vec::new(),
1986 yield_token: input.parse()?,
1987 expr: {
1988 if !input.is_empty() && !input.peek(Token![,]) && !input.peek(Token![;]) {
1989 Some(input.parse()?)
1990 } else {
1991 None
1992 }
1993 },
1994 })
1995 }
1996
1997 #[cfg(feature = "full")]
1998 fn match_arm(input: ParseStream) -> Result<Arm> {
1999 let requires_comma;
2000 Ok(Arm {
2001 attrs: input.call(Attribute::parse_outer)?,
2002 leading_vert: input.parse()?,
2003 pats: {
2004 let mut pats = Punctuated::new();
2005 let value: Pat = input.parse()?;
2006 pats.push_value(value);
2007 loop {
2008 if !input.peek(Token![|]) {
2009 break;
David Tolnay60291082018-08-28 09:54:49 -07002010 }
David Tolnay310b3262018-08-30 15:33:00 -07002011 let punct = input.parse()?;
2012 pats.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002013 let value: Pat = input.parse()?;
2014 pats.push_value(value);
David Tolnay310b3262018-08-30 15:33:00 -07002015 }
2016 pats
2017 },
2018 guard: {
2019 if input.peek(Token![if]) {
2020 let if_token: Token![if] = input.parse()?;
2021 let guard: Expr = input.parse()?;
2022 Some((if_token, Box::new(guard)))
2023 } else {
2024 None
2025 }
2026 },
2027 fat_arrow_token: input.parse()?,
2028 body: {
2029 let body = input.call(expr_early)?;
2030 requires_comma = arm_expr_requires_comma(&body);
2031 Box::new(body)
2032 },
2033 comma: {
2034 if requires_comma && !input.is_empty() {
2035 Some(input.parse()?)
2036 } else {
2037 input.parse()?
2038 }
2039 },
2040 })
Alex Crichton954046c2017-05-30 21:49:42 -07002041 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002042
Michael Layzell734adb42017-06-07 16:58:31 -04002043 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002044 fn expr_closure(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprClosure> {
David Tolnay60291082018-08-28 09:54:49 -07002045 let asyncness: Option<Token![async]> = input.parse()?;
2046 let movability: Option<Token![static]> = if asyncness.is_none() {
2047 input.parse()?
2048 } else {
2049 None
2050 };
2051 let capture: Option<Token![move]> = input.parse()?;
2052 let or1_token: Token![|] = input.parse()?;
2053
2054 let mut inputs = Punctuated::new();
2055 loop {
2056 if input.peek(Token![|]) {
2057 break;
2058 }
2059 let value = fn_arg(input)?;
2060 inputs.push_value(value);
2061 if input.peek(Token![|]) {
2062 break;
2063 }
2064 let punct: Token![,] = input.parse()?;
2065 inputs.push_punct(punct);
2066 }
2067
2068 let or2_token: Token![|] = input.parse()?;
2069
2070 let (output, body) = if input.peek(Token![->]) {
2071 let arrow_token: Token![->] = input.parse()?;
2072 let ty: Type = input.parse()?;
2073 let body: Block = input.parse()?;
2074 let output = ReturnType::Type(arrow_token, Box::new(ty));
2075 let block = Expr::Block(ExprBlock {
2076 attrs: Vec::new(),
2077 label: None,
2078 block: body,
2079 });
2080 (output, block)
2081 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07002082 let body = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002083 (ReturnType::Default, body)
2084 };
2085
2086 Ok(ExprClosure {
David Tolnay310b3262018-08-30 15:33:00 -07002087 attrs: Vec::new(),
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002088 asyncness: asyncness,
2089 movability: movability,
2090 capture: capture,
David Tolnay60291082018-08-28 09:54:49 -07002091 or1_token: or1_token,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002092 inputs: inputs,
David Tolnay60291082018-08-28 09:54:49 -07002093 or2_token: or2_token,
2094 output: output,
2095 body: Box::new(body),
2096 })
David Tolnay02a9c6f2018-08-24 18:58:45 -04002097 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002098
2099 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002100 fn expr_async(input: ParseStream) -> Result<ExprAsync> {
2101 Ok(ExprAsync {
2102 attrs: Vec::new(),
2103 async_token: input.parse()?,
2104 capture: input.parse()?,
2105 block: input.parse()?,
2106 })
David Tolnay60291082018-08-28 09:54:49 -07002107 }
Gregory Katz3e562cc2016-09-28 18:33:02 -04002108
Michael Layzell734adb42017-06-07 16:58:31 -04002109 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002110 fn fn_arg(input: ParseStream) -> Result<FnArg> {
2111 let pat: Pat = input.parse()?;
2112
2113 if input.peek(Token![:]) {
2114 Ok(FnArg::Captured(ArgCaptured {
2115 pat: pat,
2116 colon_token: input.parse()?,
2117 ty: input.parse()?,
2118 }))
2119 } else {
2120 Ok(FnArg::Inferred(pat))
2121 }
2122 }
2123
2124 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002125 fn expr_while(input: ParseStream) -> Result<ExprWhile> {
2126 let label: Option<Label> = input.parse()?;
2127 let while_token: Token![while] = input.parse()?;
2128 let cond = expr_no_struct(input)?;
David Tolnay60291082018-08-28 09:54:49 -07002129
David Tolnay310b3262018-08-30 15:33:00 -07002130 let content;
2131 let brace_token = braced!(content in input);
2132 let inner_attrs = content.call(Attribute::parse_inner)?;
2133 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07002134
David Tolnay310b3262018-08-30 15:33:00 -07002135 Ok(ExprWhile {
2136 attrs: inner_attrs,
2137 label: label,
2138 while_token: while_token,
2139 cond: Box::new(cond),
2140 body: Block {
2141 brace_token: brace_token,
2142 stmts: stmts,
2143 },
2144 })
Alex Crichton954046c2017-05-30 21:49:42 -07002145 }
2146
Michael Layzell734adb42017-06-07 16:58:31 -04002147 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002148 fn expr_while_let(input: ParseStream) -> Result<ExprWhileLet> {
2149 let label: Option<Label> = input.parse()?;
2150 let while_token: Token![while] = input.parse()?;
2151 let let_token: Token![let] = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -07002152
David Tolnay310b3262018-08-30 15:33:00 -07002153 let mut pats = Punctuated::new();
2154 let value: Pat = input.parse()?;
2155 pats.push_value(value);
2156 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2157 let punct = input.parse()?;
2158 pats.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002159 let value: Pat = input.parse()?;
2160 pats.push_value(value);
David Tolnay60291082018-08-28 09:54:49 -07002161 }
David Tolnay310b3262018-08-30 15:33:00 -07002162
2163 let eq_token: Token![=] = input.parse()?;
2164 let expr = expr_no_struct(input)?;
2165
2166 let content;
2167 let brace_token = braced!(content in input);
2168 let inner_attrs = content.call(Attribute::parse_inner)?;
2169 let stmts = content.call(Block::parse_within)?;
2170
2171 Ok(ExprWhileLet {
2172 attrs: inner_attrs,
2173 label: label,
2174 while_token: while_token,
2175 let_token: let_token,
2176 pats: pats,
2177 eq_token: eq_token,
2178 expr: Box::new(expr),
2179 body: Block {
2180 brace_token: brace_token,
2181 stmts: stmts,
2182 },
2183 })
David Tolnaybcd498f2017-12-29 12:02:33 -05002184 }
2185
2186 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002187 impl Parse for Label {
2188 fn parse(input: ParseStream) -> Result<Self> {
2189 Ok(Label {
2190 name: input.parse()?,
2191 colon_token: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002192 })
David Tolnay60291082018-08-28 09:54:49 -07002193 }
Alex Crichton954046c2017-05-30 21:49:42 -07002194 }
2195
Michael Layzell734adb42017-06-07 16:58:31 -04002196 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002197 impl Parse for Option<Label> {
2198 fn parse(input: ParseStream) -> Result<Self> {
2199 if input.peek(Lifetime) {
2200 input.parse().map(Some)
2201 } else {
2202 Ok(None)
2203 }
2204 }
2205 }
2206
2207 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002208 fn expr_continue(input: ParseStream) -> Result<ExprContinue> {
2209 Ok(ExprContinue {
2210 attrs: Vec::new(),
2211 continue_token: input.parse()?,
2212 label: input.parse()?,
2213 })
Alex Crichton954046c2017-05-30 21:49:42 -07002214 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002215
Michael Layzell734adb42017-06-07 16:58:31 -04002216 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002217 fn expr_break(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprBreak> {
2218 Ok(ExprBreak {
David Tolnay310b3262018-08-30 15:33:00 -07002219 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002220 break_token: input.parse()?,
2221 label: input.parse()?,
2222 expr: {
2223 if input.is_empty()
2224 || input.peek(Token![,])
2225 || input.peek(Token![;])
2226 || !allow_struct.0 && input.peek(token::Brace)
2227 {
2228 None
2229 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07002230 let expr = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002231 Some(Box::new(expr))
Michael Layzell92639a52017-06-01 00:07:44 -04002232 }
David Tolnay60291082018-08-28 09:54:49 -07002233 },
2234 })
Alex Crichton954046c2017-05-30 21:49:42 -07002235 }
2236
Michael Layzell734adb42017-06-07 16:58:31 -04002237 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002238 fn expr_ret(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprReturn> {
2239 Ok(ExprReturn {
David Tolnay310b3262018-08-30 15:33:00 -07002240 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002241 return_token: input.parse()?,
2242 expr: {
2243 if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) {
2244 None
2245 } else {
2246 // NOTE: return is greedy and eats blocks after it even when in a
2247 // position where structs are not allowed, such as in if statement
2248 // conditions. For example:
2249 //
2250 // if return { println!("A") } {} // Prints "A"
David Tolnay7d2e1db2018-08-30 11:49:04 -07002251 let expr = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002252 Some(Box::new(expr))
2253 }
2254 },
2255 })
2256 }
2257
2258 #[cfg(feature = "full")]
David Tolnay6e1e5052018-08-30 10:21:48 -07002259 fn expr_struct_helper(input: ParseStream, outer_attrs: Vec<Attribute>, path: Path) -> Result<ExprStruct> {
2260 let content;
2261 let brace_token = braced!(content in input);
2262 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay60291082018-08-28 09:54:49 -07002263
David Tolnay6e1e5052018-08-30 10:21:48 -07002264 let mut fields = Punctuated::new();
2265 loop {
2266 let attrs = content.call(Attribute::parse_outer)?;
2267 if content.fork().parse::<Member>().is_err() {
2268 if attrs.is_empty() {
David Tolnay60291082018-08-28 09:54:49 -07002269 break;
David Tolnay6e1e5052018-08-30 10:21:48 -07002270 } else {
2271 return Err(content.error("expected struct field"));
David Tolnay60291082018-08-28 09:54:49 -07002272 }
David Tolnay60291082018-08-28 09:54:49 -07002273 }
2274
David Tolnay6e1e5052018-08-30 10:21:48 -07002275 let member: Member = content.parse()?;
2276 let (colon_token, value) = if content.peek(Token![:]) || !member.is_named() {
2277 let colon_token: Token![:] = content.parse()?;
2278 let value: Expr = content.parse()?;
2279 (Some(colon_token), value)
2280 } else if let Member::Named(ref ident) = member {
2281 let value = Expr::Path(ExprPath {
2282 attrs: Vec::new(),
2283 qself: None,
2284 path: Path::from(ident.clone()),
2285 });
2286 (None, value)
David Tolnay60291082018-08-28 09:54:49 -07002287 } else {
David Tolnay6e1e5052018-08-30 10:21:48 -07002288 unreachable!()
David Tolnay60291082018-08-28 09:54:49 -07002289 };
2290
David Tolnay6e1e5052018-08-30 10:21:48 -07002291 fields.push(FieldValue {
2292 attrs: attrs,
2293 member: member,
2294 colon_token: colon_token,
2295 expr: value,
2296 });
2297
2298 if !content.peek(Token![,]) {
2299 break;
2300 }
2301 let punct: Token![,] = content.parse()?;
2302 fields.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002303 }
David Tolnay6e1e5052018-08-30 10:21:48 -07002304
2305 let (dot2_token, rest) = if fields.empty_or_trailing() && content.peek(Token![..]) {
2306 let dot2_token: Token![..] = content.parse()?;
2307 let rest: Expr = content.parse()?;
2308 (Some(dot2_token), Some(Box::new(rest)))
2309 } else {
2310 (None, None)
2311 };
2312
2313 Ok(ExprStruct {
2314 attrs: {
2315 let mut attrs = outer_attrs;
2316 attrs.extend(inner_attrs);
2317 attrs
2318 },
2319 brace_token: brace_token,
2320 path: path,
2321 fields: fields,
2322 dot2_token: dot2_token,
2323 rest: rest,
2324 })
Alex Crichton954046c2017-05-30 21:49:42 -07002325 }
David Tolnay055a7042016-10-02 19:23:54 -07002326
Michael Layzell734adb42017-06-07 16:58:31 -04002327 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002328 fn expr_unsafe(input: ParseStream) -> Result<ExprUnsafe> {
2329 let unsafe_token: Token![unsafe] = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -07002330
David Tolnay310b3262018-08-30 15:33:00 -07002331 let content;
2332 let brace_token = braced!(content in input);
2333 let inner_attrs = content.call(Attribute::parse_inner)?;
2334 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07002335
David Tolnay310b3262018-08-30 15:33:00 -07002336 Ok(ExprUnsafe {
2337 attrs: inner_attrs,
2338 unsafe_token: unsafe_token,
2339 block: Block {
2340 brace_token: brace_token,
2341 stmts: stmts,
2342 },
2343 })
Alex Crichton954046c2017-05-30 21:49:42 -07002344 }
David Tolnay055a7042016-10-02 19:23:54 -07002345
Michael Layzell734adb42017-06-07 16:58:31 -04002346 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002347 pub fn expr_block(input: ParseStream) -> Result<ExprBlock> {
2348 let label: Option<Label> = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -07002349
David Tolnay310b3262018-08-30 15:33:00 -07002350 let content;
2351 let brace_token = braced!(content in input);
2352 let inner_attrs = content.call(Attribute::parse_inner)?;
2353 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07002354
David Tolnay310b3262018-08-30 15:33:00 -07002355 Ok(ExprBlock {
2356 attrs: inner_attrs,
2357 label: label,
2358 block: Block {
2359 brace_token: brace_token,
2360 stmts: stmts,
2361 },
2362 })
Alex Crichton954046c2017-05-30 21:49:42 -07002363 }
David Tolnay89e05672016-10-02 14:39:42 -07002364
Michael Layzell734adb42017-06-07 16:58:31 -04002365 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002366 fn expr_range(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprRange> {
2367 Ok(ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05002368 attrs: Vec::new(),
2369 from: None,
David Tolnay60291082018-08-28 09:54:49 -07002370 limits: input.parse()?,
2371 to: {
2372 if input.is_empty()
2373 || input.peek(Token![,])
2374 || input.peek(Token![;])
2375 || !allow_struct.0 && input.peek(token::Brace)
2376 {
2377 None
2378 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07002379 let to = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002380 Some(Box::new(to))
2381 }
2382 },
2383 })
2384 }
David Tolnay438c9052016-10-07 23:24:48 -07002385
Michael Layzell734adb42017-06-07 16:58:31 -04002386 #[cfg(feature = "full")]
David Tolnayc4c631e2018-08-27 10:44:37 -07002387 impl Parse for RangeLimits {
2388 fn parse(input: ParseStream) -> Result<Self> {
2389 let lookahead = input.lookahead1();
2390 if lookahead.peek(Token![..=]) {
2391 input.parse().map(RangeLimits::Closed)
2392 } else if lookahead.peek(Token![...]) {
2393 let dot3: Token![...] = input.parse()?;
2394 Ok(RangeLimits::Closed(Token![..=](dot3.spans)))
2395 } else if lookahead.peek(Token![..]) {
2396 input.parse().map(RangeLimits::HalfOpen)
2397 } else {
2398 Err(lookahead.error())
2399 }
2400 }
Alex Crichton954046c2017-05-30 21:49:42 -07002401 }
David Tolnay438c9052016-10-07 23:24:48 -07002402
David Tolnay60291082018-08-28 09:54:49 -07002403 impl Parse for ExprPath {
2404 fn parse(input: ParseStream) -> Result<Self> {
2405 #[cfg(not(feature = "full"))]
2406 let attrs = Vec::new();
2407 #[cfg(feature = "full")]
2408 let attrs = input.call(Attribute::parse_outer)?;
David Tolnayeb981bb2018-07-21 19:31:38 -07002409
David Tolnay60291082018-08-28 09:54:49 -07002410 let (qself, path) = path::parsing::qpath(input, true)?;
2411
2412 Ok(ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002413 attrs: attrs,
David Tolnay60291082018-08-28 09:54:49 -07002414 qself: qself,
2415 path: path,
Michael Layzell92639a52017-06-01 00:07:44 -04002416 })
David Tolnay60291082018-08-28 09:54:49 -07002417 }
Alex Crichton954046c2017-05-30 21:49:42 -07002418 }
David Tolnay42602292016-10-01 22:25:45 -07002419
Michael Layzell734adb42017-06-07 16:58:31 -04002420 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002421 impl Parse for Block {
2422 fn parse(input: ParseStream) -> Result<Self> {
2423 let content;
2424 Ok(Block {
2425 brace_token: braced!(content in input),
2426 stmts: content.call(Block::parse_within)?,
Michael Layzell92639a52017-06-01 00:07:44 -04002427 })
David Tolnay60291082018-08-28 09:54:49 -07002428 }
Alex Crichton954046c2017-05-30 21:49:42 -07002429 }
David Tolnay939766a2016-09-23 23:48:12 -07002430
Michael Layzell734adb42017-06-07 16:58:31 -04002431 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002432 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002433 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
2434 input.step_cursor(|cursor| Self::old_parse_within(*cursor))
2435 }
2436
2437 named!(old_parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002438 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002439 mut standalone: many0!(do_parse!(
2440 stmt: syn!(Stmt) >>
2441 many0!(punct!(;)) >>
2442 (stmt)
2443 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002444 last: option!(do_parse!(
David Tolnayf8106f82018-08-25 21:17:45 -04002445 attrs: many0!(Attribute::old_parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002446 mut e: syn!(Expr) >>
2447 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002448 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002449 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002450 })
2451 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002452 (match last {
2453 None => standalone,
2454 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002455 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002456 standalone
2457 }
2458 })
2459 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002460 }
2461
Michael Layzell734adb42017-06-07 16:58:31 -04002462 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002463 impl Parse for Stmt {
2464 fn parse(input: ParseStream) -> Result<Self> {
2465 let ahead = input.fork();
2466 ahead.call(Attribute::parse_outer)?;
David Tolnay939766a2016-09-23 23:48:12 -07002467
David Tolnay60291082018-08-28 09:54:49 -07002468 // TODO: better error messages
2469 if {
2470 let ahead = ahead.fork();
2471 // Only parse braces here; paren and bracket will get parsed as
2472 // expression statements
2473 ahead.call(Path::parse_mod_style).is_ok()
2474 && ahead.parse::<Token![!]>().is_ok()
2475 && (ahead.peek(token::Brace) || ahead.peek(Ident))
2476 } {
2477 stmt_mac(input)
2478 } else if ahead.peek(Token![let]) {
2479 stmt_local(input).map(Stmt::Local)
2480 } else if ahead.peek(Token![pub])
2481 || ahead.peek(Token![crate]) && !ahead.peek2(Token![::])
2482 || ahead.peek(Token![extern]) && !ahead.peek2(Token![::])
2483 || ahead.peek(Token![use])
2484 || ahead.peek(Token![static]) && (ahead.peek2(Token![mut]) || ahead.peek2(Ident))
2485 || ahead.peek(Token![const])
2486 || ahead.peek(Token![unsafe]) && !ahead.peek2(token::Brace)
2487 || ahead.peek(Token![async]) && (ahead.peek2(Token![extern]) || ahead.peek2(Token![fn]))
2488 || ahead.peek(Token![fn])
2489 || ahead.peek(Token![mod])
2490 || ahead.peek(Token![type])
2491 || ahead.peek(Token![existential]) && ahead.peek2(Token![type])
2492 || ahead.peek(Token![struct])
2493 || ahead.peek(Token![enum])
2494 || ahead.peek(Token![union]) && ahead.peek2(Ident)
2495 || ahead.peek(Token![auto]) && ahead.peek2(Token![trait])
2496 || ahead.peek(Token![trait])
2497 || ahead.peek(Token![default]) && (ahead.peek2(Token![unsafe]) || ahead.peek2(Token![impl]))
2498 || ahead.peek(Token![impl])
2499 || ahead.peek(Token![macro])
2500 {
2501 input.parse().map(Stmt::Item)
Michael Layzell35418782017-06-07 09:20:25 -04002502 } else {
David Tolnay01218d12018-08-29 18:13:07 -07002503 input.call(stmt_expr)
Michael Layzell35418782017-06-07 09:20:25 -04002504 }
David Tolnay60291082018-08-28 09:54:49 -07002505 }
Alex Crichton954046c2017-05-30 21:49:42 -07002506 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002507
Michael Layzell734adb42017-06-07 16:58:31 -04002508 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002509 fn stmt_mac(input: ParseStream) -> Result<Stmt> {
2510 let attrs = input.call(Attribute::parse_outer)?;
2511 let path = input.call(Path::parse_mod_style)?;
2512 let bang_token: Token![!] = input.parse()?;
2513 let ident: Option<Ident> = input.parse()?;
2514 let (delimiter, tts) = mac::parse_delimiter(input)?;
2515 let semi_token: Option<Token![;]> = input.parse()?;
2516
2517 Ok(Stmt::Item(Item::Macro(ItemMacro {
2518 attrs: attrs,
2519 ident: ident,
2520 mac: Macro {
2521 path: path,
2522 bang_token: bang_token,
2523 delimiter: delimiter,
2524 tts: tts,
2525 },
2526 semi_token: semi_token,
2527 })))
Alex Crichton954046c2017-05-30 21:49:42 -07002528 }
David Tolnay84aa0752016-10-02 23:01:13 -07002529
Michael Layzell734adb42017-06-07 16:58:31 -04002530 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002531 fn stmt_local(input: ParseStream) -> Result<Local> {
2532 Ok(Local {
2533 attrs: input.call(Attribute::parse_outer)?,
2534 let_token: input.parse()?,
2535 pats: {
2536 let mut pats = Punctuated::new();
2537 let value: Pat = input.parse()?;
2538 pats.push_value(value);
2539 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2540 let punct = input.parse()?;
2541 pats.push_punct(punct);
2542 let value: Pat = input.parse()?;
2543 pats.push_value(value);
2544 }
2545 pats
2546 },
2547 ty: {
2548 if input.peek(Token![:]) {
2549 let colon_token: Token![:] = input.parse()?;
2550 let ty: Type = input.parse()?;
2551 Some((colon_token, Box::new(ty)))
2552 } else {
2553 None
2554 }
2555 },
2556 init: {
2557 if input.peek(Token![=]) {
2558 let eq_token: Token![=] = input.parse()?;
2559 let init: Expr = input.parse()?;
2560 Some((eq_token, Box::new(init)))
2561 } else {
2562 None
2563 }
2564 },
2565 semi_token: input.parse()?,
2566 })
2567 }
2568
2569 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07002570 fn stmt_expr(input: ParseStream) -> Result<Stmt> {
David Tolnay60291082018-08-28 09:54:49 -07002571 let mut attrs = input.call(Attribute::parse_outer)?;
David Tolnay01218d12018-08-29 18:13:07 -07002572 let mut e = expr_early(input)?;
David Tolnay60291082018-08-28 09:54:49 -07002573
2574 attrs.extend(e.replace_attrs(Vec::new()));
2575 e.replace_attrs(attrs);
2576
2577 if input.peek(Token![;]) {
David Tolnay01218d12018-08-29 18:13:07 -07002578 return Ok(Stmt::Semi(e, input.parse()?));
David Tolnay60291082018-08-28 09:54:49 -07002579 }
David Tolnay60291082018-08-28 09:54:49 -07002580
David Tolnay01218d12018-08-29 18:13:07 -07002581 match e {
2582 Expr::IfLet(_) |
2583 Expr::If(_) |
2584 Expr::WhileLet(_) |
2585 Expr::While(_) |
2586 Expr::ForLoop(_) |
2587 Expr::Loop(_) |
2588 Expr::Match(_) |
2589 Expr::TryBlock(_) |
2590 Expr::Yield(_) |
2591 Expr::Unsafe(_) |
2592 Expr::Block(_) => Ok(Stmt::Expr(e)),
2593 _ => {
2594 Err(input.error("expected semicolon"))
2595 }
2596 }
David Tolnay60291082018-08-28 09:54:49 -07002597 }
2598
2599 #[cfg(feature = "full")]
2600 impl Parse for Pat {
2601 fn parse(input: ParseStream) -> Result<Self> {
2602 // TODO: better error messages
2603 let lookahead = input.lookahead1();
2604 if lookahead.peek(Token![_]) {
David Tolnay310b3262018-08-30 15:33:00 -07002605 input.call(pat_wild).map(Pat::Wild)
David Tolnay60291082018-08-28 09:54:49 -07002606 } else if lookahead.peek(Token![box]) {
David Tolnay310b3262018-08-30 15:33:00 -07002607 input.call(pat_box).map(Pat::Box)
2608 } else if input.fork().call(pat_range).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002609 // must be before Pat::Lit
David Tolnay310b3262018-08-30 15:33:00 -07002610 input.call(pat_range).map(Pat::Range)
2611 } else if input.fork().call(pat_tuple_struct).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002612 // must be before Pat::Ident
David Tolnay310b3262018-08-30 15:33:00 -07002613 input.call(pat_tuple_struct).map(Pat::TupleStruct)
2614 } else if input.fork().call(pat_struct).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002615 // must be before Pat::Ident
David Tolnay310b3262018-08-30 15:33:00 -07002616 input.call(pat_struct).map(Pat::Struct)
2617 } else if input.fork().call(pat_macro).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002618 // must be before Pat::Ident
David Tolnay310b3262018-08-30 15:33:00 -07002619 input.call(pat_macro).map(Pat::Macro)
2620 } else if input.fork().call(pat_lit).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002621 // must be before Pat::Ident
David Tolnay310b3262018-08-30 15:33:00 -07002622 input.call(pat_lit).map(Pat::Lit)
2623 } else if input.fork().call(pat_ident).is_ok() {
2624 input.call(pat_ident).map(Pat::Ident)
2625 } else if input.fork().call(pat_path).is_ok() {
2626 input.call(pat_path).map(Pat::Path)
David Tolnay60291082018-08-28 09:54:49 -07002627 } else if lookahead.peek(token::Paren) {
David Tolnay310b3262018-08-30 15:33:00 -07002628 input.call(pat_tuple).map(Pat::Tuple)
David Tolnay60291082018-08-28 09:54:49 -07002629 } else if lookahead.peek(Token![&]) {
David Tolnay310b3262018-08-30 15:33:00 -07002630 input.call(pat_ref).map(Pat::Ref)
David Tolnay60291082018-08-28 09:54:49 -07002631 } else if lookahead.peek(token::Bracket) {
David Tolnay310b3262018-08-30 15:33:00 -07002632 input.call(pat_slice).map(Pat::Slice)
David Tolnay60291082018-08-28 09:54:49 -07002633 } else {
2634 Err(lookahead.error())
2635 }
2636 }
2637 }
2638
2639 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002640 fn pat_wild(input: ParseStream) -> Result<PatWild> {
2641 Ok(PatWild {
2642 underscore_token: input.parse()?,
2643 })
Alex Crichton954046c2017-05-30 21:49:42 -07002644 }
2645
Michael Layzell734adb42017-06-07 16:58:31 -04002646 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002647 fn pat_box(input: ParseStream) -> Result<PatBox> {
2648 Ok(PatBox {
2649 box_token: input.parse()?,
2650 pat: input.parse()?,
2651 })
David Tolnay60291082018-08-28 09:54:49 -07002652 }
2653
2654 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002655 fn pat_ident(input: ParseStream) -> Result<PatIdent> {
2656 Ok(PatIdent {
2657 by_ref: input.parse()?,
2658 mutability: input.parse()?,
2659 ident: {
2660 let ident = if input.peek(Ident) || input.peek(Token![self]) {
2661 input.call(Ident::parse_any2)?
2662 } else {
2663 return Err(input.error("expected identifier or `self`"));
2664 };
2665 if input.peek(Token![<]) || input.peek(Token![::]) {
2666 return Err(input.error("unexpected token"));
David Tolnay60291082018-08-28 09:54:49 -07002667 }
David Tolnay310b3262018-08-30 15:33:00 -07002668 ident
2669 },
2670 subpat: {
2671 if input.peek(Token![@]) {
2672 let at_token: Token![@] = input.parse()?;
2673 let subpat: Pat = input.parse()?;
2674 Some((at_token, Box::new(subpat)))
2675 } else {
2676 None
2677 }
2678 },
2679 })
David Tolnay60291082018-08-28 09:54:49 -07002680 }
2681
2682 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002683 fn pat_tuple_struct(input: ParseStream) -> Result<PatTupleStruct> {
2684 Ok(PatTupleStruct {
2685 path: input.parse()?,
2686 pat: input.call(pat_tuple)?,
2687 })
2688 }
David Tolnay60291082018-08-28 09:54:49 -07002689
David Tolnay310b3262018-08-30 15:33:00 -07002690 #[cfg(feature = "full")]
2691 fn pat_struct(input: ParseStream) -> Result<PatStruct> {
2692 let path: Path = input.parse()?;
2693
2694 let content;
2695 let brace_token = braced!(content in input);
2696
2697 let mut fields = Punctuated::new();
2698 while !content.is_empty() && !content.peek(Token![..]) {
2699 let value = content.call(field_pat)?;
2700 fields.push_value(value);
2701 if !content.peek(Token![,]) {
2702 break;
David Tolnay60291082018-08-28 09:54:49 -07002703 }
David Tolnay310b3262018-08-30 15:33:00 -07002704 let punct: Token![,] = content.parse()?;
2705 fields.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002706 }
David Tolnay310b3262018-08-30 15:33:00 -07002707
2708 let dot2_token = if fields.empty_or_trailing() && content.peek(Token![..]) {
2709 Some(content.parse()?)
2710 } else {
2711 None
2712 };
2713
2714 Ok(PatStruct {
2715 path: path,
2716 brace_token: brace_token,
2717 fields: fields,
2718 dot2_token: dot2_token,
2719 })
2720 }
2721
2722 #[cfg(feature = "full")]
2723 fn field_pat(input: ParseStream) -> Result<FieldPat> {
2724 let boxed: Option<Token![box]> = input.parse()?;
2725 let by_ref: Option<Token![ref]> = input.parse()?;
2726 let mutability: Option<Token![mut]> = input.parse()?;
2727 let member: Member = input.parse()?;
2728
2729 if boxed.is_none() && by_ref.is_none() && mutability.is_none() && input.peek(Token![:])
2730 || member.is_unnamed()
2731 {
2732 return Ok(FieldPat {
2733 attrs: Vec::new(),
2734 member: member,
2735 colon_token: input.parse()?,
2736 pat: input.parse()?,
2737 });
2738 }
2739
2740 let ident = match member {
2741 Member::Named(ident) => ident,
2742 Member::Unnamed(_) => unreachable!(),
2743 };
2744
2745 let mut pat = Pat::Ident(PatIdent {
2746 by_ref: by_ref,
2747 mutability: mutability,
2748 ident: ident.clone(),
2749 subpat: None,
2750 });
2751
2752 if let Some(boxed) = boxed {
2753 pat = Pat::Box(PatBox {
2754 pat: Box::new(pat),
2755 box_token: boxed,
2756 });
2757 }
2758
2759 Ok(FieldPat {
2760 member: Member::Named(ident),
2761 pat: Box::new(pat),
2762 attrs: Vec::new(),
2763 colon_token: None,
2764 })
Alex Crichton954046c2017-05-30 21:49:42 -07002765 }
2766
David Tolnay1501f7e2018-08-27 14:21:03 -07002767 impl Parse for Member {
2768 fn parse(input: ParseStream) -> Result<Self> {
2769 if input.peek(Ident) {
2770 input.parse().map(Member::Named)
2771 } else if input.peek(LitInt) {
2772 input.parse().map(Member::Unnamed)
2773 } else {
2774 Err(input.error("expected identifier or integer"))
2775 }
2776 }
David Tolnay85b69a42017-12-27 20:43:10 -05002777 }
2778
David Tolnay1501f7e2018-08-27 14:21:03 -07002779 impl Parse for Index {
2780 fn parse(input: ParseStream) -> Result<Self> {
2781 let lit: LitInt = input.parse()?;
2782 if let IntSuffix::None = lit.suffix() {
2783 Ok(Index {
2784 index: lit.value() as u32,
2785 span: lit.span(),
2786 })
2787 } else {
2788 Err(input.error("expected unsuffixed integer"))
2789 }
2790 }
David Tolnay85b69a42017-12-27 20:43:10 -05002791 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002792
Michael Layzell734adb42017-06-07 16:58:31 -04002793 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002794 fn pat_path(input: ParseStream) -> Result<PatPath> {
2795 let p: ExprPath = input.parse()?;
2796 Ok(PatPath {
2797 qself: p.qself,
2798 path: p.path,
2799 })
Alex Crichton954046c2017-05-30 21:49:42 -07002800 }
David Tolnay9636c052016-10-02 17:11:17 -07002801
Michael Layzell734adb42017-06-07 16:58:31 -04002802 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002803 fn pat_tuple(input: ParseStream) -> Result<PatTuple> {
2804 let content;
2805 let paren_token = parenthesized!(content in input);
David Tolnay60291082018-08-28 09:54:49 -07002806
David Tolnay310b3262018-08-30 15:33:00 -07002807 let mut front = Punctuated::new();
2808 let mut dot2_token = None::<Token![..]>;
2809 let mut comma_token = None::<Token![,]>;
2810 loop {
2811 if content.is_empty() {
2812 break;
David Tolnay60291082018-08-28 09:54:49 -07002813 }
David Tolnay310b3262018-08-30 15:33:00 -07002814 if content.peek(Token![..]) {
2815 dot2_token = Some(content.parse()?);
2816 comma_token = content.parse()?;
2817 break;
David Tolnay60291082018-08-28 09:54:49 -07002818 }
David Tolnay310b3262018-08-30 15:33:00 -07002819 let value: Pat = content.parse()?;
2820 front.push_value(value);
2821 if content.is_empty() {
2822 break;
2823 }
2824 let punct = content.parse()?;
2825 front.push_punct(punct);
2826 }
2827
2828 let back = if comma_token.is_some() {
2829 content.parse_synom(Punctuated::parse_terminated)?
2830 } else {
2831 Punctuated::new()
2832 };
2833
2834 Ok(PatTuple {
2835 paren_token: paren_token,
2836 front: front,
2837 dot2_token: dot2_token,
2838 comma_token: comma_token,
2839 back: back,
2840 })
2841 }
2842
2843 #[cfg(feature = "full")]
2844 fn pat_ref(input: ParseStream) -> Result<PatRef> {
2845 Ok(PatRef {
2846 and_token: input.parse()?,
2847 mutability: input.parse()?,
2848 pat: input.parse()?,
2849 })
2850 }
2851
2852 #[cfg(feature = "full")]
2853 fn pat_lit(input: ParseStream) -> Result<PatLit> {
2854 if input.peek(Lit) || input.peek(Token![-]) && input.peek2(Lit) {
2855 Ok(PatLit {
2856 expr: input.call(pat_lit_expr)?,
2857 })
2858 } else {
2859 Err(input.error("expected literal pattern"))
David Tolnay60291082018-08-28 09:54:49 -07002860 }
2861 }
2862
2863 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002864 fn pat_range(input: ParseStream) -> Result<PatRange> {
2865 Ok(PatRange {
2866 lo: input.call(pat_lit_expr)?,
2867 limits: input.parse()?,
2868 hi: input.call(pat_lit_expr)?,
2869 })
David Tolnay60291082018-08-28 09:54:49 -07002870 }
2871
2872 #[cfg(feature = "full")]
2873 fn pat_lit_expr(input: ParseStream) -> Result<Box<Expr>> {
2874 let neg: Option<Token![-]> = input.parse()?;
2875
2876 let lookahead = input.lookahead1();
2877 let expr = if lookahead.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -07002878 Expr::Lit(input.call(expr_lit)?)
David Tolnay60291082018-08-28 09:54:49 -07002879 } else if lookahead.peek(Ident)
2880 || lookahead.peek(Token![::])
2881 || lookahead.peek(Token![<])
2882 || lookahead.peek(Token![self])
2883 || lookahead.peek(Token![Self])
2884 || lookahead.peek(Token![super])
2885 || lookahead.peek(Token![extern])
2886 || lookahead.peek(Token![crate])
2887 {
2888 Expr::Path(input.parse()?)
2889 } else {
2890 return Err(lookahead.error());
2891 };
2892
2893 Ok(Box::new(if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002894 Expr::Unary(ExprUnary {
2895 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002896 op: UnOp::Neg(neg),
David Tolnay60291082018-08-28 09:54:49 -07002897 expr: Box::new(expr),
David Tolnay3bc597f2017-12-31 02:31:11 -05002898 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002899 } else {
David Tolnay60291082018-08-28 09:54:49 -07002900 expr
2901 }))
Alex Crichton954046c2017-05-30 21:49:42 -07002902 }
David Tolnay323279a2017-12-29 11:26:32 -05002903
2904 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002905 fn pat_slice(input: ParseStream) -> Result<PatSlice> {
2906 let content;
2907 let bracket_token = bracketed!(content in input);
David Tolnay60291082018-08-28 09:54:49 -07002908
David Tolnay310b3262018-08-30 15:33:00 -07002909 let mut front = Punctuated::new();
2910 let mut middle = None;
2911 loop {
2912 if content.is_empty() || content.peek(Token![..]) {
2913 break;
David Tolnay60291082018-08-28 09:54:49 -07002914 }
David Tolnay310b3262018-08-30 15:33:00 -07002915 let value: Pat = content.parse()?;
2916 if content.peek(Token![..]) {
2917 middle = Some(Box::new(value));
2918 break;
David Tolnay60291082018-08-28 09:54:49 -07002919 }
David Tolnay310b3262018-08-30 15:33:00 -07002920 front.push_value(value);
2921 if content.is_empty() {
2922 break;
2923 }
2924 let punct = content.parse()?;
2925 front.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002926 }
David Tolnay310b3262018-08-30 15:33:00 -07002927
2928 let dot2_token: Option<Token![..]> = content.parse()?;
2929 let mut comma_token = None::<Token![,]>;
2930 let mut back = Punctuated::new();
2931 if dot2_token.is_some() {
2932 comma_token = content.parse()?;
2933 if comma_token.is_some() {
2934 loop {
2935 if content.is_empty() {
2936 break;
2937 }
2938 let value: Pat = content.parse()?;
2939 back.push_value(value);
2940 if content.is_empty() {
2941 break;
2942 }
2943 let punct = content.parse()?;
2944 back.push_punct(punct);
2945 }
2946 }
2947 }
2948
2949 Ok(PatSlice {
2950 bracket_token: bracket_token,
2951 front: front,
2952 middle: middle,
2953 dot2_token: dot2_token,
2954 comma_token: comma_token,
2955 back: back,
2956 })
David Tolnay60291082018-08-28 09:54:49 -07002957 }
2958
2959 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002960 fn pat_macro(input: ParseStream) -> Result<PatMacro> {
2961 Ok(PatMacro {
2962 mac: input.parse()?,
2963 })
David Tolnay323279a2017-12-29 11:26:32 -05002964 }
David Tolnay1501f7e2018-08-27 14:21:03 -07002965
2966 #[cfg(feature = "full")]
2967 impl Member {
2968 fn is_named(&self) -> bool {
2969 match *self {
2970 Member::Named(_) => true,
2971 Member::Unnamed(_) => false,
2972 }
2973 }
David Tolnay60291082018-08-28 09:54:49 -07002974
2975 fn is_unnamed(&self) -> bool {
2976 match *self {
2977 Member::Named(_) => false,
2978 Member::Unnamed(_) => true,
2979 }
2980 }
David Tolnay1501f7e2018-08-27 14:21:03 -07002981 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002982}
2983
David Tolnayf4bbbd92016-09-23 14:41:55 -07002984#[cfg(feature = "printing")]
2985mod printing {
2986 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002987 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002988 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07002989 use proc_macro2::{Literal, TokenStream};
2990 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002991
David Tolnaybcf26022017-12-25 22:10:52 -05002992 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07002993 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002994 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07002995 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002996 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002997 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002998 e.to_tokens(tokens);
2999 });
3000 } else {
3001 e.to_tokens(tokens);
3002 }
3003 }
3004
David Tolnay8c91b882017-12-28 23:04:32 -05003005 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003006 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003007 tokens.append_all(attrs.outer());
3008 }
Michael Layzell734adb42017-06-07 16:58:31 -04003009
David Tolnayd997aef2018-07-21 18:42:31 -07003010 #[cfg(feature = "full")]
3011 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3012 tokens.append_all(attrs.inner());
3013 }
3014
David Tolnay8c91b882017-12-28 23:04:32 -05003015 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003016 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3017
3018 #[cfg(not(feature = "full"))]
3019 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003020
Michael Layzell734adb42017-06-07 16:58:31 -04003021 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003022 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003023 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003024 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003025 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003026 self.expr.to_tokens(tokens);
3027 }
3028 }
3029
Michael Layzell734adb42017-06-07 16:58:31 -04003030 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003031 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003032 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003033 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003034 self.place.to_tokens(tokens);
3035 self.arrow_token.to_tokens(tokens);
3036 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003037 }
3038 }
3039
Michael Layzell734adb42017-06-07 16:58:31 -04003040 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003042 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003043 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003044 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003045 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003046 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003047 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003048 }
3049 }
3050
3051 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003052 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003053 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003054 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003055 self.paren_token.surround(tokens, |tokens| {
3056 self.args.to_tokens(tokens);
3057 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003058 }
3059 }
3060
Michael Layzell734adb42017-06-07 16:58:31 -04003061 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003062 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003063 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003064 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003065 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003066 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003067 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003068 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003069 self.paren_token.surround(tokens, |tokens| {
3070 self.args.to_tokens(tokens);
3071 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003072 }
3073 }
3074
Michael Layzell734adb42017-06-07 16:58:31 -04003075 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003076 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003077 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003078 self.colon2_token.to_tokens(tokens);
3079 self.lt_token.to_tokens(tokens);
3080 self.args.to_tokens(tokens);
3081 self.gt_token.to_tokens(tokens);
3082 }
3083 }
3084
3085 #[cfg(feature = "full")]
3086 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003087 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003088 match *self {
3089 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3090 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3091 }
3092 }
3093 }
3094
3095 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003096 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003097 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003098 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003099 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003100 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003101 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003102 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003103 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003104 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003105 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003106 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003107 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003108 }
3109 }
3110
3111 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003112 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003113 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003114 self.left.to_tokens(tokens);
3115 self.op.to_tokens(tokens);
3116 self.right.to_tokens(tokens);
3117 }
3118 }
3119
3120 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003121 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003122 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003123 self.op.to_tokens(tokens);
3124 self.expr.to_tokens(tokens);
3125 }
3126 }
3127
David Tolnay8c91b882017-12-28 23:04:32 -05003128 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003129 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003130 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003131 self.lit.to_tokens(tokens);
3132 }
3133 }
3134
Alex Crichton62a0a592017-05-22 13:58:53 -07003135 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003136 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003137 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003138 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003139 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003140 self.ty.to_tokens(tokens);
3141 }
3142 }
3143
David Tolnay0cf94f22017-12-28 23:46:26 -05003144 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003145 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003146 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003147 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003148 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003149 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003150 self.ty.to_tokens(tokens);
3151 }
3152 }
3153
Michael Layzell734adb42017-06-07 16:58:31 -04003154 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003155 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003156 if let Some((ref else_token, ref else_)) = *else_ {
3157 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003158
3159 // If we are not one of the valid expressions to exist in an else
3160 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003161 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003162 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003163 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003164 }
3165 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003166 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003167 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003168 });
3169 }
3170 }
3171 }
3172 }
3173
3174 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003175 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003176 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003177 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003178 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003179 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003180 self.then_branch.to_tokens(tokens);
3181 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003182 }
3183 }
3184
Michael Layzell734adb42017-06-07 16:58:31 -04003185 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003186 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003187 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003188 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003189 self.if_token.to_tokens(tokens);
3190 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003191 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003192 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003193 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003194 self.then_branch.to_tokens(tokens);
3195 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003196 }
3197 }
3198
Michael Layzell734adb42017-06-07 16:58:31 -04003199 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003200 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003201 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003202 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003203 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003204 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003205 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003206 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003207 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003208 tokens.append_all(&self.body.stmts);
3209 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003210 }
3211 }
3212
Michael Layzell734adb42017-06-07 16:58:31 -04003213 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003214 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003215 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003216 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003217 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003218 self.while_token.to_tokens(tokens);
3219 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003220 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003221 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003222 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003223 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003224 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003225 tokens.append_all(&self.body.stmts);
3226 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003227 }
3228 }
3229
Michael Layzell734adb42017-06-07 16:58:31 -04003230 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003231 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003232 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003233 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003234 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003235 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003236 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003237 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003238 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003239 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003240 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003241 tokens.append_all(&self.body.stmts);
3242 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003243 }
3244 }
3245
Michael Layzell734adb42017-06-07 16:58:31 -04003246 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003247 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003248 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003249 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003250 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003251 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003252 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003253 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003254 tokens.append_all(&self.body.stmts);
3255 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003256 }
3257 }
3258
Michael Layzell734adb42017-06-07 16:58:31 -04003259 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003260 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003261 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003262 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003263 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003264 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003265 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003266 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003267 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003268 arm.to_tokens(tokens);
3269 // Ensure that we have a comma after a non-block arm, except
3270 // for the last one.
3271 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003272 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003273 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003274 }
3275 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003276 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003277 }
3278 }
3279
Michael Layzell734adb42017-06-07 16:58:31 -04003280 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003281 impl ToTokens for ExprAsync {
3282 fn to_tokens(&self, tokens: &mut TokenStream) {
3283 outer_attrs_to_tokens(&self.attrs, tokens);
3284 self.async_token.to_tokens(tokens);
3285 self.capture.to_tokens(tokens);
3286 self.block.to_tokens(tokens);
3287 }
3288 }
3289
3290 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003291 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003292 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003293 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003294 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003295 self.block.to_tokens(tokens);
3296 }
3297 }
3298
Michael Layzell734adb42017-06-07 16:58:31 -04003299 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003300 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003301 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003302 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003303 self.yield_token.to_tokens(tokens);
3304 self.expr.to_tokens(tokens);
3305 }
3306 }
3307
3308 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003309 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003310 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003311 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003312 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003313 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003314 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003315 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003316 for input in self.inputs.pairs() {
3317 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003318 FnArg::Captured(ArgCaptured {
3319 ref pat,
3320 ty: Type::Infer(_),
3321 ..
3322 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003323 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003324 }
David Tolnay56080682018-01-06 14:01:52 -08003325 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003326 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003327 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003328 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003329 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003330 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003331 self.body.to_tokens(tokens);
3332 }
3333 }
3334
Michael Layzell734adb42017-06-07 16:58:31 -04003335 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003336 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003337 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003338 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003339 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003340 self.block.brace_token.surround(tokens, |tokens| {
3341 inner_attrs_to_tokens(&self.attrs, tokens);
3342 tokens.append_all(&self.block.stmts);
3343 });
Nika Layzell640832a2017-12-04 13:37:09 -05003344 }
3345 }
3346
3347 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003348 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003349 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003350 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003351 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003352 self.block.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.block.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 ExprAssign {
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);
Alex Crichton62a0a592017-05-22 13:58:53 -07003363 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003364 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003365 self.right.to_tokens(tokens);
3366 }
3367 }
3368
Michael Layzell734adb42017-06-07 16:58:31 -04003369 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003370 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003371 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003372 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003373 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003374 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003375 self.right.to_tokens(tokens);
3376 }
3377 }
3378
3379 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003380 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003381 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003382 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003383 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003384 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003385 }
3386 }
3387
David Tolnay85b69a42017-12-27 20:43:10 -05003388 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003389 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003390 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003391 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003392 Member::Unnamed(ref index) => index.to_tokens(tokens),
3393 }
3394 }
3395 }
3396
David Tolnay85b69a42017-12-27 20:43:10 -05003397 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003398 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003399 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3400 lit.set_span(self.span);
3401 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003402 }
3403 }
3404
3405 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003406 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003407 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003408 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003409 self.bracket_token.surround(tokens, |tokens| {
3410 self.index.to_tokens(tokens);
3411 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003412 }
3413 }
3414
Michael Layzell734adb42017-06-07 16:58:31 -04003415 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003416 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003417 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003418 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003419 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003420 match self.limits {
3421 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3422 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3423 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003424 self.to.to_tokens(tokens);
3425 }
3426 }
3427
3428 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003429 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003430 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003431 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003432 }
3433 }
3434
Michael Layzell734adb42017-06-07 16:58:31 -04003435 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003436 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003437 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003438 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003439 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003440 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003441 self.expr.to_tokens(tokens);
3442 }
3443 }
3444
Michael Layzell734adb42017-06-07 16:58:31 -04003445 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003446 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003447 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003448 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003449 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003450 self.label.to_tokens(tokens);
3451 self.expr.to_tokens(tokens);
3452 }
3453 }
3454
Michael Layzell734adb42017-06-07 16:58:31 -04003455 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003456 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003457 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003458 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003459 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003460 self.label.to_tokens(tokens);
3461 }
3462 }
3463
Michael Layzell734adb42017-06-07 16:58:31 -04003464 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003465 impl ToTokens for ExprReturn {
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);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003468 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003469 self.expr.to_tokens(tokens);
3470 }
3471 }
3472
Michael Layzell734adb42017-06-07 16:58:31 -04003473 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003474 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003475 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003476 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003477 self.mac.to_tokens(tokens);
3478 }
3479 }
3480
3481 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003482 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003483 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003484 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003485 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003486 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003487 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003488 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003489 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003490 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003491 self.rest.to_tokens(tokens);
3492 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003493 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003494 }
3495 }
3496
Michael Layzell734adb42017-06-07 16:58:31 -04003497 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003498 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003499 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003500 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003501 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003502 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003503 self.expr.to_tokens(tokens);
3504 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003505 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003506 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003507 }
3508 }
3509
David Tolnaye98775f2017-12-28 23:17:00 -05003510 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003511 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003512 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003513 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003514 self.group_token.surround(tokens, |tokens| {
3515 self.expr.to_tokens(tokens);
3516 });
3517 }
3518 }
3519
Alex Crichton62a0a592017-05-22 13:58:53 -07003520 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003521 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003522 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003523 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003524 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003525 self.expr.to_tokens(tokens);
3526 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003527 }
3528 }
3529
Michael Layzell734adb42017-06-07 16:58:31 -04003530 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003531 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003532 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003533 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003534 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003535 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003536 }
3537 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003538
David Tolnay2ae520a2017-12-29 11:19:50 -05003539 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003540 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003541 self.tts.to_tokens(tokens);
3542 }
3543 }
3544
Michael Layzell734adb42017-06-07 16:58:31 -04003545 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003546 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003547 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003548 self.name.to_tokens(tokens);
3549 self.colon_token.to_tokens(tokens);
3550 }
3551 }
3552
3553 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003554 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003555 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003556 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003557 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003558 if let Some(ref colon_token) = self.colon_token {
3559 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003560 self.expr.to_tokens(tokens);
3561 }
David Tolnay055a7042016-10-02 19:23:54 -07003562 }
3563 }
3564
Michael Layzell734adb42017-06-07 16:58:31 -04003565 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003566 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003567 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003568 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003569 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003570 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003571 if let Some((ref if_token, ref guard)) = self.guard {
3572 if_token.to_tokens(tokens);
3573 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003574 }
David Tolnaydfb91432018-03-31 19:19:44 +02003575 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003576 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003577 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003578 }
3579 }
3580
Michael Layzell734adb42017-06-07 16:58:31 -04003581 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003582 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003583 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003584 self.underscore_token.to_tokens(tokens);
3585 }
3586 }
3587
Michael Layzell734adb42017-06-07 16:58:31 -04003588 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003589 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003590 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003591 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003592 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003593 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003594 if let Some((ref at_token, ref subpat)) = self.subpat {
3595 at_token.to_tokens(tokens);
3596 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003597 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003598 }
3599 }
3600
Michael Layzell734adb42017-06-07 16:58:31 -04003601 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003602 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003603 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003604 self.path.to_tokens(tokens);
3605 self.brace_token.surround(tokens, |tokens| {
3606 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003607 // NOTE: We need a comma before the dot2 token if it is present.
3608 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003609 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003610 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003611 self.dot2_token.to_tokens(tokens);
3612 });
3613 }
3614 }
3615
Michael Layzell734adb42017-06-07 16:58:31 -04003616 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003617 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003618 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003619 self.path.to_tokens(tokens);
3620 self.pat.to_tokens(tokens);
3621 }
3622 }
3623
Michael Layzell734adb42017-06-07 16:58:31 -04003624 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003625 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003626 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003627 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3628 }
3629 }
3630
Michael Layzell734adb42017-06-07 16:58:31 -04003631 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003632 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003633 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003634 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003635 self.front.to_tokens(tokens);
3636 if let Some(ref dot2_token) = self.dot2_token {
3637 if !self.front.empty_or_trailing() {
3638 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003639 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003640 }
David Tolnay41871922017-12-29 01:53:45 -05003641 dot2_token.to_tokens(tokens);
3642 self.comma_token.to_tokens(tokens);
3643 if self.comma_token.is_none() && !self.back.is_empty() {
3644 // Ensure there is a comma after the .. token.
3645 <Token![,]>::default().to_tokens(tokens);
3646 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003647 }
David Tolnay41871922017-12-29 01:53:45 -05003648 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003649 });
3650 }
3651 }
3652
Michael Layzell734adb42017-06-07 16:58:31 -04003653 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003654 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003655 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003656 self.box_token.to_tokens(tokens);
3657 self.pat.to_tokens(tokens);
3658 }
3659 }
3660
Michael Layzell734adb42017-06-07 16:58:31 -04003661 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003662 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003663 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003664 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003665 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003666 self.pat.to_tokens(tokens);
3667 }
3668 }
3669
Michael Layzell734adb42017-06-07 16:58:31 -04003670 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003671 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003672 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003673 self.expr.to_tokens(tokens);
3674 }
3675 }
3676
Michael Layzell734adb42017-06-07 16:58:31 -04003677 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003678 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003679 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003680 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003681 match self.limits {
3682 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003683 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003684 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003685 self.hi.to_tokens(tokens);
3686 }
3687 }
3688
Michael Layzell734adb42017-06-07 16:58:31 -04003689 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003690 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003691 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003692 // XXX: This is a mess, and it will be so easy to screw it up. How
3693 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003694 self.bracket_token.surround(tokens, |tokens| {
3695 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003696
3697 // If we need a comma before the middle or standalone .. token,
3698 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003699 if !self.front.empty_or_trailing()
3700 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003701 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003702 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003703 }
3704
3705 // If we have an identifier, we always need a .. token.
3706 if self.middle.is_some() {
3707 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003708 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003709 } else if self.dot2_token.is_some() {
3710 self.dot2_token.to_tokens(tokens);
3711 }
3712
3713 // Make sure we have a comma before the back half.
3714 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003715 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003716 self.back.to_tokens(tokens);
3717 } else {
3718 self.comma_token.to_tokens(tokens);
3719 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003720 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003721 }
3722 }
3723
Michael Layzell734adb42017-06-07 16:58:31 -04003724 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003725 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003726 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003727 self.mac.to_tokens(tokens);
3728 }
3729 }
3730
3731 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003732 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003733 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003734 self.tts.to_tokens(tokens);
3735 }
3736 }
3737
3738 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003739 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003740 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003741 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003742 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003743 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003744 }
3745 self.pat.to_tokens(tokens);
3746 }
3747 }
3748
Michael Layzell734adb42017-06-07 16:58:31 -04003749 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003750 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003751 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003752 self.brace_token.surround(tokens, |tokens| {
3753 tokens.append_all(&self.stmts);
3754 });
David Tolnay42602292016-10-01 22:25:45 -07003755 }
3756 }
3757
Michael Layzell734adb42017-06-07 16:58:31 -04003758 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003759 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003760 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003761 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003762 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003763 Stmt::Item(ref item) => item.to_tokens(tokens),
3764 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003765 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003766 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003767 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003768 }
David Tolnay42602292016-10-01 22:25:45 -07003769 }
3770 }
3771 }
David Tolnay191e0582016-10-02 18:31:09 -07003772
Michael Layzell734adb42017-06-07 16:58:31 -04003773 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003774 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003775 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003776 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003777 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003778 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003779 if let Some((ref colon_token, ref ty)) = self.ty {
3780 colon_token.to_tokens(tokens);
3781 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003782 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003783 if let Some((ref eq_token, ref init)) = self.init {
3784 eq_token.to_tokens(tokens);
3785 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003786 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003787 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003788 }
3789 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003790}