blob: 0110f10507437b0300247befc642ea0770ef583c [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
Michael Layzellb78f3b52017-06-04 19:03:03 -040018 /// E.g. 'place <- val' or `in place { val }`.
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>,
Michael Layzell6a5a1642017-06-04 19:35:15 -040022 pub kind: InPlaceKind,
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 Tolnay4a3f59a2017-12-28 21:21:12 -050030 pub exprs: 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 Tolnay4a3f59a2017-12-28 21:21:12 -050063 pub args: 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`.
96 pub Type(ExprType {
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 Layzell734adb42017-06-07 16:58:31 -0400707#[cfg(feature = "full")]
Michael Layzell6a5a1642017-06-04 19:35:15 -0400708ast_enum! {
709 #[cfg_attr(feature = "clone-impls", derive(Copy))]
710 pub enum InPlaceKind {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800711 Arrow(Token![<-]),
712 In(Token![in]),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400713 }
714}
715
Michael Layzell3936ceb2017-07-08 00:28:36 -0400716#[cfg(any(feature = "parsing", feature = "printing"))]
717#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700718fn arm_expr_requires_comma(expr: &Expr) -> bool {
719 // see https://github.com/rust-lang/rust/blob/eb8f2586e
720 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500721 match *expr {
722 Expr::Unsafe(..)
723 | Expr::Block(..)
724 | Expr::If(..)
725 | Expr::IfLet(..)
726 | Expr::Match(..)
727 | Expr::While(..)
728 | Expr::WhileLet(..)
729 | Expr::Loop(..)
730 | Expr::ForLoop(..)
731 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700732 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400733 }
734}
735
David Tolnayb9c8e322016-09-23 20:48:37 -0700736#[cfg(feature = "parsing")]
737pub mod parsing {
738 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700739 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700740
Michael Layzell734adb42017-06-07 16:58:31 -0400741 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500742 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500743 use synom::Synom;
744 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400745 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500746 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500747 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700748
David Tolnaybcf26022017-12-25 22:10:52 -0500749 // When we're parsing expressions which occur before blocks, like in an if
750 // statement's condition, we cannot parse a struct literal.
751 //
752 // Struct literals are ambiguous in certain positions
753 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700754 macro_rules! ambiguous_expr {
755 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700756 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700757 };
758 }
759
David Tolnaybcf26022017-12-25 22:10:52 -0500760 // When we are parsing an optional suffix expression, we cannot allow blocks
761 // if structs are not allowed.
762 //
763 // Example:
764 //
765 // if break {} {}
766 //
767 // is ambiguous between:
768 //
769 // if (break {}) {}
770 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400771 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400772 macro_rules! opt_ambiguous_expr {
773 ($i:expr, $allow_struct:ident) => {
774 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
775 };
776 }
777
Alex Crichton954046c2017-05-30 21:49:42 -0700778 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400779 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700780
781 fn description() -> Option<&'static str> {
782 Some("expression")
783 }
784 }
785
Michael Layzell734adb42017-06-07 16:58:31 -0400786 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700787 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
788
David Tolnaybcf26022017-12-25 22:10:52 -0500789 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400790 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500791 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500792 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400793 }
794
Michael Layzell734adb42017-06-07 16:58:31 -0400795 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500796 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500797 // NOTE: We intentionally skip assign_expr, placement_expr, and
798 // range_expr, as they are not parsed in non-full mode.
799 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400800 }
801
David Tolnaybcf26022017-12-25 22:10:52 -0500802 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400803 macro_rules! binop {
804 (
805 $name: ident,
806 $next: ident,
807 $submac: ident!( $($args:tt)* )
808 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500809 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400810 mut e: call!($next, allow_struct, allow_block) >>
811 many0!(do_parse!(
812 op: $submac!($($args)*) >>
813 rhs: call!($next, allow_struct, true) >>
814 ({
815 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500816 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400817 left: Box::new(e.into()),
818 op: op,
819 right: Box::new(rhs.into()),
820 }.into();
821 })
822 )) >>
823 (e)
824 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700825 }
David Tolnay54e854d2016-10-24 12:03:30 -0700826 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700827
David Tolnaybcf26022017-12-25 22:10:52 -0500828 // <placement> = <placement> ..
829 // <placement> += <placement> ..
830 // <placement> -= <placement> ..
831 // <placement> *= <placement> ..
832 // <placement> /= <placement> ..
833 // <placement> %= <placement> ..
834 // <placement> ^= <placement> ..
835 // <placement> &= <placement> ..
836 // <placement> |= <placement> ..
837 // <placement> <<= <placement> ..
838 // <placement> >>= <placement> ..
839 //
840 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400841 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500842 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400843 mut e: call!(placement_expr, allow_struct, allow_block) >>
844 alt!(
845 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800846 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400847 // Recurse into self to parse right-associative operator.
848 rhs: call!(assign_expr, allow_struct, true) >>
849 ({
850 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500851 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400852 left: Box::new(e.into()),
853 eq_token: eq,
854 right: Box::new(rhs.into()),
855 }.into();
856 })
857 )
858 |
859 do_parse!(
860 op: call!(BinOp::parse_assign_op) >>
861 // Recurse into self to parse right-associative operator.
862 rhs: call!(assign_expr, allow_struct, true) >>
863 ({
864 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500865 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400866 left: Box::new(e.into()),
867 op: op,
868 right: Box::new(rhs.into()),
869 }.into();
870 })
871 )
872 |
873 epsilon!()
874 ) >>
875 (e)
876 ));
877
David Tolnaybcf26022017-12-25 22:10:52 -0500878 // <range> <- <range> ..
879 //
880 // NOTE: The `in place { expr }` version of this syntax is parsed in
881 // `atom_expr`, not here.
882 //
883 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400884 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500885 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400886 mut e: call!(range_expr, allow_struct, allow_block) >>
887 alt!(
888 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800889 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400890 // Recurse into self to parse right-associative operator.
891 rhs: call!(placement_expr, allow_struct, true) >>
892 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400893 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500894 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400895 // op: BinOp::Place(larrow),
896 place: Box::new(e.into()),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400897 kind: InPlaceKind::Arrow(arrow),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400898 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400899 }.into();
900 })
901 )
902 |
903 epsilon!()
904 ) >>
905 (e)
906 ));
907
David Tolnaybcf26022017-12-25 22:10:52 -0500908 // <or> ... <or> ..
909 // <or> .. <or> ..
910 // <or> ..
911 //
912 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
913 // rules are for parsing these expressions are, but this is not correct.
914 // For example, `a .. b .. c` is not a legal expression. It should not
915 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
916 //
917 // NOTE: The form of ranges which don't include a preceding expression are
918 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400919 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500920 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400921 mut e: call!(or_expr, allow_struct, allow_block) >>
922 many0!(do_parse!(
923 limits: syn!(RangeLimits) >>
924 // We don't want to allow blocks here if we don't allow structs. See
925 // the reasoning for `opt_ambiguous_expr!` above.
926 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
927 ({
928 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500929 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400930 from: Some(Box::new(e.into())),
931 limits: limits,
932 to: hi.map(|e| Box::new(e.into())),
933 }.into();
934 })
935 )) >>
936 (e)
937 ));
938
David Tolnaybcf26022017-12-25 22:10:52 -0500939 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800940 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400941
David Tolnaybcf26022017-12-25 22:10:52 -0500942 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800943 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400944
David Tolnaybcf26022017-12-25 22:10:52 -0500945 // <bitor> == <bitor> ...
946 // <bitor> != <bitor> ...
947 // <bitor> >= <bitor> ...
948 // <bitor> <= <bitor> ...
949 // <bitor> > <bitor> ...
950 // <bitor> < <bitor> ...
951 //
952 // NOTE: This operator appears to be parsed as left-associative, but errors
953 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500954 binop!(
955 compare_expr,
956 bitor_expr,
957 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800958 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400959 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800960 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400961 |
962 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800963 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400964 |
965 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800966 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400967 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400968 do_parse!(
969 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800970 not!(punct!(<-)) >>
971 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400972 (BinOp::Lt(t))
973 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400974 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800975 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500976 )
977 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978
David Tolnaybcf26022017-12-25 22:10:52 -0500979 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500980 binop!(
981 bitor_expr,
982 bitxor_expr,
983 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
984 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400985
David Tolnaybcf26022017-12-25 22:10:52 -0500986 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500987 binop!(
988 bitxor_expr,
989 bitand_expr,
990 do_parse!(
991 // NOTE: Make sure we aren't looking at ^=.
992 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
993 )
994 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400995
David Tolnaybcf26022017-12-25 22:10:52 -0500996 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500997 binop!(
998 bitand_expr,
999 shift_expr,
1000 do_parse!(
1001 // NOTE: Make sure we aren't looking at && or &=.
1002 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1003 )
1004 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001005
David Tolnaybcf26022017-12-25 22:10:52 -05001006 // <arith> << <arith> ...
1007 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001008 binop!(
1009 shift_expr,
1010 arith_expr,
1011 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001012 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001013 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001014 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001015 )
1016 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001017
David Tolnaybcf26022017-12-25 22:10:52 -05001018 // <term> + <term> ...
1019 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001020 binop!(
1021 arith_expr,
1022 term_expr,
1023 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001024 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001025 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001026 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001027 )
1028 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001029
David Tolnaybcf26022017-12-25 22:10:52 -05001030 // <cast> * <cast> ...
1031 // <cast> / <cast> ...
1032 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001033 binop!(
1034 term_expr,
1035 cast_expr,
1036 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001037 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001038 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001039 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001040 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001041 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001042 )
1043 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001044
David Tolnaybcf26022017-12-25 22:10:52 -05001045 // <unary> as <ty>
1046 // <unary> : <ty>
David Tolnay8c91b882017-12-28 23:04:32 -05001047 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001048 mut e: call!(unary_expr, allow_struct, allow_block) >>
1049 many0!(alt!(
1050 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001051 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001052 // We can't accept `A + B` in cast expressions, as it's
1053 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001054 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001055 ({
1056 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001057 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001058 expr: Box::new(e.into()),
1059 as_token: as_,
1060 ty: Box::new(ty),
1061 }.into();
1062 })
1063 )
1064 |
1065 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001066 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001067 // We can't accept `A + B` in cast expressions, as it's
1068 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001069 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001070 ({
1071 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001072 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001073 expr: Box::new(e.into()),
1074 colon_token: colon,
1075 ty: Box::new(ty),
1076 }.into();
1077 })
1078 )
1079 )) >>
1080 (e)
1081 ));
1082
David Tolnaybcf26022017-12-25 22:10:52 -05001083 // <UnOp> <trailer>
1084 // & <trailer>
1085 // &mut <trailer>
1086 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001087 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001088 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001089 do_parse!(
1090 op: syn!(UnOp) >>
1091 expr: call!(unary_expr, allow_struct, true) >>
1092 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001093 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001094 op: op,
1095 expr: Box::new(expr.into()),
1096 }.into())
1097 )
1098 |
1099 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001100 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001101 mutability: syn!(Mutability) >>
1102 expr: call!(unary_expr, allow_struct, true) >>
1103 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001104 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001105 and_token: and,
1106 mutbl: mutability,
1107 expr: Box::new(expr.into()),
1108 }.into())
1109 )
1110 |
1111 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001112 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001113 expr: call!(unary_expr, allow_struct, true) >>
1114 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001115 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001116 box_token: box_,
1117 expr: Box::new(expr.into()),
1118 }.into())
1119 )
1120 |
1121 call!(trailer_expr, allow_struct, allow_block)
1122 ));
1123
Michael Layzell734adb42017-06-07 16:58:31 -04001124 // XXX: This duplication is ugly
1125 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001126 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001127 do_parse!(
1128 op: syn!(UnOp) >>
1129 expr: call!(unary_expr, allow_struct, true) >>
1130 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001131 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001132 op: op,
1133 expr: Box::new(expr.into()),
1134 }.into())
1135 )
1136 |
1137 call!(trailer_expr, allow_struct, allow_block)
1138 ));
1139
David Tolnaybcf26022017-12-25 22:10:52 -05001140 // <atom> (..<args>) ...
1141 // <atom> . <ident> (..<args>) ...
1142 // <atom> . <ident> ...
1143 // <atom> . <lit> ...
1144 // <atom> [ <expr> ] ...
1145 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001146 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001147 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001148 mut e: call!(atom_expr, allow_struct, allow_block) >>
1149 many0!(alt!(
1150 tap!(args: and_call => {
1151 let (args, paren) = args;
1152 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001153 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001154 func: Box::new(e.into()),
1155 args: args,
1156 paren_token: paren,
1157 }.into();
1158 })
1159 |
1160 tap!(more: and_method_call => {
1161 let mut call = more;
1162 call.expr = Box::new(e.into());
1163 e = call.into();
1164 })
1165 |
1166 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001167 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001169 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001170 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001171 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001172 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 }.into();
1174 })
1175 |
1176 tap!(i: and_index => {
1177 let (i, token) = i;
1178 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001179 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001180 expr: Box::new(e.into()),
1181 bracket_token: token,
1182 index: Box::new(i),
1183 }.into();
1184 })
1185 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001186 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001187 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001188 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001189 expr: Box::new(e.into()),
1190 question_token: question,
1191 }.into();
1192 })
1193 )) >>
1194 (e)
1195 ));
1196
Michael Layzell734adb42017-06-07 16:58:31 -04001197 // XXX: Duplication == ugly
1198 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001199 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001200 mut e: call!(atom_expr, allow_struct, allow_block) >>
1201 many0!(alt!(
1202 tap!(args: and_call => {
1203 let (args, paren) = args;
1204 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001205 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001206 func: Box::new(e.into()),
1207 args: args,
1208 paren_token: paren,
1209 }.into();
1210 })
1211 |
1212 tap!(i: and_index => {
1213 let (i, token) = i;
1214 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001215 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001216 expr: Box::new(e.into()),
1217 bracket_token: token,
1218 index: Box::new(i),
1219 }.into();
1220 })
1221 )) >>
1222 (e)
1223 ));
1224
David Tolnaybcf26022017-12-25 22:10:52 -05001225 // Parse all atomic expressions which don't have to worry about precidence
1226 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001227 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001228 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1229 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001230 |
David Tolnay8c91b882017-12-28 23:04:32 -05001231 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001232 |
1233 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001234 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001235 |
David Tolnay8c91b882017-12-28 23:04:32 -05001236 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001237 |
David Tolnay8c91b882017-12-28 23:04:32 -05001238 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001239 |
1240 call!(expr_break, allow_struct) // must be before expr_path
1241 |
David Tolnay8c91b882017-12-28 23:04:32 -05001242 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001243 |
1244 call!(expr_ret, allow_struct) // must be before expr_path
1245 |
1246 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
David Tolnay8c91b882017-12-28 23:04:32 -05001247 syn!(ExprInPlace) => { Expr::InPlace }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 |
David Tolnay8c91b882017-12-28 23:04:32 -05001249 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 |
David Tolnay8c91b882017-12-28 23:04:32 -05001251 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 |
David Tolnay8c91b882017-12-28 23:04:32 -05001253 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001254 |
David Tolnay8c91b882017-12-28 23:04:32 -05001255 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 |
David Tolnay8c91b882017-12-28 23:04:32 -05001257 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001258 |
David Tolnay8c91b882017-12-28 23:04:32 -05001259 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001260 |
David Tolnay8c91b882017-12-28 23:04:32 -05001261 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001262 |
David Tolnay8c91b882017-12-28 23:04:32 -05001263 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001264 |
David Tolnay8c91b882017-12-28 23:04:32 -05001265 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001266 |
David Tolnay8c91b882017-12-28 23:04:32 -05001267 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001268 |
David Tolnay8c91b882017-12-28 23:04:32 -05001269 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001270 |
David Tolnay8c91b882017-12-28 23:04:32 -05001271 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001272 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001273 call!(expr_closure, allow_struct)
1274 |
David Tolnay8c91b882017-12-28 23:04:32 -05001275 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001276 |
1277 // NOTE: This is the prefix-form of range
1278 call!(expr_range, allow_struct)
1279 |
David Tolnay8c91b882017-12-28 23:04:32 -05001280 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001281 |
David Tolnay8c91b882017-12-28 23:04:32 -05001282 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001283 ));
1284
Michael Layzell734adb42017-06-07 16:58:31 -04001285 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001286 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001287 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001288 |
David Tolnay8c91b882017-12-28 23:04:32 -05001289 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001290 ));
1291
Michael Layzell734adb42017-06-07 16:58:31 -04001292 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001293 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001294 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001295 |
David Tolnay8c91b882017-12-28 23:04:32 -05001296 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001297 |
David Tolnay8c91b882017-12-28 23:04:32 -05001298 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001299 |
David Tolnay8c91b882017-12-28 23:04:32 -05001300 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001301 |
David Tolnay8c91b882017-12-28 23:04:32 -05001302 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001303 |
David Tolnay8c91b882017-12-28 23:04:32 -05001304 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001305 |
David Tolnay8c91b882017-12-28 23:04:32 -05001306 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001307 |
David Tolnay8c91b882017-12-28 23:04:32 -05001308 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001309 |
David Tolnay8c91b882017-12-28 23:04:32 -05001310 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001311 |
David Tolnay8c91b882017-12-28 23:04:32 -05001312 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001313 |
David Tolnay8c91b882017-12-28 23:04:32 -05001314 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001315 ), Expr::from));
1316
David Tolnay8c91b882017-12-28 23:04:32 -05001317 impl Synom for ExprLit {
1318 named!(parse -> Self, do_parse!(
1319 lit: syn!(Lit) >>
1320 (ExprLit {
1321 attrs: Vec::new(),
1322 lit: lit,
1323 })
1324 ));
1325 }
1326
1327 #[cfg(feature = "full")]
1328 impl Synom for ExprMacro {
1329 named!(parse -> Self, do_parse!(
1330 mac: syn!(Macro) >>
1331 (ExprMacro {
1332 attrs: Vec::new(),
1333 mac: mac,
1334 })
1335 ));
1336 }
1337
David Tolnaye98775f2017-12-28 23:17:00 -05001338 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001339 impl Synom for ExprGroup {
1340 named!(parse -> Self, do_parse!(
1341 e: grouped!(syn!(Expr)) >>
1342 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001343 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001344 expr: Box::new(e.0),
1345 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001346 })
Michael Layzell93c36282017-06-04 20:43:14 -04001347 ));
1348 }
1349
David Tolnaye98775f2017-12-28 23:17:00 -05001350 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001351 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001352 named!(parse -> Self, do_parse!(
1353 e: parens!(syn!(Expr)) >>
1354 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001355 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001356 expr: Box::new(e.0),
1357 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001358 })
Michael Layzell92639a52017-06-01 00:07:44 -04001359 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001360 }
David Tolnay89e05672016-10-02 14:39:42 -07001361
Michael Layzell734adb42017-06-07 16:58:31 -04001362 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001363 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001364 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001365 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001366 place: expr_no_struct >>
1367 value: braces!(call!(Block::parse_within)) >>
1368 (ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001369 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001370 place: Box::new(place),
Michael Layzell6a5a1642017-06-04 19:35:15 -04001371 kind: InPlaceKind::In(in_),
David Tolnay8c91b882017-12-28 23:04:32 -05001372 value: Box::new(ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001373 attrs: Vec::new(),
David Tolnay8c91b882017-12-28 23:04:32 -05001374 block: Block {
1375 stmts: value.0,
1376 brace_token: value.1,
1377 },
1378 }.into()),
Michael Layzell92639a52017-06-01 00:07:44 -04001379 })
1380 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001381 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001382
Michael Layzell734adb42017-06-07 16:58:31 -04001383 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001384 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001385 named!(parse -> Self, do_parse!(
1386 elems: brackets!(call!(Delimited::parse_terminated)) >>
1387 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001388 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001389 exprs: elems.0,
1390 bracket_token: elems.1,
1391 })
1392 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001393 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001394
David Tolnay32954ef2017-12-26 22:43:16 -05001395 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001396 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001397
Michael Layzell734adb42017-06-07 16:58:31 -04001398 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001399 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001400 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001401 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001402 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001403 colon2: punct!(::) >>
1404 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001405 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001406 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001407 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001408 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001409 args: parens!(call!(Delimited::parse_terminated)) >>
1410 ({
1411 let (colon2, lt, tys, gt) = match typarams {
1412 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1413 None => (None, None, None, None),
1414 };
1415 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001416 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001417 // this expr will get overwritten after being returned
David Tolnay8c91b882017-12-28 23:04:32 -05001418 expr: Box::new(Expr::Lit(ExprLit {
1419 attrs: Vec::new(),
1420 lit: Lit {
1421 span: Span::default(),
1422 value: LitKind::Bool(false),
1423 },
Alex Crichton954046c2017-05-30 21:49:42 -07001424 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001425
Alex Crichton954046c2017-05-30 21:49:42 -07001426 method: method,
1427 args: args.0,
1428 paren_token: args.1,
1429 dot_token: dot,
1430 lt_token: lt,
1431 gt_token: gt,
1432 colon2_token: colon2,
1433 typarams: tys.unwrap_or_default(),
1434 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001435 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001436 ));
1437
Michael Layzell734adb42017-06-07 16:58:31 -04001438 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001439 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001440 named!(parse -> Self, do_parse!(
1441 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001442 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001443 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001444 args: elems.0,
1445 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001446 })
1447 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001448 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001449
Michael Layzell734adb42017-06-07 16:58:31 -04001450 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001451 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001452 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001453 if_: keyword!(if) >>
1454 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001455 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001456 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001457 cond: expr_no_struct >>
1458 then_block: braces!(call!(Block::parse_within)) >>
1459 else_block: option!(else_block) >>
1460 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001461 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001462 pat: Box::new(pat),
1463 let_token: let_,
1464 eq_token: eq,
1465 expr: Box::new(cond),
1466 if_true: Block {
1467 stmts: then_block.0,
1468 brace_token: then_block.1,
1469 },
1470 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001471 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001472 if_false: else_block.map(|p| Box::new(p.1.into())),
1473 })
1474 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001475 }
1476
Michael Layzell734adb42017-06-07 16:58:31 -04001477 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001478 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001479 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001480 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001481 cond: expr_no_struct >>
1482 then_block: braces!(call!(Block::parse_within)) >>
1483 else_block: option!(else_block) >>
1484 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001485 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001486 cond: Box::new(cond),
1487 if_true: Block {
1488 stmts: then_block.0,
1489 brace_token: then_block.1,
1490 },
1491 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001492 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001493 if_false: else_block.map(|p| Box::new(p.1.into())),
1494 })
1495 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001496 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001497
Michael Layzell734adb42017-06-07 16:58:31 -04001498 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001499 named!(else_block -> (Token![else], Expr), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001500 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001501 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001502 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001503 |
David Tolnay8c91b882017-12-28 23:04:32 -05001504 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001505 |
1506 do_parse!(
1507 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001508 (Expr::Block(ExprBlock {
1509 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001510 block: Block {
1511 stmts: else_block.0,
1512 brace_token: else_block.1,
1513 },
1514 }))
David Tolnay939766a2016-09-23 23:48:12 -07001515 )
Alex Crichton954046c2017-05-30 21:49:42 -07001516 ) >>
1517 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001518 ));
1519
Michael Layzell734adb42017-06-07 16:58:31 -04001520 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001521 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001522 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001523 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1524 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001525 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001526 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001527 expr: expr_no_struct >>
1528 loop_block: syn!(Block) >>
1529 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001530 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001531 for_token: for_,
1532 in_token: in_,
1533 pat: Box::new(pat),
1534 expr: Box::new(expr),
1535 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001536 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001537 label: lbl.map(|p| p.0),
1538 })
1539 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001540 }
Gregory Katze5f35682016-09-27 14:20:55 -04001541
Michael Layzell734adb42017-06-07 16:58:31 -04001542 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001543 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001544 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001545 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1546 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001547 loop_block: syn!(Block) >>
1548 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001549 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001550 loop_token: loop_,
1551 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001552 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001553 label: lbl.map(|p| p.0),
1554 })
1555 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001556 }
1557
Michael Layzell734adb42017-06-07 16:58:31 -04001558 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001559 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001560 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001561 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001562 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001563 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001564 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001565 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001566 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001567 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001568 expr: Box::new(obj),
1569 match_token: match_,
1570 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001571 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001572 }
1573 })
1574 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001575 }
David Tolnay1978c672016-10-27 22:05:52 -07001576
Michael Layzell734adb42017-06-07 16:58:31 -04001577 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001578 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001579 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001580 do_: keyword!(do) >>
1581 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001582 catch_block: syn!(Block) >>
1583 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001584 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001585 block: catch_block,
1586 do_token: do_,
1587 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001588 })
Michael Layzell92639a52017-06-01 00:07:44 -04001589 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001590 }
Arnavion02ef13f2017-04-25 00:54:31 -07001591
Michael Layzell734adb42017-06-07 16:58:31 -04001592 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001593 impl Synom for ExprYield {
1594 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001595 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001596 expr: option!(syn!(Expr)) >>
1597 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001598 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001599 yield_token: yield_,
1600 expr: expr.map(Box::new),
1601 })
1602 ));
1603 }
1604
1605 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001606 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001607 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001608 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001609 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001610 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1611 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001612 body: do_parse!(
1613 expr: alt!(expr_nosemi | syn!(Expr)) >>
1614 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1615 map!(input_end!(), |_| None)
1616 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001617 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001618 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001619 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001620 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001621 ) >>
1622 (Arm {
1623 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001624 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001625 attrs: attrs,
1626 pats: pats,
1627 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001628 body: Box::new(body.0),
1629 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001630 })
1631 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001632 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001633
Michael Layzell734adb42017-06-07 16:58:31 -04001634 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001635 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001636 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001637 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001638 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001639 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001640 ret_and_body: alt!(
1641 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001642 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001643 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001644 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001645 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001646 Expr::Block(ExprBlock {
1647 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001648 block: body,
1649 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001650 )
1651 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001652 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001653 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001654 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001655 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001656 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001657 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001658 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001659 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001660 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001661 body: Box::new(ret_and_body.1),
1662 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001663 ));
1664
Michael Layzell734adb42017-06-07 16:58:31 -04001665 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001666 named!(fn_arg -> FnArg, do_parse!(
1667 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001668 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001669 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001670 if let Some((colon, ty)) = ty {
1671 FnArg::Captured(ArgCaptured {
1672 pat: pat,
1673 colon_token: colon,
1674 ty: ty,
1675 })
1676 } else {
1677 FnArg::Inferred(pat)
1678 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001679 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001680 ));
1681
Michael Layzell734adb42017-06-07 16:58:31 -04001682 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001683 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001684 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001685 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1686 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001687 cond: expr_no_struct >>
1688 while_block: syn!(Block) >>
1689 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001690 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001691 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001692 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001693 cond: Box::new(cond),
1694 body: while_block,
1695 label: lbl.map(|p| p.0),
1696 })
1697 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001698 }
1699
Michael Layzell734adb42017-06-07 16:58:31 -04001700 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001701 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001702 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001703 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1704 while_: keyword!(while) >>
1705 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001706 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001707 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001708 value: expr_no_struct >>
1709 while_block: syn!(Block) >>
1710 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001711 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001712 eq_token: eq,
1713 let_token: let_,
1714 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001715 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001716 pat: Box::new(pat),
1717 expr: Box::new(value),
1718 body: while_block,
1719 label: lbl.map(|p| p.0),
1720 })
1721 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001722 }
1723
Michael Layzell734adb42017-06-07 16:58:31 -04001724 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001725 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001726 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001727 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001728 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001729 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001730 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001731 continue_token: cont,
1732 label: lbl,
1733 })
1734 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001735 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001736
Michael Layzell734adb42017-06-07 16:58:31 -04001737 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001738 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001739 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001740 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001741 // We can't allow blocks after a `break` expression when we wouldn't
1742 // allow structs, as this expression is ambiguous.
1743 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001744 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001745 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001746 label: lbl,
1747 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001748 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001749 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001750 ));
1751
Michael Layzell734adb42017-06-07 16:58:31 -04001752 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001753 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001754 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001755 // NOTE: return is greedy and eats blocks after it even when in a
1756 // position where structs are not allowed, such as in if statement
1757 // conditions. For example:
1758 //
David Tolnaybcf26022017-12-25 22:10:52 -05001759 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001760 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001761 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001762 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001763 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001764 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001765 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001766 ));
1767
Michael Layzell734adb42017-06-07 16:58:31 -04001768 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001769 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001770 named!(parse -> Self, do_parse!(
1771 path: syn!(Path) >>
1772 data: braces!(do_parse!(
1773 fields: call!(Delimited::parse_terminated) >>
1774 base: option!(
1775 cond!(fields.is_empty() || fields.trailing_delim(),
1776 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001777 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001778 base: syn!(Expr) >>
1779 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001780 )
Michael Layzell92639a52017-06-01 00:07:44 -04001781 )
1782 ) >>
1783 (fields, base)
1784 )) >>
1785 ({
1786 let ((fields, base), brace) = data;
1787 let (dots, rest) = match base.and_then(|b| b) {
1788 Some((dots, base)) => (Some(dots), Some(base)),
1789 None => (None, None),
1790 };
1791 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001792 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001793 brace_token: brace,
1794 path: path,
1795 fields: fields,
1796 dot2_token: dots,
1797 rest: rest.map(Box::new),
1798 }
1799 })
1800 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001801 }
1802
Michael Layzell734adb42017-06-07 16:58:31 -04001803 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001804 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001805 named!(parse -> Self, alt!(
1806 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001807 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001808 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001809 value: syn!(Expr) >>
1810 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001811 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001812 expr: value,
1813 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001814 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001815 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001816 })
Michael Layzell92639a52017-06-01 00:07:44 -04001817 )
1818 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001819 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001820 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001821 expr: Expr::Path(ExprPath {
1822 attrs: Vec::new(),
1823 qself: None,
1824 path: name.into(),
1825 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001826 is_shorthand: true,
1827 attrs: Vec::new(),
1828 colon_token: None,
1829 })
1830 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001831 }
David Tolnay055a7042016-10-02 19:23:54 -07001832
Michael Layzell734adb42017-06-07 16:58:31 -04001833 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001834 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001835 named!(parse -> Self, do_parse!(
1836 data: brackets!(do_parse!(
1837 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001838 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001839 times: syn!(Expr) >>
1840 (value, semi, times)
1841 )) >>
1842 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001843 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001844 expr: Box::new((data.0).0),
1845 amt: Box::new((data.0).2),
1846 bracket_token: data.1,
1847 semi_token: (data.0).1,
1848 })
1849 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001850 }
David Tolnay055a7042016-10-02 19:23:54 -07001851
Michael Layzell734adb42017-06-07 16:58:31 -04001852 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001853 impl Synom for ExprUnsafe {
1854 named!(parse -> Self, do_parse!(
1855 unsafe_: keyword!(unsafe) >>
1856 b: syn!(Block) >>
1857 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001858 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001859 unsafe_token: unsafe_,
1860 block: b,
1861 })
1862 ));
1863 }
1864
1865 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001866 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001867 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001868 b: syn!(Block) >>
1869 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001870 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001871 block: b,
1872 })
1873 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001874 }
David Tolnay89e05672016-10-02 14:39:42 -07001875
Michael Layzell734adb42017-06-07 16:58:31 -04001876 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001877 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001878 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001879 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001880 (ExprRange {
1881 attrs: Vec::new(),
1882 from: None,
1883 to: hi.map(Box::new),
1884 limits: limits,
1885 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001886 ));
1887
Michael Layzell734adb42017-06-07 16:58:31 -04001888 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001889 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001890 named!(parse -> Self, alt!(
1891 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001892 punct!(..=) => { RangeLimits::Closed }
1893 |
1894 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001895 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001896 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001897 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001898 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001899 }
David Tolnay438c9052016-10-07 23:24:48 -07001900
Alex Crichton954046c2017-05-30 21:49:42 -07001901 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001902 named!(parse -> Self, do_parse!(
1903 pair: qpath >>
1904 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001905 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001906 qself: pair.0,
1907 path: pair.1,
1908 })
1909 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001910 }
David Tolnay42602292016-10-01 22:25:45 -07001911
Michael Layzell734adb42017-06-07 16:58:31 -04001912 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001913 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001914
David Tolnay32954ef2017-12-26 22:43:16 -05001915 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001916
Michael Layzell734adb42017-06-07 16:58:31 -04001917 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001918 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001919 named!(parse -> Self, do_parse!(
1920 stmts: braces!(call!(Block::parse_within)) >>
1921 (Block {
1922 stmts: stmts.0,
1923 brace_token: stmts.1,
1924 })
1925 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001926 }
David Tolnay939766a2016-09-23 23:48:12 -07001927
Michael Layzell734adb42017-06-07 16:58:31 -04001928 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001929 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001930 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001931 many0!(punct!(;)) >>
1932 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001933 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001934 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001935 mut e: syn!(Expr) >>
1936 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001937 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001938 Stmt::Expr(Box::new(e))
1939 })
1940 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001941 (match last {
1942 None => standalone,
1943 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001944 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001945 standalone
1946 }
1947 })
1948 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001949 }
1950
Michael Layzell734adb42017-06-07 16:58:31 -04001951 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001952 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001953 named!(parse -> Self, alt!(
1954 stmt_mac
1955 |
1956 stmt_local
1957 |
1958 stmt_item
1959 |
Michael Layzell35418782017-06-07 09:20:25 -04001960 stmt_blockexpr
1961 |
Michael Layzell92639a52017-06-01 00:07:44 -04001962 stmt_expr
1963 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001964 }
David Tolnay939766a2016-09-23 23:48:12 -07001965
Michael Layzell734adb42017-06-07 16:58:31 -04001966 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001967 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001968 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001969 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001970 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001971 // Only parse braces here; paren and bracket will get parsed as
1972 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001973 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001974 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001975 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1976 attrs: attrs,
1977 ident: None,
1978 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001979 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001980 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001981 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001982 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001983 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001984 },
David Tolnayeea28d62016-10-25 20:44:08 -07001985 },
David Tolnay57b52bc2017-12-28 18:06:38 -05001986 semi_token: semi,
1987 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07001988 ));
1989
Michael Layzell734adb42017-06-07 16:58:31 -04001990 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001991 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001992 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001993 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001994 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001995 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001996 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1997 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001998 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001999 let_token: let_,
2000 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002001 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
2002 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07002003 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002004 ty: ty.map(|p| Box::new(p.1)),
2005 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002006 attrs: attrs,
2007 })))
2008 ));
2009
Michael Layzell734adb42017-06-07 16:58:31 -04002010 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002011 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002012
Michael Layzell734adb42017-06-07 16:58:31 -04002013 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002014 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002015 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002016 mut e: expr_nosemi >>
2017 // If the next token is a `.` or a `?` it is special-cased to parse as
2018 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002019 not!(punct!(.)) >>
2020 not!(punct!(?)) >>
2021 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002022 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002023 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002024 if let Some(semi) = semi {
2025 Stmt::Semi(Box::new(e), semi)
2026 } else {
2027 Stmt::Expr(Box::new(e))
2028 }
2029 })
2030 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002031
Michael Layzell734adb42017-06-07 16:58:31 -04002032 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002033 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002034 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002035 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002036 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002037 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002038 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002039 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002040 })
David Tolnay939766a2016-09-23 23:48:12 -07002041 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002042
Michael Layzell734adb42017-06-07 16:58:31 -04002043 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002044 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002045 named!(parse -> Self, alt!(
2046 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2047 |
2048 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2049 |
2050 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2051 |
2052 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2053 |
2054 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2055 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002056 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002057 |
2058 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2059 |
2060 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2061 |
2062 syn!(PatPath) => { Pat::Path }
2063 |
2064 syn!(PatTuple) => { Pat::Tuple }
2065 |
2066 syn!(PatRef) => { Pat::Ref }
2067 |
2068 syn!(PatSlice) => { Pat::Slice }
2069 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002070 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002071
Michael Layzell734adb42017-06-07 16:58:31 -04002072 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002073 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002074 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002075 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002076 |u| PatWild { underscore_token: u }
2077 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002078 }
David Tolnay84aa0752016-10-02 23:01:13 -07002079
Michael Layzell734adb42017-06-07 16:58:31 -04002080 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002081 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002082 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002083 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002084 pat: syn!(Pat) >>
2085 (PatBox {
2086 pat: Box::new(pat),
2087 box_token: boxed,
2088 })
2089 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002090 }
2091
Michael Layzell734adb42017-06-07 16:58:31 -04002092 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002093 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002094 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002095 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002096 mutability: syn!(Mutability) >>
2097 name: alt!(
2098 syn!(Ident)
2099 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002100 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002101 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002102 not!(punct!(<)) >>
2103 not!(punct!(::)) >>
2104 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002105 (PatIdent {
2106 mode: match mode {
2107 Some(mode) => BindingMode::ByRef(mode, mutability),
2108 None => BindingMode::ByValue(mutability),
2109 },
2110 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002111 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002112 subpat: subpat.map(|p| Box::new(p.1)),
2113 })
2114 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002115 }
2116
Michael Layzell734adb42017-06-07 16:58:31 -04002117 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002118 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002119 named!(parse -> Self, do_parse!(
2120 path: syn!(Path) >>
2121 tuple: syn!(PatTuple) >>
2122 (PatTupleStruct {
2123 path: path,
2124 pat: tuple,
2125 })
2126 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002127 }
2128
Michael Layzell734adb42017-06-07 16:58:31 -04002129 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002130 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002131 named!(parse -> Self, do_parse!(
2132 path: syn!(Path) >>
2133 data: braces!(do_parse!(
2134 fields: call!(Delimited::parse_terminated) >>
2135 base: option!(
2136 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002137 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002138 ) >>
2139 (fields, base)
2140 )) >>
2141 (PatStruct {
2142 path: path,
2143 fields: (data.0).0,
2144 brace_token: data.1,
2145 dot2_token: (data.0).1.and_then(|m| m),
2146 })
2147 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002148 }
2149
Michael Layzell734adb42017-06-07 16:58:31 -04002150 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002151 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002152 named!(parse -> Self, alt!(
2153 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002154 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002155 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002156 pat: syn!(Pat) >>
2157 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002158 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002159 pat: Box::new(pat),
2160 is_shorthand: false,
2161 attrs: Vec::new(),
2162 colon_token: Some(colon),
2163 })
2164 )
2165 |
2166 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002167 boxed: option!(keyword!(box)) >>
2168 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002169 mutability: syn!(Mutability) >>
2170 ident: syn!(Ident) >>
2171 ({
2172 let mut pat: Pat = PatIdent {
2173 mode: if let Some(mode) = mode {
2174 BindingMode::ByRef(mode, mutability)
2175 } else {
2176 BindingMode::ByValue(mutability)
2177 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002178 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002179 subpat: None,
2180 at_token: None,
2181 }.into();
2182 if let Some(boxed) = boxed {
2183 pat = PatBox {
2184 pat: Box::new(pat),
2185 box_token: boxed,
2186 }.into();
2187 }
2188 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002189 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002190 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002191 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002192 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002193 colon_token: None,
2194 }
2195 })
2196 )
2197 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002198 }
2199
Michael Layzell734adb42017-06-07 16:58:31 -04002200 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002201 impl Synom for Member {
2202 named!(parse -> Self, alt!(
2203 syn!(Ident) => { Member::Named }
2204 |
2205 syn!(Index) => { Member::Unnamed }
2206 ));
2207 }
2208
2209 #[cfg(feature = "full")]
2210 impl Synom for Index {
2211 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002212 lit: syn!(Lit) >>
2213 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002214 if let Ok(i) = lit.value.to_string().parse() {
2215 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002216 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002217 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002218 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002219 })
David Tolnay85b69a42017-12-27 20:43:10 -05002220 ));
2221 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002222
Michael Layzell734adb42017-06-07 16:58:31 -04002223 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002224 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002225 named!(parse -> Self, map!(
2226 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002227 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002228 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002229 }
David Tolnay9636c052016-10-02 17:11:17 -07002230
Michael Layzell734adb42017-06-07 16:58:31 -04002231 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002232 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002233 named!(parse -> Self, do_parse!(
2234 data: parens!(do_parse!(
2235 elems: call!(Delimited::parse_terminated) >>
2236 dotdot: map!(cond!(
2237 elems.is_empty() || elems.trailing_delim(),
2238 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002239 dots: punct!(..) >>
2240 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002241 (dots, trailing)
2242 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002243 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002244 rest: cond!(match dotdot {
2245 Some((_, Some(_))) => true,
2246 _ => false,
2247 },
2248 call!(Delimited::parse_terminated)) >>
2249 (elems, dotdot, rest)
2250 )) >>
2251 ({
2252 let ((mut elems, dotdot, rest), parens) = data;
2253 let (dotdot, trailing) = match dotdot {
2254 Some((a, b)) => (Some(a), Some(b)),
2255 None => (None, None),
2256 };
2257 PatTuple {
2258 paren_token: parens,
2259 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2260 dot2_token: dotdot,
2261 comma_token: trailing.and_then(|b| b),
2262 pats: {
2263 if let Some(rest) = rest {
2264 for elem in rest {
2265 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002266 }
Michael Layzell92639a52017-06-01 00:07:44 -04002267 }
2268 elems
2269 },
2270 }
2271 })
2272 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002273 }
David Tolnayfbb73232016-10-03 01:00:06 -07002274
Michael Layzell734adb42017-06-07 16:58:31 -04002275 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002276 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002277 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002278 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002279 mutability: syn!(Mutability) >>
2280 pat: syn!(Pat) >>
2281 (PatRef {
2282 pat: Box::new(pat),
2283 mutbl: mutability,
2284 and_token: and,
2285 })
2286 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002287 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002288
Michael Layzell734adb42017-06-07 16:58:31 -04002289 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002290 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002291 named!(parse -> Self, do_parse!(
2292 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002293 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002294 return parse_error(); // these need to be parsed by pat_path
2295 } else {
2296 PatLit {
2297 expr: Box::new(lit),
2298 }
2299 })
2300 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002301 }
David Tolnaye1310902016-10-29 23:40:00 -07002302
Michael Layzell734adb42017-06-07 16:58:31 -04002303 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002304 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002305 named!(parse -> Self, do_parse!(
2306 lo: pat_lit_expr >>
2307 limits: syn!(RangeLimits) >>
2308 hi: pat_lit_expr >>
2309 (PatRange {
2310 lo: Box::new(lo),
2311 hi: Box::new(hi),
2312 limits: limits,
2313 })
2314 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002315 }
David Tolnaye1310902016-10-29 23:40:00 -07002316
Michael Layzell734adb42017-06-07 16:58:31 -04002317 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002318 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002319 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002320 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002321 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002322 |
David Tolnay8c91b882017-12-28 23:04:32 -05002323 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002324 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002325 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002326 Expr::Unary(ExprUnary {
2327 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002328 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002329 expr: Box::new(v.into())
2330 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002331 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002332 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002333 })
2334 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002335
Michael Layzell734adb42017-06-07 16:58:31 -04002336 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002337 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002338 named!(parse -> Self, map!(
2339 brackets!(do_parse!(
2340 before: call!(Delimited::parse_terminated) >>
2341 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002342 dots: punct!(..) >>
2343 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002344 (dots, trailing)
2345 )) >>
2346 after: cond!(
2347 match middle {
2348 Some((_, ref trailing)) => trailing.is_some(),
2349 _ => false,
2350 },
2351 call!(Delimited::parse_terminated)
2352 ) >>
2353 (before, middle, after)
2354 )),
2355 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002356 let mut before: Delimited<Pat, Token![,]> = before;
2357 let after: Option<Delimited<Pat, Token![,]>> = after;
2358 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002359 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002360 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002361 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002362 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002363 }),
2364 bracket_token: brackets,
2365 middle: middle.and_then(|_| {
2366 if !before.is_empty() && !before.trailing_delim() {
2367 Some(Box::new(before.pop().unwrap().into_item()))
2368 } else {
2369 None
2370 }
2371 }),
2372 front: before,
2373 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002374 }
Alex Crichton954046c2017-05-30 21:49:42 -07002375 }
Michael Layzell92639a52017-06-01 00:07:44 -04002376 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002377 }
David Tolnay435a9a82016-10-29 13:47:20 -07002378
Michael Layzell734adb42017-06-07 16:58:31 -04002379 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002380 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002381 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002382 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002383 |
2384 epsilon!() => { |_| CaptureBy::Ref }
2385 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002386 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002387}
2388
David Tolnayf4bbbd92016-09-23 14:41:55 -07002389#[cfg(feature = "printing")]
2390mod printing {
2391 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002392 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002393 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002394 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002395 #[cfg(feature = "full")]
2396 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002397
David Tolnaybcf26022017-12-25 22:10:52 -05002398 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2399 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002400 #[cfg(feature = "full")]
2401 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002402 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002403 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002404 e.to_tokens(tokens);
2405 });
2406 } else {
2407 e.to_tokens(tokens);
2408 }
2409 }
2410
David Tolnay8c91b882017-12-28 23:04:32 -05002411 #[cfg(feature = "full")]
2412 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2413 tokens.append_all(attrs.outer());
2414 }
Michael Layzell734adb42017-06-07 16:58:31 -04002415
David Tolnay8c91b882017-12-28 23:04:32 -05002416 #[cfg(not(feature = "full"))]
2417 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002418 }
2419
Michael Layzell734adb42017-06-07 16:58:31 -04002420 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002421 impl ToTokens for ExprBox {
2422 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002423 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002424 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002425 self.expr.to_tokens(tokens);
2426 }
2427 }
2428
Michael Layzell734adb42017-06-07 16:58:31 -04002429 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002430 impl ToTokens for ExprInPlace {
2431 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002432 tokens.append_all(self.attrs.outer());
Michael Layzell6a5a1642017-06-04 19:35:15 -04002433 match self.kind {
2434 InPlaceKind::Arrow(ref arrow) => {
2435 self.place.to_tokens(tokens);
2436 arrow.to_tokens(tokens);
2437 self.value.to_tokens(tokens);
2438 }
2439 InPlaceKind::In(ref _in) => {
2440 _in.to_tokens(tokens);
2441 self.place.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002442 // NOTE: The second operand must be in a block, add one if
2443 // it is not present.
David Tolnay8c91b882017-12-28 23:04:32 -05002444 if let Expr::Block(_) = *self.value {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002445 self.value.to_tokens(tokens);
2446 } else {
David Tolnay32954ef2017-12-26 22:43:16 -05002447 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002448 self.value.to_tokens(tokens);
2449 })
2450 }
Michael Layzell6a5a1642017-06-04 19:35:15 -04002451 }
2452 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002453 }
2454 }
2455
Michael Layzell734adb42017-06-07 16:58:31 -04002456 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002457 impl ToTokens for ExprArray {
2458 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002459 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002460 self.bracket_token.surround(tokens, |tokens| {
2461 self.exprs.to_tokens(tokens);
2462 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002463 }
2464 }
2465
2466 impl ToTokens for ExprCall {
2467 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002468 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002469 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002470 self.paren_token.surround(tokens, |tokens| {
2471 self.args.to_tokens(tokens);
2472 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002473 }
2474 }
2475
Michael Layzell734adb42017-06-07 16:58:31 -04002476 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002477 impl ToTokens for ExprMethodCall {
2478 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002479 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002480 self.expr.to_tokens(tokens);
2481 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002482 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002483 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002484 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2485 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002486 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002487 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002488 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002489 self.paren_token.surround(tokens, |tokens| {
2490 self.args.to_tokens(tokens);
2491 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002492 }
2493 }
2494
Michael Layzell734adb42017-06-07 16:58:31 -04002495 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002496 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002497 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002498 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002499 self.paren_token.surround(tokens, |tokens| {
2500 self.args.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002501 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002502 // distinguish ExprTuple from ExprParen.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002503 if self.args.len() == 1 && !self.args.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002504 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002505 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002506 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002507 }
2508 }
2509
2510 impl ToTokens for ExprBinary {
2511 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002512 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002513 self.left.to_tokens(tokens);
2514 self.op.to_tokens(tokens);
2515 self.right.to_tokens(tokens);
2516 }
2517 }
2518
2519 impl ToTokens for ExprUnary {
2520 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002521 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002522 self.op.to_tokens(tokens);
2523 self.expr.to_tokens(tokens);
2524 }
2525 }
2526
David Tolnay8c91b882017-12-28 23:04:32 -05002527 impl ToTokens for ExprLit {
2528 fn to_tokens(&self, tokens: &mut Tokens) {
2529 attrs_to_tokens(&self.attrs, tokens);
2530 self.lit.to_tokens(tokens);
2531 }
2532 }
2533
Alex Crichton62a0a592017-05-22 13:58:53 -07002534 impl ToTokens for ExprCast {
2535 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002536 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002537 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002538 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002539 self.ty.to_tokens(tokens);
2540 }
2541 }
2542
2543 impl ToTokens for ExprType {
2544 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002545 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002546 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002547 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002548 self.ty.to_tokens(tokens);
2549 }
2550 }
2551
Michael Layzell734adb42017-06-07 16:58:31 -04002552 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002553 fn maybe_wrap_else(
2554 tokens: &mut Tokens,
2555 else_token: &Option<Token![else]>,
2556 if_false: &Option<Box<Expr>>,
2557 ) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002558 if let Some(ref if_false) = *if_false {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002559 TokensOrDefault(else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002560
2561 // If we are not one of the valid expressions to exist in an else
2562 // clause, wrap ourselves in a block.
David Tolnay8c91b882017-12-28 23:04:32 -05002563 match **if_false {
2564 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002565 if_false.to_tokens(tokens);
2566 }
2567 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002568 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002569 if_false.to_tokens(tokens);
2570 });
2571 }
2572 }
2573 }
2574 }
2575
2576 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002577 impl ToTokens for ExprIf {
2578 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002579 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002580 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002581 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002582 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002583 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002584 }
2585 }
2586
Michael Layzell734adb42017-06-07 16:58:31 -04002587 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002588 impl ToTokens for ExprIfLet {
2589 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002590 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002591 self.if_token.to_tokens(tokens);
2592 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002594 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002595 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002596 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002597 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002598 }
2599 }
2600
Michael Layzell734adb42017-06-07 16:58:31 -04002601 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002602 impl ToTokens for ExprWhile {
2603 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002604 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002605 if self.label.is_some() {
2606 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002607 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002608 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002609 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002610 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002611 self.body.to_tokens(tokens);
2612 }
2613 }
2614
Michael Layzell734adb42017-06-07 16:58:31 -04002615 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002616 impl ToTokens for ExprWhileLet {
2617 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002618 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002619 if self.label.is_some() {
2620 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002621 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002622 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002623 self.while_token.to_tokens(tokens);
2624 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002625 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002626 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002627 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002628 self.body.to_tokens(tokens);
2629 }
2630 }
2631
Michael Layzell734adb42017-06-07 16:58:31 -04002632 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002633 impl ToTokens for ExprForLoop {
2634 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002635 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002636 if self.label.is_some() {
2637 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002638 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002639 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002640 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002641 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002642 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002643 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002644 self.body.to_tokens(tokens);
2645 }
2646 }
2647
Michael Layzell734adb42017-06-07 16:58:31 -04002648 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002649 impl ToTokens for ExprLoop {
2650 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002651 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002652 if self.label.is_some() {
2653 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002654 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002655 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002656 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002657 self.body.to_tokens(tokens);
2658 }
2659 }
2660
Michael Layzell734adb42017-06-07 16:58:31 -04002661 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002662 impl ToTokens for ExprMatch {
2663 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002664 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002665 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002666 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002667 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002668 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002669 arm.to_tokens(tokens);
2670 // Ensure that we have a comma after a non-block arm, except
2671 // for the last one.
2672 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002673 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002674 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002675 }
2676 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002677 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002678 }
2679 }
2680
Michael Layzell734adb42017-06-07 16:58:31 -04002681 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002682 impl ToTokens for ExprCatch {
2683 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002684 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002685 self.do_token.to_tokens(tokens);
2686 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002687 self.block.to_tokens(tokens);
2688 }
2689 }
2690
Michael Layzell734adb42017-06-07 16:58:31 -04002691 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002692 impl ToTokens for ExprYield {
2693 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002694 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002695 self.yield_token.to_tokens(tokens);
2696 self.expr.to_tokens(tokens);
2697 }
2698 }
2699
2700 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002701 impl ToTokens for ExprClosure {
2702 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002703 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002704 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002705 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002706 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002707 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002708 FnArg::Captured(ArgCaptured {
2709 ref pat,
2710 ty: Type::Infer(_),
2711 ..
2712 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002713 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002714 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002715 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002716 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002717 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002718 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002719 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002720 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002721 self.body.to_tokens(tokens);
2722 }
2723 }
2724
Michael Layzell734adb42017-06-07 16:58:31 -04002725 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002726 impl ToTokens for ExprUnsafe {
2727 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002728 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002729 self.unsafe_token.to_tokens(tokens);
2730 self.block.to_tokens(tokens);
2731 }
2732 }
2733
2734 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002735 impl ToTokens for ExprBlock {
2736 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002737 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002738 self.block.to_tokens(tokens);
2739 }
2740 }
2741
Michael Layzell734adb42017-06-07 16:58:31 -04002742 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002743 impl ToTokens for ExprAssign {
2744 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002745 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002746 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002747 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002748 self.right.to_tokens(tokens);
2749 }
2750 }
2751
Michael Layzell734adb42017-06-07 16:58:31 -04002752 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002753 impl ToTokens for ExprAssignOp {
2754 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002755 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002756 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002757 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002758 self.right.to_tokens(tokens);
2759 }
2760 }
2761
Michael Layzell734adb42017-06-07 16:58:31 -04002762 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002763 impl ToTokens for ExprField {
2764 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002765 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002766 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002767 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002768 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 }
2770 }
2771
Michael Layzell734adb42017-06-07 16:58:31 -04002772 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002773 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002774 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002775 match *self {
2776 Member::Named(ident) => ident.to_tokens(tokens),
2777 Member::Unnamed(ref index) => index.to_tokens(tokens),
2778 }
2779 }
2780 }
2781
2782 #[cfg(feature = "full")]
2783 impl ToTokens for Index {
2784 fn to_tokens(&self, tokens: &mut Tokens) {
2785 tokens.append(TokenTree {
2786 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002787 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002788 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002789 }
2790 }
2791
2792 impl ToTokens for ExprIndex {
2793 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002794 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002795 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002796 self.bracket_token.surround(tokens, |tokens| {
2797 self.index.to_tokens(tokens);
2798 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002799 }
2800 }
2801
Michael Layzell734adb42017-06-07 16:58:31 -04002802 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002803 impl ToTokens for ExprRange {
2804 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002805 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002806 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002807 match self.limits {
2808 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2809 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2810 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002811 self.to.to_tokens(tokens);
2812 }
2813 }
2814
2815 impl ToTokens for ExprPath {
2816 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002817 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002818 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002819 }
2820 }
2821
Michael Layzell734adb42017-06-07 16:58:31 -04002822 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002823 impl ToTokens for ExprAddrOf {
2824 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002825 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002826 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002827 self.mutbl.to_tokens(tokens);
2828 self.expr.to_tokens(tokens);
2829 }
2830 }
2831
Michael Layzell734adb42017-06-07 16:58:31 -04002832 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002833 impl ToTokens for ExprBreak {
2834 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002835 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002836 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002837 self.label.to_tokens(tokens);
2838 self.expr.to_tokens(tokens);
2839 }
2840 }
2841
Michael Layzell734adb42017-06-07 16:58:31 -04002842 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002843 impl ToTokens for ExprContinue {
2844 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002845 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002846 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002847 self.label.to_tokens(tokens);
2848 }
2849 }
2850
Michael Layzell734adb42017-06-07 16:58:31 -04002851 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002852 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002853 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002854 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002855 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002856 self.expr.to_tokens(tokens);
2857 }
2858 }
2859
Michael Layzell734adb42017-06-07 16:58:31 -04002860 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002861 impl ToTokens for ExprMacro {
2862 fn to_tokens(&self, tokens: &mut Tokens) {
2863 tokens.append_all(self.attrs.outer());
2864 self.mac.to_tokens(tokens);
2865 }
2866 }
2867
2868 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002869 impl ToTokens for ExprStruct {
2870 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002871 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002872 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002873 self.brace_token.surround(tokens, |tokens| {
2874 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002875 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002876 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002877 self.rest.to_tokens(tokens);
2878 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002879 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002880 }
2881 }
2882
Michael Layzell734adb42017-06-07 16:58:31 -04002883 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002884 impl ToTokens for ExprRepeat {
2885 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002886 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002887 self.bracket_token.surround(tokens, |tokens| {
2888 self.expr.to_tokens(tokens);
2889 self.semi_token.to_tokens(tokens);
2890 self.amt.to_tokens(tokens);
2891 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002892 }
2893 }
2894
David Tolnaye98775f2017-12-28 23:17:00 -05002895 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002896 impl ToTokens for ExprGroup {
2897 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002898 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002899 self.group_token.surround(tokens, |tokens| {
2900 self.expr.to_tokens(tokens);
2901 });
2902 }
2903 }
2904
David Tolnaye98775f2017-12-28 23:17:00 -05002905 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002906 impl ToTokens for ExprParen {
2907 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002908 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002909 self.paren_token.surround(tokens, |tokens| {
2910 self.expr.to_tokens(tokens);
2911 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002912 }
2913 }
2914
Michael Layzell734adb42017-06-07 16:58:31 -04002915 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002916 impl ToTokens for ExprTry {
2917 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002918 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002919 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002920 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002921 }
2922 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002923
Michael Layzell734adb42017-06-07 16:58:31 -04002924 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002925 impl ToTokens for FieldValue {
2926 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002927 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002928 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2929 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002930 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002931 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002932 self.expr.to_tokens(tokens);
2933 }
David Tolnay055a7042016-10-02 19:23:54 -07002934 }
2935 }
2936
Michael Layzell734adb42017-06-07 16:58:31 -04002937 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002938 impl ToTokens for Arm {
2939 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002940 tokens.append_all(&self.attrs);
2941 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002942 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002943 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002944 self.guard.to_tokens(tokens);
2945 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002946 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002947 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002948 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002949 }
2950 }
2951
Michael Layzell734adb42017-06-07 16:58:31 -04002952 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002953 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002954 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002955 self.underscore_token.to_tokens(tokens);
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 PatIdent {
2961 fn to_tokens(&self, tokens: &mut Tokens) {
2962 self.mode.to_tokens(tokens);
2963 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002964 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002965 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002966 self.subpat.to_tokens(tokens);
2967 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002968 }
2969 }
2970
Michael Layzell734adb42017-06-07 16:58:31 -04002971 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002972 impl ToTokens for PatStruct {
2973 fn to_tokens(&self, tokens: &mut Tokens) {
2974 self.path.to_tokens(tokens);
2975 self.brace_token.surround(tokens, |tokens| {
2976 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002977 // NOTE: We need a comma before the dot2 token if it is present.
2978 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002979 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002980 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002981 self.dot2_token.to_tokens(tokens);
2982 });
2983 }
2984 }
2985
Michael Layzell734adb42017-06-07 16:58:31 -04002986 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002987 impl ToTokens for PatTupleStruct {
2988 fn to_tokens(&self, tokens: &mut Tokens) {
2989 self.path.to_tokens(tokens);
2990 self.pat.to_tokens(tokens);
2991 }
2992 }
2993
Michael Layzell734adb42017-06-07 16:58:31 -04002994 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002995 impl ToTokens for PatPath {
2996 fn to_tokens(&self, tokens: &mut Tokens) {
2997 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2998 }
2999 }
3000
Michael Layzell734adb42017-06-07 16:58:31 -04003001 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003002 impl ToTokens for PatTuple {
3003 fn to_tokens(&self, tokens: &mut Tokens) {
3004 self.paren_token.surround(tokens, |tokens| {
3005 for (i, token) in self.pats.iter().enumerate() {
3006 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07003007 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
3008 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003009 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003010 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003011 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003012
3013 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003014 // Ensure there is a comma before the .. token.
3015 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003016 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003017 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003018 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003019 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003020 });
3021 }
3022 }
3023
Michael Layzell734adb42017-06-07 16:58:31 -04003024 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003025 impl ToTokens for PatBox {
3026 fn to_tokens(&self, tokens: &mut Tokens) {
3027 self.box_token.to_tokens(tokens);
3028 self.pat.to_tokens(tokens);
3029 }
3030 }
3031
Michael Layzell734adb42017-06-07 16:58:31 -04003032 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003033 impl ToTokens for PatRef {
3034 fn to_tokens(&self, tokens: &mut Tokens) {
3035 self.and_token.to_tokens(tokens);
3036 self.mutbl.to_tokens(tokens);
3037 self.pat.to_tokens(tokens);
3038 }
3039 }
3040
Michael Layzell734adb42017-06-07 16:58:31 -04003041 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003042 impl ToTokens for PatLit {
3043 fn to_tokens(&self, tokens: &mut Tokens) {
3044 self.expr.to_tokens(tokens);
3045 }
3046 }
3047
Michael Layzell734adb42017-06-07 16:58:31 -04003048 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003049 impl ToTokens for PatRange {
3050 fn to_tokens(&self, tokens: &mut Tokens) {
3051 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003052 match self.limits {
3053 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3054 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3055 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003056 self.hi.to_tokens(tokens);
3057 }
3058 }
3059
Michael Layzell734adb42017-06-07 16:58:31 -04003060 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003061 impl ToTokens for PatSlice {
3062 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003063 // XXX: This is a mess, and it will be so easy to screw it up. How
3064 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003065 self.bracket_token.surround(tokens, |tokens| {
3066 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003067
3068 // If we need a comma before the middle or standalone .. token,
3069 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003070 if !self.front.empty_or_trailing()
3071 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003072 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003073 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003074 }
3075
3076 // If we have an identifier, we always need a .. token.
3077 if self.middle.is_some() {
3078 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003079 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003080 } else if self.dot2_token.is_some() {
3081 self.dot2_token.to_tokens(tokens);
3082 }
3083
3084 // Make sure we have a comma before the back half.
3085 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003086 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003087 self.back.to_tokens(tokens);
3088 } else {
3089 self.comma_token.to_tokens(tokens);
3090 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003091 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003092 }
3093 }
3094
Michael Layzell734adb42017-06-07 16:58:31 -04003095 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003096 impl ToTokens for FieldPat {
3097 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003098 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07003099 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05003100 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003101 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003102 }
3103 self.pat.to_tokens(tokens);
3104 }
3105 }
3106
Michael Layzell734adb42017-06-07 16:58:31 -04003107 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003108 impl ToTokens for BindingMode {
3109 fn to_tokens(&self, tokens: &mut Tokens) {
3110 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003111 BindingMode::ByRef(ref t, ref m) => {
3112 t.to_tokens(tokens);
3113 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003114 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003115 BindingMode::ByValue(ref m) => {
3116 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003117 }
3118 }
3119 }
3120 }
David Tolnay42602292016-10-01 22:25:45 -07003121
Michael Layzell734adb42017-06-07 16:58:31 -04003122 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07003123 impl ToTokens for CaptureBy {
3124 fn to_tokens(&self, tokens: &mut Tokens) {
3125 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003126 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003127 CaptureBy::Ref => {
3128 // nothing
3129 }
David Tolnay89e05672016-10-02 14:39:42 -07003130 }
3131 }
3132 }
3133
Michael Layzell734adb42017-06-07 16:58:31 -04003134 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003135 impl ToTokens for Block {
3136 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003137 self.brace_token.surround(tokens, |tokens| {
3138 tokens.append_all(&self.stmts);
3139 });
David Tolnay42602292016-10-01 22:25:45 -07003140 }
3141 }
3142
Michael Layzell734adb42017-06-07 16:58:31 -04003143 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003144 impl ToTokens for Stmt {
3145 fn to_tokens(&self, tokens: &mut Tokens) {
3146 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003147 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003148 Stmt::Item(ref item) => item.to_tokens(tokens),
3149 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003150 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003151 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003152 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003153 }
David Tolnay42602292016-10-01 22:25:45 -07003154 }
3155 }
3156 }
David Tolnay191e0582016-10-02 18:31:09 -07003157
Michael Layzell734adb42017-06-07 16:58:31 -04003158 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003159 impl ToTokens for Local {
3160 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003161 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003162 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003163 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003164 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003165 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003166 self.ty.to_tokens(tokens);
3167 }
3168 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003169 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003170 self.init.to_tokens(tokens);
3171 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003172 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003173 }
3174 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003175}