blob: 5f48b2c8619af3054b5ba26762075c331348f06f [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -050010use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -050011use proc_macro2::{Span, TokenStream};
David Tolnay14982012017-12-29 00:49:51 -050012#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -050013use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -050014#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050015use tt::TokenStreamHelper;
David Tolnay2ae520a2017-12-29 11:19:50 -050016#[cfg(feature = "full")]
17use std::mem;
David Tolnayf4bbbd92016-09-23 14:41:55 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -080020 /// A Rust expression.
David Tolnay614a0142018-01-07 10:25:43 -080021 ///
22 /// # Syntax tree enums
23 ///
24 /// This type is a syntax tree enum. In Syn this and other syntax tree enums
25 /// are designed to be traversed using the following rebinding idiom.
26 ///
27 /// ```
28 /// # use syn::Expr;
29 /// #
30 /// # fn example(expr: Expr) {
31 /// # const IGNORE: &str = stringify! {
32 /// let expr: Expr = /* ... */;
33 /// # };
34 /// match expr {
35 /// Expr::MethodCall(expr) => {
36 /// /* ... */
37 /// }
38 /// Expr::Cast(expr) => {
39 /// /* ... */
40 /// }
41 /// Expr::IfLet(expr) => {
42 /// /* ... */
43 /// }
44 /// /* ... */
45 /// # _ => {}
46 /// }
47 /// # }
48 /// ```
49 ///
50 /// We begin with a variable `expr` of type `Expr` that has no fields
51 /// (because it is an enum), and by matching on it and rebinding a variable
52 /// with the same name `expr` we effectively imbue our variable with all of
53 /// the data fields provided by the variant that it turned out to be. So for
54 /// example above if we ended up in the `MethodCall` case then we get to use
55 /// `expr.receiver`, `expr.args` etc; if we ended up in the `IfLet` case we
56 /// get to use `expr.pat`, `expr.then_branch`, `expr.else_branch`.
57 ///
58 /// The pattern is similar if the input expression is borrowed:
59 ///
60 /// ```
61 /// # use syn::Expr;
62 /// #
63 /// # fn example(expr: &Expr) {
64 /// match *expr {
65 /// Expr::MethodCall(ref expr) => {
66 /// # }
67 /// # _ => {}
68 /// # }
69 /// # }
70 /// ```
71 ///
72 /// This approach avoids repeating the variant names twice on every line.
73 ///
74 /// ```
75 /// # use syn::{Expr, ExprMethodCall};
76 /// #
77 /// # fn example(expr: Expr) {
78 /// # match expr {
79 /// Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive
80 /// # }
81 /// # _ => {}
82 /// # }
83 /// # }
84 /// ```
85 ///
86 /// In general, the name to which a syntax tree enum variant is bound should
87 /// be a suitable name for the complete syntax tree enum type.
88 ///
89 /// ```
90 /// # use syn::{Expr, ExprField};
91 /// #
92 /// # fn example(discriminant: &ExprField) {
93 /// // Binding is called `base` which is the name I would use if I were
94 /// // assigning `*discriminant.base` without an `if let`.
95 /// if let Expr::Tuple(ref base) = *discriminant.base {
96 /// # }
97 /// # }
98 /// ```
99 ///
100 /// A sign that you may not be choosing the right variable names is if you
101 /// see names getting repeated in your code, like accessing
102 /// `receiver.receiver` or `pat.pat` or `cond.cond`.
David Tolnay8c91b882017-12-28 23:04:32 -0500103 pub enum Expr {
David Tolnaya454c8f2018-01-07 01:01:10 -0800104 /// A box expression: `box f`.
Michael Layzell734adb42017-06-07 16:58:31 -0400105 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500106 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800107 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500110
David Tolnaya454c8f2018-01-07 01:01:10 -0800111 /// A placement expression: `place <- value`.
Michael Layzell734adb42017-06-07 16:58:31 -0400112 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500113 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -0500115 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -0700116 pub value: Box<Expr>,
117 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500118
David Tolnaya454c8f2018-01-07 01:01:10 -0800119 /// A slice literal expression: `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -0400120 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500121 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500122 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500123 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500125
David Tolnaya454c8f2018-01-07 01:01:10 -0800126 /// A function call expression: `invoke(a, b)`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700127 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -0500128 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700129 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500130 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500131 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500133
David Tolnaya454c8f2018-01-07 01:01:10 -0800134 /// A method call expression: `x.foo::<T>(a, b)`.
Michael Layzell734adb42017-06-07 16:58:31 -0400135 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500136 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -0500137 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800138 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500139 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -0500140 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500141 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500142 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700143 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500144
David Tolnaya454c8f2018-01-07 01:01:10 -0800145 /// A tuple expression: `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -0500146 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500147 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500148 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500149 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700150 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500151
David Tolnaya454c8f2018-01-07 01:01:10 -0800152 /// A binary operation: `a + b`, `a * b`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500154 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500156 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 pub right: Box<Expr>,
158 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500159
David Tolnaya454c8f2018-01-07 01:01:10 -0800160 /// A unary operation: `!x`, `*x`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -0500162 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 pub op: UnOp,
164 pub expr: Box<Expr>,
165 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500166
David Tolnaya454c8f2018-01-07 01:01:10 -0800167 /// A literal in place of an expression: `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -0500168 pub Lit(ExprLit {
169 pub attrs: Vec<Attribute>,
170 pub lit: Lit,
171 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500172
David Tolnaya454c8f2018-01-07 01:01:10 -0800173 /// A cast expression: `foo as f64`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -0500175 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800177 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800178 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500180
David Tolnaya454c8f2018-01-07 01:01:10 -0800181 /// A type ascription expression: `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -0500182 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500183 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700184 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800185 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800186 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700187 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500188
David Tolnaya454c8f2018-01-07 01:01:10 -0800189 /// An `if` expression with an optional `else` block: `if expr { ... }
190 /// else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800192 /// The `else` branch expression may only be an `If`, `IfLet`, or
193 /// `Block` expression, not any of the other types of expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400194 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500195 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500196 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500198 pub then_branch: Block,
199 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500201
David Tolnaya454c8f2018-01-07 01:01:10 -0800202 /// An `if let` expression with an optional `else` block: `if let pat =
203 /// expr { ... } else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800205 /// The `else` branch expression may only be an `If`, `IfLet`, or
206 /// `Block` expression, not any of the other types of expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400207 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500208 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800209 pub if_token: Token![if],
210 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500211 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800212 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500213 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500214 pub then_branch: Block,
215 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700216 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500217
David Tolnaya454c8f2018-01-07 01:01:10 -0800218 /// A while loop: `while expr { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400219 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500220 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500221 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800222 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500223 pub cond: Box<Expr>,
224 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700225 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500226
David Tolnaya454c8f2018-01-07 01:01:10 -0800227 /// A while-let loop: `while let pat = expr { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400228 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500229 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500230 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800231 pub while_token: Token![while],
232 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500233 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800234 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500235 pub expr: Box<Expr>,
236 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700237 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500238
David Tolnaya454c8f2018-01-07 01:01:10 -0800239 /// A for loop: `for pat in expr { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400240 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500241 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500242 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500243 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700244 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500245 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 pub expr: Box<Expr>,
247 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500249
David Tolnaya454c8f2018-01-07 01:01:10 -0800250 /// Conditionless loop: `loop { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400251 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500252 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500253 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500254 pub loop_token: Token![loop],
255 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500257
David Tolnaya454c8f2018-01-07 01:01:10 -0800258 /// A `match` expression: `match n { Some(n) => {}, None => {} }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400259 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500260 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800261 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500263 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 pub arms: Vec<Arm>,
265 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500266
David Tolnaya454c8f2018-01-07 01:01:10 -0800267 /// A closure expression: `|a, b| a + b`.
Michael Layzell734adb42017-06-07 16:58:31 -0400268 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500269 pub attrs: Vec<Attribute>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500270 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800271 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500272 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800273 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500274 pub output: ReturnType,
275 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700276 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500277
David Tolnaya454c8f2018-01-07 01:01:10 -0800278 /// An unsafe block: `unsafe { ... }`.
Nika Layzell640832a2017-12-04 13:37:09 -0500279 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500280 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500281 pub unsafe_token: Token![unsafe],
282 pub block: Block,
283 }),
284
David Tolnaya454c8f2018-01-07 01:01:10 -0800285 /// A blocked scope: `{ ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400286 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500287 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 pub block: Block,
289 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700290
David Tolnaya454c8f2018-01-07 01:01:10 -0800291 /// An assignment expression: `a = compute()`.
Michael Layzell734adb42017-06-07 16:58:31 -0400292 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500293 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500298
David Tolnaya454c8f2018-01-07 01:01:10 -0800299 /// A compound assignment expression: `counter += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400300 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500301 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700302 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500303 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700304 pub right: Box<Expr>,
305 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500306
David Tolnaya454c8f2018-01-07 01:01:10 -0800307 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500308 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400309 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500310 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500311 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800312 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500313 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500315
David Tolnay05658502018-01-07 09:56:37 -0800316 /// A square bracketed indexing expression: `vector[2]`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500318 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700319 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500320 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500321 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500323
David Tolnaya454c8f2018-01-07 01:01:10 -0800324 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
Michael Layzell734adb42017-06-07 16:58:31 -0400325 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500326 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700328 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500329 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700331
David Tolnaya454c8f2018-01-07 01:01:10 -0800332 /// A path like `std::mem::replace` possibly containing generic
333 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700334 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800335 /// A plain identifier like `x` is a path of length 1.
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500337 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700338 pub qself: Option<QSelf>,
339 pub path: Path,
340 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700341
David Tolnaya454c8f2018-01-07 01:01:10 -0800342 /// A referencing operation: `&a` or `&mut a`.
Michael Layzell734adb42017-06-07 16:58:31 -0400343 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500344 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800345 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500346 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 pub expr: Box<Expr>,
348 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500349
David Tolnaya454c8f2018-01-07 01:01:10 -0800350 /// A `break`, with an optional label to break and an optional
351 /// expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400352 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500353 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500354 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700355 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700356 pub expr: Option<Box<Expr>>,
357 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500358
David Tolnaya454c8f2018-01-07 01:01:10 -0800359 /// A `continue`, with an optional label.
Michael Layzell734adb42017-06-07 16:58:31 -0400360 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500361 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800362 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500363 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700364 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500365
David Tolnaya454c8f2018-01-07 01:01:10 -0800366 /// A `return`, with an optional value to be returned.
David Tolnayc246cd32017-12-28 23:14:32 -0500367 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500368 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800369 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500370 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700371 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700372
David Tolnaya454c8f2018-01-07 01:01:10 -0800373 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay8c91b882017-12-28 23:04:32 -0500374 pub Macro(ExprMacro #full {
375 pub attrs: Vec<Attribute>,
376 pub mac: Macro,
377 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700378
David Tolnaya454c8f2018-01-07 01:01:10 -0800379 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800381 /// The `rest` provides the value of the remaining fields as in `S { a:
382 /// 1, b: 1, ..rest }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400383 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500384 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700385 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500386 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500387 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500388 pub dot2_token: Option<Token![..]>,
389 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700390 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700391
David Tolnaya454c8f2018-01-07 01:01:10 -0800392 /// An array literal constructed from one repeated element: `[0u8; N]`.
Michael Layzell734adb42017-06-07 16:58:31 -0400393 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500394 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500395 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500397 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800398 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700399 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700400
David Tolnaya454c8f2018-01-07 01:01:10 -0800401 /// A parenthesized expression: `(a + b)`.
David Tolnaye98775f2017-12-28 23:17:00 -0500402 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500403 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500404 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500405 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700407
David Tolnaya454c8f2018-01-07 01:01:10 -0800408 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400409 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800410 /// This variant is important for faithfully representing the precedence
411 /// of expressions and is related to `None`-delimited spans in a
412 /// `TokenStream`.
David Tolnaye98775f2017-12-28 23:17:00 -0500413 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500414 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500415 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500416 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400417 }),
418
David Tolnaya454c8f2018-01-07 01:01:10 -0800419 /// A try-expression: `expr?`.
Michael Layzell734adb42017-06-07 16:58:31 -0400420 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500421 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700422 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800423 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700424 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700425
David Tolnaya454c8f2018-01-07 01:01:10 -0800426 /// A catch expression: `do catch { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400427 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500428 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800429 pub do_token: Token![do],
430 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 pub block: Block,
432 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700433
David Tolnaya454c8f2018-01-07 01:01:10 -0800434 /// A yield expression: `yield expr`.
Alex Crichtonfe110462017-06-01 12:49:27 -0700435 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500436 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800437 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700438 pub expr: Option<Box<Expr>>,
439 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500440
David Tolnaya454c8f2018-01-07 01:01:10 -0800441 /// Tokens in expression position not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500442 pub Verbatim(ExprVerbatim #manual_extra_traits {
443 pub tts: TokenStream,
444 }),
445 }
446}
447
448#[cfg(feature = "extra-traits")]
449impl Eq for ExprVerbatim {}
450
451#[cfg(feature = "extra-traits")]
452impl PartialEq for ExprVerbatim {
453 fn eq(&self, other: &Self) -> bool {
454 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
455 }
456}
457
458#[cfg(feature = "extra-traits")]
459impl Hash for ExprVerbatim {
460 fn hash<H>(&self, state: &mut H)
461 where
462 H: Hasher,
463 {
464 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700465 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700466}
467
David Tolnay8c91b882017-12-28 23:04:32 -0500468impl Expr {
469 // Not public API.
470 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500471 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500472 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500473 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800474 Expr::Box(ExprBox { ref mut attrs, .. })
475 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
476 | Expr::Array(ExprArray { ref mut attrs, .. })
477 | Expr::Call(ExprCall { ref mut attrs, .. })
478 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
479 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
480 | Expr::Binary(ExprBinary { ref mut attrs, .. })
481 | Expr::Unary(ExprUnary { ref mut attrs, .. })
482 | Expr::Lit(ExprLit { ref mut attrs, .. })
483 | Expr::Cast(ExprCast { ref mut attrs, .. })
484 | Expr::Type(ExprType { ref mut attrs, .. })
485 | Expr::If(ExprIf { ref mut attrs, .. })
486 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
487 | Expr::While(ExprWhile { ref mut attrs, .. })
488 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
489 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
490 | Expr::Loop(ExprLoop { ref mut attrs, .. })
491 | Expr::Match(ExprMatch { ref mut attrs, .. })
492 | Expr::Closure(ExprClosure { ref mut attrs, .. })
493 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
494 | Expr::Block(ExprBlock { ref mut attrs, .. })
495 | Expr::Assign(ExprAssign { ref mut attrs, .. })
496 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
497 | Expr::Field(ExprField { ref mut attrs, .. })
498 | Expr::Index(ExprIndex { ref mut attrs, .. })
499 | Expr::Range(ExprRange { ref mut attrs, .. })
500 | Expr::Path(ExprPath { ref mut attrs, .. })
501 | Expr::AddrOf(ExprAddrOf { ref mut attrs, .. })
502 | Expr::Break(ExprBreak { ref mut attrs, .. })
503 | Expr::Continue(ExprContinue { ref mut attrs, .. })
504 | Expr::Return(ExprReturn { ref mut attrs, .. })
505 | Expr::Macro(ExprMacro { ref mut attrs, .. })
506 | Expr::Struct(ExprStruct { ref mut attrs, .. })
507 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
508 | Expr::Paren(ExprParen { ref mut attrs, .. })
509 | Expr::Group(ExprGroup { ref mut attrs, .. })
510 | Expr::Try(ExprTry { ref mut attrs, .. })
511 | Expr::Catch(ExprCatch { ref mut attrs, .. })
512 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500513 Expr::Verbatim(_) => {
514 // TODO
515 Vec::new()
516 }
David Tolnay8c91b882017-12-28 23:04:32 -0500517 }
518 }
519}
520
David Tolnay85b69a42017-12-27 20:43:10 -0500521ast_enum! {
522 /// A struct or tuple struct field accessed in a struct literal or field
523 /// expression.
524 pub enum Member {
525 /// A named field like `self.x`.
526 Named(Ident),
527 /// An unnamed field like `self.0`.
528 Unnamed(Index),
529 }
530}
531
David Tolnay85b69a42017-12-27 20:43:10 -0500532ast_struct! {
533 /// The index of an unnamed tuple struct field.
534 pub struct Index #manual_extra_traits {
535 pub index: u32,
536 pub span: Span,
537 }
538}
539
David Tolnay14982012017-12-29 00:49:51 -0500540impl From<usize> for Index {
541 fn from(index: usize) -> Index {
542 assert!(index < std::u32::MAX as usize);
543 Index {
544 index: index as u32,
545 span: Span::default(),
546 }
547 }
548}
549
550#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500551impl Eq for Index {}
552
David Tolnay14982012017-12-29 00:49:51 -0500553#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500554impl PartialEq for Index {
555 fn eq(&self, other: &Self) -> bool {
556 self.index == other.index
557 }
558}
559
David Tolnay14982012017-12-29 00:49:51 -0500560#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500561impl Hash for Index {
562 fn hash<H: Hasher>(&self, state: &mut H) {
563 self.index.hash(state);
564 }
565}
566
567#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700568ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800569 /// The `::<>` explicit type parameters passed to a method call:
570 /// `parse::<u64>()`.
David Tolnayd60cfec2017-12-29 00:21:38 -0500571 pub struct MethodTurbofish {
572 pub colon2_token: Token![::],
573 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500574 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500575 pub gt_token: Token![>],
576 }
577}
578
579#[cfg(feature = "full")]
580ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800581 /// An individual generic argument to a method, like `T`.
David Tolnayd60cfec2017-12-29 00:21:38 -0500582 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800583 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500584 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800585 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500586 ///
587 /// NOTE: Identity expressions are represented as Type arguments, as
588 /// they are indistinguishable syntactically.
589 Const(Expr),
590 }
591}
592
593#[cfg(feature = "full")]
594ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 /// A field-value pair in a struct literal.
596 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500597 /// Attributes tagged on the field.
598 pub attrs: Vec<Attribute>,
599
600 /// Name or index of the field.
601 pub member: Member,
602
David Tolnay5d7098a2017-12-29 01:35:24 -0500603 /// The colon in `Struct { x: x }`. If written in shorthand like
604 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500605 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500606
Alex Crichton62a0a592017-05-22 13:58:53 -0700607 /// Value of the field.
608 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700609 }
David Tolnay055a7042016-10-02 19:23:54 -0700610}
611
Michael Layzell734adb42017-06-07 16:58:31 -0400612#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700613ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800614 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnaybcd498f2017-12-29 12:02:33 -0500615 pub struct Label {
616 pub name: Lifetime,
617 pub colon_token: Token![:],
618 }
619}
620
621#[cfg(feature = "full")]
622ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800623 /// A braced block containing Rust statements.
Alex Crichton62a0a592017-05-22 13:58:53 -0700624 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500625 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 /// Statements in a block
627 pub stmts: Vec<Stmt>,
628 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700629}
630
Michael Layzell734adb42017-06-07 16:58:31 -0400631#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700632ast_enum! {
633 /// A statement, usually ending in a semicolon.
634 pub enum Stmt {
635 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800636 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700637
Alex Crichton62a0a592017-05-22 13:58:53 -0700638 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800639 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700640
Alex Crichton62a0a592017-05-22 13:58:53 -0700641 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800642 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700643
David Tolnaya454c8f2018-01-07 01:01:10 -0800644 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800645 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700646 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700647}
648
Michael Layzell734adb42017-06-07 16:58:31 -0400649#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700650ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800651 /// A local `let` binding: `let x: u64 = s.parse()?`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500653 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800654 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700655 pub pat: Box<Pat>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500656 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500657 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500658 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700659 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700660}
661
Michael Layzell734adb42017-06-07 16:58:31 -0400662#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700663ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800664 /// A pattern in a local binding, function signature, match expression, or
665 /// various other places.
David Tolnay614a0142018-01-07 10:25:43 -0800666 ///
667 /// # Syntax tree enum
668 ///
669 /// This type is a [syntax tree enum].
670 ///
671 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700672 // Clippy false positive
673 // https://github.com/Manishearth/rust-clippy/issues/1241
674 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
675 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800676 /// A pattern that matches any value: `_`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700677 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800678 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700679 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700680
David Tolnaya454c8f2018-01-07 01:01:10 -0800681 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700682 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500683 pub by_ref: Option<Token![ref]>,
684 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700685 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500686 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700687 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700688
David Tolnaya454c8f2018-01-07 01:01:10 -0800689 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700690 pub Struct(PatStruct {
691 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500692 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500693 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800694 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700695 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700696
David Tolnaya454c8f2018-01-07 01:01:10 -0800697 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700698 pub TupleStruct(PatTupleStruct {
699 pub path: Path,
700 pub pat: PatTuple,
701 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700702
David Tolnaya454c8f2018-01-07 01:01:10 -0800703 /// A path pattern like `Color::Red`, optionally qualified with a
704 /// self-type.
705 ///
706 /// Unquailfied path patterns can legally refer to variants, structs,
707 /// constants or associated constants. Quailfied path patterns like
708 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
709 /// associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700710 pub Path(PatPath {
711 pub qself: Option<QSelf>,
712 pub path: Path,
713 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700714
David Tolnaya454c8f2018-01-07 01:01:10 -0800715 /// A tuple pattern: `(a, b)`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700716 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500717 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500718 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500719 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500720 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500721 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700722 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800723
724 /// A box pattern: `box v`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700725 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800726 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500727 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700728 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800729
730 /// A reference pattern: `&mut (first, second)`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700731 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800732 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500733 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500734 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700735 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800736
737 /// A literal pattern: `0`.
738 ///
739 /// This holds an `Expr` rather than a `Lit` because negative numbers
740 /// are represented as an `Expr::Unary`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700741 pub Lit(PatLit {
742 pub expr: Box<Expr>,
743 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800744
745 /// A range pattern: `1..=2`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700746 pub Range(PatRange {
747 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700748 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500749 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700750 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800751
752 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700753 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500754 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500755 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700756 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500757 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500758 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500759 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700760 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800761
762 /// A macro in expression position.
David Tolnay323279a2017-12-29 11:26:32 -0500763 pub Macro(PatMacro {
764 pub mac: Macro,
765 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800766
767 /// Tokens in pattern position not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500768 pub Verbatim(PatVerbatim #manual_extra_traits {
769 pub tts: TokenStream,
770 }),
771 }
772}
773
David Tolnayc43b44e2017-12-30 23:55:54 -0500774#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500775impl Eq for PatVerbatim {}
776
David Tolnayc43b44e2017-12-30 23:55:54 -0500777#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500778impl PartialEq for PatVerbatim {
779 fn eq(&self, other: &Self) -> bool {
780 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
781 }
782}
783
David Tolnayc43b44e2017-12-30 23:55:54 -0500784#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500785impl Hash for PatVerbatim {
786 fn hash<H>(&self, state: &mut H)
787 where
788 H: Hasher,
789 {
790 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700791 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700792}
793
Michael Layzell734adb42017-06-07 16:58:31 -0400794#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700795ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800796 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700797 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800798 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700799 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500800 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800801 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500802 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700803 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800804 /// 0...10 => {
805 /// return true;
806 /// }
807 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500808 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700809 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800810 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500811 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700812 /// ```
813 pub struct Arm {
814 pub attrs: Vec<Attribute>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500815 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500816 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800817 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700818 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800819 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700820 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700821}
822
Michael Layzell734adb42017-06-07 16:58:31 -0400823#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700824ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800825 /// Limit types of a range, inclusive or exclusive.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700826 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700827 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800828 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800829 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800830 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800831 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700832 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700833}
834
Michael Layzell734adb42017-06-07 16:58:31 -0400835#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700836ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800837 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700838 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800839 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
840 /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700841 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500842 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500843 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500844 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700845 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700846 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700847}
848
Michael Layzell3936ceb2017-07-08 00:28:36 -0400849#[cfg(any(feature = "parsing", feature = "printing"))]
850#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700851fn arm_expr_requires_comma(expr: &Expr) -> bool {
852 // see https://github.com/rust-lang/rust/blob/eb8f2586e
853 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500854 match *expr {
855 Expr::Unsafe(..)
856 | Expr::Block(..)
857 | Expr::If(..)
858 | Expr::IfLet(..)
859 | Expr::Match(..)
860 | Expr::While(..)
861 | Expr::WhileLet(..)
862 | Expr::Loop(..)
863 | Expr::ForLoop(..)
864 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700865 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400866 }
867}
868
David Tolnayb9c8e322016-09-23 20:48:37 -0700869#[cfg(feature = "parsing")]
870pub mod parsing {
871 use super::*;
David Tolnay056de302018-01-05 14:29:05 -0800872 use path::parsing::qpath;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500873 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -0800874 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700875
Michael Layzell734adb42017-06-07 16:58:31 -0400876 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -0800877 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500878 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -0800879 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400880 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500881 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500882 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700883
David Tolnaybcf26022017-12-25 22:10:52 -0500884 // When we're parsing expressions which occur before blocks, like in an if
885 // statement's condition, we cannot parse a struct literal.
886 //
887 // Struct literals are ambiguous in certain positions
888 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700889 macro_rules! ambiguous_expr {
890 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700891 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700892 };
893 }
894
David Tolnaybcf26022017-12-25 22:10:52 -0500895 // When we are parsing an optional suffix expression, we cannot allow blocks
896 // if structs are not allowed.
897 //
898 // Example:
899 //
900 // if break {} {}
901 //
902 // is ambiguous between:
903 //
904 // if (break {}) {}
905 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400906 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400907 macro_rules! opt_ambiguous_expr {
908 ($i:expr, $allow_struct:ident) => {
909 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
910 };
911 }
912
Alex Crichton954046c2017-05-30 21:49:42 -0700913 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400914 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700915
916 fn description() -> Option<&'static str> {
917 Some("expression")
918 }
919 }
920
Michael Layzell734adb42017-06-07 16:58:31 -0400921 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700922 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
923
David Tolnaybcf26022017-12-25 22:10:52 -0500924 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400925 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500926 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500927 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400928 }
929
Michael Layzell734adb42017-06-07 16:58:31 -0400930 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500931 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500932 // NOTE: We intentionally skip assign_expr, placement_expr, and
933 // range_expr, as they are not parsed in non-full mode.
934 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400935 }
936
David Tolnaybcf26022017-12-25 22:10:52 -0500937 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400938 macro_rules! binop {
939 (
940 $name: ident,
941 $next: ident,
942 $submac: ident!( $($args:tt)* )
943 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500944 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400945 mut e: call!($next, allow_struct, allow_block) >>
946 many0!(do_parse!(
947 op: $submac!($($args)*) >>
948 rhs: call!($next, allow_struct, true) >>
949 ({
950 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500951 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952 left: Box::new(e.into()),
953 op: op,
954 right: Box::new(rhs.into()),
955 }.into();
956 })
957 )) >>
958 (e)
959 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700960 }
David Tolnay54e854d2016-10-24 12:03:30 -0700961 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700962
David Tolnaybcf26022017-12-25 22:10:52 -0500963 // <placement> = <placement> ..
964 // <placement> += <placement> ..
965 // <placement> -= <placement> ..
966 // <placement> *= <placement> ..
967 // <placement> /= <placement> ..
968 // <placement> %= <placement> ..
969 // <placement> ^= <placement> ..
970 // <placement> &= <placement> ..
971 // <placement> |= <placement> ..
972 // <placement> <<= <placement> ..
973 // <placement> >>= <placement> ..
974 //
975 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400976 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500977 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978 mut e: call!(placement_expr, allow_struct, allow_block) >>
979 alt!(
980 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800981 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400982 // Recurse into self to parse right-associative operator.
983 rhs: call!(assign_expr, allow_struct, true) >>
984 ({
985 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500986 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500987 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400988 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -0500989 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400990 }.into();
991 })
992 )
993 |
994 do_parse!(
995 op: call!(BinOp::parse_assign_op) >>
996 // Recurse into self to parse right-associative operator.
997 rhs: call!(assign_expr, allow_struct, true) >>
998 ({
999 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -05001000 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001001 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001002 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001003 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004 }.into();
1005 })
1006 )
1007 |
1008 epsilon!()
1009 ) >>
1010 (e)
1011 ));
1012
David Tolnaybcf26022017-12-25 22:10:52 -05001013 // <range> <- <range> ..
1014 //
1015 // NOTE: The `in place { expr }` version of this syntax is parsed in
1016 // `atom_expr`, not here.
1017 //
1018 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -04001019 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001020 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001021 mut e: call!(range_expr, allow_struct, allow_block) >>
1022 alt!(
1023 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001024 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001025 // Recurse into self to parse right-associative operator.
1026 rhs: call!(placement_expr, allow_struct, true) >>
1027 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -04001028 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001029 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001030 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -05001031 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -05001032 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -05001033 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001034 }.into();
1035 })
1036 )
1037 |
1038 epsilon!()
1039 ) >>
1040 (e)
1041 ));
1042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // <or> ... <or> ..
1044 // <or> .. <or> ..
1045 // <or> ..
1046 //
1047 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
1048 // rules are for parsing these expressions are, but this is not correct.
1049 // For example, `a .. b .. c` is not a legal expression. It should not
1050 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
1051 //
1052 // NOTE: The form of ranges which don't include a preceding expression are
1053 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -04001054 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001055 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001056 mut e: call!(or_expr, allow_struct, allow_block) >>
1057 many0!(do_parse!(
1058 limits: syn!(RangeLimits) >>
1059 // We don't want to allow blocks here if we don't allow structs. See
1060 // the reasoning for `opt_ambiguous_expr!` above.
1061 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
1062 ({
1063 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -05001064 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001065 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -05001067 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001068 }.into();
1069 })
1070 )) >>
1071 (e)
1072 ));
1073
David Tolnaybcf26022017-12-25 22:10:52 -05001074 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001075 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001076
David Tolnaybcf26022017-12-25 22:10:52 -05001077 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001078 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001079
David Tolnaybcf26022017-12-25 22:10:52 -05001080 // <bitor> == <bitor> ...
1081 // <bitor> != <bitor> ...
1082 // <bitor> >= <bitor> ...
1083 // <bitor> <= <bitor> ...
1084 // <bitor> > <bitor> ...
1085 // <bitor> < <bitor> ...
1086 //
1087 // NOTE: This operator appears to be parsed as left-associative, but errors
1088 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001089 binop!(
1090 compare_expr,
1091 bitor_expr,
1092 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001093 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001094 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001095 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001096 |
1097 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001098 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001099 |
1100 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001101 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001102 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001103 do_parse!(
1104 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001105 not!(punct!(<-)) >>
1106 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001107 (BinOp::Lt(t))
1108 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001109 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001110 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001111 )
1112 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001113
David Tolnaybcf26022017-12-25 22:10:52 -05001114 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001115 binop!(
1116 bitor_expr,
1117 bitxor_expr,
1118 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1119 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001120
David Tolnaybcf26022017-12-25 22:10:52 -05001121 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001122 binop!(
1123 bitxor_expr,
1124 bitand_expr,
1125 do_parse!(
1126 // NOTE: Make sure we aren't looking at ^=.
1127 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1128 )
1129 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001130
David Tolnaybcf26022017-12-25 22:10:52 -05001131 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001132 binop!(
1133 bitand_expr,
1134 shift_expr,
1135 do_parse!(
1136 // NOTE: Make sure we aren't looking at && or &=.
1137 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1138 )
1139 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001140
David Tolnaybcf26022017-12-25 22:10:52 -05001141 // <arith> << <arith> ...
1142 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001143 binop!(
1144 shift_expr,
1145 arith_expr,
1146 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001147 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001148 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001149 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001150 )
1151 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001152
David Tolnaybcf26022017-12-25 22:10:52 -05001153 // <term> + <term> ...
1154 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001155 binop!(
1156 arith_expr,
1157 term_expr,
1158 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001159 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001160 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001161 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001162 )
1163 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001164
David Tolnaybcf26022017-12-25 22:10:52 -05001165 // <cast> * <cast> ...
1166 // <cast> / <cast> ...
1167 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001168 binop!(
1169 term_expr,
1170 cast_expr,
1171 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001172 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001174 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001175 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001176 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001177 )
1178 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179
David Tolnaybcf26022017-12-25 22:10:52 -05001180 // <unary> as <ty>
1181 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001182 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001183 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 mut e: call!(unary_expr, allow_struct, allow_block) >>
1185 many0!(alt!(
1186 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001187 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 // We can't accept `A + B` in cast expressions, as it's
1189 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001190 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001191 ({
1192 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001193 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001194 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001195 as_token: as_,
1196 ty: Box::new(ty),
1197 }.into();
1198 })
1199 )
1200 |
1201 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001202 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001203 // We can't accept `A + B` in cast expressions, as it's
1204 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001205 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001206 ({
1207 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001208 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001209 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001210 colon_token: colon,
1211 ty: Box::new(ty),
1212 }.into();
1213 })
1214 )
1215 )) >>
1216 (e)
1217 ));
1218
David Tolnay0cf94f22017-12-28 23:46:26 -05001219 // <unary> as <ty>
1220 #[cfg(not(feature = "full"))]
1221 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1222 mut e: call!(unary_expr, allow_struct, allow_block) >>
1223 many0!(do_parse!(
1224 as_: keyword!(as) >>
1225 // We can't accept `A + B` in cast expressions, as it's
1226 // ambiguous with the + expression.
1227 ty: call!(Type::without_plus) >>
1228 ({
1229 e = ExprCast {
1230 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001231 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001232 as_token: as_,
1233 ty: Box::new(ty),
1234 }.into();
1235 })
1236 )) >>
1237 (e)
1238 ));
1239
David Tolnaybcf26022017-12-25 22:10:52 -05001240 // <UnOp> <trailer>
1241 // & <trailer>
1242 // &mut <trailer>
1243 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001244 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001245 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001246 do_parse!(
1247 op: syn!(UnOp) >>
1248 expr: call!(unary_expr, allow_struct, true) >>
1249 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001250 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001251 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001252 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001253 }.into())
1254 )
1255 |
1256 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001257 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001258 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 expr: call!(unary_expr, allow_struct, true) >>
1260 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001261 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001262 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001263 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001264 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001265 }.into())
1266 )
1267 |
1268 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001269 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001270 expr: call!(unary_expr, allow_struct, true) >>
1271 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001272 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001273 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001274 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001275 }.into())
1276 )
1277 |
1278 call!(trailer_expr, allow_struct, allow_block)
1279 ));
1280
Michael Layzell734adb42017-06-07 16:58:31 -04001281 // XXX: This duplication is ugly
1282 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001283 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001284 do_parse!(
1285 op: syn!(UnOp) >>
1286 expr: call!(unary_expr, allow_struct, true) >>
1287 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001288 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001289 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001290 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001291 }.into())
1292 )
1293 |
1294 call!(trailer_expr, allow_struct, allow_block)
1295 ));
1296
David Tolnaybcf26022017-12-25 22:10:52 -05001297 // <atom> (..<args>) ...
1298 // <atom> . <ident> (..<args>) ...
1299 // <atom> . <ident> ...
1300 // <atom> . <lit> ...
1301 // <atom> [ <expr> ] ...
1302 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001303 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001304 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001305 mut e: call!(atom_expr, allow_struct, allow_block) >>
1306 many0!(alt!(
1307 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001308 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001309 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001310 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001311 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001312 args: args,
1313 paren_token: paren,
1314 }.into();
1315 })
1316 |
1317 tap!(more: and_method_call => {
1318 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001319 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 e = call.into();
1321 })
1322 |
1323 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001324 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001325 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001326 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001327 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001328 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001329 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 }.into();
1331 })
1332 |
1333 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001334 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001335 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001336 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001337 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001338 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001339 index: Box::new(i),
1340 }.into();
1341 })
1342 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001343 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001344 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001345 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001346 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001347 question_token: question,
1348 }.into();
1349 })
1350 )) >>
1351 (e)
1352 ));
1353
Michael Layzell734adb42017-06-07 16:58:31 -04001354 // XXX: Duplication == ugly
1355 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001356 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001357 mut e: call!(atom_expr, allow_struct, allow_block) >>
1358 many0!(alt!(
1359 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001360 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001361 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001362 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001363 paren_token: args.0,
1364 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001365 }.into();
1366 })
1367 |
1368 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001369 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001370 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001371 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001372 bracket_token: i.0,
1373 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001374 }.into();
1375 })
1376 )) >>
1377 (e)
1378 ));
1379
David Tolnaya454c8f2018-01-07 01:01:10 -08001380 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001381 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001382 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001383 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1384 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001385 |
David Tolnay8c91b882017-12-28 23:04:32 -05001386 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001387 |
1388 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001389 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001390 |
David Tolnay8c91b882017-12-28 23:04:32 -05001391 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001392 |
David Tolnay8c91b882017-12-28 23:04:32 -05001393 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001394 |
1395 call!(expr_break, allow_struct) // must be before expr_path
1396 |
David Tolnay8c91b882017-12-28 23:04:32 -05001397 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001398 |
1399 call!(expr_ret, allow_struct) // must be before expr_path
1400 |
David Tolnay8c91b882017-12-28 23:04:32 -05001401 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001402 |
David Tolnay8c91b882017-12-28 23:04:32 -05001403 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001404 |
David Tolnay8c91b882017-12-28 23:04:32 -05001405 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001406 |
David Tolnay8c91b882017-12-28 23:04:32 -05001407 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001408 |
David Tolnay8c91b882017-12-28 23:04:32 -05001409 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001410 |
David Tolnay8c91b882017-12-28 23:04:32 -05001411 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001412 |
David Tolnay8c91b882017-12-28 23:04:32 -05001413 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001414 |
David Tolnay8c91b882017-12-28 23:04:32 -05001415 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001416 |
David Tolnay8c91b882017-12-28 23:04:32 -05001417 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001418 |
David Tolnay8c91b882017-12-28 23:04:32 -05001419 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001420 |
David Tolnay8c91b882017-12-28 23:04:32 -05001421 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001422 |
David Tolnay8c91b882017-12-28 23:04:32 -05001423 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001424 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001425 call!(expr_closure, allow_struct)
1426 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001427 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001428 |
1429 // NOTE: This is the prefix-form of range
1430 call!(expr_range, allow_struct)
1431 |
David Tolnay8c91b882017-12-28 23:04:32 -05001432 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001433 |
David Tolnay8c91b882017-12-28 23:04:32 -05001434 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001435 ));
1436
Michael Layzell734adb42017-06-07 16:58:31 -04001437 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001438 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001439 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001440 |
David Tolnay8c91b882017-12-28 23:04:32 -05001441 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001442 ));
1443
Michael Layzell734adb42017-06-07 16:58:31 -04001444 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001445 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001446 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001447 |
David Tolnay8c91b882017-12-28 23:04:32 -05001448 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001449 |
David Tolnay8c91b882017-12-28 23:04:32 -05001450 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001451 |
David Tolnay8c91b882017-12-28 23:04:32 -05001452 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001453 |
David Tolnay8c91b882017-12-28 23:04:32 -05001454 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001455 |
David Tolnay8c91b882017-12-28 23:04:32 -05001456 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001457 |
David Tolnay8c91b882017-12-28 23:04:32 -05001458 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001459 |
David Tolnay8c91b882017-12-28 23:04:32 -05001460 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001461 |
David Tolnay8c91b882017-12-28 23:04:32 -05001462 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001463 |
David Tolnay8c91b882017-12-28 23:04:32 -05001464 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001465 |
David Tolnay8c91b882017-12-28 23:04:32 -05001466 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001467 ), Expr::from));
1468
David Tolnay8c91b882017-12-28 23:04:32 -05001469 impl Synom for ExprLit {
1470 named!(parse -> Self, do_parse!(
1471 lit: syn!(Lit) >>
1472 (ExprLit {
1473 attrs: Vec::new(),
1474 lit: lit,
1475 })
1476 ));
David Tolnay79777332018-01-07 10:04:42 -08001477
1478 fn description() -> Option<&'static str> {
1479 Some("literal")
1480 }
David Tolnay8c91b882017-12-28 23:04:32 -05001481 }
1482
1483 #[cfg(feature = "full")]
1484 impl Synom for ExprMacro {
1485 named!(parse -> Self, do_parse!(
1486 mac: syn!(Macro) >>
1487 (ExprMacro {
1488 attrs: Vec::new(),
1489 mac: mac,
1490 })
1491 ));
David Tolnay79777332018-01-07 10:04:42 -08001492
1493 fn description() -> Option<&'static str> {
1494 Some("macro invocation expression")
1495 }
David Tolnay8c91b882017-12-28 23:04:32 -05001496 }
1497
David Tolnaye98775f2017-12-28 23:17:00 -05001498 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001499 impl Synom for ExprGroup {
1500 named!(parse -> Self, do_parse!(
1501 e: grouped!(syn!(Expr)) >>
1502 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001503 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001504 expr: Box::new(e.1),
1505 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001506 })
Michael Layzell93c36282017-06-04 20:43:14 -04001507 ));
David Tolnay79777332018-01-07 10:04:42 -08001508
1509 fn description() -> Option<&'static str> {
1510 Some("expression surrounded by invisible delimiters")
1511 }
Michael Layzell93c36282017-06-04 20:43:14 -04001512 }
1513
David Tolnaye98775f2017-12-28 23:17:00 -05001514 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001515 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001516 named!(parse -> Self, do_parse!(
1517 e: parens!(syn!(Expr)) >>
1518 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001519 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001520 paren_token: e.0,
1521 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001522 })
Michael Layzell92639a52017-06-01 00:07:44 -04001523 ));
David Tolnay79777332018-01-07 10:04:42 -08001524
1525 fn description() -> Option<&'static str> {
1526 Some("parenthesized expression")
1527 }
Alex Crichton954046c2017-05-30 21:49:42 -07001528 }
David Tolnay89e05672016-10-02 14:39:42 -07001529
Michael Layzell734adb42017-06-07 16:58:31 -04001530 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001531 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001532 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001533 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001534 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001535 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001536 bracket_token: elems.0,
1537 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001538 })
1539 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001540
1541 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001542 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001543 }
Alex Crichton954046c2017-05-30 21:49:42 -07001544 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001545
David Tolnayf2cfd722017-12-31 18:02:51 -05001546 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1547 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001548
Michael Layzell734adb42017-06-07 16:58:31 -04001549 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001550 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001551 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001552 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001553 turbofish: option!(tuple!(
1554 punct!(::),
1555 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001556 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001557 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001558 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001559 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001560 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001561 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001562 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001563 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001564 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1565 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001566 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001567
Alex Crichton954046c2017-05-30 21:49:42 -07001568 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001569 turbofish: turbofish.map(|fish| MethodTurbofish {
1570 colon2_token: fish.0,
1571 lt_token: fish.1,
1572 args: fish.2,
1573 gt_token: fish.3,
1574 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001575 args: args.1,
1576 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001577 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001578 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001579 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001580 ));
1581
Michael Layzell734adb42017-06-07 16:58:31 -04001582 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001583 impl Synom for GenericMethodArgument {
1584 // TODO parse const generics as well
1585 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001586
1587 fn description() -> Option<&'static str> {
1588 Some("generic method argument")
1589 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001590 }
1591
1592 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001593 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001594 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001595 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001596 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001597 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001598 elems: elems.1,
1599 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001600 })
1601 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001602
1603 fn description() -> Option<&'static str> {
1604 Some("tuple")
1605 }
Alex Crichton954046c2017-05-30 21:49:42 -07001606 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001607
Michael Layzell734adb42017-06-07 16:58:31 -04001608 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001609 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001610 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001611 if_: keyword!(if) >>
1612 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001613 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001614 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001615 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001616 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001617 else_block: option!(else_block) >>
1618 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001619 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001620 pat: Box::new(pat),
1621 let_token: let_,
1622 eq_token: eq,
1623 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001624 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001625 brace_token: then_block.0,
1626 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001627 },
1628 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001629 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001630 })
1631 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001632
1633 fn description() -> Option<&'static str> {
1634 Some("`if let` expression")
1635 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001636 }
1637
Michael Layzell734adb42017-06-07 16:58:31 -04001638 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001639 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001640 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001641 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001642 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001643 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001644 else_block: option!(else_block) >>
1645 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001646 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001647 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001648 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001649 brace_token: then_block.0,
1650 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001651 },
1652 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001653 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001654 })
1655 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001656
1657 fn description() -> Option<&'static str> {
1658 Some("`if` expression")
1659 }
Alex Crichton954046c2017-05-30 21:49:42 -07001660 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001661
Michael Layzell734adb42017-06-07 16:58:31 -04001662 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001663 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001664 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001665 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001666 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001667 |
David Tolnay8c91b882017-12-28 23:04:32 -05001668 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001669 |
1670 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001671 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001672 (Expr::Block(ExprBlock {
1673 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001674 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001675 brace_token: else_block.0,
1676 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001677 },
1678 }))
David Tolnay939766a2016-09-23 23:48:12 -07001679 )
Alex Crichton954046c2017-05-30 21:49:42 -07001680 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001681 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001682 ));
1683
Michael Layzell734adb42017-06-07 16:58:31 -04001684 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001685 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001686 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001687 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001688 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001689 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001690 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001691 expr: expr_no_struct >>
1692 loop_block: syn!(Block) >>
1693 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001694 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001695 for_token: for_,
1696 in_token: in_,
1697 pat: Box::new(pat),
1698 expr: Box::new(expr),
1699 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001700 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001701 })
1702 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001703
1704 fn description() -> Option<&'static str> {
1705 Some("`for` loop")
1706 }
Alex Crichton954046c2017-05-30 21:49:42 -07001707 }
Gregory Katze5f35682016-09-27 14:20:55 -04001708
Michael Layzell734adb42017-06-07 16:58:31 -04001709 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001710 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001711 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001712 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001713 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001714 loop_block: syn!(Block) >>
1715 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001716 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001717 loop_token: loop_,
1718 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001719 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001720 })
1721 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001722
1723 fn description() -> Option<&'static str> {
1724 Some("`loop`")
1725 }
Alex Crichton954046c2017-05-30 21:49:42 -07001726 }
1727
Michael Layzell734adb42017-06-07 16:58:31 -04001728 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001729 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001730 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001731 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001732 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001733 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001734 (ExprMatch {
1735 attrs: Vec::new(),
1736 expr: Box::new(obj),
1737 match_token: match_,
1738 brace_token: res.0,
1739 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001740 })
1741 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001742
1743 fn description() -> Option<&'static str> {
1744 Some("`match` expression")
1745 }
Alex Crichton954046c2017-05-30 21:49:42 -07001746 }
David Tolnay1978c672016-10-27 22:05:52 -07001747
Michael Layzell734adb42017-06-07 16:58:31 -04001748 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001749 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001750 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001751 do_: keyword!(do) >>
1752 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001753 catch_block: syn!(Block) >>
1754 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001755 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001756 block: catch_block,
1757 do_token: do_,
1758 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001759 })
Michael Layzell92639a52017-06-01 00:07:44 -04001760 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001761
1762 fn description() -> Option<&'static str> {
1763 Some("`catch` expression")
1764 }
Alex Crichton954046c2017-05-30 21:49:42 -07001765 }
Arnavion02ef13f2017-04-25 00:54:31 -07001766
Michael Layzell734adb42017-06-07 16:58:31 -04001767 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001768 impl Synom for ExprYield {
1769 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001770 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001771 expr: option!(syn!(Expr)) >>
1772 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001773 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001774 yield_token: yield_,
1775 expr: expr.map(Box::new),
1776 })
1777 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001778
1779 fn description() -> Option<&'static str> {
1780 Some("`yield` expression")
1781 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001782 }
1783
1784 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001785 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001786 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001787 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001788 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001789 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1790 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001791 body: do_parse!(
1792 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001793 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1794 true => alt!(
1795 input_end!() => { |_| None }
1796 |
1797 punct!(,) => { Some }
1798 )
Alex Crichton03b30272017-08-28 09:35:24 -07001799 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001800 false => option!(punct!(,))
1801 ) >>
1802 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001803 ) >>
1804 (Arm {
1805 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001806 attrs: attrs,
1807 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001808 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001809 body: Box::new(body.0),
1810 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001811 })
1812 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001813
1814 fn description() -> Option<&'static str> {
1815 Some("`match` arm")
1816 }
Alex Crichton954046c2017-05-30 21:49:42 -07001817 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001818
Michael Layzell734adb42017-06-07 16:58:31 -04001819 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001820 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001821 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001822 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001823 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001824 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001825 ret_and_body: alt!(
1826 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001827 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001828 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001829 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001830 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001831 Expr::Block(ExprBlock {
1832 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001833 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001834 }))
David Tolnay89e05672016-10-02 14:39:42 -07001835 )
1836 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001837 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001838 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001839 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001840 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001841 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001842 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001843 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001844 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001845 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001846 body: Box::new(ret_and_body.1),
1847 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001848 ));
1849
Michael Layzell734adb42017-06-07 16:58:31 -04001850 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001851 named!(fn_arg -> FnArg, do_parse!(
1852 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001853 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001854 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001855 if let Some((colon, ty)) = ty {
1856 FnArg::Captured(ArgCaptured {
1857 pat: pat,
1858 colon_token: colon,
1859 ty: ty,
1860 })
1861 } else {
1862 FnArg::Inferred(pat)
1863 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001864 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001865 ));
1866
Michael Layzell734adb42017-06-07 16:58:31 -04001867 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001868 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001869 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001870 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001871 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001872 cond: expr_no_struct >>
1873 while_block: syn!(Block) >>
1874 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001875 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001876 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001877 cond: Box::new(cond),
1878 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001879 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001880 })
1881 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001882
1883 fn description() -> Option<&'static str> {
1884 Some("`while` expression")
1885 }
Alex Crichton954046c2017-05-30 21:49:42 -07001886 }
1887
Michael Layzell734adb42017-06-07 16:58:31 -04001888 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001889 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001890 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001891 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001892 while_: keyword!(while) >>
1893 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001894 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001895 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001896 value: expr_no_struct >>
1897 while_block: syn!(Block) >>
1898 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001899 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001900 eq_token: eq,
1901 let_token: let_,
1902 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001903 pat: Box::new(pat),
1904 expr: Box::new(value),
1905 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001906 label: label,
1907 })
1908 ));
David Tolnay79777332018-01-07 10:04:42 -08001909
1910 fn description() -> Option<&'static str> {
1911 Some("`while let` expression")
1912 }
David Tolnaybcd498f2017-12-29 12:02:33 -05001913 }
1914
1915 #[cfg(feature = "full")]
1916 impl Synom for Label {
1917 named!(parse -> Self, do_parse!(
1918 name: syn!(Lifetime) >>
1919 colon: punct!(:) >>
1920 (Label {
1921 name: name,
1922 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001923 })
1924 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001925
1926 fn description() -> Option<&'static str> {
1927 Some("`while let` expression")
1928 }
Alex Crichton954046c2017-05-30 21:49:42 -07001929 }
1930
Michael Layzell734adb42017-06-07 16:58:31 -04001931 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001932 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001933 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001934 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001935 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001936 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001937 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001938 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001939 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001940 })
1941 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001942
1943 fn description() -> Option<&'static str> {
1944 Some("`continue`")
1945 }
Alex Crichton954046c2017-05-30 21:49:42 -07001946 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001947
Michael Layzell734adb42017-06-07 16:58:31 -04001948 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001949 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001950 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001951 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001952 // We can't allow blocks after a `break` expression when we wouldn't
1953 // allow structs, as this expression is ambiguous.
1954 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001955 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001956 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001957 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001958 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001959 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001960 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001961 ));
1962
Michael Layzell734adb42017-06-07 16:58:31 -04001963 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001964 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001965 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001966 // NOTE: return is greedy and eats blocks after it even when in a
1967 // position where structs are not allowed, such as in if statement
1968 // conditions. For example:
1969 //
David Tolnaybcf26022017-12-25 22:10:52 -05001970 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001971 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001972 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001973 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001974 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001975 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001976 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001977 ));
1978
Michael Layzell734adb42017-06-07 16:58:31 -04001979 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001980 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001981 named!(parse -> Self, do_parse!(
1982 path: syn!(Path) >>
1983 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001984 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001985 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
1986 dots: punct!(..) >>
1987 base: syn!(Expr) >>
1988 (dots, base)
1989 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001990 (fields, base)
1991 )) >>
1992 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001993 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04001994 let (dots, rest) = match base.and_then(|b| b) {
1995 Some((dots, base)) => (Some(dots), Some(base)),
1996 None => (None, None),
1997 };
1998 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001999 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002000 brace_token: brace,
2001 path: path,
2002 fields: fields,
2003 dot2_token: dots,
2004 rest: rest.map(Box::new),
2005 }
2006 })
2007 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002008
2009 fn description() -> Option<&'static str> {
2010 Some("struct literal expression")
2011 }
Alex Crichton954046c2017-05-30 21:49:42 -07002012 }
2013
Michael Layzell734adb42017-06-07 16:58:31 -04002014 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002015 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04002016 named!(parse -> Self, alt!(
2017 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002018 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002019 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002020 value: syn!(Expr) >>
2021 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05002022 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002023 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07002024 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002025 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07002026 })
Michael Layzell92639a52017-06-01 00:07:44 -04002027 )
2028 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07002029 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05002030 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05002031 expr: Expr::Path(ExprPath {
2032 attrs: Vec::new(),
2033 qself: None,
2034 path: name.into(),
David Tolnay3bc597f2017-12-31 02:31:11 -05002035 }),
Michael Layzell92639a52017-06-01 00:07:44 -04002036 attrs: Vec::new(),
2037 colon_token: None,
2038 })
2039 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002040
2041 fn description() -> Option<&'static str> {
2042 Some("field-value pair: `field: value`")
2043 }
Alex Crichton954046c2017-05-30 21:49:42 -07002044 }
David Tolnay055a7042016-10-02 19:23:54 -07002045
Michael Layzell734adb42017-06-07 16:58:31 -04002046 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002047 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04002048 named!(parse -> Self, do_parse!(
2049 data: brackets!(do_parse!(
2050 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002051 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002052 times: syn!(Expr) >>
2053 (value, semi, times)
2054 )) >>
2055 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05002056 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05002057 expr: Box::new((data.1).0),
David Tolnay84d80442018-01-07 01:03:20 -08002058 len: Box::new((data.1).2),
David Tolnay8875fca2017-12-31 13:52:37 -05002059 bracket_token: data.0,
2060 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04002061 })
2062 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002063
2064 fn description() -> Option<&'static str> {
2065 Some("repeated array literal: `[val; N]`")
2066 }
Alex Crichton954046c2017-05-30 21:49:42 -07002067 }
David Tolnay055a7042016-10-02 19:23:54 -07002068
Michael Layzell734adb42017-06-07 16:58:31 -04002069 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002070 impl Synom for ExprUnsafe {
2071 named!(parse -> Self, do_parse!(
2072 unsafe_: keyword!(unsafe) >>
2073 b: syn!(Block) >>
2074 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05002075 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05002076 unsafe_token: unsafe_,
2077 block: b,
2078 })
2079 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002080
2081 fn description() -> Option<&'static str> {
2082 Some("unsafe block: `unsafe { .. }`")
2083 }
Nika Layzell640832a2017-12-04 13:37:09 -05002084 }
2085
2086 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002087 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002088 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04002089 b: syn!(Block) >>
2090 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002091 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002092 block: b,
2093 })
2094 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002095
2096 fn description() -> Option<&'static str> {
2097 Some("block: `{ .. }`")
2098 }
Alex Crichton954046c2017-05-30 21:49:42 -07002099 }
David Tolnay89e05672016-10-02 14:39:42 -07002100
Michael Layzell734adb42017-06-07 16:58:31 -04002101 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002102 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002103 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002104 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002105 (ExprRange {
2106 attrs: Vec::new(),
2107 from: None,
2108 to: hi.map(Box::new),
2109 limits: limits,
2110 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002111 ));
2112
Michael Layzell734adb42017-06-07 16:58:31 -04002113 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002114 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002115 named!(parse -> Self, alt!(
2116 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002117 punct!(..=) => { RangeLimits::Closed }
2118 |
2119 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002120 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002121 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002122 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002123 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002124
2125 fn description() -> Option<&'static str> {
2126 Some("range limit: `..`, `...` or `..=`")
2127 }
Alex Crichton954046c2017-05-30 21:49:42 -07002128 }
David Tolnay438c9052016-10-07 23:24:48 -07002129
Alex Crichton954046c2017-05-30 21:49:42 -07002130 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002131 named!(parse -> Self, do_parse!(
2132 pair: qpath >>
2133 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002134 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002135 qself: pair.0,
2136 path: pair.1,
2137 })
2138 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002139
2140 fn description() -> Option<&'static str> {
2141 Some("path: `a::b::c`")
2142 }
Alex Crichton954046c2017-05-30 21:49:42 -07002143 }
David Tolnay42602292016-10-01 22:25:45 -07002144
Michael Layzell734adb42017-06-07 16:58:31 -04002145 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002146 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002147
David Tolnay8875fca2017-12-31 13:52:37 -05002148 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002149
Michael Layzell734adb42017-06-07 16:58:31 -04002150 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002151 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002152 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002153 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002154 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002155 brace_token: stmts.0,
2156 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002157 })
2158 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002159
2160 fn description() -> Option<&'static str> {
2161 Some("block: `{ .. }`")
2162 }
Alex Crichton954046c2017-05-30 21:49:42 -07002163 }
David Tolnay939766a2016-09-23 23:48:12 -07002164
Michael Layzell734adb42017-06-07 16:58:31 -04002165 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002166 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002167 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002168 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002169 mut standalone: many0!(do_parse!(
2170 stmt: syn!(Stmt) >>
2171 many0!(punct!(;)) >>
2172 (stmt)
2173 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002174 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002175 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002176 mut e: syn!(Expr) >>
2177 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002178 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002179 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002180 })
2181 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002182 (match last {
2183 None => standalone,
2184 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002185 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002186 standalone
2187 }
2188 })
2189 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002190 }
2191
Michael Layzell734adb42017-06-07 16:58:31 -04002192 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002193 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002194 named!(parse -> Self, alt!(
2195 stmt_mac
2196 |
2197 stmt_local
2198 |
2199 stmt_item
2200 |
Michael Layzell35418782017-06-07 09:20:25 -04002201 stmt_blockexpr
2202 |
Michael Layzell92639a52017-06-01 00:07:44 -04002203 stmt_expr
2204 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002205
2206 fn description() -> Option<&'static str> {
2207 Some("statement")
2208 }
Alex Crichton954046c2017-05-30 21:49:42 -07002209 }
David Tolnay939766a2016-09-23 23:48:12 -07002210
Michael Layzell734adb42017-06-07 16:58:31 -04002211 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002212 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002213 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002214 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002215 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002216 // Only parse braces here; paren and bracket will get parsed as
2217 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002218 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002219 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002220 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002221 attrs: attrs,
2222 ident: None,
2223 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002224 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002225 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002226 delimiter: MacroDelimiter::Brace(data.0),
2227 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002228 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002229 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002230 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002231 ));
2232
Michael Layzell734adb42017-06-07 16:58:31 -04002233 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002234 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002235 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002236 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002237 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002238 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002239 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2240 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002241 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002242 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002243 let_token: let_,
2244 pat: Box::new(pat),
2245 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2246 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2247 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002248 }))
David Tolnay191e0582016-10-02 18:31:09 -07002249 ));
2250
Michael Layzell734adb42017-06-07 16:58:31 -04002251 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002252 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002253
Michael Layzell734adb42017-06-07 16:58:31 -04002254 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002255 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002256 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002257 mut e: expr_nosemi >>
2258 // If the next token is a `.` or a `?` it is special-cased to parse as
2259 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002260 not!(punct!(.)) >>
2261 not!(punct!(?)) >>
2262 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002263 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002264 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002265 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002266 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002267 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002268 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002269 }
2270 })
2271 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002272
Michael Layzell734adb42017-06-07 16:58:31 -04002273 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002274 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002275 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002276 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002277 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002278 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002279 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002280 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002281 })
David Tolnay939766a2016-09-23 23:48:12 -07002282 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002283
Michael Layzell734adb42017-06-07 16:58:31 -04002284 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002285 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002286 named!(parse -> Self, alt!(
2287 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2288 |
2289 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2290 |
2291 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2292 |
2293 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2294 |
2295 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2296 |
David Tolnay323279a2017-12-29 11:26:32 -05002297 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002298 |
2299 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2300 |
2301 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2302 |
2303 syn!(PatPath) => { Pat::Path }
2304 |
2305 syn!(PatTuple) => { Pat::Tuple }
2306 |
2307 syn!(PatRef) => { Pat::Ref }
2308 |
2309 syn!(PatSlice) => { Pat::Slice }
2310 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002311
2312 fn description() -> Option<&'static str> {
2313 Some("pattern")
2314 }
Alex Crichton954046c2017-05-30 21:49:42 -07002315 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002316
Michael Layzell734adb42017-06-07 16:58:31 -04002317 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002318 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002319 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002320 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002321 |u| PatWild { underscore_token: u }
2322 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002323
2324 fn description() -> Option<&'static str> {
2325 Some("wild pattern: `_`")
2326 }
Alex Crichton954046c2017-05-30 21:49:42 -07002327 }
David Tolnay84aa0752016-10-02 23:01:13 -07002328
Michael Layzell734adb42017-06-07 16:58:31 -04002329 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002330 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002331 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002332 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002333 pat: syn!(Pat) >>
2334 (PatBox {
2335 pat: Box::new(pat),
2336 box_token: boxed,
2337 })
2338 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002339
2340 fn description() -> Option<&'static str> {
2341 Some("box pattern")
2342 }
Alex Crichton954046c2017-05-30 21:49:42 -07002343 }
2344
Michael Layzell734adb42017-06-07 16:58:31 -04002345 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002346 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002347 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002348 by_ref: option!(keyword!(ref)) >>
2349 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002350 name: alt!(
2351 syn!(Ident)
2352 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002353 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002354 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002355 not!(punct!(<)) >>
2356 not!(punct!(::)) >>
2357 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002358 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002359 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002360 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002361 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002362 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002363 })
2364 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002365
2366 fn description() -> Option<&'static str> {
2367 Some("pattern identifier binding")
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 PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002373 named!(parse -> Self, do_parse!(
2374 path: syn!(Path) >>
2375 tuple: syn!(PatTuple) >>
2376 (PatTupleStruct {
2377 path: path,
2378 pat: tuple,
2379 })
2380 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002381
2382 fn description() -> Option<&'static str> {
2383 Some("tuple struct pattern")
2384 }
Alex Crichton954046c2017-05-30 21:49:42 -07002385 }
2386
Michael Layzell734adb42017-06-07 16:58:31 -04002387 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002388 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002389 named!(parse -> Self, do_parse!(
2390 path: syn!(Path) >>
2391 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002392 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002393 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002394 (fields, base)
2395 )) >>
2396 (PatStruct {
2397 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002398 fields: (data.1).0,
2399 brace_token: data.0,
2400 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002401 })
2402 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002403
2404 fn description() -> Option<&'static str> {
2405 Some("struct pattern")
2406 }
Alex Crichton954046c2017-05-30 21:49:42 -07002407 }
2408
Michael Layzell734adb42017-06-07 16:58:31 -04002409 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002410 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002411 named!(parse -> Self, alt!(
2412 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002413 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002414 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002415 pat: syn!(Pat) >>
2416 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002417 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002418 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002419 attrs: Vec::new(),
2420 colon_token: Some(colon),
2421 })
2422 )
2423 |
2424 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002425 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002426 by_ref: option!(keyword!(ref)) >>
2427 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002428 ident: syn!(Ident) >>
2429 ({
2430 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002431 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002432 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002433 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002434 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002435 }.into();
2436 if let Some(boxed) = boxed {
2437 pat = PatBox {
2438 pat: Box::new(pat),
2439 box_token: boxed,
2440 }.into();
2441 }
2442 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002443 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002444 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002445 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002446 colon_token: None,
2447 }
2448 })
2449 )
2450 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002451
2452 fn description() -> Option<&'static str> {
2453 Some("field pattern")
2454 }
Alex Crichton954046c2017-05-30 21:49:42 -07002455 }
2456
Michael Layzell734adb42017-06-07 16:58:31 -04002457 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002458 impl Synom for Member {
2459 named!(parse -> Self, alt!(
2460 syn!(Ident) => { Member::Named }
2461 |
2462 syn!(Index) => { Member::Unnamed }
2463 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002464
2465 fn description() -> Option<&'static str> {
2466 Some("field member")
2467 }
David Tolnay85b69a42017-12-27 20:43:10 -05002468 }
2469
2470 #[cfg(feature = "full")]
2471 impl Synom for Index {
2472 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002473 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002474 ({
David Tolnay360efd22018-01-04 23:35:26 -08002475 if let IntSuffix::None = lit.suffix() {
2476 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002477 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002478 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002479 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002480 })
David Tolnay85b69a42017-12-27 20:43:10 -05002481 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002482
2483 fn description() -> Option<&'static str> {
2484 Some("field index")
2485 }
David Tolnay85b69a42017-12-27 20:43:10 -05002486 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002487
Michael Layzell734adb42017-06-07 16:58:31 -04002488 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002489 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002490 named!(parse -> Self, map!(
2491 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002492 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002493 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002494
2495 fn description() -> Option<&'static str> {
2496 Some("path pattern")
2497 }
Alex Crichton954046c2017-05-30 21:49:42 -07002498 }
David Tolnay9636c052016-10-02 17:11:17 -07002499
Michael Layzell734adb42017-06-07 16:58:31 -04002500 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002501 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002502 named!(parse -> Self, do_parse!(
2503 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002504 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002505 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2506 tuple!(punct!(..), option!(punct!(,)))
2507 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002508 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002509 Some((_, Some(_))) => true,
2510 _ => false,
2511 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002512 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002513 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002514 )) >>
2515 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002516 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002517 let (dotdot, trailing) = match dotdot {
2518 Some((a, b)) => (Some(a), Some(b)),
2519 None => (None, None),
2520 };
2521 PatTuple {
2522 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002523 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002524 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002525 comma_token: trailing.unwrap_or_default(),
2526 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002527 }
2528 })
2529 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002530
2531 fn description() -> Option<&'static str> {
2532 Some("tuple pattern")
2533 }
Alex Crichton954046c2017-05-30 21:49:42 -07002534 }
David Tolnayfbb73232016-10-03 01:00:06 -07002535
Michael Layzell734adb42017-06-07 16:58:31 -04002536 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002537 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002538 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002539 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002540 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002541 pat: syn!(Pat) >>
2542 (PatRef {
2543 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002544 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002545 and_token: and,
2546 })
2547 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002548
2549 fn description() -> Option<&'static str> {
2550 Some("reference pattern")
2551 }
Alex Crichton954046c2017-05-30 21:49:42 -07002552 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002553
Michael Layzell734adb42017-06-07 16:58:31 -04002554 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002555 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002556 named!(parse -> Self, do_parse!(
2557 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002558 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002559 return parse_error(); // these need to be parsed by pat_path
2560 } else {
2561 PatLit {
2562 expr: Box::new(lit),
2563 }
2564 })
2565 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002566
2567 fn description() -> Option<&'static str> {
2568 Some("literal pattern")
2569 }
Alex Crichton954046c2017-05-30 21:49:42 -07002570 }
David Tolnaye1310902016-10-29 23:40:00 -07002571
Michael Layzell734adb42017-06-07 16:58:31 -04002572 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002573 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002574 named!(parse -> Self, do_parse!(
2575 lo: pat_lit_expr >>
2576 limits: syn!(RangeLimits) >>
2577 hi: pat_lit_expr >>
2578 (PatRange {
2579 lo: Box::new(lo),
2580 hi: Box::new(hi),
2581 limits: limits,
2582 })
2583 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002584
2585 fn description() -> Option<&'static str> {
2586 Some("range pattern")
2587 }
Alex Crichton954046c2017-05-30 21:49:42 -07002588 }
David Tolnaye1310902016-10-29 23:40:00 -07002589
Michael Layzell734adb42017-06-07 16:58:31 -04002590 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002591 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002592 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002593 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002594 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002595 |
David Tolnay8c91b882017-12-28 23:04:32 -05002596 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002597 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002598 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002599 Expr::Unary(ExprUnary {
2600 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002601 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002602 expr: Box::new(v)
2603 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002604 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002605 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002606 })
2607 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002608
Michael Layzell734adb42017-06-07 16:58:31 -04002609 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002610 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002611 named!(parse -> Self, map!(
2612 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002613 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002614 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002615 dots: punct!(..) >>
2616 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002617 (dots, trailing)
2618 )) >>
2619 after: cond!(
2620 match middle {
2621 Some((_, ref trailing)) => trailing.is_some(),
2622 _ => false,
2623 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002624 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002625 ) >>
2626 (before, middle, after)
2627 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002628 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002629 let mut before: Punctuated<Pat, Token![,]> = before;
2630 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002631 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002632 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002633 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002634 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002635 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002636 }),
2637 bracket_token: brackets,
2638 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002639 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002640 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002641 } else {
David Tolnay56080682018-01-06 14:01:52 -08002642 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002643 }
2644 }),
2645 front: before,
2646 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002647 }
Alex Crichton954046c2017-05-30 21:49:42 -07002648 }
Michael Layzell92639a52017-06-01 00:07:44 -04002649 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002650
2651 fn description() -> Option<&'static str> {
2652 Some("slice pattern")
2653 }
Alex Crichton954046c2017-05-30 21:49:42 -07002654 }
David Tolnay323279a2017-12-29 11:26:32 -05002655
2656 #[cfg(feature = "full")]
2657 impl Synom for PatMacro {
2658 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002659
2660 fn description() -> Option<&'static str> {
2661 Some("macro pattern")
2662 }
David Tolnay323279a2017-12-29 11:26:32 -05002663 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002664}
2665
David Tolnayf4bbbd92016-09-23 14:41:55 -07002666#[cfg(feature = "printing")]
2667mod printing {
2668 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002669 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002670 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002671 use quote::{ToTokens, Tokens};
David Tolnay61037c62018-01-05 16:21:03 -08002672 use proc_macro2::{Literal, TokenNode, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002673
David Tolnaybcf26022017-12-25 22:10:52 -05002674 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2675 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002676 #[cfg(feature = "full")]
2677 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002678 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002679 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002680 e.to_tokens(tokens);
2681 });
2682 } else {
2683 e.to_tokens(tokens);
2684 }
2685 }
2686
David Tolnay8c91b882017-12-28 23:04:32 -05002687 #[cfg(feature = "full")]
2688 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2689 tokens.append_all(attrs.outer());
2690 }
Michael Layzell734adb42017-06-07 16:58:31 -04002691
David Tolnay8c91b882017-12-28 23:04:32 -05002692 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002693 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002694
Michael Layzell734adb42017-06-07 16:58:31 -04002695 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002696 impl ToTokens for ExprBox {
2697 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002698 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002699 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002700 self.expr.to_tokens(tokens);
2701 }
2702 }
2703
Michael Layzell734adb42017-06-07 16:58:31 -04002704 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002705 impl ToTokens for ExprInPlace {
2706 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002707 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002708 self.place.to_tokens(tokens);
2709 self.arrow_token.to_tokens(tokens);
2710 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002711 }
2712 }
2713
Michael Layzell734adb42017-06-07 16:58:31 -04002714 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002715 impl ToTokens for ExprArray {
2716 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002717 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002718 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002719 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002720 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002721 }
2722 }
2723
2724 impl ToTokens for ExprCall {
2725 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002726 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002727 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002728 self.paren_token.surround(tokens, |tokens| {
2729 self.args.to_tokens(tokens);
2730 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002731 }
2732 }
2733
Michael Layzell734adb42017-06-07 16:58:31 -04002734 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002735 impl ToTokens for ExprMethodCall {
2736 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002737 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002738 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002739 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002740 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002741 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002742 self.paren_token.surround(tokens, |tokens| {
2743 self.args.to_tokens(tokens);
2744 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002745 }
2746 }
2747
Michael Layzell734adb42017-06-07 16:58:31 -04002748 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002749 impl ToTokens for MethodTurbofish {
2750 fn to_tokens(&self, tokens: &mut Tokens) {
2751 self.colon2_token.to_tokens(tokens);
2752 self.lt_token.to_tokens(tokens);
2753 self.args.to_tokens(tokens);
2754 self.gt_token.to_tokens(tokens);
2755 }
2756 }
2757
2758 #[cfg(feature = "full")]
2759 impl ToTokens for GenericMethodArgument {
2760 fn to_tokens(&self, tokens: &mut Tokens) {
2761 match *self {
2762 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2763 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2764 }
2765 }
2766 }
2767
2768 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002769 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002770 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002771 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002772 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002773 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002774 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002775 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002776 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002777 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002778 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002779 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002780 }
2781 }
2782
2783 impl ToTokens for ExprBinary {
2784 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002785 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002786 self.left.to_tokens(tokens);
2787 self.op.to_tokens(tokens);
2788 self.right.to_tokens(tokens);
2789 }
2790 }
2791
2792 impl ToTokens for ExprUnary {
2793 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002794 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002795 self.op.to_tokens(tokens);
2796 self.expr.to_tokens(tokens);
2797 }
2798 }
2799
David Tolnay8c91b882017-12-28 23:04:32 -05002800 impl ToTokens for ExprLit {
2801 fn to_tokens(&self, tokens: &mut Tokens) {
2802 attrs_to_tokens(&self.attrs, tokens);
2803 self.lit.to_tokens(tokens);
2804 }
2805 }
2806
Alex Crichton62a0a592017-05-22 13:58:53 -07002807 impl ToTokens for ExprCast {
2808 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002809 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002810 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002811 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002812 self.ty.to_tokens(tokens);
2813 }
2814 }
2815
David Tolnay0cf94f22017-12-28 23:46:26 -05002816 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002817 impl ToTokens for ExprType {
2818 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002819 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002820 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002821 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002822 self.ty.to_tokens(tokens);
2823 }
2824 }
2825
Michael Layzell734adb42017-06-07 16:58:31 -04002826 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002827 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002828 if let Some((ref else_token, ref else_)) = *else_ {
2829 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002830
2831 // If we are not one of the valid expressions to exist in an else
2832 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002833 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002834 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002835 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002836 }
2837 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002838 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002839 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002840 });
2841 }
2842 }
2843 }
2844 }
2845
2846 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002847 impl ToTokens for ExprIf {
2848 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002849 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002850 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002851 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002852 self.then_branch.to_tokens(tokens);
2853 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002854 }
2855 }
2856
Michael Layzell734adb42017-06-07 16:58:31 -04002857 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002858 impl ToTokens for ExprIfLet {
2859 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002860 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002861 self.if_token.to_tokens(tokens);
2862 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002863 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002864 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002865 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002866 self.then_branch.to_tokens(tokens);
2867 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002868 }
2869 }
2870
Michael Layzell734adb42017-06-07 16:58:31 -04002871 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002872 impl ToTokens for ExprWhile {
2873 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002874 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002875 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002876 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002877 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002878 self.body.to_tokens(tokens);
2879 }
2880 }
2881
Michael Layzell734adb42017-06-07 16:58:31 -04002882 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002883 impl ToTokens for ExprWhileLet {
2884 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002885 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002886 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002887 self.while_token.to_tokens(tokens);
2888 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002889 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002890 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002891 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002892 self.body.to_tokens(tokens);
2893 }
2894 }
2895
Michael Layzell734adb42017-06-07 16:58:31 -04002896 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002897 impl ToTokens for ExprForLoop {
2898 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002899 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002900 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002901 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002902 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002903 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002904 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002905 self.body.to_tokens(tokens);
2906 }
2907 }
2908
Michael Layzell734adb42017-06-07 16:58:31 -04002909 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002910 impl ToTokens for ExprLoop {
2911 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002912 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002913 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002914 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002915 self.body.to_tokens(tokens);
2916 }
2917 }
2918
Michael Layzell734adb42017-06-07 16:58:31 -04002919 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002920 impl ToTokens for ExprMatch {
2921 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002922 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002923 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002924 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002925 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002926 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002927 arm.to_tokens(tokens);
2928 // Ensure that we have a comma after a non-block arm, except
2929 // for the last one.
2930 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002931 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002932 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002933 }
2934 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002935 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002936 }
2937 }
2938
Michael Layzell734adb42017-06-07 16:58:31 -04002939 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002940 impl ToTokens for ExprCatch {
2941 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002942 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002943 self.do_token.to_tokens(tokens);
2944 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002945 self.block.to_tokens(tokens);
2946 }
2947 }
2948
Michael Layzell734adb42017-06-07 16:58:31 -04002949 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002950 impl ToTokens for ExprYield {
2951 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002952 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002953 self.yield_token.to_tokens(tokens);
2954 self.expr.to_tokens(tokens);
2955 }
2956 }
2957
2958 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002959 impl ToTokens for ExprClosure {
2960 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002961 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002962 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002963 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08002964 for input in self.inputs.pairs() {
2965 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05002966 FnArg::Captured(ArgCaptured {
2967 ref pat,
2968 ty: Type::Infer(_),
2969 ..
2970 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002971 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002972 }
David Tolnay56080682018-01-06 14:01:52 -08002973 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002974 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002975 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002976 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002977 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002978 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002979 self.body.to_tokens(tokens);
2980 }
2981 }
2982
Michael Layzell734adb42017-06-07 16:58:31 -04002983 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002984 impl ToTokens for ExprUnsafe {
2985 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002986 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002987 self.unsafe_token.to_tokens(tokens);
2988 self.block.to_tokens(tokens);
2989 }
2990 }
2991
2992 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002993 impl ToTokens for ExprBlock {
2994 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002995 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002996 self.block.to_tokens(tokens);
2997 }
2998 }
2999
Michael Layzell734adb42017-06-07 16:58:31 -04003000 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003001 impl ToTokens for ExprAssign {
3002 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003003 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003004 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003005 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003006 self.right.to_tokens(tokens);
3007 }
3008 }
3009
Michael Layzell734adb42017-06-07 16:58:31 -04003010 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003011 impl ToTokens for ExprAssignOp {
3012 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003013 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003014 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003015 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003016 self.right.to_tokens(tokens);
3017 }
3018 }
3019
Michael Layzell734adb42017-06-07 16:58:31 -04003020 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003021 impl ToTokens for ExprField {
3022 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003023 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05003024 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003025 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05003026 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003027 }
3028 }
3029
Michael Layzell734adb42017-06-07 16:58:31 -04003030 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05003031 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07003032 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003033 match *self {
3034 Member::Named(ident) => ident.to_tokens(tokens),
3035 Member::Unnamed(ref index) => index.to_tokens(tokens),
3036 }
3037 }
3038 }
3039
David Tolnay85b69a42017-12-27 20:43:10 -05003040 impl ToTokens for Index {
3041 fn to_tokens(&self, tokens: &mut Tokens) {
3042 tokens.append(TokenTree {
3043 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05003044 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05003045 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003046 }
3047 }
3048
3049 impl ToTokens for ExprIndex {
3050 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003051 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003052 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003053 self.bracket_token.surround(tokens, |tokens| {
3054 self.index.to_tokens(tokens);
3055 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003056 }
3057 }
3058
Michael Layzell734adb42017-06-07 16:58:31 -04003059 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003060 impl ToTokens for ExprRange {
3061 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003062 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003063 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003064 match self.limits {
3065 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3066 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
3067 }
Alex Crichton62a0a592017-05-22 13:58:53 -07003068 self.to.to_tokens(tokens);
3069 }
3070 }
3071
3072 impl ToTokens for ExprPath {
3073 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003074 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003075 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07003076 }
3077 }
3078
Michael Layzell734adb42017-06-07 16:58:31 -04003079 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003080 impl ToTokens for ExprAddrOf {
3081 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003082 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003083 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003084 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003085 self.expr.to_tokens(tokens);
3086 }
3087 }
3088
Michael Layzell734adb42017-06-07 16:58:31 -04003089 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003090 impl ToTokens for ExprBreak {
3091 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003092 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003093 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003094 self.label.to_tokens(tokens);
3095 self.expr.to_tokens(tokens);
3096 }
3097 }
3098
Michael Layzell734adb42017-06-07 16:58:31 -04003099 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003100 impl ToTokens for ExprContinue {
3101 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003102 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003103 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003104 self.label.to_tokens(tokens);
3105 }
3106 }
3107
Michael Layzell734adb42017-06-07 16:58:31 -04003108 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003109 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003110 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003111 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003112 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003113 self.expr.to_tokens(tokens);
3114 }
3115 }
3116
Michael Layzell734adb42017-06-07 16:58:31 -04003117 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003118 impl ToTokens for ExprMacro {
3119 fn to_tokens(&self, tokens: &mut Tokens) {
3120 tokens.append_all(self.attrs.outer());
3121 self.mac.to_tokens(tokens);
3122 }
3123 }
3124
3125 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003126 impl ToTokens for ExprStruct {
3127 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003128 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003129 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003130 self.brace_token.surround(tokens, |tokens| {
3131 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003132 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003133 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003134 self.rest.to_tokens(tokens);
3135 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003136 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003137 }
3138 }
3139
Michael Layzell734adb42017-06-07 16:58:31 -04003140 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003141 impl ToTokens for ExprRepeat {
3142 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003143 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003144 self.bracket_token.surround(tokens, |tokens| {
3145 self.expr.to_tokens(tokens);
3146 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003147 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003148 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003149 }
3150 }
3151
David Tolnaye98775f2017-12-28 23:17:00 -05003152 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003153 impl ToTokens for ExprGroup {
3154 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003155 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003156 self.group_token.surround(tokens, |tokens| {
3157 self.expr.to_tokens(tokens);
3158 });
3159 }
3160 }
3161
David Tolnaye98775f2017-12-28 23:17:00 -05003162 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003163 impl ToTokens for ExprParen {
3164 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003165 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003166 self.paren_token.surround(tokens, |tokens| {
3167 self.expr.to_tokens(tokens);
3168 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003169 }
3170 }
3171
Michael Layzell734adb42017-06-07 16:58:31 -04003172 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003173 impl ToTokens for ExprTry {
3174 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003175 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003176 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003177 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003178 }
3179 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003180
David Tolnay2ae520a2017-12-29 11:19:50 -05003181 impl ToTokens for ExprVerbatim {
3182 fn to_tokens(&self, tokens: &mut Tokens) {
3183 self.tts.to_tokens(tokens);
3184 }
3185 }
3186
Michael Layzell734adb42017-06-07 16:58:31 -04003187 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003188 impl ToTokens for Label {
3189 fn to_tokens(&self, tokens: &mut Tokens) {
3190 self.name.to_tokens(tokens);
3191 self.colon_token.to_tokens(tokens);
3192 }
3193 }
3194
3195 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003196 impl ToTokens for FieldValue {
3197 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003198 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003199 if let Some(ref colon_token) = self.colon_token {
3200 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003201 self.expr.to_tokens(tokens);
3202 }
David Tolnay055a7042016-10-02 19:23:54 -07003203 }
3204 }
3205
Michael Layzell734adb42017-06-07 16:58:31 -04003206 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003207 impl ToTokens for Arm {
3208 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003209 tokens.append_all(&self.attrs);
3210 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003211 if let Some((ref if_token, ref guard)) = self.guard {
3212 if_token.to_tokens(tokens);
3213 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003214 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003215 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003216 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003217 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003218 }
3219 }
3220
Michael Layzell734adb42017-06-07 16:58:31 -04003221 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003222 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003223 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003224 self.underscore_token.to_tokens(tokens);
3225 }
3226 }
3227
Michael Layzell734adb42017-06-07 16:58:31 -04003228 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003229 impl ToTokens for PatIdent {
3230 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003231 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003232 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003233 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003234 if let Some((ref at_token, ref subpat)) = self.subpat {
3235 at_token.to_tokens(tokens);
3236 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003237 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003238 }
3239 }
3240
Michael Layzell734adb42017-06-07 16:58:31 -04003241 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003242 impl ToTokens for PatStruct {
3243 fn to_tokens(&self, tokens: &mut Tokens) {
3244 self.path.to_tokens(tokens);
3245 self.brace_token.surround(tokens, |tokens| {
3246 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003247 // NOTE: We need a comma before the dot2 token if it is present.
3248 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003249 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003250 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003251 self.dot2_token.to_tokens(tokens);
3252 });
3253 }
3254 }
3255
Michael Layzell734adb42017-06-07 16:58:31 -04003256 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003257 impl ToTokens for PatTupleStruct {
3258 fn to_tokens(&self, tokens: &mut Tokens) {
3259 self.path.to_tokens(tokens);
3260 self.pat.to_tokens(tokens);
3261 }
3262 }
3263
Michael Layzell734adb42017-06-07 16:58:31 -04003264 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003265 impl ToTokens for PatPath {
3266 fn to_tokens(&self, tokens: &mut Tokens) {
3267 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3268 }
3269 }
3270
Michael Layzell734adb42017-06-07 16:58:31 -04003271 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003272 impl ToTokens for PatTuple {
3273 fn to_tokens(&self, tokens: &mut Tokens) {
3274 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003275 self.front.to_tokens(tokens);
3276 if let Some(ref dot2_token) = self.dot2_token {
3277 if !self.front.empty_or_trailing() {
3278 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003279 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003280 }
David Tolnay41871922017-12-29 01:53:45 -05003281 dot2_token.to_tokens(tokens);
3282 self.comma_token.to_tokens(tokens);
3283 if self.comma_token.is_none() && !self.back.is_empty() {
3284 // Ensure there is a comma after the .. token.
3285 <Token![,]>::default().to_tokens(tokens);
3286 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003287 }
David Tolnay41871922017-12-29 01:53:45 -05003288 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003289 });
3290 }
3291 }
3292
Michael Layzell734adb42017-06-07 16:58:31 -04003293 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003294 impl ToTokens for PatBox {
3295 fn to_tokens(&self, tokens: &mut Tokens) {
3296 self.box_token.to_tokens(tokens);
3297 self.pat.to_tokens(tokens);
3298 }
3299 }
3300
Michael Layzell734adb42017-06-07 16:58:31 -04003301 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003302 impl ToTokens for PatRef {
3303 fn to_tokens(&self, tokens: &mut Tokens) {
3304 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003305 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003306 self.pat.to_tokens(tokens);
3307 }
3308 }
3309
Michael Layzell734adb42017-06-07 16:58:31 -04003310 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003311 impl ToTokens for PatLit {
3312 fn to_tokens(&self, tokens: &mut Tokens) {
3313 self.expr.to_tokens(tokens);
3314 }
3315 }
3316
Michael Layzell734adb42017-06-07 16:58:31 -04003317 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003318 impl ToTokens for PatRange {
3319 fn to_tokens(&self, tokens: &mut Tokens) {
3320 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003321 match self.limits {
3322 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3323 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3324 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003325 self.hi.to_tokens(tokens);
3326 }
3327 }
3328
Michael Layzell734adb42017-06-07 16:58:31 -04003329 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003330 impl ToTokens for PatSlice {
3331 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003332 // XXX: This is a mess, and it will be so easy to screw it up. How
3333 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003334 self.bracket_token.surround(tokens, |tokens| {
3335 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003336
3337 // If we need a comma before the middle or standalone .. token,
3338 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003339 if !self.front.empty_or_trailing()
3340 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003341 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003342 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003343 }
3344
3345 // If we have an identifier, we always need a .. token.
3346 if self.middle.is_some() {
3347 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003348 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003349 } else if self.dot2_token.is_some() {
3350 self.dot2_token.to_tokens(tokens);
3351 }
3352
3353 // Make sure we have a comma before the back half.
3354 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003355 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003356 self.back.to_tokens(tokens);
3357 } else {
3358 self.comma_token.to_tokens(tokens);
3359 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003360 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003361 }
3362 }
3363
Michael Layzell734adb42017-06-07 16:58:31 -04003364 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003365 impl ToTokens for PatMacro {
3366 fn to_tokens(&self, tokens: &mut Tokens) {
3367 self.mac.to_tokens(tokens);
3368 }
3369 }
3370
3371 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003372 impl ToTokens for PatVerbatim {
3373 fn to_tokens(&self, tokens: &mut Tokens) {
3374 self.tts.to_tokens(tokens);
3375 }
3376 }
3377
3378 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003379 impl ToTokens for FieldPat {
3380 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003381 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003382 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003383 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003384 }
3385 self.pat.to_tokens(tokens);
3386 }
3387 }
3388
Michael Layzell734adb42017-06-07 16:58:31 -04003389 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003390 impl ToTokens for Block {
3391 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003392 self.brace_token.surround(tokens, |tokens| {
3393 tokens.append_all(&self.stmts);
3394 });
David Tolnay42602292016-10-01 22:25:45 -07003395 }
3396 }
3397
Michael Layzell734adb42017-06-07 16:58:31 -04003398 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003399 impl ToTokens for Stmt {
3400 fn to_tokens(&self, tokens: &mut Tokens) {
3401 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003402 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003403 Stmt::Item(ref item) => item.to_tokens(tokens),
3404 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003405 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003406 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003407 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003408 }
David Tolnay42602292016-10-01 22:25:45 -07003409 }
3410 }
3411 }
David Tolnay191e0582016-10-02 18:31:09 -07003412
Michael Layzell734adb42017-06-07 16:58:31 -04003413 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003414 impl ToTokens for Local {
3415 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003416 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003417 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003418 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003419 if let Some((ref colon_token, ref ty)) = self.ty {
3420 colon_token.to_tokens(tokens);
3421 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003422 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003423 if let Some((ref eq_token, ref init)) = self.init {
3424 eq_token.to_tokens(tokens);
3425 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003426 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003427 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003428 }
3429 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003430}