blob: 6a4fc69b5296cfa0e61b6b3729d8e6137797d37f [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay85b69a42017-12-27 20:43:10 -05003#[cfg(feature = "full")]
4use proc_macro2::Span;
5#[cfg(feature = "full")]
6use std::hash::{Hash, Hasher};
David Tolnayf4bbbd92016-09-23 14:41:55 -07007
Alex Crichton62a0a592017-05-22 13:58:53 -07008ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -05009 /// An expression.
10 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070011 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040012 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050013 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080014 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050015 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070016 }),
Clar Charrd22b5702017-03-10 15:24:56 -050017
David Tolnay8701a5c2017-12-28 23:31:10 -050018 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040019 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050020 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070021 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050022 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070023 pub value: Box<Expr>,
24 }),
Clar Charrd22b5702017-03-10 15:24:56 -050025
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040027 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050028 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050029 pub bracket_token: token::Bracket,
David Tolnay2a86fdd2017-12-28 23:34:28 -050030 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070031 }),
Clar Charrd22b5702017-03-10 15:24:56 -050032
Alex Crichton62a0a592017-05-22 13:58:53 -070033 /// A function call.
34 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050035 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050037 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050038 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 }),
Clar Charrd22b5702017-03-10 15:24:56 -050040
Alex Crichton62a0a592017-05-22 13:58:53 -070041 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
42 ///
43 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080044 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070045 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040046 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050047 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070048 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050050 pub method: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 pub colon2_token: Option<Token![::]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050052 pub lt_token: Option<Token![<]>,
53 pub typarams: Delimited<Type, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub gt_token: Option<Token![>]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050055 pub paren_token: token::Paren,
56 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
Clar Charrd22b5702017-03-10 15:24:56 -050058
Alex Crichton62a0a592017-05-22 13:58:53 -070059 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050060 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050061 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050062 pub paren_token: token::Paren,
David Tolnay2a86fdd2017-12-28 23:34:28 -050063 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }),
Clar Charrd22b5702017-03-10 15:24:56 -050065
Alex Crichton62a0a592017-05-22 13:58:53 -070066 /// A binary operation, e.g. `a + b`, `a * b`.
67 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050070 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub right: Box<Expr>,
72 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A unary operation, e.g. `!x`, `*x`.
75 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub op: UnOp,
78 pub expr: Box<Expr>,
79 }),
Clar Charrd22b5702017-03-10 15:24:56 -050080
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050082 pub Lit(ExprLit {
83 pub attrs: Vec<Attribute>,
84 pub lit: Lit,
85 }),
Clar Charrd22b5702017-03-10 15:24:56 -050086
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A cast, e.g. `foo as f64`.
88 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050089 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A type ascription, e.g. `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -050096 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -050097 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800100 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500102
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 /// An `if` block, with an optional else block
104 ///
105 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400106 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub cond: Box<Expr>,
110 pub if_true: Block,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub else_token: Option<Token![else]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500112 pub if_false: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500114
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 /// An `if let` expression with an optional else block
116 ///
117 /// E.g., `if let pat = expr { block } else { expr }`
118 ///
119 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400120 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500121 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub if_token: Token![if],
123 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500124 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800125 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500126 pub expr: Box<Expr>,
127 pub if_true: Block,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800128 pub else_token: Option<Token![else]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500129 pub if_false: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700130 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500131
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 /// A while loop, with an optional label
133 ///
134 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400135 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500136 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700137 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800138 pub colon_token: Option<Token![:]>,
139 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500140 pub cond: Box<Expr>,
141 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500143
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 /// A while-let loop, with an optional label.
145 ///
146 /// E.g., `'label: while let pat = expr { block }`
147 ///
148 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700151 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub colon_token: Option<Token![:]>,
153 pub while_token: Token![while],
154 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800156 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500157 pub expr: Box<Expr>,
158 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500160
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// A for loop, with an optional label.
162 ///
163 /// E.g., `'label: for pat in expr { block }`
164 ///
165 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400166 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500167 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500168 pub label: Option<Lifetime>,
169 pub colon_token: Option<Token![:]>,
170 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500172 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub expr: Box<Expr>,
174 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500176
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 /// Conditionless loop with an optional label.
178 ///
179 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400180 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500181 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700182 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800183 pub colon_token: Option<Token![:]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500184 pub loop_token: Token![loop],
185 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500187
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400189 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500190 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800191 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500193 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 pub arms: Vec<Arm>,
195 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500196
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400198 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500199 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 pub capture: CaptureBy,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub or1_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500202 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500204 pub output: ReturnType,
205 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500207
Nika Layzell640832a2017-12-04 13:37:09 -0500208 /// An unsafe block (`unsafe { ... }`)
209 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500210 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500211 pub unsafe_token: Token![unsafe],
212 pub block: Block,
213 }),
214
215 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400216 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500217 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub block: Block,
219 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700220
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400222 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500223 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800225 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500226 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500228
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 /// An assignment with an operator
230 ///
231 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400232 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500233 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500235 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 pub right: Box<Expr>,
237 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500238
David Tolnay85b69a42017-12-27 20:43:10 -0500239 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
240 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500243 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800244 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500245 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500247
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 /// An indexing operation (`foo[2]`)
249 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500250 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500252 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500253 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500255
David Tolnaybe55d7b2017-12-17 23:41:20 -0800256 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400257 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500258 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700260 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500261 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700263
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 /// Variable reference, possibly containing `::` and/or type
265 /// parameters, e.g. foo::bar::<baz>.
266 ///
267 /// Optionally "qualified",
268 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
269 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500270 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700271 pub qself: Option<QSelf>,
272 pub path: Path,
273 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700274
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400276 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500277 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800278 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub mutbl: Mutability,
280 pub expr: Box<Expr>,
281 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500282
Alex Crichton62a0a592017-05-22 13:58:53 -0700283 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400284 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500285 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500286 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700287 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 pub expr: Option<Box<Expr>>,
289 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500290
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400292 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500293 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800294 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500295 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500297
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500299 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500300 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500302 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500306 pub Macro(ExprMacro #full {
307 pub attrs: Vec<Attribute>,
308 pub mac: Macro,
309 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700310
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 /// A struct literal expression.
312 ///
313 /// For example, `Foo {x: 1, y: 2}`, or
314 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400315 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500316 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500318 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500319 pub fields: Delimited<FieldValue, Token![,]>,
320 pub dot2_token: Option<Token![..]>,
321 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700323
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 /// An array literal constructed from one repeated element.
325 ///
326 /// For example, `[1; 5]`. The first expression is the element
327 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400328 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500329 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500330 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500332 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700333 pub amt: Box<Expr>,
334 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700335
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500337 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500338 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500339 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500340 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700342
Michael Layzell93c36282017-06-04 20:43:14 -0400343 /// No-op: used solely so we can pretty-print faithfully
344 ///
345 /// A `group` represents a `None`-delimited span in the input
346 /// `TokenStream` which affects the precidence of the resulting
347 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500348 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500349 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500350 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500351 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400352 }),
353
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400355 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500356 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700357 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700360
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 /// A catch expression.
362 ///
363 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400364 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500365 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub do_token: Token![do],
367 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700368 pub block: Block,
369 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700370
371 /// A yield expression.
372 ///
373 /// E.g. `yield expr`
374 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500375 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800376 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700377 pub expr: Option<Box<Expr>>,
378 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700380}
381
David Tolnay8c91b882017-12-28 23:04:32 -0500382impl Expr {
383 // Not public API.
384 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500385 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500386 pub fn attrs_mut(&mut self) -> &mut Vec<Attribute> {
387 match *self {
388 Expr::Box(ExprBox { ref mut attrs, .. }) |
389 Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
390 Expr::Array(ExprArray { ref mut attrs, .. }) |
391 Expr::Call(ExprCall { ref mut attrs, .. }) |
392 Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
393 Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
394 Expr::Binary(ExprBinary { ref mut attrs, .. }) |
395 Expr::Unary(ExprUnary { ref mut attrs, .. }) |
396 Expr::Lit(ExprLit { ref mut attrs, .. }) |
397 Expr::Cast(ExprCast { ref mut attrs, .. }) |
398 Expr::Type(ExprType { ref mut attrs, .. }) |
399 Expr::If(ExprIf { ref mut attrs, .. }) |
400 Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
401 Expr::While(ExprWhile { ref mut attrs, .. }) |
402 Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
403 Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
404 Expr::Loop(ExprLoop { ref mut attrs, .. }) |
405 Expr::Match(ExprMatch { ref mut attrs, .. }) |
406 Expr::Closure(ExprClosure { ref mut attrs, .. }) |
407 Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
408 Expr::Block(ExprBlock { ref mut attrs, .. }) |
409 Expr::Assign(ExprAssign { ref mut attrs, .. }) |
410 Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
411 Expr::Field(ExprField { ref mut attrs, .. }) |
412 Expr::Index(ExprIndex { ref mut attrs, .. }) |
413 Expr::Range(ExprRange { ref mut attrs, .. }) |
414 Expr::Path(ExprPath { ref mut attrs, .. }) |
415 Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
416 Expr::Break(ExprBreak { ref mut attrs, .. }) |
417 Expr::Continue(ExprContinue { ref mut attrs, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500418 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500419 Expr::Macro(ExprMacro { ref mut attrs, .. }) |
420 Expr::Struct(ExprStruct { ref mut attrs, .. }) |
421 Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
422 Expr::Paren(ExprParen { ref mut attrs, .. }) |
423 Expr::Group(ExprGroup { ref mut attrs, .. }) |
424 Expr::Try(ExprTry { ref mut attrs, .. }) |
425 Expr::Catch(ExprCatch { ref mut attrs, .. }) |
426 Expr::Yield(ExprYield { ref mut attrs, .. }) => attrs,
427 }
428 }
429}
430
Michael Layzell734adb42017-06-07 16:58:31 -0400431#[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500432ast_enum! {
433 /// A struct or tuple struct field accessed in a struct literal or field
434 /// expression.
435 pub enum Member {
436 /// A named field like `self.x`.
437 Named(Ident),
438 /// An unnamed field like `self.0`.
439 Unnamed(Index),
440 }
441}
442
443#[cfg(feature = "full")]
444ast_struct! {
445 /// The index of an unnamed tuple struct field.
446 pub struct Index #manual_extra_traits {
447 pub index: u32,
448 pub span: Span,
449 }
450}
451
452#[cfg(feature = "full")]
453impl Eq for Index {}
454
455#[cfg(feature = "full")]
456impl PartialEq for Index {
457 fn eq(&self, other: &Self) -> bool {
458 self.index == other.index
459 }
460}
461
462#[cfg(feature = "full")]
463impl Hash for Index {
464 fn hash<H: Hasher>(&self, state: &mut H) {
465 self.index.hash(state);
466 }
467}
468
469#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700470ast_struct! {
471 /// A field-value pair in a struct literal.
472 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500473 /// Attributes tagged on the field.
474 pub attrs: Vec<Attribute>,
475
476 /// Name or index of the field.
477 pub member: Member,
478
479 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500480
Alex Crichton62a0a592017-05-22 13:58:53 -0700481 /// Value of the field.
482 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500483
Alex Crichton62a0a592017-05-22 13:58:53 -0700484 /// Whether this is a shorthand field, e.g. `Struct { x }`
485 /// instead of `Struct { x: x }`.
486 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 }
David Tolnay055a7042016-10-02 19:23:54 -0700488}
489
Michael Layzell734adb42017-06-07 16:58:31 -0400490#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700491ast_struct! {
492 /// A Block (`{ .. }`).
493 ///
494 /// E.g. `{ .. }` as in `fn foo() { .. }`
495 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500496 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 /// Statements in a block
498 pub stmts: Vec<Stmt>,
499 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700500}
501
Michael Layzell734adb42017-06-07 16:58:31 -0400502#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700503ast_enum! {
504 /// A statement, usually ending in a semicolon.
505 pub enum Stmt {
506 /// A local (let) binding.
507 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700508
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 /// An item definition.
510 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700511
Alex Crichton62a0a592017-05-22 13:58:53 -0700512 /// Expr without trailing semicolon.
513 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700514
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700517 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700518}
519
Michael Layzell734adb42017-06-07 16:58:31 -0400520#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700521ast_struct! {
522 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
523 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700526 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500527 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800528 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500529 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700530 /// Initializer expression to set the value, if any
531 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500532 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700533 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700534}
535
Michael Layzell734adb42017-06-07 16:58:31 -0400536#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700537ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 // Clippy false positive
539 // https://github.com/Manishearth/rust-clippy/issues/1241
540 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
541 pub enum Pat {
542 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700543 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800544 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700545 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700546
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
548 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
549 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
550 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700551 pub Ident(PatIdent {
552 pub mode: BindingMode,
553 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800554 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500555 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700557
Alex Crichton62a0a592017-05-22 13:58:53 -0700558 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
559 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700560 pub Struct(PatStruct {
561 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500562 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500563 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800564 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700565 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700566
Alex Crichton62a0a592017-05-22 13:58:53 -0700567 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
568 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
569 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700570 pub TupleStruct(PatTupleStruct {
571 pub path: Path,
572 pub pat: PatTuple,
573 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700574
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 /// A possibly qualified path pattern.
576 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
577 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
578 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700579 pub Path(PatPath {
580 pub qself: Option<QSelf>,
581 pub path: Path,
582 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700583
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 /// A tuple pattern `(a, b)`.
585 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
586 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500588 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500589 pub pats: Delimited<Pat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500591 pub dots_pos: Option<usize>,
592 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700593 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700594 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500597 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700598 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700599 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700600 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800601 pub and_token: Token![&],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500602 pub mutbl: Mutability,
603 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700604 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700605 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700606 pub Lit(PatLit {
607 pub expr: Box<Expr>,
608 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800609 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700610 pub Range(PatRange {
611 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700612 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500613 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700614 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400615 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700616 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500617 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800618 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700619 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500621 pub dot2_token: Option<Token![..]>,
622 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700623 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700624 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800625 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700627}
628
Michael Layzell734adb42017-06-07 16:58:31 -0400629#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700630ast_struct! {
631 /// An arm of a 'match'.
632 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800633 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700634 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500635 /// ```rust
636 /// # #![feature(dotdoteq_in_patterns)]
637 /// #
638 /// # fn main() {
639 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700640 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500641 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700642 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500643 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500645 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700646 /// ```
647 pub struct Arm {
648 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800649 pub pats: Delimited<Pat, Token![|]>,
650 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700651 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800652 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800654 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700655 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700656}
657
Michael Layzell734adb42017-06-07 16:58:31 -0400658#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700659ast_enum! {
660 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700661 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700662 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700664 Ref,
665 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700666}
667
Michael Layzell734adb42017-06-07 16:58:31 -0400668#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700669ast_enum! {
670 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700671 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700672 pub enum RangeLimits {
673 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800674 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700675 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800676 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700677 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700678}
679
Michael Layzell734adb42017-06-07 16:58:31 -0400680#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700681ast_struct! {
682 /// A single field in a struct pattern
683 ///
684 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
685 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
686 /// except `is_shorthand` is true
687 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500688 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500690 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500691 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 /// The pattern the field is destructured to
693 pub pat: Box<Pat>,
694 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700696}
697
Michael Layzell734adb42017-06-07 16:58:31 -0400698#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700699ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700700 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700701 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800702 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 ByValue(Mutability),
704 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700705}
706
Michael Layzell3936ceb2017-07-08 00:28:36 -0400707#[cfg(any(feature = "parsing", feature = "printing"))]
708#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700709fn arm_expr_requires_comma(expr: &Expr) -> bool {
710 // see https://github.com/rust-lang/rust/blob/eb8f2586e
711 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500712 match *expr {
713 Expr::Unsafe(..)
714 | Expr::Block(..)
715 | Expr::If(..)
716 | Expr::IfLet(..)
717 | Expr::Match(..)
718 | Expr::While(..)
719 | Expr::WhileLet(..)
720 | Expr::Loop(..)
721 | Expr::ForLoop(..)
722 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700723 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400724 }
725}
726
David Tolnayb9c8e322016-09-23 20:48:37 -0700727#[cfg(feature = "parsing")]
728pub mod parsing {
729 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700730 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700731
Michael Layzell734adb42017-06-07 16:58:31 -0400732 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500733 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500734 use synom::Synom;
735 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400736 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500737 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500738 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700739
David Tolnaybcf26022017-12-25 22:10:52 -0500740 // When we're parsing expressions which occur before blocks, like in an if
741 // statement's condition, we cannot parse a struct literal.
742 //
743 // Struct literals are ambiguous in certain positions
744 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700745 macro_rules! ambiguous_expr {
746 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700747 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700748 };
749 }
750
David Tolnaybcf26022017-12-25 22:10:52 -0500751 // When we are parsing an optional suffix expression, we cannot allow blocks
752 // if structs are not allowed.
753 //
754 // Example:
755 //
756 // if break {} {}
757 //
758 // is ambiguous between:
759 //
760 // if (break {}) {}
761 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400762 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400763 macro_rules! opt_ambiguous_expr {
764 ($i:expr, $allow_struct:ident) => {
765 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
766 };
767 }
768
Alex Crichton954046c2017-05-30 21:49:42 -0700769 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400770 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700771
772 fn description() -> Option<&'static str> {
773 Some("expression")
774 }
775 }
776
Michael Layzell734adb42017-06-07 16:58:31 -0400777 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700778 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
779
David Tolnaybcf26022017-12-25 22:10:52 -0500780 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400781 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500782 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500783 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400784 }
785
Michael Layzell734adb42017-06-07 16:58:31 -0400786 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500787 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500788 // NOTE: We intentionally skip assign_expr, placement_expr, and
789 // range_expr, as they are not parsed in non-full mode.
790 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400791 }
792
David Tolnaybcf26022017-12-25 22:10:52 -0500793 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400794 macro_rules! binop {
795 (
796 $name: ident,
797 $next: ident,
798 $submac: ident!( $($args:tt)* )
799 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500800 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400801 mut e: call!($next, allow_struct, allow_block) >>
802 many0!(do_parse!(
803 op: $submac!($($args)*) >>
804 rhs: call!($next, allow_struct, true) >>
805 ({
806 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500807 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400808 left: Box::new(e.into()),
809 op: op,
810 right: Box::new(rhs.into()),
811 }.into();
812 })
813 )) >>
814 (e)
815 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700816 }
David Tolnay54e854d2016-10-24 12:03:30 -0700817 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700818
David Tolnaybcf26022017-12-25 22:10:52 -0500819 // <placement> = <placement> ..
820 // <placement> += <placement> ..
821 // <placement> -= <placement> ..
822 // <placement> *= <placement> ..
823 // <placement> /= <placement> ..
824 // <placement> %= <placement> ..
825 // <placement> ^= <placement> ..
826 // <placement> &= <placement> ..
827 // <placement> |= <placement> ..
828 // <placement> <<= <placement> ..
829 // <placement> >>= <placement> ..
830 //
831 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400832 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500833 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400834 mut e: call!(placement_expr, allow_struct, allow_block) >>
835 alt!(
836 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800837 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400838 // Recurse into self to parse right-associative operator.
839 rhs: call!(assign_expr, allow_struct, true) >>
840 ({
841 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500842 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400843 left: Box::new(e.into()),
844 eq_token: eq,
845 right: Box::new(rhs.into()),
846 }.into();
847 })
848 )
849 |
850 do_parse!(
851 op: call!(BinOp::parse_assign_op) >>
852 // Recurse into self to parse right-associative operator.
853 rhs: call!(assign_expr, allow_struct, true) >>
854 ({
855 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500856 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400857 left: Box::new(e.into()),
858 op: op,
859 right: Box::new(rhs.into()),
860 }.into();
861 })
862 )
863 |
864 epsilon!()
865 ) >>
866 (e)
867 ));
868
David Tolnaybcf26022017-12-25 22:10:52 -0500869 // <range> <- <range> ..
870 //
871 // NOTE: The `in place { expr }` version of this syntax is parsed in
872 // `atom_expr`, not here.
873 //
874 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400875 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500876 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400877 mut e: call!(range_expr, allow_struct, allow_block) >>
878 alt!(
879 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800880 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400881 // Recurse into self to parse right-associative operator.
882 rhs: call!(placement_expr, allow_struct, true) >>
883 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400884 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500885 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400886 // op: BinOp::Place(larrow),
887 place: Box::new(e.into()),
David Tolnay8701a5c2017-12-28 23:31:10 -0500888 arrow_token: arrow,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400889 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400890 }.into();
891 })
892 )
893 |
894 epsilon!()
895 ) >>
896 (e)
897 ));
898
David Tolnaybcf26022017-12-25 22:10:52 -0500899 // <or> ... <or> ..
900 // <or> .. <or> ..
901 // <or> ..
902 //
903 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
904 // rules are for parsing these expressions are, but this is not correct.
905 // For example, `a .. b .. c` is not a legal expression. It should not
906 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
907 //
908 // NOTE: The form of ranges which don't include a preceding expression are
909 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400910 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500911 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400912 mut e: call!(or_expr, allow_struct, allow_block) >>
913 many0!(do_parse!(
914 limits: syn!(RangeLimits) >>
915 // We don't want to allow blocks here if we don't allow structs. See
916 // the reasoning for `opt_ambiguous_expr!` above.
917 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
918 ({
919 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500920 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400921 from: Some(Box::new(e.into())),
922 limits: limits,
923 to: hi.map(|e| Box::new(e.into())),
924 }.into();
925 })
926 )) >>
927 (e)
928 ));
929
David Tolnaybcf26022017-12-25 22:10:52 -0500930 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800931 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400932
David Tolnaybcf26022017-12-25 22:10:52 -0500933 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800934 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400935
David Tolnaybcf26022017-12-25 22:10:52 -0500936 // <bitor> == <bitor> ...
937 // <bitor> != <bitor> ...
938 // <bitor> >= <bitor> ...
939 // <bitor> <= <bitor> ...
940 // <bitor> > <bitor> ...
941 // <bitor> < <bitor> ...
942 //
943 // NOTE: This operator appears to be parsed as left-associative, but errors
944 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500945 binop!(
946 compare_expr,
947 bitor_expr,
948 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800949 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400950 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800951 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952 |
953 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800954 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400955 |
956 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400958 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400959 do_parse!(
960 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800961 not!(punct!(<-)) >>
962 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400963 (BinOp::Lt(t))
964 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400965 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800966 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500967 )
968 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400969
David Tolnaybcf26022017-12-25 22:10:52 -0500970 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500971 binop!(
972 bitor_expr,
973 bitxor_expr,
974 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
975 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400976
David Tolnaybcf26022017-12-25 22:10:52 -0500977 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500978 binop!(
979 bitxor_expr,
980 bitand_expr,
981 do_parse!(
982 // NOTE: Make sure we aren't looking at ^=.
983 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
984 )
985 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400986
David Tolnaybcf26022017-12-25 22:10:52 -0500987 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500988 binop!(
989 bitand_expr,
990 shift_expr,
991 do_parse!(
992 // NOTE: Make sure we aren't looking at && or &=.
993 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
994 )
995 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400996
David Tolnaybcf26022017-12-25 22:10:52 -0500997 // <arith> << <arith> ...
998 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -0500999 binop!(
1000 shift_expr,
1001 arith_expr,
1002 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001003 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001005 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001006 )
1007 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001008
David Tolnaybcf26022017-12-25 22:10:52 -05001009 // <term> + <term> ...
1010 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001011 binop!(
1012 arith_expr,
1013 term_expr,
1014 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001018 )
1019 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001020
David Tolnaybcf26022017-12-25 22:10:52 -05001021 // <cast> * <cast> ...
1022 // <cast> / <cast> ...
1023 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001024 binop!(
1025 term_expr,
1026 cast_expr,
1027 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001028 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001029 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001030 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001031 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001033 )
1034 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001035
David Tolnaybcf26022017-12-25 22:10:52 -05001036 // <unary> as <ty>
1037 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001038 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001039 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001040 mut e: call!(unary_expr, allow_struct, allow_block) >>
1041 many0!(alt!(
1042 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001043 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001044 // We can't accept `A + B` in cast expressions, as it's
1045 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001046 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001047 ({
1048 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001049 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001050 expr: Box::new(e.into()),
1051 as_token: as_,
1052 ty: Box::new(ty),
1053 }.into();
1054 })
1055 )
1056 |
1057 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001058 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001059 // We can't accept `A + B` in cast expressions, as it's
1060 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001061 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001062 ({
1063 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001064 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001065 expr: Box::new(e.into()),
1066 colon_token: colon,
1067 ty: Box::new(ty),
1068 }.into();
1069 })
1070 )
1071 )) >>
1072 (e)
1073 ));
1074
David Tolnay0cf94f22017-12-28 23:46:26 -05001075 // <unary> as <ty>
1076 #[cfg(not(feature = "full"))]
1077 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1078 mut e: call!(unary_expr, allow_struct, allow_block) >>
1079 many0!(do_parse!(
1080 as_: keyword!(as) >>
1081 // We can't accept `A + B` in cast expressions, as it's
1082 // ambiguous with the + expression.
1083 ty: call!(Type::without_plus) >>
1084 ({
1085 e = ExprCast {
1086 attrs: Vec::new(),
1087 expr: Box::new(e.into()),
1088 as_token: as_,
1089 ty: Box::new(ty),
1090 }.into();
1091 })
1092 )) >>
1093 (e)
1094 ));
1095
David Tolnaybcf26022017-12-25 22:10:52 -05001096 // <UnOp> <trailer>
1097 // & <trailer>
1098 // &mut <trailer>
1099 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001100 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001101 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001102 do_parse!(
1103 op: syn!(UnOp) >>
1104 expr: call!(unary_expr, allow_struct, true) >>
1105 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001106 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001107 op: op,
1108 expr: Box::new(expr.into()),
1109 }.into())
1110 )
1111 |
1112 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001113 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001114 mutability: syn!(Mutability) >>
1115 expr: call!(unary_expr, allow_struct, true) >>
1116 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001117 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001118 and_token: and,
1119 mutbl: mutability,
1120 expr: Box::new(expr.into()),
1121 }.into())
1122 )
1123 |
1124 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001125 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001126 expr: call!(unary_expr, allow_struct, true) >>
1127 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001128 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001129 box_token: box_,
1130 expr: Box::new(expr.into()),
1131 }.into())
1132 )
1133 |
1134 call!(trailer_expr, allow_struct, allow_block)
1135 ));
1136
Michael Layzell734adb42017-06-07 16:58:31 -04001137 // XXX: This duplication is ugly
1138 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001139 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001140 do_parse!(
1141 op: syn!(UnOp) >>
1142 expr: call!(unary_expr, allow_struct, true) >>
1143 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001144 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001145 op: op,
1146 expr: Box::new(expr.into()),
1147 }.into())
1148 )
1149 |
1150 call!(trailer_expr, allow_struct, allow_block)
1151 ));
1152
David Tolnaybcf26022017-12-25 22:10:52 -05001153 // <atom> (..<args>) ...
1154 // <atom> . <ident> (..<args>) ...
1155 // <atom> . <ident> ...
1156 // <atom> . <lit> ...
1157 // <atom> [ <expr> ] ...
1158 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001159 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001160 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001161 mut e: call!(atom_expr, allow_struct, allow_block) >>
1162 many0!(alt!(
1163 tap!(args: and_call => {
1164 let (args, paren) = args;
1165 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001166 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001167 func: Box::new(e.into()),
1168 args: args,
1169 paren_token: paren,
1170 }.into();
1171 })
1172 |
1173 tap!(more: and_method_call => {
1174 let mut call = more;
1175 call.expr = Box::new(e.into());
1176 e = call.into();
1177 })
1178 |
1179 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001180 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001182 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001183 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001185 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001186 }.into();
1187 })
1188 |
1189 tap!(i: and_index => {
1190 let (i, token) = i;
1191 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001192 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001193 expr: Box::new(e.into()),
1194 bracket_token: token,
1195 index: Box::new(i),
1196 }.into();
1197 })
1198 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001199 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001200 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001201 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001202 expr: Box::new(e.into()),
1203 question_token: question,
1204 }.into();
1205 })
1206 )) >>
1207 (e)
1208 ));
1209
Michael Layzell734adb42017-06-07 16:58:31 -04001210 // XXX: Duplication == ugly
1211 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001212 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001213 mut e: call!(atom_expr, allow_struct, allow_block) >>
1214 many0!(alt!(
1215 tap!(args: and_call => {
1216 let (args, paren) = args;
1217 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001218 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001219 func: Box::new(e.into()),
1220 args: args,
1221 paren_token: paren,
1222 }.into();
1223 })
1224 |
1225 tap!(i: and_index => {
1226 let (i, token) = i;
1227 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001228 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001229 expr: Box::new(e.into()),
1230 bracket_token: token,
1231 index: Box::new(i),
1232 }.into();
1233 })
1234 )) >>
1235 (e)
1236 ));
1237
David Tolnaybcf26022017-12-25 22:10:52 -05001238 // Parse all atomic expressions which don't have to worry about precidence
1239 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001240 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001241 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1242 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001243 |
David Tolnay8c91b882017-12-28 23:04:32 -05001244 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001245 |
1246 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001247 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 |
David Tolnay8c91b882017-12-28 23:04:32 -05001249 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 |
David Tolnay8c91b882017-12-28 23:04:32 -05001251 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 |
1253 call!(expr_break, allow_struct) // must be before expr_path
1254 |
David Tolnay8c91b882017-12-28 23:04:32 -05001255 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 |
1257 call!(expr_ret, allow_struct) // must be before expr_path
1258 |
David Tolnay8c91b882017-12-28 23:04:32 -05001259 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001260 |
David Tolnay8c91b882017-12-28 23:04:32 -05001261 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001262 |
David Tolnay8c91b882017-12-28 23:04:32 -05001263 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001264 |
David Tolnay8c91b882017-12-28 23:04:32 -05001265 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001266 |
David Tolnay8c91b882017-12-28 23:04:32 -05001267 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001268 |
David Tolnay8c91b882017-12-28 23:04:32 -05001269 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001270 |
David Tolnay8c91b882017-12-28 23:04:32 -05001271 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001272 |
David Tolnay8c91b882017-12-28 23:04:32 -05001273 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001274 |
David Tolnay8c91b882017-12-28 23:04:32 -05001275 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001276 |
David Tolnay8c91b882017-12-28 23:04:32 -05001277 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001278 |
David Tolnay8c91b882017-12-28 23:04:32 -05001279 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001280 |
David Tolnay8c91b882017-12-28 23:04:32 -05001281 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001282 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001283 call!(expr_closure, allow_struct)
1284 |
David Tolnay8c91b882017-12-28 23:04:32 -05001285 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001286 |
1287 // NOTE: This is the prefix-form of range
1288 call!(expr_range, allow_struct)
1289 |
David Tolnay8c91b882017-12-28 23:04:32 -05001290 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001291 |
David Tolnay8c91b882017-12-28 23:04:32 -05001292 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001293 ));
1294
Michael Layzell734adb42017-06-07 16:58:31 -04001295 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001296 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001297 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001298 |
David Tolnay8c91b882017-12-28 23:04:32 -05001299 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001300 ));
1301
Michael Layzell734adb42017-06-07 16:58:31 -04001302 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001303 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001304 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001305 |
David Tolnay8c91b882017-12-28 23:04:32 -05001306 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001307 |
David Tolnay8c91b882017-12-28 23:04:32 -05001308 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001309 |
David Tolnay8c91b882017-12-28 23:04:32 -05001310 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001311 |
David Tolnay8c91b882017-12-28 23:04:32 -05001312 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001313 |
David Tolnay8c91b882017-12-28 23:04:32 -05001314 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001315 |
David Tolnay8c91b882017-12-28 23:04:32 -05001316 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001317 |
David Tolnay8c91b882017-12-28 23:04:32 -05001318 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001319 |
David Tolnay8c91b882017-12-28 23:04:32 -05001320 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001321 |
David Tolnay8c91b882017-12-28 23:04:32 -05001322 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001323 |
David Tolnay8c91b882017-12-28 23:04:32 -05001324 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001325 ), Expr::from));
1326
David Tolnay8c91b882017-12-28 23:04:32 -05001327 impl Synom for ExprLit {
1328 named!(parse -> Self, do_parse!(
1329 lit: syn!(Lit) >>
1330 (ExprLit {
1331 attrs: Vec::new(),
1332 lit: lit,
1333 })
1334 ));
1335 }
1336
1337 #[cfg(feature = "full")]
1338 impl Synom for ExprMacro {
1339 named!(parse -> Self, do_parse!(
1340 mac: syn!(Macro) >>
1341 (ExprMacro {
1342 attrs: Vec::new(),
1343 mac: mac,
1344 })
1345 ));
1346 }
1347
David Tolnaye98775f2017-12-28 23:17:00 -05001348 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001349 impl Synom for ExprGroup {
1350 named!(parse -> Self, do_parse!(
1351 e: grouped!(syn!(Expr)) >>
1352 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001353 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001354 expr: Box::new(e.0),
1355 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001356 })
Michael Layzell93c36282017-06-04 20:43:14 -04001357 ));
1358 }
1359
David Tolnaye98775f2017-12-28 23:17:00 -05001360 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001361 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001362 named!(parse -> Self, do_parse!(
1363 e: parens!(syn!(Expr)) >>
1364 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001365 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001366 expr: Box::new(e.0),
1367 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001368 })
Michael Layzell92639a52017-06-01 00:07:44 -04001369 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001370 }
David Tolnay89e05672016-10-02 14:39:42 -07001371
Michael Layzell734adb42017-06-07 16:58:31 -04001372 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001373 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001374 named!(parse -> Self, do_parse!(
1375 elems: brackets!(call!(Delimited::parse_terminated)) >>
1376 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001377 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001378 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001379 bracket_token: elems.1,
1380 })
1381 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001382 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001383
David Tolnay32954ef2017-12-26 22:43:16 -05001384 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001385 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001386
Michael Layzell734adb42017-06-07 16:58:31 -04001387 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001388 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001389 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001390 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001391 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001392 colon2: punct!(::) >>
1393 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001394 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001395 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001396 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001397 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001398 args: parens!(call!(Delimited::parse_terminated)) >>
1399 ({
1400 let (colon2, lt, tys, gt) = match typarams {
1401 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1402 None => (None, None, None, None),
1403 };
1404 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001405 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001406 // this expr will get overwritten after being returned
David Tolnay8c91b882017-12-28 23:04:32 -05001407 expr: Box::new(Expr::Lit(ExprLit {
1408 attrs: Vec::new(),
1409 lit: Lit {
1410 span: Span::default(),
1411 value: LitKind::Bool(false),
1412 },
Alex Crichton954046c2017-05-30 21:49:42 -07001413 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001414
Alex Crichton954046c2017-05-30 21:49:42 -07001415 method: method,
1416 args: args.0,
1417 paren_token: args.1,
1418 dot_token: dot,
1419 lt_token: lt,
1420 gt_token: gt,
1421 colon2_token: colon2,
1422 typarams: tys.unwrap_or_default(),
1423 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001424 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001425 ));
1426
Michael Layzell734adb42017-06-07 16:58:31 -04001427 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001428 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001429 named!(parse -> Self, do_parse!(
1430 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001431 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001432 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001433 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001434 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001435 })
1436 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001437 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001438
Michael Layzell734adb42017-06-07 16:58:31 -04001439 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001440 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001441 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001442 if_: keyword!(if) >>
1443 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001444 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001445 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001446 cond: expr_no_struct >>
1447 then_block: braces!(call!(Block::parse_within)) >>
1448 else_block: option!(else_block) >>
1449 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001450 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001451 pat: Box::new(pat),
1452 let_token: let_,
1453 eq_token: eq,
1454 expr: Box::new(cond),
1455 if_true: Block {
1456 stmts: then_block.0,
1457 brace_token: then_block.1,
1458 },
1459 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001460 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001461 if_false: else_block.map(|p| Box::new(p.1.into())),
1462 })
1463 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001464 }
1465
Michael Layzell734adb42017-06-07 16:58:31 -04001466 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001467 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001468 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001469 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001470 cond: expr_no_struct >>
1471 then_block: braces!(call!(Block::parse_within)) >>
1472 else_block: option!(else_block) >>
1473 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001474 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001475 cond: Box::new(cond),
1476 if_true: Block {
1477 stmts: then_block.0,
1478 brace_token: then_block.1,
1479 },
1480 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001481 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001482 if_false: else_block.map(|p| Box::new(p.1.into())),
1483 })
1484 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001485 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001486
Michael Layzell734adb42017-06-07 16:58:31 -04001487 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001488 named!(else_block -> (Token![else], Expr), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001489 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001490 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001491 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001492 |
David Tolnay8c91b882017-12-28 23:04:32 -05001493 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001494 |
1495 do_parse!(
1496 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001497 (Expr::Block(ExprBlock {
1498 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001499 block: Block {
1500 stmts: else_block.0,
1501 brace_token: else_block.1,
1502 },
1503 }))
David Tolnay939766a2016-09-23 23:48:12 -07001504 )
Alex Crichton954046c2017-05-30 21:49:42 -07001505 ) >>
1506 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001507 ));
1508
Michael Layzell734adb42017-06-07 16:58:31 -04001509 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001510 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001511 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001512 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1513 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001514 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001515 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001516 expr: expr_no_struct >>
1517 loop_block: syn!(Block) >>
1518 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001519 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001520 for_token: for_,
1521 in_token: in_,
1522 pat: Box::new(pat),
1523 expr: Box::new(expr),
1524 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001525 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001526 label: lbl.map(|p| p.0),
1527 })
1528 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001529 }
Gregory Katze5f35682016-09-27 14:20:55 -04001530
Michael Layzell734adb42017-06-07 16:58:31 -04001531 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001532 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001533 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001534 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1535 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001536 loop_block: syn!(Block) >>
1537 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001538 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001539 loop_token: loop_,
1540 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001541 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001542 label: lbl.map(|p| p.0),
1543 })
1544 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001545 }
1546
Michael Layzell734adb42017-06-07 16:58:31 -04001547 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001548 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001549 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001550 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001551 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001552 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001553 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001554 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001555 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001556 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001557 expr: Box::new(obj),
1558 match_token: match_,
1559 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001560 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001561 }
1562 })
1563 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001564 }
David Tolnay1978c672016-10-27 22:05:52 -07001565
Michael Layzell734adb42017-06-07 16:58:31 -04001566 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001567 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001568 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001569 do_: keyword!(do) >>
1570 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001571 catch_block: syn!(Block) >>
1572 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001573 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001574 block: catch_block,
1575 do_token: do_,
1576 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001577 })
Michael Layzell92639a52017-06-01 00:07:44 -04001578 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001579 }
Arnavion02ef13f2017-04-25 00:54:31 -07001580
Michael Layzell734adb42017-06-07 16:58:31 -04001581 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001582 impl Synom for ExprYield {
1583 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001584 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001585 expr: option!(syn!(Expr)) >>
1586 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001587 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001588 yield_token: yield_,
1589 expr: expr.map(Box::new),
1590 })
1591 ));
1592 }
1593
1594 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001595 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001596 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001597 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001598 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001599 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1600 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001601 body: do_parse!(
1602 expr: alt!(expr_nosemi | syn!(Expr)) >>
1603 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1604 map!(input_end!(), |_| None)
1605 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001606 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001607 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001608 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001609 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001610 ) >>
1611 (Arm {
1612 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001613 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001614 attrs: attrs,
1615 pats: pats,
1616 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001617 body: Box::new(body.0),
1618 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001619 })
1620 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001621 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001622
Michael Layzell734adb42017-06-07 16:58:31 -04001623 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001624 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001625 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001626 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001627 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001628 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001629 ret_and_body: alt!(
1630 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001631 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001632 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001633 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001634 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001635 Expr::Block(ExprBlock {
1636 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001637 block: body,
1638 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001639 )
1640 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001641 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001642 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001643 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001644 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001646 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001647 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001648 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001649 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001650 body: Box::new(ret_and_body.1),
1651 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001652 ));
1653
Michael Layzell734adb42017-06-07 16:58:31 -04001654 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001655 named!(fn_arg -> FnArg, do_parse!(
1656 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001657 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001658 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001659 if let Some((colon, ty)) = ty {
1660 FnArg::Captured(ArgCaptured {
1661 pat: pat,
1662 colon_token: colon,
1663 ty: ty,
1664 })
1665 } else {
1666 FnArg::Inferred(pat)
1667 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001668 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001669 ));
1670
Michael Layzell734adb42017-06-07 16:58:31 -04001671 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001672 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001673 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001674 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1675 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001676 cond: expr_no_struct >>
1677 while_block: syn!(Block) >>
1678 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001679 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001680 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001681 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001682 cond: Box::new(cond),
1683 body: while_block,
1684 label: lbl.map(|p| p.0),
1685 })
1686 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001687 }
1688
Michael Layzell734adb42017-06-07 16:58:31 -04001689 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001690 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001691 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001692 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1693 while_: keyword!(while) >>
1694 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001695 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001696 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001697 value: expr_no_struct >>
1698 while_block: syn!(Block) >>
1699 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001700 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001701 eq_token: eq,
1702 let_token: let_,
1703 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001704 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001705 pat: Box::new(pat),
1706 expr: Box::new(value),
1707 body: while_block,
1708 label: lbl.map(|p| p.0),
1709 })
1710 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001711 }
1712
Michael Layzell734adb42017-06-07 16:58:31 -04001713 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001714 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001715 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001716 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001717 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001718 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001719 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001720 continue_token: cont,
1721 label: lbl,
1722 })
1723 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001724 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001725
Michael Layzell734adb42017-06-07 16:58:31 -04001726 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001727 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001728 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001729 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001730 // We can't allow blocks after a `break` expression when we wouldn't
1731 // allow structs, as this expression is ambiguous.
1732 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001733 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001734 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001735 label: lbl,
1736 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001737 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001738 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001739 ));
1740
Michael Layzell734adb42017-06-07 16:58:31 -04001741 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001742 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001743 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001744 // NOTE: return is greedy and eats blocks after it even when in a
1745 // position where structs are not allowed, such as in if statement
1746 // conditions. For example:
1747 //
David Tolnaybcf26022017-12-25 22:10:52 -05001748 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001749 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001750 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001751 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001752 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001753 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001754 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001755 ));
1756
Michael Layzell734adb42017-06-07 16:58:31 -04001757 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001758 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001759 named!(parse -> Self, do_parse!(
1760 path: syn!(Path) >>
1761 data: braces!(do_parse!(
1762 fields: call!(Delimited::parse_terminated) >>
1763 base: option!(
1764 cond!(fields.is_empty() || fields.trailing_delim(),
1765 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001766 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001767 base: syn!(Expr) >>
1768 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001769 )
Michael Layzell92639a52017-06-01 00:07:44 -04001770 )
1771 ) >>
1772 (fields, base)
1773 )) >>
1774 ({
1775 let ((fields, base), brace) = data;
1776 let (dots, rest) = match base.and_then(|b| b) {
1777 Some((dots, base)) => (Some(dots), Some(base)),
1778 None => (None, None),
1779 };
1780 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001781 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001782 brace_token: brace,
1783 path: path,
1784 fields: fields,
1785 dot2_token: dots,
1786 rest: rest.map(Box::new),
1787 }
1788 })
1789 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001790 }
1791
Michael Layzell734adb42017-06-07 16:58:31 -04001792 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001793 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001794 named!(parse -> Self, alt!(
1795 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001796 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001797 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001798 value: syn!(Expr) >>
1799 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001800 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001801 expr: value,
1802 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001803 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001804 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001805 })
Michael Layzell92639a52017-06-01 00:07:44 -04001806 )
1807 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001808 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001809 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001810 expr: Expr::Path(ExprPath {
1811 attrs: Vec::new(),
1812 qself: None,
1813 path: name.into(),
1814 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001815 is_shorthand: true,
1816 attrs: Vec::new(),
1817 colon_token: None,
1818 })
1819 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001820 }
David Tolnay055a7042016-10-02 19:23:54 -07001821
Michael Layzell734adb42017-06-07 16:58:31 -04001822 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001823 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001824 named!(parse -> Self, do_parse!(
1825 data: brackets!(do_parse!(
1826 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001827 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001828 times: syn!(Expr) >>
1829 (value, semi, times)
1830 )) >>
1831 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001832 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001833 expr: Box::new((data.0).0),
1834 amt: Box::new((data.0).2),
1835 bracket_token: data.1,
1836 semi_token: (data.0).1,
1837 })
1838 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001839 }
David Tolnay055a7042016-10-02 19:23:54 -07001840
Michael Layzell734adb42017-06-07 16:58:31 -04001841 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001842 impl Synom for ExprUnsafe {
1843 named!(parse -> Self, do_parse!(
1844 unsafe_: keyword!(unsafe) >>
1845 b: syn!(Block) >>
1846 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001847 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001848 unsafe_token: unsafe_,
1849 block: b,
1850 })
1851 ));
1852 }
1853
1854 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001855 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001856 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001857 b: syn!(Block) >>
1858 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001859 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001860 block: b,
1861 })
1862 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001863 }
David Tolnay89e05672016-10-02 14:39:42 -07001864
Michael Layzell734adb42017-06-07 16:58:31 -04001865 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001866 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001867 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001868 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001869 (ExprRange {
1870 attrs: Vec::new(),
1871 from: None,
1872 to: hi.map(Box::new),
1873 limits: limits,
1874 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001875 ));
1876
Michael Layzell734adb42017-06-07 16:58:31 -04001877 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001878 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001879 named!(parse -> Self, alt!(
1880 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001881 punct!(..=) => { RangeLimits::Closed }
1882 |
1883 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001884 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001885 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001886 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001887 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001888 }
David Tolnay438c9052016-10-07 23:24:48 -07001889
Alex Crichton954046c2017-05-30 21:49:42 -07001890 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001891 named!(parse -> Self, do_parse!(
1892 pair: qpath >>
1893 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001894 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001895 qself: pair.0,
1896 path: pair.1,
1897 })
1898 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001899 }
David Tolnay42602292016-10-01 22:25:45 -07001900
Michael Layzell734adb42017-06-07 16:58:31 -04001901 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001902 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001903
David Tolnay32954ef2017-12-26 22:43:16 -05001904 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001905
Michael Layzell734adb42017-06-07 16:58:31 -04001906 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001907 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001908 named!(parse -> Self, do_parse!(
1909 stmts: braces!(call!(Block::parse_within)) >>
1910 (Block {
1911 stmts: stmts.0,
1912 brace_token: stmts.1,
1913 })
1914 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001915 }
David Tolnay939766a2016-09-23 23:48:12 -07001916
Michael Layzell734adb42017-06-07 16:58:31 -04001917 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001918 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001919 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001920 many0!(punct!(;)) >>
1921 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001922 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001923 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001924 mut e: syn!(Expr) >>
1925 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001926 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001927 Stmt::Expr(Box::new(e))
1928 })
1929 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001930 (match last {
1931 None => standalone,
1932 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001933 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001934 standalone
1935 }
1936 })
1937 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001938 }
1939
Michael Layzell734adb42017-06-07 16:58:31 -04001940 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001941 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001942 named!(parse -> Self, alt!(
1943 stmt_mac
1944 |
1945 stmt_local
1946 |
1947 stmt_item
1948 |
Michael Layzell35418782017-06-07 09:20:25 -04001949 stmt_blockexpr
1950 |
Michael Layzell92639a52017-06-01 00:07:44 -04001951 stmt_expr
1952 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001953 }
David Tolnay939766a2016-09-23 23:48:12 -07001954
Michael Layzell734adb42017-06-07 16:58:31 -04001955 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001956 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001957 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001958 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001959 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001960 // Only parse braces here; paren and bracket will get parsed as
1961 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001962 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001963 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001964 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1965 attrs: attrs,
1966 ident: None,
1967 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001968 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001969 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001970 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001971 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001972 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001973 },
David Tolnayeea28d62016-10-25 20:44:08 -07001974 },
David Tolnay57b52bc2017-12-28 18:06:38 -05001975 semi_token: semi,
1976 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07001977 ));
1978
Michael Layzell734adb42017-06-07 16:58:31 -04001979 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001980 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001981 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001982 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001983 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001984 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001985 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1986 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001987 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001988 let_token: let_,
1989 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001990 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
1991 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001992 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001993 ty: ty.map(|p| Box::new(p.1)),
1994 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001995 attrs: attrs,
1996 })))
1997 ));
1998
Michael Layzell734adb42017-06-07 16:58:31 -04001999 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002000 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002001
Michael Layzell734adb42017-06-07 16:58:31 -04002002 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002003 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002004 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002005 mut e: expr_nosemi >>
2006 // If the next token is a `.` or a `?` it is special-cased to parse as
2007 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002008 not!(punct!(.)) >>
2009 not!(punct!(?)) >>
2010 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002011 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002012 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002013 if let Some(semi) = semi {
2014 Stmt::Semi(Box::new(e), semi)
2015 } else {
2016 Stmt::Expr(Box::new(e))
2017 }
2018 })
2019 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002020
Michael Layzell734adb42017-06-07 16:58:31 -04002021 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002022 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002023 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002024 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002025 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002026 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002027 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002028 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002029 })
David Tolnay939766a2016-09-23 23:48:12 -07002030 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002031
Michael Layzell734adb42017-06-07 16:58:31 -04002032 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002033 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002034 named!(parse -> Self, alt!(
2035 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2036 |
2037 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2038 |
2039 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2040 |
2041 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2042 |
2043 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2044 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002045 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002046 |
2047 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2048 |
2049 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2050 |
2051 syn!(PatPath) => { Pat::Path }
2052 |
2053 syn!(PatTuple) => { Pat::Tuple }
2054 |
2055 syn!(PatRef) => { Pat::Ref }
2056 |
2057 syn!(PatSlice) => { Pat::Slice }
2058 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002059 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002060
Michael Layzell734adb42017-06-07 16:58:31 -04002061 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002062 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002063 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002064 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002065 |u| PatWild { underscore_token: u }
2066 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002067 }
David Tolnay84aa0752016-10-02 23:01:13 -07002068
Michael Layzell734adb42017-06-07 16:58:31 -04002069 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002070 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002071 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002072 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002073 pat: syn!(Pat) >>
2074 (PatBox {
2075 pat: Box::new(pat),
2076 box_token: boxed,
2077 })
2078 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002079 }
2080
Michael Layzell734adb42017-06-07 16:58:31 -04002081 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002082 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002083 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002084 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002085 mutability: syn!(Mutability) >>
2086 name: alt!(
2087 syn!(Ident)
2088 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002089 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002090 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002091 not!(punct!(<)) >>
2092 not!(punct!(::)) >>
2093 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002094 (PatIdent {
2095 mode: match mode {
2096 Some(mode) => BindingMode::ByRef(mode, mutability),
2097 None => BindingMode::ByValue(mutability),
2098 },
2099 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002100 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002101 subpat: subpat.map(|p| Box::new(p.1)),
2102 })
2103 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002104 }
2105
Michael Layzell734adb42017-06-07 16:58:31 -04002106 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002107 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002108 named!(parse -> Self, do_parse!(
2109 path: syn!(Path) >>
2110 tuple: syn!(PatTuple) >>
2111 (PatTupleStruct {
2112 path: path,
2113 pat: tuple,
2114 })
2115 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002116 }
2117
Michael Layzell734adb42017-06-07 16:58:31 -04002118 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002119 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002120 named!(parse -> Self, do_parse!(
2121 path: syn!(Path) >>
2122 data: braces!(do_parse!(
2123 fields: call!(Delimited::parse_terminated) >>
2124 base: option!(
2125 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002126 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002127 ) >>
2128 (fields, base)
2129 )) >>
2130 (PatStruct {
2131 path: path,
2132 fields: (data.0).0,
2133 brace_token: data.1,
2134 dot2_token: (data.0).1.and_then(|m| m),
2135 })
2136 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002137 }
2138
Michael Layzell734adb42017-06-07 16:58:31 -04002139 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002140 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002141 named!(parse -> Self, alt!(
2142 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002143 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002144 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002145 pat: syn!(Pat) >>
2146 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002147 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002148 pat: Box::new(pat),
2149 is_shorthand: false,
2150 attrs: Vec::new(),
2151 colon_token: Some(colon),
2152 })
2153 )
2154 |
2155 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002156 boxed: option!(keyword!(box)) >>
2157 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002158 mutability: syn!(Mutability) >>
2159 ident: syn!(Ident) >>
2160 ({
2161 let mut pat: Pat = PatIdent {
2162 mode: if let Some(mode) = mode {
2163 BindingMode::ByRef(mode, mutability)
2164 } else {
2165 BindingMode::ByValue(mutability)
2166 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002167 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002168 subpat: None,
2169 at_token: None,
2170 }.into();
2171 if let Some(boxed) = boxed {
2172 pat = PatBox {
2173 pat: Box::new(pat),
2174 box_token: boxed,
2175 }.into();
2176 }
2177 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002178 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002179 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002180 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002181 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002182 colon_token: None,
2183 }
2184 })
2185 )
2186 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002187 }
2188
Michael Layzell734adb42017-06-07 16:58:31 -04002189 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002190 impl Synom for Member {
2191 named!(parse -> Self, alt!(
2192 syn!(Ident) => { Member::Named }
2193 |
2194 syn!(Index) => { Member::Unnamed }
2195 ));
2196 }
2197
2198 #[cfg(feature = "full")]
2199 impl Synom for Index {
2200 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002201 lit: syn!(Lit) >>
2202 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002203 if let Ok(i) = lit.value.to_string().parse() {
2204 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002205 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002206 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002207 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002208 })
David Tolnay85b69a42017-12-27 20:43:10 -05002209 ));
2210 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002211
Michael Layzell734adb42017-06-07 16:58:31 -04002212 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002213 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002214 named!(parse -> Self, map!(
2215 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002216 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002217 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002218 }
David Tolnay9636c052016-10-02 17:11:17 -07002219
Michael Layzell734adb42017-06-07 16:58:31 -04002220 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002221 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002222 named!(parse -> Self, do_parse!(
2223 data: parens!(do_parse!(
2224 elems: call!(Delimited::parse_terminated) >>
2225 dotdot: map!(cond!(
2226 elems.is_empty() || elems.trailing_delim(),
2227 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002228 dots: punct!(..) >>
2229 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002230 (dots, trailing)
2231 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002232 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002233 rest: cond!(match dotdot {
2234 Some((_, Some(_))) => true,
2235 _ => false,
2236 },
2237 call!(Delimited::parse_terminated)) >>
2238 (elems, dotdot, rest)
2239 )) >>
2240 ({
2241 let ((mut elems, dotdot, rest), parens) = data;
2242 let (dotdot, trailing) = match dotdot {
2243 Some((a, b)) => (Some(a), Some(b)),
2244 None => (None, None),
2245 };
2246 PatTuple {
2247 paren_token: parens,
2248 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2249 dot2_token: dotdot,
2250 comma_token: trailing.and_then(|b| b),
2251 pats: {
2252 if let Some(rest) = rest {
2253 for elem in rest {
2254 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002255 }
Michael Layzell92639a52017-06-01 00:07:44 -04002256 }
2257 elems
2258 },
2259 }
2260 })
2261 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002262 }
David Tolnayfbb73232016-10-03 01:00:06 -07002263
Michael Layzell734adb42017-06-07 16:58:31 -04002264 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002265 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002266 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002267 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002268 mutability: syn!(Mutability) >>
2269 pat: syn!(Pat) >>
2270 (PatRef {
2271 pat: Box::new(pat),
2272 mutbl: mutability,
2273 and_token: and,
2274 })
2275 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002276 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002277
Michael Layzell734adb42017-06-07 16:58:31 -04002278 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002279 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002280 named!(parse -> Self, do_parse!(
2281 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002282 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002283 return parse_error(); // these need to be parsed by pat_path
2284 } else {
2285 PatLit {
2286 expr: Box::new(lit),
2287 }
2288 })
2289 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002290 }
David Tolnaye1310902016-10-29 23:40:00 -07002291
Michael Layzell734adb42017-06-07 16:58:31 -04002292 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002293 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002294 named!(parse -> Self, do_parse!(
2295 lo: pat_lit_expr >>
2296 limits: syn!(RangeLimits) >>
2297 hi: pat_lit_expr >>
2298 (PatRange {
2299 lo: Box::new(lo),
2300 hi: Box::new(hi),
2301 limits: limits,
2302 })
2303 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002304 }
David Tolnaye1310902016-10-29 23:40:00 -07002305
Michael Layzell734adb42017-06-07 16:58:31 -04002306 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002307 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002308 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002309 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002310 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002311 |
David Tolnay8c91b882017-12-28 23:04:32 -05002312 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002313 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002314 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002315 Expr::Unary(ExprUnary {
2316 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002317 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002318 expr: Box::new(v.into())
2319 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002320 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002321 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002322 })
2323 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002324
Michael Layzell734adb42017-06-07 16:58:31 -04002325 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002326 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002327 named!(parse -> Self, map!(
2328 brackets!(do_parse!(
2329 before: call!(Delimited::parse_terminated) >>
2330 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002331 dots: punct!(..) >>
2332 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002333 (dots, trailing)
2334 )) >>
2335 after: cond!(
2336 match middle {
2337 Some((_, ref trailing)) => trailing.is_some(),
2338 _ => false,
2339 },
2340 call!(Delimited::parse_terminated)
2341 ) >>
2342 (before, middle, after)
2343 )),
2344 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002345 let mut before: Delimited<Pat, Token![,]> = before;
2346 let after: Option<Delimited<Pat, Token![,]>> = after;
2347 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002348 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002349 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002350 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002351 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002352 }),
2353 bracket_token: brackets,
2354 middle: middle.and_then(|_| {
2355 if !before.is_empty() && !before.trailing_delim() {
2356 Some(Box::new(before.pop().unwrap().into_item()))
2357 } else {
2358 None
2359 }
2360 }),
2361 front: before,
2362 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002363 }
Alex Crichton954046c2017-05-30 21:49:42 -07002364 }
Michael Layzell92639a52017-06-01 00:07:44 -04002365 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002366 }
David Tolnay435a9a82016-10-29 13:47:20 -07002367
Michael Layzell734adb42017-06-07 16:58:31 -04002368 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002369 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002370 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002371 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002372 |
2373 epsilon!() => { |_| CaptureBy::Ref }
2374 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002375 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002376}
2377
David Tolnayf4bbbd92016-09-23 14:41:55 -07002378#[cfg(feature = "printing")]
2379mod printing {
2380 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002381 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002382 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002383 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002384 #[cfg(feature = "full")]
2385 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002386
David Tolnaybcf26022017-12-25 22:10:52 -05002387 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2388 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002389 #[cfg(feature = "full")]
2390 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002391 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002392 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002393 e.to_tokens(tokens);
2394 });
2395 } else {
2396 e.to_tokens(tokens);
2397 }
2398 }
2399
David Tolnay8c91b882017-12-28 23:04:32 -05002400 #[cfg(feature = "full")]
2401 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2402 tokens.append_all(attrs.outer());
2403 }
Michael Layzell734adb42017-06-07 16:58:31 -04002404
David Tolnay8c91b882017-12-28 23:04:32 -05002405 #[cfg(not(feature = "full"))]
2406 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002407 }
2408
Michael Layzell734adb42017-06-07 16:58:31 -04002409 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002410 impl ToTokens for ExprBox {
2411 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002412 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002413 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002414 self.expr.to_tokens(tokens);
2415 }
2416 }
2417
Michael Layzell734adb42017-06-07 16:58:31 -04002418 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002419 impl ToTokens for ExprInPlace {
2420 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002421 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002422 self.place.to_tokens(tokens);
2423 self.arrow_token.to_tokens(tokens);
2424 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002425 }
2426 }
2427
Michael Layzell734adb42017-06-07 16:58:31 -04002428 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002429 impl ToTokens for ExprArray {
2430 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002431 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002432 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002433 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002434 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002435 }
2436 }
2437
2438 impl ToTokens for ExprCall {
2439 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002440 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002441 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002442 self.paren_token.surround(tokens, |tokens| {
2443 self.args.to_tokens(tokens);
2444 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002445 }
2446 }
2447
Michael Layzell734adb42017-06-07 16:58:31 -04002448 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002449 impl ToTokens for ExprMethodCall {
2450 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002451 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002452 self.expr.to_tokens(tokens);
2453 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002454 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002455 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002456 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2457 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002458 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002459 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002460 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002461 self.paren_token.surround(tokens, |tokens| {
2462 self.args.to_tokens(tokens);
2463 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002464 }
2465 }
2466
Michael Layzell734adb42017-06-07 16:58:31 -04002467 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002468 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002469 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002470 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002471 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002472 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002473 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002474 // distinguish ExprTuple from ExprParen.
David Tolnay2a86fdd2017-12-28 23:34:28 -05002475 if self.elems.len() == 1 && !self.elems.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002476 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002477 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002478 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002479 }
2480 }
2481
2482 impl ToTokens for ExprBinary {
2483 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002484 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002485 self.left.to_tokens(tokens);
2486 self.op.to_tokens(tokens);
2487 self.right.to_tokens(tokens);
2488 }
2489 }
2490
2491 impl ToTokens for ExprUnary {
2492 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002493 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002494 self.op.to_tokens(tokens);
2495 self.expr.to_tokens(tokens);
2496 }
2497 }
2498
David Tolnay8c91b882017-12-28 23:04:32 -05002499 impl ToTokens for ExprLit {
2500 fn to_tokens(&self, tokens: &mut Tokens) {
2501 attrs_to_tokens(&self.attrs, tokens);
2502 self.lit.to_tokens(tokens);
2503 }
2504 }
2505
Alex Crichton62a0a592017-05-22 13:58:53 -07002506 impl ToTokens for ExprCast {
2507 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002508 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002509 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002510 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002511 self.ty.to_tokens(tokens);
2512 }
2513 }
2514
David Tolnay0cf94f22017-12-28 23:46:26 -05002515 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002516 impl ToTokens for ExprType {
2517 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002518 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002519 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002520 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002521 self.ty.to_tokens(tokens);
2522 }
2523 }
2524
Michael Layzell734adb42017-06-07 16:58:31 -04002525 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002526 fn maybe_wrap_else(
2527 tokens: &mut Tokens,
2528 else_token: &Option<Token![else]>,
2529 if_false: &Option<Box<Expr>>,
2530 ) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002531 if let Some(ref if_false) = *if_false {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002532 TokensOrDefault(else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002533
2534 // If we are not one of the valid expressions to exist in an else
2535 // clause, wrap ourselves in a block.
David Tolnay8c91b882017-12-28 23:04:32 -05002536 match **if_false {
2537 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002538 if_false.to_tokens(tokens);
2539 }
2540 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002541 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002542 if_false.to_tokens(tokens);
2543 });
2544 }
2545 }
2546 }
2547 }
2548
2549 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002550 impl ToTokens for ExprIf {
2551 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002552 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002553 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002554 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002555 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002556 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002557 }
2558 }
2559
Michael Layzell734adb42017-06-07 16:58:31 -04002560 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002561 impl ToTokens for ExprIfLet {
2562 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002563 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002564 self.if_token.to_tokens(tokens);
2565 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002566 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002567 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002568 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002569 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002570 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002571 }
2572 }
2573
Michael Layzell734adb42017-06-07 16:58:31 -04002574 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002575 impl ToTokens for ExprWhile {
2576 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002577 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002578 if self.label.is_some() {
2579 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002580 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002581 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002582 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002583 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002584 self.body.to_tokens(tokens);
2585 }
2586 }
2587
Michael Layzell734adb42017-06-07 16:58:31 -04002588 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002589 impl ToTokens for ExprWhileLet {
2590 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002591 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002592 if self.label.is_some() {
2593 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002594 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002595 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002596 self.while_token.to_tokens(tokens);
2597 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002598 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002599 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002600 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002601 self.body.to_tokens(tokens);
2602 }
2603 }
2604
Michael Layzell734adb42017-06-07 16:58:31 -04002605 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002606 impl ToTokens for ExprForLoop {
2607 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002608 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002609 if self.label.is_some() {
2610 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002611 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002612 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002613 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002614 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002615 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002616 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002617 self.body.to_tokens(tokens);
2618 }
2619 }
2620
Michael Layzell734adb42017-06-07 16:58:31 -04002621 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002622 impl ToTokens for ExprLoop {
2623 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002624 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002625 if self.label.is_some() {
2626 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002627 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002628 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002629 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002630 self.body.to_tokens(tokens);
2631 }
2632 }
2633
Michael Layzell734adb42017-06-07 16:58:31 -04002634 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002635 impl ToTokens for ExprMatch {
2636 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002637 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002638 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002639 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002640 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002641 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002642 arm.to_tokens(tokens);
2643 // Ensure that we have a comma after a non-block arm, except
2644 // for the last one.
2645 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002646 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002647 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002648 }
2649 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002650 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002651 }
2652 }
2653
Michael Layzell734adb42017-06-07 16:58:31 -04002654 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002655 impl ToTokens for ExprCatch {
2656 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002657 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002658 self.do_token.to_tokens(tokens);
2659 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002660 self.block.to_tokens(tokens);
2661 }
2662 }
2663
Michael Layzell734adb42017-06-07 16:58:31 -04002664 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002665 impl ToTokens for ExprYield {
2666 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002667 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002668 self.yield_token.to_tokens(tokens);
2669 self.expr.to_tokens(tokens);
2670 }
2671 }
2672
2673 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002674 impl ToTokens for ExprClosure {
2675 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002676 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002677 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002678 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002679 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002680 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002681 FnArg::Captured(ArgCaptured {
2682 ref pat,
2683 ty: Type::Infer(_),
2684 ..
2685 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002686 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002687 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002688 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002689 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002690 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002691 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002692 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002693 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002694 self.body.to_tokens(tokens);
2695 }
2696 }
2697
Michael Layzell734adb42017-06-07 16:58:31 -04002698 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002699 impl ToTokens for ExprUnsafe {
2700 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002701 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002702 self.unsafe_token.to_tokens(tokens);
2703 self.block.to_tokens(tokens);
2704 }
2705 }
2706
2707 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002708 impl ToTokens for ExprBlock {
2709 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002710 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002711 self.block.to_tokens(tokens);
2712 }
2713 }
2714
Michael Layzell734adb42017-06-07 16:58:31 -04002715 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002716 impl ToTokens for ExprAssign {
2717 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002718 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002719 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002720 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002721 self.right.to_tokens(tokens);
2722 }
2723 }
2724
Michael Layzell734adb42017-06-07 16:58:31 -04002725 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002726 impl ToTokens for ExprAssignOp {
2727 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002728 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002729 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002730 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002731 self.right.to_tokens(tokens);
2732 }
2733 }
2734
Michael Layzell734adb42017-06-07 16:58:31 -04002735 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002736 impl ToTokens for ExprField {
2737 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002738 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002739 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002740 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002741 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002742 }
2743 }
2744
Michael Layzell734adb42017-06-07 16:58:31 -04002745 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002746 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002747 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002748 match *self {
2749 Member::Named(ident) => ident.to_tokens(tokens),
2750 Member::Unnamed(ref index) => index.to_tokens(tokens),
2751 }
2752 }
2753 }
2754
2755 #[cfg(feature = "full")]
2756 impl ToTokens for Index {
2757 fn to_tokens(&self, tokens: &mut Tokens) {
2758 tokens.append(TokenTree {
2759 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002760 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002761 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002762 }
2763 }
2764
2765 impl ToTokens for ExprIndex {
2766 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002767 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002768 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002769 self.bracket_token.surround(tokens, |tokens| {
2770 self.index.to_tokens(tokens);
2771 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002772 }
2773 }
2774
Michael Layzell734adb42017-06-07 16:58:31 -04002775 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002776 impl ToTokens for ExprRange {
2777 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002778 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002779 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002780 match self.limits {
2781 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2782 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2783 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002784 self.to.to_tokens(tokens);
2785 }
2786 }
2787
2788 impl ToTokens for ExprPath {
2789 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002790 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002791 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002792 }
2793 }
2794
Michael Layzell734adb42017-06-07 16:58:31 -04002795 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002796 impl ToTokens for ExprAddrOf {
2797 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002798 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002799 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002800 self.mutbl.to_tokens(tokens);
2801 self.expr.to_tokens(tokens);
2802 }
2803 }
2804
Michael Layzell734adb42017-06-07 16:58:31 -04002805 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002806 impl ToTokens for ExprBreak {
2807 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002808 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002809 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002810 self.label.to_tokens(tokens);
2811 self.expr.to_tokens(tokens);
2812 }
2813 }
2814
Michael Layzell734adb42017-06-07 16:58:31 -04002815 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002816 impl ToTokens for ExprContinue {
2817 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002818 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002819 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002820 self.label.to_tokens(tokens);
2821 }
2822 }
2823
Michael Layzell734adb42017-06-07 16:58:31 -04002824 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002825 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002826 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002827 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002828 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002829 self.expr.to_tokens(tokens);
2830 }
2831 }
2832
Michael Layzell734adb42017-06-07 16:58:31 -04002833 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002834 impl ToTokens for ExprMacro {
2835 fn to_tokens(&self, tokens: &mut Tokens) {
2836 tokens.append_all(self.attrs.outer());
2837 self.mac.to_tokens(tokens);
2838 }
2839 }
2840
2841 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002842 impl ToTokens for ExprStruct {
2843 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002844 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002845 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002846 self.brace_token.surround(tokens, |tokens| {
2847 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002848 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002849 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002850 self.rest.to_tokens(tokens);
2851 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002852 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002853 }
2854 }
2855
Michael Layzell734adb42017-06-07 16:58:31 -04002856 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002857 impl ToTokens for ExprRepeat {
2858 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002859 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002860 self.bracket_token.surround(tokens, |tokens| {
2861 self.expr.to_tokens(tokens);
2862 self.semi_token.to_tokens(tokens);
2863 self.amt.to_tokens(tokens);
2864 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002865 }
2866 }
2867
David Tolnaye98775f2017-12-28 23:17:00 -05002868 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002869 impl ToTokens for ExprGroup {
2870 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002871 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002872 self.group_token.surround(tokens, |tokens| {
2873 self.expr.to_tokens(tokens);
2874 });
2875 }
2876 }
2877
David Tolnaye98775f2017-12-28 23:17:00 -05002878 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002879 impl ToTokens for ExprParen {
2880 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002881 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002882 self.paren_token.surround(tokens, |tokens| {
2883 self.expr.to_tokens(tokens);
2884 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002885 }
2886 }
2887
Michael Layzell734adb42017-06-07 16:58:31 -04002888 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002889 impl ToTokens for ExprTry {
2890 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002891 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002892 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002893 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002894 }
2895 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002896
Michael Layzell734adb42017-06-07 16:58:31 -04002897 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002898 impl ToTokens for FieldValue {
2899 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002900 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002901 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2902 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002903 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002904 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002905 self.expr.to_tokens(tokens);
2906 }
David Tolnay055a7042016-10-02 19:23:54 -07002907 }
2908 }
2909
Michael Layzell734adb42017-06-07 16:58:31 -04002910 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002911 impl ToTokens for Arm {
2912 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002913 tokens.append_all(&self.attrs);
2914 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002915 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002916 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002917 self.guard.to_tokens(tokens);
2918 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002919 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002920 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002921 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002922 }
2923 }
2924
Michael Layzell734adb42017-06-07 16:58:31 -04002925 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002926 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002927 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002928 self.underscore_token.to_tokens(tokens);
2929 }
2930 }
2931
Michael Layzell734adb42017-06-07 16:58:31 -04002932 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002933 impl ToTokens for PatIdent {
2934 fn to_tokens(&self, tokens: &mut Tokens) {
2935 self.mode.to_tokens(tokens);
2936 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002937 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002938 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002939 self.subpat.to_tokens(tokens);
2940 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002941 }
2942 }
2943
Michael Layzell734adb42017-06-07 16:58:31 -04002944 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002945 impl ToTokens for PatStruct {
2946 fn to_tokens(&self, tokens: &mut Tokens) {
2947 self.path.to_tokens(tokens);
2948 self.brace_token.surround(tokens, |tokens| {
2949 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002950 // NOTE: We need a comma before the dot2 token if it is present.
2951 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002952 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002953 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 self.dot2_token.to_tokens(tokens);
2955 });
2956 }
2957 }
2958
Michael Layzell734adb42017-06-07 16:58:31 -04002959 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002960 impl ToTokens for PatTupleStruct {
2961 fn to_tokens(&self, tokens: &mut Tokens) {
2962 self.path.to_tokens(tokens);
2963 self.pat.to_tokens(tokens);
2964 }
2965 }
2966
Michael Layzell734adb42017-06-07 16:58:31 -04002967 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002968 impl ToTokens for PatPath {
2969 fn to_tokens(&self, tokens: &mut Tokens) {
2970 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2971 }
2972 }
2973
Michael Layzell734adb42017-06-07 16:58:31 -04002974 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002975 impl ToTokens for PatTuple {
2976 fn to_tokens(&self, tokens: &mut Tokens) {
2977 self.paren_token.surround(tokens, |tokens| {
2978 for (i, token) in self.pats.iter().enumerate() {
2979 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07002980 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
2981 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002982 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002983 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002984 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002985
2986 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002987 // Ensure there is a comma before the .. token.
2988 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002989 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002990 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002991 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002992 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002993 });
2994 }
2995 }
2996
Michael Layzell734adb42017-06-07 16:58:31 -04002997 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002998 impl ToTokens for PatBox {
2999 fn to_tokens(&self, tokens: &mut Tokens) {
3000 self.box_token.to_tokens(tokens);
3001 self.pat.to_tokens(tokens);
3002 }
3003 }
3004
Michael Layzell734adb42017-06-07 16:58:31 -04003005 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003006 impl ToTokens for PatRef {
3007 fn to_tokens(&self, tokens: &mut Tokens) {
3008 self.and_token.to_tokens(tokens);
3009 self.mutbl.to_tokens(tokens);
3010 self.pat.to_tokens(tokens);
3011 }
3012 }
3013
Michael Layzell734adb42017-06-07 16:58:31 -04003014 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003015 impl ToTokens for PatLit {
3016 fn to_tokens(&self, tokens: &mut Tokens) {
3017 self.expr.to_tokens(tokens);
3018 }
3019 }
3020
Michael Layzell734adb42017-06-07 16:58:31 -04003021 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003022 impl ToTokens for PatRange {
3023 fn to_tokens(&self, tokens: &mut Tokens) {
3024 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003025 match self.limits {
3026 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3027 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3028 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003029 self.hi.to_tokens(tokens);
3030 }
3031 }
3032
Michael Layzell734adb42017-06-07 16:58:31 -04003033 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 impl ToTokens for PatSlice {
3035 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003036 // XXX: This is a mess, and it will be so easy to screw it up. How
3037 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003038 self.bracket_token.surround(tokens, |tokens| {
3039 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003040
3041 // If we need a comma before the middle or standalone .. token,
3042 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003043 if !self.front.empty_or_trailing()
3044 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003045 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003046 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003047 }
3048
3049 // If we have an identifier, we always need a .. token.
3050 if self.middle.is_some() {
3051 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003052 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003053 } else if self.dot2_token.is_some() {
3054 self.dot2_token.to_tokens(tokens);
3055 }
3056
3057 // Make sure we have a comma before the back half.
3058 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003059 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003060 self.back.to_tokens(tokens);
3061 } else {
3062 self.comma_token.to_tokens(tokens);
3063 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003064 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003065 }
3066 }
3067
Michael Layzell734adb42017-06-07 16:58:31 -04003068 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003069 impl ToTokens for FieldPat {
3070 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003071 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07003072 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05003073 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003074 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003075 }
3076 self.pat.to_tokens(tokens);
3077 }
3078 }
3079
Michael Layzell734adb42017-06-07 16:58:31 -04003080 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003081 impl ToTokens for BindingMode {
3082 fn to_tokens(&self, tokens: &mut Tokens) {
3083 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003084 BindingMode::ByRef(ref t, ref m) => {
3085 t.to_tokens(tokens);
3086 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003087 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003088 BindingMode::ByValue(ref m) => {
3089 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003090 }
3091 }
3092 }
3093 }
David Tolnay42602292016-10-01 22:25:45 -07003094
Michael Layzell734adb42017-06-07 16:58:31 -04003095 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07003096 impl ToTokens for CaptureBy {
3097 fn to_tokens(&self, tokens: &mut Tokens) {
3098 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003099 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003100 CaptureBy::Ref => {
3101 // nothing
3102 }
David Tolnay89e05672016-10-02 14:39:42 -07003103 }
3104 }
3105 }
3106
Michael Layzell734adb42017-06-07 16:58:31 -04003107 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003108 impl ToTokens for Block {
3109 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003110 self.brace_token.surround(tokens, |tokens| {
3111 tokens.append_all(&self.stmts);
3112 });
David Tolnay42602292016-10-01 22:25:45 -07003113 }
3114 }
3115
Michael Layzell734adb42017-06-07 16:58:31 -04003116 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003117 impl ToTokens for Stmt {
3118 fn to_tokens(&self, tokens: &mut Tokens) {
3119 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003120 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003121 Stmt::Item(ref item) => item.to_tokens(tokens),
3122 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003123 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003124 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003125 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003126 }
David Tolnay42602292016-10-01 22:25:45 -07003127 }
3128 }
3129 }
David Tolnay191e0582016-10-02 18:31:09 -07003130
Michael Layzell734adb42017-06-07 16:58:31 -04003131 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003132 impl ToTokens for Local {
3133 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003134 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003135 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003136 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003137 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003138 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003139 self.ty.to_tokens(tokens);
3140 }
3141 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003142 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003143 self.init.to_tokens(tokens);
3144 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003145 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003146 }
3147 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003148}