blob: 3742fb4f5cac562f7c69f085bde8edcd605c5fd0 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnaye303b7c2018-05-20 16:46:35 -070010use proc_macro2::{Span, TokenStream};
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay14982012017-12-29 00:49:51 -050012#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -050013use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -050014#[cfg(feature = "full")]
15use std::mem;
David Tolnay94d2b792018-04-29 12:26:10 -070016#[cfg(feature = "extra-traits")]
17use tt::TokenStreamHelper;
David Tolnayf4bbbd92016-09-23 14:41:55 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -080020 /// A Rust expression.
David Tolnay614a0142018-01-07 10:25:43 -080021 ///
David Tolnay461d98e2018-01-07 11:07:19 -080022 /// *This type is available if Syn is built with the `"derive"` or `"full"`
23 /// feature.*
24 ///
David Tolnay614a0142018-01-07 10:25:43 -080025 /// # Syntax tree enums
26 ///
27 /// This type is a syntax tree enum. In Syn this and other syntax tree enums
28 /// are designed to be traversed using the following rebinding idiom.
29 ///
30 /// ```
31 /// # use syn::Expr;
32 /// #
33 /// # fn example(expr: Expr) {
34 /// # const IGNORE: &str = stringify! {
35 /// let expr: Expr = /* ... */;
36 /// # };
37 /// match expr {
38 /// Expr::MethodCall(expr) => {
39 /// /* ... */
40 /// }
41 /// Expr::Cast(expr) => {
42 /// /* ... */
43 /// }
44 /// Expr::IfLet(expr) => {
45 /// /* ... */
46 /// }
47 /// /* ... */
48 /// # _ => {}
49 /// }
50 /// # }
51 /// ```
52 ///
53 /// We begin with a variable `expr` of type `Expr` that has no fields
54 /// (because it is an enum), and by matching on it and rebinding a variable
55 /// with the same name `expr` we effectively imbue our variable with all of
56 /// the data fields provided by the variant that it turned out to be. So for
57 /// example above if we ended up in the `MethodCall` case then we get to use
58 /// `expr.receiver`, `expr.args` etc; if we ended up in the `IfLet` case we
59 /// get to use `expr.pat`, `expr.then_branch`, `expr.else_branch`.
60 ///
61 /// The pattern is similar if the input expression is borrowed:
62 ///
63 /// ```
64 /// # use syn::Expr;
65 /// #
66 /// # fn example(expr: &Expr) {
67 /// match *expr {
68 /// Expr::MethodCall(ref expr) => {
69 /// # }
70 /// # _ => {}
71 /// # }
72 /// # }
73 /// ```
74 ///
75 /// This approach avoids repeating the variant names twice on every line.
76 ///
77 /// ```
78 /// # use syn::{Expr, ExprMethodCall};
79 /// #
80 /// # fn example(expr: Expr) {
81 /// # match expr {
82 /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive
83 /// # }
84 /// # _ => {}
85 /// # }
86 /// # }
87 /// ```
88 ///
89 /// In general, the name to which a syntax tree enum variant is bound should
90 /// be a suitable name for the complete syntax tree enum type.
91 ///
92 /// ```
93 /// # use syn::{Expr, ExprField};
94 /// #
95 /// # fn example(discriminant: &ExprField) {
96 /// // Binding is called `base` which is the name I would use if I were
97 /// // assigning `*discriminant.base` without an `if let`.
98 /// if let Expr::Tuple(ref base) = *discriminant.base {
99 /// # }
100 /// # }
101 /// ```
102 ///
103 /// A sign that you may not be choosing the right variable names is if you
104 /// see names getting repeated in your code, like accessing
105 /// `receiver.receiver` or `pat.pat` or `cond.cond`.
David Tolnay8c91b882017-12-28 23:04:32 -0500106 pub enum Expr {
David Tolnaya454c8f2018-01-07 01:01:10 -0800107 /// A box expression: `box f`.
David Tolnay461d98e2018-01-07 11:07:19 -0800108 ///
109 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400110 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500111 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800112 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500113 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500115
David Tolnaya454c8f2018-01-07 01:01:10 -0800116 /// A placement expression: `place <- value`.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -0500122 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 pub value: Box<Expr>,
124 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500125
David Tolnaya454c8f2018-01-07 01:01:10 -0800126 /// A slice literal expression: `[a, b, c, d]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400129 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500130 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500131 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500134
David Tolnaya454c8f2018-01-07 01:01:10 -0800135 /// A function call expression: `invoke(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -0500140 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700141 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500142 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500143 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500145
David Tolnaya454c8f2018-01-07 01:01:10 -0800146 /// A method call expression: `x.foo::<T>(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800147 ///
148 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -0500151 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -0500154 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500156 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500158
David Tolnaya454c8f2018-01-07 01:01:10 -0800159 /// A tuple expression: `(a, b, c, d)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800160 ///
161 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -0500162 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500163 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500164 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500165 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500167
David Tolnaya454c8f2018-01-07 01:01:10 -0800168 /// A binary operation: `a + b`, `a * b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800169 ///
170 /// *This type is available if Syn is built with the `"derive"` or
171 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500173 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500175 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 pub right: Box<Expr>,
177 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500178
David Tolnaya454c8f2018-01-07 01:01:10 -0800179 /// A unary operation: `!x`, `*x`.
David Tolnay461d98e2018-01-07 11:07:19 -0800180 ///
181 /// *This type is available if Syn is built with the `"derive"` or
182 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700183 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 pub op: UnOp,
186 pub expr: Box<Expr>,
187 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500188
David Tolnaya454c8f2018-01-07 01:01:10 -0800189 /// A literal in place of an expression: `1`, `"foo"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800190 ///
191 /// *This type is available if Syn is built with the `"derive"` or
192 /// `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500193 pub Lit(ExprLit {
194 pub attrs: Vec<Attribute>,
195 pub lit: Lit,
196 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500197
David Tolnaya454c8f2018-01-07 01:01:10 -0800198 /// A cast expression: `foo as f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800199 ///
200 /// *This type is available if Syn is built with the `"derive"` or
201 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700202 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -0500203 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800206 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500208
David Tolnaya454c8f2018-01-07 01:01:10 -0800209 /// A type ascription expression: `foo: f64`.
David Tolnay461d98e2018-01-07 11:07:19 -0800210 ///
211 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay0cf94f22017-12-28 23:46:26 -0500212 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500213 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700214 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800215 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800216 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500218
David Tolnaya454c8f2018-01-07 01:01:10 -0800219 /// An `if` expression with an optional `else` block: `if expr { ... }
220 /// else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800222 /// The `else` branch expression may only be an `If`, `IfLet`, or
223 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800224 ///
225 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500228 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500230 pub then_branch: Block,
231 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500233
David Tolnaya454c8f2018-01-07 01:01:10 -0800234 /// An `if let` expression with an optional `else` block: `if let pat =
235 /// expr { ... } else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800237 /// The `else` branch expression may only be an `If`, `IfLet`, or
238 /// `Block` expression, not any of the other types of expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800239 ///
240 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800243 pub if_token: Token![if],
244 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200245 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500248 pub then_branch: Block,
249 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500251
David Tolnaya454c8f2018-01-07 01:01:10 -0800252 /// A while loop: `while expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800253 ///
254 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400255 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500256 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500257 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800258 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500259 pub cond: Box<Expr>,
260 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500262
David Tolnaya454c8f2018-01-07 01:01:10 -0800263 /// A while-let loop: `while let pat = expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800264 ///
265 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400266 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500267 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500268 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800269 pub while_token: Token![while],
270 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200271 pub pats: Punctuated<Pat, Token![|]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500273 pub expr: Box<Expr>,
274 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
David Tolnaya454c8f2018-01-07 01:01:10 -0800277 /// A for loop: `for pat in expr { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800278 ///
279 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400280 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500281 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500282 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500283 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500285 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 pub expr: Box<Expr>,
287 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500289
David Tolnaya454c8f2018-01-07 01:01:10 -0800290 /// Conditionless loop: `loop { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800291 ///
292 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400293 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500295 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub loop_token: Token![loop],
297 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500299
David Tolnaya454c8f2018-01-07 01:01:10 -0800300 /// A `match` expression: `match n { Some(n) => {}, None => {} }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800301 ///
302 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400303 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500304 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800305 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500307 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 pub arms: Vec<Arm>,
309 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500310
David Tolnaya454c8f2018-01-07 01:01:10 -0800311 /// A closure expression: `|a, b| a + b`.
David Tolnay461d98e2018-01-07 11:07:19 -0800312 ///
313 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400314 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500315 pub attrs: Vec<Attribute>,
David Tolnay13d4c0e2018-03-31 20:53:59 +0200316 pub movability: Option<Token![static]>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500317 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800318 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500319 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800320 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500321 pub output: ReturnType,
322 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500324
David Tolnaya454c8f2018-01-07 01:01:10 -0800325 /// An unsafe block: `unsafe { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800326 ///
327 /// *This type is available if Syn is built with the `"full"` feature.*
Nika Layzell640832a2017-12-04 13:37:09 -0500328 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500329 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500330 pub unsafe_token: Token![unsafe],
331 pub block: Block,
332 }),
333
David Tolnaya454c8f2018-01-07 01:01:10 -0800334 /// A blocked scope: `{ ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800335 ///
336 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400337 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500338 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 pub block: Block,
340 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700341
David Tolnaya454c8f2018-01-07 01:01:10 -0800342 /// An assignment expression: `a = compute()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800343 ///
344 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400345 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500346 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800348 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500349 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700350 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500351
David Tolnaya454c8f2018-01-07 01:01:10 -0800352 /// A compound assignment expression: `counter += 1`.
David Tolnay461d98e2018-01-07 11:07:19 -0800353 ///
354 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400355 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500356 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700357 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500358 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub right: Box<Expr>,
360 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500361
David Tolnaya454c8f2018-01-07 01:01:10 -0800362 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500363 /// field (`obj.0`).
David Tolnay461d98e2018-01-07 11:07:19 -0800364 ///
365 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd5147742018-06-30 10:09:52 -0700366 pub Field(ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -0500367 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500368 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800369 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500370 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500372
David Tolnay05658502018-01-07 09:56:37 -0800373 /// A square bracketed indexing expression: `vector[2]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800374 ///
375 /// *This type is available if Syn is built with the `"derive"` or
376 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700377 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500378 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500380 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500381 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500383
David Tolnaya454c8f2018-01-07 01:01:10 -0800384 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800385 ///
386 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400387 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500388 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700390 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500391 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700393
David Tolnaya454c8f2018-01-07 01:01:10 -0800394 /// A path like `std::mem::replace` possibly containing generic
395 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800397 /// A plain identifier like `x` is a path of length 1.
David Tolnay461d98e2018-01-07 11:07:19 -0800398 ///
399 /// *This type is available if Syn is built with the `"derive"` or
400 /// `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500402 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700403 pub qself: Option<QSelf>,
404 pub path: Path,
405 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700406
David Tolnaya454c8f2018-01-07 01:01:10 -0800407 /// A referencing operation: `&a` or `&mut a`.
David Tolnay461d98e2018-01-07 11:07:19 -0800408 ///
409 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay00674ba2018-03-31 18:14:11 +0200410 pub Reference(ExprReference #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500411 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800412 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500413 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 pub expr: Box<Expr>,
415 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500416
David Tolnaya454c8f2018-01-07 01:01:10 -0800417 /// A `break`, with an optional label to break and an optional
418 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800419 ///
420 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400421 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500422 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500423 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700424 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700425 pub expr: Option<Box<Expr>>,
426 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500427
David Tolnaya454c8f2018-01-07 01:01:10 -0800428 /// A `continue`, with an optional label.
David Tolnay461d98e2018-01-07 11:07:19 -0800429 ///
430 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400431 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500432 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800433 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500434 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700435 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500436
David Tolnaya454c8f2018-01-07 01:01:10 -0800437 /// A `return`, with an optional value to be returned.
David Tolnay461d98e2018-01-07 11:07:19 -0800438 ///
439 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayc246cd32017-12-28 23:14:32 -0500440 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500441 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800442 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500443 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700444 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700445
David Tolnaya454c8f2018-01-07 01:01:10 -0800446 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800447 ///
448 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay8c91b882017-12-28 23:04:32 -0500449 pub Macro(ExprMacro #full {
450 pub attrs: Vec<Attribute>,
451 pub mac: Macro,
452 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700453
David Tolnaya454c8f2018-01-07 01:01:10 -0800454 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700455 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800456 /// The `rest` provides the value of the remaining fields as in `S { a:
457 /// 1, b: 1, ..rest }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800458 ///
459 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400460 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500461 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700462 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500463 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500464 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500465 pub dot2_token: Option<Token![..]>,
466 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700467 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700468
David Tolnaya454c8f2018-01-07 01:01:10 -0800469 /// An array literal constructed from one repeated element: `[0u8; N]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800470 ///
471 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400472 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500473 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500474 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700475 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500476 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800477 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700478 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700479
David Tolnaya454c8f2018-01-07 01:01:10 -0800480 /// A parenthesized expression: `(a + b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800481 ///
482 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay9374bc02018-01-27 18:49:36 -0800483 pub Paren(ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -0500484 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500485 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500486 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700488
David Tolnaya454c8f2018-01-07 01:01:10 -0800489 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400490 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800491 /// This variant is important for faithfully representing the precedence
492 /// of expressions and is related to `None`-delimited spans in a
493 /// `TokenStream`.
David Tolnay461d98e2018-01-07 11:07:19 -0800494 ///
495 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaye98775f2017-12-28 23:17:00 -0500496 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500497 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500498 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500499 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400500 }),
501
David Tolnaya454c8f2018-01-07 01:01:10 -0800502 /// A try-expression: `expr?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800503 ///
504 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400505 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500506 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700507 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800508 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700510
David Tolnaya454c8f2018-01-07 01:01:10 -0800511 /// A catch expression: `do catch { ... }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800512 ///
513 /// *This type is available if Syn is built with the `"full"` feature.*
Michael Layzell734adb42017-06-07 16:58:31 -0400514 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500515 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516 pub do_token: Token![do],
517 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700518 pub block: Block,
519 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700520
David Tolnaya454c8f2018-01-07 01:01:10 -0800521 /// A yield expression: `yield expr`.
David Tolnay461d98e2018-01-07 11:07:19 -0800522 ///
523 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonfe110462017-06-01 12:49:27 -0700524 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500525 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800526 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700527 pub expr: Option<Box<Expr>>,
528 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500529
David Tolnaya454c8f2018-01-07 01:01:10 -0800530 /// Tokens in expression position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800531 ///
532 /// *This type is available if Syn is built with the `"derive"` or
533 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500534 pub Verbatim(ExprVerbatim #manual_extra_traits {
535 pub tts: TokenStream,
536 }),
537 }
538}
539
540#[cfg(feature = "extra-traits")]
541impl Eq for ExprVerbatim {}
542
543#[cfg(feature = "extra-traits")]
544impl PartialEq for ExprVerbatim {
545 fn eq(&self, other: &Self) -> bool {
546 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
547 }
548}
549
550#[cfg(feature = "extra-traits")]
551impl Hash for ExprVerbatim {
552 fn hash<H>(&self, state: &mut H)
553 where
554 H: Hasher,
555 {
556 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700558}
559
David Tolnay8c91b882017-12-28 23:04:32 -0500560impl Expr {
561 // Not public API.
562 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500563 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500564 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500565 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800566 Expr::Box(ExprBox { ref mut attrs, .. })
567 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
568 | Expr::Array(ExprArray { ref mut attrs, .. })
569 | Expr::Call(ExprCall { ref mut attrs, .. })
570 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
571 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
572 | Expr::Binary(ExprBinary { ref mut attrs, .. })
573 | Expr::Unary(ExprUnary { ref mut attrs, .. })
574 | Expr::Lit(ExprLit { ref mut attrs, .. })
575 | Expr::Cast(ExprCast { ref mut attrs, .. })
576 | Expr::Type(ExprType { ref mut attrs, .. })
577 | Expr::If(ExprIf { ref mut attrs, .. })
578 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
579 | Expr::While(ExprWhile { ref mut attrs, .. })
580 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
581 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
582 | Expr::Loop(ExprLoop { ref mut attrs, .. })
583 | Expr::Match(ExprMatch { ref mut attrs, .. })
584 | Expr::Closure(ExprClosure { ref mut attrs, .. })
585 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
586 | Expr::Block(ExprBlock { ref mut attrs, .. })
587 | Expr::Assign(ExprAssign { ref mut attrs, .. })
588 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
589 | Expr::Field(ExprField { ref mut attrs, .. })
590 | Expr::Index(ExprIndex { ref mut attrs, .. })
591 | Expr::Range(ExprRange { ref mut attrs, .. })
592 | Expr::Path(ExprPath { ref mut attrs, .. })
David Tolnay00674ba2018-03-31 18:14:11 +0200593 | Expr::Reference(ExprReference { ref mut attrs, .. })
David Tolnay61037c62018-01-05 16:21:03 -0800594 | Expr::Break(ExprBreak { ref mut attrs, .. })
595 | Expr::Continue(ExprContinue { ref mut attrs, .. })
596 | Expr::Return(ExprReturn { ref mut attrs, .. })
597 | Expr::Macro(ExprMacro { ref mut attrs, .. })
598 | Expr::Struct(ExprStruct { ref mut attrs, .. })
599 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
600 | Expr::Paren(ExprParen { ref mut attrs, .. })
601 | Expr::Group(ExprGroup { ref mut attrs, .. })
602 | Expr::Try(ExprTry { ref mut attrs, .. })
603 | Expr::Catch(ExprCatch { ref mut attrs, .. })
604 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500605 Expr::Verbatim(_) => {
606 // TODO
607 Vec::new()
608 }
David Tolnay8c91b882017-12-28 23:04:32 -0500609 }
610 }
611}
612
David Tolnay85b69a42017-12-27 20:43:10 -0500613ast_enum! {
614 /// A struct or tuple struct field accessed in a struct literal or field
615 /// expression.
David Tolnay461d98e2018-01-07 11:07:19 -0800616 ///
617 /// *This type is available if Syn is built with the `"derive"` or `"full"`
618 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500619 pub enum Member {
620 /// A named field like `self.x`.
621 Named(Ident),
622 /// An unnamed field like `self.0`.
623 Unnamed(Index),
624 }
625}
626
David Tolnay85b69a42017-12-27 20:43:10 -0500627ast_struct! {
628 /// The index of an unnamed tuple struct field.
David Tolnay461d98e2018-01-07 11:07:19 -0800629 ///
630 /// *This type is available if Syn is built with the `"derive"` or `"full"`
631 /// feature.*
David Tolnay85b69a42017-12-27 20:43:10 -0500632 pub struct Index #manual_extra_traits {
633 pub index: u32,
634 pub span: Span,
635 }
636}
637
David Tolnay14982012017-12-29 00:49:51 -0500638impl From<usize> for Index {
639 fn from(index: usize) -> Index {
David Tolnay34071ba2018-05-20 20:00:41 -0700640 assert!(index < u32::max_value() as usize);
David Tolnay14982012017-12-29 00:49:51 -0500641 Index {
642 index: index as u32,
Alex Crichton9a4dca22018-03-28 06:32:19 -0700643 span: Span::call_site(),
David Tolnay14982012017-12-29 00:49:51 -0500644 }
645 }
646}
647
648#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500649impl Eq for Index {}
650
David Tolnay14982012017-12-29 00:49:51 -0500651#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500652impl PartialEq for Index {
653 fn eq(&self, other: &Self) -> bool {
654 self.index == other.index
655 }
656}
657
David Tolnay14982012017-12-29 00:49:51 -0500658#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500659impl Hash for Index {
660 fn hash<H: Hasher>(&self, state: &mut H) {
661 self.index.hash(state);
662 }
663}
664
665#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700666ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800667 /// The `::<>` explicit type parameters passed to a method call:
668 /// `parse::<u64>()`.
David Tolnay461d98e2018-01-07 11:07:19 -0800669 ///
670 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500671 pub struct MethodTurbofish {
672 pub colon2_token: Token![::],
673 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500674 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500675 pub gt_token: Token![>],
676 }
677}
678
679#[cfg(feature = "full")]
680ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800681 /// An individual generic argument to a method, like `T`.
David Tolnay461d98e2018-01-07 11:07:19 -0800682 ///
683 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnayd60cfec2017-12-29 00:21:38 -0500684 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800685 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500686 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800687 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500688 ///
689 /// NOTE: Identity expressions are represented as Type arguments, as
690 /// they are indistinguishable syntactically.
691 Const(Expr),
692 }
693}
694
695#[cfg(feature = "full")]
696ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700697 /// A field-value pair in a struct literal.
David Tolnay461d98e2018-01-07 11:07:19 -0800698 ///
699 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500701 /// Attributes tagged on the field.
702 pub attrs: Vec<Attribute>,
703
704 /// Name or index of the field.
705 pub member: Member,
706
David Tolnay5d7098a2017-12-29 01:35:24 -0500707 /// The colon in `Struct { x: x }`. If written in shorthand like
708 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500709 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500710
Alex Crichton62a0a592017-05-22 13:58:53 -0700711 /// Value of the field.
712 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700713 }
David Tolnay055a7042016-10-02 19:23:54 -0700714}
715
Michael Layzell734adb42017-06-07 16:58:31 -0400716#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700717ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800718 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnay461d98e2018-01-07 11:07:19 -0800719 ///
720 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnaybcd498f2017-12-29 12:02:33 -0500721 pub struct Label {
722 pub name: Lifetime,
723 pub colon_token: Token![:],
724 }
725}
726
727#[cfg(feature = "full")]
728ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800729 /// A braced block containing Rust statements.
David Tolnay461d98e2018-01-07 11:07:19 -0800730 ///
731 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500733 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700734 /// Statements in a block
735 pub stmts: Vec<Stmt>,
736 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700737}
738
Michael Layzell734adb42017-06-07 16:58:31 -0400739#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700740ast_enum! {
741 /// A statement, usually ending in a semicolon.
David Tolnay461d98e2018-01-07 11:07:19 -0800742 ///
743 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 pub enum Stmt {
745 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800746 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700747
Alex Crichton62a0a592017-05-22 13:58:53 -0700748 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800749 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700750
Alex Crichton62a0a592017-05-22 13:58:53 -0700751 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800752 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700753
David Tolnaya454c8f2018-01-07 01:01:10 -0800754 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800755 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700756 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700757}
758
Michael Layzell734adb42017-06-07 16:58:31 -0400759#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700760ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800761 /// A local `let` binding: `let x: u64 = s.parse()?`.
David Tolnay461d98e2018-01-07 11:07:19 -0800762 ///
763 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700764 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500765 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766 pub let_token: Token![let],
David Tolnay5b5b7d22018-03-31 21:05:00 +0200767 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500768 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500769 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500770 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700771 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700772}
773
Michael Layzell734adb42017-06-07 16:58:31 -0400774#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700775ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800776 /// A pattern in a local binding, function signature, match expression, or
777 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800778 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800779 /// *This type is available if Syn is built with the `"full"` feature.*
780 ///
David Tolnay614a0142018-01-07 10:25:43 -0800781 /// # Syntax tree enum
782 ///
783 /// This type is a [syntax tree enum].
784 ///
785 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700786 // Clippy false positive
787 // https://github.com/Manishearth/rust-clippy/issues/1241
788 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
789 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800790 /// A pattern that matches any value: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800791 ///
792 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700793 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800794 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700796
David Tolnaya454c8f2018-01-07 01:01:10 -0800797 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
David Tolnay461d98e2018-01-07 11:07:19 -0800798 ///
799 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700800 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500801 pub by_ref: Option<Token![ref]>,
802 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700803 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500804 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700806
David Tolnaya454c8f2018-01-07 01:01:10 -0800807 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
David Tolnay461d98e2018-01-07 11:07:19 -0800808 ///
809 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700810 pub Struct(PatStruct {
811 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500812 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500813 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800814 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700815 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700816
David Tolnaya454c8f2018-01-07 01:01:10 -0800817 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800818 ///
819 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700820 pub TupleStruct(PatTupleStruct {
821 pub path: Path,
822 pub pat: PatTuple,
823 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700824
David Tolnaya454c8f2018-01-07 01:01:10 -0800825 /// A path pattern like `Color::Red`, optionally qualified with a
826 /// self-type.
827 ///
828 /// Unquailfied path patterns can legally refer to variants, structs,
829 /// constants or associated constants. Quailfied path patterns like
830 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
831 /// associated constants.
David Tolnay461d98e2018-01-07 11:07:19 -0800832 ///
833 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700834 pub Path(PatPath {
835 pub qself: Option<QSelf>,
836 pub path: Path,
837 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700838
David Tolnaya454c8f2018-01-07 01:01:10 -0800839 /// A tuple pattern: `(a, b)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800840 ///
841 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700842 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500843 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500844 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500845 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500846 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500847 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700848 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800849
850 /// A box pattern: `box v`.
David Tolnay461d98e2018-01-07 11:07:19 -0800851 ///
852 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700853 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800854 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500855 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700856 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800857
858 /// A reference pattern: `&mut (first, second)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800859 ///
860 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700861 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800862 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500863 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500864 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700865 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800866
867 /// A literal pattern: `0`.
868 ///
869 /// This holds an `Expr` rather than a `Lit` because negative numbers
870 /// are represented as an `Expr::Unary`.
David Tolnay461d98e2018-01-07 11:07:19 -0800871 ///
872 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 pub Lit(PatLit {
874 pub expr: Box<Expr>,
875 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800876
877 /// A range pattern: `1..=2`.
David Tolnay461d98e2018-01-07 11:07:19 -0800878 ///
879 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 pub Range(PatRange {
881 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700882 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500883 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700884 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800885
886 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
David Tolnay461d98e2018-01-07 11:07:19 -0800887 ///
888 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700889 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500890 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500891 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700892 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500893 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500894 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500895 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800897
898 /// A macro in expression position.
David Tolnay461d98e2018-01-07 11:07:19 -0800899 ///
900 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500901 pub Macro(PatMacro {
902 pub mac: Macro,
903 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800904
905 /// Tokens in pattern position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800906 ///
907 /// *This type is available if Syn is built with the `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500908 pub Verbatim(PatVerbatim #manual_extra_traits {
909 pub tts: TokenStream,
910 }),
911 }
912}
913
David Tolnayc43b44e2017-12-30 23:55:54 -0500914#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500915impl Eq for PatVerbatim {}
916
David Tolnayc43b44e2017-12-30 23:55:54 -0500917#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500918impl PartialEq for PatVerbatim {
919 fn eq(&self, other: &Self) -> bool {
920 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
921 }
922}
923
David Tolnayc43b44e2017-12-30 23:55:54 -0500924#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500925impl Hash for PatVerbatim {
926 fn hash<H>(&self, state: &mut H)
927 where
928 H: Hasher,
929 {
930 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700931 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700932}
933
Michael Layzell734adb42017-06-07 16:58:31 -0400934#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700935ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800936 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700937 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800938 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700939 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500940 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800941 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500942 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700943 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800944 /// 0...10 => {
945 /// return true;
946 /// }
947 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500948 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800950 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500951 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700952 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800953 ///
954 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700955 pub struct Arm {
956 pub attrs: Vec<Attribute>,
David Tolnay18cc4d42018-03-31 18:47:20 +0200957 pub leading_vert: Option<Token![|]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500958 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500959 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnaydfb91432018-03-31 19:19:44 +0200960 pub fat_arrow_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700963 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700964}
965
Michael Layzell734adb42017-06-07 16:58:31 -0400966#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700967ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800968 /// Limit types of a range, inclusive or exclusive.
David Tolnay461d98e2018-01-07 11:07:19 -0800969 ///
970 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700971 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700972 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800973 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800975 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800976 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700977 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700978}
979
Michael Layzell734adb42017-06-07 16:58:31 -0400980#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700981ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800982 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700983 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800984 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
985 /// 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 -0800986 ///
987 /// *This type is available if Syn is built with the `"full"` feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700988 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500989 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500990 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500991 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700992 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700993 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700994}
995
Michael Layzell3936ceb2017-07-08 00:28:36 -0400996#[cfg(any(feature = "parsing", feature = "printing"))]
997#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700998fn arm_expr_requires_comma(expr: &Expr) -> bool {
999 // see https://github.com/rust-lang/rust/blob/eb8f2586e
1000 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -05001001 match *expr {
1002 Expr::Unsafe(..)
1003 | Expr::Block(..)
1004 | Expr::If(..)
1005 | Expr::IfLet(..)
1006 | Expr::Match(..)
1007 | Expr::While(..)
1008 | Expr::WhileLet(..)
1009 | Expr::Loop(..)
1010 | Expr::ForLoop(..)
1011 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -07001012 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -04001013 }
1014}
1015
David Tolnayb9c8e322016-09-23 20:48:37 -07001016#[cfg(feature = "parsing")]
1017pub mod parsing {
1018 use super::*;
David Tolnay9cc2f092018-08-24 15:51:37 -04001019 use path::parsing::mod_style_path_segment;
David Tolnay2ccf32a2017-12-29 00:34:26 -05001020 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -08001021 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -07001022
David Tolnaydfc886b2018-01-06 08:03:09 -08001023 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -04001024 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -05001025 use parse_error;
David Tolnay94d2b792018-04-29 12:26:10 -07001026 #[cfg(feature = "full")]
1027 use proc_macro2::TokenStream;
David Tolnay203557a2017-12-27 23:59:33 -05001028 use synom::PResult;
David Tolnay94d2b792018-04-29 12:26:10 -07001029 use synom::Synom;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001030
David Tolnaybcf26022017-12-25 22:10:52 -05001031 // When we're parsing expressions which occur before blocks, like in an if
1032 // statement's condition, we cannot parse a struct literal.
1033 //
1034 // Struct literals are ambiguous in certain positions
1035 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -07001036 macro_rules! ambiguous_expr {
1037 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -07001038 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -07001039 };
1040 }
1041
David Tolnaybcf26022017-12-25 22:10:52 -05001042 // When we are parsing an optional suffix expression, we cannot allow blocks
1043 // if structs are not allowed.
1044 //
1045 // Example:
1046 //
1047 // if break {} {}
1048 //
1049 // is ambiguous between:
1050 //
1051 // if (break {}) {}
1052 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -04001053 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001054 macro_rules! opt_ambiguous_expr {
1055 ($i:expr, $allow_struct:ident) => {
1056 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
1057 };
1058 }
1059
Alex Crichton954046c2017-05-30 21:49:42 -07001060 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -04001061 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -07001062
1063 fn description() -> Option<&'static str> {
1064 Some("expression")
1065 }
1066 }
1067
Michael Layzell734adb42017-06-07 16:58:31 -04001068 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -07001069 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
1070
David Tolnaybcf26022017-12-25 22:10:52 -05001071 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -04001072 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05001073 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001074 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -04001075 }
1076
Michael Layzell734adb42017-06-07 16:58:31 -04001077 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -05001078 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -05001079 // NOTE: We intentionally skip assign_expr, placement_expr, and
1080 // range_expr, as they are not parsed in non-full mode.
1081 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -04001082 }
1083
David Tolnaybcf26022017-12-25 22:10:52 -05001084 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -04001085 macro_rules! binop {
1086 (
1087 $name: ident,
1088 $next: ident,
1089 $submac: ident!( $($args:tt)* )
1090 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -05001091 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001092 mut e: call!($next, allow_struct, allow_block) >>
1093 many0!(do_parse!(
1094 op: $submac!($($args)*) >>
1095 rhs: call!($next, allow_struct, true) >>
1096 ({
1097 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -05001098 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001099 left: Box::new(e.into()),
1100 op: op,
1101 right: Box::new(rhs.into()),
1102 }.into();
1103 })
1104 )) >>
1105 (e)
1106 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001107 }
David Tolnay54e854d2016-10-24 12:03:30 -07001108 }
David Tolnayb9c8e322016-09-23 20:48:37 -07001109
David Tolnaybcf26022017-12-25 22:10:52 -05001110 // <placement> = <placement> ..
1111 // <placement> += <placement> ..
1112 // <placement> -= <placement> ..
1113 // <placement> *= <placement> ..
1114 // <placement> /= <placement> ..
1115 // <placement> %= <placement> ..
1116 // <placement> ^= <placement> ..
1117 // <placement> &= <placement> ..
1118 // <placement> |= <placement> ..
1119 // <placement> <<= <placement> ..
1120 // <placement> >>= <placement> ..
1121 //
1122 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001123 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001124 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001125 mut e: call!(placement_expr, allow_struct, allow_block) >>
1126 alt!(
1127 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001128 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001129 // Recurse into self to parse right-associative operator.
1130 rhs: call!(assign_expr, allow_struct, true) >>
1131 ({
1132 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -05001133 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001134 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001135 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -05001136 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001137 }.into();
1138 })
1139 )
1140 |
1141 do_parse!(
1142 op: call!(BinOp::parse_assign_op) >>
1143 // Recurse into self to parse right-associative operator.
1144 rhs: call!(assign_expr, allow_struct, true) >>
1145 ({
1146 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001147 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001148 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001149 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001150 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001151 }.into();
1152 })
1153 )
1154 |
1155 epsilon!()
1156 ) >>
1157 (e)
1158 ));
1159
David Tolnaybcf26022017-12-25 22:10:52 -05001160 // <range> <- <range> ..
1161 //
1162 // NOTE: The `in place { expr }` version of this syntax is parsed in
1163 // `atom_expr`, not here.
1164 //
1165 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001166 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001167 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 mut e: call!(range_expr, allow_struct, allow_block) >>
1169 alt!(
1170 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001171 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001172 // Recurse into self to parse right-associative operator.
1173 rhs: call!(placement_expr, allow_struct, true) >>
1174 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001175 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001176 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001177 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001178 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001179 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001180 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 }.into();
1182 })
1183 )
1184 |
1185 epsilon!()
1186 ) >>
1187 (e)
1188 ));
1189
David Tolnaybcf26022017-12-25 22:10:52 -05001190 // <or> ... <or> ..
1191 // <or> .. <or> ..
1192 // <or> ..
1193 //
1194 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1195 // rules are for parsing these expressions are, but this is not correct.
1196 // For example, `a .. b .. c` is not a legal expression. It should not
1197 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1198 //
1199 // NOTE: The form of ranges which don't include a preceding expression are
1200 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001201 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001202 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001203 mut e: call!(or_expr, allow_struct, allow_block) >>
1204 many0!(do_parse!(
1205 limits: syn!(RangeLimits) >>
1206 // We don't want to allow blocks here if we don't allow structs. See
1207 // the reasoning for `opt_ambiguous_expr!` above.
1208 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1209 ({
1210 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001211 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001212 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001213 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001214 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001215 }.into();
1216 })
1217 )) >>
1218 (e)
1219 ));
1220
David Tolnaybcf26022017-12-25 22:10:52 -05001221 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001222 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001223
David Tolnaybcf26022017-12-25 22:10:52 -05001224 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001225 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001226
David Tolnaybcf26022017-12-25 22:10:52 -05001227 // <bitor> == <bitor> ...
1228 // <bitor> != <bitor> ...
1229 // <bitor> >= <bitor> ...
1230 // <bitor> <= <bitor> ...
1231 // <bitor> > <bitor> ...
1232 // <bitor> < <bitor> ...
1233 //
1234 // NOTE: This operator appears to be parsed as left-associative, but errors
1235 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001236 binop!(
1237 compare_expr,
1238 bitor_expr,
1239 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001240 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001241 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001242 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001243 |
1244 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001245 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001246 |
1247 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001248 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001249 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001250 do_parse!(
1251 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001252 not!(punct!(<-)) >>
1253 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001254 (BinOp::Lt(t))
1255 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001258 )
1259 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001260
David Tolnaybcf26022017-12-25 22:10:52 -05001261 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001262 binop!(
1263 bitor_expr,
1264 bitxor_expr,
1265 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1266 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267
David Tolnaybcf26022017-12-25 22:10:52 -05001268 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001269 binop!(
1270 bitxor_expr,
1271 bitand_expr,
1272 do_parse!(
1273 // NOTE: Make sure we aren't looking at ^=.
1274 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1275 )
1276 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001277
David Tolnaybcf26022017-12-25 22:10:52 -05001278 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001279 binop!(
1280 bitand_expr,
1281 shift_expr,
1282 do_parse!(
1283 // NOTE: Make sure we aren't looking at && or &=.
1284 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1285 )
1286 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001287
David Tolnaybcf26022017-12-25 22:10:52 -05001288 // <arith> << <arith> ...
1289 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001290 binop!(
1291 shift_expr,
1292 arith_expr,
1293 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001295 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001296 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001297 )
1298 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001299
David Tolnaybcf26022017-12-25 22:10:52 -05001300 // <term> + <term> ...
1301 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001302 binop!(
1303 arith_expr,
1304 term_expr,
1305 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001306 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001307 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001308 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001309 )
1310 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001311
David Tolnaybcf26022017-12-25 22:10:52 -05001312 // <cast> * <cast> ...
1313 // <cast> / <cast> ...
1314 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001315 binop!(
1316 term_expr,
1317 cast_expr,
1318 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001319 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001321 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001322 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001323 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001324 )
1325 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326
David Tolnaybcf26022017-12-25 22:10:52 -05001327 // <unary> as <ty>
1328 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001329 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001330 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001331 mut e: call!(unary_expr, allow_struct, allow_block) >>
1332 many0!(alt!(
1333 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001334 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001335 // We can't accept `A + B` in cast expressions, as it's
1336 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001337 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338 ({
1339 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001340 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001341 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001342 as_token: as_,
1343 ty: Box::new(ty),
1344 }.into();
1345 })
1346 )
1347 |
1348 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001349 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001350 // We can't accept `A + B` in cast expressions, as it's
1351 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001352 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001353 ({
1354 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001355 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001356 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001357 colon_token: colon,
1358 ty: Box::new(ty),
1359 }.into();
1360 })
1361 )
1362 )) >>
1363 (e)
1364 ));
1365
David Tolnay0cf94f22017-12-28 23:46:26 -05001366 // <unary> as <ty>
1367 #[cfg(not(feature = "full"))]
1368 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1369 mut e: call!(unary_expr, allow_struct, allow_block) >>
1370 many0!(do_parse!(
1371 as_: keyword!(as) >>
1372 // We can't accept `A + B` in cast expressions, as it's
1373 // ambiguous with the + expression.
1374 ty: call!(Type::without_plus) >>
1375 ({
1376 e = ExprCast {
1377 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001378 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001379 as_token: as_,
1380 ty: Box::new(ty),
1381 }.into();
1382 })
1383 )) >>
1384 (e)
1385 ));
1386
David Tolnaybcf26022017-12-25 22:10:52 -05001387 // <UnOp> <trailer>
1388 // & <trailer>
1389 // &mut <trailer>
1390 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001391 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001392 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001393 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001394 attrs: many0!(Attribute::parse_outer) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001395 op: syn!(UnOp) >>
1396 expr: call!(unary_expr, allow_struct, true) >>
1397 (ExprUnary {
David Tolnay5d314dc2018-07-21 16:40:01 -07001398 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001399 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001400 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001401 }.into())
1402 )
1403 |
1404 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001405 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001406 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001407 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001408 expr: call!(unary_expr, allow_struct, true) >>
David Tolnay00674ba2018-03-31 18:14:11 +02001409 (ExprReference {
David Tolnay5d314dc2018-07-21 16:40:01 -07001410 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001411 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001412 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001413 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001414 }.into())
1415 )
1416 |
1417 do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001418 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001419 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001420 expr: call!(unary_expr, allow_struct, true) >>
1421 (ExprBox {
David Tolnay5d314dc2018-07-21 16:40:01 -07001422 attrs: attrs,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001423 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001424 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001425 }.into())
1426 )
1427 |
1428 call!(trailer_expr, allow_struct, allow_block)
1429 ));
1430
Michael Layzell734adb42017-06-07 16:58:31 -04001431 // XXX: This duplication is ugly
1432 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001433 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001434 do_parse!(
1435 op: syn!(UnOp) >>
1436 expr: call!(unary_expr, allow_struct, true) >>
1437 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001438 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001439 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001440 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001441 }.into())
1442 )
1443 |
1444 call!(trailer_expr, allow_struct, allow_block)
1445 ));
1446
David Tolnayd997aef2018-07-21 18:42:31 -07001447 #[cfg(feature = "full")]
David Tolnay5d314dc2018-07-21 16:40:01 -07001448 fn take_outer(attrs: &mut Vec<Attribute>) -> Vec<Attribute> {
1449 let mut outer = Vec::new();
1450 let mut inner = Vec::new();
1451 for attr in mem::replace(attrs, Vec::new()) {
1452 match attr.style {
1453 AttrStyle::Outer => outer.push(attr),
1454 AttrStyle::Inner(_) => inner.push(attr),
1455 }
1456 }
1457 *attrs = inner;
1458 outer
1459 }
1460
David Tolnaybcf26022017-12-25 22:10:52 -05001461 // <atom> (..<args>) ...
1462 // <atom> . <ident> (..<args>) ...
1463 // <atom> . <ident> ...
1464 // <atom> . <lit> ...
1465 // <atom> [ <expr> ] ...
1466 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001467 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001468 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001469 mut e: call!(atom_expr, allow_struct, allow_block) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001470 outer_attrs: value!({
1471 let mut attrs = e.replace_attrs(Vec::new());
1472 let outer_attrs = take_outer(&mut attrs);
1473 e.replace_attrs(attrs);
1474 outer_attrs
1475 }) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001476 many0!(alt!(
1477 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001478 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001479 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001480 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001481 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001482 args: args,
1483 paren_token: paren,
1484 }.into();
1485 })
1486 |
1487 tap!(more: and_method_call => {
1488 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001489 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001490 e = call.into();
1491 })
1492 |
1493 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001494 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001495 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001496 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001497 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001498 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001499 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001500 }.into();
1501 })
1502 |
1503 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001504 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001505 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001506 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001507 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001508 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001509 index: Box::new(i),
1510 }.into();
1511 })
1512 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001513 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001514 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001515 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001516 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001517 question_token: question,
1518 }.into();
1519 })
1520 )) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001521 ({
1522 let mut attrs = outer_attrs;
1523 attrs.extend(e.replace_attrs(Vec::new()));
1524 e.replace_attrs(attrs);
1525 e
1526 })
Michael Layzellb78f3b52017-06-04 19:03:03 -04001527 ));
1528
Michael Layzell734adb42017-06-07 16:58:31 -04001529 // XXX: Duplication == ugly
1530 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001531 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001532 mut e: call!(atom_expr, allow_struct, allow_block) >>
1533 many0!(alt!(
1534 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001535 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001536 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001537 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001538 paren_token: args.0,
1539 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001540 }.into();
1541 })
1542 |
David Tolnayd5147742018-06-30 10:09:52 -07001543 tap!(field: and_field => {
1544 let (token, member) = field;
1545 e = ExprField {
1546 attrs: Vec::new(),
1547 base: Box::new(e),
1548 dot_token: token,
1549 member: member,
1550 }.into();
1551 })
1552 |
Michael Layzell734adb42017-06-07 16:58:31 -04001553 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001554 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001555 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001556 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001557 bracket_token: i.0,
1558 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001559 }.into();
1560 })
1561 )) >>
1562 (e)
1563 ));
1564
David Tolnaya454c8f2018-01-07 01:01:10 -08001565 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001566 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001567 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001568 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1569 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001570 |
David Tolnay8c91b882017-12-28 23:04:32 -05001571 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001572 |
Yusuke Sasaki851062f2018-08-01 06:28:28 +09001573 // must be before ExprStruct
1574 call!(unstable_async_block) => { Expr::Verbatim }
1575 |
David Tolnayf7177052018-08-24 15:31:50 -04001576 // must be before ExprStruct
1577 call!(unstable_try_block) => { Expr::Verbatim }
1578 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001579 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001580 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001581 |
David Tolnay8c91b882017-12-28 23:04:32 -05001582 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001583 |
David Tolnay8c91b882017-12-28 23:04:32 -05001584 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001585 |
1586 call!(expr_break, allow_struct) // must be before expr_path
1587 |
David Tolnay8c91b882017-12-28 23:04:32 -05001588 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001589 |
1590 call!(expr_ret, allow_struct) // must be before expr_path
1591 |
David Tolnay8c91b882017-12-28 23:04:32 -05001592 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001593 |
David Tolnay8c91b882017-12-28 23:04:32 -05001594 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001595 |
David Tolnay8c91b882017-12-28 23:04:32 -05001596 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001597 |
David Tolnay8c91b882017-12-28 23:04:32 -05001598 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001599 |
David Tolnay8c91b882017-12-28 23:04:32 -05001600 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001601 |
David Tolnay8c91b882017-12-28 23:04:32 -05001602 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001603 |
David Tolnay8c91b882017-12-28 23:04:32 -05001604 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001605 |
David Tolnay8c91b882017-12-28 23:04:32 -05001606 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001607 |
David Tolnay8c91b882017-12-28 23:04:32 -05001608 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001609 |
David Tolnay8c91b882017-12-28 23:04:32 -05001610 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001611 |
David Tolnay8c91b882017-12-28 23:04:32 -05001612 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001613 |
David Tolnay8c91b882017-12-28 23:04:32 -05001614 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001615 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001616 call!(expr_closure, allow_struct)
1617 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001618 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001619 |
David Tolnay5d08ae62018-08-01 00:08:48 -07001620 call!(unstable_labeled_block) => { Expr::Verbatim }
1621 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001622 // NOTE: This is the prefix-form of range
1623 call!(expr_range, allow_struct)
1624 |
David Tolnay8c91b882017-12-28 23:04:32 -05001625 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001626 |
David Tolnay8c91b882017-12-28 23:04:32 -05001627 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001628 ));
1629
Michael Layzell734adb42017-06-07 16:58:31 -04001630 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001631 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001632 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001633 |
David Tolnay9374bc02018-01-27 18:49:36 -08001634 syn!(ExprParen) => { Expr::Paren }
1635 |
David Tolnay8c91b882017-12-28 23:04:32 -05001636 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001637 ));
1638
Michael Layzell734adb42017-06-07 16:58:31 -04001639 #[cfg(feature = "full")]
David Tolnay313a36f2018-04-29 20:13:04 -07001640 named!(expr_nosemi -> Expr, do_parse!(
1641 nosemi: alt!(
1642 syn!(ExprIf) => { Expr::If }
1643 |
1644 syn!(ExprIfLet) => { Expr::IfLet }
1645 |
1646 syn!(ExprWhile) => { Expr::While }
1647 |
1648 syn!(ExprWhileLet) => { Expr::WhileLet }
1649 |
1650 syn!(ExprForLoop) => { Expr::ForLoop }
1651 |
1652 syn!(ExprLoop) => { Expr::Loop }
1653 |
1654 syn!(ExprMatch) => { Expr::Match }
1655 |
1656 syn!(ExprCatch) => { Expr::Catch }
1657 |
1658 syn!(ExprYield) => { Expr::Yield }
1659 |
1660 syn!(ExprUnsafe) => { Expr::Unsafe }
1661 |
1662 syn!(ExprBlock) => { Expr::Block }
David Tolnay5d08ae62018-08-01 00:08:48 -07001663 |
1664 call!(unstable_labeled_block) => { Expr::Verbatim }
David Tolnay313a36f2018-04-29 20:13:04 -07001665 ) >>
1666 // If the next token is a `.` or a `?` it is special-cased to parse
1667 // as an expression instead of a blockexpression.
1668 not!(punct!(.)) >>
1669 not!(punct!(?)) >>
David Tolnay83ddebb2018-05-05 00:29:13 -07001670 (nosemi)
David Tolnay313a36f2018-04-29 20:13:04 -07001671 ));
Michael Layzell35418782017-06-07 09:20:25 -04001672
David Tolnay8c91b882017-12-28 23:04:32 -05001673 impl Synom for ExprLit {
David Tolnayeb981bb2018-07-21 19:31:38 -07001674 #[cfg(not(feature = "full"))]
1675 named!(parse -> Self, do_parse!(
1676 lit: syn!(Lit) >>
1677 (ExprLit {
1678 attrs: Vec::new(),
1679 lit: lit,
1680 })
1681 ));
1682
1683 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001684 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07001685 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001686 lit: syn!(Lit) >>
1687 (ExprLit {
David Tolnay73c9b522018-07-21 15:58:40 -07001688 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001689 lit: lit,
1690 })
1691 ));
David Tolnay79777332018-01-07 10:04:42 -08001692
1693 fn description() -> Option<&'static str> {
1694 Some("literal")
1695 }
David Tolnay8c91b882017-12-28 23:04:32 -05001696 }
1697
1698 #[cfg(feature = "full")]
1699 impl Synom for ExprMacro {
1700 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001701 attrs: many0!(Attribute::parse_outer) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001702 mac: syn!(Macro) >>
1703 (ExprMacro {
David Tolnay5d314dc2018-07-21 16:40:01 -07001704 attrs: attrs,
David Tolnay8c91b882017-12-28 23:04:32 -05001705 mac: mac,
1706 })
1707 ));
David Tolnay79777332018-01-07 10:04:42 -08001708
1709 fn description() -> Option<&'static str> {
1710 Some("macro invocation expression")
1711 }
David Tolnay8c91b882017-12-28 23:04:32 -05001712 }
1713
David Tolnaye98775f2017-12-28 23:17:00 -05001714 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001715 impl Synom for ExprGroup {
1716 named!(parse -> Self, do_parse!(
1717 e: grouped!(syn!(Expr)) >>
1718 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001719 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001720 expr: Box::new(e.1),
1721 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001722 })
Michael Layzell93c36282017-06-04 20:43:14 -04001723 ));
David Tolnay79777332018-01-07 10:04:42 -08001724
1725 fn description() -> Option<&'static str> {
1726 Some("expression surrounded by invisible delimiters")
1727 }
Michael Layzell93c36282017-06-04 20:43:14 -04001728 }
1729
Alex Crichton954046c2017-05-30 21:49:42 -07001730 impl Synom for ExprParen {
David Tolnayeb981bb2018-07-21 19:31:38 -07001731 #[cfg(not(feature = "full"))]
1732 named!(parse -> Self, do_parse!(
1733 e: parens!(syn!(Expr)) >>
1734 (ExprParen {
1735 attrs: Vec::new(),
1736 paren_token: e.0,
1737 expr: Box::new(e.1),
1738 })
1739 ));
1740
1741 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04001742 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001743 outer_attrs: many0!(Attribute::parse_outer) >>
1744 e: parens!(tuple!(
1745 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001746 syn!(Expr),
David Tolnay5d314dc2018-07-21 16:40:01 -07001747 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001748 (ExprParen {
David Tolnay5d314dc2018-07-21 16:40:01 -07001749 attrs: {
1750 let mut attrs = outer_attrs;
1751 attrs.extend((e.1).0);
1752 attrs
1753 },
David Tolnay8875fca2017-12-31 13:52:37 -05001754 paren_token: e.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001755 expr: Box::new((e.1).1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001756 })
Michael Layzell92639a52017-06-01 00:07:44 -04001757 ));
David Tolnay79777332018-01-07 10:04:42 -08001758
1759 fn description() -> Option<&'static str> {
1760 Some("parenthesized expression")
1761 }
Alex Crichton954046c2017-05-30 21:49:42 -07001762 }
David Tolnay89e05672016-10-02 14:39:42 -07001763
Michael Layzell734adb42017-06-07 16:58:31 -04001764 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001765 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001766 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001767 outer_attrs: many0!(Attribute::parse_outer) >>
1768 elems: brackets!(tuple!(
1769 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001770 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001771 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001772 (ExprArray {
David Tolnay5d314dc2018-07-21 16:40:01 -07001773 attrs: {
1774 let mut attrs = outer_attrs;
1775 attrs.extend((elems.1).0);
1776 attrs
1777 },
David Tolnay8875fca2017-12-31 13:52:37 -05001778 bracket_token: elems.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07001779 elems: (elems.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001780 })
1781 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001782
1783 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001784 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001785 }
Alex Crichton954046c2017-05-30 21:49:42 -07001786 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001787
David Tolnayf2cfd722017-12-31 18:02:51 -05001788 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
David Tolnay76178be2018-07-31 23:06:15 -07001789 parens!(Punctuated::parse_terminated)
1790 );
David Tolnayfa0edf22016-09-23 22:58:24 -07001791
Michael Layzell734adb42017-06-07 16:58:31 -04001792 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001793 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001794 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001795 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001796 turbofish: option!(tuple!(
1797 punct!(::),
1798 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001799 call!(Punctuated::parse_terminated),
David Tolnay0235ba62018-07-21 19:20:50 -07001800 punct!(>),
David Tolnayfa0edf22016-09-23 22:58:24 -07001801 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001802 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001803 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001804 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001805 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001806 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001807 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
hcplaa511792018-05-29 07:13:01 +03001808 tts: TokenStream::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001809 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001810
Alex Crichton954046c2017-05-30 21:49:42 -07001811 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001812 turbofish: turbofish.map(|fish| MethodTurbofish {
1813 colon2_token: fish.0,
1814 lt_token: fish.1,
1815 args: fish.2,
1816 gt_token: fish.3,
1817 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001818 args: args.1,
1819 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001820 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001821 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001822 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001823 ));
1824
Michael Layzell734adb42017-06-07 16:58:31 -04001825 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001826 impl Synom for GenericMethodArgument {
1827 // TODO parse const generics as well
1828 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001829
1830 fn description() -> Option<&'static str> {
1831 Some("generic method argument")
1832 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001833 }
1834
1835 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001836 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001837 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001838 outer_attrs: many0!(Attribute::parse_outer) >>
1839 elems: parens!(tuple!(
1840 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001841 call!(Punctuated::parse_terminated),
David Tolnay5d314dc2018-07-21 16:40:01 -07001842 )) >>
David Tolnay05362582017-12-26 01:33:57 -05001843 (ExprTuple {
David Tolnay5d314dc2018-07-21 16:40:01 -07001844 attrs: {
1845 let mut attrs = outer_attrs;
1846 attrs.extend((elems.1).0);
1847 attrs
1848 },
1849 elems: (elems.1).1,
David Tolnay8875fca2017-12-31 13:52:37 -05001850 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001851 })
1852 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001853
1854 fn description() -> Option<&'static str> {
1855 Some("tuple")
1856 }
Alex Crichton954046c2017-05-30 21:49:42 -07001857 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001858
Michael Layzell734adb42017-06-07 16:58:31 -04001859 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001860 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001861 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001862 if_: keyword!(if) >>
1863 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02001864 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001865 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001866 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001867 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001868 else_block: option!(else_block) >>
1869 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001870 attrs: Vec::new(),
David Tolnay5b5b7d22018-03-31 21:05:00 +02001871 pats: pats,
Michael Layzell92639a52017-06-01 00:07:44 -04001872 let_token: let_,
1873 eq_token: eq,
1874 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001875 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001876 brace_token: then_block.0,
1877 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001878 },
1879 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001880 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001881 })
1882 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001883
1884 fn description() -> Option<&'static str> {
1885 Some("`if let` expression")
1886 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001887 }
1888
Michael Layzell734adb42017-06-07 16:58:31 -04001889 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001890 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001891 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001892 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001893 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001894 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001895 else_block: option!(else_block) >>
1896 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001897 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001898 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001899 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001900 brace_token: then_block.0,
1901 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001902 },
1903 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001904 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001905 })
1906 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001907
1908 fn description() -> Option<&'static str> {
1909 Some("`if` expression")
1910 }
Alex Crichton954046c2017-05-30 21:49:42 -07001911 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001912
Michael Layzell734adb42017-06-07 16:58:31 -04001913 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001914 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001915 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001916 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001917 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001918 |
David Tolnay8c91b882017-12-28 23:04:32 -05001919 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001920 |
1921 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001922 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001923 (Expr::Block(ExprBlock {
1924 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001925 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001926 brace_token: else_block.0,
1927 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001928 },
1929 }))
David Tolnay939766a2016-09-23 23:48:12 -07001930 )
Alex Crichton954046c2017-05-30 21:49:42 -07001931 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001932 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001933 ));
1934
Michael Layzell734adb42017-06-07 16:58:31 -04001935 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001936 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001937 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001938 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001939 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001940 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001941 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001942 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001943 expr: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001944 block: braces!(tuple!(
1945 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001946 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001947 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001948 (ExprForLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001949 attrs: {
1950 let mut attrs = outer_attrs;
1951 attrs.extend((block.1).0);
1952 attrs
1953 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001954 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001955 for_token: for_,
1956 pat: Box::new(pat),
1957 in_token: in_,
1958 expr: Box::new(expr),
1959 body: Block {
1960 brace_token: block.0,
1961 stmts: (block.1).1,
1962 },
Michael Layzell92639a52017-06-01 00:07:44 -04001963 })
1964 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001965
1966 fn description() -> Option<&'static str> {
1967 Some("`for` loop")
1968 }
Alex Crichton954046c2017-05-30 21:49:42 -07001969 }
Gregory Katze5f35682016-09-27 14:20:55 -04001970
Michael Layzell734adb42017-06-07 16:58:31 -04001971 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001972 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001973 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07001974 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001975 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001976 loop_: keyword!(loop) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07001977 block: braces!(tuple!(
1978 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07001979 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07001980 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001981 (ExprLoop {
David Tolnay5d314dc2018-07-21 16:40:01 -07001982 attrs: {
1983 let mut attrs = outer_attrs;
1984 attrs.extend((block.1).0);
1985 attrs
1986 },
David Tolnaybcd498f2017-12-29 12:02:33 -05001987 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07001988 loop_token: loop_,
1989 body: Block {
1990 brace_token: block.0,
1991 stmts: (block.1).1,
1992 },
Michael Layzell92639a52017-06-01 00:07:44 -04001993 })
1994 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001995
1996 fn description() -> Option<&'static str> {
1997 Some("`loop`")
1998 }
Alex Crichton954046c2017-05-30 21:49:42 -07001999 }
2000
Michael Layzell734adb42017-06-07 16:58:31 -04002001 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002002 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002003 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002004 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002005 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002006 obj: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002007 braced_content: braces!(tuple!(
2008 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002009 many0!(syn!(Arm)),
David Tolnay5d314dc2018-07-21 16:40:01 -07002010 )) >>
David Tolnay8875fca2017-12-31 13:52:37 -05002011 (ExprMatch {
David Tolnay5d314dc2018-07-21 16:40:01 -07002012 attrs: {
2013 let mut attrs = outer_attrs;
2014 attrs.extend((braced_content.1).0);
2015 attrs
2016 },
David Tolnay8875fca2017-12-31 13:52:37 -05002017 expr: Box::new(obj),
2018 match_token: match_,
David Tolnay5d314dc2018-07-21 16:40:01 -07002019 brace_token: braced_content.0,
2020 arms: (braced_content.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002021 })
2022 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002023
2024 fn description() -> Option<&'static str> {
2025 Some("`match` expression")
2026 }
Alex Crichton954046c2017-05-30 21:49:42 -07002027 }
David Tolnay1978c672016-10-27 22:05:52 -07002028
Michael Layzell734adb42017-06-07 16:58:31 -04002029 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002030 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04002031 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002032 do_: keyword!(do) >>
2033 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002034 catch_block: syn!(Block) >>
2035 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05002036 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002037 block: catch_block,
2038 do_token: do_,
2039 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002040 })
Michael Layzell92639a52017-06-01 00:07:44 -04002041 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002042
2043 fn description() -> Option<&'static str> {
2044 Some("`catch` expression")
2045 }
Alex Crichton954046c2017-05-30 21:49:42 -07002046 }
Arnavion02ef13f2017-04-25 00:54:31 -07002047
Michael Layzell734adb42017-06-07 16:58:31 -04002048 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002049 impl Synom for ExprYield {
2050 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002051 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07002052 expr: option!(syn!(Expr)) >>
2053 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05002054 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07002055 yield_token: yield_,
2056 expr: expr.map(Box::new),
2057 })
2058 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002059
2060 fn description() -> Option<&'static str> {
2061 Some("`yield` expression")
2062 }
Alex Crichtonfe110462017-06-01 12:49:27 -07002063 }
2064
2065 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002066 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04002067 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002068 attrs: many0!(Attribute::parse_outer) >>
David Tolnay18cc4d42018-03-31 18:47:20 +02002069 leading_vert: option!(punct!(|)) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002070 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002071 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
David Tolnaydfb91432018-03-31 19:19:44 +02002072 fat_arrow: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07002073 body: do_parse!(
2074 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002075 comma: switch!(value!(arm_expr_requires_comma(&expr)),
2076 true => alt!(
2077 input_end!() => { |_| None }
2078 |
2079 punct!(,) => { Some }
2080 )
Alex Crichton03b30272017-08-28 09:35:24 -07002081 |
David Tolnaydc03aec2017-12-30 01:54:18 -05002082 false => option!(punct!(,))
2083 ) >>
2084 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04002085 ) >>
2086 (Arm {
David Tolnaydfb91432018-03-31 19:19:44 +02002087 fat_arrow_token: fat_arrow,
Michael Layzell92639a52017-06-01 00:07:44 -04002088 attrs: attrs,
David Tolnay18cc4d42018-03-31 18:47:20 +02002089 leading_vert: leading_vert,
Michael Layzell92639a52017-06-01 00:07:44 -04002090 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002091 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07002092 body: Box::new(body.0),
2093 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002094 })
2095 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002096
2097 fn description() -> Option<&'static str> {
2098 Some("`match` arm")
2099 }
Alex Crichton954046c2017-05-30 21:49:42 -07002100 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002101
Michael Layzell734adb42017-06-07 16:58:31 -04002102 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002103 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnay7a008ff2018-07-31 22:52:56 -07002104 begin: call!(verbatim::grab_cursor) >>
David Tolnay713a6722018-07-21 15:49:40 -07002105 attrs: many0!(Attribute::parse_outer) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002106 asyncness: option!(keyword!(async)) >>
2107 movability: option!(cond_reduce!(asyncness.is_none(), keyword!(static))) >>
David Tolnayefc96fb2017-12-29 02:03:15 -05002108 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002109 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002110 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002111 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07002112 ret_and_body: alt!(
2113 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002114 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002115 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002116 body: syn!(Block) >>
David Tolnay76178be2018-07-31 23:06:15 -07002117 (
2118 ReturnType::Type(arrow, Box::new(ty)),
2119 Expr::Block(ExprBlock {
2120 attrs: Vec::new(),
2121 block: body,
2122 },
2123 ))
David Tolnay89e05672016-10-02 14:39:42 -07002124 )
2125 |
David Tolnayf93b90d2017-11-11 19:21:26 -08002126 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07002127 ) >>
David Tolnay9be32582018-07-31 22:37:26 -07002128 end: call!(verbatim::grab_cursor) >>
David Tolnay7a008ff2018-07-31 22:52:56 -07002129 ({
2130 if asyncness.is_some() {
2131 // TODO: include asyncness in ExprClosure
2132 // https://github.com/dtolnay/syn/issues/396
2133 Expr::Verbatim(ExprVerbatim {
2134 tts: verbatim::token_range(begin..end),
2135 })
2136 } else {
2137 Expr::Closure(ExprClosure {
2138 attrs: attrs,
2139 movability: movability,
2140 capture: capture,
2141 or1_token: or1,
2142 inputs: inputs,
2143 or2_token: or2,
2144 output: ret_and_body.0,
2145 body: Box::new(ret_and_body.1),
2146 })
2147 }
Yusuke Sasaki2dec3152018-07-31 20:41:50 +09002148 })
2149 ));
2150
2151 #[cfg(feature = "full")]
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002152 named!(unstable_async_block -> ExprVerbatim, do_parse!(
David Tolnay9be32582018-07-31 22:37:26 -07002153 begin: call!(verbatim::grab_cursor) >>
David Tolnay5757f452018-07-31 22:53:40 -07002154 many0!(Attribute::parse_outer) >>
2155 keyword!(async) >>
2156 option!(keyword!(move)) >>
2157 syn!(Block) >>
David Tolnay9be32582018-07-31 22:37:26 -07002158 end: call!(verbatim::grab_cursor) >>
2159 (ExprVerbatim {
2160 tts: verbatim::token_range(begin..end),
Yusuke Sasaki851062f2018-08-01 06:28:28 +09002161 })
2162 ));
2163
2164 #[cfg(feature = "full")]
David Tolnayf7177052018-08-24 15:31:50 -04002165 named!(unstable_try_block -> ExprVerbatim, do_parse!(
2166 begin: call!(verbatim::grab_cursor) >>
2167 many0!(Attribute::parse_outer) >>
2168 keyword!(try) >>
2169 syn!(Block) >>
2170 end: call!(verbatim::grab_cursor) >>
2171 (ExprVerbatim {
2172 tts: verbatim::token_range(begin..end),
2173 })
2174 ));
2175
2176 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002177 named!(fn_arg -> FnArg, do_parse!(
2178 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002179 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002180 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05002181 if let Some((colon, ty)) = ty {
2182 FnArg::Captured(ArgCaptured {
2183 pat: pat,
2184 colon_token: colon,
2185 ty: ty,
2186 })
2187 } else {
2188 FnArg::Inferred(pat)
2189 }
David Tolnaybb6feae2016-10-02 21:25:20 -07002190 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04002191 ));
2192
Michael Layzell734adb42017-06-07 16:58:31 -04002193 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002194 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04002195 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002196 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002197 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002198 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002199 cond: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002200 block: braces!(tuple!(
2201 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002202 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002203 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002204 (ExprWhile {
David Tolnay5d314dc2018-07-21 16:40:01 -07002205 attrs: {
2206 let mut attrs = outer_attrs;
2207 attrs.extend((block.1).0);
2208 attrs
2209 },
2210 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002211 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04002212 cond: Box::new(cond),
David Tolnay5d314dc2018-07-21 16:40:01 -07002213 body: Block {
2214 brace_token: block.0,
2215 stmts: (block.1).1,
2216 },
Michael Layzell92639a52017-06-01 00:07:44 -04002217 })
2218 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002219
2220 fn description() -> Option<&'static str> {
2221 Some("`while` expression")
2222 }
Alex Crichton954046c2017-05-30 21:49:42 -07002223 }
2224
Michael Layzell734adb42017-06-07 16:58:31 -04002225 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002226 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04002227 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002228 outer_attrs: many0!(Attribute::parse_outer) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002229 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002230 while_: keyword!(while) >>
2231 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002232 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002233 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002234 value: expr_no_struct >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002235 block: braces!(tuple!(
2236 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002237 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002238 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002239 (ExprWhileLet {
David Tolnay5d314dc2018-07-21 16:40:01 -07002240 attrs: {
2241 let mut attrs = outer_attrs;
2242 attrs.extend((block.1).0);
2243 attrs
2244 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002245 label: label,
David Tolnay5d314dc2018-07-21 16:40:01 -07002246 while_token: while_,
2247 let_token: let_,
2248 pats: pats,
2249 eq_token: eq,
2250 expr: Box::new(value),
2251 body: Block {
2252 brace_token: block.0,
2253 stmts: (block.1).1,
2254 },
David Tolnaybcd498f2017-12-29 12:02:33 -05002255 })
2256 ));
David Tolnay79777332018-01-07 10:04:42 -08002257
2258 fn description() -> Option<&'static str> {
2259 Some("`while let` expression")
2260 }
David Tolnaybcd498f2017-12-29 12:02:33 -05002261 }
2262
2263 #[cfg(feature = "full")]
2264 impl Synom for Label {
2265 named!(parse -> Self, do_parse!(
2266 name: syn!(Lifetime) >>
2267 colon: punct!(:) >>
2268 (Label {
2269 name: name,
2270 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04002271 })
2272 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002273
2274 fn description() -> Option<&'static str> {
2275 Some("`while let` expression")
2276 }
Alex Crichton954046c2017-05-30 21:49:42 -07002277 }
2278
Michael Layzell734adb42017-06-07 16:58:31 -04002279 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002280 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04002281 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002282 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002283 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002284 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002285 (ExprContinue {
David Tolnay5d314dc2018-07-21 16:40:01 -07002286 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002287 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05002288 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04002289 })
2290 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002291
2292 fn description() -> Option<&'static str> {
2293 Some("`continue`")
2294 }
Alex Crichton954046c2017-05-30 21:49:42 -07002295 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04002296
Michael Layzell734adb42017-06-07 16:58:31 -04002297 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002298 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002299 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002300 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05002301 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002302 // We can't allow blocks after a `break` expression when we wouldn't
2303 // allow structs, as this expression is ambiguous.
2304 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002305 (ExprBreak {
David Tolnay5d314dc2018-07-21 16:40:01 -07002306 attrs: attrs,
David Tolnaybcd498f2017-12-29 12:02:33 -05002307 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002308 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002309 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002310 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04002311 ));
2312
Michael Layzell734adb42017-06-07 16:58:31 -04002313 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002314 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002315 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002316 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002317 // NOTE: return is greedy and eats blocks after it even when in a
2318 // position where structs are not allowed, such as in if statement
2319 // conditions. For example:
2320 //
David Tolnaybcf26022017-12-25 22:10:52 -05002321 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07002322 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05002323 (ExprReturn {
David Tolnay5d314dc2018-07-21 16:40:01 -07002324 attrs: attrs,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002325 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07002326 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002327 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07002328 ));
2329
Michael Layzell734adb42017-06-07 16:58:31 -04002330 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002331 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002332 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002333 outer_attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002334 path: syn!(Path) >>
2335 data: braces!(do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002336 inner_attrs: many0!(Attribute::parse_inner) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05002337 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002338 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
2339 dots: punct!(..) >>
2340 base: syn!(Expr) >>
2341 (dots, base)
2342 ))) >>
David Tolnay5d314dc2018-07-21 16:40:01 -07002343 (inner_attrs, fields, base)
Michael Layzell92639a52017-06-01 00:07:44 -04002344 )) >>
2345 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002346 let (brace, (inner_attrs, fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002347 let (dots, rest) = match base.and_then(|b| b) {
2348 Some((dots, base)) => (Some(dots), Some(base)),
2349 None => (None, None),
2350 };
2351 ExprStruct {
David Tolnay5d314dc2018-07-21 16:40:01 -07002352 attrs: {
2353 let mut attrs = outer_attrs;
2354 attrs.extend(inner_attrs);
2355 attrs
2356 },
Michael Layzell92639a52017-06-01 00:07:44 -04002357 brace_token: brace,
2358 path: path,
2359 fields: fields,
2360 dot2_token: dots,
2361 rest: rest.map(Box::new),
2362 }
2363 })
2364 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002365
2366 fn description() -> Option<&'static str> {
2367 Some("struct literal expression")
2368 }
Alex Crichton954046c2017-05-30 21:49:42 -07002369 }
2370
Michael Layzell734adb42017-06-07 16:58:31 -04002371 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002372 impl Synom for FieldValue {
David Tolnayc42b90a2018-01-18 23:11:37 -08002373 named!(parse -> Self, do_parse!(
2374 attrs: many0!(Attribute::parse_outer) >>
2375 field_value: alt!(
2376 tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr))
2377 |
2378 map!(syn!(Ident), |name| (
Alex Crichtona74a1c82018-05-16 10:20:44 -07002379 Member::Named(name.clone()),
David Tolnayc42b90a2018-01-18 23:11:37 -08002380 None,
2381 Expr::Path(ExprPath {
2382 attrs: Vec::new(),
2383 qself: None,
2384 path: name.into(),
2385 }),
2386 ))
2387 ) >>
2388 (FieldValue {
2389 attrs: attrs,
2390 member: field_value.0,
2391 colon_token: field_value.1,
2392 expr: field_value.2,
Michael Layzell92639a52017-06-01 00:07:44 -04002393 })
2394 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002395
2396 fn description() -> Option<&'static str> {
2397 Some("field-value pair: `field: value`")
2398 }
Alex Crichton954046c2017-05-30 21:49:42 -07002399 }
David Tolnay055a7042016-10-02 19:23:54 -07002400
Michael Layzell734adb42017-06-07 16:58:31 -04002401 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002402 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002403 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002404 outer_attrs: many0!(Attribute::parse_outer) >>
2405 data: brackets!(tuple!(
2406 many0!(Attribute::parse_inner),
2407 syn!(Expr),
2408 punct!(;),
David Tolnay0235ba62018-07-21 19:20:50 -07002409 syn!(Expr),
Michael Layzell92639a52017-06-01 00:07:44 -04002410 )) >>
2411 (ExprRepeat {
David Tolnay5d314dc2018-07-21 16:40:01 -07002412 attrs: {
2413 let mut attrs = outer_attrs;
2414 attrs.extend((data.1).0);
2415 attrs
2416 },
2417 expr: Box::new((data.1).1),
2418 len: Box::new((data.1).3),
David Tolnay8875fca2017-12-31 13:52:37 -05002419 bracket_token: data.0,
David Tolnay5d314dc2018-07-21 16:40:01 -07002420 semi_token: (data.1).2,
Michael Layzell92639a52017-06-01 00:07:44 -04002421 })
2422 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002423
2424 fn description() -> Option<&'static str> {
2425 Some("repeated array literal: `[val; N]`")
2426 }
Alex Crichton954046c2017-05-30 21:49:42 -07002427 }
David Tolnay055a7042016-10-02 19:23:54 -07002428
Michael Layzell734adb42017-06-07 16:58:31 -04002429 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002430 impl Synom for ExprUnsafe {
2431 named!(parse -> Self, do_parse!(
David Tolnayc4be3512018-08-27 06:25:44 -07002432 outer_attrs: many0!(Attribute::parse_outer) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002433 unsafe_: keyword!(unsafe) >>
David Tolnayc4be3512018-08-27 06:25:44 -07002434 block: braces!(tuple!(
2435 many0!(Attribute::parse_inner),
2436 call!(Block::parse_within),
2437 )) >>
Nika Layzell640832a2017-12-04 13:37:09 -05002438 (ExprUnsafe {
David Tolnayc4be3512018-08-27 06:25:44 -07002439 attrs: {
2440 let mut attrs = outer_attrs;
2441 attrs.extend((block.1).0);
2442 attrs
2443 },
Nika Layzell640832a2017-12-04 13:37:09 -05002444 unsafe_token: unsafe_,
David Tolnayc4be3512018-08-27 06:25:44 -07002445 block: Block {
2446 brace_token: block.0,
2447 stmts: (block.1).1,
2448 },
Nika Layzell640832a2017-12-04 13:37:09 -05002449 })
2450 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002451
2452 fn description() -> Option<&'static str> {
2453 Some("unsafe block: `unsafe { .. }`")
2454 }
Nika Layzell640832a2017-12-04 13:37:09 -05002455 }
2456
2457 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002458 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002459 named!(parse -> Self, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002460 outer_attrs: many0!(Attribute::parse_outer) >>
2461 block: braces!(tuple!(
2462 many0!(Attribute::parse_inner),
David Tolnay0235ba62018-07-21 19:20:50 -07002463 call!(Block::parse_within),
David Tolnay5d314dc2018-07-21 16:40:01 -07002464 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002465 (ExprBlock {
David Tolnay5d314dc2018-07-21 16:40:01 -07002466 attrs: {
2467 let mut attrs = outer_attrs;
2468 attrs.extend((block.1).0);
2469 attrs
2470 },
2471 block: Block {
2472 brace_token: block.0,
2473 stmts: (block.1).1,
2474 },
Michael Layzell92639a52017-06-01 00:07:44 -04002475 })
2476 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002477
2478 fn description() -> Option<&'static str> {
2479 Some("block: `{ .. }`")
2480 }
Alex Crichton954046c2017-05-30 21:49:42 -07002481 }
David Tolnay89e05672016-10-02 14:39:42 -07002482
Michael Layzell734adb42017-06-07 16:58:31 -04002483 #[cfg(feature = "full")]
David Tolnay5d08ae62018-08-01 00:08:48 -07002484 named!(unstable_labeled_block -> ExprVerbatim, do_parse!(
2485 begin: call!(verbatim::grab_cursor) >>
2486 many0!(Attribute::parse_outer) >>
David Tolnay61e15e52018-08-01 00:28:36 -07002487 syn!(Label) >>
David Tolnay5d08ae62018-08-01 00:08:48 -07002488 braces!(tuple!(
2489 many0!(Attribute::parse_inner),
2490 call!(Block::parse_within),
2491 )) >>
2492 end: call!(verbatim::grab_cursor) >>
2493 (ExprVerbatim {
2494 tts: verbatim::token_range(begin..end),
2495 })
2496 ));
2497
2498 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002499 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002500 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002501 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002502 (ExprRange {
2503 attrs: Vec::new(),
2504 from: None,
2505 to: hi.map(Box::new),
2506 limits: limits,
2507 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002508 ));
2509
Michael Layzell734adb42017-06-07 16:58:31 -04002510 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002511 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002512 named!(parse -> Self, alt!(
2513 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002514 punct!(..=) => { RangeLimits::Closed }
2515 |
2516 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002517 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002518 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002519 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002520 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002521
2522 fn description() -> Option<&'static str> {
2523 Some("range limit: `..`, `...` or `..=`")
2524 }
Alex Crichton954046c2017-05-30 21:49:42 -07002525 }
David Tolnay438c9052016-10-07 23:24:48 -07002526
Alex Crichton954046c2017-05-30 21:49:42 -07002527 impl Synom for ExprPath {
David Tolnayeb981bb2018-07-21 19:31:38 -07002528 #[cfg(not(feature = "full"))]
2529 named!(parse -> Self, do_parse!(
2530 pair: qpath >>
2531 (ExprPath {
2532 attrs: Vec::new(),
2533 qself: pair.0,
2534 path: pair.1,
2535 })
2536 ));
2537
2538 #[cfg(feature = "full")]
Michael Layzell92639a52017-06-01 00:07:44 -04002539 named!(parse -> Self, do_parse!(
David Tolnay73c9b522018-07-21 15:58:40 -07002540 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002541 pair: qpath >>
2542 (ExprPath {
David Tolnay73c9b522018-07-21 15:58:40 -07002543 attrs: attrs,
Michael Layzell92639a52017-06-01 00:07:44 -04002544 qself: pair.0,
2545 path: pair.1,
2546 })
2547 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002548
2549 fn description() -> Option<&'static str> {
2550 Some("path: `a::b::c`")
2551 }
Alex Crichton954046c2017-05-30 21:49:42 -07002552 }
David Tolnay42602292016-10-01 22:25:45 -07002553
David Tolnay9cc2f092018-08-24 15:51:37 -04002554 named!(path -> Path, do_parse!(
2555 colon: option!(punct!(::)) >>
2556 segments: call!(Punctuated::<_, Token![::]>::parse_separated_nonempty_with, path_segment) >>
2557 cond_reduce!(segments.first().map_or(true, |seg| seg.value().ident != "dyn")) >>
2558 (Path {
2559 leading_colon: colon,
2560 segments: segments,
2561 })
2562 ));
2563
2564 named!(path_segment -> PathSegment, alt!(
2565 do_parse!(
2566 ident: syn!(Ident) >>
2567 colon2: punct!(::) >>
2568 lt: punct!(<) >>
2569 args: call!(Punctuated::parse_terminated) >>
2570 gt: punct!(>) >>
2571 (PathSegment {
2572 ident: ident,
2573 arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {
2574 colon2_token: Some(colon2),
2575 lt_token: lt,
2576 args: args,
2577 gt_token: gt,
2578 }),
2579 })
2580 )
2581 |
2582 mod_style_path_segment
2583 ));
2584
2585 named!(qpath -> (Option<QSelf>, Path), alt!(
2586 map!(path, |p| (None, p))
2587 |
2588 do_parse!(
2589 lt: punct!(<) >>
2590 this: syn!(Type) >>
2591 path: option!(tuple!(keyword!(as), syn!(Path))) >>
2592 gt: punct!(>) >>
2593 colon2: punct!(::) >>
2594 rest: call!(Punctuated::parse_separated_nonempty_with, path_segment) >>
2595 ({
2596 let (pos, as_, path) = match path {
2597 Some((as_, mut path)) => {
2598 let pos = path.segments.len();
2599 path.segments.push_punct(colon2);
2600 path.segments.extend(rest.into_pairs());
2601 (pos, Some(as_), path)
2602 }
2603 None => {
2604 (0, None, Path {
2605 leading_colon: Some(colon2),
2606 segments: rest,
2607 })
2608 }
2609 };
2610 (Some(QSelf {
2611 lt_token: lt,
2612 ty: Box::new(this),
2613 position: pos,
2614 as_token: as_,
2615 gt_token: gt,
2616 }), path)
2617 })
2618 )
2619 |
2620 map!(keyword!(self), |s| (None, s.into()))
2621 ));
2622
David Tolnay85b69a42017-12-27 20:43:10 -05002623 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002624
David Tolnay8875fca2017-12-31 13:52:37 -05002625 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002626
Michael Layzell734adb42017-06-07 16:58:31 -04002627 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002628 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002629 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002630 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002631 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002632 brace_token: stmts.0,
2633 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002634 })
2635 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002636
2637 fn description() -> Option<&'static str> {
2638 Some("block: `{ .. }`")
2639 }
Alex Crichton954046c2017-05-30 21:49:42 -07002640 }
David Tolnay939766a2016-09-23 23:48:12 -07002641
Michael Layzell734adb42017-06-07 16:58:31 -04002642 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002643 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002644 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002645 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002646 mut standalone: many0!(do_parse!(
2647 stmt: syn!(Stmt) >>
2648 many0!(punct!(;)) >>
2649 (stmt)
2650 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002651 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002652 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002653 mut e: syn!(Expr) >>
2654 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002655 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002656 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002657 })
2658 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002659 (match last {
2660 None => standalone,
2661 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002662 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002663 standalone
2664 }
2665 })
2666 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002667 }
2668
Michael Layzell734adb42017-06-07 16:58:31 -04002669 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002670 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002671 named!(parse -> Self, alt!(
2672 stmt_mac
2673 |
2674 stmt_local
2675 |
2676 stmt_item
2677 |
Michael Layzell35418782017-06-07 09:20:25 -04002678 stmt_blockexpr
2679 |
Michael Layzell92639a52017-06-01 00:07:44 -04002680 stmt_expr
2681 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002682
2683 fn description() -> Option<&'static str> {
2684 Some("statement")
2685 }
Alex Crichton954046c2017-05-30 21:49:42 -07002686 }
David Tolnay939766a2016-09-23 23:48:12 -07002687
Michael Layzell734adb42017-06-07 16:58:31 -04002688 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002689 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002690 attrs: many0!(Attribute::parse_outer) >>
David Tolnayd69fc2b2018-01-23 09:39:14 -08002691 what: call!(Path::parse_mod_style) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002692 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002693 // Only parse braces here; paren and bracket will get parsed as
2694 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002695 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002696 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002697 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002698 attrs: attrs,
2699 ident: None,
2700 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002701 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002702 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002703 delimiter: MacroDelimiter::Brace(data.0),
2704 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002705 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002706 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002707 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002708 ));
2709
Michael Layzell734adb42017-06-07 16:58:31 -04002710 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002711 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002712 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002713 let_: keyword!(let) >>
David Tolnay5b5b7d22018-03-31 21:05:00 +02002714 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002715 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002716 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2717 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002718 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002719 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002720 let_token: let_,
David Tolnay5b5b7d22018-03-31 21:05:00 +02002721 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05002722 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2723 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2724 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002725 }))
David Tolnay191e0582016-10-02 18:31:09 -07002726 ));
2727
Michael Layzell734adb42017-06-07 16:58:31 -04002728 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002729 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002730
Michael Layzell734adb42017-06-07 16:58:31 -04002731 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002732 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002733 mut attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002734 mut e: expr_nosemi >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002735 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002736 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002737 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002738 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002739 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002740 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002741 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002742 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002743 }
2744 })
2745 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002746
Michael Layzell734adb42017-06-07 16:58:31 -04002747 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002748 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay5d314dc2018-07-21 16:40:01 -07002749 mut attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002750 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002751 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002752 ({
David Tolnay5d314dc2018-07-21 16:40:01 -07002753 attrs.extend(e.replace_attrs(Vec::new()));
David Tolnay2ae520a2017-12-29 11:19:50 -05002754 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002755 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002756 })
David Tolnay939766a2016-09-23 23:48:12 -07002757 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002758
Michael Layzell734adb42017-06-07 16:58:31 -04002759 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002760 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002761 named!(parse -> Self, alt!(
2762 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2763 |
2764 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2765 |
2766 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2767 |
2768 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2769 |
2770 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2771 |
David Tolnay323279a2017-12-29 11:26:32 -05002772 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002773 |
2774 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2775 |
2776 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2777 |
2778 syn!(PatPath) => { Pat::Path }
2779 |
2780 syn!(PatTuple) => { Pat::Tuple }
2781 |
2782 syn!(PatRef) => { Pat::Ref }
2783 |
2784 syn!(PatSlice) => { Pat::Slice }
2785 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002786
2787 fn description() -> Option<&'static str> {
2788 Some("pattern")
2789 }
Alex Crichton954046c2017-05-30 21:49:42 -07002790 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002791
Michael Layzell734adb42017-06-07 16:58:31 -04002792 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002793 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002794 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002795 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002796 |u| PatWild { underscore_token: u }
2797 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002798
2799 fn description() -> Option<&'static str> {
2800 Some("wild pattern: `_`")
2801 }
Alex Crichton954046c2017-05-30 21:49:42 -07002802 }
David Tolnay84aa0752016-10-02 23:01:13 -07002803
Michael Layzell734adb42017-06-07 16:58:31 -04002804 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002805 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002806 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002807 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002808 pat: syn!(Pat) >>
2809 (PatBox {
2810 pat: Box::new(pat),
2811 box_token: boxed,
2812 })
2813 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002814
2815 fn description() -> Option<&'static str> {
2816 Some("box pattern")
2817 }
Alex Crichton954046c2017-05-30 21:49:42 -07002818 }
2819
Michael Layzell734adb42017-06-07 16:58:31 -04002820 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002821 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002822 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002823 by_ref: option!(keyword!(ref)) >>
2824 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002825 name: alt!(
2826 syn!(Ident)
2827 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002828 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002829 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002830 not!(punct!(<)) >>
2831 not!(punct!(::)) >>
2832 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002833 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002834 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002835 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002836 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002837 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002838 })
2839 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002840
2841 fn description() -> Option<&'static str> {
2842 Some("pattern identifier binding")
2843 }
Alex Crichton954046c2017-05-30 21:49:42 -07002844 }
2845
Michael Layzell734adb42017-06-07 16:58:31 -04002846 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002847 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002848 named!(parse -> Self, do_parse!(
2849 path: syn!(Path) >>
2850 tuple: syn!(PatTuple) >>
2851 (PatTupleStruct {
2852 path: path,
2853 pat: tuple,
2854 })
2855 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002856
2857 fn description() -> Option<&'static str> {
2858 Some("tuple struct pattern")
2859 }
Alex Crichton954046c2017-05-30 21:49:42 -07002860 }
2861
Michael Layzell734adb42017-06-07 16:58:31 -04002862 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002863 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002864 named!(parse -> Self, do_parse!(
2865 path: syn!(Path) >>
2866 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002867 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002868 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002869 (fields, base)
2870 )) >>
2871 (PatStruct {
2872 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002873 fields: (data.1).0,
2874 brace_token: data.0,
2875 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002876 })
2877 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002878
2879 fn description() -> Option<&'static str> {
2880 Some("struct pattern")
2881 }
Alex Crichton954046c2017-05-30 21:49:42 -07002882 }
2883
Michael Layzell734adb42017-06-07 16:58:31 -04002884 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002885 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002886 named!(parse -> Self, alt!(
2887 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002888 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002889 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002890 pat: syn!(Pat) >>
2891 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002892 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002893 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002894 attrs: Vec::new(),
2895 colon_token: Some(colon),
2896 })
2897 )
2898 |
2899 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002900 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002901 by_ref: option!(keyword!(ref)) >>
2902 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002903 ident: syn!(Ident) >>
2904 ({
2905 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002906 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002907 mutability: mutability,
Alex Crichtona74a1c82018-05-16 10:20:44 -07002908 ident: ident.clone(),
Michael Layzell92639a52017-06-01 00:07:44 -04002909 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002910 }.into();
2911 if let Some(boxed) = boxed {
2912 pat = PatBox {
2913 pat: Box::new(pat),
2914 box_token: boxed,
2915 }.into();
2916 }
2917 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002918 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002919 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002920 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002921 colon_token: None,
2922 }
2923 })
2924 )
2925 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002926
2927 fn description() -> Option<&'static str> {
2928 Some("field pattern")
2929 }
Alex Crichton954046c2017-05-30 21:49:42 -07002930 }
2931
David Tolnay85b69a42017-12-27 20:43:10 -05002932 impl Synom for Member {
2933 named!(parse -> Self, alt!(
2934 syn!(Ident) => { Member::Named }
2935 |
2936 syn!(Index) => { Member::Unnamed }
2937 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002938
2939 fn description() -> Option<&'static str> {
2940 Some("field member")
2941 }
David Tolnay85b69a42017-12-27 20:43:10 -05002942 }
2943
David Tolnay85b69a42017-12-27 20:43:10 -05002944 impl Synom for Index {
2945 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002946 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002947 ({
David Tolnay360efd22018-01-04 23:35:26 -08002948 if let IntSuffix::None = lit.suffix() {
Alex Crichton9a4dca22018-03-28 06:32:19 -07002949 Index { index: lit.value() as u32, span: lit.span() }
Alex Crichton954046c2017-05-30 21:49:42 -07002950 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002951 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002952 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002953 })
David Tolnay85b69a42017-12-27 20:43:10 -05002954 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002955
2956 fn description() -> Option<&'static str> {
2957 Some("field index")
2958 }
David Tolnay85b69a42017-12-27 20:43:10 -05002959 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002960
Michael Layzell734adb42017-06-07 16:58:31 -04002961 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002962 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002963 named!(parse -> Self, map!(
2964 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002965 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002966 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002967
2968 fn description() -> Option<&'static str> {
2969 Some("path pattern")
2970 }
Alex Crichton954046c2017-05-30 21:49:42 -07002971 }
David Tolnay9636c052016-10-02 17:11:17 -07002972
Michael Layzell734adb42017-06-07 16:58:31 -04002973 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002974 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002975 named!(parse -> Self, do_parse!(
2976 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002977 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002978 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2979 tuple!(punct!(..), option!(punct!(,)))
2980 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002981 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002982 Some((_, Some(_))) => true,
2983 _ => false,
2984 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002985 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002986 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002987 )) >>
2988 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002989 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002990 let (dotdot, trailing) = match dotdot {
2991 Some((a, b)) => (Some(a), Some(b)),
2992 None => (None, None),
2993 };
2994 PatTuple {
2995 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002996 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002997 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002998 comma_token: trailing.unwrap_or_default(),
2999 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04003000 }
3001 })
3002 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003003
3004 fn description() -> Option<&'static str> {
3005 Some("tuple pattern")
3006 }
Alex Crichton954046c2017-05-30 21:49:42 -07003007 }
David Tolnayfbb73232016-10-03 01:00:06 -07003008
Michael Layzell734adb42017-06-07 16:58:31 -04003009 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003010 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04003011 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003012 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05003013 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003014 pat: syn!(Pat) >>
3015 (PatRef {
3016 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05003017 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04003018 and_token: and,
3019 })
3020 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003021
3022 fn description() -> Option<&'static str> {
3023 Some("reference pattern")
3024 }
Alex Crichton954046c2017-05-30 21:49:42 -07003025 }
David Tolnayffdb97f2016-10-03 01:28:33 -07003026
Michael Layzell734adb42017-06-07 16:58:31 -04003027 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003028 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04003029 named!(parse -> Self, do_parse!(
3030 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05003031 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04003032 return parse_error(); // these need to be parsed by pat_path
3033 } else {
3034 PatLit {
3035 expr: Box::new(lit),
3036 }
3037 })
3038 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003039
3040 fn description() -> Option<&'static str> {
3041 Some("literal pattern")
3042 }
Alex Crichton954046c2017-05-30 21:49:42 -07003043 }
David Tolnaye1310902016-10-29 23:40:00 -07003044
Michael Layzell734adb42017-06-07 16:58:31 -04003045 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003046 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04003047 named!(parse -> Self, do_parse!(
3048 lo: pat_lit_expr >>
3049 limits: syn!(RangeLimits) >>
3050 hi: pat_lit_expr >>
3051 (PatRange {
3052 lo: Box::new(lo),
3053 hi: Box::new(hi),
3054 limits: limits,
3055 })
3056 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003057
3058 fn description() -> Option<&'static str> {
3059 Some("range pattern")
3060 }
Alex Crichton954046c2017-05-30 21:49:42 -07003061 }
David Tolnaye1310902016-10-29 23:40:00 -07003062
Michael Layzell734adb42017-06-07 16:58:31 -04003063 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07003064 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003065 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07003066 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05003067 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07003068 |
David Tolnay8c91b882017-12-28 23:04:32 -05003069 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07003070 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05003071 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05003072 Expr::Unary(ExprUnary {
3073 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05003074 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05003075 expr: Box::new(v)
3076 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003077 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05003078 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07003079 })
3080 ));
David Tolnay8b308c22016-10-03 01:24:10 -07003081
Michael Layzell734adb42017-06-07 16:58:31 -04003082 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07003083 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04003084 named!(parse -> Self, map!(
3085 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05003086 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003087 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08003088 dots: punct!(..) >>
3089 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04003090 (dots, trailing)
3091 )) >>
3092 after: cond!(
3093 match middle {
3094 Some((_, ref trailing)) => trailing.is_some(),
3095 _ => false,
3096 },
David Tolnayf2cfd722017-12-31 18:02:51 -05003097 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04003098 ) >>
3099 (before, middle, after)
3100 )),
David Tolnay8875fca2017-12-31 13:52:37 -05003101 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05003102 let mut before: Punctuated<Pat, Token![,]> = before;
3103 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08003104 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04003105 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003106 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04003107 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003108 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04003109 }),
3110 bracket_token: brackets,
3111 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05003112 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04003113 None
David Tolnaydc03aec2017-12-30 01:54:18 -05003114 } else {
David Tolnay56080682018-01-06 14:01:52 -08003115 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04003116 }
3117 }),
3118 front: before,
3119 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07003120 }
Alex Crichton954046c2017-05-30 21:49:42 -07003121 }
Michael Layzell92639a52017-06-01 00:07:44 -04003122 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003123
3124 fn description() -> Option<&'static str> {
3125 Some("slice pattern")
3126 }
Alex Crichton954046c2017-05-30 21:49:42 -07003127 }
David Tolnay323279a2017-12-29 11:26:32 -05003128
3129 #[cfg(feature = "full")]
3130 impl Synom for PatMacro {
3131 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08003132
3133 fn description() -> Option<&'static str> {
3134 Some("macro pattern")
3135 }
David Tolnay323279a2017-12-29 11:26:32 -05003136 }
David Tolnayb9c8e322016-09-23 20:48:37 -07003137}
3138
David Tolnayf4bbbd92016-09-23 14:41:55 -07003139#[cfg(feature = "printing")]
3140mod printing {
3141 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04003142 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07003143 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -07003144 use proc_macro2::{Literal, TokenStream};
3145 use quote::{ToTokens, TokenStreamExt};
David Tolnayf4bbbd92016-09-23 14:41:55 -07003146
David Tolnaybcf26022017-12-25 22:10:52 -05003147 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
David Tolnayb6a0e7d2018-05-20 22:06:47 -07003148 // before appending it to `TokenStream`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04003149 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003150 fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05003151 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05003152 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003153 e.to_tokens(tokens);
3154 });
3155 } else {
3156 e.to_tokens(tokens);
3157 }
3158 }
3159
David Tolnay8c91b882017-12-28 23:04:32 -05003160 #[cfg(feature = "full")]
David Tolnayd997aef2018-07-21 18:42:31 -07003161 fn outer_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
David Tolnay8c91b882017-12-28 23:04:32 -05003162 tokens.append_all(attrs.outer());
3163 }
Michael Layzell734adb42017-06-07 16:58:31 -04003164
David Tolnayd997aef2018-07-21 18:42:31 -07003165 #[cfg(feature = "full")]
3166 fn inner_attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) {
3167 tokens.append_all(attrs.inner());
3168 }
3169
David Tolnay8c91b882017-12-28 23:04:32 -05003170 #[cfg(not(feature = "full"))]
David Tolnayd997aef2018-07-21 18:42:31 -07003171 fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
3172
3173 #[cfg(not(feature = "full"))]
3174 fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07003175
Michael Layzell734adb42017-06-07 16:58:31 -04003176 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003177 impl ToTokens for ExprBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003178 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003179 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003180 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003181 self.expr.to_tokens(tokens);
3182 }
3183 }
3184
Michael Layzell734adb42017-06-07 16:58:31 -04003185 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003186 impl ToTokens for ExprInPlace {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003187 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003188 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8701a5c2017-12-28 23:31:10 -05003189 self.place.to_tokens(tokens);
3190 self.arrow_token.to_tokens(tokens);
3191 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003192 }
3193 }
3194
Michael Layzell734adb42017-06-07 16:58:31 -04003195 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003196 impl ToTokens for ExprArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003197 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003198 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003199 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003200 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003201 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003202 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003203 }
3204 }
3205
3206 impl ToTokens for ExprCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003207 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003208 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003209 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003210 self.paren_token.surround(tokens, |tokens| {
3211 self.args.to_tokens(tokens);
3212 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003213 }
3214 }
3215
Michael Layzell734adb42017-06-07 16:58:31 -04003216 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003217 impl ToTokens for ExprMethodCall {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003218 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003219 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay76418512017-12-28 23:47:47 -05003220 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003221 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003222 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05003223 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003224 self.paren_token.surround(tokens, |tokens| {
3225 self.args.to_tokens(tokens);
3226 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003227 }
3228 }
3229
Michael Layzell734adb42017-06-07 16:58:31 -04003230 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05003231 impl ToTokens for MethodTurbofish {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003232 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003233 self.colon2_token.to_tokens(tokens);
3234 self.lt_token.to_tokens(tokens);
3235 self.args.to_tokens(tokens);
3236 self.gt_token.to_tokens(tokens);
3237 }
3238 }
3239
3240 #[cfg(feature = "full")]
3241 impl ToTokens for GenericMethodArgument {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003242 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd60cfec2017-12-29 00:21:38 -05003243 match *self {
3244 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
3245 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
3246 }
3247 }
3248 }
3249
3250 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05003251 impl ToTokens for ExprTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003252 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003253 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003254 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003255 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay2a86fdd2017-12-28 23:34:28 -05003256 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003257 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05003258 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08003259 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003260 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003261 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003262 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003263 }
3264 }
3265
3266 impl ToTokens for ExprBinary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003267 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003268 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003269 self.left.to_tokens(tokens);
3270 self.op.to_tokens(tokens);
3271 self.right.to_tokens(tokens);
3272 }
3273 }
3274
3275 impl ToTokens for ExprUnary {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003276 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003277 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003278 self.op.to_tokens(tokens);
3279 self.expr.to_tokens(tokens);
3280 }
3281 }
3282
David Tolnay8c91b882017-12-28 23:04:32 -05003283 impl ToTokens for ExprLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003284 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003285 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003286 self.lit.to_tokens(tokens);
3287 }
3288 }
3289
Alex Crichton62a0a592017-05-22 13:58:53 -07003290 impl ToTokens for ExprCast {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003291 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003292 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003293 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003294 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003295 self.ty.to_tokens(tokens);
3296 }
3297 }
3298
David Tolnay0cf94f22017-12-28 23:46:26 -05003299 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003300 impl ToTokens for ExprType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003301 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003302 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003303 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003304 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003305 self.ty.to_tokens(tokens);
3306 }
3307 }
3308
Michael Layzell734adb42017-06-07 16:58:31 -04003309 #[cfg(feature = "full")]
Alex Crichtona74a1c82018-05-16 10:20:44 -07003310 fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003311 if let Some((ref else_token, ref else_)) = *else_ {
3312 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003313
3314 // If we are not one of the valid expressions to exist in an else
3315 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05003316 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05003317 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003318 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003319 }
3320 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05003321 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05003322 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003323 });
3324 }
3325 }
3326 }
3327 }
3328
3329 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003330 impl ToTokens for ExprIf {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003331 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003332 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003333 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003334 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003335 self.then_branch.to_tokens(tokens);
3336 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003337 }
3338 }
3339
Michael Layzell734adb42017-06-07 16:58:31 -04003340 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003341 impl ToTokens for ExprIfLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003342 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003343 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003344 self.if_token.to_tokens(tokens);
3345 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003346 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003347 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003348 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05003349 self.then_branch.to_tokens(tokens);
3350 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07003351 }
3352 }
3353
Michael Layzell734adb42017-06-07 16:58:31 -04003354 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003355 impl ToTokens for ExprWhile {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003356 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003357 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003358 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003359 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003360 wrap_bare_struct(tokens, &self.cond);
David Tolnay5d314dc2018-07-21 16:40:01 -07003361 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003362 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003363 tokens.append_all(&self.body.stmts);
3364 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003365 }
3366 }
3367
Michael Layzell734adb42017-06-07 16:58:31 -04003368 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003369 impl ToTokens for ExprWhileLet {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003370 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003371 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003372 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003373 self.while_token.to_tokens(tokens);
3374 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003375 self.pats.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003376 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003377 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003378 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003379 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003380 tokens.append_all(&self.body.stmts);
3381 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003382 }
3383 }
3384
Michael Layzell734adb42017-06-07 16:58:31 -04003385 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003386 impl ToTokens for ExprForLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003387 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003388 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003389 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003390 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003391 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003392 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003393 wrap_bare_struct(tokens, &self.expr);
David Tolnay5d314dc2018-07-21 16:40:01 -07003394 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003395 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003396 tokens.append_all(&self.body.stmts);
3397 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003398 }
3399 }
3400
Michael Layzell734adb42017-06-07 16:58:31 -04003401 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003402 impl ToTokens for ExprLoop {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003403 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003404 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnaybcd498f2017-12-29 12:02:33 -05003405 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003406 self.loop_token.to_tokens(tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003407 self.body.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003408 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003409 tokens.append_all(&self.body.stmts);
3410 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003411 }
3412 }
3413
Michael Layzell734adb42017-06-07 16:58:31 -04003414 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003415 impl ToTokens for ExprMatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003416 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003417 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003418 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003419 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003420 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003421 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay51382052017-12-27 13:46:21 -05003422 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003423 arm.to_tokens(tokens);
3424 // Ensure that we have a comma after a non-block arm, except
3425 // for the last one.
3426 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07003427 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003428 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003429 }
3430 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003431 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003432 }
3433 }
3434
Michael Layzell734adb42017-06-07 16:58:31 -04003435 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003436 impl ToTokens for ExprCatch {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003437 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003438 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003439 self.do_token.to_tokens(tokens);
3440 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003441 self.block.to_tokens(tokens);
3442 }
3443 }
3444
Michael Layzell734adb42017-06-07 16:58:31 -04003445 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07003446 impl ToTokens for ExprYield {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003447 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003448 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonfe110462017-06-01 12:49:27 -07003449 self.yield_token.to_tokens(tokens);
3450 self.expr.to_tokens(tokens);
3451 }
3452 }
3453
3454 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003455 impl ToTokens for ExprClosure {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003456 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003457 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay13d4c0e2018-03-31 20:53:59 +02003458 self.movability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003459 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003460 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08003461 for input in self.inputs.pairs() {
3462 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05003463 FnArg::Captured(ArgCaptured {
3464 ref pat,
3465 ty: Type::Infer(_),
3466 ..
3467 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07003468 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07003469 }
David Tolnay56080682018-01-06 14:01:52 -08003470 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07003471 }
David Tolnayf2cfd722017-12-31 18:02:51 -05003472 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003473 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003474 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05003475 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003476 self.body.to_tokens(tokens);
3477 }
3478 }
3479
Michael Layzell734adb42017-06-07 16:58:31 -04003480 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05003481 impl ToTokens for ExprUnsafe {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003482 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003483 outer_attrs_to_tokens(&self.attrs, tokens);
Nika Layzell640832a2017-12-04 13:37:09 -05003484 self.unsafe_token.to_tokens(tokens);
David Tolnayc4be3512018-08-27 06:25:44 -07003485 self.block.brace_token.surround(tokens, |tokens| {
3486 inner_attrs_to_tokens(&self.attrs, tokens);
3487 tokens.append_all(&self.block.stmts);
3488 });
Nika Layzell640832a2017-12-04 13:37:09 -05003489 }
3490 }
3491
3492 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003493 impl ToTokens for ExprBlock {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003494 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003495 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003496 self.block.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003497 inner_attrs_to_tokens(&self.attrs, tokens);
David Tolnay5d314dc2018-07-21 16:40:01 -07003498 tokens.append_all(&self.block.stmts);
3499 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003500 }
3501 }
3502
Michael Layzell734adb42017-06-07 16:58:31 -04003503 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003504 impl ToTokens for ExprAssign {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003505 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003506 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003507 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003508 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003509 self.right.to_tokens(tokens);
3510 }
3511 }
3512
Michael Layzell734adb42017-06-07 16:58:31 -04003513 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003514 impl ToTokens for ExprAssignOp {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003515 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003516 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003517 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003518 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003519 self.right.to_tokens(tokens);
3520 }
3521 }
3522
3523 impl ToTokens for ExprField {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003524 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003525 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003526 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003527 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003528 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003529 }
3530 }
3531
David Tolnay85b69a42017-12-27 20:43:10 -05003532 impl ToTokens for Member {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003533 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay85b69a42017-12-27 20:43:10 -05003534 match *self {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003535 Member::Named(ref ident) => ident.to_tokens(tokens),
David Tolnay85b69a42017-12-27 20:43:10 -05003536 Member::Unnamed(ref index) => index.to_tokens(tokens),
3537 }
3538 }
3539 }
3540
David Tolnay85b69a42017-12-27 20:43:10 -05003541 impl ToTokens for Index {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003542 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton9a4dca22018-03-28 06:32:19 -07003543 let mut lit = Literal::i64_unsuffixed(i64::from(self.index));
3544 lit.set_span(self.span);
3545 tokens.append(lit);
Alex Crichton62a0a592017-05-22 13:58:53 -07003546 }
3547 }
3548
3549 impl ToTokens for ExprIndex {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003550 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003551 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003552 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003553 self.bracket_token.surround(tokens, |tokens| {
3554 self.index.to_tokens(tokens);
3555 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003556 }
3557 }
3558
Michael Layzell734adb42017-06-07 16:58:31 -04003559 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003560 impl ToTokens for ExprRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003561 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003562 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003563 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003564 match self.limits {
3565 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3566 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3567 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003568 self.to.to_tokens(tokens);
3569 }
3570 }
3571
3572 impl ToTokens for ExprPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003573 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003574 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003575 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003576 }
3577 }
3578
Michael Layzell734adb42017-06-07 16:58:31 -04003579 #[cfg(feature = "full")]
David Tolnay00674ba2018-03-31 18:14:11 +02003580 impl ToTokens for ExprReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003581 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003582 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003583 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003584 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003585 self.expr.to_tokens(tokens);
3586 }
3587 }
3588
Michael Layzell734adb42017-06-07 16:58:31 -04003589 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003590 impl ToTokens for ExprBreak {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003591 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003592 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003593 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003594 self.label.to_tokens(tokens);
3595 self.expr.to_tokens(tokens);
3596 }
3597 }
3598
Michael Layzell734adb42017-06-07 16:58:31 -04003599 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003600 impl ToTokens for ExprContinue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003601 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003602 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003603 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003604 self.label.to_tokens(tokens);
3605 }
3606 }
3607
Michael Layzell734adb42017-06-07 16:58:31 -04003608 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003609 impl ToTokens for ExprReturn {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003610 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003611 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003612 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003613 self.expr.to_tokens(tokens);
3614 }
3615 }
3616
Michael Layzell734adb42017-06-07 16:58:31 -04003617 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003618 impl ToTokens for ExprMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003619 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003620 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay8c91b882017-12-28 23:04:32 -05003621 self.mac.to_tokens(tokens);
3622 }
3623 }
3624
3625 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003626 impl ToTokens for ExprStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003627 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003628 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003629 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003630 self.brace_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003631 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003632 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003633 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003634 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003635 self.rest.to_tokens(tokens);
3636 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003637 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003638 }
3639 }
3640
Michael Layzell734adb42017-06-07 16:58:31 -04003641 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003642 impl ToTokens for ExprRepeat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003643 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003644 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003645 self.bracket_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003646 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003647 self.expr.to_tokens(tokens);
3648 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003649 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003650 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003651 }
3652 }
3653
David Tolnaye98775f2017-12-28 23:17:00 -05003654 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003655 impl ToTokens for ExprGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003656 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003657 outer_attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003658 self.group_token.surround(tokens, |tokens| {
3659 self.expr.to_tokens(tokens);
3660 });
3661 }
3662 }
3663
Alex Crichton62a0a592017-05-22 13:58:53 -07003664 impl ToTokens for ExprParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003665 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003666 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003667 self.paren_token.surround(tokens, |tokens| {
David Tolnayd997aef2018-07-21 18:42:31 -07003668 inner_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003669 self.expr.to_tokens(tokens);
3670 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003671 }
3672 }
3673
Michael Layzell734adb42017-06-07 16:58:31 -04003674 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003675 impl ToTokens for ExprTry {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003676 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003677 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003678 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003679 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003680 }
3681 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003682
David Tolnay2ae520a2017-12-29 11:19:50 -05003683 impl ToTokens for ExprVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003684 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003685 self.tts.to_tokens(tokens);
3686 }
3687 }
3688
Michael Layzell734adb42017-06-07 16:58:31 -04003689 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003690 impl ToTokens for Label {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003691 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybcd498f2017-12-29 12:02:33 -05003692 self.name.to_tokens(tokens);
3693 self.colon_token.to_tokens(tokens);
3694 }
3695 }
3696
3697 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003698 impl ToTokens for FieldValue {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003699 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003700 outer_attrs_to_tokens(&self.attrs, tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003701 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003702 if let Some(ref colon_token) = self.colon_token {
3703 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003704 self.expr.to_tokens(tokens);
3705 }
David Tolnay055a7042016-10-02 19:23:54 -07003706 }
3707 }
3708
Michael Layzell734adb42017-06-07 16:58:31 -04003709 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003710 impl ToTokens for Arm {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003711 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003712 tokens.append_all(&self.attrs);
David Tolnay18cc4d42018-03-31 18:47:20 +02003713 self.leading_vert.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003714 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003715 if let Some((ref if_token, ref guard)) = self.guard {
3716 if_token.to_tokens(tokens);
3717 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003718 }
David Tolnaydfb91432018-03-31 19:19:44 +02003719 self.fat_arrow_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003720 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003721 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003722 }
3723 }
3724
Michael Layzell734adb42017-06-07 16:58:31 -04003725 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003726 impl ToTokens for PatWild {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003727 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003728 self.underscore_token.to_tokens(tokens);
3729 }
3730 }
3731
Michael Layzell734adb42017-06-07 16:58:31 -04003732 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003733 impl ToTokens for PatIdent {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003734 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay24237fb2017-12-29 02:15:26 -05003735 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003736 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003737 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003738 if let Some((ref at_token, ref subpat)) = self.subpat {
3739 at_token.to_tokens(tokens);
3740 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003741 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003742 }
3743 }
3744
Michael Layzell734adb42017-06-07 16:58:31 -04003745 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003746 impl ToTokens for PatStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003747 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003748 self.path.to_tokens(tokens);
3749 self.brace_token.surround(tokens, |tokens| {
3750 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003751 // NOTE: We need a comma before the dot2 token if it is present.
3752 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003753 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003754 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003755 self.dot2_token.to_tokens(tokens);
3756 });
3757 }
3758 }
3759
Michael Layzell734adb42017-06-07 16:58:31 -04003760 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003761 impl ToTokens for PatTupleStruct {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003762 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003763 self.path.to_tokens(tokens);
3764 self.pat.to_tokens(tokens);
3765 }
3766 }
3767
Michael Layzell734adb42017-06-07 16:58:31 -04003768 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003769 impl ToTokens for PatPath {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003770 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003771 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3772 }
3773 }
3774
Michael Layzell734adb42017-06-07 16:58:31 -04003775 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003776 impl ToTokens for PatTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003777 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003778 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003779 self.front.to_tokens(tokens);
3780 if let Some(ref dot2_token) = self.dot2_token {
3781 if !self.front.empty_or_trailing() {
3782 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003783 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003784 }
David Tolnay41871922017-12-29 01:53:45 -05003785 dot2_token.to_tokens(tokens);
3786 self.comma_token.to_tokens(tokens);
3787 if self.comma_token.is_none() && !self.back.is_empty() {
3788 // Ensure there is a comma after the .. token.
3789 <Token![,]>::default().to_tokens(tokens);
3790 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003791 }
David Tolnay41871922017-12-29 01:53:45 -05003792 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003793 });
3794 }
3795 }
3796
Michael Layzell734adb42017-06-07 16:58:31 -04003797 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003798 impl ToTokens for PatBox {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003799 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003800 self.box_token.to_tokens(tokens);
3801 self.pat.to_tokens(tokens);
3802 }
3803 }
3804
Michael Layzell734adb42017-06-07 16:58:31 -04003805 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003806 impl ToTokens for PatRef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003807 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003808 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003809 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003810 self.pat.to_tokens(tokens);
3811 }
3812 }
3813
Michael Layzell734adb42017-06-07 16:58:31 -04003814 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003815 impl ToTokens for PatLit {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003816 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003817 self.expr.to_tokens(tokens);
3818 }
3819 }
3820
Michael Layzell734adb42017-06-07 16:58:31 -04003821 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003822 impl ToTokens for PatRange {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003823 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003824 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003825 match self.limits {
3826 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3827 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3828 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003829 self.hi.to_tokens(tokens);
3830 }
3831 }
3832
Michael Layzell734adb42017-06-07 16:58:31 -04003833 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003834 impl ToTokens for PatSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003835 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003836 // XXX: This is a mess, and it will be so easy to screw it up. How
3837 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003838 self.bracket_token.surround(tokens, |tokens| {
3839 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003840
3841 // If we need a comma before the middle or standalone .. token,
3842 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003843 if !self.front.empty_or_trailing()
3844 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003845 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003846 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003847 }
3848
3849 // If we have an identifier, we always need a .. token.
3850 if self.middle.is_some() {
3851 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003852 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003853 } else if self.dot2_token.is_some() {
3854 self.dot2_token.to_tokens(tokens);
3855 }
3856
3857 // Make sure we have a comma before the back half.
3858 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003859 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003860 self.back.to_tokens(tokens);
3861 } else {
3862 self.comma_token.to_tokens(tokens);
3863 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003864 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003865 }
3866 }
3867
Michael Layzell734adb42017-06-07 16:58:31 -04003868 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003869 impl ToTokens for PatMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003870 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -05003871 self.mac.to_tokens(tokens);
3872 }
3873 }
3874
3875 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003876 impl ToTokens for PatVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003877 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -05003878 self.tts.to_tokens(tokens);
3879 }
3880 }
3881
3882 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003883 impl ToTokens for FieldPat {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003884 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003885 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003886 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003887 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003888 }
3889 self.pat.to_tokens(tokens);
3890 }
3891 }
3892
Michael Layzell734adb42017-06-07 16:58:31 -04003893 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003894 impl ToTokens for Block {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003895 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003896 self.brace_token.surround(tokens, |tokens| {
3897 tokens.append_all(&self.stmts);
3898 });
David Tolnay42602292016-10-01 22:25:45 -07003899 }
3900 }
3901
Michael Layzell734adb42017-06-07 16:58:31 -04003902 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003903 impl ToTokens for Stmt {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003904 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay42602292016-10-01 22:25:45 -07003905 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003906 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003907 Stmt::Item(ref item) => item.to_tokens(tokens),
3908 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003909 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003910 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003911 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003912 }
David Tolnay42602292016-10-01 22:25:45 -07003913 }
3914 }
3915 }
David Tolnay191e0582016-10-02 18:31:09 -07003916
Michael Layzell734adb42017-06-07 16:58:31 -04003917 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003918 impl ToTokens for Local {
Alex Crichtona74a1c82018-05-16 10:20:44 -07003919 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayd997aef2018-07-21 18:42:31 -07003920 outer_attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003921 self.let_token.to_tokens(tokens);
David Tolnay5b5b7d22018-03-31 21:05:00 +02003922 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003923 if let Some((ref colon_token, ref ty)) = self.ty {
3924 colon_token.to_tokens(tokens);
3925 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003926 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003927 if let Some((ref eq_token, ref init)) = self.init {
3928 eq_token.to_tokens(tokens);
3929 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003930 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003931 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003932 }
3933 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003934}