blob: f1077fe179ebf61018e5c40bec29126cb68570c7 [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 {
David Tolnay096d4982017-12-28 23:18:18 -0500572 #[cfg(feature = "full")]
David Tolnay94f06632018-08-31 10:17:17 -0700573 fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500574 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800575 Expr::Box(ExprBox { ref mut attrs, .. })
576 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
577 | Expr::Array(ExprArray { ref mut attrs, .. })
578 | Expr::Call(ExprCall { ref mut attrs, .. })
579 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
580 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
581 | Expr::Binary(ExprBinary { ref mut attrs, .. })
582 | Expr::Unary(ExprUnary { ref mut attrs, .. })
583 | Expr::Lit(ExprLit { ref mut attrs, .. })
584 | Expr::Cast(ExprCast { ref mut attrs, .. })
585 | Expr::Type(ExprType { ref mut attrs, .. })
586 | Expr::If(ExprIf { ref mut attrs, .. })
587 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
588 | Expr::While(ExprWhile { ref mut attrs, .. })
589 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
590 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
591 | Expr::Loop(ExprLoop { ref mut attrs, .. })
592 | Expr::Match(ExprMatch { ref mut attrs, .. })
593 | Expr::Closure(ExprClosure { ref mut attrs, .. })
594 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
595 | Expr::Block(ExprBlock { ref mut attrs, .. })
596 | Expr::Assign(ExprAssign { ref mut attrs, .. })
597 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
598 | Expr::Field(ExprField { ref mut attrs, .. })
599 | Expr::Index(ExprIndex { ref mut attrs, .. })
600 | Expr::Range(ExprRange { ref mut attrs, .. })
601 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200602 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800603 | Expr::Break(ExprBreak { ref mut attrs, .. })
604 | Expr::Continue(ExprContinue { ref mut attrs, .. })
605 | Expr::Return(ExprReturn { ref mut attrs, .. })
606 | Expr::Macro(ExprMacro { ref mut attrs, .. })
607 | Expr::Struct(ExprStruct { ref mut attrs, .. })
608 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
609 | Expr::Paren(ExprParen { ref mut attrs, .. })
610 | Expr::Group(ExprGroup { ref mut attrs, .. })
611 | Expr::Try(ExprTry { ref mut attrs, .. })
David Tolnay02a9c6f2018-08-24 18:58:45 -0400612 | Expr::Async(ExprAsync { ref mut attrs, .. })
David Tolnayfb2dd4b2018-08-24 16:45:34 -0400613 | Expr::TryBlock(ExprTryBlock { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800614 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay10f464a2018-08-30 18:48:55 -0700615 Expr::Verbatim(_) => Vec::new(),
David Tolnay8c91b882017-12-28 23:04:32 -0500616 }
617 }
618}
619
David Tolnay85b69a42017-12-27 20:43:10 -0500620ast_enum! {
621 /// A struct or tuple struct field accessed in a struct literal or field
622 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800623 ///
624 /// *This type is available if Syn is built with the `"derive"` or `"full"`
625 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500626 pub enum Member {
627 /// A named field like `self.x`.
628 Named(Ident),
629 /// An unnamed field like `self.0`.
630 Unnamed(Index),
631 }
632}
633
David Tolnay85b69a42017-12-27 20:43:10 -0500634ast_struct! {
635 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800636 ///
637 /// *This type is available if Syn is built with the `"derive"` or `"full"`
638 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500639 pub struct Index #manual_extra_traits {
640 pub index: u32,
641 pub span: Span,
642 }
643}
644
David Tolnay14982012017-12-29 00:49:51 -0500645impl From<usize> for Index {
646 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700647 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500648 Index {
649 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700650 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500651 }
652 }
653}
654
655#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500656impl Eq for Index {}
657
David Tolnay14982012017-12-29 00:49:51 -0500658#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500659impl PartialEq for Index {
660 fn eq(&self, other: &Self) -> bool {
661 self.index == other.index
662 }
663}
664
David Tolnay14982012017-12-29 00:49:51 -0500665#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500666impl Hash for Index {
667 fn hash<H: Hasher>(&self, state: &mut H) {
668 self.index.hash(state);
669 }
670}
671
672#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700673ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800674 /// The `::<>` explicit type parameters passed to a method call:
675 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800676 ///
677 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500678 pub struct MethodTurbofish {
679 pub colon2_token: Token![::],
680 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500681 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500682 pub gt_token: Token![>],
683 }
684}
685
686#[cfg(feature = "full")]
687ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800688 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800689 ///
690 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500691 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800692 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500693 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800694 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500695 ///
696 /// NOTE: Identity expressions are represented as Type arguments, as
697 /// they are indistinguishable syntactically.
698 Const(Expr),
699 }
700}
701
702#[cfg(feature = "full")]
703ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800705 ///
706 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700707 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500708 /// Attributes tagged on the field.
709 pub attrs: Vec<Attribute>,
710
711 /// Name or index of the field.
712 pub member: Member,
713
David Tolnay5d7098a2017-12-29 01:35:24 -0500714 /// The colon in `Struct { x: x }`. If written in shorthand like
715 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500716 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500717
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 /// Value of the field.
719 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700720 }
David Tolnay055a7042016-10-02 19:23:54 -0700721}
722
Michael Layzell734adb42017-06-07 16:58:31 -0400723#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700724ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800725 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800726 ///
727 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500728 pub struct Label {
729 pub name: Lifetime,
730 pub colon_token: Token![:],
731 }
732}
733
734#[cfg(feature = "full")]
735ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800736 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800737 ///
738 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700739 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500740 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700741 /// Statements in a block
742 pub stmts: Vec<Stmt>,
743 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700744}
745
Michael Layzell734adb42017-06-07 16:58:31 -0400746#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700747ast_enum! {
748 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800749 ///
750 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700751 pub enum Stmt {
752 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800753 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700754
Alex Crichton62a0a592017-05-22 13:58:53 -0700755 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800756 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700757
Alex Crichton62a0a592017-05-22 13:58:53 -0700758 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800759 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700760
David Tolnaya454c8f2018-01-07 01:01:10 -0800761 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800762 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700764}
765
Michael Layzell734adb42017-06-07 16:58:31 -0400766#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700767ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800768 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800769 ///
770 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700771 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500772 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800773 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200774 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500775 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500776 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500777 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700778 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700779}
780
Michael Layzell734adb42017-06-07 16:58:31 -0400781#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700782ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800783 /// A pattern in a local binding, function signature, match expression, or
784 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800785 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800786 /// *This type is available if Syn is built with the `"full"` feature.*
787 ///
David Tolnay614a0142018-01-07 10:25:43 -0800788 /// # Syntax tree enum
789 ///
790 /// This type is a [syntax tree enum].
791 ///
792 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700793 // Clippy false positive
794 // https://github.com/Manishearth/rust-clippy/issues/1241
795 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
796 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800797 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800798 ///
799 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700800 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800801 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700802 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700803
David Tolnaya454c8f2018-01-07 01:01:10 -0800804 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800805 ///
806 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700807 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500808 pub by_ref: Option<Token![ref]>,
809 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700810 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500811 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700812 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700813
David Tolnaya454c8f2018-01-07 01:01:10 -0800814 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800815 ///
816 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700817 pub Struct(PatStruct {
818 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500819 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500820 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800821 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700822 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700823
David Tolnaya454c8f2018-01-07 01:01:10 -0800824 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800825 ///
826 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 pub TupleStruct(PatTupleStruct {
828 pub path: Path,
829 pub pat: PatTuple,
830 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700831
David Tolnaya454c8f2018-01-07 01:01:10 -0800832 /// A path pattern like `Color::Red`, optionally qualified with a
833 /// self-type.
834 ///
835 /// Unquailfied path patterns can legally refer to variants, structs,
836 /// constants or associated constants. Quailfied path patterns like
837 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
838 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800839 ///
840 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700841 pub Path(PatPath {
842 pub qself: Option<QSelf>,
843 pub path: Path,
844 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700845
David Tolnaya454c8f2018-01-07 01:01:10 -0800846 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800847 ///
848 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700849 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500850 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500851 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500852 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500853 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500854 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700855 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800856
857 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800858 ///
859 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700860 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500862 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700863 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800864
865 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800866 ///
867 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700868 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800869 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500870 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500871 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700872 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800873
874 /// A literal pattern: `0`.
875 ///
876 /// This holds an `Expr` rather than a `Lit` because negative numbers
877 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800878 ///
879 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 pub Lit(PatLit {
881 pub expr: Box<Expr>,
882 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800883
884 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800885 ///
886 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700887 pub Range(PatRange {
888 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700889 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500890 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800892
893 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800894 ///
895 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500897 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500898 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700899 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500900 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500901 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500902 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700903 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800904
905 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800906 ///
907 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500908 pub Macro(PatMacro {
909 pub mac: Macro,
910 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800911
912 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800913 ///
914 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500915 pub Verbatim(PatVerbatim #manual_extra_traits {
916 pub tts: TokenStream,
917 }),
918 }
919}
920
David Tolnayc43b44e2017-12-30 23:55:54 -0500921#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500922impl Eq for PatVerbatim {}
923
David Tolnayc43b44e2017-12-30 23:55:54 -0500924#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500925impl PartialEq for PatVerbatim {
926 fn eq(&self, other: &Self) -> bool {
927 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
928 }
929}
930
David Tolnayc43b44e2017-12-30 23:55:54 -0500931#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500932impl Hash for PatVerbatim {
933 fn hash<H>(&self, state: &mut H)
934 where
935 H: Hasher,
936 {
937 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700938 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700939}
940
Michael Layzell734adb42017-06-07 16:58:31 -0400941#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700942ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800943 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700944 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800945 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700946 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500947 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800948 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500949 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700950 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800951 /// 0...10 => {
952 /// return true;
953 /// }
954 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500955 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700956 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800957 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500958 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700959 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800960 ///
961 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 pub struct Arm {
963 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200964 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500965 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500966 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200967 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700968 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800969 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700970 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700971}
972
Michael Layzell734adb42017-06-07 16:58:31 -0400973#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700974ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800975 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800976 ///
977 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700978 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700979 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800980 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800981 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800982 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800983 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700985}
986
Michael Layzell734adb42017-06-07 16:58:31 -0400987#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700988ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800989 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700990 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800991 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
992 /// 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 -0800993 ///
994 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700995 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500996 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500997 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500998 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700999 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001001}
1002
Michael Layzell3936ceb2017-07-08 00:28:36 -04001003#[cfg(any(feature = "parsing", feature = "printing"))]
1004#[cfg(feature = "full")]
David Tolnaye532d6b2018-08-30 16:55:01 -07001005fn requires_terminator(expr: &Expr) -> bool {
David Tolnay01218d12018-08-29 18:13:07 -07001006 // see https://github.com/rust-lang/rust/blob/eb8f2586e/src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001007 match *expr {
1008 Expr::Unsafe(..)
1009 | Expr::Block(..)
1010 | Expr::If(..)
1011 | Expr::IfLet(..)
1012 | Expr::Match(..)
1013 | Expr::While(..)
1014 | Expr::WhileLet(..)
1015 | Expr::Loop(..)
1016 | Expr::ForLoop(..)
David Tolnay02a9c6f2018-08-24 18:58:45 -04001017 | Expr::Async(..)
David Tolnayfb2dd4b2018-08-24 16:45:34 -04001018 | Expr::TryBlock(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001019 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001020 }
1021}
1022
David Tolnayb9c8e322016-09-23 20:48:37 -07001023#[cfg(feature = "parsing")]
1024pub mod parsing {
1025 use super::*;
David Tolnayb9c8e322016-09-23 20:48:37 -07001026
Michael Layzell734adb42017-06-07 16:58:31 -04001027 #[cfg(feature = "full")]
David Tolnay94d304f2018-08-30 23:43:53 -07001028 use ext::IdentExt;
David Tolnay10951d52018-08-31 10:27:39 -07001029 use parse::{Parse, ParseStream, Result};
David Tolnay94d304f2018-08-30 23:43:53 -07001030 use path;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001031
David Tolnaybcf26022017-12-25 22:10:52 -05001032 // When we're parsing expressions which occur before blocks, like in an if
1033 // statement's condition, we cannot parse a struct literal.
1034 //
1035 // Struct literals are ambiguous in certain positions
1036 // https://github.com/rust-lang/rfcs/pull/92
David Tolnay9389c382018-08-27 09:13:37 -07001037 #[derive(Copy, Clone)]
1038 pub struct AllowStruct(bool);
1039
David Tolnay01218d12018-08-29 18:13:07 -07001040 #[derive(Copy, Clone, PartialEq, PartialOrd)]
1041 enum Precedence {
1042 Any,
1043 Assign,
1044 Placement,
1045 Range,
1046 Or,
1047 And,
1048 Compare,
1049 BitOr,
1050 BitXor,
1051 BitAnd,
1052 Shift,
1053 Arithmetic,
1054 Term,
1055 Cast,
1056 }
1057
1058 impl Precedence {
1059 fn of(op: &BinOp) -> Self {
1060 match *op {
1061 BinOp::Add(_) | BinOp::Sub(_) => Precedence::Arithmetic,
1062 BinOp::Mul(_) | BinOp::Div(_) | BinOp::Rem(_) => Precedence::Term,
1063 BinOp::And(_) => Precedence::And,
1064 BinOp::Or(_) => Precedence::Or,
1065 BinOp::BitXor(_) => Precedence::BitXor,
1066 BinOp::BitAnd(_) => Precedence::BitAnd,
1067 BinOp::BitOr(_) => Precedence::BitOr,
1068 BinOp::Shl(_) | BinOp::Shr(_) => Precedence::Shift,
David Tolnay73b7ca12018-08-30 21:05:13 -07001069 BinOp::Eq(_)
1070 | BinOp::Lt(_)
1071 | BinOp::Le(_)
1072 | BinOp::Ne(_)
1073 | BinOp::Ge(_)
1074 | BinOp::Gt(_) => Precedence::Compare,
1075 BinOp::AddEq(_)
1076 | BinOp::SubEq(_)
1077 | BinOp::MulEq(_)
1078 | BinOp::DivEq(_)
1079 | BinOp::RemEq(_)
1080 | BinOp::BitXorEq(_)
1081 | BinOp::BitAndEq(_)
1082 | BinOp::BitOrEq(_)
1083 | BinOp::ShlEq(_)
1084 | BinOp::ShrEq(_) => Precedence::Assign,
David Tolnay01218d12018-08-29 18:13:07 -07001085 }
1086 }
1087 }
1088
David Tolnay9389c382018-08-27 09:13:37 -07001089 impl Parse for Expr {
1090 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001091 ambiguous_expr(input, AllowStruct(true))
Alex Crichton954046c2017-05-30 21:49:42 -07001092 }
1093 }
1094
Michael Layzell734adb42017-06-07 16:58:31 -04001095 #[cfg(feature = "full")]
David Tolnay9fb0aed2018-08-27 10:23:12 -07001096 fn expr_no_struct(input: ParseStream) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001097 ambiguous_expr(input, AllowStruct(false))
David Tolnay9fb0aed2018-08-27 10:23:12 -07001098 }
David Tolnayaf2557e2016-10-24 11:52:21 -07001099
David Tolnay01218d12018-08-29 18:13:07 -07001100 #[cfg(feature = "full")]
David Tolnay73b7ca12018-08-30 21:05:13 -07001101 fn parse_expr(
1102 input: ParseStream,
1103 mut lhs: Expr,
1104 allow_struct: AllowStruct,
1105 base: Precedence,
1106 ) -> Result<Expr> {
David Tolnay01218d12018-08-29 18:13:07 -07001107 loop {
David Tolnay73b7ca12018-08-30 21:05:13 -07001108 if input
1109 .fork()
1110 .parse::<BinOp>()
1111 .ok()
1112 .map_or(false, |op| Precedence::of(&op) >= base)
1113 {
David Tolnay01218d12018-08-29 18:13:07 -07001114 let op: BinOp = input.parse()?;
1115 let precedence = Precedence::of(&op);
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 || next == precedence && precedence == 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::Binary(ExprBinary {
1126 attrs: Vec::new(),
1127 left: Box::new(lhs),
1128 op: op,
1129 right: Box::new(rhs),
1130 });
David Tolnay73b7ca12018-08-30 21:05:13 -07001131 } else if Precedence::Assign >= base
1132 && input.peek(Token![=])
1133 && !input.peek(Token![==])
1134 && !input.peek(Token![=>])
1135 {
David Tolnay01218d12018-08-29 18:13:07 -07001136 let eq_token: Token![=] = input.parse()?;
David Tolnay7d2e1db2018-08-30 11:49:04 -07001137 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001138 loop {
1139 let next = peek_precedence(input);
1140 if next >= Precedence::Assign {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001141 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001142 } else {
1143 break;
1144 }
1145 }
1146 lhs = Expr::Assign(ExprAssign {
1147 attrs: Vec::new(),
1148 left: Box::new(lhs),
1149 eq_token: eq_token,
1150 right: Box::new(rhs),
1151 });
1152 } else if Precedence::Placement >= base && input.peek(Token![<-]) {
1153 let arrow_token: Token![<-] = input.parse()?;
David Tolnay7d2e1db2018-08-30 11:49:04 -07001154 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001155 loop {
1156 let next = peek_precedence(input);
1157 if next > Precedence::Placement {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001158 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001159 } else {
1160 break;
1161 }
1162 }
1163 lhs = Expr::InPlace(ExprInPlace {
1164 attrs: Vec::new(),
1165 place: Box::new(lhs),
1166 arrow_token: arrow_token,
1167 value: Box::new(rhs),
1168 });
1169 } else if Precedence::Range >= base && input.peek(Token![..]) {
1170 let limits: RangeLimits = input.parse()?;
1171 let rhs = if input.is_empty()
1172 || input.peek(Token![,])
1173 || input.peek(Token![;])
1174 || !allow_struct.0 && input.peek(token::Brace)
1175 {
1176 None
1177 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001178 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001179 loop {
1180 let next = peek_precedence(input);
1181 if next > Precedence::Range {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001182 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay01218d12018-08-29 18:13:07 -07001183 } else {
1184 break;
1185 }
1186 }
1187 Some(rhs)
1188 };
1189 lhs = Expr::Range(ExprRange {
1190 attrs: Vec::new(),
1191 from: Some(Box::new(lhs)),
1192 limits: limits,
1193 to: rhs.map(Box::new),
1194 });
1195 } else if Precedence::Cast >= base && input.peek(Token![as]) {
1196 let as_token: Token![as] = input.parse()?;
1197 let ty = input.call(Type::without_plus)?;
1198 lhs = Expr::Cast(ExprCast {
1199 attrs: Vec::new(),
1200 expr: Box::new(lhs),
1201 as_token: as_token,
1202 ty: Box::new(ty),
1203 });
1204 } else if Precedence::Cast >= base && input.peek(Token![:]) && !input.peek(Token![::]) {
1205 let colon_token: Token![:] = input.parse()?;
1206 let ty = input.call(Type::without_plus)?;
1207 lhs = Expr::Type(ExprType {
1208 attrs: Vec::new(),
1209 expr: Box::new(lhs),
1210 colon_token: colon_token,
1211 ty: Box::new(ty),
1212 });
1213 } else {
1214 break;
1215 }
1216 }
1217 Ok(lhs)
1218 }
1219
David Tolnay3e541292018-08-30 11:42:15 -07001220 #[cfg(not(feature = "full"))]
David Tolnay73b7ca12018-08-30 21:05:13 -07001221 fn parse_expr(
1222 input: ParseStream,
1223 mut lhs: Expr,
1224 allow_struct: AllowStruct,
1225 base: Precedence,
1226 ) -> Result<Expr> {
David Tolnay3e541292018-08-30 11:42:15 -07001227 loop {
David Tolnay73b7ca12018-08-30 21:05:13 -07001228 if input
1229 .fork()
1230 .parse::<BinOp>()
1231 .ok()
1232 .map_or(false, |op| Precedence::of(&op) >= base)
1233 {
David Tolnay3e541292018-08-30 11:42:15 -07001234 let op: BinOp = input.parse()?;
1235 let precedence = Precedence::of(&op);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001236 let mut rhs = unary_expr(input, allow_struct)?;
David Tolnay3e541292018-08-30 11:42:15 -07001237 loop {
1238 let next = peek_precedence(input);
1239 if next > precedence || next == precedence && precedence == Precedence::Assign {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001240 rhs = parse_expr(input, rhs, allow_struct, next)?;
David Tolnay3e541292018-08-30 11:42:15 -07001241 } else {
1242 break;
1243 }
1244 }
1245 lhs = Expr::Binary(ExprBinary {
1246 attrs: Vec::new(),
1247 left: Box::new(lhs),
1248 op: op,
1249 right: Box::new(rhs),
1250 });
1251 } else if Precedence::Cast >= base && input.peek(Token![as]) {
1252 let as_token: Token![as] = input.parse()?;
1253 let ty = input.call(Type::without_plus)?;
1254 lhs = Expr::Cast(ExprCast {
1255 attrs: Vec::new(),
1256 expr: Box::new(lhs),
1257 as_token: as_token,
1258 ty: Box::new(ty),
1259 });
1260 } else {
1261 break;
1262 }
1263 }
1264 Ok(lhs)
1265 }
1266
David Tolnay01218d12018-08-29 18:13:07 -07001267 fn peek_precedence(input: ParseStream) -> Precedence {
1268 if let Ok(op) = input.fork().parse() {
1269 Precedence::of(&op)
David Tolnay3e541292018-08-30 11:42:15 -07001270 } else if input.peek(Token![=]) && !input.peek(Token![=>]) {
David Tolnay01218d12018-08-29 18:13:07 -07001271 Precedence::Assign
1272 } else if input.peek(Token![<-]) {
1273 Precedence::Placement
1274 } else if input.peek(Token![..]) {
1275 Precedence::Range
1276 } else if input.peek(Token![as]) || input.peek(Token![:]) && !input.peek(Token![::]) {
1277 Precedence::Cast
1278 } else {
1279 Precedence::Any
1280 }
1281 }
1282
David Tolnaybcf26022017-12-25 22:10:52 -05001283 // Parse an arbitrary expression.
David Tolnay73b7ca12018-08-30 21:05:13 -07001284 fn ambiguous_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001285 let lhs = unary_expr(input, allow_struct)?;
1286 parse_expr(input, lhs, allow_struct, Precedence::Any)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001287 }
1288
David Tolnaybcf26022017-12-25 22:10:52 -05001289 // <UnOp> <trailer>
1290 // & <trailer>
1291 // &mut <trailer>
1292 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001293 #[cfg(feature = "full")]
David Tolnay73b7ca12018-08-30 21:05:13 -07001294 fn unary_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001295 let ahead = input.fork();
1296 ahead.call(Attribute::parse_outer)?;
1297 if ahead.peek(Token![&])
1298 || ahead.peek(Token![box])
1299 || ahead.peek(Token![*])
1300 || ahead.peek(Token![!])
1301 || ahead.peek(Token![-])
1302 {
1303 let attrs = input.call(Attribute::parse_outer)?;
1304 if input.peek(Token![&]) {
1305 Ok(Expr::Reference(ExprReference {
1306 attrs: attrs,
1307 and_token: input.parse()?,
1308 mutability: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001309 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001310 }))
1311 } else if input.peek(Token![box]) {
1312 Ok(Expr::Box(ExprBox {
1313 attrs: attrs,
1314 box_token: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001315 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001316 }))
1317 } else {
1318 Ok(Expr::Unary(ExprUnary {
1319 attrs: attrs,
1320 op: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001321 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001322 }))
1323 }
1324 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001325 trailer_expr(input, allow_struct)
David Tolnay377263f2018-08-27 13:48:30 -07001326 }
1327 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001328
Michael Layzell734adb42017-06-07 16:58:31 -04001329 // XXX: This duplication is ugly
1330 #[cfg(not(feature = "full"))]
David Tolnay73b7ca12018-08-30 21:05:13 -07001331 fn unary_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay377263f2018-08-27 13:48:30 -07001332 let ahead = input.fork();
1333 ahead.call(Attribute::parse_outer)?;
1334 if ahead.peek(Token![*]) || ahead.peek(Token![!]) || ahead.peek(Token![-]) {
1335 Ok(Expr::Unary(ExprUnary {
1336 attrs: input.call(Attribute::parse_outer)?,
1337 op: input.parse()?,
David Tolnay7d2e1db2018-08-30 11:49:04 -07001338 expr: Box::new(unary_expr(input, allow_struct)?),
David Tolnay377263f2018-08-27 13:48:30 -07001339 }))
1340 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001341 trailer_expr(input, allow_struct)
David Tolnay377263f2018-08-27 13:48:30 -07001342 }
1343 }
Michael Layzell734adb42017-06-07 16:58:31 -04001344
David Tolnayd997aef2018-07-21 18:42:31 -07001345 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001346 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1347 let mut outer = Vec::new();
1348 let mut inner = Vec::new();
1349 for attr in mem::replace(attrs, Vec::new()) {
1350 match attr.style {
1351 AttrStyle::Outer => outer.push(attr),
1352 AttrStyle::Inner(_) => inner.push(attr),
1353 }
1354 }
1355 *attrs = inner;
1356 outer
1357 }
1358
David Tolnaybcf26022017-12-25 22:10:52 -05001359 // <atom> (..<args>) ...
1360 // <atom> . <ident> (..<args>) ...
1361 // <atom> . <ident> ...
1362 // <atom> . <lit> ...
1363 // <atom> [ <expr> ] ...
1364 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001365 #[cfg(feature = "full")]
David Tolnay73b7ca12018-08-30 21:05:13 -07001366 fn trailer_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001367 let mut e = atom_expr(input, allow_struct)?;
David Tolnay1501f7e2018-08-27 14:21:03 -07001368
1369 let mut attrs = e.replace_attrs(Vec::new());
1370 let outer_attrs = take_outer(&mut attrs);
1371 e.replace_attrs(attrs);
1372
David Tolnay01218d12018-08-29 18:13:07 -07001373 e = trailer_helper(input, e)?;
1374
1375 let mut attrs = outer_attrs;
1376 attrs.extend(e.replace_attrs(Vec::new()));
1377 e.replace_attrs(attrs);
1378 Ok(e)
1379 }
1380
1381 #[cfg(feature = "full")]
1382 fn trailer_helper(input: ParseStream, mut e: Expr) -> Result<Expr> {
David Tolnay1501f7e2018-08-27 14:21:03 -07001383 loop {
1384 if input.peek(token::Paren) {
1385 let content;
1386 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001387 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001388 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001389 paren_token: parenthesized!(content in input),
David Tolnay60484fe2018-08-30 18:43:04 -07001390 args: content.parse_terminated(Expr::parse)?,
David Tolnay1501f7e2018-08-27 14:21:03 -07001391 });
1392 } else if input.peek(Token![.]) && !input.peek(Token![..]) {
1393 let dot_token: Token![.] = input.parse()?;
1394 let member: Member = input.parse()?;
1395 let turbofish = if member.is_named() && input.peek(Token![::]) {
1396 Some(MethodTurbofish {
1397 colon2_token: input.parse()?,
1398 lt_token: input.parse()?,
1399 args: {
1400 let mut args = Punctuated::new();
1401 loop {
1402 if input.peek(Token![>]) {
1403 break;
1404 }
David Tolnay310b3262018-08-30 15:33:00 -07001405 let value = input.call(generic_method_argument)?;
David Tolnay1501f7e2018-08-27 14:21:03 -07001406 args.push_value(value);
1407 if input.peek(Token![>]) {
1408 break;
1409 }
1410 let punct = input.parse()?;
1411 args.push_punct(punct);
1412 }
1413 args
1414 },
1415 gt_token: input.parse()?,
1416 })
1417 } else {
1418 None
1419 };
1420
1421 if turbofish.is_some() || input.peek(token::Paren) {
1422 if let Member::Named(method) = member {
1423 let content;
1424 e = Expr::MethodCall(ExprMethodCall {
1425 attrs: Vec::new(),
1426 receiver: Box::new(e),
1427 dot_token: dot_token,
1428 method: method,
1429 turbofish: turbofish,
1430 paren_token: parenthesized!(content in input),
David Tolnay60484fe2018-08-30 18:43:04 -07001431 args: content.parse_terminated(Expr::parse)?,
David Tolnay1501f7e2018-08-27 14:21:03 -07001432 });
1433 continue;
1434 }
1435 }
1436
1437 e = Expr::Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001438 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001439 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001440 dot_token: dot_token,
David Tolnay85b69a42017-12-27 20:43:10 -05001441 member: member,
David Tolnay1501f7e2018-08-27 14:21:03 -07001442 });
1443 } else if input.peek(token::Bracket) {
1444 let content;
1445 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001446 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001447 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001448 bracket_token: bracketed!(content in input),
1449 index: content.parse()?,
1450 });
1451 } else if input.peek(Token![?]) {
1452 e = Expr::Try(ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001453 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001454 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001455 question_token: input.parse()?,
1456 });
1457 } else {
1458 break;
1459 }
1460 }
David Tolnay1501f7e2018-08-27 14:21:03 -07001461 Ok(e)
1462 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001463
Michael Layzell734adb42017-06-07 16:58:31 -04001464 // XXX: Duplication == ugly
1465 #[cfg(not(feature = "full"))]
David Tolnay73b7ca12018-08-30 21:05:13 -07001466 fn trailer_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay7d2e1db2018-08-30 11:49:04 -07001467 let mut e = atom_expr(input, allow_struct)?;
David Tolnay1501f7e2018-08-27 14:21:03 -07001468
1469 loop {
1470 if input.peek(token::Paren) {
1471 let content;
1472 e = Expr::Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001473 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001474 func: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001475 paren_token: parenthesized!(content in input),
David Tolnay60484fe2018-08-30 18:43:04 -07001476 args: content.parse_terminated(Expr::parse)?,
David Tolnay1501f7e2018-08-27 14:21:03 -07001477 });
1478 } else if input.peek(Token![.]) {
1479 e = Expr::Field(ExprField {
David Tolnayd5147742018-06-30 10:09:52 -07001480 attrs: Vec::new(),
1481 base: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001482 dot_token: input.parse()?,
1483 member: input.parse()?,
1484 });
1485 } else if input.peek(token::Bracket) {
1486 let content;
1487 e = Expr::Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001488 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001489 expr: Box::new(e),
David Tolnay1501f7e2018-08-27 14:21:03 -07001490 bracket_token: bracketed!(content in input),
1491 index: content.parse()?,
1492 });
1493 } else {
1494 break;
1495 }
1496 }
1497
1498 Ok(e)
1499 }
Michael Layzell734adb42017-06-07 16:58:31 -04001500
David Tolnaya454c8f2018-01-07 01:01:10 -08001501 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001502 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001503 #[cfg(feature = "full")]
David Tolnay7d2e1db2018-08-30 11:49:04 -07001504 fn atom_expr(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay6e1e5052018-08-30 10:21:48 -07001505 if input.peek(token::Group) {
David Tolnay310b3262018-08-30 15:33:00 -07001506 return input.call(expr_group).map(Expr::Group);
David Tolnay6e1e5052018-08-30 10:21:48 -07001507 }
1508
1509 let mut attrs = input.call(Attribute::parse_outer)?;
1510
1511 let mut expr = if input.peek(token::Group) {
David Tolnay310b3262018-08-30 15:33:00 -07001512 Expr::Group(input.call(expr_group)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001513 } else if input.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -07001514 Expr::Lit(input.call(expr_lit)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001515 } else if input.peek(Token![async])
1516 && (input.peek2(token::Brace) || input.peek2(Token![move]) && input.peek3(token::Brace))
1517 {
David Tolnay310b3262018-08-30 15:33:00 -07001518 Expr::Async(input.call(expr_async)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001519 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001520 Expr::TryBlock(input.call(expr_try_block)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001521 } else if input.peek(Token![|])
1522 || input.peek(Token![async]) && (input.peek2(Token![|]) || input.peek2(Token![move]))
1523 || input.peek(Token![static])
1524 || input.peek(Token![move])
1525 {
1526 Expr::Closure(expr_closure(input, allow_struct)?)
1527 } else if input.peek(Ident)
1528 || input.peek(Token![::])
1529 || input.peek(Token![<])
1530 || input.peek(Token![self])
1531 || input.peek(Token![Self])
1532 || input.peek(Token![super])
1533 || input.peek(Token![extern])
1534 || input.peek(Token![crate])
1535 {
1536 path_or_macro_or_struct(input, allow_struct)?
1537 } else if input.peek(token::Paren) {
1538 paren_or_tuple(input)?
1539 } else if input.peek(Token![break]) {
1540 Expr::Break(expr_break(input, allow_struct)?)
1541 } else if input.peek(Token![continue]) {
David Tolnay310b3262018-08-30 15:33:00 -07001542 Expr::Continue(input.call(expr_continue)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001543 } else if input.peek(Token![return]) {
1544 Expr::Return(expr_ret(input, allow_struct)?)
1545 } else if input.peek(token::Bracket) {
1546 array_or_repeat(input)?
1547 } else if input.peek(Token![if]) {
1548 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001549 Expr::IfLet(input.call(expr_if_let)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001550 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001551 Expr::If(input.call(expr_if)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001552 }
1553 } else if input.peek(Token![while]) {
1554 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001555 Expr::WhileLet(input.call(expr_while_let)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001556 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001557 Expr::While(input.call(expr_while)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001558 }
1559 } else if input.peek(Token![for]) {
David Tolnay310b3262018-08-30 15:33:00 -07001560 Expr::ForLoop(input.call(expr_for_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001561 } else if input.peek(Token![loop]) {
David Tolnay310b3262018-08-30 15:33:00 -07001562 Expr::Loop(input.call(expr_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001563 } else if input.peek(Token![match]) {
David Tolnay310b3262018-08-30 15:33:00 -07001564 Expr::Match(input.call(expr_match)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001565 } else if input.peek(Token![yield]) {
David Tolnay310b3262018-08-30 15:33:00 -07001566 Expr::Yield(input.call(expr_yield)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001567 } else if input.peek(Token![unsafe]) {
David Tolnay310b3262018-08-30 15:33:00 -07001568 Expr::Unsafe(input.call(expr_unsafe)?)
David Tolnay7d2e1db2018-08-30 11:49:04 -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 if input.peek(Token![..]) {
1572 Expr::Range(expr_range(input, allow_struct)?)
1573 } else if input.peek(Lifetime) {
1574 let the_label: Label = input.parse()?;
1575 let mut expr = if input.peek(Token![while]) {
1576 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001577 Expr::WhileLet(input.call(expr_while_let)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001578 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001579 Expr::While(input.call(expr_while)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001580 }
1581 } else if input.peek(Token![for]) {
David Tolnay310b3262018-08-30 15:33:00 -07001582 Expr::ForLoop(input.call(expr_for_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001583 } else if input.peek(Token![loop]) {
David Tolnay310b3262018-08-30 15:33:00 -07001584 Expr::Loop(input.call(expr_loop)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001585 } else if input.peek(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001586 Expr::Block(input.call(expr_block)?)
David Tolnay6e1e5052018-08-30 10:21:48 -07001587 } else {
1588 return Err(input.error("expected loop or block expression"));
1589 };
1590 match expr {
David Tolnay73b7ca12018-08-30 21:05:13 -07001591 Expr::WhileLet(ExprWhileLet { ref mut label, .. })
1592 | Expr::While(ExprWhile { ref mut label, .. })
1593 | Expr::ForLoop(ExprForLoop { ref mut label, .. })
1594 | Expr::Loop(ExprLoop { ref mut label, .. })
1595 | Expr::Block(ExprBlock { ref mut label, .. }) => *label = Some(the_label),
David Tolnay6e1e5052018-08-30 10:21:48 -07001596 _ => unreachable!(),
1597 }
1598 expr
1599 } else {
1600 return Err(input.error("expected expression"));
1601 };
1602
1603 attrs.extend(expr.replace_attrs(Vec::new()));
1604 expr.replace_attrs(attrs);
1605 Ok(expr)
1606 }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001607
Michael Layzell734adb42017-06-07 16:58:31 -04001608 #[cfg(not(feature = "full"))]
David Tolnay7d2e1db2018-08-30 11:49:04 -07001609 fn atom_expr(input: ParseStream, _allow_struct: AllowStruct) -> Result<Expr> {
David Tolnay6e1e5052018-08-30 10:21:48 -07001610 if input.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -07001611 input.call(expr_lit).map(Expr::Lit)
David Tolnay6e1e5052018-08-30 10:21:48 -07001612 } else if input.peek(token::Paren) {
David Tolnay310b3262018-08-30 15:33:00 -07001613 input.call(expr_paren).map(Expr::Paren)
David Tolnay6e1e5052018-08-30 10:21:48 -07001614 } else if input.peek(Ident)
1615 || input.peek(Token![::])
1616 || input.peek(Token![<])
1617 || input.peek(Token![self])
1618 || input.peek(Token![Self])
1619 || input.peek(Token![super])
1620 || input.peek(Token![extern])
1621 || input.peek(Token![crate])
1622 {
1623 input.parse().map(Expr::Path)
1624 } else {
1625 Err(input.error("unsupported expression; enable syn's features=[\"full\"]"))
1626 }
1627 }
1628
1629 #[cfg(feature = "full")]
1630 fn path_or_macro_or_struct(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
1631 let expr: ExprPath = input.parse()?;
1632 if expr.qself.is_some() {
1633 return Ok(Expr::Path(expr));
1634 }
1635
1636 if input.peek(Token![!]) && !input.peek(Token![!=]) {
1637 let mut contains_arguments = false;
1638 for segment in &expr.path.segments {
1639 match segment.arguments {
1640 PathArguments::None => {}
1641 PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
1642 contains_arguments = true;
1643 }
1644 }
1645 }
1646
1647 if !contains_arguments {
1648 let bang_token: Token![!] = input.parse()?;
1649 let (delimiter, tts) = mac::parse_delimiter(input)?;
1650 return Ok(Expr::Macro(ExprMacro {
1651 attrs: Vec::new(),
1652 mac: Macro {
1653 path: expr.path,
1654 bang_token: bang_token,
1655 delimiter: delimiter,
1656 tts: tts,
1657 },
1658 }));
1659 }
1660 }
1661
1662 if allow_struct.0 && input.peek(token::Brace) {
1663 let outer_attrs = Vec::new();
1664 expr_struct_helper(input, outer_attrs, expr.path).map(Expr::Struct)
1665 } else {
1666 Ok(Expr::Path(expr))
1667 }
1668 }
1669
1670 #[cfg(feature = "full")]
1671 fn paren_or_tuple(input: ParseStream) -> Result<Expr> {
1672 let content;
1673 let paren_token = parenthesized!(content in input);
1674 let inner_attrs = content.call(Attribute::parse_inner)?;
1675 if content.is_empty() {
1676 return Ok(Expr::Tuple(ExprTuple {
1677 attrs: inner_attrs,
1678 paren_token: paren_token,
1679 elems: Punctuated::new(),
1680 }));
1681 }
1682
1683 let first: Expr = content.parse()?;
1684 if content.is_empty() {
1685 return Ok(Expr::Paren(ExprParen {
1686 attrs: inner_attrs,
1687 paren_token: paren_token,
1688 expr: Box::new(first),
1689 }));
1690 }
1691
1692 let mut elems = Punctuated::new();
1693 elems.push_value(first);
1694 while !content.is_empty() {
1695 let punct = content.parse()?;
1696 elems.push_punct(punct);
1697 if content.is_empty() {
1698 break;
1699 }
1700 let value = content.parse()?;
1701 elems.push_value(value);
1702 }
1703 Ok(Expr::Tuple(ExprTuple {
1704 attrs: inner_attrs,
1705 paren_token: paren_token,
1706 elems: elems,
1707 }))
1708 }
1709
1710 #[cfg(feature = "full")]
1711 fn array_or_repeat(input: ParseStream) -> Result<Expr> {
1712 let content;
1713 let bracket_token = bracketed!(content in input);
1714 let inner_attrs = content.call(Attribute::parse_inner)?;
1715 if content.is_empty() {
1716 return Ok(Expr::Array(ExprArray {
1717 attrs: inner_attrs,
1718 bracket_token: bracket_token,
1719 elems: Punctuated::new(),
1720 }));
1721 }
1722
1723 let first: Expr = content.parse()?;
1724 if content.is_empty() || content.peek(Token![,]) {
1725 let mut elems = Punctuated::new();
1726 elems.push_value(first);
1727 while !content.is_empty() {
1728 let punct = content.parse()?;
1729 elems.push_punct(punct);
1730 if content.is_empty() {
1731 break;
1732 }
1733 let value = content.parse()?;
1734 elems.push_value(value);
1735 }
1736 Ok(Expr::Array(ExprArray {
1737 attrs: inner_attrs,
1738 bracket_token: bracket_token,
1739 elems: elems,
1740 }))
1741 } else if content.peek(Token![;]) {
1742 let semi_token: Token![;] = content.parse()?;
1743 let len: Expr = content.parse()?;
1744 Ok(Expr::Repeat(ExprRepeat {
1745 attrs: inner_attrs,
1746 bracket_token: bracket_token,
1747 expr: Box::new(first),
1748 semi_token: semi_token,
1749 len: Box::new(len),
1750 }))
1751 } else {
1752 Err(content.error("expected `,` or `;`"))
1753 }
1754 }
Michael Layzell734adb42017-06-07 16:58:31 -04001755
Michael Layzell734adb42017-06-07 16:58:31 -04001756 #[cfg(feature = "full")]
David Tolnay01218d12018-08-29 18:13:07 -07001757 fn expr_early(input: ParseStream) -> Result<Expr> {
1758 let mut attrs = input.call(Attribute::parse_outer)?;
1759 let mut expr = if input.peek(Token![if]) {
1760 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001761 Expr::IfLet(input.call(expr_if_let)?)
David Tolnay01218d12018-08-29 18:13:07 -07001762 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001763 Expr::If(input.call(expr_if)?)
David Tolnay01218d12018-08-29 18:13:07 -07001764 }
1765 } else if input.peek(Token![while]) {
1766 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001767 Expr::WhileLet(input.call(expr_while_let)?)
David Tolnay01218d12018-08-29 18:13:07 -07001768 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001769 Expr::While(input.call(expr_while)?)
David Tolnay01218d12018-08-29 18:13:07 -07001770 }
1771 } else if input.peek(Token![for]) {
David Tolnay310b3262018-08-30 15:33:00 -07001772 Expr::ForLoop(input.call(expr_for_loop)?)
David Tolnay01218d12018-08-29 18:13:07 -07001773 } else if input.peek(Token![loop]) {
David Tolnay310b3262018-08-30 15:33:00 -07001774 Expr::Loop(input.call(expr_loop)?)
David Tolnay01218d12018-08-29 18:13:07 -07001775 } else if input.peek(Token![match]) {
David Tolnay310b3262018-08-30 15:33:00 -07001776 Expr::Match(input.call(expr_match)?)
David Tolnay01218d12018-08-29 18:13:07 -07001777 } else if input.peek(Token![try]) && input.peek2(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001778 Expr::TryBlock(input.call(expr_try_block)?)
David Tolnay01218d12018-08-29 18:13:07 -07001779 } else if input.peek(Token![unsafe]) {
David Tolnay310b3262018-08-30 15:33:00 -07001780 Expr::Unsafe(input.call(expr_unsafe)?)
David Tolnay01218d12018-08-29 18:13:07 -07001781 } else if input.peek(token::Brace) {
David Tolnay310b3262018-08-30 15:33:00 -07001782 Expr::Block(input.call(expr_block)?)
David Tolnay01218d12018-08-29 18:13:07 -07001783 } else {
1784 let allow_struct = AllowStruct(true);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001785 let mut expr = unary_expr(input, allow_struct)?;
David Tolnay01218d12018-08-29 18:13:07 -07001786
1787 attrs.extend(expr.replace_attrs(Vec::new()));
1788 expr.replace_attrs(attrs);
1789
David Tolnay7d2e1db2018-08-30 11:49:04 -07001790 return parse_expr(input, expr, allow_struct, Precedence::Any);
David Tolnay01218d12018-08-29 18:13:07 -07001791 };
1792
1793 if input.peek(Token![.]) || input.peek(Token![?]) {
1794 expr = trailer_helper(input, expr)?;
1795
1796 attrs.extend(expr.replace_attrs(Vec::new()));
1797 expr.replace_attrs(attrs);
1798
1799 let allow_struct = AllowStruct(true);
David Tolnay7d2e1db2018-08-30 11:49:04 -07001800 return parse_expr(input, expr, allow_struct, Precedence::Any);
David Tolnay01218d12018-08-29 18:13:07 -07001801 }
1802
1803 attrs.extend(expr.replace_attrs(Vec::new()));
1804 expr.replace_attrs(attrs);
1805 Ok(expr)
1806 }
Michael Layzell35418782017-06-07 09:20:25 -04001807
David Tolnay310b3262018-08-30 15:33:00 -07001808 pub fn expr_lit(input: ParseStream) -> Result<ExprLit> {
1809 Ok(ExprLit {
1810 attrs: Vec::new(),
1811 lit: input.parse()?,
1812 })
David Tolnay8c91b882017-12-28 23:04:32 -05001813 }
1814
1815 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001816 fn expr_group(input: ParseStream) -> Result<ExprGroup> {
David Tolnay10951d52018-08-31 10:27:39 -07001817 let group = private::parse_group(input)?;
David Tolnay310b3262018-08-30 15:33:00 -07001818 Ok(ExprGroup {
1819 attrs: Vec::new(),
David Tolnayf57f76f2018-08-31 10:23:17 -07001820 group_token: group.token,
1821 expr: group.content.parse()?,
David Tolnay310b3262018-08-30 15:33:00 -07001822 })
1823 }
1824
1825 #[cfg(not(feature = "full"))]
1826 fn expr_paren(input: ParseStream) -> Result<ExprParen> {
1827 let content;
1828 Ok(ExprParen {
1829 attrs: Vec::new(),
1830 paren_token: parenthesized!(content in input),
1831 expr: content.parse()?,
1832 })
David Tolnay8c91b882017-12-28 23:04:32 -05001833 }
1834
David Tolnaye98775f2017-12-28 23:17:00 -05001835 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001836 fn generic_method_argument(input: ParseStream) -> Result<GenericMethodArgument> {
David Tolnayd60cfec2017-12-29 00:21:38 -05001837 // TODO parse const generics as well
David Tolnay8db2d662018-08-30 17:40:59 -07001838 input.parse().map(GenericMethodArgument::Type)
David Tolnayd60cfec2017-12-29 00:21:38 -05001839 }
1840
1841 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001842 fn expr_if_let(input: ParseStream) -> Result<ExprIfLet> {
1843 Ok(ExprIfLet {
1844 attrs: Vec::new(),
1845 if_token: input.parse()?,
1846 let_token: input.parse()?,
1847 pats: {
1848 let mut pats = Punctuated::new();
1849 let value: Pat = input.parse()?;
1850 pats.push_value(value);
David Tolnay73b7ca12018-08-30 21:05:13 -07001851 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
David Tolnay310b3262018-08-30 15:33:00 -07001852 let punct = input.parse()?;
1853 pats.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07001854 let value: Pat = input.parse()?;
1855 pats.push_value(value);
David Tolnay310b3262018-08-30 15:33:00 -07001856 }
1857 pats
1858 },
1859 eq_token: input.parse()?,
1860 expr: Box::new(input.call(expr_no_struct)?),
1861 then_branch: input.parse()?,
1862 else_branch: {
1863 if input.peek(Token![else]) {
1864 Some(input.call(else_block)?)
1865 } else {
1866 None
1867 }
1868 },
1869 })
David Tolnay29f9ce12016-10-02 20:58:40 -07001870 }
1871
Michael Layzell734adb42017-06-07 16:58:31 -04001872 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001873 fn expr_if(input: ParseStream) -> Result<ExprIf> {
1874 Ok(ExprIf {
1875 attrs: Vec::new(),
1876 if_token: input.parse()?,
1877 cond: Box::new(input.call(expr_no_struct)?),
1878 then_branch: input.parse()?,
1879 else_branch: {
1880 if input.peek(Token![else]) {
1881 Some(input.call(else_block)?)
1882 } else {
1883 None
1884 }
1885 },
1886 })
Alex Crichton954046c2017-05-30 21:49:42 -07001887 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001888
Michael Layzell734adb42017-06-07 16:58:31 -04001889 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07001890 fn else_block(input: ParseStream) -> Result<(Token![else], Box<Expr>)> {
1891 let else_token: Token![else] = input.parse()?;
1892
1893 let lookahead = input.lookahead1();
1894 let else_branch = if input.peek(Token![if]) {
1895 if input.peek2(Token![let]) {
David Tolnay310b3262018-08-30 15:33:00 -07001896 input.call(expr_if_let).map(Expr::IfLet)?
David Tolnay60291082018-08-28 09:54:49 -07001897 } else {
David Tolnay310b3262018-08-30 15:33:00 -07001898 input.call(expr_if).map(Expr::If)?
David Tolnay60291082018-08-28 09:54:49 -07001899 }
1900 } else if input.peek(token::Brace) {
1901 Expr::Block(ExprBlock {
1902 attrs: Vec::new(),
1903 label: None,
1904 block: input.parse()?,
1905 })
1906 } else {
1907 return Err(lookahead.error());
1908 };
1909
1910 Ok((else_token, Box::new(else_branch)))
1911 }
David Tolnay939766a2016-09-23 23:48:12 -07001912
Michael Layzell734adb42017-06-07 16:58:31 -04001913 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001914 fn expr_for_loop(input: ParseStream) -> Result<ExprForLoop> {
1915 let label: Option<Label> = input.parse()?;
1916 let for_token: Token![for] = input.parse()?;
1917 let pat: Pat = input.parse()?;
1918 let in_token: Token![in] = input.parse()?;
1919 let expr: Expr = input.call(expr_no_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07001920
David Tolnay310b3262018-08-30 15:33:00 -07001921 let content;
1922 let brace_token = braced!(content in input);
1923 let inner_attrs = content.call(Attribute::parse_inner)?;
1924 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07001925
David Tolnay310b3262018-08-30 15:33:00 -07001926 Ok(ExprForLoop {
1927 attrs: inner_attrs,
1928 label: label,
1929 for_token: for_token,
1930 pat: Box::new(pat),
1931 in_token: in_token,
1932 expr: Box::new(expr),
1933 body: Block {
David Tolnay60291082018-08-28 09:54:49 -07001934 brace_token: brace_token,
David Tolnay310b3262018-08-30 15:33:00 -07001935 stmts: stmts,
1936 },
1937 })
Alex Crichton954046c2017-05-30 21:49:42 -07001938 }
David Tolnay1978c672016-10-27 22:05:52 -07001939
Michael Layzell734adb42017-06-07 16:58:31 -04001940 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001941 fn expr_loop(input: ParseStream) -> Result<ExprLoop> {
1942 let label: Option<Label> = input.parse()?;
1943 let loop_token: Token![loop] = input.parse()?;
1944
1945 let content;
1946 let brace_token = braced!(content in input);
1947 let inner_attrs = content.call(Attribute::parse_inner)?;
1948 let stmts = content.call(Block::parse_within)?;
1949
1950 Ok(ExprLoop {
1951 attrs: inner_attrs,
1952 label: label,
1953 loop_token: loop_token,
1954 body: Block {
1955 brace_token: brace_token,
1956 stmts: stmts,
1957 },
1958 })
Alex Crichton954046c2017-05-30 21:49:42 -07001959 }
Arnavion02ef13f2017-04-25 00:54:31 -07001960
Michael Layzell734adb42017-06-07 16:58:31 -04001961 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07001962 fn expr_match(input: ParseStream) -> Result<ExprMatch> {
1963 let match_token: Token![match] = input.parse()?;
1964 let expr = expr_no_struct(input)?;
1965
1966 let content;
1967 let brace_token = braced!(content in input);
1968 let inner_attrs = content.call(Attribute::parse_inner)?;
1969
1970 let mut arms = Vec::new();
1971 while !content.is_empty() {
1972 arms.push(content.call(match_arm)?);
1973 }
1974
1975 Ok(ExprMatch {
1976 attrs: inner_attrs,
1977 match_token: match_token,
1978 expr: Box::new(expr),
1979 brace_token: brace_token,
1980 arms: arms,
1981 })
1982 }
1983
1984 #[cfg(feature = "full")]
1985 fn expr_try_block(input: ParseStream) -> Result<ExprTryBlock> {
1986 Ok(ExprTryBlock {
1987 attrs: Vec::new(),
1988 try_token: input.parse()?,
1989 block: input.parse()?,
1990 })
1991 }
1992
1993 #[cfg(feature = "full")]
1994 fn expr_yield(input: ParseStream) -> Result<ExprYield> {
1995 Ok(ExprYield {
1996 attrs: Vec::new(),
1997 yield_token: input.parse()?,
1998 expr: {
1999 if !input.is_empty() && !input.peek(Token![,]) && !input.peek(Token![;]) {
2000 Some(input.parse()?)
2001 } else {
2002 None
2003 }
2004 },
2005 })
2006 }
2007
2008 #[cfg(feature = "full")]
2009 fn match_arm(input: ParseStream) -> Result<Arm> {
2010 let requires_comma;
2011 Ok(Arm {
2012 attrs: input.call(Attribute::parse_outer)?,
2013 leading_vert: input.parse()?,
2014 pats: {
2015 let mut pats = Punctuated::new();
2016 let value: Pat = input.parse()?;
2017 pats.push_value(value);
2018 loop {
2019 if !input.peek(Token![|]) {
2020 break;
David Tolnay60291082018-08-28 09:54:49 -07002021 }
David Tolnay310b3262018-08-30 15:33:00 -07002022 let punct = input.parse()?;
2023 pats.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002024 let value: Pat = input.parse()?;
2025 pats.push_value(value);
David Tolnay310b3262018-08-30 15:33:00 -07002026 }
2027 pats
2028 },
2029 guard: {
2030 if input.peek(Token![if]) {
2031 let if_token: Token![if] = input.parse()?;
2032 let guard: Expr = input.parse()?;
2033 Some((if_token, Box::new(guard)))
2034 } else {
2035 None
2036 }
2037 },
2038 fat_arrow_token: input.parse()?,
2039 body: {
2040 let body = input.call(expr_early)?;
David Tolnaye532d6b2018-08-30 16:55:01 -07002041 requires_comma = requires_terminator(&body);
David Tolnay310b3262018-08-30 15:33:00 -07002042 Box::new(body)
2043 },
2044 comma: {
2045 if requires_comma && !input.is_empty() {
2046 Some(input.parse()?)
2047 } else {
2048 input.parse()?
2049 }
2050 },
2051 })
Alex Crichton954046c2017-05-30 21:49:42 -07002052 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002053
Michael Layzell734adb42017-06-07 16:58:31 -04002054 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002055 fn expr_closure(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprClosure> {
David Tolnay60291082018-08-28 09:54:49 -07002056 let asyncness: Option<Token![async]> = input.parse()?;
2057 let movability: Option<Token![static]> = if asyncness.is_none() {
2058 input.parse()?
2059 } else {
2060 None
2061 };
2062 let capture: Option<Token![move]> = input.parse()?;
2063 let or1_token: Token![|] = input.parse()?;
2064
2065 let mut inputs = Punctuated::new();
2066 loop {
2067 if input.peek(Token![|]) {
2068 break;
2069 }
2070 let value = fn_arg(input)?;
2071 inputs.push_value(value);
2072 if input.peek(Token![|]) {
2073 break;
2074 }
2075 let punct: Token![,] = input.parse()?;
2076 inputs.push_punct(punct);
2077 }
2078
2079 let or2_token: Token![|] = input.parse()?;
2080
2081 let (output, body) = if input.peek(Token![->]) {
2082 let arrow_token: Token![->] = input.parse()?;
2083 let ty: Type = input.parse()?;
2084 let body: Block = input.parse()?;
2085 let output = ReturnType::Type(arrow_token, Box::new(ty));
2086 let block = Expr::Block(ExprBlock {
2087 attrs: Vec::new(),
2088 label: None,
2089 block: body,
2090 });
2091 (output, block)
2092 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07002093 let body = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002094 (ReturnType::Default, body)
2095 };
2096
2097 Ok(ExprClosure {
David Tolnay310b3262018-08-30 15:33:00 -07002098 attrs: Vec::new(),
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002099 asyncness: asyncness,
2100 movability: movability,
2101 capture: capture,
David Tolnay60291082018-08-28 09:54:49 -07002102 or1_token: or1_token,
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09002103 inputs: inputs,
David Tolnay60291082018-08-28 09:54:49 -07002104 or2_token: or2_token,
2105 output: output,
2106 body: Box::new(body),
2107 })
David Tolnay02a9c6f2018-08-24 18:58:45 -04002108 }
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002109
2110 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002111 fn expr_async(input: ParseStream) -> Result<ExprAsync> {
2112 Ok(ExprAsync {
2113 attrs: Vec::new(),
2114 async_token: input.parse()?,
2115 capture: input.parse()?,
2116 block: input.parse()?,
2117 })
David Tolnay60291082018-08-28 09:54:49 -07002118 }
Gregory Katz3e562cc2016-09-28 18:33:02 -04002119
Michael Layzell734adb42017-06-07 16:58:31 -04002120 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002121 fn fn_arg(input: ParseStream) -> Result<FnArg> {
2122 let pat: Pat = input.parse()?;
2123
2124 if input.peek(Token![:]) {
2125 Ok(FnArg::Captured(ArgCaptured {
2126 pat: pat,
2127 colon_token: input.parse()?,
2128 ty: input.parse()?,
2129 }))
2130 } else {
2131 Ok(FnArg::Inferred(pat))
2132 }
2133 }
2134
2135 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002136 fn expr_while(input: ParseStream) -> Result<ExprWhile> {
2137 let label: Option<Label> = input.parse()?;
2138 let while_token: Token![while] = input.parse()?;
2139 let cond = expr_no_struct(input)?;
David Tolnay60291082018-08-28 09:54:49 -07002140
David Tolnay310b3262018-08-30 15:33:00 -07002141 let content;
2142 let brace_token = braced!(content in input);
2143 let inner_attrs = content.call(Attribute::parse_inner)?;
2144 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07002145
David Tolnay310b3262018-08-30 15:33:00 -07002146 Ok(ExprWhile {
2147 attrs: inner_attrs,
2148 label: label,
2149 while_token: while_token,
2150 cond: Box::new(cond),
2151 body: Block {
2152 brace_token: brace_token,
2153 stmts: stmts,
2154 },
2155 })
Alex Crichton954046c2017-05-30 21:49:42 -07002156 }
2157
Michael Layzell734adb42017-06-07 16:58:31 -04002158 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002159 fn expr_while_let(input: ParseStream) -> Result<ExprWhileLet> {
2160 let label: Option<Label> = input.parse()?;
2161 let while_token: Token![while] = input.parse()?;
2162 let let_token: Token![let] = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -07002163
David Tolnay310b3262018-08-30 15:33:00 -07002164 let mut pats = Punctuated::new();
2165 let value: Pat = input.parse()?;
2166 pats.push_value(value);
2167 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2168 let punct = input.parse()?;
2169 pats.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002170 let value: Pat = input.parse()?;
2171 pats.push_value(value);
David Tolnay60291082018-08-28 09:54:49 -07002172 }
David Tolnay310b3262018-08-30 15:33:00 -07002173
2174 let eq_token: Token![=] = input.parse()?;
2175 let expr = expr_no_struct(input)?;
2176
2177 let content;
2178 let brace_token = braced!(content in input);
2179 let inner_attrs = content.call(Attribute::parse_inner)?;
2180 let stmts = content.call(Block::parse_within)?;
2181
2182 Ok(ExprWhileLet {
2183 attrs: inner_attrs,
2184 label: label,
2185 while_token: while_token,
2186 let_token: let_token,
2187 pats: pats,
2188 eq_token: eq_token,
2189 expr: Box::new(expr),
2190 body: Block {
2191 brace_token: brace_token,
2192 stmts: stmts,
2193 },
2194 })
David Tolnaybcd498f2017-12-29 12:02:33 -05002195 }
2196
2197 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002198 impl Parse for Label {
2199 fn parse(input: ParseStream) -> Result<Self> {
2200 Ok(Label {
2201 name: input.parse()?,
2202 colon_token: input.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -04002203 })
David Tolnay60291082018-08-28 09:54:49 -07002204 }
Alex Crichton954046c2017-05-30 21:49:42 -07002205 }
2206
Michael Layzell734adb42017-06-07 16:58:31 -04002207 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002208 impl Parse for Option<Label> {
2209 fn parse(input: ParseStream) -> Result<Self> {
2210 if input.peek(Lifetime) {
2211 input.parse().map(Some)
2212 } else {
2213 Ok(None)
2214 }
2215 }
2216 }
2217
2218 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002219 fn expr_continue(input: ParseStream) -> Result<ExprContinue> {
2220 Ok(ExprContinue {
2221 attrs: Vec::new(),
2222 continue_token: input.parse()?,
2223 label: input.parse()?,
2224 })
Alex Crichton954046c2017-05-30 21:49:42 -07002225 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002226
Michael Layzell734adb42017-06-07 16:58:31 -04002227 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002228 fn expr_break(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprBreak> {
2229 Ok(ExprBreak {
David Tolnay310b3262018-08-30 15:33:00 -07002230 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002231 break_token: input.parse()?,
2232 label: input.parse()?,
2233 expr: {
2234 if input.is_empty()
2235 || input.peek(Token![,])
2236 || input.peek(Token![;])
2237 || !allow_struct.0 && input.peek(token::Brace)
2238 {
2239 None
2240 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07002241 let expr = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002242 Some(Box::new(expr))
Michael Layzell92639a52017-06-01 00:07:44 -04002243 }
David Tolnay60291082018-08-28 09:54:49 -07002244 },
2245 })
Alex Crichton954046c2017-05-30 21:49:42 -07002246 }
2247
Michael Layzell734adb42017-06-07 16:58:31 -04002248 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002249 fn expr_ret(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprReturn> {
2250 Ok(ExprReturn {
David Tolnay310b3262018-08-30 15:33:00 -07002251 attrs: Vec::new(),
David Tolnay60291082018-08-28 09:54:49 -07002252 return_token: input.parse()?,
2253 expr: {
2254 if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) {
2255 None
2256 } else {
2257 // NOTE: return is greedy and eats blocks after it even when in a
2258 // position where structs are not allowed, such as in if statement
2259 // conditions. For example:
2260 //
2261 // if return { println!("A") } {} // Prints "A"
David Tolnay7d2e1db2018-08-30 11:49:04 -07002262 let expr = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002263 Some(Box::new(expr))
2264 }
2265 },
2266 })
2267 }
2268
2269 #[cfg(feature = "full")]
David Tolnay73b7ca12018-08-30 21:05:13 -07002270 fn expr_struct_helper(
2271 input: ParseStream,
2272 outer_attrs: Vec<Attribute>,
2273 path: Path,
2274 ) -> Result<ExprStruct> {
David Tolnay6e1e5052018-08-30 10:21:48 -07002275 let content;
2276 let brace_token = braced!(content in input);
2277 let inner_attrs = content.call(Attribute::parse_inner)?;
David Tolnay60291082018-08-28 09:54:49 -07002278
David Tolnay6e1e5052018-08-30 10:21:48 -07002279 let mut fields = Punctuated::new();
2280 loop {
2281 let attrs = content.call(Attribute::parse_outer)?;
2282 if content.fork().parse::<Member>().is_err() {
2283 if attrs.is_empty() {
David Tolnay60291082018-08-28 09:54:49 -07002284 break;
David Tolnay6e1e5052018-08-30 10:21:48 -07002285 } else {
2286 return Err(content.error("expected struct field"));
David Tolnay60291082018-08-28 09:54:49 -07002287 }
David Tolnay60291082018-08-28 09:54:49 -07002288 }
2289
David Tolnay6e1e5052018-08-30 10:21:48 -07002290 let member: Member = content.parse()?;
2291 let (colon_token, value) = if content.peek(Token![:]) || !member.is_named() {
2292 let colon_token: Token![:] = content.parse()?;
2293 let value: Expr = content.parse()?;
2294 (Some(colon_token), value)
2295 } else if let Member::Named(ref ident) = member {
2296 let value = Expr::Path(ExprPath {
2297 attrs: Vec::new(),
2298 qself: None,
2299 path: Path::from(ident.clone()),
2300 });
2301 (None, value)
David Tolnay60291082018-08-28 09:54:49 -07002302 } else {
David Tolnay6e1e5052018-08-30 10:21:48 -07002303 unreachable!()
David Tolnay60291082018-08-28 09:54:49 -07002304 };
2305
David Tolnay6e1e5052018-08-30 10:21:48 -07002306 fields.push(FieldValue {
2307 attrs: attrs,
2308 member: member,
2309 colon_token: colon_token,
2310 expr: value,
2311 });
2312
2313 if !content.peek(Token![,]) {
2314 break;
2315 }
2316 let punct: Token![,] = content.parse()?;
2317 fields.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002318 }
David Tolnay6e1e5052018-08-30 10:21:48 -07002319
2320 let (dot2_token, rest) = if fields.empty_or_trailing() && content.peek(Token![..]) {
2321 let dot2_token: Token![..] = content.parse()?;
2322 let rest: Expr = content.parse()?;
2323 (Some(dot2_token), Some(Box::new(rest)))
2324 } else {
2325 (None, None)
2326 };
2327
2328 Ok(ExprStruct {
2329 attrs: {
2330 let mut attrs = outer_attrs;
2331 attrs.extend(inner_attrs);
2332 attrs
2333 },
2334 brace_token: brace_token,
2335 path: path,
2336 fields: fields,
2337 dot2_token: dot2_token,
2338 rest: rest,
2339 })
Alex Crichton954046c2017-05-30 21:49:42 -07002340 }
David Tolnay055a7042016-10-02 19:23:54 -07002341
Michael Layzell734adb42017-06-07 16:58:31 -04002342 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002343 fn expr_unsafe(input: ParseStream) -> Result<ExprUnsafe> {
2344 let unsafe_token: Token![unsafe] = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -07002345
David Tolnay310b3262018-08-30 15:33:00 -07002346 let content;
2347 let brace_token = braced!(content in input);
2348 let inner_attrs = content.call(Attribute::parse_inner)?;
2349 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07002350
David Tolnay310b3262018-08-30 15:33:00 -07002351 Ok(ExprUnsafe {
2352 attrs: inner_attrs,
2353 unsafe_token: unsafe_token,
2354 block: Block {
2355 brace_token: brace_token,
2356 stmts: stmts,
2357 },
2358 })
Alex Crichton954046c2017-05-30 21:49:42 -07002359 }
David Tolnay055a7042016-10-02 19:23:54 -07002360
Michael Layzell734adb42017-06-07 16:58:31 -04002361 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002362 pub fn expr_block(input: ParseStream) -> Result<ExprBlock> {
2363 let label: Option<Label> = input.parse()?;
David Tolnay60291082018-08-28 09:54:49 -07002364
David Tolnay310b3262018-08-30 15:33:00 -07002365 let content;
2366 let brace_token = braced!(content in input);
2367 let inner_attrs = content.call(Attribute::parse_inner)?;
2368 let stmts = content.call(Block::parse_within)?;
David Tolnay60291082018-08-28 09:54:49 -07002369
David Tolnay310b3262018-08-30 15:33:00 -07002370 Ok(ExprBlock {
2371 attrs: inner_attrs,
2372 label: label,
2373 block: Block {
2374 brace_token: brace_token,
2375 stmts: stmts,
2376 },
2377 })
Alex Crichton954046c2017-05-30 21:49:42 -07002378 }
David Tolnay89e05672016-10-02 14:39:42 -07002379
Michael Layzell734adb42017-06-07 16:58:31 -04002380 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002381 fn expr_range(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprRange> {
2382 Ok(ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05002383 attrs: Vec::new(),
2384 from: None,
David Tolnay60291082018-08-28 09:54:49 -07002385 limits: input.parse()?,
2386 to: {
2387 if input.is_empty()
2388 || input.peek(Token![,])
2389 || input.peek(Token![;])
2390 || !allow_struct.0 && input.peek(token::Brace)
2391 {
2392 None
2393 } else {
David Tolnay7d2e1db2018-08-30 11:49:04 -07002394 let to = ambiguous_expr(input, allow_struct)?;
David Tolnay60291082018-08-28 09:54:49 -07002395 Some(Box::new(to))
2396 }
2397 },
2398 })
2399 }
David Tolnay438c9052016-10-07 23:24:48 -07002400
Michael Layzell734adb42017-06-07 16:58:31 -04002401 #[cfg(feature = "full")]
David Tolnayc4c631e2018-08-27 10:44:37 -07002402 impl Parse for RangeLimits {
2403 fn parse(input: ParseStream) -> Result<Self> {
2404 let lookahead = input.lookahead1();
2405 if lookahead.peek(Token![..=]) {
2406 input.parse().map(RangeLimits::Closed)
2407 } else if lookahead.peek(Token![...]) {
2408 let dot3: Token![...] = input.parse()?;
2409 Ok(RangeLimits::Closed(Token![..=](dot3.spans)))
2410 } else if lookahead.peek(Token![..]) {
2411 input.parse().map(RangeLimits::HalfOpen)
2412 } else {
2413 Err(lookahead.error())
2414 }
2415 }
Alex Crichton954046c2017-05-30 21:49:42 -07002416 }
David Tolnay438c9052016-10-07 23:24:48 -07002417
David Tolnay60291082018-08-28 09:54:49 -07002418 impl Parse for ExprPath {
2419 fn parse(input: ParseStream) -> Result<Self> {
2420 #[cfg(not(feature = "full"))]
2421 let attrs = Vec::new();
2422 #[cfg(feature = "full")]
2423 let attrs = input.call(Attribute::parse_outer)?;
David Tolnayeb981bb2018-07-21 19:31:38 -07002424
David Tolnay60291082018-08-28 09:54:49 -07002425 let (qself, path) = path::parsing::qpath(input, true)?;
2426
2427 Ok(ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002428 attrs: attrs,
David Tolnay60291082018-08-28 09:54:49 -07002429 qself: qself,
2430 path: path,
Michael Layzell92639a52017-06-01 00:07:44 -04002431 })
David Tolnay60291082018-08-28 09:54:49 -07002432 }
Alex Crichton954046c2017-05-30 21:49:42 -07002433 }
David Tolnay42602292016-10-01 22:25:45 -07002434
Michael Layzell734adb42017-06-07 16:58:31 -04002435 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002436 impl Parse for Block {
2437 fn parse(input: ParseStream) -> Result<Self> {
2438 let content;
2439 Ok(Block {
2440 brace_token: braced!(content in input),
2441 stmts: content.call(Block::parse_within)?,
Michael Layzell92639a52017-06-01 00:07:44 -04002442 })
David Tolnay60291082018-08-28 09:54:49 -07002443 }
Alex Crichton954046c2017-05-30 21:49:42 -07002444 }
David Tolnay939766a2016-09-23 23:48:12 -07002445
Michael Layzell734adb42017-06-07 16:58:31 -04002446 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002447 impl Block {
David Tolnay9389c382018-08-27 09:13:37 -07002448 pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
David Tolnaye532d6b2018-08-30 16:55:01 -07002449 let mut stmts = Vec::new();
David Tolnay7158c5f2018-08-30 17:28:34 -07002450 loop {
2451 while input.peek(Token![;]) {
2452 input.parse::<Token![;]>()?;
Michael Layzell92639a52017-06-01 00:07:44 -04002453 }
David Tolnay7158c5f2018-08-30 17:28:34 -07002454 if input.is_empty() {
2455 break;
2456 }
2457 let s = parse_stmt(input, true)?;
2458 let requires_semicolon = if let Stmt::Expr(ref s) = s {
2459 requires_terminator(s)
2460 } else {
2461 false
2462 };
David Tolnaye532d6b2018-08-30 16:55:01 -07002463 stmts.push(s);
David Tolnay7158c5f2018-08-30 17:28:34 -07002464 if input.is_empty() {
2465 break;
2466 } else if requires_semicolon {
2467 return Err(input.error("unexpected token"));
2468 }
David Tolnaye532d6b2018-08-30 16:55:01 -07002469 }
2470 Ok(stmts)
2471 }
Alex Crichton954046c2017-05-30 21:49:42 -07002472 }
2473
Michael Layzell734adb42017-06-07 16:58:31 -04002474 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002475 impl Parse for Stmt {
2476 fn parse(input: ParseStream) -> Result<Self> {
David Tolnaye532d6b2018-08-30 16:55:01 -07002477 parse_stmt(input, false)
2478 }
2479 }
David Tolnay939766a2016-09-23 23:48:12 -07002480
David Tolnaye532d6b2018-08-30 16:55:01 -07002481 #[cfg(feature = "full")]
2482 fn parse_stmt(input: ParseStream, allow_nosemi: bool) -> Result<Stmt> {
2483 let ahead = input.fork();
2484 ahead.call(Attribute::parse_outer)?;
2485
David Tolnaye532d6b2018-08-30 16:55:01 -07002486 if {
2487 let ahead = ahead.fork();
2488 // Only parse braces here; paren and bracket will get parsed as
2489 // expression statements
2490 ahead.call(Path::parse_mod_style).is_ok()
2491 && ahead.parse::<Token![!]>().is_ok()
2492 && (ahead.peek(token::Brace) || ahead.peek(Ident))
2493 } {
2494 stmt_mac(input)
2495 } else if ahead.peek(Token![let]) {
2496 stmt_local(input).map(Stmt::Local)
2497 } else if ahead.peek(Token![pub])
2498 || ahead.peek(Token![crate]) && !ahead.peek2(Token![::])
2499 || ahead.peek(Token![extern]) && !ahead.peek2(Token![::])
2500 || ahead.peek(Token![use])
2501 || ahead.peek(Token![static]) && (ahead.peek2(Token![mut]) || ahead.peek2(Ident))
2502 || ahead.peek(Token![const])
2503 || ahead.peek(Token![unsafe]) && !ahead.peek2(token::Brace)
2504 || ahead.peek(Token![async]) && (ahead.peek2(Token![extern]) || ahead.peek2(Token![fn]))
2505 || ahead.peek(Token![fn])
2506 || ahead.peek(Token![mod])
2507 || ahead.peek(Token![type])
2508 || ahead.peek(Token![existential]) && ahead.peek2(Token![type])
2509 || ahead.peek(Token![struct])
2510 || ahead.peek(Token![enum])
2511 || ahead.peek(Token![union]) && ahead.peek2(Ident)
2512 || ahead.peek(Token![auto]) && ahead.peek2(Token![trait])
2513 || ahead.peek(Token![trait])
David Tolnay73b7ca12018-08-30 21:05:13 -07002514 || ahead.peek(Token![default])
2515 && (ahead.peek2(Token![unsafe]) || ahead.peek2(Token![impl ]))
2516 || ahead.peek(Token![impl ])
David Tolnaye532d6b2018-08-30 16:55:01 -07002517 || ahead.peek(Token![macro])
2518 {
2519 input.parse().map(Stmt::Item)
2520 } else {
2521 stmt_expr(input, allow_nosemi)
David Tolnay60291082018-08-28 09:54:49 -07002522 }
Alex Crichton954046c2017-05-30 21:49:42 -07002523 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002524
Michael Layzell734adb42017-06-07 16:58:31 -04002525 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002526 fn stmt_mac(input: ParseStream) -> Result<Stmt> {
2527 let attrs = input.call(Attribute::parse_outer)?;
2528 let path = input.call(Path::parse_mod_style)?;
2529 let bang_token: Token![!] = input.parse()?;
2530 let ident: Option<Ident> = input.parse()?;
2531 let (delimiter, tts) = mac::parse_delimiter(input)?;
2532 let semi_token: Option<Token![;]> = input.parse()?;
2533
2534 Ok(Stmt::Item(Item::Macro(ItemMacro {
2535 attrs: attrs,
2536 ident: ident,
2537 mac: Macro {
2538 path: path,
2539 bang_token: bang_token,
2540 delimiter: delimiter,
2541 tts: tts,
2542 },
2543 semi_token: semi_token,
2544 })))
Alex Crichton954046c2017-05-30 21:49:42 -07002545 }
David Tolnay84aa0752016-10-02 23:01:13 -07002546
Michael Layzell734adb42017-06-07 16:58:31 -04002547 #[cfg(feature = "full")]
David Tolnay60291082018-08-28 09:54:49 -07002548 fn stmt_local(input: ParseStream) -> Result<Local> {
2549 Ok(Local {
2550 attrs: input.call(Attribute::parse_outer)?,
2551 let_token: input.parse()?,
2552 pats: {
2553 let mut pats = Punctuated::new();
2554 let value: Pat = input.parse()?;
2555 pats.push_value(value);
2556 while input.peek(Token![|]) && !input.peek(Token![||]) && !input.peek(Token![|=]) {
2557 let punct = input.parse()?;
2558 pats.push_punct(punct);
2559 let value: Pat = input.parse()?;
2560 pats.push_value(value);
2561 }
2562 pats
2563 },
2564 ty: {
2565 if input.peek(Token![:]) {
2566 let colon_token: Token![:] = input.parse()?;
2567 let ty: Type = input.parse()?;
2568 Some((colon_token, Box::new(ty)))
2569 } else {
2570 None
2571 }
2572 },
2573 init: {
2574 if input.peek(Token![=]) {
2575 let eq_token: Token![=] = input.parse()?;
2576 let init: Expr = input.parse()?;
2577 Some((eq_token, Box::new(init)))
2578 } else {
2579 None
2580 }
2581 },
2582 semi_token: input.parse()?,
2583 })
2584 }
2585
2586 #[cfg(feature = "full")]
David Tolnaye532d6b2018-08-30 16:55:01 -07002587 fn stmt_expr(input: ParseStream, allow_nosemi: bool) -> Result<Stmt> {
David Tolnay60291082018-08-28 09:54:49 -07002588 let mut attrs = input.call(Attribute::parse_outer)?;
David Tolnay01218d12018-08-29 18:13:07 -07002589 let mut e = expr_early(input)?;
David Tolnay60291082018-08-28 09:54:49 -07002590
2591 attrs.extend(e.replace_attrs(Vec::new()));
2592 e.replace_attrs(attrs);
2593
2594 if input.peek(Token![;]) {
David Tolnay01218d12018-08-29 18:13:07 -07002595 return Ok(Stmt::Semi(e, input.parse()?));
David Tolnay60291082018-08-28 09:54:49 -07002596 }
David Tolnay60291082018-08-28 09:54:49 -07002597
David Tolnayf00a2762018-08-30 17:22:22 -07002598 if allow_nosemi || !requires_terminator(&e) {
David Tolnaye532d6b2018-08-30 16:55:01 -07002599 Ok(Stmt::Expr(e))
2600 } else {
2601 Err(input.error("expected semicolon"))
David Tolnay01218d12018-08-29 18:13:07 -07002602 }
David Tolnay60291082018-08-28 09:54:49 -07002603 }
2604
2605 #[cfg(feature = "full")]
2606 impl Parse for Pat {
2607 fn parse(input: ParseStream) -> Result<Self> {
2608 // TODO: better error messages
2609 let lookahead = input.lookahead1();
2610 if lookahead.peek(Token![_]) {
David Tolnay310b3262018-08-30 15:33:00 -07002611 input.call(pat_wild).map(Pat::Wild)
David Tolnay60291082018-08-28 09:54:49 -07002612 } else if lookahead.peek(Token![box]) {
David Tolnay310b3262018-08-30 15:33:00 -07002613 input.call(pat_box).map(Pat::Box)
2614 } else if input.fork().call(pat_range).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002615 // must be before Pat::Lit
David Tolnay310b3262018-08-30 15:33:00 -07002616 input.call(pat_range).map(Pat::Range)
2617 } else if input.fork().call(pat_tuple_struct).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_tuple_struct).map(Pat::TupleStruct)
2620 } else if input.fork().call(pat_struct).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_struct).map(Pat::Struct)
2623 } else if input.fork().call(pat_macro).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002624 // must be before Pat::Ident
David Tolnay310b3262018-08-30 15:33:00 -07002625 input.call(pat_macro).map(Pat::Macro)
2626 } else if input.fork().call(pat_lit).is_ok() {
David Tolnay60291082018-08-28 09:54:49 -07002627 // must be before Pat::Ident
David Tolnay310b3262018-08-30 15:33:00 -07002628 input.call(pat_lit).map(Pat::Lit)
2629 } else if input.fork().call(pat_ident).is_ok() {
2630 input.call(pat_ident).map(Pat::Ident)
2631 } else if input.fork().call(pat_path).is_ok() {
2632 input.call(pat_path).map(Pat::Path)
David Tolnay60291082018-08-28 09:54:49 -07002633 } else if lookahead.peek(token::Paren) {
David Tolnay310b3262018-08-30 15:33:00 -07002634 input.call(pat_tuple).map(Pat::Tuple)
David Tolnay60291082018-08-28 09:54:49 -07002635 } else if lookahead.peek(Token![&]) {
David Tolnay310b3262018-08-30 15:33:00 -07002636 input.call(pat_ref).map(Pat::Ref)
David Tolnay60291082018-08-28 09:54:49 -07002637 } else if lookahead.peek(token::Bracket) {
David Tolnay310b3262018-08-30 15:33:00 -07002638 input.call(pat_slice).map(Pat::Slice)
David Tolnay60291082018-08-28 09:54:49 -07002639 } else {
2640 Err(lookahead.error())
2641 }
2642 }
2643 }
2644
2645 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002646 fn pat_wild(input: ParseStream) -> Result<PatWild> {
2647 Ok(PatWild {
2648 underscore_token: input.parse()?,
2649 })
Alex Crichton954046c2017-05-30 21:49:42 -07002650 }
2651
Michael Layzell734adb42017-06-07 16:58:31 -04002652 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002653 fn pat_box(input: ParseStream) -> Result<PatBox> {
2654 Ok(PatBox {
2655 box_token: input.parse()?,
2656 pat: input.parse()?,
2657 })
David Tolnay60291082018-08-28 09:54:49 -07002658 }
2659
2660 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002661 fn pat_ident(input: ParseStream) -> Result<PatIdent> {
2662 Ok(PatIdent {
2663 by_ref: input.parse()?,
2664 mutability: input.parse()?,
2665 ident: {
2666 let ident = if input.peek(Ident) || input.peek(Token![self]) {
David Tolnay0dea1b92018-08-30 17:47:29 -07002667 input.call(Ident::parse_any)?
David Tolnay310b3262018-08-30 15:33:00 -07002668 } else {
2669 return Err(input.error("expected identifier or `self`"));
2670 };
2671 if input.peek(Token![<]) || input.peek(Token![::]) {
2672 return Err(input.error("unexpected token"));
David Tolnay60291082018-08-28 09:54:49 -07002673 }
David Tolnay310b3262018-08-30 15:33:00 -07002674 ident
2675 },
2676 subpat: {
2677 if input.peek(Token![@]) {
2678 let at_token: Token![@] = input.parse()?;
2679 let subpat: Pat = input.parse()?;
2680 Some((at_token, Box::new(subpat)))
2681 } else {
2682 None
2683 }
2684 },
2685 })
David Tolnay60291082018-08-28 09:54:49 -07002686 }
2687
2688 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002689 fn pat_tuple_struct(input: ParseStream) -> Result<PatTupleStruct> {
2690 Ok(PatTupleStruct {
2691 path: input.parse()?,
2692 pat: input.call(pat_tuple)?,
2693 })
2694 }
David Tolnay60291082018-08-28 09:54:49 -07002695
David Tolnay310b3262018-08-30 15:33:00 -07002696 #[cfg(feature = "full")]
2697 fn pat_struct(input: ParseStream) -> Result<PatStruct> {
2698 let path: Path = input.parse()?;
2699
2700 let content;
2701 let brace_token = braced!(content in input);
2702
2703 let mut fields = Punctuated::new();
2704 while !content.is_empty() && !content.peek(Token![..]) {
2705 let value = content.call(field_pat)?;
2706 fields.push_value(value);
2707 if !content.peek(Token![,]) {
2708 break;
David Tolnay60291082018-08-28 09:54:49 -07002709 }
David Tolnay310b3262018-08-30 15:33:00 -07002710 let punct: Token![,] = content.parse()?;
2711 fields.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002712 }
David Tolnay310b3262018-08-30 15:33:00 -07002713
2714 let dot2_token = if fields.empty_or_trailing() && content.peek(Token![..]) {
2715 Some(content.parse()?)
2716 } else {
2717 None
2718 };
2719
2720 Ok(PatStruct {
2721 path: path,
2722 brace_token: brace_token,
2723 fields: fields,
2724 dot2_token: dot2_token,
2725 })
2726 }
2727
2728 #[cfg(feature = "full")]
2729 fn field_pat(input: ParseStream) -> Result<FieldPat> {
2730 let boxed: Option<Token![box]> = input.parse()?;
2731 let by_ref: Option<Token![ref]> = input.parse()?;
2732 let mutability: Option<Token![mut]> = input.parse()?;
2733 let member: Member = input.parse()?;
2734
2735 if boxed.is_none() && by_ref.is_none() && mutability.is_none() && input.peek(Token![:])
2736 || member.is_unnamed()
2737 {
2738 return Ok(FieldPat {
2739 attrs: Vec::new(),
2740 member: member,
2741 colon_token: input.parse()?,
2742 pat: input.parse()?,
2743 });
2744 }
2745
2746 let ident = match member {
2747 Member::Named(ident) => ident,
2748 Member::Unnamed(_) => unreachable!(),
2749 };
2750
2751 let mut pat = Pat::Ident(PatIdent {
2752 by_ref: by_ref,
2753 mutability: mutability,
2754 ident: ident.clone(),
2755 subpat: None,
2756 });
2757
2758 if let Some(boxed) = boxed {
2759 pat = Pat::Box(PatBox {
2760 pat: Box::new(pat),
2761 box_token: boxed,
2762 });
2763 }
2764
2765 Ok(FieldPat {
2766 member: Member::Named(ident),
2767 pat: Box::new(pat),
2768 attrs: Vec::new(),
2769 colon_token: None,
2770 })
Alex Crichton954046c2017-05-30 21:49:42 -07002771 }
2772
David Tolnay1501f7e2018-08-27 14:21:03 -07002773 impl Parse for Member {
2774 fn parse(input: ParseStream) -> Result<Self> {
2775 if input.peek(Ident) {
2776 input.parse().map(Member::Named)
2777 } else if input.peek(LitInt) {
2778 input.parse().map(Member::Unnamed)
2779 } else {
2780 Err(input.error("expected identifier or integer"))
2781 }
2782 }
David Tolnay85b69a42017-12-27 20:43:10 -05002783 }
2784
David Tolnay1501f7e2018-08-27 14:21:03 -07002785 impl Parse for Index {
2786 fn parse(input: ParseStream) -> Result<Self> {
2787 let lit: LitInt = input.parse()?;
2788 if let IntSuffix::None = lit.suffix() {
2789 Ok(Index {
2790 index: lit.value() as u32,
2791 span: lit.span(),
2792 })
2793 } else {
2794 Err(input.error("expected unsuffixed integer"))
2795 }
2796 }
David Tolnay85b69a42017-12-27 20:43:10 -05002797 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002798
Michael Layzell734adb42017-06-07 16:58:31 -04002799 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002800 fn pat_path(input: ParseStream) -> Result<PatPath> {
2801 let p: ExprPath = input.parse()?;
2802 Ok(PatPath {
2803 qself: p.qself,
2804 path: p.path,
2805 })
Alex Crichton954046c2017-05-30 21:49:42 -07002806 }
David Tolnay9636c052016-10-02 17:11:17 -07002807
Michael Layzell734adb42017-06-07 16:58:31 -04002808 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002809 fn pat_tuple(input: ParseStream) -> Result<PatTuple> {
2810 let content;
2811 let paren_token = parenthesized!(content in input);
David Tolnay60291082018-08-28 09:54:49 -07002812
David Tolnay310b3262018-08-30 15:33:00 -07002813 let mut front = Punctuated::new();
2814 let mut dot2_token = None::<Token![..]>;
2815 let mut comma_token = None::<Token![,]>;
2816 loop {
2817 if content.is_empty() {
2818 break;
David Tolnay60291082018-08-28 09:54:49 -07002819 }
David Tolnay310b3262018-08-30 15:33:00 -07002820 if content.peek(Token![..]) {
2821 dot2_token = Some(content.parse()?);
2822 comma_token = content.parse()?;
2823 break;
David Tolnay60291082018-08-28 09:54:49 -07002824 }
David Tolnay310b3262018-08-30 15:33:00 -07002825 let value: Pat = content.parse()?;
2826 front.push_value(value);
2827 if content.is_empty() {
2828 break;
2829 }
2830 let punct = content.parse()?;
2831 front.push_punct(punct);
2832 }
2833
David Tolnayf5ebc192018-08-30 18:23:46 -07002834 let mut back = Punctuated::new();
2835 while !content.is_empty() {
2836 let value: Pat = content.parse()?;
2837 back.push_value(value);
2838 if content.is_empty() {
2839 break;
2840 }
2841 let punct = content.parse()?;
2842 back.push_punct(punct);
2843 }
David Tolnay310b3262018-08-30 15:33:00 -07002844
2845 Ok(PatTuple {
2846 paren_token: paren_token,
2847 front: front,
2848 dot2_token: dot2_token,
2849 comma_token: comma_token,
2850 back: back,
2851 })
2852 }
2853
2854 #[cfg(feature = "full")]
2855 fn pat_ref(input: ParseStream) -> Result<PatRef> {
2856 Ok(PatRef {
2857 and_token: input.parse()?,
2858 mutability: input.parse()?,
2859 pat: input.parse()?,
2860 })
2861 }
2862
2863 #[cfg(feature = "full")]
2864 fn pat_lit(input: ParseStream) -> Result<PatLit> {
2865 if input.peek(Lit) || input.peek(Token![-]) && input.peek2(Lit) {
2866 Ok(PatLit {
2867 expr: input.call(pat_lit_expr)?,
2868 })
2869 } else {
2870 Err(input.error("expected literal pattern"))
David Tolnay60291082018-08-28 09:54:49 -07002871 }
2872 }
2873
2874 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002875 fn pat_range(input: ParseStream) -> Result<PatRange> {
2876 Ok(PatRange {
2877 lo: input.call(pat_lit_expr)?,
2878 limits: input.parse()?,
2879 hi: input.call(pat_lit_expr)?,
2880 })
David Tolnay60291082018-08-28 09:54:49 -07002881 }
2882
2883 #[cfg(feature = "full")]
2884 fn pat_lit_expr(input: ParseStream) -> Result<Box<Expr>> {
2885 let neg: Option<Token![-]> = input.parse()?;
2886
2887 let lookahead = input.lookahead1();
2888 let expr = if lookahead.peek(Lit) {
David Tolnay310b3262018-08-30 15:33:00 -07002889 Expr::Lit(input.call(expr_lit)?)
David Tolnay60291082018-08-28 09:54:49 -07002890 } else if lookahead.peek(Ident)
2891 || lookahead.peek(Token![::])
2892 || lookahead.peek(Token![<])
2893 || lookahead.peek(Token![self])
2894 || lookahead.peek(Token![Self])
2895 || lookahead.peek(Token![super])
2896 || lookahead.peek(Token![extern])
2897 || lookahead.peek(Token![crate])
2898 {
2899 Expr::Path(input.parse()?)
2900 } else {
2901 return Err(lookahead.error());
2902 };
2903
2904 Ok(Box::new(if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002905 Expr::Unary(ExprUnary {
2906 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002907 op: UnOp::Neg(neg),
David Tolnay60291082018-08-28 09:54:49 -07002908 expr: Box::new(expr),
David Tolnay3bc597f2017-12-31 02:31:11 -05002909 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002910 } else {
David Tolnay60291082018-08-28 09:54:49 -07002911 expr
2912 }))
Alex Crichton954046c2017-05-30 21:49:42 -07002913 }
David Tolnay323279a2017-12-29 11:26:32 -05002914
2915 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002916 fn pat_slice(input: ParseStream) -> Result<PatSlice> {
2917 let content;
2918 let bracket_token = bracketed!(content in input);
David Tolnay60291082018-08-28 09:54:49 -07002919
David Tolnay310b3262018-08-30 15:33:00 -07002920 let mut front = Punctuated::new();
2921 let mut middle = None;
2922 loop {
2923 if content.is_empty() || content.peek(Token![..]) {
2924 break;
David Tolnay60291082018-08-28 09:54:49 -07002925 }
David Tolnay310b3262018-08-30 15:33:00 -07002926 let value: Pat = content.parse()?;
2927 if content.peek(Token![..]) {
2928 middle = Some(Box::new(value));
2929 break;
David Tolnay60291082018-08-28 09:54:49 -07002930 }
David Tolnay310b3262018-08-30 15:33:00 -07002931 front.push_value(value);
2932 if content.is_empty() {
2933 break;
2934 }
2935 let punct = content.parse()?;
2936 front.push_punct(punct);
David Tolnay60291082018-08-28 09:54:49 -07002937 }
David Tolnay310b3262018-08-30 15:33:00 -07002938
2939 let dot2_token: Option<Token![..]> = content.parse()?;
2940 let mut comma_token = None::<Token![,]>;
2941 let mut back = Punctuated::new();
2942 if dot2_token.is_some() {
2943 comma_token = content.parse()?;
2944 if comma_token.is_some() {
2945 loop {
2946 if content.is_empty() {
2947 break;
2948 }
2949 let value: Pat = content.parse()?;
2950 back.push_value(value);
2951 if content.is_empty() {
2952 break;
2953 }
2954 let punct = content.parse()?;
2955 back.push_punct(punct);
2956 }
2957 }
2958 }
2959
2960 Ok(PatSlice {
2961 bracket_token: bracket_token,
2962 front: front,
2963 middle: middle,
2964 dot2_token: dot2_token,
2965 comma_token: comma_token,
2966 back: back,
2967 })
David Tolnay60291082018-08-28 09:54:49 -07002968 }
2969
2970 #[cfg(feature = "full")]
David Tolnay310b3262018-08-30 15:33:00 -07002971 fn pat_macro(input: ParseStream) -> Result<PatMacro> {
2972 Ok(PatMacro {
2973 mac: input.parse()?,
2974 })
David Tolnay323279a2017-12-29 11:26:32 -05002975 }
David Tolnay1501f7e2018-08-27 14:21:03 -07002976
2977 #[cfg(feature = "full")]
2978 impl Member {
2979 fn is_named(&self) -> bool {
2980 match *self {
2981 Member::Named(_) => true,
2982 Member::Unnamed(_) => false,
2983 }
2984 }
David Tolnay60291082018-08-28 09:54:49 -07002985
2986 fn is_unnamed(&self) -> bool {
2987 match *self {
2988 Member::Named(_) => false,
2989 Member::Unnamed(_) => true,
2990 }
2991 }
David Tolnay1501f7e2018-08-27 14:21:03 -07002992 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002993}
2994
David Tolnayf4bbbd92016-09-23 14:41:55 -07002995#[cfg(feature = "printing")]
2996mod printing {
2997 use super::*;
David Tolnay64023912018-08-31 09:51:12 -07002998
Alex Crichtona74a1c82018-05-16 10:20:44 -07002999 use proc_macro2::{Literal, TokenStream};
3000 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003001
David Tolnay64023912018-08-31 09:51:12 -07003002 #[cfg(feature = "full")]
3003 use attr::FilterAttrs;
3004 #[cfg(feature = "full")]
3005 use print::TokensOrDefault;
3006
David Tolnaybcf26022017-12-25 22:10:52 -05003007 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003008 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003009 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003010 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003011 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003012 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003013 e.to_tokens(tokens);
3014 });
3015 } else {
3016 e.to_tokens(tokens);
3017 }
3018 }
3019
David Tolnay8c91b882017-12-28 23:04:32 -05003020 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003021 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003022 tokens.append_all(attrs.outer());
3023 }
Michael Layzell734adb42017-06-07 16:58:31 -04003024
David Tolnayd997aef2018-07-21 18:42:31 -07003025 #[cfg(feature = "full")]
3026 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3027 tokens.append_all(attrs.inner());
3028 }
3029
David Tolnay8c91b882017-12-28 23:04:32 -05003030 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003031 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3032
3033 #[cfg(not(feature = "full"))]
3034 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003035
Michael Layzell734adb42017-06-07 16:58:31 -04003036 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003037 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003038 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003039 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003040 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 self.expr.to_tokens(tokens);
3042 }
3043 }
3044
Michael Layzell734adb42017-06-07 16:58:31 -04003045 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003046 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003047 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003048 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003049 self.place.to_tokens(tokens);
3050 self.arrow_token.to_tokens(tokens);
3051 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003052 }
3053 }
3054
Michael Layzell734adb42017-06-07 16:58:31 -04003055 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003056 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003057 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003058 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003059 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003060 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003061 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003062 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003063 }
3064 }
3065
3066 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003067 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003068 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003069 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003070 self.paren_token.surround(tokens, |tokens| {
3071 self.args.to_tokens(tokens);
3072 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003073 }
3074 }
3075
Michael Layzell734adb42017-06-07 16:58:31 -04003076 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003077 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003078 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003079 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003080 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003081 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003082 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003083 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003084 self.paren_token.surround(tokens, |tokens| {
3085 self.args.to_tokens(tokens);
3086 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003087 }
3088 }
3089
Michael Layzell734adb42017-06-07 16:58:31 -04003090 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003091 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003092 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003093 self.colon2_token.to_tokens(tokens);
3094 self.lt_token.to_tokens(tokens);
3095 self.args.to_tokens(tokens);
3096 self.gt_token.to_tokens(tokens);
3097 }
3098 }
3099
3100 #[cfg(feature = "full")]
3101 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003102 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003103 match *self {
3104 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3105 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3106 }
3107 }
3108 }
3109
3110 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003111 impl ToTokens for ExprTuple {
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 Crichtonccbb45d2017-05-23 10:58:24 -07003114 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003115 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003116 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003117 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003118 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003119 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003120 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003121 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003122 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003123 }
3124 }
3125
3126 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003127 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003128 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003129 self.left.to_tokens(tokens);
3130 self.op.to_tokens(tokens);
3131 self.right.to_tokens(tokens);
3132 }
3133 }
3134
3135 impl ToTokens for ExprUnary {
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.op.to_tokens(tokens);
3139 self.expr.to_tokens(tokens);
3140 }
3141 }
3142
David Tolnay8c91b882017-12-28 23:04:32 -05003143 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003144 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003145 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003146 self.lit.to_tokens(tokens);
3147 }
3148 }
3149
Alex Crichton62a0a592017-05-22 13:58:53 -07003150 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003151 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003152 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003153 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003154 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003155 self.ty.to_tokens(tokens);
3156 }
3157 }
3158
David Tolnay0cf94f22017-12-28 23:46:26 -05003159 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003160 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003161 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003162 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003163 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003164 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003165 self.ty.to_tokens(tokens);
3166 }
3167 }
3168
Michael Layzell734adb42017-06-07 16:58:31 -04003169 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003170 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003171 if let Some((ref else_token, ref else_)) = *else_ {
3172 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003173
3174 // If we are not one of the valid expressions to exist in an else
3175 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003176 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003177 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003178 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003179 }
3180 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003181 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003182 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003183 });
3184 }
3185 }
3186 }
3187 }
3188
3189 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003190 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003191 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003192 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003193 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003194 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003195 self.then_branch.to_tokens(tokens);
3196 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003197 }
3198 }
3199
Michael Layzell734adb42017-06-07 16:58:31 -04003200 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003201 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003202 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003203 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003204 self.if_token.to_tokens(tokens);
3205 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003206 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003207 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003208 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003209 self.then_branch.to_tokens(tokens);
3210 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003211 }
3212 }
3213
Michael Layzell734adb42017-06-07 16:58:31 -04003214 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003215 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003216 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003217 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003218 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003219 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003220 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003221 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003222 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003223 tokens.append_all(&self.body.stmts);
3224 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003225 }
3226 }
3227
Michael Layzell734adb42017-06-07 16:58:31 -04003228 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003229 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003230 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003231 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003232 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003233 self.while_token.to_tokens(tokens);
3234 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003235 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003236 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003237 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003238 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003239 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003240 tokens.append_all(&self.body.stmts);
3241 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003242 }
3243 }
3244
Michael Layzell734adb42017-06-07 16:58:31 -04003245 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003246 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003247 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003248 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003249 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003250 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003251 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003252 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003253 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003254 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003255 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003256 tokens.append_all(&self.body.stmts);
3257 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003258 }
3259 }
3260
Michael Layzell734adb42017-06-07 16:58:31 -04003261 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003262 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003263 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003264 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003265 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003266 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003267 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003268 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003269 tokens.append_all(&self.body.stmts);
3270 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003271 }
3272 }
3273
Michael Layzell734adb42017-06-07 16:58:31 -04003274 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003275 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003276 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003277 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003278 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003279 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003280 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003281 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003282 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003283 arm.to_tokens(tokens);
3284 // Ensure that we have a comma after a non-block arm, except
3285 // for the last one.
3286 let is_last = i == self.arms.len() - 1;
David Tolnaye532d6b2018-08-30 16:55:01 -07003287 if !is_last && requires_terminator(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003288 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003289 }
3290 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003291 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003292 }
3293 }
3294
Michael Layzell734adb42017-06-07 16:58:31 -04003295 #[cfg(feature = "full")]
David Tolnay02a9c6f2018-08-24 18:58:45 -04003296 impl ToTokens for ExprAsync {
3297 fn to_tokens(&self, tokens: &mut TokenStream) {
3298 outer_attrs_to_tokens(&self.attrs, tokens);
3299 self.async_token.to_tokens(tokens);
3300 self.capture.to_tokens(tokens);
3301 self.block.to_tokens(tokens);
3302 }
3303 }
3304
3305 #[cfg(feature = "full")]
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003306 impl ToTokens for ExprTryBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003307 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003308 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnayfb2dd4b2018-08-24 16:45:34 -04003309 self.try_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003310 self.block.to_tokens(tokens);
3311 }
3312 }
3313
Michael Layzell734adb42017-06-07 16:58:31 -04003314 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003315 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003316 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003317 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003318 self.yield_token.to_tokens(tokens);
3319 self.expr.to_tokens(tokens);
3320 }
3321 }
3322
3323 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003324 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003325 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003326 outer_attrs_to_tokens(&self.attrs, tokens);
Yusuke Sasaki4e5d9662018-07-21 02:49:47 +09003327 self.asyncness.to_tokens(tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003328 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003329 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003330 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003331 for input in self.inputs.pairs() {
3332 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003333 FnArg::Captured(ArgCaptured {
3334 ref pat,
3335 ty: Type::Infer(_),
3336 ..
3337 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003338 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003339 }
David Tolnay56080682018-01-06 14:01:52 -08003340 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003341 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003342 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003343 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003344 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003345 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003346 self.body.to_tokens(tokens);
3347 }
3348 }
3349
Michael Layzell734adb42017-06-07 16:58:31 -04003350 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003351 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003352 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003353 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003354 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003355 self.block.brace_token.surround(tokens, |tokens| {
3356 inner_attrs_to_tokens(&self.attrs, tokens);
3357 tokens.append_all(&self.block.stmts);
3358 });
Nika Layzell640832a2017-12-04 13:37:09 -05003359 }
3360 }
3361
3362 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003363 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003364 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003365 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay1d8e9962018-08-24 19:04:20 -04003366 self.label.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003367 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003368 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003369 tokens.append_all(&self.block.stmts);
3370 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003371 }
3372 }
3373
Michael Layzell734adb42017-06-07 16:58:31 -04003374 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003375 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003376 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003377 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003378 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003379 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003380 self.right.to_tokens(tokens);
3381 }
3382 }
3383
Michael Layzell734adb42017-06-07 16:58:31 -04003384 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003385 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003386 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003387 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003388 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003389 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003390 self.right.to_tokens(tokens);
3391 }
3392 }
3393
3394 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003395 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003396 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003397 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003398 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003399 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003400 }
3401 }
3402
David Tolnay85b69a42017-12-27 20:43:10 -05003403 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003404 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003405 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003406 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003407 Member::Unnamed(ref index) => index.to_tokens(tokens),
3408 }
3409 }
3410 }
3411
David Tolnay85b69a42017-12-27 20:43:10 -05003412 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003413 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003414 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3415 lit.set_span(self.span);
3416 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003417 }
3418 }
3419
3420 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003421 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003422 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003423 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003424 self.bracket_token.surround(tokens, |tokens| {
3425 self.index.to_tokens(tokens);
3426 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003427 }
3428 }
3429
Michael Layzell734adb42017-06-07 16:58:31 -04003430 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003431 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003432 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003433 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003434 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003435 match self.limits {
3436 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3437 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3438 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003439 self.to.to_tokens(tokens);
3440 }
3441 }
3442
3443 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003444 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003445 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003446 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003447 }
3448 }
3449
Michael Layzell734adb42017-06-07 16:58:31 -04003450 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003451 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003452 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003453 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003454 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003455 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003456 self.expr.to_tokens(tokens);
3457 }
3458 }
3459
Michael Layzell734adb42017-06-07 16:58:31 -04003460 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003461 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003462 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003463 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003464 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003465 self.label.to_tokens(tokens);
3466 self.expr.to_tokens(tokens);
3467 }
3468 }
3469
Michael Layzell734adb42017-06-07 16:58:31 -04003470 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003471 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003472 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003473 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003474 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003475 self.label.to_tokens(tokens);
3476 }
3477 }
3478
Michael Layzell734adb42017-06-07 16:58:31 -04003479 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003480 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003481 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003482 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003483 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003484 self.expr.to_tokens(tokens);
3485 }
3486 }
3487
Michael Layzell734adb42017-06-07 16:58:31 -04003488 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003489 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003490 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003491 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003492 self.mac.to_tokens(tokens);
3493 }
3494 }
3495
3496 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003497 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003498 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003499 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003500 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003501 self.brace_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.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003504 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003505 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003506 self.rest.to_tokens(tokens);
3507 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003508 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003509 }
3510 }
3511
Michael Layzell734adb42017-06-07 16:58:31 -04003512 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003513 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003514 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003515 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003516 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003517 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003518 self.expr.to_tokens(tokens);
3519 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003520 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003521 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003522 }
3523 }
3524
David Tolnaye98775f2017-12-28 23:17:00 -05003525 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003526 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003527 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003528 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003529 self.group_token.surround(tokens, |tokens| {
3530 self.expr.to_tokens(tokens);
3531 });
3532 }
3533 }
3534
Alex Crichton62a0a592017-05-22 13:58:53 -07003535 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003536 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003537 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003538 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003539 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003540 self.expr.to_tokens(tokens);
3541 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003542 }
3543 }
3544
Michael Layzell734adb42017-06-07 16:58:31 -04003545 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003546 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003547 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003548 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003549 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003550 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003551 }
3552 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003553
David Tolnay2ae520a2017-12-29 11:19:50 -05003554 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003555 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003556 self.tts.to_tokens(tokens);
3557 }
3558 }
3559
Michael Layzell734adb42017-06-07 16:58:31 -04003560 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003561 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003562 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003563 self.name.to_tokens(tokens);
3564 self.colon_token.to_tokens(tokens);
3565 }
3566 }
3567
3568 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003569 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003570 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003571 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003572 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003573 if let Some(ref colon_token) = self.colon_token {
3574 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003575 self.expr.to_tokens(tokens);
3576 }
David Tolnay055a7042016-10-02 19:23:54 -07003577 }
3578 }
3579
Michael Layzell734adb42017-06-07 16:58:31 -04003580 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003581 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003582 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003583 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003584 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003585 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003586 if let Some((ref if_token, ref guard)) = self.guard {
3587 if_token.to_tokens(tokens);
3588 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003589 }
David Tolnaydfb91432018-03-31 19:19:44 +02003590 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003591 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003592 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003593 }
3594 }
3595
Michael Layzell734adb42017-06-07 16:58:31 -04003596 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003597 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003598 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003599 self.underscore_token.to_tokens(tokens);
3600 }
3601 }
3602
Michael Layzell734adb42017-06-07 16:58:31 -04003603 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003604 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003605 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003606 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003607 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003608 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003609 if let Some((ref at_token, ref subpat)) = self.subpat {
3610 at_token.to_tokens(tokens);
3611 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003612 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003613 }
3614 }
3615
Michael Layzell734adb42017-06-07 16:58:31 -04003616 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003617 impl ToTokens for PatStruct {
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.brace_token.surround(tokens, |tokens| {
3621 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003622 // NOTE: We need a comma before the dot2 token if it is present.
3623 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003624 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003625 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003626 self.dot2_token.to_tokens(tokens);
3627 });
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 PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003633 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003634 self.path.to_tokens(tokens);
3635 self.pat.to_tokens(tokens);
3636 }
3637 }
3638
Michael Layzell734adb42017-06-07 16:58:31 -04003639 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003640 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003641 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003642 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3643 }
3644 }
3645
Michael Layzell734adb42017-06-07 16:58:31 -04003646 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003647 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003648 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003649 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003650 self.front.to_tokens(tokens);
3651 if let Some(ref dot2_token) = self.dot2_token {
3652 if !self.front.empty_or_trailing() {
3653 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003654 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003655 }
David Tolnay41871922017-12-29 01:53:45 -05003656 dot2_token.to_tokens(tokens);
3657 self.comma_token.to_tokens(tokens);
3658 if self.comma_token.is_none() && !self.back.is_empty() {
3659 // Ensure there is a comma after the .. token.
3660 <Token![,]>::default().to_tokens(tokens);
3661 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003662 }
David Tolnay41871922017-12-29 01:53:45 -05003663 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003664 });
3665 }
3666 }
3667
Michael Layzell734adb42017-06-07 16:58:31 -04003668 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003669 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003670 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003671 self.box_token.to_tokens(tokens);
3672 self.pat.to_tokens(tokens);
3673 }
3674 }
3675
Michael Layzell734adb42017-06-07 16:58:31 -04003676 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003677 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003678 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003679 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003680 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003681 self.pat.to_tokens(tokens);
3682 }
3683 }
3684
Michael Layzell734adb42017-06-07 16:58:31 -04003685 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003686 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003687 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003688 self.expr.to_tokens(tokens);
3689 }
3690 }
3691
Michael Layzell734adb42017-06-07 16:58:31 -04003692 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003693 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003694 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003695 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003696 match self.limits {
3697 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
David Tolnay7ac699c2018-08-24 14:00:58 -04003698 RangeLimits::Closed(ref t) => Token![...](t.spans).to_tokens(tokens),
David Tolnay475288a2017-12-19 22:59:44 -08003699 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003700 self.hi.to_tokens(tokens);
3701 }
3702 }
3703
Michael Layzell734adb42017-06-07 16:58:31 -04003704 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003705 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003706 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003707 // XXX: This is a mess, and it will be so easy to screw it up. How
3708 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003709 self.bracket_token.surround(tokens, |tokens| {
3710 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003711
3712 // If we need a comma before the middle or standalone .. token,
3713 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003714 if !self.front.empty_or_trailing()
3715 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003716 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003717 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003718 }
3719
3720 // If we have an identifier, we always need a .. token.
3721 if self.middle.is_some() {
3722 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003723 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003724 } else if self.dot2_token.is_some() {
3725 self.dot2_token.to_tokens(tokens);
3726 }
3727
3728 // Make sure we have a comma before the back half.
3729 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003730 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003731 self.back.to_tokens(tokens);
3732 } else {
3733 self.comma_token.to_tokens(tokens);
3734 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003735 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003736 }
3737 }
3738
Michael Layzell734adb42017-06-07 16:58:31 -04003739 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003740 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003741 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003742 self.mac.to_tokens(tokens);
3743 }
3744 }
3745
3746 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003747 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003748 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003749 self.tts.to_tokens(tokens);
3750 }
3751 }
3752
3753 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003754 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003755 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003756 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003757 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003758 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003759 }
3760 self.pat.to_tokens(tokens);
3761 }
3762 }
3763
Michael Layzell734adb42017-06-07 16:58:31 -04003764 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003765 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003766 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003767 self.brace_token.surround(tokens, |tokens| {
3768 tokens.append_all(&self.stmts);
3769 });
David Tolnay42602292016-10-01 22:25:45 -07003770 }
3771 }
3772
Michael Layzell734adb42017-06-07 16:58:31 -04003773 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003774 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003775 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003776 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003777 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003778 Stmt::Item(ref item) => item.to_tokens(tokens),
3779 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003780 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003781 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003782 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003783 }
David Tolnay42602292016-10-01 22:25:45 -07003784 }
3785 }
3786 }
David Tolnay191e0582016-10-02 18:31:09 -07003787
Michael Layzell734adb42017-06-07 16:58:31 -04003788 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003789 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003790 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003791 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003792 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003793 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003794 if let Some((ref colon_token, ref ty)) = self.ty {
3795 colon_token.to_tokens(tokens);
3796 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003797 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003798 if let Some((ref eq_token, ref init)) = self.init {
3799 eq_token.to_tokens(tokens);
3800 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003801 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003802 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003803 }
3804 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003805}