blob: 2d85e500938d0bd01215dd0f25c35712be92b2ac [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.*
Michael Layzell734adb42017-06-07 16:58:31 -0400409 pub AddrOf(ExprAddrOf #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, .. })
592 | Expr::AddrOf(ExprAddrOf { ref mut attrs, .. })
593 | 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 Tolnayf2cfd722017-12-31 18:02:51 -0500956 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500957 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800958 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700959 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800960 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700962}
963
Michael Layzell734adb42017-06-07 16:58:31 -0400964#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700965ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800966 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800967 ///
968 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700969 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700970 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800971 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800972 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800973 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800974 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700975 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700976}
977
Michael Layzell734adb42017-06-07 16:58:31 -0400978#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700979ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800980 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700981 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800982 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
983 /// 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 -0800984 ///
985 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700986 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500987 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500988 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500989 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700990 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700991 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700992}
993
Michael Layzell3936ceb2017-07-08 00:28:36 -0400994#[cfg(any(feature = "parsing", feature = "printing"))]
995#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700996fn arm_expr_requires_comma(expr: &Expr) -> bool {
997 // see https://github.com/rust-lang/rust/blob/eb8f2586e
998 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500999 match *expr {
1000 Expr::Unsafe(..)
1001 | Expr::Block(..)
1002 | Expr::If(..)
1003 | Expr::IfLet(..)
1004 | Expr::Match(..)
1005 | Expr::While(..)
1006 | Expr::WhileLet(..)
1007 | Expr::Loop(..)
1008 | Expr::ForLoop(..)
1009 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001010 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001011 }
1012}
1013
David Tolnayb9c8e322016-09-23 20:48:37 -07001014#[cfg(feature = "parsing")]
1015pub mod parsing {
1016 use super::*;
David Tolnay056de302018-01-05 14:29:05 -08001017 use path::parsing::qpath;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001018 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001019 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001020
Michael Layzell734adb42017-06-07 16:58:31 -04001021 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -08001022 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -05001023 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -08001024 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001025 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001026 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -05001027 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001028
David Tolnaybcf26022017-12-25 22:10:52 -05001029 // When we're parsing expressions which occur before blocks, like in an if
1030 // statement's condition, we cannot parse a struct literal.
1031 //
1032 // Struct literals are ambiguous in certain positions
1033 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001034 macro_rules! ambiguous_expr {
1035 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001036 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001037 };
1038 }
1039
David Tolnaybcf26022017-12-25 22:10:52 -05001040 // When we are parsing an optional suffix expression, we cannot allow blocks
1041 // if structs are not allowed.
1042 //
1043 // Example:
1044 //
1045 // if break {} {}
1046 //
1047 // is ambiguous between:
1048 //
1049 // if (break {}) {}
1050 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001051 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001052 macro_rules! opt_ambiguous_expr {
1053 ($i:expr, $allow_struct:ident) => {
1054 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1055 };
1056 }
1057
Alex Crichton954046c2017-05-30 21:49:42 -07001058 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001059 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001060
1061 fn description() -> Option<&'static str> {
1062 Some("expression")
1063 }
1064 }
1065
Michael Layzell734adb42017-06-07 16:58:31 -04001066 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001067 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1068
David Tolnaybcf26022017-12-25 22:10:52 -05001069 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001070 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001071 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001072 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001073 }
1074
Michael Layzell734adb42017-06-07 16:58:31 -04001075 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001076 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001077 // NOTE: We intentionally skip assign_expr, placement_expr, and
1078 // range_expr, as they are not parsed in non-full mode.
1079 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001080 }
1081
David Tolnaybcf26022017-12-25 22:10:52 -05001082 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001083 macro_rules! binop {
1084 (
1085 $name: ident,
1086 $next: ident,
1087 $submac: ident!( $($args:tt)* )
1088 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001089 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001090 mut e: call!($next, allow_struct, allow_block) >>
1091 many0!(do_parse!(
1092 op: $submac!($($args)*) >>
1093 rhs: call!($next, allow_struct, true) >>
1094 ({
1095 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001096 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001097 left: Box::new(e.into()),
1098 op: op,
1099 right: Box::new(rhs.into()),
1100 }.into();
1101 })
1102 )) >>
1103 (e)
1104 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001105 }
David Tolnay54e854d2016-10-24 12:03:30 -07001106 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001107
David Tolnaybcf26022017-12-25 22:10:52 -05001108 // <placement> = <placement> ..
1109 // <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 //
1120 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001121 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001122 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001123 mut e: call!(placement_expr, allow_struct, allow_block) >>
1124 alt!(
1125 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001126 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001127 // Recurse into self to parse right-associative operator.
1128 rhs: call!(assign_expr, allow_struct, true) >>
1129 ({
1130 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001131 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001132 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001133 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001134 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001135 }.into();
1136 })
1137 )
1138 |
1139 do_parse!(
1140 op: call!(BinOp::parse_assign_op) >>
1141 // Recurse into self to parse right-associative operator.
1142 rhs: call!(assign_expr, allow_struct, true) >>
1143 ({
1144 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001145 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001146 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001147 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001148 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001149 }.into();
1150 })
1151 )
1152 |
1153 epsilon!()
1154 ) >>
1155 (e)
1156 ));
1157
David Tolnaybcf26022017-12-25 22:10:52 -05001158 // <range> <- <range> ..
1159 //
1160 // NOTE: The `in place { expr }` version of this syntax is parsed in
1161 // `atom_expr`, not here.
1162 //
1163 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001164 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001165 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001166 mut e: call!(range_expr, allow_struct, allow_block) >>
1167 alt!(
1168 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001169 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001170 // Recurse into self to parse right-associative operator.
1171 rhs: call!(placement_expr, allow_struct, true) >>
1172 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001174 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001175 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001176 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001177 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001178 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179 }.into();
1180 })
1181 )
1182 |
1183 epsilon!()
1184 ) >>
1185 (e)
1186 ));
1187
David Tolnaybcf26022017-12-25 22:10:52 -05001188 // <or> ... <or> ..
1189 // <or> .. <or> ..
1190 // <or> ..
1191 //
1192 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1193 // rules are for parsing these expressions are, but this is not correct.
1194 // For example, `a .. b .. c` is not a legal expression. It should not
1195 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1196 //
1197 // NOTE: The form of ranges which don't include a preceding expression are
1198 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001199 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001200 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001201 mut e: call!(or_expr, allow_struct, allow_block) >>
1202 many0!(do_parse!(
1203 limits: syn!(RangeLimits) >>
1204 // We don't want to allow blocks here if we don't allow structs. See
1205 // the reasoning for `opt_ambiguous_expr!` above.
1206 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1207 ({
1208 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001209 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001210 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001211 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001212 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001213 }.into();
1214 })
1215 )) >>
1216 (e)
1217 ));
1218
David Tolnaybcf26022017-12-25 22:10:52 -05001219 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001220 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001221
David Tolnaybcf26022017-12-25 22:10:52 -05001222 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001223 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001224
David Tolnaybcf26022017-12-25 22:10:52 -05001225 // <bitor> == <bitor> ...
1226 // <bitor> != <bitor> ...
1227 // <bitor> >= <bitor> ...
1228 // <bitor> <= <bitor> ...
1229 // <bitor> > <bitor> ...
1230 // <bitor> < <bitor> ...
1231 //
1232 // NOTE: This operator appears to be parsed as left-associative, but errors
1233 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001234 binop!(
1235 compare_expr,
1236 bitor_expr,
1237 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001238 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001239 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001240 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001241 |
1242 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001243 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001244 |
1245 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001246 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001248 do_parse!(
1249 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001250 not!(punct!(<-)) >>
1251 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001252 (BinOp::Lt(t))
1253 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001254 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001255 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001256 )
1257 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001258
David Tolnaybcf26022017-12-25 22:10:52 -05001259 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001260 binop!(
1261 bitor_expr,
1262 bitxor_expr,
1263 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1264 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001265
David Tolnaybcf26022017-12-25 22:10:52 -05001266 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001267 binop!(
1268 bitxor_expr,
1269 bitand_expr,
1270 do_parse!(
1271 // NOTE: Make sure we aren't looking at ^=.
1272 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1273 )
1274 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001275
David Tolnaybcf26022017-12-25 22:10:52 -05001276 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001277 binop!(
1278 bitand_expr,
1279 shift_expr,
1280 do_parse!(
1281 // NOTE: Make sure we aren't looking at && or &=.
1282 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1283 )
1284 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001285
David Tolnaybcf26022017-12-25 22:10:52 -05001286 // <arith> << <arith> ...
1287 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001288 binop!(
1289 shift_expr,
1290 arith_expr,
1291 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001292 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001293 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001295 )
1296 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001297
David Tolnaybcf26022017-12-25 22:10:52 -05001298 // <term> + <term> ...
1299 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001300 binop!(
1301 arith_expr,
1302 term_expr,
1303 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001304 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001305 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001306 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001307 )
1308 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001309
David Tolnaybcf26022017-12-25 22:10:52 -05001310 // <cast> * <cast> ...
1311 // <cast> / <cast> ...
1312 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001313 binop!(
1314 term_expr,
1315 cast_expr,
1316 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001317 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001318 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001319 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001321 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001322 )
1323 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001324
David Tolnaybcf26022017-12-25 22:10:52 -05001325 // <unary> as <ty>
1326 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001327 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001328 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001329 mut e: call!(unary_expr, allow_struct, allow_block) >>
1330 many0!(alt!(
1331 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001332 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001333 // We can't accept `A + B` in cast expressions, as it's
1334 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001335 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001336 ({
1337 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001338 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001339 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001340 as_token: as_,
1341 ty: Box::new(ty),
1342 }.into();
1343 })
1344 )
1345 |
1346 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001347 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001348 // We can't accept `A + B` in cast expressions, as it's
1349 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001350 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001351 ({
1352 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001353 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001354 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001355 colon_token: colon,
1356 ty: Box::new(ty),
1357 }.into();
1358 })
1359 )
1360 )) >>
1361 (e)
1362 ));
1363
David Tolnay0cf94f22017-12-28 23:46:26 -05001364 // <unary> as <ty>
1365 #[cfg(not(feature = "full"))]
1366 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1367 mut e: call!(unary_expr, allow_struct, allow_block) >>
1368 many0!(do_parse!(
1369 as_: keyword!(as) >>
1370 // We can't accept `A + B` in cast expressions, as it's
1371 // ambiguous with the + expression.
1372 ty: call!(Type::without_plus) >>
1373 ({
1374 e = ExprCast {
1375 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001376 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001377 as_token: as_,
1378 ty: Box::new(ty),
1379 }.into();
1380 })
1381 )) >>
1382 (e)
1383 ));
1384
David Tolnaybcf26022017-12-25 22:10:52 -05001385 // <UnOp> <trailer>
1386 // & <trailer>
1387 // &mut <trailer>
1388 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001389 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001390 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001391 do_parse!(
1392 op: syn!(UnOp) >>
1393 expr: call!(unary_expr, allow_struct, true) >>
1394 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001395 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001396 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001397 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001398 }.into())
1399 )
1400 |
1401 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001402 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001403 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001404 expr: call!(unary_expr, allow_struct, true) >>
1405 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001406 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001407 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001408 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001409 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001410 }.into())
1411 )
1412 |
1413 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001414 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001415 expr: call!(unary_expr, allow_struct, true) >>
1416 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001417 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001418 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001419 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001420 }.into())
1421 )
1422 |
1423 call!(trailer_expr, allow_struct, allow_block)
1424 ));
1425
Michael Layzell734adb42017-06-07 16:58:31 -04001426 // XXX: This duplication is ugly
1427 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001428 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001429 do_parse!(
1430 op: syn!(UnOp) >>
1431 expr: call!(unary_expr, allow_struct, true) >>
1432 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001433 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001434 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001435 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001436 }.into())
1437 )
1438 |
1439 call!(trailer_expr, allow_struct, allow_block)
1440 ));
1441
David Tolnaybcf26022017-12-25 22:10:52 -05001442 // <atom> (..<args>) ...
1443 // <atom> . <ident> (..<args>) ...
1444 // <atom> . <ident> ...
1445 // <atom> . <lit> ...
1446 // <atom> [ <expr> ] ...
1447 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001448 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001449 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001450 mut e: call!(atom_expr, allow_struct, allow_block) >>
1451 many0!(alt!(
1452 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001453 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001454 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001455 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001456 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001457 args: args,
1458 paren_token: paren,
1459 }.into();
1460 })
1461 |
1462 tap!(more: and_method_call => {
1463 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001464 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001465 e = call.into();
1466 })
1467 |
1468 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001469 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001470 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001471 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001472 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001473 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001474 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001475 }.into();
1476 })
1477 |
1478 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001479 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001480 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001481 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001482 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001483 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001484 index: Box::new(i),
1485 }.into();
1486 })
1487 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001488 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001489 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001490 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001491 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001492 question_token: question,
1493 }.into();
1494 })
1495 )) >>
1496 (e)
1497 ));
1498
Michael Layzell734adb42017-06-07 16:58:31 -04001499 // XXX: Duplication == ugly
1500 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001501 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001502 mut e: call!(atom_expr, allow_struct, allow_block) >>
1503 many0!(alt!(
1504 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001505 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001506 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001507 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001508 paren_token: args.0,
1509 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001510 }.into();
1511 })
1512 |
1513 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001514 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001515 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001516 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001517 bracket_token: i.0,
1518 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001519 }.into();
1520 })
1521 )) >>
1522 (e)
1523 ));
1524
David Tolnaya454c8f2018-01-07 01:01:10 -08001525 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001526 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001527 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001528 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1529 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001530 |
David Tolnay8c91b882017-12-28 23:04:32 -05001531 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001532 |
1533 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001534 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001535 |
David Tolnay8c91b882017-12-28 23:04:32 -05001536 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001537 |
David Tolnay8c91b882017-12-28 23:04:32 -05001538 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001539 |
1540 call!(expr_break, allow_struct) // must be before expr_path
1541 |
David Tolnay8c91b882017-12-28 23:04:32 -05001542 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001543 |
1544 call!(expr_ret, allow_struct) // must be before expr_path
1545 |
David Tolnay8c91b882017-12-28 23:04:32 -05001546 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001547 |
David Tolnay8c91b882017-12-28 23:04:32 -05001548 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001549 |
David Tolnay8c91b882017-12-28 23:04:32 -05001550 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001551 |
David Tolnay8c91b882017-12-28 23:04:32 -05001552 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001553 |
David Tolnay8c91b882017-12-28 23:04:32 -05001554 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001555 |
David Tolnay8c91b882017-12-28 23:04:32 -05001556 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001557 |
David Tolnay8c91b882017-12-28 23:04:32 -05001558 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001559 |
David Tolnay8c91b882017-12-28 23:04:32 -05001560 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001561 |
David Tolnay8c91b882017-12-28 23:04:32 -05001562 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001563 |
David Tolnay8c91b882017-12-28 23:04:32 -05001564 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001565 |
David Tolnay8c91b882017-12-28 23:04:32 -05001566 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001567 |
David Tolnay8c91b882017-12-28 23:04:32 -05001568 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001569 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001570 call!(expr_closure, allow_struct)
1571 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001572 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001573 |
1574 // NOTE: This is the prefix-form of range
1575 call!(expr_range, allow_struct)
1576 |
David Tolnay8c91b882017-12-28 23:04:32 -05001577 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001578 |
David Tolnay8c91b882017-12-28 23:04:32 -05001579 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001580 ));
1581
Michael Layzell734adb42017-06-07 16:58:31 -04001582 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001583 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001584 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001585 |
David Tolnay9374bc02018-01-27 18:49:36 -08001586 syn!(ExprParen) => { Expr::Paren }
1587 |
David Tolnay8c91b882017-12-28 23:04:32 -05001588 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001589 ));
1590
Michael Layzell734adb42017-06-07 16:58:31 -04001591 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001592 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001593 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001594 |
David Tolnay8c91b882017-12-28 23:04:32 -05001595 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001596 |
David Tolnay8c91b882017-12-28 23:04:32 -05001597 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001598 |
David Tolnay8c91b882017-12-28 23:04:32 -05001599 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001600 |
David Tolnay8c91b882017-12-28 23:04:32 -05001601 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001602 |
David Tolnay8c91b882017-12-28 23:04:32 -05001603 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001604 |
David Tolnay8c91b882017-12-28 23:04:32 -05001605 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001606 |
David Tolnay8c91b882017-12-28 23:04:32 -05001607 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001608 |
David Tolnay8c91b882017-12-28 23:04:32 -05001609 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001610 |
David Tolnay8c91b882017-12-28 23:04:32 -05001611 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001612 |
David Tolnay8c91b882017-12-28 23:04:32 -05001613 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001614 ), Expr::from));
1615
David Tolnay8c91b882017-12-28 23:04:32 -05001616 impl Synom for ExprLit {
1617 named!(parse -> Self, do_parse!(
1618 lit: syn!(Lit) >>
1619 (ExprLit {
1620 attrs: Vec::new(),
1621 lit: lit,
1622 })
1623 ));
David Tolnay79777332018-01-07 10:04:42 -08001624
1625 fn description() -> Option<&'static str> {
1626 Some("literal")
1627 }
David Tolnay8c91b882017-12-28 23:04:32 -05001628 }
1629
1630 #[cfg(feature = "full")]
1631 impl Synom for ExprMacro {
1632 named!(parse -> Self, do_parse!(
1633 mac: syn!(Macro) >>
1634 (ExprMacro {
1635 attrs: Vec::new(),
1636 mac: mac,
1637 })
1638 ));
David Tolnay79777332018-01-07 10:04:42 -08001639
1640 fn description() -> Option<&'static str> {
1641 Some("macro invocation expression")
1642 }
David Tolnay8c91b882017-12-28 23:04:32 -05001643 }
1644
David Tolnaye98775f2017-12-28 23:17:00 -05001645 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001646 impl Synom for ExprGroup {
1647 named!(parse -> Self, do_parse!(
1648 e: grouped!(syn!(Expr)) >>
1649 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001650 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001651 expr: Box::new(e.1),
1652 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001653 })
Michael Layzell93c36282017-06-04 20:43:14 -04001654 ));
David Tolnay79777332018-01-07 10:04:42 -08001655
1656 fn description() -> Option<&'static str> {
1657 Some("expression surrounded by invisible delimiters")
1658 }
Michael Layzell93c36282017-06-04 20:43:14 -04001659 }
1660
Alex Crichton954046c2017-05-30 21:49:42 -07001661 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001662 named!(parse -> Self, do_parse!(
1663 e: parens!(syn!(Expr)) >>
1664 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001665 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001666 paren_token: e.0,
1667 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001668 })
Michael Layzell92639a52017-06-01 00:07:44 -04001669 ));
David Tolnay79777332018-01-07 10:04:42 -08001670
1671 fn description() -> Option<&'static str> {
1672 Some("parenthesized expression")
1673 }
Alex Crichton954046c2017-05-30 21:49:42 -07001674 }
David Tolnay89e05672016-10-02 14:39:42 -07001675
Michael Layzell734adb42017-06-07 16:58:31 -04001676 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001677 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001678 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001679 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001680 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001681 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001682 bracket_token: elems.0,
1683 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001684 })
1685 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001686
1687 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001688 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001689 }
Alex Crichton954046c2017-05-30 21:49:42 -07001690 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001691
David Tolnayf2cfd722017-12-31 18:02:51 -05001692 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1693 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001694
Michael Layzell734adb42017-06-07 16:58:31 -04001695 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001696 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001697 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001698 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001699 turbofish: option!(tuple!(
1700 punct!(::),
1701 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001702 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001703 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001704 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001705 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001706 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001707 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001708 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001709 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001710 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1711 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001712 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001713
Alex Crichton954046c2017-05-30 21:49:42 -07001714 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001715 turbofish: turbofish.map(|fish| MethodTurbofish {
1716 colon2_token: fish.0,
1717 lt_token: fish.1,
1718 args: fish.2,
1719 gt_token: fish.3,
1720 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001721 args: args.1,
1722 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001723 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001724 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001725 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001726 ));
1727
Michael Layzell734adb42017-06-07 16:58:31 -04001728 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001729 impl Synom for GenericMethodArgument {
1730 // TODO parse const generics as well
1731 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001732
1733 fn description() -> Option<&'static str> {
1734 Some("generic method argument")
1735 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001736 }
1737
1738 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001739 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001740 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001741 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001742 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001743 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001744 elems: elems.1,
1745 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001746 })
1747 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001748
1749 fn description() -> Option<&'static str> {
1750 Some("tuple")
1751 }
Alex Crichton954046c2017-05-30 21:49:42 -07001752 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001753
Michael Layzell734adb42017-06-07 16:58:31 -04001754 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001755 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001756 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001757 if_: keyword!(if) >>
1758 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001759 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001760 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001761 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001762 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001763 else_block: option!(else_block) >>
1764 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001765 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001766 pat: Box::new(pat),
1767 let_token: let_,
1768 eq_token: eq,
1769 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001770 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001771 brace_token: then_block.0,
1772 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001773 },
1774 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001775 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001776 })
1777 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001778
1779 fn description() -> Option<&'static str> {
1780 Some("`if let` expression")
1781 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001782 }
1783
Michael Layzell734adb42017-06-07 16:58:31 -04001784 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001785 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001786 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001787 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001788 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001789 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001790 else_block: option!(else_block) >>
1791 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001792 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001793 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001794 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001795 brace_token: then_block.0,
1796 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001797 },
1798 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001799 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001800 })
1801 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001802
1803 fn description() -> Option<&'static str> {
1804 Some("`if` expression")
1805 }
Alex Crichton954046c2017-05-30 21:49:42 -07001806 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001807
Michael Layzell734adb42017-06-07 16:58:31 -04001808 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001809 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001810 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001811 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001812 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001813 |
David Tolnay8c91b882017-12-28 23:04:32 -05001814 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001815 |
1816 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001817 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001818 (Expr::Block(ExprBlock {
1819 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001820 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001821 brace_token: else_block.0,
1822 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001823 },
1824 }))
David Tolnay939766a2016-09-23 23:48:12 -07001825 )
Alex Crichton954046c2017-05-30 21:49:42 -07001826 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001827 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001828 ));
1829
Michael Layzell734adb42017-06-07 16:58:31 -04001830 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001831 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001832 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001833 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001834 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001835 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001836 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001837 expr: expr_no_struct >>
1838 loop_block: syn!(Block) >>
1839 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001840 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001841 for_token: for_,
1842 in_token: in_,
1843 pat: Box::new(pat),
1844 expr: Box::new(expr),
1845 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001846 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001847 })
1848 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001849
1850 fn description() -> Option<&'static str> {
1851 Some("`for` loop")
1852 }
Alex Crichton954046c2017-05-30 21:49:42 -07001853 }
Gregory Katze5f35682016-09-27 14:20:55 -04001854
Michael Layzell734adb42017-06-07 16:58:31 -04001855 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001856 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001857 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001858 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001859 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001860 loop_block: syn!(Block) >>
1861 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001862 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001863 loop_token: loop_,
1864 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001865 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001866 })
1867 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001868
1869 fn description() -> Option<&'static str> {
1870 Some("`loop`")
1871 }
Alex Crichton954046c2017-05-30 21:49:42 -07001872 }
1873
Michael Layzell734adb42017-06-07 16:58:31 -04001874 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001875 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001876 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001877 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001878 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001879 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001880 (ExprMatch {
1881 attrs: Vec::new(),
1882 expr: Box::new(obj),
1883 match_token: match_,
1884 brace_token: res.0,
1885 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001886 })
1887 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001888
1889 fn description() -> Option<&'static str> {
1890 Some("`match` expression")
1891 }
Alex Crichton954046c2017-05-30 21:49:42 -07001892 }
David Tolnay1978c672016-10-27 22:05:52 -07001893
Michael Layzell734adb42017-06-07 16:58:31 -04001894 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001895 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001896 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001897 do_: keyword!(do) >>
1898 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001899 catch_block: syn!(Block) >>
1900 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001901 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001902 block: catch_block,
1903 do_token: do_,
1904 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001905 })
Michael Layzell92639a52017-06-01 00:07:44 -04001906 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001907
1908 fn description() -> Option<&'static str> {
1909 Some("`catch` expression")
1910 }
Alex Crichton954046c2017-05-30 21:49:42 -07001911 }
Arnavion02ef13f2017-04-25 00:54:31 -07001912
Michael Layzell734adb42017-06-07 16:58:31 -04001913 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001914 impl Synom for ExprYield {
1915 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001916 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001917 expr: option!(syn!(Expr)) >>
1918 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001919 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001920 yield_token: yield_,
1921 expr: expr.map(Box::new),
1922 })
1923 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001924
1925 fn description() -> Option<&'static str> {
1926 Some("`yield` expression")
1927 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001928 }
1929
1930 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001931 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001932 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001933 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001934 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001935 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1936 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001937 body: do_parse!(
1938 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001939 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1940 true => alt!(
1941 input_end!() => { |_| None }
1942 |
1943 punct!(,) => { Some }
1944 )
Alex Crichton03b30272017-08-28 09:35:24 -07001945 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001946 false => option!(punct!(,))
1947 ) >>
1948 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001949 ) >>
1950 (Arm {
1951 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001952 attrs: attrs,
1953 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001954 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001955 body: Box::new(body.0),
1956 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001957 })
1958 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001959
1960 fn description() -> Option<&'static str> {
1961 Some("`match` arm")
1962 }
Alex Crichton954046c2017-05-30 21:49:42 -07001963 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001964
Michael Layzell734adb42017-06-07 16:58:31 -04001965 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001966 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001967 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001968 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001969 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001970 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001971 ret_and_body: alt!(
1972 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001973 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001974 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001975 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001976 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001977 Expr::Block(ExprBlock {
1978 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001979 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001980 }))
David Tolnay89e05672016-10-02 14:39:42 -07001981 )
1982 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001983 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001984 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001985 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001986 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001987 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001988 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001989 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001990 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001991 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001992 body: Box::new(ret_and_body.1),
1993 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001994 ));
1995
Michael Layzell734adb42017-06-07 16:58:31 -04001996 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001997 named!(fn_arg -> FnArg, do_parse!(
1998 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001999 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002000 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002001 if let Some((colon, ty)) = ty {
2002 FnArg::Captured(ArgCaptured {
2003 pat: pat,
2004 colon_token: colon,
2005 ty: ty,
2006 })
2007 } else {
2008 FnArg::Inferred(pat)
2009 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002010 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002011 ));
2012
Michael Layzell734adb42017-06-07 16:58:31 -04002013 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002014 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002015 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05002016 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002017 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002018 cond: expr_no_struct >>
2019 while_block: syn!(Block) >>
2020 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05002021 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002022 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002023 cond: Box::new(cond),
2024 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05002025 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002026 })
2027 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002028
2029 fn description() -> Option<&'static str> {
2030 Some("`while` expression")
2031 }
Alex Crichton954046c2017-05-30 21:49:42 -07002032 }
2033
Michael Layzell734adb42017-06-07 16:58:31 -04002034 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002035 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002036 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05002037 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002038 while_: keyword!(while) >>
2039 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002040 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002041 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002042 value: expr_no_struct >>
2043 while_block: syn!(Block) >>
2044 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05002045 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002046 eq_token: eq,
2047 let_token: let_,
2048 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002049 pat: Box::new(pat),
2050 expr: Box::new(value),
2051 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05002052 label: label,
2053 })
2054 ));
David Tolnay79777332018-01-07 10:04:42 -08002055
2056 fn description() -> Option<&'static str> {
2057 Some("`while let` expression")
2058 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002059 }
2060
2061 #[cfg(feature = "full")]
2062 impl Synom for Label {
2063 named!(parse -> Self, do_parse!(
2064 name: syn!(Lifetime) >>
2065 colon: punct!(:) >>
2066 (Label {
2067 name: name,
2068 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002069 })
2070 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002071
2072 fn description() -> Option<&'static str> {
2073 Some("`while let` expression")
2074 }
Alex Crichton954046c2017-05-30 21:49:42 -07002075 }
2076
Michael Layzell734adb42017-06-07 16:58:31 -04002077 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002078 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002079 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002080 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002081 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002082 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05002083 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002084 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002085 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002086 })
2087 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002088
2089 fn description() -> Option<&'static str> {
2090 Some("`continue`")
2091 }
Alex Crichton954046c2017-05-30 21:49:42 -07002092 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002093
Michael Layzell734adb42017-06-07 16:58:31 -04002094 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002095 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002096 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002097 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002098 // We can't allow blocks after a `break` expression when we wouldn't
2099 // allow structs, as this expression is ambiguous.
2100 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002101 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05002102 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05002103 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002104 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002105 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002106 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002107 ));
2108
Michael Layzell734adb42017-06-07 16:58:31 -04002109 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002110 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002111 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002112 // NOTE: return is greedy and eats blocks after it even when in a
2113 // position where structs are not allowed, such as in if statement
2114 // conditions. For example:
2115 //
David Tolnaybcf26022017-12-25 22:10:52 -05002116 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002117 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002118 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05002119 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002120 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002121 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002122 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002123 ));
2124
Michael Layzell734adb42017-06-07 16:58:31 -04002125 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002126 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002127 named!(parse -> Self, do_parse!(
2128 path: syn!(Path) >>
2129 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002130 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002131 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2132 dots: punct!(..) >>
2133 base: syn!(Expr) >>
2134 (dots, base)
2135 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002136 (fields, base)
2137 )) >>
2138 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002139 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002140 let (dots, rest) = match base.and_then(|b| b) {
2141 Some((dots, base)) => (Some(dots), Some(base)),
2142 None => (None, None),
2143 };
2144 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05002145 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002146 brace_token: brace,
2147 path: path,
2148 fields: fields,
2149 dot2_token: dots,
2150 rest: rest.map(Box::new),
2151 }
2152 })
2153 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002154
2155 fn description() -> Option<&'static str> {
2156 Some("struct literal expression")
2157 }
Alex Crichton954046c2017-05-30 21:49:42 -07002158 }
2159
Michael Layzell734adb42017-06-07 16:58:31 -04002160 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002161 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002162 named!(parse -> Self, do_parse!(
2163 attrs: many0!(Attribute::parse_outer) >>
2164 field_value: alt!(
2165 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2166 |
2167 map!(syn!(Ident), |name| (
2168 Member::Named(name),
2169 None,
2170 Expr::Path(ExprPath {
2171 attrs: Vec::new(),
2172 qself: None,
2173 path: name.into(),
2174 }),
2175 ))
2176 ) >>
2177 (FieldValue {
2178 attrs: attrs,
2179 member: field_value.0,
2180 colon_token: field_value.1,
2181 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002182 })
2183 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002184
2185 fn description() -> Option<&'static str> {
2186 Some("field-value pair: `field: value`")
2187 }
Alex Crichton954046c2017-05-30 21:49:42 -07002188 }
David Tolnay055a7042016-10-02 19:23:54 -07002189
Michael Layzell734adb42017-06-07 16:58:31 -04002190 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002191 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002192 named!(parse -> Self, do_parse!(
2193 data: brackets!(do_parse!(
2194 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002195 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002196 times: syn!(Expr) >>
2197 (value, semi, times)
2198 )) >>
2199 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05002200 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05002201 expr: Box::new((data.1).0),
David Tolnay84d80442018-01-07 01:03:20 -08002202 len: Box::new((data.1).2),
David Tolnay8875fca2017-12-31 13:52:37 -05002203 bracket_token: data.0,
2204 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002205 })
2206 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002207
2208 fn description() -> Option<&'static str> {
2209 Some("repeated array literal: `[val; N]`")
2210 }
Alex Crichton954046c2017-05-30 21:49:42 -07002211 }
David Tolnay055a7042016-10-02 19:23:54 -07002212
Michael Layzell734adb42017-06-07 16:58:31 -04002213 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002214 impl Synom for ExprUnsafe {
2215 named!(parse -> Self, do_parse!(
2216 unsafe_: keyword!(unsafe) >>
2217 b: syn!(Block) >>
2218 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002219 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002220 unsafe_token: unsafe_,
2221 block: b,
2222 })
2223 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002224
2225 fn description() -> Option<&'static str> {
2226 Some("unsafe block: `unsafe { .. }`")
2227 }
Nika Layzell640832a2017-12-04 13:37:09 -05002228 }
2229
2230 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002231 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002232 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04002233 b: syn!(Block) >>
2234 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002235 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002236 block: b,
2237 })
2238 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002239
2240 fn description() -> Option<&'static str> {
2241 Some("block: `{ .. }`")
2242 }
Alex Crichton954046c2017-05-30 21:49:42 -07002243 }
David Tolnay89e05672016-10-02 14:39:42 -07002244
Michael Layzell734adb42017-06-07 16:58:31 -04002245 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002246 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002247 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002248 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002249 (ExprRange {
2250 attrs: Vec::new(),
2251 from: None,
2252 to: hi.map(Box::new),
2253 limits: limits,
2254 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002255 ));
2256
Michael Layzell734adb42017-06-07 16:58:31 -04002257 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002258 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002259 named!(parse -> Self, alt!(
2260 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002261 punct!(..=) => { RangeLimits::Closed }
2262 |
2263 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002264 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002265 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002266 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002267 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002268
2269 fn description() -> Option<&'static str> {
2270 Some("range limit: `..`, `...` or `..=`")
2271 }
Alex Crichton954046c2017-05-30 21:49:42 -07002272 }
David Tolnay438c9052016-10-07 23:24:48 -07002273
Alex Crichton954046c2017-05-30 21:49:42 -07002274 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002275 named!(parse -> Self, do_parse!(
2276 pair: qpath >>
2277 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002278 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002279 qself: pair.0,
2280 path: pair.1,
2281 })
2282 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002283
2284 fn description() -> Option<&'static str> {
2285 Some("path: `a::b::c`")
2286 }
Alex Crichton954046c2017-05-30 21:49:42 -07002287 }
David Tolnay42602292016-10-01 22:25:45 -07002288
Michael Layzell734adb42017-06-07 16:58:31 -04002289 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002290 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002291
David Tolnay8875fca2017-12-31 13:52:37 -05002292 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002293
Michael Layzell734adb42017-06-07 16:58:31 -04002294 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002295 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002296 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002297 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002298 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002299 brace_token: stmts.0,
2300 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002301 })
2302 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002303
2304 fn description() -> Option<&'static str> {
2305 Some("block: `{ .. }`")
2306 }
Alex Crichton954046c2017-05-30 21:49:42 -07002307 }
David Tolnay939766a2016-09-23 23:48:12 -07002308
Michael Layzell734adb42017-06-07 16:58:31 -04002309 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002310 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002311 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002312 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002313 mut standalone: many0!(do_parse!(
2314 stmt: syn!(Stmt) >>
2315 many0!(punct!(;)) >>
2316 (stmt)
2317 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002318 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002319 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002320 mut e: syn!(Expr) >>
2321 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002322 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002323 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002324 })
2325 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002326 (match last {
2327 None => standalone,
2328 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002329 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002330 standalone
2331 }
2332 })
2333 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002334 }
2335
Michael Layzell734adb42017-06-07 16:58:31 -04002336 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002337 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002338 named!(parse -> Self, alt!(
2339 stmt_mac
2340 |
2341 stmt_local
2342 |
2343 stmt_item
2344 |
Michael Layzell35418782017-06-07 09:20:25 -04002345 stmt_blockexpr
2346 |
Michael Layzell92639a52017-06-01 00:07:44 -04002347 stmt_expr
2348 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002349
2350 fn description() -> Option<&'static str> {
2351 Some("statement")
2352 }
Alex Crichton954046c2017-05-30 21:49:42 -07002353 }
David Tolnay939766a2016-09-23 23:48:12 -07002354
Michael Layzell734adb42017-06-07 16:58:31 -04002355 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002356 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002357 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002358 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002359 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002360 // Only parse braces here; paren and bracket will get parsed as
2361 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002362 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002363 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002364 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002365 attrs: attrs,
2366 ident: None,
2367 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002368 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002369 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002370 delimiter: MacroDelimiter::Brace(data.0),
2371 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002372 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002373 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002374 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002375 ));
2376
Michael Layzell734adb42017-06-07 16:58:31 -04002377 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002378 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002379 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002380 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002381 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002382 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002383 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2384 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002385 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002386 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002387 let_token: let_,
2388 pat: Box::new(pat),
2389 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2390 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2391 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002392 }))
David Tolnay191e0582016-10-02 18:31:09 -07002393 ));
2394
Michael Layzell734adb42017-06-07 16:58:31 -04002395 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002396 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002397
Michael Layzell734adb42017-06-07 16:58:31 -04002398 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002399 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002400 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002401 mut e: expr_nosemi >>
2402 // If the next token is a `.` or a `?` it is special-cased to parse as
2403 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002404 not!(punct!(.)) >>
2405 not!(punct!(?)) >>
2406 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002407 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002408 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002409 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002410 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002411 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002412 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002413 }
2414 })
2415 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002416
Michael Layzell734adb42017-06-07 16:58:31 -04002417 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002418 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002419 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002420 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002421 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002422 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002423 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002424 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002425 })
David Tolnay939766a2016-09-23 23:48:12 -07002426 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002427
Michael Layzell734adb42017-06-07 16:58:31 -04002428 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002429 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002430 named!(parse -> Self, alt!(
2431 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2432 |
2433 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2434 |
2435 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2436 |
2437 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2438 |
2439 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2440 |
David Tolnay323279a2017-12-29 11:26:32 -05002441 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002442 |
2443 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2444 |
2445 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2446 |
2447 syn!(PatPath) => { Pat::Path }
2448 |
2449 syn!(PatTuple) => { Pat::Tuple }
2450 |
2451 syn!(PatRef) => { Pat::Ref }
2452 |
2453 syn!(PatSlice) => { Pat::Slice }
2454 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002455
2456 fn description() -> Option<&'static str> {
2457 Some("pattern")
2458 }
Alex Crichton954046c2017-05-30 21:49:42 -07002459 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002460
Michael Layzell734adb42017-06-07 16:58:31 -04002461 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002462 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002463 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002464 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002465 |u| PatWild { underscore_token: u }
2466 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002467
2468 fn description() -> Option<&'static str> {
2469 Some("wild pattern: `_`")
2470 }
Alex Crichton954046c2017-05-30 21:49:42 -07002471 }
David Tolnay84aa0752016-10-02 23:01:13 -07002472
Michael Layzell734adb42017-06-07 16:58:31 -04002473 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002474 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002475 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002476 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002477 pat: syn!(Pat) >>
2478 (PatBox {
2479 pat: Box::new(pat),
2480 box_token: boxed,
2481 })
2482 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002483
2484 fn description() -> Option<&'static str> {
2485 Some("box pattern")
2486 }
Alex Crichton954046c2017-05-30 21:49:42 -07002487 }
2488
Michael Layzell734adb42017-06-07 16:58:31 -04002489 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002490 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002491 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002492 by_ref: option!(keyword!(ref)) >>
2493 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002494 name: alt!(
2495 syn!(Ident)
2496 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002497 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002498 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002499 not!(punct!(<)) >>
2500 not!(punct!(::)) >>
2501 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002502 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002503 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002504 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002505 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002506 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002507 })
2508 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002509
2510 fn description() -> Option<&'static str> {
2511 Some("pattern identifier binding")
2512 }
Alex Crichton954046c2017-05-30 21:49:42 -07002513 }
2514
Michael Layzell734adb42017-06-07 16:58:31 -04002515 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002516 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002517 named!(parse -> Self, do_parse!(
2518 path: syn!(Path) >>
2519 tuple: syn!(PatTuple) >>
2520 (PatTupleStruct {
2521 path: path,
2522 pat: tuple,
2523 })
2524 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002525
2526 fn description() -> Option<&'static str> {
2527 Some("tuple struct pattern")
2528 }
Alex Crichton954046c2017-05-30 21:49:42 -07002529 }
2530
Michael Layzell734adb42017-06-07 16:58:31 -04002531 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002532 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002533 named!(parse -> Self, do_parse!(
2534 path: syn!(Path) >>
2535 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002536 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002537 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002538 (fields, base)
2539 )) >>
2540 (PatStruct {
2541 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002542 fields: (data.1).0,
2543 brace_token: data.0,
2544 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002545 })
2546 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002547
2548 fn description() -> Option<&'static str> {
2549 Some("struct pattern")
2550 }
Alex Crichton954046c2017-05-30 21:49:42 -07002551 }
2552
Michael Layzell734adb42017-06-07 16:58:31 -04002553 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002554 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002555 named!(parse -> Self, alt!(
2556 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002557 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002558 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002559 pat: syn!(Pat) >>
2560 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002561 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002562 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002563 attrs: Vec::new(),
2564 colon_token: Some(colon),
2565 })
2566 )
2567 |
2568 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002569 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002570 by_ref: option!(keyword!(ref)) >>
2571 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002572 ident: syn!(Ident) >>
2573 ({
2574 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002575 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002576 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002577 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002578 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002579 }.into();
2580 if let Some(boxed) = boxed {
2581 pat = PatBox {
2582 pat: Box::new(pat),
2583 box_token: boxed,
2584 }.into();
2585 }
2586 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002587 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002588 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002589 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002590 colon_token: None,
2591 }
2592 })
2593 )
2594 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002595
2596 fn description() -> Option<&'static str> {
2597 Some("field pattern")
2598 }
Alex Crichton954046c2017-05-30 21:49:42 -07002599 }
2600
Michael Layzell734adb42017-06-07 16:58:31 -04002601 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002602 impl Synom for Member {
2603 named!(parse -> Self, alt!(
2604 syn!(Ident) => { Member::Named }
2605 |
2606 syn!(Index) => { Member::Unnamed }
2607 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002608
2609 fn description() -> Option<&'static str> {
2610 Some("field member")
2611 }
David Tolnay85b69a42017-12-27 20:43:10 -05002612 }
2613
2614 #[cfg(feature = "full")]
2615 impl Synom for Index {
2616 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002617 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002618 ({
David Tolnay360efd22018-01-04 23:35:26 -08002619 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002620 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002621 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002622 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002623 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002624 })
David Tolnay85b69a42017-12-27 20:43:10 -05002625 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002626
2627 fn description() -> Option<&'static str> {
2628 Some("field index")
2629 }
David Tolnay85b69a42017-12-27 20:43:10 -05002630 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002631
Michael Layzell734adb42017-06-07 16:58:31 -04002632 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002633 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002634 named!(parse -> Self, map!(
2635 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002636 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002637 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002638
2639 fn description() -> Option<&'static str> {
2640 Some("path pattern")
2641 }
Alex Crichton954046c2017-05-30 21:49:42 -07002642 }
David Tolnay9636c052016-10-02 17:11:17 -07002643
Michael Layzell734adb42017-06-07 16:58:31 -04002644 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002645 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002646 named!(parse -> Self, do_parse!(
2647 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002648 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002649 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2650 tuple!(punct!(..), option!(punct!(,)))
2651 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002652 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002653 Some((_, Some(_))) => true,
2654 _ => false,
2655 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002656 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002657 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002658 )) >>
2659 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002660 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002661 let (dotdot, trailing) = match dotdot {
2662 Some((a, b)) => (Some(a), Some(b)),
2663 None => (None, None),
2664 };
2665 PatTuple {
2666 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002667 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002668 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002669 comma_token: trailing.unwrap_or_default(),
2670 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002671 }
2672 })
2673 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002674
2675 fn description() -> Option<&'static str> {
2676 Some("tuple pattern")
2677 }
Alex Crichton954046c2017-05-30 21:49:42 -07002678 }
David Tolnayfbb73232016-10-03 01:00:06 -07002679
Michael Layzell734adb42017-06-07 16:58:31 -04002680 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002681 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002682 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002683 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002684 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002685 pat: syn!(Pat) >>
2686 (PatRef {
2687 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002688 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002689 and_token: and,
2690 })
2691 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002692
2693 fn description() -> Option<&'static str> {
2694 Some("reference pattern")
2695 }
Alex Crichton954046c2017-05-30 21:49:42 -07002696 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002697
Michael Layzell734adb42017-06-07 16:58:31 -04002698 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002699 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002700 named!(parse -> Self, do_parse!(
2701 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002702 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002703 return parse_error(); // these need to be parsed by pat_path
2704 } else {
2705 PatLit {
2706 expr: Box::new(lit),
2707 }
2708 })
2709 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002710
2711 fn description() -> Option<&'static str> {
2712 Some("literal pattern")
2713 }
Alex Crichton954046c2017-05-30 21:49:42 -07002714 }
David Tolnaye1310902016-10-29 23:40:00 -07002715
Michael Layzell734adb42017-06-07 16:58:31 -04002716 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002717 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002718 named!(parse -> Self, do_parse!(
2719 lo: pat_lit_expr >>
2720 limits: syn!(RangeLimits) >>
2721 hi: pat_lit_expr >>
2722 (PatRange {
2723 lo: Box::new(lo),
2724 hi: Box::new(hi),
2725 limits: limits,
2726 })
2727 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002728
2729 fn description() -> Option<&'static str> {
2730 Some("range pattern")
2731 }
Alex Crichton954046c2017-05-30 21:49:42 -07002732 }
David Tolnaye1310902016-10-29 23:40:00 -07002733
Michael Layzell734adb42017-06-07 16:58:31 -04002734 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002735 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002736 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002737 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002738 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002739 |
David Tolnay8c91b882017-12-28 23:04:32 -05002740 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002741 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002742 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002743 Expr::Unary(ExprUnary {
2744 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002745 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002746 expr: Box::new(v)
2747 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002748 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002749 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002750 })
2751 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002752
Michael Layzell734adb42017-06-07 16:58:31 -04002753 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002754 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002755 named!(parse -> Self, map!(
2756 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002757 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002758 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002759 dots: punct!(..) >>
2760 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002761 (dots, trailing)
2762 )) >>
2763 after: cond!(
2764 match middle {
2765 Some((_, ref trailing)) => trailing.is_some(),
2766 _ => false,
2767 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002768 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002769 ) >>
2770 (before, middle, after)
2771 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002772 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002773 let mut before: Punctuated<Pat, Token![,]> = before;
2774 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002775 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002776 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002777 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002778 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002779 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002780 }),
2781 bracket_token: brackets,
2782 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002783 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002784 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002785 } else {
David Tolnay56080682018-01-06 14:01:52 -08002786 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002787 }
2788 }),
2789 front: before,
2790 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002791 }
Alex Crichton954046c2017-05-30 21:49:42 -07002792 }
Michael Layzell92639a52017-06-01 00:07:44 -04002793 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002794
2795 fn description() -> Option<&'static str> {
2796 Some("slice pattern")
2797 }
Alex Crichton954046c2017-05-30 21:49:42 -07002798 }
David Tolnay323279a2017-12-29 11:26:32 -05002799
2800 #[cfg(feature = "full")]
2801 impl Synom for PatMacro {
2802 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002803
2804 fn description() -> Option<&'static str> {
2805 Some("macro pattern")
2806 }
David Tolnay323279a2017-12-29 11:26:32 -05002807 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002808}
2809
David Tolnayf4bbbd92016-09-23 14:41:55 -07002810#[cfg(feature = "printing")]
2811mod printing {
2812 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002813 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002814 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002815 use quote::{ToTokens, Tokens};
Alex Crichton9a4dca22018-03-28 06:32:19 -07002816 use proc_macro2::Literal;
David Tolnayf4bbbd92016-09-23 14:41:55 -07002817
David Tolnaybcf26022017-12-25 22:10:52 -05002818 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2819 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002820 #[cfg(feature = "full")]
2821 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002822 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002823 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002824 e.to_tokens(tokens);
2825 });
2826 } else {
2827 e.to_tokens(tokens);
2828 }
2829 }
2830
David Tolnay8c91b882017-12-28 23:04:32 -05002831 #[cfg(feature = "full")]
2832 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2833 tokens.append_all(attrs.outer());
2834 }
Michael Layzell734adb42017-06-07 16:58:31 -04002835
David Tolnay8c91b882017-12-28 23:04:32 -05002836 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002837 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002838
Michael Layzell734adb42017-06-07 16:58:31 -04002839 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002840 impl ToTokens for ExprBox {
2841 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002842 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002843 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002844 self.expr.to_tokens(tokens);
2845 }
2846 }
2847
Michael Layzell734adb42017-06-07 16:58:31 -04002848 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002849 impl ToTokens for ExprInPlace {
2850 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002851 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002852 self.place.to_tokens(tokens);
2853 self.arrow_token.to_tokens(tokens);
2854 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002855 }
2856 }
2857
Michael Layzell734adb42017-06-07 16:58:31 -04002858 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002859 impl ToTokens for ExprArray {
2860 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002861 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002862 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002863 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002864 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002865 }
2866 }
2867
2868 impl ToTokens for ExprCall {
2869 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002870 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002871 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002872 self.paren_token.surround(tokens, |tokens| {
2873 self.args.to_tokens(tokens);
2874 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002875 }
2876 }
2877
Michael Layzell734adb42017-06-07 16:58:31 -04002878 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002879 impl ToTokens for ExprMethodCall {
2880 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002881 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002882 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002883 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002884 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002885 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002886 self.paren_token.surround(tokens, |tokens| {
2887 self.args.to_tokens(tokens);
2888 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002889 }
2890 }
2891
Michael Layzell734adb42017-06-07 16:58:31 -04002892 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002893 impl ToTokens for MethodTurbofish {
2894 fn to_tokens(&self, tokens: &mut Tokens) {
2895 self.colon2_token.to_tokens(tokens);
2896 self.lt_token.to_tokens(tokens);
2897 self.args.to_tokens(tokens);
2898 self.gt_token.to_tokens(tokens);
2899 }
2900 }
2901
2902 #[cfg(feature = "full")]
2903 impl ToTokens for GenericMethodArgument {
2904 fn to_tokens(&self, tokens: &mut Tokens) {
2905 match *self {
2906 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2907 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2908 }
2909 }
2910 }
2911
2912 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002913 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002914 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002915 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002916 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002917 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002918 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002919 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002920 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002921 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002922 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002923 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002924 }
2925 }
2926
2927 impl ToTokens for ExprBinary {
2928 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002929 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002930 self.left.to_tokens(tokens);
2931 self.op.to_tokens(tokens);
2932 self.right.to_tokens(tokens);
2933 }
2934 }
2935
2936 impl ToTokens for ExprUnary {
2937 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002938 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002939 self.op.to_tokens(tokens);
2940 self.expr.to_tokens(tokens);
2941 }
2942 }
2943
David Tolnay8c91b882017-12-28 23:04:32 -05002944 impl ToTokens for ExprLit {
2945 fn to_tokens(&self, tokens: &mut Tokens) {
2946 attrs_to_tokens(&self.attrs, tokens);
2947 self.lit.to_tokens(tokens);
2948 }
2949 }
2950
Alex Crichton62a0a592017-05-22 13:58:53 -07002951 impl ToTokens for ExprCast {
2952 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002953 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002954 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002955 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002956 self.ty.to_tokens(tokens);
2957 }
2958 }
2959
David Tolnay0cf94f22017-12-28 23:46:26 -05002960 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002961 impl ToTokens for ExprType {
2962 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002963 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002964 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002965 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002966 self.ty.to_tokens(tokens);
2967 }
2968 }
2969
Michael Layzell734adb42017-06-07 16:58:31 -04002970 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002971 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002972 if let Some((ref else_token, ref else_)) = *else_ {
2973 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002974
2975 // If we are not one of the valid expressions to exist in an else
2976 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002977 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002978 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002979 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002980 }
2981 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002982 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002983 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002984 });
2985 }
2986 }
2987 }
2988 }
2989
2990 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002991 impl ToTokens for ExprIf {
2992 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002993 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002994 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002995 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002996 self.then_branch.to_tokens(tokens);
2997 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002998 }
2999 }
3000
Michael Layzell734adb42017-06-07 16:58:31 -04003001 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003002 impl ToTokens for ExprIfLet {
3003 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003004 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003005 self.if_token.to_tokens(tokens);
3006 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003007 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003008 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003009 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003010 self.then_branch.to_tokens(tokens);
3011 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003012 }
3013 }
3014
Michael Layzell734adb42017-06-07 16:58:31 -04003015 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003016 impl ToTokens for ExprWhile {
3017 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003018 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003019 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003020 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003021 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07003022 self.body.to_tokens(tokens);
3023 }
3024 }
3025
Michael Layzell734adb42017-06-07 16:58:31 -04003026 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003027 impl ToTokens for ExprWhileLet {
3028 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003029 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003030 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003031 self.while_token.to_tokens(tokens);
3032 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003033 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003035 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07003036 self.body.to_tokens(tokens);
3037 }
3038 }
3039
Michael Layzell734adb42017-06-07 16:58:31 -04003040 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 impl ToTokens for ExprForLoop {
3042 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003043 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003044 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003045 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003046 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003047 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003048 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07003049 self.body.to_tokens(tokens);
3050 }
3051 }
3052
Michael Layzell734adb42017-06-07 16:58:31 -04003053 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003054 impl ToTokens for ExprLoop {
3055 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003056 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003057 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003058 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003059 self.body.to_tokens(tokens);
3060 }
3061 }
3062
Michael Layzell734adb42017-06-07 16:58:31 -04003063 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003064 impl ToTokens for ExprMatch {
3065 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003066 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003067 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003068 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003069 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05003070 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003071 arm.to_tokens(tokens);
3072 // Ensure that we have a comma after a non-block arm, except
3073 // for the last one.
3074 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003075 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003076 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003077 }
3078 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003079 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003080 }
3081 }
3082
Michael Layzell734adb42017-06-07 16:58:31 -04003083 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003084 impl ToTokens for ExprCatch {
3085 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003086 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003087 self.do_token.to_tokens(tokens);
3088 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003089 self.block.to_tokens(tokens);
3090 }
3091 }
3092
Michael Layzell734adb42017-06-07 16:58:31 -04003093 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003094 impl ToTokens for ExprYield {
3095 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003096 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07003097 self.yield_token.to_tokens(tokens);
3098 self.expr.to_tokens(tokens);
3099 }
3100 }
3101
3102 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003103 impl ToTokens for ExprClosure {
3104 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003105 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003106 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003107 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003108 for input in self.inputs.pairs() {
3109 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003110 FnArg::Captured(ArgCaptured {
3111 ref pat,
3112 ty: Type::Infer(_),
3113 ..
3114 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003115 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003116 }
David Tolnay56080682018-01-06 14:01:52 -08003117 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003118 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003119 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003120 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003121 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003122 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003123 self.body.to_tokens(tokens);
3124 }
3125 }
3126
Michael Layzell734adb42017-06-07 16:58:31 -04003127 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003128 impl ToTokens for ExprUnsafe {
3129 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003130 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05003131 self.unsafe_token.to_tokens(tokens);
3132 self.block.to_tokens(tokens);
3133 }
3134 }
3135
3136 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003137 impl ToTokens for ExprBlock {
3138 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003139 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003140 self.block.to_tokens(tokens);
3141 }
3142 }
3143
Michael Layzell734adb42017-06-07 16:58:31 -04003144 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003145 impl ToTokens for ExprAssign {
3146 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003147 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003148 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003149 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003150 self.right.to_tokens(tokens);
3151 }
3152 }
3153
Michael Layzell734adb42017-06-07 16:58:31 -04003154 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003155 impl ToTokens for ExprAssignOp {
3156 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003157 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003158 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003159 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003160 self.right.to_tokens(tokens);
3161 }
3162 }
3163
Michael Layzell734adb42017-06-07 16:58:31 -04003164 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003165 impl ToTokens for ExprField {
3166 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003167 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003168 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003169 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003170 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003171 }
3172 }
3173
David Tolnay85b69a42017-12-27 20:43:10 -05003174 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07003175 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003176 match *self {
3177 Member::Named(ident) => ident.to_tokens(tokens),
3178 Member::Unnamed(ref index) => index.to_tokens(tokens),
3179 }
3180 }
3181 }
3182
David Tolnay85b69a42017-12-27 20:43:10 -05003183 impl ToTokens for Index {
3184 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003185 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3186 lit.set_span(self.span);
3187 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003188 }
3189 }
3190
3191 impl ToTokens for ExprIndex {
3192 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003193 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003194 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003195 self.bracket_token.surround(tokens, |tokens| {
3196 self.index.to_tokens(tokens);
3197 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003198 }
3199 }
3200
Michael Layzell734adb42017-06-07 16:58:31 -04003201 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003202 impl ToTokens for ExprRange {
3203 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003204 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003205 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003206 match self.limits {
3207 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3208 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3209 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003210 self.to.to_tokens(tokens);
3211 }
3212 }
3213
3214 impl ToTokens for ExprPath {
3215 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003216 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003217 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003218 }
3219 }
3220
Michael Layzell734adb42017-06-07 16:58:31 -04003221 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003222 impl ToTokens for ExprAddrOf {
3223 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003224 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003225 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003226 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003227 self.expr.to_tokens(tokens);
3228 }
3229 }
3230
Michael Layzell734adb42017-06-07 16:58:31 -04003231 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003232 impl ToTokens for ExprBreak {
3233 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003234 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003235 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003236 self.label.to_tokens(tokens);
3237 self.expr.to_tokens(tokens);
3238 }
3239 }
3240
Michael Layzell734adb42017-06-07 16:58:31 -04003241 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003242 impl ToTokens for ExprContinue {
3243 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003244 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003245 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003246 self.label.to_tokens(tokens);
3247 }
3248 }
3249
Michael Layzell734adb42017-06-07 16:58:31 -04003250 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003251 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003252 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003253 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003254 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003255 self.expr.to_tokens(tokens);
3256 }
3257 }
3258
Michael Layzell734adb42017-06-07 16:58:31 -04003259 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003260 impl ToTokens for ExprMacro {
3261 fn to_tokens(&self, tokens: &mut Tokens) {
3262 tokens.append_all(self.attrs.outer());
3263 self.mac.to_tokens(tokens);
3264 }
3265 }
3266
3267 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003268 impl ToTokens for ExprStruct {
3269 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003270 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003271 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003272 self.brace_token.surround(tokens, |tokens| {
3273 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003274 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003275 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003276 self.rest.to_tokens(tokens);
3277 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003278 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003279 }
3280 }
3281
Michael Layzell734adb42017-06-07 16:58:31 -04003282 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003283 impl ToTokens for ExprRepeat {
3284 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003285 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003286 self.bracket_token.surround(tokens, |tokens| {
3287 self.expr.to_tokens(tokens);
3288 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003289 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003290 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003291 }
3292 }
3293
David Tolnaye98775f2017-12-28 23:17:00 -05003294 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003295 impl ToTokens for ExprGroup {
3296 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003297 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003298 self.group_token.surround(tokens, |tokens| {
3299 self.expr.to_tokens(tokens);
3300 });
3301 }
3302 }
3303
Alex Crichton62a0a592017-05-22 13:58:53 -07003304 impl ToTokens for ExprParen {
3305 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003306 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003307 self.paren_token.surround(tokens, |tokens| {
3308 self.expr.to_tokens(tokens);
3309 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003310 }
3311 }
3312
Michael Layzell734adb42017-06-07 16:58:31 -04003313 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003314 impl ToTokens for ExprTry {
3315 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003316 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003317 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003318 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003319 }
3320 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003321
David Tolnay2ae520a2017-12-29 11:19:50 -05003322 impl ToTokens for ExprVerbatim {
3323 fn to_tokens(&self, tokens: &mut Tokens) {
3324 self.tts.to_tokens(tokens);
3325 }
3326 }
3327
Michael Layzell734adb42017-06-07 16:58:31 -04003328 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003329 impl ToTokens for Label {
3330 fn to_tokens(&self, tokens: &mut Tokens) {
3331 self.name.to_tokens(tokens);
3332 self.colon_token.to_tokens(tokens);
3333 }
3334 }
3335
3336 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003337 impl ToTokens for FieldValue {
3338 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc42b90a2018-01-18 23:11:37 -08003339 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003340 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003341 if let Some(ref colon_token) = self.colon_token {
3342 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003343 self.expr.to_tokens(tokens);
3344 }
David Tolnay055a7042016-10-02 19:23:54 -07003345 }
3346 }
3347
Michael Layzell734adb42017-06-07 16:58:31 -04003348 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003349 impl ToTokens for Arm {
3350 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003351 tokens.append_all(&self.attrs);
3352 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003353 if let Some((ref if_token, ref guard)) = self.guard {
3354 if_token.to_tokens(tokens);
3355 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003356 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003357 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003358 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003359 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003360 }
3361 }
3362
Michael Layzell734adb42017-06-07 16:58:31 -04003363 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003364 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003365 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003366 self.underscore_token.to_tokens(tokens);
3367 }
3368 }
3369
Michael Layzell734adb42017-06-07 16:58:31 -04003370 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003371 impl ToTokens for PatIdent {
3372 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003373 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003374 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003375 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003376 if let Some((ref at_token, ref subpat)) = self.subpat {
3377 at_token.to_tokens(tokens);
3378 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003379 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003380 }
3381 }
3382
Michael Layzell734adb42017-06-07 16:58:31 -04003383 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003384 impl ToTokens for PatStruct {
3385 fn to_tokens(&self, tokens: &mut Tokens) {
3386 self.path.to_tokens(tokens);
3387 self.brace_token.surround(tokens, |tokens| {
3388 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003389 // NOTE: We need a comma before the dot2 token if it is present.
3390 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003391 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003392 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003393 self.dot2_token.to_tokens(tokens);
3394 });
3395 }
3396 }
3397
Michael Layzell734adb42017-06-07 16:58:31 -04003398 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003399 impl ToTokens for PatTupleStruct {
3400 fn to_tokens(&self, tokens: &mut Tokens) {
3401 self.path.to_tokens(tokens);
3402 self.pat.to_tokens(tokens);
3403 }
3404 }
3405
Michael Layzell734adb42017-06-07 16:58:31 -04003406 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003407 impl ToTokens for PatPath {
3408 fn to_tokens(&self, tokens: &mut Tokens) {
3409 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3410 }
3411 }
3412
Michael Layzell734adb42017-06-07 16:58:31 -04003413 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003414 impl ToTokens for PatTuple {
3415 fn to_tokens(&self, tokens: &mut Tokens) {
3416 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003417 self.front.to_tokens(tokens);
3418 if let Some(ref dot2_token) = self.dot2_token {
3419 if !self.front.empty_or_trailing() {
3420 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003421 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003422 }
David Tolnay41871922017-12-29 01:53:45 -05003423 dot2_token.to_tokens(tokens);
3424 self.comma_token.to_tokens(tokens);
3425 if self.comma_token.is_none() && !self.back.is_empty() {
3426 // Ensure there is a comma after the .. token.
3427 <Token![,]>::default().to_tokens(tokens);
3428 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003429 }
David Tolnay41871922017-12-29 01:53:45 -05003430 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003431 });
3432 }
3433 }
3434
Michael Layzell734adb42017-06-07 16:58:31 -04003435 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003436 impl ToTokens for PatBox {
3437 fn to_tokens(&self, tokens: &mut Tokens) {
3438 self.box_token.to_tokens(tokens);
3439 self.pat.to_tokens(tokens);
3440 }
3441 }
3442
Michael Layzell734adb42017-06-07 16:58:31 -04003443 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003444 impl ToTokens for PatRef {
3445 fn to_tokens(&self, tokens: &mut Tokens) {
3446 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003447 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003448 self.pat.to_tokens(tokens);
3449 }
3450 }
3451
Michael Layzell734adb42017-06-07 16:58:31 -04003452 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003453 impl ToTokens for PatLit {
3454 fn to_tokens(&self, tokens: &mut Tokens) {
3455 self.expr.to_tokens(tokens);
3456 }
3457 }
3458
Michael Layzell734adb42017-06-07 16:58:31 -04003459 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003460 impl ToTokens for PatRange {
3461 fn to_tokens(&self, tokens: &mut Tokens) {
3462 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003463 match self.limits {
3464 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3465 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3466 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003467 self.hi.to_tokens(tokens);
3468 }
3469 }
3470
Michael Layzell734adb42017-06-07 16:58:31 -04003471 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003472 impl ToTokens for PatSlice {
3473 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003474 // XXX: This is a mess, and it will be so easy to screw it up. How
3475 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003476 self.bracket_token.surround(tokens, |tokens| {
3477 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003478
3479 // If we need a comma before the middle or standalone .. token,
3480 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003481 if !self.front.empty_or_trailing()
3482 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003483 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003484 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003485 }
3486
3487 // If we have an identifier, we always need a .. token.
3488 if self.middle.is_some() {
3489 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003490 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003491 } else if self.dot2_token.is_some() {
3492 self.dot2_token.to_tokens(tokens);
3493 }
3494
3495 // Make sure we have a comma before the back half.
3496 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003497 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003498 self.back.to_tokens(tokens);
3499 } else {
3500 self.comma_token.to_tokens(tokens);
3501 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003502 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003503 }
3504 }
3505
Michael Layzell734adb42017-06-07 16:58:31 -04003506 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003507 impl ToTokens for PatMacro {
3508 fn to_tokens(&self, tokens: &mut Tokens) {
3509 self.mac.to_tokens(tokens);
3510 }
3511 }
3512
3513 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003514 impl ToTokens for PatVerbatim {
3515 fn to_tokens(&self, tokens: &mut Tokens) {
3516 self.tts.to_tokens(tokens);
3517 }
3518 }
3519
3520 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003521 impl ToTokens for FieldPat {
3522 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003523 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003524 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003525 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003526 }
3527 self.pat.to_tokens(tokens);
3528 }
3529 }
3530
Michael Layzell734adb42017-06-07 16:58:31 -04003531 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003532 impl ToTokens for Block {
3533 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003534 self.brace_token.surround(tokens, |tokens| {
3535 tokens.append_all(&self.stmts);
3536 });
David Tolnay42602292016-10-01 22:25:45 -07003537 }
3538 }
3539
Michael Layzell734adb42017-06-07 16:58:31 -04003540 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003541 impl ToTokens for Stmt {
3542 fn to_tokens(&self, tokens: &mut Tokens) {
3543 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003544 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003545 Stmt::Item(ref item) => item.to_tokens(tokens),
3546 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003547 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003548 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003549 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003550 }
David Tolnay42602292016-10-01 22:25:45 -07003551 }
3552 }
3553 }
David Tolnay191e0582016-10-02 18:31:09 -07003554
Michael Layzell734adb42017-06-07 16:58:31 -04003555 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003556 impl ToTokens for Local {
3557 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003558 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003559 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003560 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003561 if let Some((ref colon_token, ref ty)) = self.ty {
3562 colon_token.to_tokens(tokens);
3563 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003564 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003565 if let Some((ref eq_token, ref init)) = self.init {
3566 eq_token.to_tokens(tokens);
3567 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003568 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003569 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003570 }
3571 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003572}