blob: 583daac0bbfae261bb7320fd97fd08a8979baed8 [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 Tolnaye98775f2017-12-28 23:17:00 -0500482 pub Paren(ExprParen #full {
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,
David Tolnay66bb8d52018-01-08 08:22:31 -0800642 span: Span::def_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 Tolnay8c91b882017-12-28 23:04:32 -05001586 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001587 ));
1588
Michael Layzell734adb42017-06-07 16:58:31 -04001589 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001590 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001591 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001592 |
David Tolnay8c91b882017-12-28 23:04:32 -05001593 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001594 |
David Tolnay8c91b882017-12-28 23:04:32 -05001595 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001596 |
David Tolnay8c91b882017-12-28 23:04:32 -05001597 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001598 |
David Tolnay8c91b882017-12-28 23:04:32 -05001599 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001600 |
David Tolnay8c91b882017-12-28 23:04:32 -05001601 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001602 |
David Tolnay8c91b882017-12-28 23:04:32 -05001603 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001604 |
David Tolnay8c91b882017-12-28 23:04:32 -05001605 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001606 |
David Tolnay8c91b882017-12-28 23:04:32 -05001607 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001608 |
David Tolnay8c91b882017-12-28 23:04:32 -05001609 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001610 |
David Tolnay8c91b882017-12-28 23:04:32 -05001611 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001612 ), Expr::from));
1613
David Tolnay8c91b882017-12-28 23:04:32 -05001614 impl Synom for ExprLit {
1615 named!(parse -> Self, do_parse!(
1616 lit: syn!(Lit) >>
1617 (ExprLit {
1618 attrs: Vec::new(),
1619 lit: lit,
1620 })
1621 ));
David Tolnay79777332018-01-07 10:04:42 -08001622
1623 fn description() -> Option<&'static str> {
1624 Some("literal")
1625 }
David Tolnay8c91b882017-12-28 23:04:32 -05001626 }
1627
1628 #[cfg(feature = "full")]
1629 impl Synom for ExprMacro {
1630 named!(parse -> Self, do_parse!(
1631 mac: syn!(Macro) >>
1632 (ExprMacro {
1633 attrs: Vec::new(),
1634 mac: mac,
1635 })
1636 ));
David Tolnay79777332018-01-07 10:04:42 -08001637
1638 fn description() -> Option<&'static str> {
1639 Some("macro invocation expression")
1640 }
David Tolnay8c91b882017-12-28 23:04:32 -05001641 }
1642
David Tolnaye98775f2017-12-28 23:17:00 -05001643 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001644 impl Synom for ExprGroup {
1645 named!(parse -> Self, do_parse!(
1646 e: grouped!(syn!(Expr)) >>
1647 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001648 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001649 expr: Box::new(e.1),
1650 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001651 })
Michael Layzell93c36282017-06-04 20:43:14 -04001652 ));
David Tolnay79777332018-01-07 10:04:42 -08001653
1654 fn description() -> Option<&'static str> {
1655 Some("expression surrounded by invisible delimiters")
1656 }
Michael Layzell93c36282017-06-04 20:43:14 -04001657 }
1658
David Tolnaye98775f2017-12-28 23:17:00 -05001659 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001660 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001661 named!(parse -> Self, do_parse!(
1662 e: parens!(syn!(Expr)) >>
1663 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001664 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001665 paren_token: e.0,
1666 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001667 })
Michael Layzell92639a52017-06-01 00:07:44 -04001668 ));
David Tolnay79777332018-01-07 10:04:42 -08001669
1670 fn description() -> Option<&'static str> {
1671 Some("parenthesized expression")
1672 }
Alex Crichton954046c2017-05-30 21:49:42 -07001673 }
David Tolnay89e05672016-10-02 14:39:42 -07001674
Michael Layzell734adb42017-06-07 16:58:31 -04001675 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001676 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001677 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001678 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001679 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001680 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001681 bracket_token: elems.0,
1682 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001683 })
1684 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001685
1686 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001687 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001688 }
Alex Crichton954046c2017-05-30 21:49:42 -07001689 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001690
David Tolnayf2cfd722017-12-31 18:02:51 -05001691 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1692 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001693
Michael Layzell734adb42017-06-07 16:58:31 -04001694 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001695 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001696 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001697 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001698 turbofish: option!(tuple!(
1699 punct!(::),
1700 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001701 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001702 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001703 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001704 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001705 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001706 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001707 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001708 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001709 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1710 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001711 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001712
Alex Crichton954046c2017-05-30 21:49:42 -07001713 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001714 turbofish: turbofish.map(|fish| MethodTurbofish {
1715 colon2_token: fish.0,
1716 lt_token: fish.1,
1717 args: fish.2,
1718 gt_token: fish.3,
1719 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001720 args: args.1,
1721 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001722 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001723 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001724 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001725 ));
1726
Michael Layzell734adb42017-06-07 16:58:31 -04001727 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001728 impl Synom for GenericMethodArgument {
1729 // TODO parse const generics as well
1730 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001731
1732 fn description() -> Option<&'static str> {
1733 Some("generic method argument")
1734 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001735 }
1736
1737 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001738 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001739 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001740 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001741 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001742 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001743 elems: elems.1,
1744 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001745 })
1746 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001747
1748 fn description() -> Option<&'static str> {
1749 Some("tuple")
1750 }
Alex Crichton954046c2017-05-30 21:49:42 -07001751 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001752
Michael Layzell734adb42017-06-07 16:58:31 -04001753 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001754 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001755 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001756 if_: keyword!(if) >>
1757 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001758 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001759 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001760 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001761 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001762 else_block: option!(else_block) >>
1763 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001764 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001765 pat: Box::new(pat),
1766 let_token: let_,
1767 eq_token: eq,
1768 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001769 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001770 brace_token: then_block.0,
1771 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001772 },
1773 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001774 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001775 })
1776 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001777
1778 fn description() -> Option<&'static str> {
1779 Some("`if let` expression")
1780 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001781 }
1782
Michael Layzell734adb42017-06-07 16:58:31 -04001783 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001784 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001785 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001786 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001787 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001788 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001789 else_block: option!(else_block) >>
1790 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001791 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001792 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001793 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001794 brace_token: then_block.0,
1795 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001796 },
1797 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001798 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001799 })
1800 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001801
1802 fn description() -> Option<&'static str> {
1803 Some("`if` expression")
1804 }
Alex Crichton954046c2017-05-30 21:49:42 -07001805 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001806
Michael Layzell734adb42017-06-07 16:58:31 -04001807 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001808 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001809 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001810 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001811 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001812 |
David Tolnay8c91b882017-12-28 23:04:32 -05001813 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001814 |
1815 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001816 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001817 (Expr::Block(ExprBlock {
1818 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001819 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001820 brace_token: else_block.0,
1821 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001822 },
1823 }))
David Tolnay939766a2016-09-23 23:48:12 -07001824 )
Alex Crichton954046c2017-05-30 21:49:42 -07001825 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001826 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001827 ));
1828
Michael Layzell734adb42017-06-07 16:58:31 -04001829 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001830 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001831 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001832 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001833 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001834 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001835 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001836 expr: expr_no_struct >>
1837 loop_block: syn!(Block) >>
1838 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001839 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001840 for_token: for_,
1841 in_token: in_,
1842 pat: Box::new(pat),
1843 expr: Box::new(expr),
1844 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001845 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001846 })
1847 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001848
1849 fn description() -> Option<&'static str> {
1850 Some("`for` loop")
1851 }
Alex Crichton954046c2017-05-30 21:49:42 -07001852 }
Gregory Katze5f35682016-09-27 14:20:55 -04001853
Michael Layzell734adb42017-06-07 16:58:31 -04001854 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001855 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001856 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001857 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001858 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001859 loop_block: syn!(Block) >>
1860 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001861 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001862 loop_token: loop_,
1863 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001864 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001865 })
1866 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001867
1868 fn description() -> Option<&'static str> {
1869 Some("`loop`")
1870 }
Alex Crichton954046c2017-05-30 21:49:42 -07001871 }
1872
Michael Layzell734adb42017-06-07 16:58:31 -04001873 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001874 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001875 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001876 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001877 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001878 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001879 (ExprMatch {
1880 attrs: Vec::new(),
1881 expr: Box::new(obj),
1882 match_token: match_,
1883 brace_token: res.0,
1884 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001885 })
1886 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001887
1888 fn description() -> Option<&'static str> {
1889 Some("`match` expression")
1890 }
Alex Crichton954046c2017-05-30 21:49:42 -07001891 }
David Tolnay1978c672016-10-27 22:05:52 -07001892
Michael Layzell734adb42017-06-07 16:58:31 -04001893 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001894 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001895 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001896 do_: keyword!(do) >>
1897 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001898 catch_block: syn!(Block) >>
1899 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001900 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001901 block: catch_block,
1902 do_token: do_,
1903 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001904 })
Michael Layzell92639a52017-06-01 00:07:44 -04001905 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001906
1907 fn description() -> Option<&'static str> {
1908 Some("`catch` expression")
1909 }
Alex Crichton954046c2017-05-30 21:49:42 -07001910 }
Arnavion02ef13f2017-04-25 00:54:31 -07001911
Michael Layzell734adb42017-06-07 16:58:31 -04001912 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001913 impl Synom for ExprYield {
1914 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001915 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001916 expr: option!(syn!(Expr)) >>
1917 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001918 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001919 yield_token: yield_,
1920 expr: expr.map(Box::new),
1921 })
1922 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001923
1924 fn description() -> Option<&'static str> {
1925 Some("`yield` expression")
1926 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001927 }
1928
1929 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001930 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001931 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001932 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001933 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001934 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1935 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001936 body: do_parse!(
1937 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001938 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1939 true => alt!(
1940 input_end!() => { |_| None }
1941 |
1942 punct!(,) => { Some }
1943 )
Alex Crichton03b30272017-08-28 09:35:24 -07001944 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001945 false => option!(punct!(,))
1946 ) >>
1947 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001948 ) >>
1949 (Arm {
1950 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001951 attrs: attrs,
1952 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001953 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001954 body: Box::new(body.0),
1955 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001956 })
1957 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001958
1959 fn description() -> Option<&'static str> {
1960 Some("`match` arm")
1961 }
Alex Crichton954046c2017-05-30 21:49:42 -07001962 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001963
Michael Layzell734adb42017-06-07 16:58:31 -04001964 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001965 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001966 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001967 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001968 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001969 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001970 ret_and_body: alt!(
1971 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001972 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001973 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001974 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001975 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001976 Expr::Block(ExprBlock {
1977 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001978 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001979 }))
David Tolnay89e05672016-10-02 14:39:42 -07001980 )
1981 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001982 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001983 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001984 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001985 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001986 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001987 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001988 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001989 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001990 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001991 body: Box::new(ret_and_body.1),
1992 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001993 ));
1994
Michael Layzell734adb42017-06-07 16:58:31 -04001995 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001996 named!(fn_arg -> FnArg, do_parse!(
1997 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001998 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001999 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002000 if let Some((colon, ty)) = ty {
2001 FnArg::Captured(ArgCaptured {
2002 pat: pat,
2003 colon_token: colon,
2004 ty: ty,
2005 })
2006 } else {
2007 FnArg::Inferred(pat)
2008 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002009 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002010 ));
2011
Michael Layzell734adb42017-06-07 16:58:31 -04002012 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002013 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002014 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05002015 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002016 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002017 cond: expr_no_struct >>
2018 while_block: syn!(Block) >>
2019 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05002020 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002021 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002022 cond: Box::new(cond),
2023 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05002024 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002025 })
2026 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002027
2028 fn description() -> Option<&'static str> {
2029 Some("`while` expression")
2030 }
Alex Crichton954046c2017-05-30 21:49:42 -07002031 }
2032
Michael Layzell734adb42017-06-07 16:58:31 -04002033 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002034 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002035 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05002036 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002037 while_: keyword!(while) >>
2038 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002039 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002040 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002041 value: expr_no_struct >>
2042 while_block: syn!(Block) >>
2043 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05002044 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002045 eq_token: eq,
2046 let_token: let_,
2047 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002048 pat: Box::new(pat),
2049 expr: Box::new(value),
2050 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05002051 label: label,
2052 })
2053 ));
David Tolnay79777332018-01-07 10:04:42 -08002054
2055 fn description() -> Option<&'static str> {
2056 Some("`while let` expression")
2057 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002058 }
2059
2060 #[cfg(feature = "full")]
2061 impl Synom for Label {
2062 named!(parse -> Self, do_parse!(
2063 name: syn!(Lifetime) >>
2064 colon: punct!(:) >>
2065 (Label {
2066 name: name,
2067 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002068 })
2069 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002070
2071 fn description() -> Option<&'static str> {
2072 Some("`while let` expression")
2073 }
Alex Crichton954046c2017-05-30 21:49:42 -07002074 }
2075
Michael Layzell734adb42017-06-07 16:58:31 -04002076 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002077 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002078 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002079 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002080 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002081 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05002082 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002083 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002084 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002085 })
2086 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002087
2088 fn description() -> Option<&'static str> {
2089 Some("`continue`")
2090 }
Alex Crichton954046c2017-05-30 21:49:42 -07002091 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002092
Michael Layzell734adb42017-06-07 16:58:31 -04002093 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002094 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002095 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002096 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002097 // We can't allow blocks after a `break` expression when we wouldn't
2098 // allow structs, as this expression is ambiguous.
2099 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002100 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05002101 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05002102 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002103 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002104 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002105 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002106 ));
2107
Michael Layzell734adb42017-06-07 16:58:31 -04002108 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002109 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002110 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002111 // NOTE: return is greedy and eats blocks after it even when in a
2112 // position where structs are not allowed, such as in if statement
2113 // conditions. For example:
2114 //
David Tolnaybcf26022017-12-25 22:10:52 -05002115 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002116 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002117 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05002118 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002119 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002120 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002121 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002122 ));
2123
Michael Layzell734adb42017-06-07 16:58:31 -04002124 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002125 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002126 named!(parse -> Self, do_parse!(
2127 path: syn!(Path) >>
2128 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002129 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002130 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2131 dots: punct!(..) >>
2132 base: syn!(Expr) >>
2133 (dots, base)
2134 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002135 (fields, base)
2136 )) >>
2137 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002138 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002139 let (dots, rest) = match base.and_then(|b| b) {
2140 Some((dots, base)) => (Some(dots), Some(base)),
2141 None => (None, None),
2142 };
2143 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05002144 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002145 brace_token: brace,
2146 path: path,
2147 fields: fields,
2148 dot2_token: dots,
2149 rest: rest.map(Box::new),
2150 }
2151 })
2152 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002153
2154 fn description() -> Option<&'static str> {
2155 Some("struct literal expression")
2156 }
Alex Crichton954046c2017-05-30 21:49:42 -07002157 }
2158
Michael Layzell734adb42017-06-07 16:58:31 -04002159 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002160 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002161 named!(parse -> Self, do_parse!(
2162 attrs: many0!(Attribute::parse_outer) >>
2163 field_value: alt!(
2164 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2165 |
2166 map!(syn!(Ident), |name| (
2167 Member::Named(name),
2168 None,
2169 Expr::Path(ExprPath {
2170 attrs: Vec::new(),
2171 qself: None,
2172 path: name.into(),
2173 }),
2174 ))
2175 ) >>
2176 (FieldValue {
2177 attrs: attrs,
2178 member: field_value.0,
2179 colon_token: field_value.1,
2180 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002181 })
2182 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002183
2184 fn description() -> Option<&'static str> {
2185 Some("field-value pair: `field: value`")
2186 }
Alex Crichton954046c2017-05-30 21:49:42 -07002187 }
David Tolnay055a7042016-10-02 19:23:54 -07002188
Michael Layzell734adb42017-06-07 16:58:31 -04002189 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002190 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002191 named!(parse -> Self, do_parse!(
2192 data: brackets!(do_parse!(
2193 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002194 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002195 times: syn!(Expr) >>
2196 (value, semi, times)
2197 )) >>
2198 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05002199 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05002200 expr: Box::new((data.1).0),
David Tolnay84d80442018-01-07 01:03:20 -08002201 len: Box::new((data.1).2),
David Tolnay8875fca2017-12-31 13:52:37 -05002202 bracket_token: data.0,
2203 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002204 })
2205 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002206
2207 fn description() -> Option<&'static str> {
2208 Some("repeated array literal: `[val; N]`")
2209 }
Alex Crichton954046c2017-05-30 21:49:42 -07002210 }
David Tolnay055a7042016-10-02 19:23:54 -07002211
Michael Layzell734adb42017-06-07 16:58:31 -04002212 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002213 impl Synom for ExprUnsafe {
2214 named!(parse -> Self, do_parse!(
2215 unsafe_: keyword!(unsafe) >>
2216 b: syn!(Block) >>
2217 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002218 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002219 unsafe_token: unsafe_,
2220 block: b,
2221 })
2222 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002223
2224 fn description() -> Option<&'static str> {
2225 Some("unsafe block: `unsafe { .. }`")
2226 }
Nika Layzell640832a2017-12-04 13:37:09 -05002227 }
2228
2229 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002230 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002231 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04002232 b: syn!(Block) >>
2233 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002234 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002235 block: b,
2236 })
2237 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002238
2239 fn description() -> Option<&'static str> {
2240 Some("block: `{ .. }`")
2241 }
Alex Crichton954046c2017-05-30 21:49:42 -07002242 }
David Tolnay89e05672016-10-02 14:39:42 -07002243
Michael Layzell734adb42017-06-07 16:58:31 -04002244 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002245 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002246 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002247 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002248 (ExprRange {
2249 attrs: Vec::new(),
2250 from: None,
2251 to: hi.map(Box::new),
2252 limits: limits,
2253 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002254 ));
2255
Michael Layzell734adb42017-06-07 16:58:31 -04002256 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002257 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002258 named!(parse -> Self, alt!(
2259 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002260 punct!(..=) => { RangeLimits::Closed }
2261 |
2262 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002263 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002264 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002265 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002266 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002267
2268 fn description() -> Option<&'static str> {
2269 Some("range limit: `..`, `...` or `..=`")
2270 }
Alex Crichton954046c2017-05-30 21:49:42 -07002271 }
David Tolnay438c9052016-10-07 23:24:48 -07002272
Alex Crichton954046c2017-05-30 21:49:42 -07002273 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002274 named!(parse -> Self, do_parse!(
2275 pair: qpath >>
2276 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002277 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002278 qself: pair.0,
2279 path: pair.1,
2280 })
2281 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002282
2283 fn description() -> Option<&'static str> {
2284 Some("path: `a::b::c`")
2285 }
Alex Crichton954046c2017-05-30 21:49:42 -07002286 }
David Tolnay42602292016-10-01 22:25:45 -07002287
Michael Layzell734adb42017-06-07 16:58:31 -04002288 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002289 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002290
David Tolnay8875fca2017-12-31 13:52:37 -05002291 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002292
Michael Layzell734adb42017-06-07 16:58:31 -04002293 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002294 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002295 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002296 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002297 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002298 brace_token: stmts.0,
2299 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002300 })
2301 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002302
2303 fn description() -> Option<&'static str> {
2304 Some("block: `{ .. }`")
2305 }
Alex Crichton954046c2017-05-30 21:49:42 -07002306 }
David Tolnay939766a2016-09-23 23:48:12 -07002307
Michael Layzell734adb42017-06-07 16:58:31 -04002308 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002309 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002310 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002311 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002312 mut standalone: many0!(do_parse!(
2313 stmt: syn!(Stmt) >>
2314 many0!(punct!(;)) >>
2315 (stmt)
2316 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002317 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002318 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002319 mut e: syn!(Expr) >>
2320 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002321 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002322 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002323 })
2324 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002325 (match last {
2326 None => standalone,
2327 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002328 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002329 standalone
2330 }
2331 })
2332 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002333 }
2334
Michael Layzell734adb42017-06-07 16:58:31 -04002335 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002336 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002337 named!(parse -> Self, alt!(
2338 stmt_mac
2339 |
2340 stmt_local
2341 |
2342 stmt_item
2343 |
Michael Layzell35418782017-06-07 09:20:25 -04002344 stmt_blockexpr
2345 |
Michael Layzell92639a52017-06-01 00:07:44 -04002346 stmt_expr
2347 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002348
2349 fn description() -> Option<&'static str> {
2350 Some("statement")
2351 }
Alex Crichton954046c2017-05-30 21:49:42 -07002352 }
David Tolnay939766a2016-09-23 23:48:12 -07002353
Michael Layzell734adb42017-06-07 16:58:31 -04002354 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002355 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002356 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002357 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002358 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002359 // Only parse braces here; paren and bracket will get parsed as
2360 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002361 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002362 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002363 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002364 attrs: attrs,
2365 ident: None,
2366 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002367 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002368 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002369 delimiter: MacroDelimiter::Brace(data.0),
2370 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002371 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002372 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002373 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002374 ));
2375
Michael Layzell734adb42017-06-07 16:58:31 -04002376 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002377 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002378 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002379 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002380 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002381 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002382 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2383 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002384 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002385 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002386 let_token: let_,
2387 pat: Box::new(pat),
2388 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2389 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2390 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002391 }))
David Tolnay191e0582016-10-02 18:31:09 -07002392 ));
2393
Michael Layzell734adb42017-06-07 16:58:31 -04002394 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002395 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002396
Michael Layzell734adb42017-06-07 16:58:31 -04002397 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002398 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002399 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002400 mut e: expr_nosemi >>
2401 // If the next token is a `.` or a `?` it is special-cased to parse as
2402 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002403 not!(punct!(.)) >>
2404 not!(punct!(?)) >>
2405 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002406 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002407 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002408 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002409 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002410 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002411 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002412 }
2413 })
2414 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002415
Michael Layzell734adb42017-06-07 16:58:31 -04002416 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002417 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002418 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002419 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002420 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002421 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002422 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002423 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002424 })
David Tolnay939766a2016-09-23 23:48:12 -07002425 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002426
Michael Layzell734adb42017-06-07 16:58:31 -04002427 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002428 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002429 named!(parse -> Self, alt!(
2430 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2431 |
2432 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2433 |
2434 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2435 |
2436 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2437 |
2438 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2439 |
David Tolnay323279a2017-12-29 11:26:32 -05002440 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002441 |
2442 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2443 |
2444 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2445 |
2446 syn!(PatPath) => { Pat::Path }
2447 |
2448 syn!(PatTuple) => { Pat::Tuple }
2449 |
2450 syn!(PatRef) => { Pat::Ref }
2451 |
2452 syn!(PatSlice) => { Pat::Slice }
2453 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002454
2455 fn description() -> Option<&'static str> {
2456 Some("pattern")
2457 }
Alex Crichton954046c2017-05-30 21:49:42 -07002458 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002459
Michael Layzell734adb42017-06-07 16:58:31 -04002460 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002461 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002462 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002463 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002464 |u| PatWild { underscore_token: u }
2465 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002466
2467 fn description() -> Option<&'static str> {
2468 Some("wild pattern: `_`")
2469 }
Alex Crichton954046c2017-05-30 21:49:42 -07002470 }
David Tolnay84aa0752016-10-02 23:01:13 -07002471
Michael Layzell734adb42017-06-07 16:58:31 -04002472 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002473 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002474 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002475 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002476 pat: syn!(Pat) >>
2477 (PatBox {
2478 pat: Box::new(pat),
2479 box_token: boxed,
2480 })
2481 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002482
2483 fn description() -> Option<&'static str> {
2484 Some("box pattern")
2485 }
Alex Crichton954046c2017-05-30 21:49:42 -07002486 }
2487
Michael Layzell734adb42017-06-07 16:58:31 -04002488 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002489 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002490 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002491 by_ref: option!(keyword!(ref)) >>
2492 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002493 name: alt!(
2494 syn!(Ident)
2495 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002496 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002497 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002498 not!(punct!(<)) >>
2499 not!(punct!(::)) >>
2500 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002501 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002502 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002503 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002504 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002505 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002506 })
2507 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002508
2509 fn description() -> Option<&'static str> {
2510 Some("pattern identifier binding")
2511 }
Alex Crichton954046c2017-05-30 21:49:42 -07002512 }
2513
Michael Layzell734adb42017-06-07 16:58:31 -04002514 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002515 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002516 named!(parse -> Self, do_parse!(
2517 path: syn!(Path) >>
2518 tuple: syn!(PatTuple) >>
2519 (PatTupleStruct {
2520 path: path,
2521 pat: tuple,
2522 })
2523 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002524
2525 fn description() -> Option<&'static str> {
2526 Some("tuple struct pattern")
2527 }
Alex Crichton954046c2017-05-30 21:49:42 -07002528 }
2529
Michael Layzell734adb42017-06-07 16:58:31 -04002530 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002531 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002532 named!(parse -> Self, do_parse!(
2533 path: syn!(Path) >>
2534 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002535 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002536 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002537 (fields, base)
2538 )) >>
2539 (PatStruct {
2540 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002541 fields: (data.1).0,
2542 brace_token: data.0,
2543 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002544 })
2545 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002546
2547 fn description() -> Option<&'static str> {
2548 Some("struct pattern")
2549 }
Alex Crichton954046c2017-05-30 21:49:42 -07002550 }
2551
Michael Layzell734adb42017-06-07 16:58:31 -04002552 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002553 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002554 named!(parse -> Self, alt!(
2555 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002556 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002557 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002558 pat: syn!(Pat) >>
2559 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002560 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002561 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002562 attrs: Vec::new(),
2563 colon_token: Some(colon),
2564 })
2565 )
2566 |
2567 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002568 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002569 by_ref: option!(keyword!(ref)) >>
2570 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002571 ident: syn!(Ident) >>
2572 ({
2573 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002574 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002575 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002576 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002577 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002578 }.into();
2579 if let Some(boxed) = boxed {
2580 pat = PatBox {
2581 pat: Box::new(pat),
2582 box_token: boxed,
2583 }.into();
2584 }
2585 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002586 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002587 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002588 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002589 colon_token: None,
2590 }
2591 })
2592 )
2593 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002594
2595 fn description() -> Option<&'static str> {
2596 Some("field pattern")
2597 }
Alex Crichton954046c2017-05-30 21:49:42 -07002598 }
2599
Michael Layzell734adb42017-06-07 16:58:31 -04002600 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002601 impl Synom for Member {
2602 named!(parse -> Self, alt!(
2603 syn!(Ident) => { Member::Named }
2604 |
2605 syn!(Index) => { Member::Unnamed }
2606 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002607
2608 fn description() -> Option<&'static str> {
2609 Some("field member")
2610 }
David Tolnay85b69a42017-12-27 20:43:10 -05002611 }
2612
2613 #[cfg(feature = "full")]
2614 impl Synom for Index {
2615 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002616 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002617 ({
David Tolnay360efd22018-01-04 23:35:26 -08002618 if let IntSuffix::None = lit.suffix() {
2619 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002620 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002621 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002622 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002623 })
David Tolnay85b69a42017-12-27 20:43:10 -05002624 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002625
2626 fn description() -> Option<&'static str> {
2627 Some("field index")
2628 }
David Tolnay85b69a42017-12-27 20:43:10 -05002629 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002630
Michael Layzell734adb42017-06-07 16:58:31 -04002631 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002632 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002633 named!(parse -> Self, map!(
2634 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002635 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002636 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002637
2638 fn description() -> Option<&'static str> {
2639 Some("path pattern")
2640 }
Alex Crichton954046c2017-05-30 21:49:42 -07002641 }
David Tolnay9636c052016-10-02 17:11:17 -07002642
Michael Layzell734adb42017-06-07 16:58:31 -04002643 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002644 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002645 named!(parse -> Self, do_parse!(
2646 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002647 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002648 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2649 tuple!(punct!(..), option!(punct!(,)))
2650 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002651 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002652 Some((_, Some(_))) => true,
2653 _ => false,
2654 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002655 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002656 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002657 )) >>
2658 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002659 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002660 let (dotdot, trailing) = match dotdot {
2661 Some((a, b)) => (Some(a), Some(b)),
2662 None => (None, None),
2663 };
2664 PatTuple {
2665 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002666 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002667 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002668 comma_token: trailing.unwrap_or_default(),
2669 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002670 }
2671 })
2672 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002673
2674 fn description() -> Option<&'static str> {
2675 Some("tuple pattern")
2676 }
Alex Crichton954046c2017-05-30 21:49:42 -07002677 }
David Tolnayfbb73232016-10-03 01:00:06 -07002678
Michael Layzell734adb42017-06-07 16:58:31 -04002679 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002680 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002681 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002682 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002683 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002684 pat: syn!(Pat) >>
2685 (PatRef {
2686 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002687 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002688 and_token: and,
2689 })
2690 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002691
2692 fn description() -> Option<&'static str> {
2693 Some("reference pattern")
2694 }
Alex Crichton954046c2017-05-30 21:49:42 -07002695 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002696
Michael Layzell734adb42017-06-07 16:58:31 -04002697 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002698 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002699 named!(parse -> Self, do_parse!(
2700 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002701 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002702 return parse_error(); // these need to be parsed by pat_path
2703 } else {
2704 PatLit {
2705 expr: Box::new(lit),
2706 }
2707 })
2708 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002709
2710 fn description() -> Option<&'static str> {
2711 Some("literal pattern")
2712 }
Alex Crichton954046c2017-05-30 21:49:42 -07002713 }
David Tolnaye1310902016-10-29 23:40:00 -07002714
Michael Layzell734adb42017-06-07 16:58:31 -04002715 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002716 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002717 named!(parse -> Self, do_parse!(
2718 lo: pat_lit_expr >>
2719 limits: syn!(RangeLimits) >>
2720 hi: pat_lit_expr >>
2721 (PatRange {
2722 lo: Box::new(lo),
2723 hi: Box::new(hi),
2724 limits: limits,
2725 })
2726 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002727
2728 fn description() -> Option<&'static str> {
2729 Some("range pattern")
2730 }
Alex Crichton954046c2017-05-30 21:49:42 -07002731 }
David Tolnaye1310902016-10-29 23:40:00 -07002732
Michael Layzell734adb42017-06-07 16:58:31 -04002733 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002734 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002735 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002736 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002737 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002738 |
David Tolnay8c91b882017-12-28 23:04:32 -05002739 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002740 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002741 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002742 Expr::Unary(ExprUnary {
2743 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002744 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002745 expr: Box::new(v)
2746 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002747 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002748 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002749 })
2750 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002751
Michael Layzell734adb42017-06-07 16:58:31 -04002752 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002753 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002754 named!(parse -> Self, map!(
2755 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002756 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002757 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002758 dots: punct!(..) >>
2759 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002760 (dots, trailing)
2761 )) >>
2762 after: cond!(
2763 match middle {
2764 Some((_, ref trailing)) => trailing.is_some(),
2765 _ => false,
2766 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002767 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002768 ) >>
2769 (before, middle, after)
2770 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002771 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002772 let mut before: Punctuated<Pat, Token![,]> = before;
2773 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002774 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002775 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002776 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002777 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002778 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002779 }),
2780 bracket_token: brackets,
2781 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002782 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002783 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002784 } else {
David Tolnay56080682018-01-06 14:01:52 -08002785 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002786 }
2787 }),
2788 front: before,
2789 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002790 }
Alex Crichton954046c2017-05-30 21:49:42 -07002791 }
Michael Layzell92639a52017-06-01 00:07:44 -04002792 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002793
2794 fn description() -> Option<&'static str> {
2795 Some("slice pattern")
2796 }
Alex Crichton954046c2017-05-30 21:49:42 -07002797 }
David Tolnay323279a2017-12-29 11:26:32 -05002798
2799 #[cfg(feature = "full")]
2800 impl Synom for PatMacro {
2801 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002802
2803 fn description() -> Option<&'static str> {
2804 Some("macro pattern")
2805 }
David Tolnay323279a2017-12-29 11:26:32 -05002806 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002807}
2808
David Tolnayf4bbbd92016-09-23 14:41:55 -07002809#[cfg(feature = "printing")]
2810mod printing {
2811 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002812 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002813 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002814 use quote::{ToTokens, Tokens};
David Tolnay61037c62018-01-05 16:21:03 -08002815 use proc_macro2::{Literal, TokenNode, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002816
David Tolnaybcf26022017-12-25 22:10:52 -05002817 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2818 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002819 #[cfg(feature = "full")]
2820 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002821 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002822 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002823 e.to_tokens(tokens);
2824 });
2825 } else {
2826 e.to_tokens(tokens);
2827 }
2828 }
2829
David Tolnay8c91b882017-12-28 23:04:32 -05002830 #[cfg(feature = "full")]
2831 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2832 tokens.append_all(attrs.outer());
2833 }
Michael Layzell734adb42017-06-07 16:58:31 -04002834
David Tolnay8c91b882017-12-28 23:04:32 -05002835 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002836 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002837
Michael Layzell734adb42017-06-07 16:58:31 -04002838 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002839 impl ToTokens for ExprBox {
2840 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002841 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002842 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002843 self.expr.to_tokens(tokens);
2844 }
2845 }
2846
Michael Layzell734adb42017-06-07 16:58:31 -04002847 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002848 impl ToTokens for ExprInPlace {
2849 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002850 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002851 self.place.to_tokens(tokens);
2852 self.arrow_token.to_tokens(tokens);
2853 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002854 }
2855 }
2856
Michael Layzell734adb42017-06-07 16:58:31 -04002857 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002858 impl ToTokens for ExprArray {
2859 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002860 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002861 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002862 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002863 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002864 }
2865 }
2866
2867 impl ToTokens for ExprCall {
2868 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002869 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002870 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002871 self.paren_token.surround(tokens, |tokens| {
2872 self.args.to_tokens(tokens);
2873 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002874 }
2875 }
2876
Michael Layzell734adb42017-06-07 16:58:31 -04002877 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002878 impl ToTokens for ExprMethodCall {
2879 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002880 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002881 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002882 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002883 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002884 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002885 self.paren_token.surround(tokens, |tokens| {
2886 self.args.to_tokens(tokens);
2887 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002888 }
2889 }
2890
Michael Layzell734adb42017-06-07 16:58:31 -04002891 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002892 impl ToTokens for MethodTurbofish {
2893 fn to_tokens(&self, tokens: &mut Tokens) {
2894 self.colon2_token.to_tokens(tokens);
2895 self.lt_token.to_tokens(tokens);
2896 self.args.to_tokens(tokens);
2897 self.gt_token.to_tokens(tokens);
2898 }
2899 }
2900
2901 #[cfg(feature = "full")]
2902 impl ToTokens for GenericMethodArgument {
2903 fn to_tokens(&self, tokens: &mut Tokens) {
2904 match *self {
2905 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2906 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2907 }
2908 }
2909 }
2910
2911 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002912 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002913 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002914 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002915 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002916 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002917 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002918 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002919 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002920 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002921 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002922 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002923 }
2924 }
2925
2926 impl ToTokens for ExprBinary {
2927 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002928 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002929 self.left.to_tokens(tokens);
2930 self.op.to_tokens(tokens);
2931 self.right.to_tokens(tokens);
2932 }
2933 }
2934
2935 impl ToTokens for ExprUnary {
2936 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002937 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002938 self.op.to_tokens(tokens);
2939 self.expr.to_tokens(tokens);
2940 }
2941 }
2942
David Tolnay8c91b882017-12-28 23:04:32 -05002943 impl ToTokens for ExprLit {
2944 fn to_tokens(&self, tokens: &mut Tokens) {
2945 attrs_to_tokens(&self.attrs, tokens);
2946 self.lit.to_tokens(tokens);
2947 }
2948 }
2949
Alex Crichton62a0a592017-05-22 13:58:53 -07002950 impl ToTokens for ExprCast {
2951 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002952 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002953 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002955 self.ty.to_tokens(tokens);
2956 }
2957 }
2958
David Tolnay0cf94f22017-12-28 23:46:26 -05002959 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002960 impl ToTokens for ExprType {
2961 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002962 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002963 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002964 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002965 self.ty.to_tokens(tokens);
2966 }
2967 }
2968
Michael Layzell734adb42017-06-07 16:58:31 -04002969 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002970 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002971 if let Some((ref else_token, ref else_)) = *else_ {
2972 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002973
2974 // If we are not one of the valid expressions to exist in an else
2975 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002976 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002977 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002978 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002979 }
2980 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002981 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002982 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002983 });
2984 }
2985 }
2986 }
2987 }
2988
2989 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002990 impl ToTokens for ExprIf {
2991 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002992 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002993 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002994 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002995 self.then_branch.to_tokens(tokens);
2996 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002997 }
2998 }
2999
Michael Layzell734adb42017-06-07 16:58:31 -04003000 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003001 impl ToTokens for ExprIfLet {
3002 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003003 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003004 self.if_token.to_tokens(tokens);
3005 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003006 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003007 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003008 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003009 self.then_branch.to_tokens(tokens);
3010 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003011 }
3012 }
3013
Michael Layzell734adb42017-06-07 16:58:31 -04003014 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003015 impl ToTokens for ExprWhile {
3016 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003017 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003018 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003019 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003020 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07003021 self.body.to_tokens(tokens);
3022 }
3023 }
3024
Michael Layzell734adb42017-06-07 16:58:31 -04003025 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003026 impl ToTokens for ExprWhileLet {
3027 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003028 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003029 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003030 self.while_token.to_tokens(tokens);
3031 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003032 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003033 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003034 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07003035 self.body.to_tokens(tokens);
3036 }
3037 }
3038
Michael Layzell734adb42017-06-07 16:58:31 -04003039 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003040 impl ToTokens for ExprForLoop {
3041 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003042 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003043 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003044 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003045 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003046 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003047 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07003048 self.body.to_tokens(tokens);
3049 }
3050 }
3051
Michael Layzell734adb42017-06-07 16:58:31 -04003052 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003053 impl ToTokens for ExprLoop {
3054 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003055 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05003056 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003057 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003058 self.body.to_tokens(tokens);
3059 }
3060 }
3061
Michael Layzell734adb42017-06-07 16:58:31 -04003062 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003063 impl ToTokens for ExprMatch {
3064 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003065 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003066 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003067 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003068 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05003069 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003070 arm.to_tokens(tokens);
3071 // Ensure that we have a comma after a non-block arm, except
3072 // for the last one.
3073 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003074 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003075 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003076 }
3077 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003078 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003079 }
3080 }
3081
Michael Layzell734adb42017-06-07 16:58:31 -04003082 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003083 impl ToTokens for ExprCatch {
3084 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003085 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003086 self.do_token.to_tokens(tokens);
3087 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003088 self.block.to_tokens(tokens);
3089 }
3090 }
3091
Michael Layzell734adb42017-06-07 16:58:31 -04003092 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003093 impl ToTokens for ExprYield {
3094 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003095 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07003096 self.yield_token.to_tokens(tokens);
3097 self.expr.to_tokens(tokens);
3098 }
3099 }
3100
3101 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003102 impl ToTokens for ExprClosure {
3103 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003104 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003105 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003106 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003107 for input in self.inputs.pairs() {
3108 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003109 FnArg::Captured(ArgCaptured {
3110 ref pat,
3111 ty: Type::Infer(_),
3112 ..
3113 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003114 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003115 }
David Tolnay56080682018-01-06 14:01:52 -08003116 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003117 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003118 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003119 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003120 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003121 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003122 self.body.to_tokens(tokens);
3123 }
3124 }
3125
Michael Layzell734adb42017-06-07 16:58:31 -04003126 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003127 impl ToTokens for ExprUnsafe {
3128 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003129 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05003130 self.unsafe_token.to_tokens(tokens);
3131 self.block.to_tokens(tokens);
3132 }
3133 }
3134
3135 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003136 impl ToTokens for ExprBlock {
3137 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003138 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003139 self.block.to_tokens(tokens);
3140 }
3141 }
3142
Michael Layzell734adb42017-06-07 16:58:31 -04003143 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003144 impl ToTokens for ExprAssign {
3145 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003146 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003147 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003148 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003149 self.right.to_tokens(tokens);
3150 }
3151 }
3152
Michael Layzell734adb42017-06-07 16:58:31 -04003153 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003154 impl ToTokens for ExprAssignOp {
3155 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003156 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003157 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003158 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003159 self.right.to_tokens(tokens);
3160 }
3161 }
3162
Michael Layzell734adb42017-06-07 16:58:31 -04003163 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003164 impl ToTokens for ExprField {
3165 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003166 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003167 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003168 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003169 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003170 }
3171 }
3172
David Tolnay85b69a42017-12-27 20:43:10 -05003173 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07003174 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003175 match *self {
3176 Member::Named(ident) => ident.to_tokens(tokens),
3177 Member::Unnamed(ref index) => index.to_tokens(tokens),
3178 }
3179 }
3180 }
3181
David Tolnay85b69a42017-12-27 20:43:10 -05003182 impl ToTokens for Index {
3183 fn to_tokens(&self, tokens: &mut Tokens) {
3184 tokens.append(TokenTree {
3185 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05003186 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05003187 });
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
David Tolnaye98775f2017-12-28 23:17:00 -05003304 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003305 impl ToTokens for ExprParen {
3306 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003307 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003308 self.paren_token.surround(tokens, |tokens| {
3309 self.expr.to_tokens(tokens);
3310 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003311 }
3312 }
3313
Michael Layzell734adb42017-06-07 16:58:31 -04003314 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003315 impl ToTokens for ExprTry {
3316 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003317 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003318 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003319 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003320 }
3321 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003322
David Tolnay2ae520a2017-12-29 11:19:50 -05003323 impl ToTokens for ExprVerbatim {
3324 fn to_tokens(&self, tokens: &mut Tokens) {
3325 self.tts.to_tokens(tokens);
3326 }
3327 }
3328
Michael Layzell734adb42017-06-07 16:58:31 -04003329 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003330 impl ToTokens for Label {
3331 fn to_tokens(&self, tokens: &mut Tokens) {
3332 self.name.to_tokens(tokens);
3333 self.colon_token.to_tokens(tokens);
3334 }
3335 }
3336
3337 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003338 impl ToTokens for FieldValue {
3339 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayc42b90a2018-01-18 23:11:37 -08003340 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003341 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003342 if let Some(ref colon_token) = self.colon_token {
3343 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003344 self.expr.to_tokens(tokens);
3345 }
David Tolnay055a7042016-10-02 19:23:54 -07003346 }
3347 }
3348
Michael Layzell734adb42017-06-07 16:58:31 -04003349 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003350 impl ToTokens for Arm {
3351 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003352 tokens.append_all(&self.attrs);
3353 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003354 if let Some((ref if_token, ref guard)) = self.guard {
3355 if_token.to_tokens(tokens);
3356 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003357 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003358 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003359 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003360 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003361 }
3362 }
3363
Michael Layzell734adb42017-06-07 16:58:31 -04003364 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003365 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003366 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003367 self.underscore_token.to_tokens(tokens);
3368 }
3369 }
3370
Michael Layzell734adb42017-06-07 16:58:31 -04003371 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003372 impl ToTokens for PatIdent {
3373 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003374 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003375 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003376 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003377 if let Some((ref at_token, ref subpat)) = self.subpat {
3378 at_token.to_tokens(tokens);
3379 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003380 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003381 }
3382 }
3383
Michael Layzell734adb42017-06-07 16:58:31 -04003384 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003385 impl ToTokens for PatStruct {
3386 fn to_tokens(&self, tokens: &mut Tokens) {
3387 self.path.to_tokens(tokens);
3388 self.brace_token.surround(tokens, |tokens| {
3389 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003390 // NOTE: We need a comma before the dot2 token if it is present.
3391 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003392 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003393 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003394 self.dot2_token.to_tokens(tokens);
3395 });
3396 }
3397 }
3398
Michael Layzell734adb42017-06-07 16:58:31 -04003399 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003400 impl ToTokens for PatTupleStruct {
3401 fn to_tokens(&self, tokens: &mut Tokens) {
3402 self.path.to_tokens(tokens);
3403 self.pat.to_tokens(tokens);
3404 }
3405 }
3406
Michael Layzell734adb42017-06-07 16:58:31 -04003407 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003408 impl ToTokens for PatPath {
3409 fn to_tokens(&self, tokens: &mut Tokens) {
3410 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3411 }
3412 }
3413
Michael Layzell734adb42017-06-07 16:58:31 -04003414 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003415 impl ToTokens for PatTuple {
3416 fn to_tokens(&self, tokens: &mut Tokens) {
3417 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003418 self.front.to_tokens(tokens);
3419 if let Some(ref dot2_token) = self.dot2_token {
3420 if !self.front.empty_or_trailing() {
3421 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003422 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003423 }
David Tolnay41871922017-12-29 01:53:45 -05003424 dot2_token.to_tokens(tokens);
3425 self.comma_token.to_tokens(tokens);
3426 if self.comma_token.is_none() && !self.back.is_empty() {
3427 // Ensure there is a comma after the .. token.
3428 <Token![,]>::default().to_tokens(tokens);
3429 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003430 }
David Tolnay41871922017-12-29 01:53:45 -05003431 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003432 });
3433 }
3434 }
3435
Michael Layzell734adb42017-06-07 16:58:31 -04003436 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003437 impl ToTokens for PatBox {
3438 fn to_tokens(&self, tokens: &mut Tokens) {
3439 self.box_token.to_tokens(tokens);
3440 self.pat.to_tokens(tokens);
3441 }
3442 }
3443
Michael Layzell734adb42017-06-07 16:58:31 -04003444 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003445 impl ToTokens for PatRef {
3446 fn to_tokens(&self, tokens: &mut Tokens) {
3447 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003448 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003449 self.pat.to_tokens(tokens);
3450 }
3451 }
3452
Michael Layzell734adb42017-06-07 16:58:31 -04003453 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003454 impl ToTokens for PatLit {
3455 fn to_tokens(&self, tokens: &mut Tokens) {
3456 self.expr.to_tokens(tokens);
3457 }
3458 }
3459
Michael Layzell734adb42017-06-07 16:58:31 -04003460 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003461 impl ToTokens for PatRange {
3462 fn to_tokens(&self, tokens: &mut Tokens) {
3463 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003464 match self.limits {
3465 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3466 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3467 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003468 self.hi.to_tokens(tokens);
3469 }
3470 }
3471
Michael Layzell734adb42017-06-07 16:58:31 -04003472 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003473 impl ToTokens for PatSlice {
3474 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003475 // XXX: This is a mess, and it will be so easy to screw it up. How
3476 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003477 self.bracket_token.surround(tokens, |tokens| {
3478 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003479
3480 // If we need a comma before the middle or standalone .. token,
3481 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003482 if !self.front.empty_or_trailing()
3483 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003484 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003485 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003486 }
3487
3488 // If we have an identifier, we always need a .. token.
3489 if self.middle.is_some() {
3490 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003491 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003492 } else if self.dot2_token.is_some() {
3493 self.dot2_token.to_tokens(tokens);
3494 }
3495
3496 // Make sure we have a comma before the back half.
3497 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003498 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003499 self.back.to_tokens(tokens);
3500 } else {
3501 self.comma_token.to_tokens(tokens);
3502 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003503 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003504 }
3505 }
3506
Michael Layzell734adb42017-06-07 16:58:31 -04003507 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003508 impl ToTokens for PatMacro {
3509 fn to_tokens(&self, tokens: &mut Tokens) {
3510 self.mac.to_tokens(tokens);
3511 }
3512 }
3513
3514 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003515 impl ToTokens for PatVerbatim {
3516 fn to_tokens(&self, tokens: &mut Tokens) {
3517 self.tts.to_tokens(tokens);
3518 }
3519 }
3520
3521 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003522 impl ToTokens for FieldPat {
3523 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003524 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003525 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003526 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003527 }
3528 self.pat.to_tokens(tokens);
3529 }
3530 }
3531
Michael Layzell734adb42017-06-07 16:58:31 -04003532 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003533 impl ToTokens for Block {
3534 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003535 self.brace_token.surround(tokens, |tokens| {
3536 tokens.append_all(&self.stmts);
3537 });
David Tolnay42602292016-10-01 22:25:45 -07003538 }
3539 }
3540
Michael Layzell734adb42017-06-07 16:58:31 -04003541 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003542 impl ToTokens for Stmt {
3543 fn to_tokens(&self, tokens: &mut Tokens) {
3544 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003545 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003546 Stmt::Item(ref item) => item.to_tokens(tokens),
3547 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003548 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003549 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003550 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003551 }
David Tolnay42602292016-10-01 22:25:45 -07003552 }
3553 }
3554 }
David Tolnay191e0582016-10-02 18:31:09 -07003555
Michael Layzell734adb42017-06-07 16:58:31 -04003556 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003557 impl ToTokens for Local {
3558 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003559 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003560 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003561 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003562 if let Some((ref colon_token, ref ty)) = self.ty {
3563 colon_token.to_tokens(tokens);
3564 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003565 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003566 if let Some((ref eq_token, ref init)) = self.init {
3567 eq_token.to_tokens(tokens);
3568 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003569 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003570 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003571 }
3572 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003573}