blob: 5dec492a360defc60ab435c0e72f5904782d9b13 [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 Tolnayf2cfd722017-12-31 18:02:51 -050010use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -050011use proc_macro2::{Span, TokenStream};
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 = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050015use tt::TokenStreamHelper;
David Tolnay2ae520a2017-12-29 11:19:50 -050016#[cfg(feature = "full")]
17use std::mem;
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 Tolnay4a3f59a2017-12-28 21:21:12 -0500245 pub pat: Box<Pat>,
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 Tolnay4a3f59a2017-12-28 21:21:12 -0500271 pub pat: Box<Pat>,
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>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500316 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800317 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500318 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500320 pub output: ReturnType,
321 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500323
David Tolnaya454c8f2018-01-07 01:01:10 -0800324 /// An unsafe block: `unsafe { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800325 ///
326 /// *This type is available if Syn is built with the `"full"` feature.*
Nika Layzell640832a2017-12-04 13:37:09 -0500327 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500328 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500329 pub unsafe_token: Token![unsafe],
330 pub block: Block,
331 }),
332
David Tolnaya454c8f2018-01-07 01:01:10 -0800333 /// A blocked scope: `{ ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800334 ///
335 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400336 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500337 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700338 pub block: Block,
339 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700340
David Tolnaya454c8f2018-01-07 01:01:10 -0800341 /// An assignment expression: `a = compute()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800342 ///
343 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400344 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500345 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700346 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800347 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500348 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500350
David Tolnaya454c8f2018-01-07 01:01:10 -0800351 /// A compound assignment expression: `counter += 1`.
David Tolnay461d98e2018-01-07 11:07:19 -0800352 ///
353 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400354 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500355 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700356 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500357 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700358 pub right: Box<Expr>,
359 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500360
David Tolnaya454c8f2018-01-07 01:01:10 -0800361 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500362 /// field (`obj.0`).
David Tolnay461d98e2018-01-07 11:07:19 -0800363 ///
364 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400365 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500366 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500367 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500369 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700370 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500371
David Tolnay05658502018-01-07 09:56:37 -0800372 /// A square bracketed indexing expression: `vector[2]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800373 ///
374 /// *This type is available if Syn is built with the `"derive"` or
375 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700376 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500377 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700378 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500379 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500380 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700381 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500382
David Tolnaya454c8f2018-01-07 01:01:10 -0800383 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800384 ///
385 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400386 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500387 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500390 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700391 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700392
David Tolnaya454c8f2018-01-07 01:01:10 -0800393 /// A path like `std::mem::replace` possibly containing generic
394 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800396 /// A plain identifier like `x` is a path of length 1.
David Tolnay461d98e2018-01-07 11:07:19 -0800397 ///
398 /// *This type is available if Syn is built with the `"derive"` or
399 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700400 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500401 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700402 pub qself: Option<QSelf>,
403 pub path: Path,
404 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700405
David Tolnaya454c8f2018-01-07 01:01:10 -0800406 /// A referencing operation: `&a` or `&mut a`.
David Tolnay461d98e2018-01-07 11:07:19 -0800407 ///
408 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay00674ba2018-03-31 18:14:11 +0200409 pub Reference(ExprReference #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500410 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800411 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500412 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700413 pub expr: Box<Expr>,
414 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500415
David Tolnaya454c8f2018-01-07 01:01:10 -0800416 /// A `break`, with an optional label to break and an optional
417 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800418 ///
419 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400420 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500421 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500422 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700423 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700424 pub expr: Option<Box<Expr>>,
425 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500426
David Tolnaya454c8f2018-01-07 01:01:10 -0800427 /// A `continue`, with an optional label.
David Tolnay461d98e2018-01-07 11:07:19 -0800428 ///
429 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400430 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500431 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800432 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500433 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500435
David Tolnaya454c8f2018-01-07 01:01:10 -0800436 /// A `return`, with an optional value to be returned.
David Tolnay461d98e2018-01-07 11:07:19 -0800437 ///
438 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayc246cd32017-12-28 23:14:32 -0500439 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500440 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800441 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500442 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700443 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700444
David Tolnaya454c8f2018-01-07 01:01:10 -0800445 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800446 ///
447 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500448 pub Macro(ExprMacro #full {
449 pub attrs: Vec<Attribute>,
450 pub mac: Macro,
451 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700452
David Tolnaya454c8f2018-01-07 01:01:10 -0800453 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700454 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800455 /// The `rest` provides the value of the remaining fields as in `S { a:
456 /// 1, b: 1, ..rest }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800457 ///
458 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400459 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500460 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700461 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500462 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500463 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500464 pub dot2_token: Option<Token![..]>,
465 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700466 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700467
David Tolnaya454c8f2018-01-07 01:01:10 -0800468 /// An array literal constructed from one repeated element: `[0u8; N]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800469 ///
470 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400471 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500472 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500473 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700474 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500475 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800476 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700477 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700478
David Tolnaya454c8f2018-01-07 01:01:10 -0800479 /// A parenthesized expression: `(a + b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800480 ///
481 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9374bc02018-01-27 18:49:36 -0800482 pub Paren(ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -0500483 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500484 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500485 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700486 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700487
David Tolnaya454c8f2018-01-07 01:01:10 -0800488 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400489 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800490 /// This variant is important for faithfully representing the precedence
491 /// of expressions and is related to `None`-delimited spans in a
492 /// `TokenStream`.
David Tolnay461d98e2018-01-07 11:07:19 -0800493 ///
494 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye98775f2017-12-28 23:17:00 -0500495 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500496 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500497 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500498 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400499 }),
500
David Tolnaya454c8f2018-01-07 01:01:10 -0800501 /// A try-expression: `expr?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800502 ///
503 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400504 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500505 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800507 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700509
David Tolnaya454c8f2018-01-07 01:01:10 -0800510 /// A catch expression: `do catch { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800511 ///
512 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400513 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500514 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800515 pub do_token: Token![do],
516 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700517 pub block: Block,
518 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700519
David Tolnaya454c8f2018-01-07 01:01:10 -0800520 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800521 ///
522 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700523 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700526 pub expr: Option<Box<Expr>>,
527 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500528
David Tolnaya454c8f2018-01-07 01:01:10 -0800529 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800530 ///
531 /// *This type is available if Syn is built with the `"derive"` or
532 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500533 pub Verbatim(ExprVerbatim #manual_extra_traits {
534 pub tts: TokenStream,
535 }),
536 }
537}
538
539#[cfg(feature = "extra-traits")]
540impl Eq for ExprVerbatim {}
541
542#[cfg(feature = "extra-traits")]
543impl PartialEq for ExprVerbatim {
544 fn eq(&self, other: &Self) -> bool {
545 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
546 }
547}
548
549#[cfg(feature = "extra-traits")]
550impl Hash for ExprVerbatim {
551 fn hash<H>(&self, state: &mut H)
552 where
553 H: Hasher,
554 {
555 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700556 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700557}
558
David Tolnay8c91b882017-12-28 23:04:32 -0500559impl Expr {
560 // Not public API.
561 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500562 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500563 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500564 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800565 Expr::Box(ExprBox { ref mut attrs, .. })
566 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
567 | Expr::Array(ExprArray { ref mut attrs, .. })
568 | Expr::Call(ExprCall { ref mut attrs, .. })
569 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
570 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
571 | Expr::Binary(ExprBinary { ref mut attrs, .. })
572 | Expr::Unary(ExprUnary { ref mut attrs, .. })
573 | Expr::Lit(ExprLit { ref mut attrs, .. })
574 | Expr::Cast(ExprCast { ref mut attrs, .. })
575 | Expr::Type(ExprType { ref mut attrs, .. })
576 | Expr::If(ExprIf { ref mut attrs, .. })
577 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
578 | Expr::While(ExprWhile { ref mut attrs, .. })
579 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
580 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
581 | Expr::Loop(ExprLoop { ref mut attrs, .. })
582 | Expr::Match(ExprMatch { ref mut attrs, .. })
583 | Expr::Closure(ExprClosure { ref mut attrs, .. })
584 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
585 | Expr::Block(ExprBlock { ref mut attrs, .. })
586 | Expr::Assign(ExprAssign { ref mut attrs, .. })
587 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
588 | Expr::Field(ExprField { ref mut attrs, .. })
589 | Expr::Index(ExprIndex { ref mut attrs, .. })
590 | Expr::Range(ExprRange { ref mut attrs, .. })
591 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200592 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800593 | Expr::Break(ExprBreak { ref mut attrs, .. })
594 | Expr::Continue(ExprContinue { ref mut attrs, .. })
595 | Expr::Return(ExprReturn { ref mut attrs, .. })
596 | Expr::Macro(ExprMacro { ref mut attrs, .. })
597 | Expr::Struct(ExprStruct { ref mut attrs, .. })
598 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
599 | Expr::Paren(ExprParen { ref mut attrs, .. })
600 | Expr::Group(ExprGroup { ref mut attrs, .. })
601 | Expr::Try(ExprTry { ref mut attrs, .. })
602 | Expr::Catch(ExprCatch { ref mut attrs, .. })
603 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500604 Expr::Verbatim(_) => {
605 // TODO
606 Vec::new()
607 }
David Tolnay8c91b882017-12-28 23:04:32 -0500608 }
609 }
610}
611
David Tolnay85b69a42017-12-27 20:43:10 -0500612ast_enum! {
613 /// A struct or tuple struct field accessed in a struct literal or field
614 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800615 ///
616 /// *This type is available if Syn is built with the `"derive"` or `"full"`
617 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500618 pub enum Member {
619 /// A named field like `self.x`.
620 Named(Ident),
621 /// An unnamed field like `self.0`.
622 Unnamed(Index),
623 }
624}
625
David Tolnay85b69a42017-12-27 20:43:10 -0500626ast_struct! {
627 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800628 ///
629 /// *This type is available if Syn is built with the `"derive"` or `"full"`
630 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500631 pub struct Index #manual_extra_traits {
632 pub index: u32,
633 pub span: Span,
634 }
635}
636
David Tolnay14982012017-12-29 00:49:51 -0500637impl From<usize> for Index {
638 fn from(index: usize) -> Index {
639 assert!(index < std::u32::MAX as usize);
640 Index {
641 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700642 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500643 }
644 }
645}
646
647#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500648impl Eq for Index {}
649
David Tolnay14982012017-12-29 00:49:51 -0500650#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500651impl PartialEq for Index {
652 fn eq(&self, other: &Self) -> bool {
653 self.index == other.index
654 }
655}
656
David Tolnay14982012017-12-29 00:49:51 -0500657#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500658impl Hash for Index {
659 fn hash<H: Hasher>(&self, state: &mut H) {
660 self.index.hash(state);
661 }
662}
663
664#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700665ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800666 /// The `::<>` explicit type parameters passed to a method call:
667 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800668 ///
669 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500670 pub struct MethodTurbofish {
671 pub colon2_token: Token![::],
672 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500673 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500674 pub gt_token: Token![>],
675 }
676}
677
678#[cfg(feature = "full")]
679ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800680 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800681 ///
682 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500683 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800684 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500685 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800686 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500687 ///
688 /// NOTE: Identity expressions are represented as Type arguments, as
689 /// they are indistinguishable syntactically.
690 Const(Expr),
691 }
692}
693
694#[cfg(feature = "full")]
695ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700696 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800697 ///
698 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700699 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500700 /// Attributes tagged on the field.
701 pub attrs: Vec<Attribute>,
702
703 /// Name or index of the field.
704 pub member: Member,
705
David Tolnay5d7098a2017-12-29 01:35:24 -0500706 /// The colon in `Struct { x: x }`. If written in shorthand like
707 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500708 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500709
Alex Crichton62a0a592017-05-22 13:58:53 -0700710 /// Value of the field.
711 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 }
David Tolnay055a7042016-10-02 19:23:54 -0700713}
714
Michael Layzell734adb42017-06-07 16:58:31 -0400715#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700716ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800717 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800718 ///
719 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500720 pub struct Label {
721 pub name: Lifetime,
722 pub colon_token: Token![:],
723 }
724}
725
726#[cfg(feature = "full")]
727ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800728 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800729 ///
730 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700731 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500732 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700733 /// Statements in a block
734 pub stmts: Vec<Stmt>,
735 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700736}
737
Michael Layzell734adb42017-06-07 16:58:31 -0400738#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700739ast_enum! {
740 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800741 ///
742 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700743 pub enum Stmt {
744 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800745 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700746
Alex Crichton62a0a592017-05-22 13:58:53 -0700747 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800748 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700749
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800751 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700752
David Tolnaya454c8f2018-01-07 01:01:10 -0800753 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800754 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700755 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700756}
757
Michael Layzell734adb42017-06-07 16:58:31 -0400758#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700759ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800760 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800761 ///
762 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500764 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800765 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700766 pub pat: Box<Pat>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500767 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500768 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500769 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700770 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700771}
772
Michael Layzell734adb42017-06-07 16:58:31 -0400773#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700774ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800775 /// A pattern in a local binding, function signature, match expression, or
776 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800777 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800778 /// *This type is available if Syn is built with the `"full"` feature.*
779 ///
David Tolnay614a0142018-01-07 10:25:43 -0800780 /// # Syntax tree enum
781 ///
782 /// This type is a [syntax tree enum].
783 ///
784 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700785 // Clippy false positive
786 // https://github.com/Manishearth/rust-clippy/issues/1241
787 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
788 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800789 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800790 ///
791 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700792 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800793 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700794 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700795
David Tolnaya454c8f2018-01-07 01:01:10 -0800796 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800797 ///
798 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700799 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500800 pub by_ref: Option<Token![ref]>,
801 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700802 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500803 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700804 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700805
David Tolnaya454c8f2018-01-07 01:01:10 -0800806 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800807 ///
808 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700809 pub Struct(PatStruct {
810 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500811 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500812 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800813 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700814 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700815
David Tolnaya454c8f2018-01-07 01:01:10 -0800816 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800817 ///
818 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700819 pub TupleStruct(PatTupleStruct {
820 pub path: Path,
821 pub pat: PatTuple,
822 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700823
David Tolnaya454c8f2018-01-07 01:01:10 -0800824 /// A path pattern like `Color::Red`, optionally qualified with a
825 /// self-type.
826 ///
827 /// Unquailfied path patterns can legally refer to variants, structs,
828 /// constants or associated constants. Quailfied path patterns like
829 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
830 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800831 ///
832 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700833 pub Path(PatPath {
834 pub qself: Option<QSelf>,
835 pub path: Path,
836 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700837
David Tolnaya454c8f2018-01-07 01:01:10 -0800838 /// A tuple pattern: `(a, b)`.
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 Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500842 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500843 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500844 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500845 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500846 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700847 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800848
849 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800850 ///
851 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700852 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800853 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500854 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700855 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800856
857 /// A reference pattern: `&mut (first, second)`.
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 Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800861 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500862 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500863 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700864 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800865
866 /// A literal pattern: `0`.
867 ///
868 /// This holds an `Expr` rather than a `Lit` because negative numbers
869 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800870 ///
871 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700872 pub Lit(PatLit {
873 pub expr: Box<Expr>,
874 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800875
876 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800877 ///
878 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700879 pub Range(PatRange {
880 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700881 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500882 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700883 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800884
885 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800886 ///
887 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700888 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500889 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500890 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500892 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500893 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500894 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700895 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800896
897 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800898 ///
899 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500900 pub Macro(PatMacro {
901 pub mac: Macro,
902 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800903
904 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800905 ///
906 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500907 pub Verbatim(PatVerbatim #manual_extra_traits {
908 pub tts: TokenStream,
909 }),
910 }
911}
912
David Tolnayc43b44e2017-12-30 23:55:54 -0500913#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500914impl Eq for PatVerbatim {}
915
David Tolnayc43b44e2017-12-30 23:55:54 -0500916#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500917impl PartialEq for PatVerbatim {
918 fn eq(&self, other: &Self) -> bool {
919 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
920 }
921}
922
David Tolnayc43b44e2017-12-30 23:55:54 -0500923#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500924impl Hash for PatVerbatim {
925 fn hash<H>(&self, state: &mut H)
926 where
927 H: Hasher,
928 {
929 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700930 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700931}
932
Michael Layzell734adb42017-06-07 16:58:31 -0400933#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700934ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800935 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700936 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800937 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700938 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500939 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800940 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500941 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700942 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800943 /// 0...10 => {
944 /// return true;
945 /// }
946 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500947 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700948 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800949 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500950 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800952 ///
953 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 pub struct Arm {
955 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200956 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500957 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500958 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800959 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700960 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800961 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700963}
964
Michael Layzell734adb42017-06-07 16:58:31 -0400965#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700966ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800967 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800968 ///
969 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700970 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700971 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800972 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800973 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800974 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800975 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700976 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700977}
978
Michael Layzell734adb42017-06-07 16:58:31 -0400979#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700980ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800981 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700982 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800983 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
984 /// 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 -0800985 ///
986 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700987 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500988 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500989 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500990 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700991 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700992 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700993}
994
Michael Layzell3936ceb2017-07-08 00:28:36 -0400995#[cfg(any(feature = "parsing", feature = "printing"))]
996#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700997fn arm_expr_requires_comma(expr: &Expr) -> bool {
998 // see https://github.com/rust-lang/rust/blob/eb8f2586e
999 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001000 match *expr {
1001 Expr::Unsafe(..)
1002 | Expr::Block(..)
1003 | Expr::If(..)
1004 | Expr::IfLet(..)
1005 | Expr::Match(..)
1006 | Expr::While(..)
1007 | Expr::WhileLet(..)
1008 | Expr::Loop(..)
1009 | Expr::ForLoop(..)
1010 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001011 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001012 }
1013}
1014
David Tolnayb9c8e322016-09-23 20:48:37 -07001015#[cfg(feature = "parsing")]
1016pub mod parsing {
1017 use super::*;
David Tolnay056de302018-01-05 14:29:05 -08001018 use path::parsing::qpath;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001019 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001020 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001021
Michael Layzell734adb42017-06-07 16:58:31 -04001022 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -08001023 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -05001024 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -08001025 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001026 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001027 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -05001028 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001029
David Tolnaybcf26022017-12-25 22:10:52 -05001030 // When we're parsing expressions which occur before blocks, like in an if
1031 // statement's condition, we cannot parse a struct literal.
1032 //
1033 // Struct literals are ambiguous in certain positions
1034 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001035 macro_rules! ambiguous_expr {
1036 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001037 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001038 };
1039 }
1040
David Tolnaybcf26022017-12-25 22:10:52 -05001041 // When we are parsing an optional suffix expression, we cannot allow blocks
1042 // if structs are not allowed.
1043 //
1044 // Example:
1045 //
1046 // if break {} {}
1047 //
1048 // is ambiguous between:
1049 //
1050 // if (break {}) {}
1051 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001052 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001053 macro_rules! opt_ambiguous_expr {
1054 ($i:expr, $allow_struct:ident) => {
1055 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1056 };
1057 }
1058
Alex Crichton954046c2017-05-30 21:49:42 -07001059 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001060 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001061
1062 fn description() -> Option<&'static str> {
1063 Some("expression")
1064 }
1065 }
1066
Michael Layzell734adb42017-06-07 16:58:31 -04001067 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001068 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1069
David Tolnaybcf26022017-12-25 22:10:52 -05001070 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001071 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001072 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001073 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001074 }
1075
Michael Layzell734adb42017-06-07 16:58:31 -04001076 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001077 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001078 // NOTE: We intentionally skip assign_expr, placement_expr, and
1079 // range_expr, as they are not parsed in non-full mode.
1080 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001081 }
1082
David Tolnaybcf26022017-12-25 22:10:52 -05001083 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001084 macro_rules! binop {
1085 (
1086 $name: ident,
1087 $next: ident,
1088 $submac: ident!( $($args:tt)* )
1089 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001090 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001091 mut e: call!($next, allow_struct, allow_block) >>
1092 many0!(do_parse!(
1093 op: $submac!($($args)*) >>
1094 rhs: call!($next, allow_struct, true) >>
1095 ({
1096 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001097 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001098 left: Box::new(e.into()),
1099 op: op,
1100 right: Box::new(rhs.into()),
1101 }.into();
1102 })
1103 )) >>
1104 (e)
1105 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001106 }
David Tolnay54e854d2016-10-24 12:03:30 -07001107 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001108
David Tolnaybcf26022017-12-25 22:10:52 -05001109 // <placement> = <placement> ..
1110 // <placement> += <placement> ..
1111 // <placement> -= <placement> ..
1112 // <placement> *= <placement> ..
1113 // <placement> /= <placement> ..
1114 // <placement> %= <placement> ..
1115 // <placement> ^= <placement> ..
1116 // <placement> &= <placement> ..
1117 // <placement> |= <placement> ..
1118 // <placement> <<= <placement> ..
1119 // <placement> >>= <placement> ..
1120 //
1121 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001122 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001123 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001124 mut e: call!(placement_expr, allow_struct, allow_block) >>
1125 alt!(
1126 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001127 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001128 // Recurse into self to parse right-associative operator.
1129 rhs: call!(assign_expr, allow_struct, true) >>
1130 ({
1131 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001132 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001133 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001134 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001135 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001136 }.into();
1137 })
1138 )
1139 |
1140 do_parse!(
1141 op: call!(BinOp::parse_assign_op) >>
1142 // Recurse into self to parse right-associative operator.
1143 rhs: call!(assign_expr, allow_struct, true) >>
1144 ({
1145 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001146 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001147 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001148 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001149 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001150 }.into();
1151 })
1152 )
1153 |
1154 epsilon!()
1155 ) >>
1156 (e)
1157 ));
1158
David Tolnaybcf26022017-12-25 22:10:52 -05001159 // <range> <- <range> ..
1160 //
1161 // NOTE: The `in place { expr }` version of this syntax is parsed in
1162 // `atom_expr`, not here.
1163 //
1164 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001165 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001166 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001167 mut e: call!(range_expr, allow_struct, allow_block) >>
1168 alt!(
1169 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001170 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001171 // Recurse into self to parse right-associative operator.
1172 rhs: call!(placement_expr, allow_struct, true) >>
1173 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001174 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001175 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001176 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001177 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001178 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001179 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001180 }.into();
1181 })
1182 )
1183 |
1184 epsilon!()
1185 ) >>
1186 (e)
1187 ));
1188
David Tolnaybcf26022017-12-25 22:10:52 -05001189 // <or> ... <or> ..
1190 // <or> .. <or> ..
1191 // <or> ..
1192 //
1193 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1194 // rules are for parsing these expressions are, but this is not correct.
1195 // For example, `a .. b .. c` is not a legal expression. It should not
1196 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1197 //
1198 // NOTE: The form of ranges which don't include a preceding expression are
1199 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001200 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001201 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001202 mut e: call!(or_expr, allow_struct, allow_block) >>
1203 many0!(do_parse!(
1204 limits: syn!(RangeLimits) >>
1205 // We don't want to allow blocks here if we don't allow structs. See
1206 // the reasoning for `opt_ambiguous_expr!` above.
1207 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1208 ({
1209 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001210 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001211 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001212 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001213 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001214 }.into();
1215 })
1216 )) >>
1217 (e)
1218 ));
1219
David Tolnaybcf26022017-12-25 22:10:52 -05001220 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001221 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001222
David Tolnaybcf26022017-12-25 22:10:52 -05001223 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001224 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001225
David Tolnaybcf26022017-12-25 22:10:52 -05001226 // <bitor> == <bitor> ...
1227 // <bitor> != <bitor> ...
1228 // <bitor> >= <bitor> ...
1229 // <bitor> <= <bitor> ...
1230 // <bitor> > <bitor> ...
1231 // <bitor> < <bitor> ...
1232 //
1233 // NOTE: This operator appears to be parsed as left-associative, but errors
1234 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001235 binop!(
1236 compare_expr,
1237 bitor_expr,
1238 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001239 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001240 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001241 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 |
1243 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001244 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001245 |
1246 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001247 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001249 do_parse!(
1250 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001251 not!(punct!(<-)) >>
1252 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001253 (BinOp::Lt(t))
1254 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001255 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001256 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001257 )
1258 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259
David Tolnaybcf26022017-12-25 22:10:52 -05001260 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001261 binop!(
1262 bitor_expr,
1263 bitxor_expr,
1264 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1265 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001266
David Tolnaybcf26022017-12-25 22:10:52 -05001267 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001268 binop!(
1269 bitxor_expr,
1270 bitand_expr,
1271 do_parse!(
1272 // NOTE: Make sure we aren't looking at ^=.
1273 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1274 )
1275 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001276
David Tolnaybcf26022017-12-25 22:10:52 -05001277 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001278 binop!(
1279 bitand_expr,
1280 shift_expr,
1281 do_parse!(
1282 // NOTE: Make sure we aren't looking at && or &=.
1283 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1284 )
1285 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001286
David Tolnaybcf26022017-12-25 22:10:52 -05001287 // <arith> << <arith> ...
1288 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001289 binop!(
1290 shift_expr,
1291 arith_expr,
1292 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001293 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001294 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001295 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001296 )
1297 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001298
David Tolnaybcf26022017-12-25 22:10:52 -05001299 // <term> + <term> ...
1300 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001301 binop!(
1302 arith_expr,
1303 term_expr,
1304 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001305 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001306 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001307 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001308 )
1309 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001310
David Tolnaybcf26022017-12-25 22:10:52 -05001311 // <cast> * <cast> ...
1312 // <cast> / <cast> ...
1313 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001314 binop!(
1315 term_expr,
1316 cast_expr,
1317 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001318 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001319 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001320 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001321 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001322 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001323 )
1324 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001325
David Tolnaybcf26022017-12-25 22:10:52 -05001326 // <unary> as <ty>
1327 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001328 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001329 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 mut e: call!(unary_expr, allow_struct, allow_block) >>
1331 many0!(alt!(
1332 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001333 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 // We can't accept `A + B` in cast expressions, as it's
1335 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001336 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001337 ({
1338 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001339 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001340 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001341 as_token: as_,
1342 ty: Box::new(ty),
1343 }.into();
1344 })
1345 )
1346 |
1347 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001348 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001349 // We can't accept `A + B` in cast expressions, as it's
1350 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001351 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001352 ({
1353 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001354 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001355 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001356 colon_token: colon,
1357 ty: Box::new(ty),
1358 }.into();
1359 })
1360 )
1361 )) >>
1362 (e)
1363 ));
1364
David Tolnay0cf94f22017-12-28 23:46:26 -05001365 // <unary> as <ty>
1366 #[cfg(not(feature = "full"))]
1367 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1368 mut e: call!(unary_expr, allow_struct, allow_block) >>
1369 many0!(do_parse!(
1370 as_: keyword!(as) >>
1371 // We can't accept `A + B` in cast expressions, as it's
1372 // ambiguous with the + expression.
1373 ty: call!(Type::without_plus) >>
1374 ({
1375 e = ExprCast {
1376 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001377 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001378 as_token: as_,
1379 ty: Box::new(ty),
1380 }.into();
1381 })
1382 )) >>
1383 (e)
1384 ));
1385
David Tolnaybcf26022017-12-25 22:10:52 -05001386 // <UnOp> <trailer>
1387 // & <trailer>
1388 // &mut <trailer>
1389 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001390 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001391 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001392 do_parse!(
1393 op: syn!(UnOp) >>
1394 expr: call!(unary_expr, allow_struct, true) >>
1395 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001396 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001397 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001398 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001399 }.into())
1400 )
1401 |
1402 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001403 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001404 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001405 expr: call!(unary_expr, allow_struct, true) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001406 (ExprReference {
David Tolnay8c91b882017-12-28 23:04:32 -05001407 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001408 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001409 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001410 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001411 }.into())
1412 )
1413 |
1414 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001415 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001416 expr: call!(unary_expr, allow_struct, true) >>
1417 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001418 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001419 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001420 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001421 }.into())
1422 )
1423 |
1424 call!(trailer_expr, allow_struct, allow_block)
1425 ));
1426
Michael Layzell734adb42017-06-07 16:58:31 -04001427 // XXX: This duplication is ugly
1428 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001429 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001430 do_parse!(
1431 op: syn!(UnOp) >>
1432 expr: call!(unary_expr, allow_struct, true) >>
1433 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001434 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001435 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001436 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001437 }.into())
1438 )
1439 |
1440 call!(trailer_expr, allow_struct, allow_block)
1441 ));
1442
David Tolnaybcf26022017-12-25 22:10:52 -05001443 // <atom> (..<args>) ...
1444 // <atom> . <ident> (..<args>) ...
1445 // <atom> . <ident> ...
1446 // <atom> . <lit> ...
1447 // <atom> [ <expr> ] ...
1448 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001449 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001450 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001451 mut e: call!(atom_expr, allow_struct, allow_block) >>
1452 many0!(alt!(
1453 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001454 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001455 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001456 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001457 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001458 args: args,
1459 paren_token: paren,
1460 }.into();
1461 })
1462 |
1463 tap!(more: and_method_call => {
1464 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001465 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001466 e = call.into();
1467 })
1468 |
1469 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001470 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001471 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001472 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001473 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001474 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001475 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001476 }.into();
1477 })
1478 |
1479 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001480 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001481 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001482 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001483 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001484 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001485 index: Box::new(i),
1486 }.into();
1487 })
1488 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001489 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001490 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001491 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001492 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001493 question_token: question,
1494 }.into();
1495 })
1496 )) >>
1497 (e)
1498 ));
1499
Michael Layzell734adb42017-06-07 16:58:31 -04001500 // XXX: Duplication == ugly
1501 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001502 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001503 mut e: call!(atom_expr, allow_struct, allow_block) >>
1504 many0!(alt!(
1505 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001506 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001507 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001508 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001509 paren_token: args.0,
1510 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001511 }.into();
1512 })
1513 |
1514 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001515 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001516 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001517 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001518 bracket_token: i.0,
1519 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001520 }.into();
1521 })
1522 )) >>
1523 (e)
1524 ));
1525
David Tolnaya454c8f2018-01-07 01:01:10 -08001526 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001527 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001528 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001529 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1530 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001531 |
David Tolnay8c91b882017-12-28 23:04:32 -05001532 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001533 |
1534 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001535 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001536 |
David Tolnay8c91b882017-12-28 23:04:32 -05001537 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001538 |
David Tolnay8c91b882017-12-28 23:04:32 -05001539 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001540 |
1541 call!(expr_break, allow_struct) // must be before expr_path
1542 |
David Tolnay8c91b882017-12-28 23:04:32 -05001543 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001544 |
1545 call!(expr_ret, allow_struct) // must be before expr_path
1546 |
David Tolnay8c91b882017-12-28 23:04:32 -05001547 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001548 |
David Tolnay8c91b882017-12-28 23:04:32 -05001549 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001550 |
David Tolnay8c91b882017-12-28 23:04:32 -05001551 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001552 |
David Tolnay8c91b882017-12-28 23:04:32 -05001553 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001554 |
David Tolnay8c91b882017-12-28 23:04:32 -05001555 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001556 |
David Tolnay8c91b882017-12-28 23:04:32 -05001557 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001558 |
David Tolnay8c91b882017-12-28 23:04:32 -05001559 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001560 |
David Tolnay8c91b882017-12-28 23:04:32 -05001561 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001562 |
David Tolnay8c91b882017-12-28 23:04:32 -05001563 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001564 |
David Tolnay8c91b882017-12-28 23:04:32 -05001565 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001566 |
David Tolnay8c91b882017-12-28 23:04:32 -05001567 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001568 |
David Tolnay8c91b882017-12-28 23:04:32 -05001569 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001570 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001571 call!(expr_closure, allow_struct)
1572 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001573 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001574 |
1575 // NOTE: This is the prefix-form of range
1576 call!(expr_range, allow_struct)
1577 |
David Tolnay8c91b882017-12-28 23:04:32 -05001578 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001579 |
David Tolnay8c91b882017-12-28 23:04:32 -05001580 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001581 ));
1582
Michael Layzell734adb42017-06-07 16:58:31 -04001583 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001584 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001585 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001586 |
David Tolnay9374bc02018-01-27 18:49:36 -08001587 syn!(ExprParen) => { Expr::Paren }
1588 |
David Tolnay8c91b882017-12-28 23:04:32 -05001589 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001590 ));
1591
Michael Layzell734adb42017-06-07 16:58:31 -04001592 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001593 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001594 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001595 |
David Tolnay8c91b882017-12-28 23:04:32 -05001596 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001597 |
David Tolnay8c91b882017-12-28 23:04:32 -05001598 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001599 |
David Tolnay8c91b882017-12-28 23:04:32 -05001600 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001601 |
David Tolnay8c91b882017-12-28 23:04:32 -05001602 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001603 |
David Tolnay8c91b882017-12-28 23:04:32 -05001604 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001605 |
David Tolnay8c91b882017-12-28 23:04:32 -05001606 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001607 |
David Tolnay8c91b882017-12-28 23:04:32 -05001608 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001609 |
David Tolnay8c91b882017-12-28 23:04:32 -05001610 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001611 |
David Tolnay8c91b882017-12-28 23:04:32 -05001612 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001613 |
David Tolnay8c91b882017-12-28 23:04:32 -05001614 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001615 ), Expr::from));
1616
David Tolnay8c91b882017-12-28 23:04:32 -05001617 impl Synom for ExprLit {
1618 named!(parse -> Self, do_parse!(
1619 lit: syn!(Lit) >>
1620 (ExprLit {
1621 attrs: Vec::new(),
1622 lit: lit,
1623 })
1624 ));
David Tolnay79777332018-01-07 10:04:42 -08001625
1626 fn description() -> Option<&'static str> {
1627 Some("literal")
1628 }
David Tolnay8c91b882017-12-28 23:04:32 -05001629 }
1630
1631 #[cfg(feature = "full")]
1632 impl Synom for ExprMacro {
1633 named!(parse -> Self, do_parse!(
1634 mac: syn!(Macro) >>
1635 (ExprMacro {
1636 attrs: Vec::new(),
1637 mac: mac,
1638 })
1639 ));
David Tolnay79777332018-01-07 10:04:42 -08001640
1641 fn description() -> Option<&'static str> {
1642 Some("macro invocation expression")
1643 }
David Tolnay8c91b882017-12-28 23:04:32 -05001644 }
1645
David Tolnaye98775f2017-12-28 23:17:00 -05001646 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001647 impl Synom for ExprGroup {
1648 named!(parse -> Self, do_parse!(
1649 e: grouped!(syn!(Expr)) >>
1650 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001651 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001652 expr: Box::new(e.1),
1653 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001654 })
Michael Layzell93c36282017-06-04 20:43:14 -04001655 ));
David Tolnay79777332018-01-07 10:04:42 -08001656
1657 fn description() -> Option<&'static str> {
1658 Some("expression surrounded by invisible delimiters")
1659 }
Michael Layzell93c36282017-06-04 20:43:14 -04001660 }
1661
Alex Crichton954046c2017-05-30 21:49:42 -07001662 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001663 named!(parse -> Self, do_parse!(
1664 e: parens!(syn!(Expr)) >>
1665 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001666 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001667 paren_token: e.0,
1668 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001669 })
Michael Layzell92639a52017-06-01 00:07:44 -04001670 ));
David Tolnay79777332018-01-07 10:04:42 -08001671
1672 fn description() -> Option<&'static str> {
1673 Some("parenthesized expression")
1674 }
Alex Crichton954046c2017-05-30 21:49:42 -07001675 }
David Tolnay89e05672016-10-02 14:39:42 -07001676
Michael Layzell734adb42017-06-07 16:58:31 -04001677 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001678 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001679 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001680 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001681 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001682 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001683 bracket_token: elems.0,
1684 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001685 })
1686 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001687
1688 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001689 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001690 }
Alex Crichton954046c2017-05-30 21:49:42 -07001691 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001692
David Tolnayf2cfd722017-12-31 18:02:51 -05001693 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1694 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001695
Michael Layzell734adb42017-06-07 16:58:31 -04001696 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001697 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001698 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001699 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001700 turbofish: option!(tuple!(
1701 punct!(::),
1702 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001703 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001704 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001705 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001706 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001707 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001708 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001709 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001710 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001711 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1712 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001713 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001714
Alex Crichton954046c2017-05-30 21:49:42 -07001715 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001716 turbofish: turbofish.map(|fish| MethodTurbofish {
1717 colon2_token: fish.0,
1718 lt_token: fish.1,
1719 args: fish.2,
1720 gt_token: fish.3,
1721 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001722 args: args.1,
1723 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001724 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001725 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001726 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001727 ));
1728
Michael Layzell734adb42017-06-07 16:58:31 -04001729 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001730 impl Synom for GenericMethodArgument {
1731 // TODO parse const generics as well
1732 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001733
1734 fn description() -> Option<&'static str> {
1735 Some("generic method argument")
1736 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001737 }
1738
1739 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001740 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001741 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001742 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001743 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001744 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001745 elems: elems.1,
1746 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001747 })
1748 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001749
1750 fn description() -> Option<&'static str> {
1751 Some("tuple")
1752 }
Alex Crichton954046c2017-05-30 21:49:42 -07001753 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001754
Michael Layzell734adb42017-06-07 16:58:31 -04001755 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001756 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001757 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001758 if_: keyword!(if) >>
1759 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001760 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001761 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001762 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001763 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001764 else_block: option!(else_block) >>
1765 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001766 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001767 pat: Box::new(pat),
1768 let_token: let_,
1769 eq_token: eq,
1770 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001771 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001772 brace_token: then_block.0,
1773 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001774 },
1775 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001776 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001777 })
1778 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001779
1780 fn description() -> Option<&'static str> {
1781 Some("`if let` expression")
1782 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001783 }
1784
Michael Layzell734adb42017-06-07 16:58:31 -04001785 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001786 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001787 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001788 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001789 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001790 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001791 else_block: option!(else_block) >>
1792 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001793 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001794 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001795 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001796 brace_token: then_block.0,
1797 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001798 },
1799 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001800 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001801 })
1802 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001803
1804 fn description() -> Option<&'static str> {
1805 Some("`if` expression")
1806 }
Alex Crichton954046c2017-05-30 21:49:42 -07001807 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001808
Michael Layzell734adb42017-06-07 16:58:31 -04001809 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001810 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001811 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001812 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001813 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001814 |
David Tolnay8c91b882017-12-28 23:04:32 -05001815 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001816 |
1817 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001818 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001819 (Expr::Block(ExprBlock {
1820 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001821 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001822 brace_token: else_block.0,
1823 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001824 },
1825 }))
David Tolnay939766a2016-09-23 23:48:12 -07001826 )
Alex Crichton954046c2017-05-30 21:49:42 -07001827 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001828 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001829 ));
1830
Michael Layzell734adb42017-06-07 16:58:31 -04001831 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001832 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001833 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001834 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001835 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001836 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001837 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001838 expr: expr_no_struct >>
1839 loop_block: syn!(Block) >>
1840 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001841 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001842 for_token: for_,
1843 in_token: in_,
1844 pat: Box::new(pat),
1845 expr: Box::new(expr),
1846 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001847 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001848 })
1849 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001850
1851 fn description() -> Option<&'static str> {
1852 Some("`for` loop")
1853 }
Alex Crichton954046c2017-05-30 21:49:42 -07001854 }
Gregory Katze5f35682016-09-27 14:20:55 -04001855
Michael Layzell734adb42017-06-07 16:58:31 -04001856 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001857 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001858 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001859 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001860 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001861 loop_block: syn!(Block) >>
1862 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001863 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001864 loop_token: loop_,
1865 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001866 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001867 })
1868 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001869
1870 fn description() -> Option<&'static str> {
1871 Some("`loop`")
1872 }
Alex Crichton954046c2017-05-30 21:49:42 -07001873 }
1874
Michael Layzell734adb42017-06-07 16:58:31 -04001875 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001876 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001877 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001878 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001879 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001880 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001881 (ExprMatch {
1882 attrs: Vec::new(),
1883 expr: Box::new(obj),
1884 match_token: match_,
1885 brace_token: res.0,
1886 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001887 })
1888 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001889
1890 fn description() -> Option<&'static str> {
1891 Some("`match` expression")
1892 }
Alex Crichton954046c2017-05-30 21:49:42 -07001893 }
David Tolnay1978c672016-10-27 22:05:52 -07001894
Michael Layzell734adb42017-06-07 16:58:31 -04001895 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001896 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001897 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001898 do_: keyword!(do) >>
1899 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001900 catch_block: syn!(Block) >>
1901 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001902 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001903 block: catch_block,
1904 do_token: do_,
1905 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001906 })
Michael Layzell92639a52017-06-01 00:07:44 -04001907 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001908
1909 fn description() -> Option<&'static str> {
1910 Some("`catch` expression")
1911 }
Alex Crichton954046c2017-05-30 21:49:42 -07001912 }
Arnavion02ef13f2017-04-25 00:54:31 -07001913
Michael Layzell734adb42017-06-07 16:58:31 -04001914 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001915 impl Synom for ExprYield {
1916 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001917 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001918 expr: option!(syn!(Expr)) >>
1919 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001920 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001921 yield_token: yield_,
1922 expr: expr.map(Box::new),
1923 })
1924 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001925
1926 fn description() -> Option<&'static str> {
1927 Some("`yield` expression")
1928 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001929 }
1930
1931 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001932 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001933 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001934 attrs: many0!(Attribute::parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02001935 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001936 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001937 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1938 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001939 body: do_parse!(
1940 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001941 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1942 true => alt!(
1943 input_end!() => { |_| None }
1944 |
1945 punct!(,) => { Some }
1946 )
Alex Crichton03b30272017-08-28 09:35:24 -07001947 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001948 false => option!(punct!(,))
1949 ) >>
1950 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001951 ) >>
1952 (Arm {
1953 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001954 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02001955 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04001956 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001957 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001958 body: Box::new(body.0),
1959 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001960 })
1961 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001962
1963 fn description() -> Option<&'static str> {
1964 Some("`match` arm")
1965 }
Alex Crichton954046c2017-05-30 21:49:42 -07001966 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001967
Michael Layzell734adb42017-06-07 16:58:31 -04001968 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001969 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001970 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001971 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001972 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001973 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001974 ret_and_body: alt!(
1975 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001976 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001977 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001978 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001979 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001980 Expr::Block(ExprBlock {
1981 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001982 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001983 }))
David Tolnay89e05672016-10-02 14:39:42 -07001984 )
1985 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001986 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001987 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001988 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001989 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001990 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001991 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001992 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001993 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001994 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001995 body: Box::new(ret_and_body.1),
1996 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001997 ));
1998
Michael Layzell734adb42017-06-07 16:58:31 -04001999 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002000 named!(fn_arg -> FnArg, do_parse!(
2001 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002002 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002003 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002004 if let Some((colon, ty)) = ty {
2005 FnArg::Captured(ArgCaptured {
2006 pat: pat,
2007 colon_token: colon,
2008 ty: ty,
2009 })
2010 } else {
2011 FnArg::Inferred(pat)
2012 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002013 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002014 ));
2015
Michael Layzell734adb42017-06-07 16:58:31 -04002016 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002017 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002018 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05002019 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002020 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002021 cond: expr_no_struct >>
2022 while_block: syn!(Block) >>
2023 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05002024 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002025 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002026 cond: Box::new(cond),
2027 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05002028 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002029 })
2030 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002031
2032 fn description() -> Option<&'static str> {
2033 Some("`while` expression")
2034 }
Alex Crichton954046c2017-05-30 21:49:42 -07002035 }
2036
Michael Layzell734adb42017-06-07 16:58:31 -04002037 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002038 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002039 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05002040 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002041 while_: keyword!(while) >>
2042 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002043 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002044 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002045 value: expr_no_struct >>
2046 while_block: syn!(Block) >>
2047 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05002048 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002049 eq_token: eq,
2050 let_token: let_,
2051 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002052 pat: Box::new(pat),
2053 expr: Box::new(value),
2054 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05002055 label: label,
2056 })
2057 ));
David Tolnay79777332018-01-07 10:04:42 -08002058
2059 fn description() -> Option<&'static str> {
2060 Some("`while let` expression")
2061 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002062 }
2063
2064 #[cfg(feature = "full")]
2065 impl Synom for Label {
2066 named!(parse -> Self, do_parse!(
2067 name: syn!(Lifetime) >>
2068 colon: punct!(:) >>
2069 (Label {
2070 name: name,
2071 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002072 })
2073 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002074
2075 fn description() -> Option<&'static str> {
2076 Some("`while let` expression")
2077 }
Alex Crichton954046c2017-05-30 21:49:42 -07002078 }
2079
Michael Layzell734adb42017-06-07 16:58:31 -04002080 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002081 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002082 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002083 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002084 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002085 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05002086 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002087 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002088 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002089 })
2090 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002091
2092 fn description() -> Option<&'static str> {
2093 Some("`continue`")
2094 }
Alex Crichton954046c2017-05-30 21:49:42 -07002095 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002096
Michael Layzell734adb42017-06-07 16:58:31 -04002097 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002098 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002099 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002100 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002101 // We can't allow blocks after a `break` expression when we wouldn't
2102 // allow structs, as this expression is ambiguous.
2103 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002104 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05002105 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05002106 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002107 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002108 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002109 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002110 ));
2111
Michael Layzell734adb42017-06-07 16:58:31 -04002112 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002113 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002114 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002115 // NOTE: return is greedy and eats blocks after it even when in a
2116 // position where structs are not allowed, such as in if statement
2117 // conditions. For example:
2118 //
David Tolnaybcf26022017-12-25 22:10:52 -05002119 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002120 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002121 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05002122 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002123 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002124 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002125 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002126 ));
2127
Michael Layzell734adb42017-06-07 16:58:31 -04002128 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002129 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002130 named!(parse -> Self, do_parse!(
2131 path: syn!(Path) >>
2132 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002133 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002134 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2135 dots: punct!(..) >>
2136 base: syn!(Expr) >>
2137 (dots, base)
2138 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002139 (fields, base)
2140 )) >>
2141 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002142 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002143 let (dots, rest) = match base.and_then(|b| b) {
2144 Some((dots, base)) => (Some(dots), Some(base)),
2145 None => (None, None),
2146 };
2147 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05002148 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002149 brace_token: brace,
2150 path: path,
2151 fields: fields,
2152 dot2_token: dots,
2153 rest: rest.map(Box::new),
2154 }
2155 })
2156 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002157
2158 fn description() -> Option<&'static str> {
2159 Some("struct literal expression")
2160 }
Alex Crichton954046c2017-05-30 21:49:42 -07002161 }
2162
Michael Layzell734adb42017-06-07 16:58:31 -04002163 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002164 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002165 named!(parse -> Self, do_parse!(
2166 attrs: many0!(Attribute::parse_outer) >>
2167 field_value: alt!(
2168 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2169 |
2170 map!(syn!(Ident), |name| (
2171 Member::Named(name),
2172 None,
2173 Expr::Path(ExprPath {
2174 attrs: Vec::new(),
2175 qself: None,
2176 path: name.into(),
2177 }),
2178 ))
2179 ) >>
2180 (FieldValue {
2181 attrs: attrs,
2182 member: field_value.0,
2183 colon_token: field_value.1,
2184 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002185 })
2186 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002187
2188 fn description() -> Option<&'static str> {
2189 Some("field-value pair: `field: value`")
2190 }
Alex Crichton954046c2017-05-30 21:49:42 -07002191 }
David Tolnay055a7042016-10-02 19:23:54 -07002192
Michael Layzell734adb42017-06-07 16:58:31 -04002193 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002194 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002195 named!(parse -> Self, do_parse!(
2196 data: brackets!(do_parse!(
2197 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002198 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002199 times: syn!(Expr) >>
2200 (value, semi, times)
2201 )) >>
2202 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05002203 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05002204 expr: Box::new((data.1).0),
David Tolnay84d80442018-01-07 01:03:20 -08002205 len: Box::new((data.1).2),
David Tolnay8875fca2017-12-31 13:52:37 -05002206 bracket_token: data.0,
2207 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002208 })
2209 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002210
2211 fn description() -> Option<&'static str> {
2212 Some("repeated array literal: `[val; N]`")
2213 }
Alex Crichton954046c2017-05-30 21:49:42 -07002214 }
David Tolnay055a7042016-10-02 19:23:54 -07002215
Michael Layzell734adb42017-06-07 16:58:31 -04002216 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002217 impl Synom for ExprUnsafe {
2218 named!(parse -> Self, do_parse!(
2219 unsafe_: keyword!(unsafe) >>
2220 b: syn!(Block) >>
2221 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002222 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002223 unsafe_token: unsafe_,
2224 block: b,
2225 })
2226 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002227
2228 fn description() -> Option<&'static str> {
2229 Some("unsafe block: `unsafe { .. }`")
2230 }
Nika Layzell640832a2017-12-04 13:37:09 -05002231 }
2232
2233 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002234 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002235 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04002236 b: syn!(Block) >>
2237 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002238 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002239 block: b,
2240 })
2241 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002242
2243 fn description() -> Option<&'static str> {
2244 Some("block: `{ .. }`")
2245 }
Alex Crichton954046c2017-05-30 21:49:42 -07002246 }
David Tolnay89e05672016-10-02 14:39:42 -07002247
Michael Layzell734adb42017-06-07 16:58:31 -04002248 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002249 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002250 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002251 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002252 (ExprRange {
2253 attrs: Vec::new(),
2254 from: None,
2255 to: hi.map(Box::new),
2256 limits: limits,
2257 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002258 ));
2259
Michael Layzell734adb42017-06-07 16:58:31 -04002260 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002261 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002262 named!(parse -> Self, alt!(
2263 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002264 punct!(..=) => { RangeLimits::Closed }
2265 |
2266 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002267 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002268 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002269 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002270 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002271
2272 fn description() -> Option<&'static str> {
2273 Some("range limit: `..`, `...` or `..=`")
2274 }
Alex Crichton954046c2017-05-30 21:49:42 -07002275 }
David Tolnay438c9052016-10-07 23:24:48 -07002276
Alex Crichton954046c2017-05-30 21:49:42 -07002277 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002278 named!(parse -> Self, do_parse!(
2279 pair: qpath >>
2280 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002281 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002282 qself: pair.0,
2283 path: pair.1,
2284 })
2285 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002286
2287 fn description() -> Option<&'static str> {
2288 Some("path: `a::b::c`")
2289 }
Alex Crichton954046c2017-05-30 21:49:42 -07002290 }
David Tolnay42602292016-10-01 22:25:45 -07002291
Michael Layzell734adb42017-06-07 16:58:31 -04002292 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002293 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002294
David Tolnay8875fca2017-12-31 13:52:37 -05002295 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002296
Michael Layzell734adb42017-06-07 16:58:31 -04002297 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002298 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002299 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002300 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002301 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002302 brace_token: stmts.0,
2303 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002304 })
2305 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002306
2307 fn description() -> Option<&'static str> {
2308 Some("block: `{ .. }`")
2309 }
Alex Crichton954046c2017-05-30 21:49:42 -07002310 }
David Tolnay939766a2016-09-23 23:48:12 -07002311
Michael Layzell734adb42017-06-07 16:58:31 -04002312 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002313 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002314 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002315 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002316 mut standalone: many0!(do_parse!(
2317 stmt: syn!(Stmt) >>
2318 many0!(punct!(;)) >>
2319 (stmt)
2320 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002321 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002322 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002323 mut e: syn!(Expr) >>
2324 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002325 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002326 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002327 })
2328 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002329 (match last {
2330 None => standalone,
2331 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002332 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002333 standalone
2334 }
2335 })
2336 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002337 }
2338
Michael Layzell734adb42017-06-07 16:58:31 -04002339 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002340 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002341 named!(parse -> Self, alt!(
2342 stmt_mac
2343 |
2344 stmt_local
2345 |
2346 stmt_item
2347 |
Michael Layzell35418782017-06-07 09:20:25 -04002348 stmt_blockexpr
2349 |
Michael Layzell92639a52017-06-01 00:07:44 -04002350 stmt_expr
2351 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002352
2353 fn description() -> Option<&'static str> {
2354 Some("statement")
2355 }
Alex Crichton954046c2017-05-30 21:49:42 -07002356 }
David Tolnay939766a2016-09-23 23:48:12 -07002357
Michael Layzell734adb42017-06-07 16:58:31 -04002358 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002359 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002360 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002361 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002362 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002363 // Only parse braces here; paren and bracket will get parsed as
2364 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002365 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002366 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002367 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002368 attrs: attrs,
2369 ident: None,
2370 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002371 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002372 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002373 delimiter: MacroDelimiter::Brace(data.0),
2374 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002375 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002376 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002377 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002378 ));
2379
Michael Layzell734adb42017-06-07 16:58:31 -04002380 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002381 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002382 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002383 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002384 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002385 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002386 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2387 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002388 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002389 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002390 let_token: let_,
2391 pat: Box::new(pat),
2392 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2393 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2394 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002395 }))
David Tolnay191e0582016-10-02 18:31:09 -07002396 ));
2397
Michael Layzell734adb42017-06-07 16:58:31 -04002398 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002399 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002400
Michael Layzell734adb42017-06-07 16:58:31 -04002401 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002402 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002403 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002404 mut e: expr_nosemi >>
2405 // If the next token is a `.` or a `?` it is special-cased to parse as
2406 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002407 not!(punct!(.)) >>
2408 not!(punct!(?)) >>
2409 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002410 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002411 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002412 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002413 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002414 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002415 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002416 }
2417 })
2418 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002419
Michael Layzell734adb42017-06-07 16:58:31 -04002420 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002421 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002422 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002423 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002424 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002425 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002426 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002427 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002428 })
David Tolnay939766a2016-09-23 23:48:12 -07002429 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002430
Michael Layzell734adb42017-06-07 16:58:31 -04002431 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002432 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002433 named!(parse -> Self, alt!(
2434 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2435 |
2436 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2437 |
2438 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2439 |
2440 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2441 |
2442 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2443 |
David Tolnay323279a2017-12-29 11:26:32 -05002444 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002445 |
2446 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2447 |
2448 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2449 |
2450 syn!(PatPath) => { Pat::Path }
2451 |
2452 syn!(PatTuple) => { Pat::Tuple }
2453 |
2454 syn!(PatRef) => { Pat::Ref }
2455 |
2456 syn!(PatSlice) => { Pat::Slice }
2457 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002458
2459 fn description() -> Option<&'static str> {
2460 Some("pattern")
2461 }
Alex Crichton954046c2017-05-30 21:49:42 -07002462 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002463
Michael Layzell734adb42017-06-07 16:58:31 -04002464 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002465 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002466 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002467 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002468 |u| PatWild { underscore_token: u }
2469 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002470
2471 fn description() -> Option<&'static str> {
2472 Some("wild pattern: `_`")
2473 }
Alex Crichton954046c2017-05-30 21:49:42 -07002474 }
David Tolnay84aa0752016-10-02 23:01:13 -07002475
Michael Layzell734adb42017-06-07 16:58:31 -04002476 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002477 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002478 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002479 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002480 pat: syn!(Pat) >>
2481 (PatBox {
2482 pat: Box::new(pat),
2483 box_token: boxed,
2484 })
2485 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002486
2487 fn description() -> Option<&'static str> {
2488 Some("box pattern")
2489 }
Alex Crichton954046c2017-05-30 21:49:42 -07002490 }
2491
Michael Layzell734adb42017-06-07 16:58:31 -04002492 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002493 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002494 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002495 by_ref: option!(keyword!(ref)) >>
2496 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002497 name: alt!(
2498 syn!(Ident)
2499 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002500 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002501 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002502 not!(punct!(<)) >>
2503 not!(punct!(::)) >>
2504 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002505 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002506 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002507 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002508 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002509 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002510 })
2511 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002512
2513 fn description() -> Option<&'static str> {
2514 Some("pattern identifier binding")
2515 }
Alex Crichton954046c2017-05-30 21:49:42 -07002516 }
2517
Michael Layzell734adb42017-06-07 16:58:31 -04002518 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002519 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002520 named!(parse -> Self, do_parse!(
2521 path: syn!(Path) >>
2522 tuple: syn!(PatTuple) >>
2523 (PatTupleStruct {
2524 path: path,
2525 pat: tuple,
2526 })
2527 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002528
2529 fn description() -> Option<&'static str> {
2530 Some("tuple struct pattern")
2531 }
Alex Crichton954046c2017-05-30 21:49:42 -07002532 }
2533
Michael Layzell734adb42017-06-07 16:58:31 -04002534 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002535 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002536 named!(parse -> Self, do_parse!(
2537 path: syn!(Path) >>
2538 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002539 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002540 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002541 (fields, base)
2542 )) >>
2543 (PatStruct {
2544 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002545 fields: (data.1).0,
2546 brace_token: data.0,
2547 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002548 })
2549 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002550
2551 fn description() -> Option<&'static str> {
2552 Some("struct pattern")
2553 }
Alex Crichton954046c2017-05-30 21:49:42 -07002554 }
2555
Michael Layzell734adb42017-06-07 16:58:31 -04002556 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002557 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002558 named!(parse -> Self, alt!(
2559 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002560 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002561 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002562 pat: syn!(Pat) >>
2563 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002564 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002565 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002566 attrs: Vec::new(),
2567 colon_token: Some(colon),
2568 })
2569 )
2570 |
2571 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002572 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002573 by_ref: option!(keyword!(ref)) >>
2574 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002575 ident: syn!(Ident) >>
2576 ({
2577 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002578 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002579 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002580 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002581 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002582 }.into();
2583 if let Some(boxed) = boxed {
2584 pat = PatBox {
2585 pat: Box::new(pat),
2586 box_token: boxed,
2587 }.into();
2588 }
2589 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002590 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002591 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002592 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002593 colon_token: None,
2594 }
2595 })
2596 )
2597 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002598
2599 fn description() -> Option<&'static str> {
2600 Some("field pattern")
2601 }
Alex Crichton954046c2017-05-30 21:49:42 -07002602 }
2603
Michael Layzell734adb42017-06-07 16:58:31 -04002604 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002605 impl Synom for Member {
2606 named!(parse -> Self, alt!(
2607 syn!(Ident) => { Member::Named }
2608 |
2609 syn!(Index) => { Member::Unnamed }
2610 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002611
2612 fn description() -> Option<&'static str> {
2613 Some("field member")
2614 }
David Tolnay85b69a42017-12-27 20:43:10 -05002615 }
2616
2617 #[cfg(feature = "full")]
2618 impl Synom for Index {
2619 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002620 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002621 ({
David Tolnay360efd22018-01-04 23:35:26 -08002622 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002623 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002624 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002625 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002626 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002627 })
David Tolnay85b69a42017-12-27 20:43:10 -05002628 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002629
2630 fn description() -> Option<&'static str> {
2631 Some("field index")
2632 }
David Tolnay85b69a42017-12-27 20:43:10 -05002633 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002634
Michael Layzell734adb42017-06-07 16:58:31 -04002635 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002636 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002637 named!(parse -> Self, map!(
2638 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002639 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002640 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002641
2642 fn description() -> Option<&'static str> {
2643 Some("path pattern")
2644 }
Alex Crichton954046c2017-05-30 21:49:42 -07002645 }
David Tolnay9636c052016-10-02 17:11:17 -07002646
Michael Layzell734adb42017-06-07 16:58:31 -04002647 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002648 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002649 named!(parse -> Self, do_parse!(
2650 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002651 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002652 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2653 tuple!(punct!(..), option!(punct!(,)))
2654 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002655 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002656 Some((_, Some(_))) => true,
2657 _ => false,
2658 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002659 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002660 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002661 )) >>
2662 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002663 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002664 let (dotdot, trailing) = match dotdot {
2665 Some((a, b)) => (Some(a), Some(b)),
2666 None => (None, None),
2667 };
2668 PatTuple {
2669 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002670 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002671 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002672 comma_token: trailing.unwrap_or_default(),
2673 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002674 }
2675 })
2676 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002677
2678 fn description() -> Option<&'static str> {
2679 Some("tuple pattern")
2680 }
Alex Crichton954046c2017-05-30 21:49:42 -07002681 }
David Tolnayfbb73232016-10-03 01:00:06 -07002682
Michael Layzell734adb42017-06-07 16:58:31 -04002683 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002684 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002685 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002686 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002687 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002688 pat: syn!(Pat) >>
2689 (PatRef {
2690 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002691 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002692 and_token: and,
2693 })
2694 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002695
2696 fn description() -> Option<&'static str> {
2697 Some("reference pattern")
2698 }
Alex Crichton954046c2017-05-30 21:49:42 -07002699 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002700
Michael Layzell734adb42017-06-07 16:58:31 -04002701 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002702 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002703 named!(parse -> Self, do_parse!(
2704 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002705 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002706 return parse_error(); // these need to be parsed by pat_path
2707 } else {
2708 PatLit {
2709 expr: Box::new(lit),
2710 }
2711 })
2712 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002713
2714 fn description() -> Option<&'static str> {
2715 Some("literal pattern")
2716 }
Alex Crichton954046c2017-05-30 21:49:42 -07002717 }
David Tolnaye1310902016-10-29 23:40:00 -07002718
Michael Layzell734adb42017-06-07 16:58:31 -04002719 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002720 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002721 named!(parse -> Self, do_parse!(
2722 lo: pat_lit_expr >>
2723 limits: syn!(RangeLimits) >>
2724 hi: pat_lit_expr >>
2725 (PatRange {
2726 lo: Box::new(lo),
2727 hi: Box::new(hi),
2728 limits: limits,
2729 })
2730 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002731
2732 fn description() -> Option<&'static str> {
2733 Some("range pattern")
2734 }
Alex Crichton954046c2017-05-30 21:49:42 -07002735 }
David Tolnaye1310902016-10-29 23:40:00 -07002736
Michael Layzell734adb42017-06-07 16:58:31 -04002737 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002738 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002739 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002740 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002741 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002742 |
David Tolnay8c91b882017-12-28 23:04:32 -05002743 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002744 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002745 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002746 Expr::Unary(ExprUnary {
2747 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002748 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002749 expr: Box::new(v)
2750 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002751 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002752 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002753 })
2754 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002755
Michael Layzell734adb42017-06-07 16:58:31 -04002756 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002757 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002758 named!(parse -> Self, map!(
2759 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002760 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002761 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002762 dots: punct!(..) >>
2763 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002764 (dots, trailing)
2765 )) >>
2766 after: cond!(
2767 match middle {
2768 Some((_, ref trailing)) => trailing.is_some(),
2769 _ => false,
2770 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002771 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002772 ) >>
2773 (before, middle, after)
2774 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002775 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002776 let mut before: Punctuated<Pat, Token![,]> = before;
2777 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002778 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002779 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002780 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002781 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002782 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002783 }),
2784 bracket_token: brackets,
2785 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002786 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002787 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002788 } else {
David Tolnay56080682018-01-06 14:01:52 -08002789 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002790 }
2791 }),
2792 front: before,
2793 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002794 }
Alex Crichton954046c2017-05-30 21:49:42 -07002795 }
Michael Layzell92639a52017-06-01 00:07:44 -04002796 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002797
2798 fn description() -> Option<&'static str> {
2799 Some("slice pattern")
2800 }
Alex Crichton954046c2017-05-30 21:49:42 -07002801 }
David Tolnay323279a2017-12-29 11:26:32 -05002802
2803 #[cfg(feature = "full")]
2804 impl Synom for PatMacro {
2805 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002806
2807 fn description() -> Option<&'static str> {
2808 Some("macro pattern")
2809 }
David Tolnay323279a2017-12-29 11:26:32 -05002810 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002811}
2812
David Tolnayf4bbbd92016-09-23 14:41:55 -07002813#[cfg(feature = "printing")]
2814mod printing {
2815 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002816 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002817 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002818 use quote::{ToTokens, Tokens};
Alex Crichton9a4dca22018-03-28 06:32:19 -07002819 use proc_macro2::Literal;
David Tolnayf4bbbd92016-09-23 14:41:55 -07002820
David Tolnaybcf26022017-12-25 22:10:52 -05002821 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2822 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002823 #[cfg(feature = "full")]
2824 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002825 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002826 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002827 e.to_tokens(tokens);
2828 });
2829 } else {
2830 e.to_tokens(tokens);
2831 }
2832 }
2833
David Tolnay8c91b882017-12-28 23:04:32 -05002834 #[cfg(feature = "full")]
2835 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2836 tokens.append_all(attrs.outer());
2837 }
Michael Layzell734adb42017-06-07 16:58:31 -04002838
David Tolnay8c91b882017-12-28 23:04:32 -05002839 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002840 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002841
Michael Layzell734adb42017-06-07 16:58:31 -04002842 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002843 impl ToTokens for ExprBox {
2844 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002845 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002846 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002847 self.expr.to_tokens(tokens);
2848 }
2849 }
2850
Michael Layzell734adb42017-06-07 16:58:31 -04002851 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002852 impl ToTokens for ExprInPlace {
2853 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002854 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002855 self.place.to_tokens(tokens);
2856 self.arrow_token.to_tokens(tokens);
2857 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002858 }
2859 }
2860
Michael Layzell734adb42017-06-07 16:58:31 -04002861 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002862 impl ToTokens for ExprArray {
2863 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002864 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002865 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002866 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002867 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002868 }
2869 }
2870
2871 impl ToTokens for ExprCall {
2872 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002873 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002874 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002875 self.paren_token.surround(tokens, |tokens| {
2876 self.args.to_tokens(tokens);
2877 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002878 }
2879 }
2880
Michael Layzell734adb42017-06-07 16:58:31 -04002881 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002882 impl ToTokens for ExprMethodCall {
2883 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002884 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002885 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002886 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002887 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002888 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002889 self.paren_token.surround(tokens, |tokens| {
2890 self.args.to_tokens(tokens);
2891 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002892 }
2893 }
2894
Michael Layzell734adb42017-06-07 16:58:31 -04002895 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002896 impl ToTokens for MethodTurbofish {
2897 fn to_tokens(&self, tokens: &mut Tokens) {
2898 self.colon2_token.to_tokens(tokens);
2899 self.lt_token.to_tokens(tokens);
2900 self.args.to_tokens(tokens);
2901 self.gt_token.to_tokens(tokens);
2902 }
2903 }
2904
2905 #[cfg(feature = "full")]
2906 impl ToTokens for GenericMethodArgument {
2907 fn to_tokens(&self, tokens: &mut Tokens) {
2908 match *self {
2909 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2910 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2911 }
2912 }
2913 }
2914
2915 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002916 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002917 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002918 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002919 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002920 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002921 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002922 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002923 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002924 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002925 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002926 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002927 }
2928 }
2929
2930 impl ToTokens for ExprBinary {
2931 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002932 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002933 self.left.to_tokens(tokens);
2934 self.op.to_tokens(tokens);
2935 self.right.to_tokens(tokens);
2936 }
2937 }
2938
2939 impl ToTokens for ExprUnary {
2940 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002941 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002942 self.op.to_tokens(tokens);
2943 self.expr.to_tokens(tokens);
2944 }
2945 }
2946
David Tolnay8c91b882017-12-28 23:04:32 -05002947 impl ToTokens for ExprLit {
2948 fn to_tokens(&self, tokens: &mut Tokens) {
2949 attrs_to_tokens(&self.attrs, tokens);
2950 self.lit.to_tokens(tokens);
2951 }
2952 }
2953
Alex Crichton62a0a592017-05-22 13:58:53 -07002954 impl ToTokens for ExprCast {
2955 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002956 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002957 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002958 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002959 self.ty.to_tokens(tokens);
2960 }
2961 }
2962
David Tolnay0cf94f22017-12-28 23:46:26 -05002963 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002964 impl ToTokens for ExprType {
2965 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002966 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002967 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002968 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002969 self.ty.to_tokens(tokens);
2970 }
2971 }
2972
Michael Layzell734adb42017-06-07 16:58:31 -04002973 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002974 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002975 if let Some((ref else_token, ref else_)) = *else_ {
2976 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002977
2978 // If we are not one of the valid expressions to exist in an else
2979 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002980 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002981 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002982 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002983 }
2984 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002985 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002986 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002987 });
2988 }
2989 }
2990 }
2991 }
2992
2993 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002994 impl ToTokens for ExprIf {
2995 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002996 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002997 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002998 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002999 self.then_branch.to_tokens(tokens);
3000 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003001 }
3002 }
3003
Michael Layzell734adb42017-06-07 16:58:31 -04003004 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003005 impl ToTokens for ExprIfLet {
3006 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003007 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003008 self.if_token.to_tokens(tokens);
3009 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003010 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003012 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003013 self.then_branch.to_tokens(tokens);
3014 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003015 }
3016 }
3017
Michael Layzell734adb42017-06-07 16:58:31 -04003018 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003019 impl ToTokens for ExprWhile {
3020 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003021 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003022 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003023 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003024 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07003025 self.body.to_tokens(tokens);
3026 }
3027 }
3028
Michael Layzell734adb42017-06-07 16:58:31 -04003029 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003030 impl ToTokens for ExprWhileLet {
3031 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003032 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003033 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 self.while_token.to_tokens(tokens);
3035 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003036 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003037 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003038 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07003039 self.body.to_tokens(tokens);
3040 }
3041 }
3042
Michael Layzell734adb42017-06-07 16:58:31 -04003043 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003044 impl ToTokens for ExprForLoop {
3045 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003046 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003047 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003048 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003049 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003050 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003051 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07003052 self.body.to_tokens(tokens);
3053 }
3054 }
3055
Michael Layzell734adb42017-06-07 16:58:31 -04003056 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003057 impl ToTokens for ExprLoop {
3058 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003059 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003060 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003061 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003062 self.body.to_tokens(tokens);
3063 }
3064 }
3065
Michael Layzell734adb42017-06-07 16:58:31 -04003066 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003067 impl ToTokens for ExprMatch {
3068 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003069 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003070 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003071 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003072 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05003073 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003074 arm.to_tokens(tokens);
3075 // Ensure that we have a comma after a non-block arm, except
3076 // for the last one.
3077 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003078 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003079 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003080 }
3081 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003082 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003083 }
3084 }
3085
Michael Layzell734adb42017-06-07 16:58:31 -04003086 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003087 impl ToTokens for ExprCatch {
3088 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003089 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003090 self.do_token.to_tokens(tokens);
3091 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003092 self.block.to_tokens(tokens);
3093 }
3094 }
3095
Michael Layzell734adb42017-06-07 16:58:31 -04003096 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003097 impl ToTokens for ExprYield {
3098 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003099 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07003100 self.yield_token.to_tokens(tokens);
3101 self.expr.to_tokens(tokens);
3102 }
3103 }
3104
3105 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003106 impl ToTokens for ExprClosure {
3107 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003108 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003109 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003110 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003111 for input in self.inputs.pairs() {
3112 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003113 FnArg::Captured(ArgCaptured {
3114 ref pat,
3115 ty: Type::Infer(_),
3116 ..
3117 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003118 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003119 }
David Tolnay56080682018-01-06 14:01:52 -08003120 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003121 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003122 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003123 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003124 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003125 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003126 self.body.to_tokens(tokens);
3127 }
3128 }
3129
Michael Layzell734adb42017-06-07 16:58:31 -04003130 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003131 impl ToTokens for ExprUnsafe {
3132 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003133 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05003134 self.unsafe_token.to_tokens(tokens);
3135 self.block.to_tokens(tokens);
3136 }
3137 }
3138
3139 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003140 impl ToTokens for ExprBlock {
3141 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003142 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003143 self.block.to_tokens(tokens);
3144 }
3145 }
3146
Michael Layzell734adb42017-06-07 16:58:31 -04003147 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003148 impl ToTokens for ExprAssign {
3149 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003150 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003151 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003152 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003153 self.right.to_tokens(tokens);
3154 }
3155 }
3156
Michael Layzell734adb42017-06-07 16:58:31 -04003157 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003158 impl ToTokens for ExprAssignOp {
3159 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003160 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003161 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003162 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003163 self.right.to_tokens(tokens);
3164 }
3165 }
3166
Michael Layzell734adb42017-06-07 16:58:31 -04003167 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003168 impl ToTokens for ExprField {
3169 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003170 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003171 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003172 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003173 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003174 }
3175 }
3176
David Tolnay85b69a42017-12-27 20:43:10 -05003177 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07003178 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003179 match *self {
3180 Member::Named(ident) => ident.to_tokens(tokens),
3181 Member::Unnamed(ref index) => index.to_tokens(tokens),
3182 }
3183 }
3184 }
3185
David Tolnay85b69a42017-12-27 20:43:10 -05003186 impl ToTokens for Index {
3187 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003188 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3189 lit.set_span(self.span);
3190 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003191 }
3192 }
3193
3194 impl ToTokens for ExprIndex {
3195 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003196 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003197 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003198 self.bracket_token.surround(tokens, |tokens| {
3199 self.index.to_tokens(tokens);
3200 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003201 }
3202 }
3203
Michael Layzell734adb42017-06-07 16:58:31 -04003204 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003205 impl ToTokens for ExprRange {
3206 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003207 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003208 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003209 match self.limits {
3210 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3211 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3212 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003213 self.to.to_tokens(tokens);
3214 }
3215 }
3216
3217 impl ToTokens for ExprPath {
3218 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003219 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003220 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003221 }
3222 }
3223
Michael Layzell734adb42017-06-07 16:58:31 -04003224 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003225 impl ToTokens for ExprReference {
Alex Crichton62a0a592017-05-22 13:58:53 -07003226 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003227 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003228 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003229 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003230 self.expr.to_tokens(tokens);
3231 }
3232 }
3233
Michael Layzell734adb42017-06-07 16:58:31 -04003234 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003235 impl ToTokens for ExprBreak {
3236 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003237 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003238 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003239 self.label.to_tokens(tokens);
3240 self.expr.to_tokens(tokens);
3241 }
3242 }
3243
Michael Layzell734adb42017-06-07 16:58:31 -04003244 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003245 impl ToTokens for ExprContinue {
3246 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003247 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003248 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003249 self.label.to_tokens(tokens);
3250 }
3251 }
3252
Michael Layzell734adb42017-06-07 16:58:31 -04003253 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003254 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003255 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003256 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003257 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003258 self.expr.to_tokens(tokens);
3259 }
3260 }
3261
Michael Layzell734adb42017-06-07 16:58:31 -04003262 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003263 impl ToTokens for ExprMacro {
3264 fn to_tokens(&self, tokens: &mut Tokens) {
3265 tokens.append_all(self.attrs.outer());
3266 self.mac.to_tokens(tokens);
3267 }
3268 }
3269
3270 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003271 impl ToTokens for ExprStruct {
3272 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003273 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003274 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003275 self.brace_token.surround(tokens, |tokens| {
3276 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003277 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003278 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003279 self.rest.to_tokens(tokens);
3280 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003281 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003282 }
3283 }
3284
Michael Layzell734adb42017-06-07 16:58:31 -04003285 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003286 impl ToTokens for ExprRepeat {
3287 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003288 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003289 self.bracket_token.surround(tokens, |tokens| {
3290 self.expr.to_tokens(tokens);
3291 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003292 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003293 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003294 }
3295 }
3296
David Tolnaye98775f2017-12-28 23:17:00 -05003297 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003298 impl ToTokens for ExprGroup {
3299 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003300 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003301 self.group_token.surround(tokens, |tokens| {
3302 self.expr.to_tokens(tokens);
3303 });
3304 }
3305 }
3306
Alex Crichton62a0a592017-05-22 13:58:53 -07003307 impl ToTokens for ExprParen {
3308 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003309 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003310 self.paren_token.surround(tokens, |tokens| {
3311 self.expr.to_tokens(tokens);
3312 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003313 }
3314 }
3315
Michael Layzell734adb42017-06-07 16:58:31 -04003316 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003317 impl ToTokens for ExprTry {
3318 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003319 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003320 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003321 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003322 }
3323 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003324
David Tolnay2ae520a2017-12-29 11:19:50 -05003325 impl ToTokens for ExprVerbatim {
3326 fn to_tokens(&self, tokens: &mut Tokens) {
3327 self.tts.to_tokens(tokens);
3328 }
3329 }
3330
Michael Layzell734adb42017-06-07 16:58:31 -04003331 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003332 impl ToTokens for Label {
3333 fn to_tokens(&self, tokens: &mut Tokens) {
3334 self.name.to_tokens(tokens);
3335 self.colon_token.to_tokens(tokens);
3336 }
3337 }
3338
3339 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003340 impl ToTokens for FieldValue {
3341 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc42b90a2018-01-18 23:11:37 -08003342 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003343 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003344 if let Some(ref colon_token) = self.colon_token {
3345 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003346 self.expr.to_tokens(tokens);
3347 }
David Tolnay055a7042016-10-02 19:23:54 -07003348 }
3349 }
3350
Michael Layzell734adb42017-06-07 16:58:31 -04003351 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003352 impl ToTokens for Arm {
3353 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003354 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003355 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003356 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003357 if let Some((ref if_token, ref guard)) = self.guard {
3358 if_token.to_tokens(tokens);
3359 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003360 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003361 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003362 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003363 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003364 }
3365 }
3366
Michael Layzell734adb42017-06-07 16:58:31 -04003367 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003368 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003369 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003370 self.underscore_token.to_tokens(tokens);
3371 }
3372 }
3373
Michael Layzell734adb42017-06-07 16:58:31 -04003374 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003375 impl ToTokens for PatIdent {
3376 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003377 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003378 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003379 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003380 if let Some((ref at_token, ref subpat)) = self.subpat {
3381 at_token.to_tokens(tokens);
3382 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003383 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003384 }
3385 }
3386
Michael Layzell734adb42017-06-07 16:58:31 -04003387 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003388 impl ToTokens for PatStruct {
3389 fn to_tokens(&self, tokens: &mut Tokens) {
3390 self.path.to_tokens(tokens);
3391 self.brace_token.surround(tokens, |tokens| {
3392 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003393 // NOTE: We need a comma before the dot2 token if it is present.
3394 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003395 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003396 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003397 self.dot2_token.to_tokens(tokens);
3398 });
3399 }
3400 }
3401
Michael Layzell734adb42017-06-07 16:58:31 -04003402 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003403 impl ToTokens for PatTupleStruct {
3404 fn to_tokens(&self, tokens: &mut Tokens) {
3405 self.path.to_tokens(tokens);
3406 self.pat.to_tokens(tokens);
3407 }
3408 }
3409
Michael Layzell734adb42017-06-07 16:58:31 -04003410 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003411 impl ToTokens for PatPath {
3412 fn to_tokens(&self, tokens: &mut Tokens) {
3413 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3414 }
3415 }
3416
Michael Layzell734adb42017-06-07 16:58:31 -04003417 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003418 impl ToTokens for PatTuple {
3419 fn to_tokens(&self, tokens: &mut Tokens) {
3420 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003421 self.front.to_tokens(tokens);
3422 if let Some(ref dot2_token) = self.dot2_token {
3423 if !self.front.empty_or_trailing() {
3424 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003425 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003426 }
David Tolnay41871922017-12-29 01:53:45 -05003427 dot2_token.to_tokens(tokens);
3428 self.comma_token.to_tokens(tokens);
3429 if self.comma_token.is_none() && !self.back.is_empty() {
3430 // Ensure there is a comma after the .. token.
3431 <Token![,]>::default().to_tokens(tokens);
3432 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003433 }
David Tolnay41871922017-12-29 01:53:45 -05003434 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003435 });
3436 }
3437 }
3438
Michael Layzell734adb42017-06-07 16:58:31 -04003439 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003440 impl ToTokens for PatBox {
3441 fn to_tokens(&self, tokens: &mut Tokens) {
3442 self.box_token.to_tokens(tokens);
3443 self.pat.to_tokens(tokens);
3444 }
3445 }
3446
Michael Layzell734adb42017-06-07 16:58:31 -04003447 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003448 impl ToTokens for PatRef {
3449 fn to_tokens(&self, tokens: &mut Tokens) {
3450 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003451 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003452 self.pat.to_tokens(tokens);
3453 }
3454 }
3455
Michael Layzell734adb42017-06-07 16:58:31 -04003456 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003457 impl ToTokens for PatLit {
3458 fn to_tokens(&self, tokens: &mut Tokens) {
3459 self.expr.to_tokens(tokens);
3460 }
3461 }
3462
Michael Layzell734adb42017-06-07 16:58:31 -04003463 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003464 impl ToTokens for PatRange {
3465 fn to_tokens(&self, tokens: &mut Tokens) {
3466 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003467 match self.limits {
3468 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3469 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3470 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003471 self.hi.to_tokens(tokens);
3472 }
3473 }
3474
Michael Layzell734adb42017-06-07 16:58:31 -04003475 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003476 impl ToTokens for PatSlice {
3477 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003478 // XXX: This is a mess, and it will be so easy to screw it up. How
3479 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003480 self.bracket_token.surround(tokens, |tokens| {
3481 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003482
3483 // If we need a comma before the middle or standalone .. token,
3484 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003485 if !self.front.empty_or_trailing()
3486 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003487 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003488 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003489 }
3490
3491 // If we have an identifier, we always need a .. token.
3492 if self.middle.is_some() {
3493 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003494 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003495 } else if self.dot2_token.is_some() {
3496 self.dot2_token.to_tokens(tokens);
3497 }
3498
3499 // Make sure we have a comma before the back half.
3500 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003501 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003502 self.back.to_tokens(tokens);
3503 } else {
3504 self.comma_token.to_tokens(tokens);
3505 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003506 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003507 }
3508 }
3509
Michael Layzell734adb42017-06-07 16:58:31 -04003510 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003511 impl ToTokens for PatMacro {
3512 fn to_tokens(&self, tokens: &mut Tokens) {
3513 self.mac.to_tokens(tokens);
3514 }
3515 }
3516
3517 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003518 impl ToTokens for PatVerbatim {
3519 fn to_tokens(&self, tokens: &mut Tokens) {
3520 self.tts.to_tokens(tokens);
3521 }
3522 }
3523
3524 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003525 impl ToTokens for FieldPat {
3526 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003527 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003528 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003529 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003530 }
3531 self.pat.to_tokens(tokens);
3532 }
3533 }
3534
Michael Layzell734adb42017-06-07 16:58:31 -04003535 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003536 impl ToTokens for Block {
3537 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003538 self.brace_token.surround(tokens, |tokens| {
3539 tokens.append_all(&self.stmts);
3540 });
David Tolnay42602292016-10-01 22:25:45 -07003541 }
3542 }
3543
Michael Layzell734adb42017-06-07 16:58:31 -04003544 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003545 impl ToTokens for Stmt {
3546 fn to_tokens(&self, tokens: &mut Tokens) {
3547 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003548 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003549 Stmt::Item(ref item) => item.to_tokens(tokens),
3550 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003551 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003552 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003553 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003554 }
David Tolnay42602292016-10-01 22:25:45 -07003555 }
3556 }
3557 }
David Tolnay191e0582016-10-02 18:31:09 -07003558
Michael Layzell734adb42017-06-07 16:58:31 -04003559 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003560 impl ToTokens for Local {
3561 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003562 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003563 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003564 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003565 if let Some((ref colon_token, ref ty)) = self.ty {
3566 colon_token.to_tokens(tokens);
3567 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003568 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003569 if let Some((ref eq_token, ref init)) = self.init {
3570 eq_token.to_tokens(tokens);
3571 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003572 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003573 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003574 }
3575 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003576}