blob: f717b2bb6cd78333fc55ae71e5ac926638e2278d [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayf4bbbd92016-09-23 14:41:55 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_struct! {
5 /// An expression.
6 pub struct Expr {
7 /// Type of the expression.
8 pub node: ExprKind,
Clar Charrd22b5702017-03-10 15:24:56 -05009
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// Attributes tagged on the expression.
11 pub attrs: Vec<Attribute>,
12 }
David Tolnay7184b132016-10-30 10:06:37 -070013}
14
15impl From<ExprKind> for Expr {
16 fn from(node: ExprKind) -> Expr {
17 Expr {
18 node: node,
19 attrs: Vec::new(),
20 }
21 }
22}
23
Alex Crichton62a0a592017-05-22 13:58:53 -070024ast_enum_of_structs! {
25 pub enum ExprKind {
26 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040027 pub Box(ExprBox #full {
Alex Crichton62a0a592017-05-22 13:58:53 -070028 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080029 pub box_token: Token![box],
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }),
Clar Charrd22b5702017-03-10 15:24:56 -050031
Michael Layzellb78f3b52017-06-04 19:03:03 -040032 /// E.g. 'place <- val' or `in place { val }`.
Michael Layzell734adb42017-06-07 16:58:31 -040033 pub InPlace(ExprInPlace #full {
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub place: Box<Expr>,
Michael Layzell6a5a1642017-06-04 19:35:15 -040035 pub kind: InPlaceKind,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub value: Box<Expr>,
37 }),
Clar Charrd22b5702017-03-10 15:24:56 -050038
Alex Crichton62a0a592017-05-22 13:58:53 -070039 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040040 pub Array(ExprArray #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -080041 pub exprs: Delimited<Expr, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070042 pub bracket_token: tokens::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -070043 }),
Clar Charrd22b5702017-03-10 15:24:56 -050044
Alex Crichton62a0a592017-05-22 13:58:53 -070045 /// A function call.
46 pub Call(ExprCall {
47 pub func: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080048 pub args: Delimited<Expr, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070049 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -070050 }),
Clar Charrd22b5702017-03-10 15:24:56 -050051
Alex Crichton62a0a592017-05-22 13:58:53 -070052 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
53 ///
54 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080055 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070056 /// (within the angle brackets).
57 ///
Alex Crichton62a0a592017-05-22 13:58:53 -070058 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
59 /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
Michael Layzell734adb42017-06-07 16:58:31 -040060 pub MethodCall(ExprMethodCall #full {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070062 pub method: Ident,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080063 pub typarams: Delimited<Type, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080064 pub args: Delimited<Expr, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -080066 pub dot_token: Token![.],
67 pub lt_token: Option<Token![<]>,
68 pub colon2_token: Option<Token![::]>,
69 pub gt_token: Option<Token![>]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
Clar Charrd22b5702017-03-10 15:24:56 -050071
Alex Crichton62a0a592017-05-22 13:58:53 -070072 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050073 pub Tuple(ExprTuple #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -080074 pub args: Delimited<Expr, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070075 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -080076 pub lone_comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 }),
Clar Charrd22b5702017-03-10 15:24:56 -050078
Alex Crichton62a0a592017-05-22 13:58:53 -070079 /// A binary operation, e.g. `a + b`, `a * b`.
80 pub Binary(ExprBinary {
81 pub op: BinOp,
82 pub left: Box<Expr>,
83 pub right: Box<Expr>,
84 }),
Clar Charrd22b5702017-03-10 15:24:56 -050085
Alex Crichton62a0a592017-05-22 13:58:53 -070086 /// A unary operation, e.g. `!x`, `*x`.
87 pub Unary(ExprUnary {
88 pub op: UnOp,
89 pub expr: Box<Expr>,
90 }),
Clar Charrd22b5702017-03-10 15:24:56 -050091
Alex Crichton62a0a592017-05-22 13:58:53 -070092 /// A literal, e.g. `1`, `"foo"`.
93 pub Lit(Lit),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A cast, e.g. `foo as f64`.
96 pub Cast(ExprCast {
97 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080098 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080099 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700100 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500101
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 /// A type ascription, e.g. `foo: f64`.
103 pub Type(ExprType {
104 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800105 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800106 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700107 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500108
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 /// An `if` block, with an optional else block
110 ///
111 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400112 pub If(ExprIf #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 pub cond: Box<Expr>,
114 pub if_true: Block,
115 pub if_false: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800116 pub if_token: Token![if],
117 pub else_token: Option<Token![else]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700118 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500119
Alex Crichton62a0a592017-05-22 13:58:53 -0700120 /// An `if let` expression with an optional else block
121 ///
122 /// E.g., `if let pat = expr { block } else { expr }`
123 ///
124 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400125 pub IfLet(ExprIfLet #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 pub pat: Box<Pat>,
127 pub expr: Box<Expr>,
128 pub if_true: Block,
129 pub if_false: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800130 pub if_token: Token![if],
131 pub let_token: Token![let],
132 pub eq_token: Token![=],
133 pub else_token: Option<Token![else]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500135
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 /// A while loop, with an optional label
137 ///
138 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400139 pub While(ExprWhile #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 pub cond: Box<Expr>,
141 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700142 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800143 pub colon_token: Option<Token![:]>,
144 pub while_token: Token![while],
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500146
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 /// A while-let loop, with an optional label.
148 ///
149 /// E.g., `'label: while let pat = expr { block }`
150 ///
151 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400152 pub WhileLet(ExprWhileLet #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 pub pat: Box<Pat>,
154 pub expr: Box<Expr>,
155 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700156 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800157 pub colon_token: Option<Token![:]>,
158 pub while_token: Token![while],
159 pub let_token: Token![let],
160 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500162
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 /// A for loop, with an optional label.
164 ///
165 /// E.g., `'label: for pat in expr { block }`
166 ///
167 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400168 pub ForLoop(ExprForLoop #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700169 pub pat: Box<Pat>,
170 pub expr: Box<Expr>,
171 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700172 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800173 pub for_token: Token![for],
174 pub colon_token: Option<Token![:]>,
175 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500177
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 /// Conditionless loop with an optional label.
179 ///
180 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400181 pub Loop(ExprLoop #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700183 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800184 pub loop_token: Token![loop],
185 pub colon_token: Option<Token![:]>,
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 Tolnayf8db7ba2017-11-11 22:52:16 -0800190 pub match_token: Token![match],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700191 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 pub expr: Box<Expr>,
193 pub arms: Vec<Arm>,
194 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500195
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400197 pub Closure(ExprClosure #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700198 pub capture: CaptureBy,
199 pub decl: Box<FnDecl>,
200 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub or1_token: Token![|],
202 pub or2_token: Token![|],
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500204
Nika Layzell640832a2017-12-04 13:37:09 -0500205 /// An unsafe block (`unsafe { ... }`)
206 pub Unsafe(ExprUnsafe #full {
207 pub unsafe_token: Token![unsafe],
208 pub block: Block,
209 }),
210
211 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400212 pub Block(ExprBlock #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700213 pub block: Block,
214 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700215
Alex Crichton62a0a592017-05-22 13:58:53 -0700216 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400217 pub Assign(ExprAssign #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub left: Box<Expr>,
219 pub right: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800220 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223 /// An assignment with an operator
224 ///
225 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub AssignOp(ExprAssignOp #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 pub op: BinOp,
228 pub left: Box<Expr>,
229 pub right: Box<Expr>,
230 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500231
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 /// Access of a named struct field (`obj.foo`)
Michael Layzell734adb42017-06-07 16:58:31 -0400233 pub Field(ExprField #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 pub expr: Box<Expr>,
235 pub field: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800236 pub dot_token: Token![.],
Alex Crichton62a0a592017-05-22 13:58:53 -0700237 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500238
Alex Crichton62a0a592017-05-22 13:58:53 -0700239 /// Access of an unnamed field of a struct or tuple-struct
240 ///
241 /// For example, `foo.0`.
Michael Layzell734adb42017-06-07 16:58:31 -0400242 pub TupField(ExprTupField #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700244 pub field: Lit,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800245 pub dot_token: Token![.],
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 {
250 pub expr: Box<Expr>,
251 pub index: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700252 pub bracket_token: tokens::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500254
David Tolnaybe55d7b2017-12-17 23:41:20 -0800255 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400256 pub Range(ExprRange #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700257 pub from: Option<Box<Expr>>,
258 pub to: Option<Box<Expr>>,
259 pub limits: RangeLimits,
260 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700261
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 /// Variable reference, possibly containing `::` and/or type
263 /// parameters, e.g. foo::bar::<baz>.
264 ///
265 /// Optionally "qualified",
266 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
267 pub Path(ExprPath {
268 pub qself: Option<QSelf>,
269 pub path: Path,
270 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700271
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400273 pub AddrOf(ExprAddrOf #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800274 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 pub mutbl: Mutability,
276 pub expr: Box<Expr>,
277 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500278
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400280 pub Break(ExprBreak #full {
David Tolnay63e3dee2017-06-03 20:13:17 -0700281 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 pub expr: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800283 pub break_token: Token![break],
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500285
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400287 pub Continue(ExprContinue #full {
David Tolnay63e3dee2017-06-03 20:13:17 -0700288 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800289 pub continue_token: Token![continue],
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500291
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 /// A `return`, with an optional value to be returned
Michael Layzell734adb42017-06-07 16:58:31 -0400293 pub Ret(ExprRet #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 pub expr: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub return_token: Token![return],
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700297
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 /// A macro invocation; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800299 pub Macro(Macro),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700300
Alex Crichton62a0a592017-05-22 13:58:53 -0700301 /// A struct literal expression.
302 ///
303 /// For example, `Foo {x: 1, y: 2}`, or
304 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400305 pub Struct(ExprStruct #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800307 pub fields: Delimited<FieldValue, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 pub rest: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800309 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700310 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700312
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 /// An array literal constructed from one repeated element.
314 ///
315 /// For example, `[1; 5]`. The first expression is the element
316 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400317 pub Repeat(ExprRepeat #full {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700318 pub bracket_token: tokens::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800319 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700320 pub expr: Box<Expr>,
321 pub amt: Box<Expr>,
322 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700323
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 /// No-op: used solely so we can pretty-print faithfully
325 pub Paren(ExprParen {
326 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700327 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700328 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700329
Michael Layzell93c36282017-06-04 20:43:14 -0400330 /// No-op: used solely so we can pretty-print faithfully
331 ///
332 /// A `group` represents a `None`-delimited span in the input
333 /// `TokenStream` which affects the precidence of the resulting
334 /// expression. They are used for macro hygiene.
335 pub Group(ExprGroup {
336 pub expr: Box<Expr>,
337 pub group_token: tokens::Group,
338 }),
339
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400341 pub Try(ExprTry #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700342 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800343 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700344 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700345
Alex Crichton62a0a592017-05-22 13:58:53 -0700346 /// A catch expression.
347 ///
348 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400349 pub Catch(ExprCatch #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800350 pub do_token: Token![do],
351 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 pub block: Block,
353 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700354
355 /// A yield expression.
356 ///
357 /// E.g. `yield expr`
358 pub Yield(ExprYield #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800359 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700360 pub expr: Option<Box<Expr>>,
361 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700363}
364
Michael Layzell734adb42017-06-07 16:58:31 -0400365#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700366ast_struct! {
367 /// A field-value pair in a struct literal.
368 pub struct FieldValue {
369 /// Name of the field.
370 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -0500371
Alex Crichton62a0a592017-05-22 13:58:53 -0700372 /// Value of the field.
373 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500374
Alex Crichton62a0a592017-05-22 13:58:53 -0700375 /// Whether this is a shorthand field, e.g. `Struct { x }`
376 /// instead of `Struct { x: x }`.
377 pub is_shorthand: bool,
Clar Charrd22b5702017-03-10 15:24:56 -0500378
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 /// Attributes tagged on the field.
380 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700381
David Tolnayf8db7ba2017-11-11 22:52:16 -0800382 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 }
David Tolnay055a7042016-10-02 19:23:54 -0700384}
385
Michael Layzell734adb42017-06-07 16:58:31 -0400386#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700387ast_struct! {
388 /// A Block (`{ .. }`).
389 ///
390 /// E.g. `{ .. }` as in `fn foo() { .. }`
391 pub struct Block {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700392 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700393 /// Statements in a block
394 pub stmts: Vec<Stmt>,
395 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700396}
397
Michael Layzell734adb42017-06-07 16:58:31 -0400398#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700399ast_enum! {
400 /// A statement, usually ending in a semicolon.
401 pub enum Stmt {
402 /// A local (let) binding.
403 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700404
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 /// An item definition.
406 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700407
Alex Crichton62a0a592017-05-22 13:58:53 -0700408 /// Expr without trailing semicolon.
409 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700410
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800412 Semi(Box<Expr>, Token![;]),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700413
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 /// Macro invocation.
David Tolnaydecf28d2017-11-11 11:56:45 -0800415 Macro(Box<(Macro, MacStmtStyle, Vec<Attribute>)>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700416 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700417}
418
Michael Layzell734adb42017-06-07 16:58:31 -0400419#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700420ast_enum! {
421 /// How a macro was invoked.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700422 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700423 pub enum MacStmtStyle {
424 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
425 /// `foo!(...);`, `foo![...];`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800426 Semicolon(Token![;]),
Clar Charrd22b5702017-03-10 15:24:56 -0500427
Alex Crichton62a0a592017-05-22 13:58:53 -0700428 /// The macro statement had braces; e.g. foo! { ... }
429 Braces,
Clar Charrd22b5702017-03-10 15:24:56 -0500430
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 /// The macro statement had parentheses or brackets and no semicolon; e.g.
432 /// `foo!(...)`. All of these will end up being converted into macro
433 /// expressions.
434 NoBraces,
435 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700436}
437
Michael Layzell734adb42017-06-07 16:58:31 -0400438#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700439ast_struct! {
440 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
441 pub struct Local {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800442 pub let_token: Token![let],
443 pub colon_token: Option<Token![:]>,
444 pub eq_token: Option<Token![=]>,
445 pub semi_token: Token![;],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700446
Alex Crichton62a0a592017-05-22 13:58:53 -0700447 pub pat: Box<Pat>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800448 pub ty: Option<Box<Type>>,
Clar Charrd22b5702017-03-10 15:24:56 -0500449
Alex Crichton62a0a592017-05-22 13:58:53 -0700450 /// Initializer expression to set the value, if any
451 pub init: Option<Box<Expr>>,
452 pub attrs: Vec<Attribute>,
453 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700454}
455
Michael Layzell734adb42017-06-07 16:58:31 -0400456#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700457ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700458 // Clippy false positive
459 // https://github.com/Manishearth/rust-clippy/issues/1241
460 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
461 pub enum Pat {
462 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700463 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800464 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700465 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700466
Alex Crichton62a0a592017-05-22 13:58:53 -0700467 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
468 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
469 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
470 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700471 pub Ident(PatIdent {
472 pub mode: BindingMode,
473 pub ident: Ident,
474 pub subpat: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800475 pub at_token: Option<Token![@]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700476 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700477
Alex Crichton62a0a592017-05-22 13:58:53 -0700478 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
479 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700480 pub Struct(PatStruct {
481 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800482 pub fields: Delimited<FieldPat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700483 pub brace_token: tokens::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800484 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700485 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700486
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
488 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
489 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700490 pub TupleStruct(PatTupleStruct {
491 pub path: Path,
492 pub pat: PatTuple,
493 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700494
Alex Crichton62a0a592017-05-22 13:58:53 -0700495 /// A possibly qualified path pattern.
496 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
497 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
498 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700499 pub Path(PatPath {
500 pub qself: Option<QSelf>,
501 pub path: Path,
502 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700503
Alex Crichton62a0a592017-05-22 13:58:53 -0700504 /// A tuple pattern `(a, b)`.
505 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
506 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700507 pub Tuple(PatTuple {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800508 pub pats: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700509 pub dots_pos: Option<usize>,
510 pub paren_token: tokens::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800511 pub dot2_token: Option<Token![..]>,
512 pub comma_token: Option<Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700513 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700515 pub Box(PatBox {
516 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800517 pub box_token: Token![box],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700518 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700519 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700520 pub Ref(PatRef {
521 pub pat: Box<Pat>,
522 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800523 pub and_token: Token![&],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700524 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700526 pub Lit(PatLit {
527 pub expr: Box<Expr>,
528 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800529 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700530 pub Range(PatRange {
531 pub lo: Box<Expr>,
532 pub hi: Box<Expr>,
533 pub limits: RangeLimits,
534 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400535 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536 pub Slice(PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800537 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700538 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800539 pub back: Delimited<Pat, Token![,]>,
540 pub dot2_token: Option<Token![..]>,
541 pub comma_token: Option<Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 pub bracket_token: tokens::Bracket,
543 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700544 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800545 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700547}
548
Michael Layzell734adb42017-06-07 16:58:31 -0400549#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700550ast_struct! {
551 /// An arm of a 'match'.
552 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800553 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700554 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500555 /// ```rust
556 /// # #![feature(dotdoteq_in_patterns)]
557 /// #
558 /// # fn main() {
559 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700560 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500561 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700562 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500563 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700564 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500565 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700566 /// ```
567 pub struct Arm {
568 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800569 pub pats: Delimited<Pat, Token![|]>,
570 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700571 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800572 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700573 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800574 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700576}
577
Michael Layzell734adb42017-06-07 16:58:31 -0400578#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700579ast_enum! {
580 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700581 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700582 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800583 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 Ref,
585 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700586}
587
Michael Layzell734adb42017-06-07 16:58:31 -0400588#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700589ast_enum! {
590 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700591 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700592 pub enum RangeLimits {
593 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800594 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800596 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700597 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700598}
599
Michael Layzell734adb42017-06-07 16:58:31 -0400600#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700601ast_struct! {
602 /// A single field in a struct pattern
603 ///
604 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
605 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
606 /// except `is_shorthand` is true
607 pub struct FieldPat {
608 /// The identifier for the field
609 pub ident: Ident,
610 /// The pattern the field is destructured to
611 pub pat: Box<Pat>,
612 pub is_shorthand: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800613 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700614 pub attrs: Vec<Attribute>,
615 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700616}
617
Michael Layzell734adb42017-06-07 16:58:31 -0400618#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700619ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700620 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700621 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 ByValue(Mutability),
624 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700625}
626
Michael Layzell734adb42017-06-07 16:58:31 -0400627#[cfg(feature = "full")]
Michael Layzell6a5a1642017-06-04 19:35:15 -0400628ast_enum! {
629 #[cfg_attr(feature = "clone-impls", derive(Copy))]
630 pub enum InPlaceKind {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800631 Arrow(Token![<-]),
632 In(Token![in]),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400633 }
634}
635
Michael Layzell3936ceb2017-07-08 00:28:36 -0400636#[cfg(any(feature = "parsing", feature = "printing"))]
637#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700638fn arm_expr_requires_comma(expr: &Expr) -> bool {
639 // see https://github.com/rust-lang/rust/blob/eb8f2586e
640 // /src/libsyntax/parse/classify.rs#L17-L37
641 match expr.node {
Nika Layzell640832a2017-12-04 13:37:09 -0500642 ExprKind::Unsafe(..) |
Alex Crichton03b30272017-08-28 09:35:24 -0700643 ExprKind::Block(..) |
644 ExprKind::If(..) |
645 ExprKind::IfLet(..) |
646 ExprKind::Match(..) |
647 ExprKind::While(..) |
648 ExprKind::WhileLet(..) |
649 ExprKind::Loop(..) |
650 ExprKind::ForLoop(..) |
651 ExprKind::Catch(..) => false,
652 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400653 }
654}
655
David Tolnayb9c8e322016-09-23 20:48:37 -0700656#[cfg(feature = "parsing")]
657pub mod parsing {
658 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700659 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700660
Michael Layzell734adb42017-06-07 16:58:31 -0400661 #[cfg(feature = "full")]
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -0700662 use proc_macro2::{TokenStream, TokenNode, Delimiter, Term};
Michael Layzell734adb42017-06-07 16:58:31 -0400663 use synom::{PResult, Cursor, Synom};
664 #[cfg(feature = "full")]
665 use synom::parse_error;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700666
David Tolnaybcf26022017-12-25 22:10:52 -0500667 // When we're parsing expressions which occur before blocks, like in an if
668 // statement's condition, we cannot parse a struct literal.
669 //
670 // Struct literals are ambiguous in certain positions
671 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700672 macro_rules! ambiguous_expr {
673 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700674 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700675 };
676 }
677
David Tolnaybcf26022017-12-25 22:10:52 -0500678 // When we are parsing an optional suffix expression, we cannot allow blocks
679 // if structs are not allowed.
680 //
681 // Example:
682 //
683 // if break {} {}
684 //
685 // is ambiguous between:
686 //
687 // if (break {}) {}
688 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400689 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400690 macro_rules! opt_ambiguous_expr {
691 ($i:expr, $allow_struct:ident) => {
692 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
693 };
694 }
695
Alex Crichton954046c2017-05-30 21:49:42 -0700696 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400697 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700698
699 fn description() -> Option<&'static str> {
700 Some("expression")
701 }
702 }
703
Michael Layzell734adb42017-06-07 16:58:31 -0400704 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700705 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
706
David Tolnaybcf26022017-12-25 22:10:52 -0500707 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400708 #[cfg(feature = "full")]
709 fn ambiguous_expr(i: Cursor,
710 allow_struct: bool,
711 allow_block: bool)
712 -> PResult<Expr> {
Michael Layzellb78f3b52017-06-04 19:03:03 -0400713 map!(
David Tolnay54e854d2016-10-24 12:03:30 -0700714 i,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400715 call!(assign_expr, allow_struct, allow_block),
716 ExprKind::into
717 )
718 }
719
Michael Layzell734adb42017-06-07 16:58:31 -0400720 #[cfg(not(feature = "full"))]
721 fn ambiguous_expr(i: Cursor,
722 allow_struct: bool,
723 allow_block: bool)
724 -> PResult<Expr> {
725 map!(
726 i,
727 // NOTE: We intentionally skip assign_expr, placement_expr, and
728 // range_expr, as they are not parsed in non-full mode.
729 call!(or_expr, allow_struct, allow_block),
730 ExprKind::into
731 )
732 }
733
David Tolnaybcf26022017-12-25 22:10:52 -0500734 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400735 macro_rules! binop {
736 (
737 $name: ident,
738 $next: ident,
739 $submac: ident!( $($args:tt)* )
740 ) => {
741 named!($name(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
742 mut e: call!($next, allow_struct, allow_block) >>
743 many0!(do_parse!(
744 op: $submac!($($args)*) >>
745 rhs: call!($next, allow_struct, true) >>
746 ({
747 e = ExprBinary {
748 left: Box::new(e.into()),
749 op: op,
750 right: Box::new(rhs.into()),
751 }.into();
752 })
753 )) >>
754 (e)
755 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700756 }
David Tolnay54e854d2016-10-24 12:03:30 -0700757 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700758
David Tolnaybcf26022017-12-25 22:10:52 -0500759 // <placement> = <placement> ..
760 // <placement> += <placement> ..
761 // <placement> -= <placement> ..
762 // <placement> *= <placement> ..
763 // <placement> /= <placement> ..
764 // <placement> %= <placement> ..
765 // <placement> ^= <placement> ..
766 // <placement> &= <placement> ..
767 // <placement> |= <placement> ..
768 // <placement> <<= <placement> ..
769 // <placement> >>= <placement> ..
770 //
771 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400772 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400773 named!(assign_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
774 mut e: call!(placement_expr, allow_struct, allow_block) >>
775 alt!(
776 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800777 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400778 // Recurse into self to parse right-associative operator.
779 rhs: call!(assign_expr, allow_struct, true) >>
780 ({
781 e = ExprAssign {
782 left: Box::new(e.into()),
783 eq_token: eq,
784 right: Box::new(rhs.into()),
785 }.into();
786 })
787 )
788 |
789 do_parse!(
790 op: call!(BinOp::parse_assign_op) >>
791 // Recurse into self to parse right-associative operator.
792 rhs: call!(assign_expr, allow_struct, true) >>
793 ({
794 e = ExprAssignOp {
795 left: Box::new(e.into()),
796 op: op,
797 right: Box::new(rhs.into()),
798 }.into();
799 })
800 )
801 |
802 epsilon!()
803 ) >>
804 (e)
805 ));
806
David Tolnaybcf26022017-12-25 22:10:52 -0500807 // <range> <- <range> ..
808 //
809 // NOTE: The `in place { expr }` version of this syntax is parsed in
810 // `atom_expr`, not here.
811 //
812 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400813 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400814 named!(placement_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
815 mut e: call!(range_expr, allow_struct, allow_block) >>
816 alt!(
817 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800818 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400819 // Recurse into self to parse right-associative operator.
820 rhs: call!(placement_expr, allow_struct, true) >>
821 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400822 e = ExprInPlace {
823 // op: BinOp::Place(larrow),
824 place: Box::new(e.into()),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400825 kind: InPlaceKind::Arrow(arrow),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400826 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400827 }.into();
828 })
829 )
830 |
831 epsilon!()
832 ) >>
833 (e)
834 ));
835
David Tolnaybcf26022017-12-25 22:10:52 -0500836 // <or> ... <or> ..
837 // <or> .. <or> ..
838 // <or> ..
839 //
840 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
841 // rules are for parsing these expressions are, but this is not correct.
842 // For example, `a .. b .. c` is not a legal expression. It should not
843 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
844 //
845 // NOTE: The form of ranges which don't include a preceding expression are
846 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400847 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400848 named!(range_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
849 mut e: call!(or_expr, allow_struct, allow_block) >>
850 many0!(do_parse!(
851 limits: syn!(RangeLimits) >>
852 // We don't want to allow blocks here if we don't allow structs. See
853 // the reasoning for `opt_ambiguous_expr!` above.
854 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
855 ({
856 e = ExprRange {
857 from: Some(Box::new(e.into())),
858 limits: limits,
859 to: hi.map(|e| Box::new(e.into())),
860 }.into();
861 })
862 )) >>
863 (e)
864 ));
865
David Tolnaybcf26022017-12-25 22:10:52 -0500866 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800867 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400868
David Tolnaybcf26022017-12-25 22:10:52 -0500869 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800870 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400871
David Tolnaybcf26022017-12-25 22:10:52 -0500872 // <bitor> == <bitor> ...
873 // <bitor> != <bitor> ...
874 // <bitor> >= <bitor> ...
875 // <bitor> <= <bitor> ...
876 // <bitor> > <bitor> ...
877 // <bitor> < <bitor> ...
878 //
879 // NOTE: This operator appears to be parsed as left-associative, but errors
880 // if it is used in a non-associative manner.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400881 binop!(compare_expr, bitor_expr, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800882 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400883 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800884 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400885 |
886 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800887 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400888 |
889 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800890 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400891 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400892 do_parse!(
893 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800894 not!(punct!(<-)) >>
895 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400896 (BinOp::Lt(t))
897 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400898 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800899 punct!(>) => { BinOp::Gt }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400900 ));
901
David Tolnaybcf26022017-12-25 22:10:52 -0500902 // <bitxor> | <bitxor> ...
Michael Layzellb78f3b52017-06-04 19:03:03 -0400903 binop!(bitor_expr, bitxor_expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800904 not!(punct!(||)) >>
905 not!(punct!(|=)) >>
906 t: punct!(|) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400907 (BinOp::BitOr(t))
908 ));
909
David Tolnaybcf26022017-12-25 22:10:52 -0500910 // <bitand> ^ <bitand> ...
Michael Layzellb78f3b52017-06-04 19:03:03 -0400911 binop!(bitxor_expr, bitand_expr, do_parse!(
912 // NOTE: Make sure we aren't looking at ^=.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800913 not!(punct!(^=)) >>
914 t: punct!(^) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400915 (BinOp::BitXor(t))
916 ));
917
David Tolnaybcf26022017-12-25 22:10:52 -0500918 // <shift> & <shift> ...
Michael Layzellb78f3b52017-06-04 19:03:03 -0400919 binop!(bitand_expr, shift_expr, do_parse!(
920 // NOTE: Make sure we aren't looking at && or &=.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800921 not!(punct!(&&)) >>
922 not!(punct!(&=)) >>
923 t: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400924 (BinOp::BitAnd(t))
925 ));
926
David Tolnaybcf26022017-12-25 22:10:52 -0500927 // <arith> << <arith> ...
928 // <arith> >> <arith> ...
Michael Layzellb78f3b52017-06-04 19:03:03 -0400929 binop!(shift_expr, arith_expr, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800930 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400931 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800932 punct!(>>) => { BinOp::Shr }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400933 ));
934
David Tolnaybcf26022017-12-25 22:10:52 -0500935 // <term> + <term> ...
936 // <term> - <term> ...
Michael Layzellb78f3b52017-06-04 19:03:03 -0400937 binop!(arith_expr, term_expr, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800938 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400939 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800940 punct!(-) => { BinOp::Sub }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400941 ));
942
David Tolnaybcf26022017-12-25 22:10:52 -0500943 // <cast> * <cast> ...
944 // <cast> / <cast> ...
945 // <cast> % <cast> ...
Michael Layzellb78f3b52017-06-04 19:03:03 -0400946 binop!(term_expr, cast_expr, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800947 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400948 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800949 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400950 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800951 punct!(%) => { BinOp::Rem }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952 ));
953
David Tolnaybcf26022017-12-25 22:10:52 -0500954 // <unary> as <ty>
955 // <unary> : <ty>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400956 named!(cast_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
957 mut e: call!(unary_expr, allow_struct, allow_block) >>
958 many0!(alt!(
959 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800960 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400961 // We can't accept `A + B` in cast expressions, as it's
962 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800963 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400964 ({
965 e = ExprCast {
966 expr: Box::new(e.into()),
967 as_token: as_,
968 ty: Box::new(ty),
969 }.into();
970 })
971 )
972 |
973 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400975 // We can't accept `A + B` in cast expressions, as it's
976 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800977 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978 ({
979 e = ExprType {
980 expr: Box::new(e.into()),
981 colon_token: colon,
982 ty: Box::new(ty),
983 }.into();
984 })
985 )
986 )) >>
987 (e)
988 ));
989
David Tolnaybcf26022017-12-25 22:10:52 -0500990 // <UnOp> <trailer>
991 // & <trailer>
992 // &mut <trailer>
993 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -0400994 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400995 named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
996 do_parse!(
997 op: syn!(UnOp) >>
998 expr: call!(unary_expr, allow_struct, true) >>
999 (ExprUnary {
1000 op: op,
1001 expr: Box::new(expr.into()),
1002 }.into())
1003 )
1004 |
1005 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001006 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001007 mutability: syn!(Mutability) >>
1008 expr: call!(unary_expr, allow_struct, true) >>
1009 (ExprAddrOf {
1010 and_token: and,
1011 mutbl: mutability,
1012 expr: Box::new(expr.into()),
1013 }.into())
1014 )
1015 |
1016 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001018 expr: call!(unary_expr, allow_struct, true) >>
1019 (ExprBox {
1020 box_token: box_,
1021 expr: Box::new(expr.into()),
1022 }.into())
1023 )
1024 |
1025 call!(trailer_expr, allow_struct, allow_block)
1026 ));
1027
Michael Layzell734adb42017-06-07 16:58:31 -04001028 // XXX: This duplication is ugly
1029 #[cfg(not(feature = "full"))]
1030 named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
1031 do_parse!(
1032 op: syn!(UnOp) >>
1033 expr: call!(unary_expr, allow_struct, true) >>
1034 (ExprUnary {
1035 op: op,
1036 expr: Box::new(expr.into()),
1037 }.into())
1038 )
1039 |
1040 call!(trailer_expr, allow_struct, allow_block)
1041 ));
1042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // <atom> (..<args>) ...
1044 // <atom> . <ident> (..<args>) ...
1045 // <atom> . <ident> ...
1046 // <atom> . <lit> ...
1047 // <atom> [ <expr> ] ...
1048 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001049 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001050 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
1051 mut e: call!(atom_expr, allow_struct, allow_block) >>
1052 many0!(alt!(
1053 tap!(args: and_call => {
1054 let (args, paren) = args;
1055 e = ExprCall {
1056 func: Box::new(e.into()),
1057 args: args,
1058 paren_token: paren,
1059 }.into();
1060 })
1061 |
1062 tap!(more: and_method_call => {
1063 let mut call = more;
1064 call.expr = Box::new(e.into());
1065 e = call.into();
1066 })
1067 |
1068 tap!(field: and_field => {
1069 let (field, token) = field;
1070 e = ExprField {
1071 expr: Box::new(e.into()),
1072 field: field,
1073 dot_token: token,
1074 }.into();
1075 })
1076 |
1077 tap!(field: and_tup_field => {
1078 let (field, token) = field;
1079 e = ExprTupField {
1080 expr: Box::new(e.into()),
1081 field: field,
1082 dot_token: token,
1083 }.into();
1084 })
1085 |
1086 tap!(i: and_index => {
1087 let (i, token) = i;
1088 e = ExprIndex {
1089 expr: Box::new(e.into()),
1090 bracket_token: token,
1091 index: Box::new(i),
1092 }.into();
1093 })
1094 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001095 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001096 e = ExprTry {
1097 expr: Box::new(e.into()),
1098 question_token: question,
1099 }.into();
1100 })
1101 )) >>
1102 (e)
1103 ));
1104
Michael Layzell734adb42017-06-07 16:58:31 -04001105 // XXX: Duplication == ugly
1106 #[cfg(not(feature = "full"))]
1107 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
1108 mut e: call!(atom_expr, allow_struct, allow_block) >>
1109 many0!(alt!(
1110 tap!(args: and_call => {
1111 let (args, paren) = args;
1112 e = ExprCall {
1113 func: Box::new(e.into()),
1114 args: args,
1115 paren_token: paren,
1116 }.into();
1117 })
1118 |
1119 tap!(i: and_index => {
1120 let (i, token) = i;
1121 e = ExprIndex {
1122 expr: Box::new(e.into()),
1123 bracket_token: token,
1124 index: Box::new(i),
1125 }.into();
1126 })
1127 )) >>
1128 (e)
1129 ));
1130
David Tolnaybcf26022017-12-25 22:10:52 -05001131 // Parse all atomic expressions which don't have to worry about precidence
1132 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001133 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001134 named!(atom_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
Michael Layzell93c36282017-06-04 20:43:14 -04001135 syn!(ExprGroup) => { ExprKind::Group } // must be placed first
1136 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001137 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
1138 |
1139 // must be before expr_path
1140 cond_reduce!(allow_struct, map!(syn!(ExprStruct), ExprKind::Struct))
1141 |
1142 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
1143 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001144 syn!(Macro) => { ExprKind::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001145 |
1146 call!(expr_break, allow_struct) // must be before expr_path
1147 |
1148 syn!(ExprContinue) => { ExprKind::Continue } // must be before expr_path
1149 |
1150 call!(expr_ret, allow_struct) // must be before expr_path
1151 |
1152 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
1153 syn!(ExprInPlace) => { ExprKind::InPlace }
1154 |
1155 syn!(ExprArray) => { ExprKind::Array }
1156 |
David Tolnay05362582017-12-26 01:33:57 -05001157 syn!(ExprTuple) => { ExprKind::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001158 |
1159 syn!(ExprIf) => { ExprKind::If }
1160 |
1161 syn!(ExprIfLet) => { ExprKind::IfLet }
1162 |
1163 syn!(ExprWhile) => { ExprKind::While }
1164 |
1165 syn!(ExprWhileLet) => { ExprKind::WhileLet }
1166 |
1167 syn!(ExprForLoop) => { ExprKind::ForLoop }
1168 |
1169 syn!(ExprLoop) => { ExprKind::Loop }
1170 |
1171 syn!(ExprMatch) => { ExprKind::Match }
1172 |
1173 syn!(ExprCatch) => { ExprKind::Catch }
1174 |
Alex Crichtonfe110462017-06-01 12:49:27 -07001175 syn!(ExprYield) => { ExprKind::Yield }
1176 |
Nika Layzell640832a2017-12-04 13:37:09 -05001177 syn!(ExprUnsafe) => { ExprKind::Unsafe }
1178 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179 call!(expr_closure, allow_struct)
1180 |
1181 cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
1182 |
1183 // NOTE: This is the prefix-form of range
1184 call!(expr_range, allow_struct)
1185 |
1186 syn!(ExprPath) => { ExprKind::Path }
1187 |
1188 syn!(ExprRepeat) => { ExprKind::Repeat }
1189 ));
1190
Michael Layzell734adb42017-06-07 16:58:31 -04001191 #[cfg(not(feature = "full"))]
1192 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> ExprKind, alt!(
1193 syn!(ExprGroup) => { ExprKind::Group } // must be placed first
1194 |
1195 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
1196 |
1197 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
1198 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001199 syn!(Macro) => { ExprKind::Macro } // must be before expr_path
Michael Layzell734adb42017-06-07 16:58:31 -04001200 |
1201 syn!(ExprPath) => { ExprKind::Path }
1202 ));
1203
1204
1205 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001206 named!(expr_nosemi -> Expr, map!(alt!(
1207 syn!(ExprIf) => { ExprKind::If }
1208 |
1209 syn!(ExprIfLet) => { ExprKind::IfLet }
1210 |
1211 syn!(ExprWhile) => { ExprKind::While }
1212 |
1213 syn!(ExprWhileLet) => { ExprKind::WhileLet }
1214 |
1215 syn!(ExprForLoop) => { ExprKind::ForLoop }
1216 |
1217 syn!(ExprLoop) => { ExprKind::Loop }
1218 |
1219 syn!(ExprMatch) => { ExprKind::Match }
1220 |
1221 syn!(ExprCatch) => { ExprKind::Catch }
1222 |
Alex Crichtonfe110462017-06-01 12:49:27 -07001223 syn!(ExprYield) => { ExprKind::Yield }
1224 |
Nika Layzell640832a2017-12-04 13:37:09 -05001225 syn!(ExprUnsafe) => { ExprKind::Unsafe }
1226 |
Michael Layzell35418782017-06-07 09:20:25 -04001227 syn!(ExprBlock) => { ExprKind::Block }
1228 ), Expr::from));
1229
Michael Layzell93c36282017-06-04 20:43:14 -04001230 impl Synom for ExprGroup {
1231 named!(parse -> Self, do_parse!(
1232 e: grouped!(syn!(Expr)) >>
1233 (ExprGroup {
1234 expr: Box::new(e.0),
1235 group_token: e.1,
1236 }.into())
1237 ));
1238 }
1239
Alex Crichton954046c2017-05-30 21:49:42 -07001240 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001241 named!(parse -> Self, do_parse!(
1242 e: parens!(syn!(Expr)) >>
1243 (ExprParen {
1244 expr: Box::new(e.0),
1245 paren_token: e.1,
1246 }.into())
1247 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001248 }
David Tolnay89e05672016-10-02 14:39:42 -07001249
Michael Layzell734adb42017-06-07 16:58:31 -04001250 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001251 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001252 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001253 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001254 place: expr_no_struct >>
1255 value: braces!(call!(Block::parse_within)) >>
1256 (ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001257 place: Box::new(place),
Michael Layzell6a5a1642017-06-04 19:35:15 -04001258 kind: InPlaceKind::In(in_),
Michael Layzell92639a52017-06-01 00:07:44 -04001259 value: Box::new(Expr {
1260 node: ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001261 block: Block {
1262 stmts: value.0,
1263 brace_token: value.1,
1264 },
1265 }.into(),
1266 attrs: Vec::new(),
1267 }),
1268 })
1269 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001270 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001271
Michael Layzell734adb42017-06-07 16:58:31 -04001272 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001273 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001274 named!(parse -> Self, do_parse!(
1275 elems: brackets!(call!(Delimited::parse_terminated)) >>
1276 (ExprArray {
1277 exprs: elems.0,
1278 bracket_token: elems.1,
1279 })
1280 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001281 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001282
David Tolnayf8db7ba2017-11-11 22:52:16 -08001283 named!(and_call -> (Delimited<Expr, Token![,]>, tokens::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001284 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001285
Michael Layzell734adb42017-06-07 16:58:31 -04001286 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001287 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001288 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001289 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001290 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001291 colon2: punct!(::) >>
1292 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001293 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001294 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001295 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001296 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001297 args: parens!(call!(Delimited::parse_terminated)) >>
1298 ({
1299 let (colon2, lt, tys, gt) = match typarams {
1300 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1301 None => (None, None, None, None),
1302 };
1303 ExprMethodCall {
1304 // this expr will get overwritten after being returned
1305 expr: Box::new(ExprKind::Lit(Lit {
1306 span: Span::default(),
1307 value: LitKind::Bool(false),
1308 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001309
Alex Crichton954046c2017-05-30 21:49:42 -07001310 method: method,
1311 args: args.0,
1312 paren_token: args.1,
1313 dot_token: dot,
1314 lt_token: lt,
1315 gt_token: gt,
1316 colon2_token: colon2,
1317 typarams: tys.unwrap_or_default(),
1318 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001319 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001320 ));
1321
Michael Layzell734adb42017-06-07 16:58:31 -04001322 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001323 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001324 named!(parse -> Self, do_parse!(
1325 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001326 (ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001327 args: elems.0,
1328 paren_token: elems.1,
1329 lone_comma: None, // TODO: parse this
1330 })
1331 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001332 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001333
Michael Layzell734adb42017-06-07 16:58:31 -04001334 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001335 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001336 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001337 if_: keyword!(if) >>
1338 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001339 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001340 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001341 cond: expr_no_struct >>
1342 then_block: braces!(call!(Block::parse_within)) >>
1343 else_block: option!(else_block) >>
1344 (ExprIfLet {
1345 pat: Box::new(pat),
1346 let_token: let_,
1347 eq_token: eq,
1348 expr: Box::new(cond),
1349 if_true: Block {
1350 stmts: then_block.0,
1351 brace_token: then_block.1,
1352 },
1353 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001354 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001355 if_false: else_block.map(|p| Box::new(p.1.into())),
1356 })
1357 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001358 }
1359
Michael Layzell734adb42017-06-07 16:58:31 -04001360 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001361 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001362 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001363 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001364 cond: expr_no_struct >>
1365 then_block: braces!(call!(Block::parse_within)) >>
1366 else_block: option!(else_block) >>
1367 (ExprIf {
1368 cond: Box::new(cond),
1369 if_true: Block {
1370 stmts: then_block.0,
1371 brace_token: then_block.1,
1372 },
1373 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001374 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001375 if_false: else_block.map(|p| Box::new(p.1.into())),
1376 })
1377 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001378 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001379
Michael Layzell734adb42017-06-07 16:58:31 -04001380 #[cfg(feature = "full")]
David Tolnayf8db7ba2017-11-11 22:52:16 -08001381 named!(else_block -> (Token![else], ExprKind), do_parse!(
1382 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001383 expr: alt!(
1384 syn!(ExprIf) => { ExprKind::If }
1385 |
1386 syn!(ExprIfLet) => { ExprKind::IfLet }
1387 |
1388 do_parse!(
1389 else_block: braces!(call!(Block::parse_within)) >>
1390 (ExprKind::Block(ExprBlock {
Alex Crichton954046c2017-05-30 21:49:42 -07001391 block: Block {
1392 stmts: else_block.0,
1393 brace_token: else_block.1,
1394 },
1395 }))
David Tolnay939766a2016-09-23 23:48:12 -07001396 )
Alex Crichton954046c2017-05-30 21:49:42 -07001397 ) >>
1398 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001399 ));
1400
David Tolnaybb6feae2016-10-02 21:25:20 -07001401
Michael Layzell734adb42017-06-07 16:58:31 -04001402 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001403 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001404 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001405 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1406 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001407 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001408 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001409 expr: expr_no_struct >>
1410 loop_block: syn!(Block) >>
1411 (ExprForLoop {
1412 for_token: for_,
1413 in_token: in_,
1414 pat: Box::new(pat),
1415 expr: Box::new(expr),
1416 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001417 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001418 label: lbl.map(|p| p.0),
1419 })
1420 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001421 }
Gregory Katze5f35682016-09-27 14:20:55 -04001422
Michael Layzell734adb42017-06-07 16:58:31 -04001423 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001424 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001425 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001426 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1427 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001428 loop_block: syn!(Block) >>
1429 (ExprLoop {
1430 loop_token: loop_,
1431 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001432 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001433 label: lbl.map(|p| p.0),
1434 })
1435 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001436 }
1437
Michael Layzell734adb42017-06-07 16:58:31 -04001438 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001439 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001440 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001441 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001442 obj: expr_no_struct >>
Alex Crichton03b30272017-08-28 09:35:24 -07001443 res: braces!(many0!(syn!(Arm))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001444 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001445 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001446 ExprMatch {
1447 expr: Box::new(obj),
1448 match_token: match_,
1449 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001450 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001451 }
1452 })
1453 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001454 }
David Tolnay1978c672016-10-27 22:05:52 -07001455
Michael Layzell734adb42017-06-07 16:58:31 -04001456 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001457 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001458 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001459 do_: keyword!(do) >>
1460 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001461 catch_block: syn!(Block) >>
1462 (ExprCatch {
1463 block: catch_block,
1464 do_token: do_,
1465 catch_token: catch_,
1466 }.into())
1467 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001468 }
Arnavion02ef13f2017-04-25 00:54:31 -07001469
Michael Layzell734adb42017-06-07 16:58:31 -04001470 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001471 impl Synom for ExprYield {
1472 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001473 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001474 expr: option!(syn!(Expr)) >>
1475 (ExprYield {
1476 yield_token: yield_,
1477 expr: expr.map(Box::new),
1478 })
1479 ));
1480 }
1481
1482 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001483 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001484 named!(parse -> Self, do_parse!(
1485 attrs: many0!(call!(Attribute::parse_outer)) >>
1486 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001487 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1488 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001489 body: do_parse!(
1490 expr: alt!(expr_nosemi | syn!(Expr)) >>
1491 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1492 map!(input_end!(), |_| None)
1493 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001494 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001495 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001496 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001497 (expr, comma1.and_then(|x| x).or(comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001498 ) >>
1499 (Arm {
1500 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001501 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001502 attrs: attrs,
1503 pats: pats,
1504 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001505 body: Box::new(body.0),
1506 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001507 })
1508 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001509 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001510
Michael Layzell734adb42017-06-07 16:58:31 -04001511 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001512 named!(expr_closure(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001513 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001514 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001515 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001516 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001517 ret_and_body: alt!(
1518 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001519 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001520 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001521 body: syn!(Block) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001522 (ReturnType::Type(ty, arrow),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001523 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001524 block: body,
1525 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001526 )
1527 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001528 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001529 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001530 (ExprClosure {
1531 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001532 or1_token: or1,
1533 or2_token: or2,
Alex Crichton62a0a592017-05-22 13:58:53 -07001534 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -07001535 inputs: inputs,
1536 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -07001537 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001538 dot_tokens: None,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001539 fn_token: <Token![fn]>::default(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001540 generics: Generics::default(),
1541 paren_token: tokens::Paren::default(),
David Tolnay89e05672016-10-02 14:39:42 -07001542 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001543 body: Box::new(ret_and_body.1),
1544 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001545 ));
1546
Michael Layzell734adb42017-06-07 16:58:31 -04001547 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001548 named!(fn_arg -> FnArg, do_parse!(
1549 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001550 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001551 ({
1552 let (colon, ty) = ty.unwrap_or_else(|| {
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001553 (<Token![:]>::default(), TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001554 underscore_token: <Token![_]>::default(),
Alex Crichton954046c2017-05-30 21:49:42 -07001555 }.into())
1556 });
1557 ArgCaptured {
1558 pat: pat,
1559 colon_token: colon,
1560 ty: ty,
1561 }.into()
David Tolnaybb6feae2016-10-02 21:25:20 -07001562 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001563 ));
1564
Michael Layzell734adb42017-06-07 16:58:31 -04001565 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001566 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001567 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001568 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1569 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001570 cond: expr_no_struct >>
1571 while_block: syn!(Block) >>
1572 (ExprWhile {
1573 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001574 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001575 cond: Box::new(cond),
1576 body: while_block,
1577 label: lbl.map(|p| p.0),
1578 })
1579 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001580 }
1581
Michael Layzell734adb42017-06-07 16:58:31 -04001582 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001583 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001584 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001585 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1586 while_: keyword!(while) >>
1587 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001588 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001589 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001590 value: expr_no_struct >>
1591 while_block: syn!(Block) >>
1592 (ExprWhileLet {
1593 eq_token: eq,
1594 let_token: let_,
1595 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001596 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001597 pat: Box::new(pat),
1598 expr: Box::new(value),
1599 body: while_block,
1600 label: lbl.map(|p| p.0),
1601 })
1602 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001603 }
1604
Michael Layzell734adb42017-06-07 16:58:31 -04001605 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001606 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001607 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001608 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001609 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001610 (ExprContinue {
1611 continue_token: cont,
1612 label: lbl,
1613 })
1614 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001615 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001616
Michael Layzell734adb42017-06-07 16:58:31 -04001617 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001618 named!(expr_break(allow_struct: bool) -> ExprKind, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001619 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001620 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001621 // We can't allow blocks after a `break` expression when we wouldn't
1622 // allow structs, as this expression is ambiguous.
1623 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001624 (ExprBreak {
1625 label: lbl,
1626 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001627 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001628 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001629 ));
1630
Michael Layzell734adb42017-06-07 16:58:31 -04001631 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001632 named!(expr_ret(allow_struct: bool) -> ExprKind, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001633 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001634 // NOTE: return is greedy and eats blocks after it even when in a
1635 // position where structs are not allowed, such as in if statement
1636 // conditions. For example:
1637 //
David Tolnaybcf26022017-12-25 22:10:52 -05001638 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001639 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001640 (ExprRet {
1641 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001642 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001643 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001644 ));
1645
Michael Layzell734adb42017-06-07 16:58:31 -04001646 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001647 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001648 named!(parse -> Self, do_parse!(
1649 path: syn!(Path) >>
1650 data: braces!(do_parse!(
1651 fields: call!(Delimited::parse_terminated) >>
1652 base: option!(
1653 cond!(fields.is_empty() || fields.trailing_delim(),
1654 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001655 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001656 base: syn!(Expr) >>
1657 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001658 )
Michael Layzell92639a52017-06-01 00:07:44 -04001659 )
1660 ) >>
1661 (fields, base)
1662 )) >>
1663 ({
1664 let ((fields, base), brace) = data;
1665 let (dots, rest) = match base.and_then(|b| b) {
1666 Some((dots, base)) => (Some(dots), Some(base)),
1667 None => (None, None),
1668 };
1669 ExprStruct {
1670 brace_token: brace,
1671 path: path,
1672 fields: fields,
1673 dot2_token: dots,
1674 rest: rest.map(Box::new),
1675 }
1676 })
1677 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001678 }
1679
Michael Layzell734adb42017-06-07 16:58:31 -04001680 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001681 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001682 named!(parse -> Self, alt!(
1683 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -07001684 ident: field_ident >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001685 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001686 value: syn!(Expr) >>
1687 (FieldValue {
David Tolnay570695e2017-06-03 16:15:13 -07001688 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04001689 expr: value,
1690 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001691 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001692 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001693 })
Michael Layzell92639a52017-06-01 00:07:44 -04001694 )
1695 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001696 map!(syn!(Ident), |name| FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001697 ident: name.clone(),
1698 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
1699 is_shorthand: true,
1700 attrs: Vec::new(),
1701 colon_token: None,
1702 })
1703 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001704 }
David Tolnay055a7042016-10-02 19:23:54 -07001705
Michael Layzell734adb42017-06-07 16:58:31 -04001706 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001707 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001708 named!(parse -> Self, do_parse!(
1709 data: brackets!(do_parse!(
1710 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001711 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001712 times: syn!(Expr) >>
1713 (value, semi, times)
1714 )) >>
1715 (ExprRepeat {
1716 expr: Box::new((data.0).0),
1717 amt: Box::new((data.0).2),
1718 bracket_token: data.1,
1719 semi_token: (data.0).1,
1720 })
1721 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001722 }
David Tolnay055a7042016-10-02 19:23:54 -07001723
Michael Layzell734adb42017-06-07 16:58:31 -04001724 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001725 impl Synom for ExprUnsafe {
1726 named!(parse -> Self, do_parse!(
1727 unsafe_: keyword!(unsafe) >>
1728 b: syn!(Block) >>
1729 (ExprUnsafe {
1730 unsafe_token: unsafe_,
1731 block: b,
1732 })
1733 ));
1734 }
1735
1736 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001737 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001738 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001739 b: syn!(Block) >>
1740 (ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001741 block: b,
1742 })
1743 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001744 }
David Tolnay89e05672016-10-02 14:39:42 -07001745
Michael Layzell734adb42017-06-07 16:58:31 -04001746 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001747 named!(expr_range(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001748 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001749 hi: opt_ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001750 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001751 ));
1752
Michael Layzell734adb42017-06-07 16:58:31 -04001753 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001754 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001755 named!(parse -> Self, alt!(
1756 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001757 punct!(..=) => { RangeLimits::Closed }
1758 |
1759 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001760 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001761 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001762 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001763 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001764 }
David Tolnay438c9052016-10-07 23:24:48 -07001765
Alex Crichton954046c2017-05-30 21:49:42 -07001766 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001767 named!(parse -> Self, do_parse!(
1768 pair: qpath >>
1769 (ExprPath {
1770 qself: pair.0,
1771 path: pair.1,
1772 })
1773 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001774 }
David Tolnay42602292016-10-01 22:25:45 -07001775
Michael Layzell734adb42017-06-07 16:58:31 -04001776 #[cfg(feature = "full")]
David Tolnayf8db7ba2017-11-11 22:52:16 -08001777 named!(and_field -> (Ident, Token![.]),
1778 map!(tuple!(punct!(.), syn!(Ident)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001779
Michael Layzell734adb42017-06-07 16:58:31 -04001780 #[cfg(feature = "full")]
David Tolnayf8db7ba2017-11-11 22:52:16 -08001781 named!(and_tup_field -> (Lit, Token![.]),
1782 map!(tuple!(punct!(.), syn!(Lit)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001783
Alex Crichton954046c2017-05-30 21:49:42 -07001784 named!(and_index -> (Expr, tokens::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001785
Michael Layzell734adb42017-06-07 16:58:31 -04001786 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001787 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001788 named!(parse -> Self, do_parse!(
1789 stmts: braces!(call!(Block::parse_within)) >>
1790 (Block {
1791 stmts: stmts.0,
1792 brace_token: stmts.1,
1793 })
1794 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001795 }
David Tolnay939766a2016-09-23 23:48:12 -07001796
Michael Layzell734adb42017-06-07 16:58:31 -04001797 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001798 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001799 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001800 many0!(punct!(;)) >>
1801 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001802 last: option!(do_parse!(
1803 attrs: many0!(call!(Attribute::parse_outer)) >>
1804 mut e: syn!(Expr) >>
1805 ({
1806 e.attrs = attrs;
1807 Stmt::Expr(Box::new(e))
1808 })
1809 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001810 (match last {
1811 None => standalone,
1812 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001813 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001814 standalone
1815 }
1816 })
1817 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001818 }
1819
Michael Layzell734adb42017-06-07 16:58:31 -04001820 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001821 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001822 named!(parse -> Self, alt!(
1823 stmt_mac
1824 |
1825 stmt_local
1826 |
1827 stmt_item
1828 |
Michael Layzell35418782017-06-07 09:20:25 -04001829 stmt_blockexpr
1830 |
Michael Layzell92639a52017-06-01 00:07:44 -04001831 stmt_expr
1832 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001833 }
David Tolnay939766a2016-09-23 23:48:12 -07001834
Michael Layzell734adb42017-06-07 16:58:31 -04001835 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001836 named!(stmt_mac -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001837 attrs: many0!(call!(Attribute::parse_outer)) >>
1838 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001839 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001840 // Only parse braces here; paren and bracket will get parsed as
1841 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001842 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001843 semi: option!(punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001844 (Stmt::Macro(Box::new((
1845 Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001846 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001847 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001848 tokens: vec![TokenTree(proc_macro2::TokenTree {
Alex Crichton954046c2017-05-30 21:49:42 -07001849 span: ((data.1).0).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001850 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnayeea28d62016-10-25 20:44:08 -07001851 })],
1852 },
Alex Crichton954046c2017-05-30 21:49:42 -07001853 match semi {
1854 Some(semi) => MacStmtStyle::Semicolon(semi),
1855 None => MacStmtStyle::Braces,
David Tolnay60d48942016-10-30 14:34:52 -07001856 },
David Tolnayeea28d62016-10-25 20:44:08 -07001857 attrs,
1858 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001859 ));
1860
Michael Layzell734adb42017-06-07 16:58:31 -04001861 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001862 named!(stmt_local -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001863 attrs: many0!(call!(Attribute::parse_outer)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001864 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001865 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001866 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001867 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1868 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001869 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001870 let_token: let_,
1871 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001872 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
1873 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001874 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001875 ty: ty.map(|p| Box::new(p.1)),
1876 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001877 attrs: attrs,
1878 })))
1879 ));
1880
Michael Layzell734adb42017-06-07 16:58:31 -04001881 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001882 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07001883
Michael Layzell734adb42017-06-07 16:58:31 -04001884 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001885 named!(stmt_blockexpr -> Stmt, do_parse!(
1886 attrs: many0!(call!(Attribute::parse_outer)) >>
1887 mut e: expr_nosemi >>
1888 // If the next token is a `.` or a `?` it is special-cased to parse as
1889 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08001890 not!(punct!(.)) >>
1891 not!(punct!(?)) >>
1892 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04001893 ({
1894 e.attrs = attrs;
1895 if let Some(semi) = semi {
1896 Stmt::Semi(Box::new(e), semi)
1897 } else {
1898 Stmt::Expr(Box::new(e))
1899 }
1900 })
1901 ));
David Tolnaycfe55022016-10-02 22:02:27 -07001902
Michael Layzell734adb42017-06-07 16:58:31 -04001903 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07001904 named!(stmt_expr -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001905 attrs: many0!(call!(Attribute::parse_outer)) >>
1906 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001907 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07001908 ({
1909 e.attrs = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04001910 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07001911 })
David Tolnay939766a2016-09-23 23:48:12 -07001912 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001913
Michael Layzell734adb42017-06-07 16:58:31 -04001914 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001915 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04001916 named!(parse -> Self, alt!(
1917 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
1918 |
1919 syn!(PatBox) => { Pat::Box } // must be before pat_ident
1920 |
1921 syn!(PatRange) => { Pat::Range } // must be before pat_lit
1922 |
1923 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
1924 |
1925 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
1926 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001927 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04001928 |
1929 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
1930 |
1931 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
1932 |
1933 syn!(PatPath) => { Pat::Path }
1934 |
1935 syn!(PatTuple) => { Pat::Tuple }
1936 |
1937 syn!(PatRef) => { Pat::Ref }
1938 |
1939 syn!(PatSlice) => { Pat::Slice }
1940 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001941 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001942
Michael Layzell734adb42017-06-07 16:58:31 -04001943 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001944 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04001945 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001946 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04001947 |u| PatWild { underscore_token: u }
1948 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001949 }
David Tolnay84aa0752016-10-02 23:01:13 -07001950
Michael Layzell734adb42017-06-07 16:58:31 -04001951 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001952 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04001953 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001954 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001955 pat: syn!(Pat) >>
1956 (PatBox {
1957 pat: Box::new(pat),
1958 box_token: boxed,
1959 })
1960 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001961 }
1962
Michael Layzell734adb42017-06-07 16:58:31 -04001963 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001964 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04001965 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001966 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001967 mutability: syn!(Mutability) >>
1968 name: alt!(
1969 syn!(Ident)
1970 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001971 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04001972 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001973 not!(punct!(<)) >>
1974 not!(punct!(::)) >>
1975 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001976 (PatIdent {
1977 mode: match mode {
1978 Some(mode) => BindingMode::ByRef(mode, mutability),
1979 None => BindingMode::ByValue(mutability),
1980 },
1981 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001982 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001983 subpat: subpat.map(|p| Box::new(p.1)),
1984 })
1985 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001986 }
1987
Michael Layzell734adb42017-06-07 16:58:31 -04001988 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001989 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001990 named!(parse -> Self, do_parse!(
1991 path: syn!(Path) >>
1992 tuple: syn!(PatTuple) >>
1993 (PatTupleStruct {
1994 path: path,
1995 pat: tuple,
1996 })
1997 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001998 }
1999
Michael Layzell734adb42017-06-07 16:58:31 -04002000 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002001 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002002 named!(parse -> Self, do_parse!(
2003 path: syn!(Path) >>
2004 data: braces!(do_parse!(
2005 fields: call!(Delimited::parse_terminated) >>
2006 base: option!(
2007 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002008 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002009 ) >>
2010 (fields, base)
2011 )) >>
2012 (PatStruct {
2013 path: path,
2014 fields: (data.0).0,
2015 brace_token: data.1,
2016 dot2_token: (data.0).1.and_then(|m| m),
2017 })
2018 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002019 }
2020
Michael Layzell734adb42017-06-07 16:58:31 -04002021 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002022 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002023 named!(parse -> Self, alt!(
2024 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -07002025 ident: field_ident >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002026 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002027 pat: syn!(Pat) >>
2028 (FieldPat {
2029 ident: ident,
2030 pat: Box::new(pat),
2031 is_shorthand: false,
2032 attrs: Vec::new(),
2033 colon_token: Some(colon),
2034 })
2035 )
2036 |
2037 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002038 boxed: option!(keyword!(box)) >>
2039 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002040 mutability: syn!(Mutability) >>
2041 ident: syn!(Ident) >>
2042 ({
2043 let mut pat: Pat = PatIdent {
2044 mode: if let Some(mode) = mode {
2045 BindingMode::ByRef(mode, mutability)
2046 } else {
2047 BindingMode::ByValue(mutability)
2048 },
2049 ident: ident.clone(),
2050 subpat: None,
2051 at_token: None,
2052 }.into();
2053 if let Some(boxed) = boxed {
2054 pat = PatBox {
2055 pat: Box::new(pat),
2056 box_token: boxed,
2057 }.into();
2058 }
2059 FieldPat {
Alex Crichton954046c2017-05-30 21:49:42 -07002060 ident: ident,
2061 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002062 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002063 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002064 colon_token: None,
2065 }
2066 })
2067 )
2068 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002069 }
2070
Michael Layzell734adb42017-06-07 16:58:31 -04002071 #[cfg(feature = "full")]
David Tolnay570695e2017-06-03 16:15:13 -07002072 named!(field_ident -> Ident, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07002073 syn!(Ident)
2074 |
2075 do_parse!(
2076 lit: syn!(Lit) >>
2077 ({
David Tolnay570695e2017-06-03 16:15:13 -07002078 let s = lit.to_string();
2079 if s.parse::<usize>().is_ok() {
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07002080 Ident::new(Term::intern(&s), lit.span)
Alex Crichton954046c2017-05-30 21:49:42 -07002081 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002082 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002083 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002084 })
2085 )
2086 ));
2087
Michael Layzell734adb42017-06-07 16:58:31 -04002088 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002089 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002090 named!(parse -> Self, map!(
2091 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002092 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002093 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002094 }
David Tolnay9636c052016-10-02 17:11:17 -07002095
Michael Layzell734adb42017-06-07 16:58:31 -04002096 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002097 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002098 named!(parse -> Self, do_parse!(
2099 data: parens!(do_parse!(
2100 elems: call!(Delimited::parse_terminated) >>
2101 dotdot: map!(cond!(
2102 elems.is_empty() || elems.trailing_delim(),
2103 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002104 dots: punct!(..) >>
2105 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002106 (dots, trailing)
2107 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002108 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002109 rest: cond!(match dotdot {
2110 Some((_, Some(_))) => true,
2111 _ => false,
2112 },
2113 call!(Delimited::parse_terminated)) >>
2114 (elems, dotdot, rest)
2115 )) >>
2116 ({
2117 let ((mut elems, dotdot, rest), parens) = data;
2118 let (dotdot, trailing) = match dotdot {
2119 Some((a, b)) => (Some(a), Some(b)),
2120 None => (None, None),
2121 };
2122 PatTuple {
2123 paren_token: parens,
2124 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2125 dot2_token: dotdot,
2126 comma_token: trailing.and_then(|b| b),
2127 pats: {
2128 if let Some(rest) = rest {
2129 for elem in rest {
2130 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002131 }
Michael Layzell92639a52017-06-01 00:07:44 -04002132 }
2133 elems
2134 },
2135 }
2136 })
2137 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002138 }
David Tolnayfbb73232016-10-03 01:00:06 -07002139
Michael Layzell734adb42017-06-07 16:58:31 -04002140 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002141 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002142 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002143 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002144 mutability: syn!(Mutability) >>
2145 pat: syn!(Pat) >>
2146 (PatRef {
2147 pat: Box::new(pat),
2148 mutbl: mutability,
2149 and_token: and,
2150 })
2151 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002152 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002153
Michael Layzell734adb42017-06-07 16:58:31 -04002154 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002155 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002156 named!(parse -> Self, do_parse!(
2157 lit: pat_lit_expr >>
2158 (if let ExprKind::Path(_) = lit.node {
2159 return parse_error(); // these need to be parsed by pat_path
2160 } else {
2161 PatLit {
2162 expr: Box::new(lit),
2163 }
2164 })
2165 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002166 }
David Tolnaye1310902016-10-29 23:40:00 -07002167
Michael Layzell734adb42017-06-07 16:58:31 -04002168 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002169 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002170 named!(parse -> Self, do_parse!(
2171 lo: pat_lit_expr >>
2172 limits: syn!(RangeLimits) >>
2173 hi: pat_lit_expr >>
2174 (PatRange {
2175 lo: Box::new(lo),
2176 hi: Box::new(hi),
2177 limits: limits,
2178 })
2179 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002180 }
David Tolnaye1310902016-10-29 23:40:00 -07002181
Michael Layzell734adb42017-06-07 16:58:31 -04002182 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002183 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002184 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002185 v: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07002186 syn!(Lit) => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002187 |
Alex Crichton954046c2017-05-30 21:49:42 -07002188 syn!(ExprPath) => { ExprKind::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002189 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002190 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07002191 ExprKind::Unary(ExprUnary {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002192 op: UnOp::Neg(<Token![-]>::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07002193 expr: Box::new(v.into())
2194 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002195 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002196 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002197 })
2198 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002199
Michael Layzell734adb42017-06-07 16:58:31 -04002200 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002201 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002202 named!(parse -> Self, map!(
2203 brackets!(do_parse!(
2204 before: call!(Delimited::parse_terminated) >>
2205 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002206 dots: punct!(..) >>
2207 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002208 (dots, trailing)
2209 )) >>
2210 after: cond!(
2211 match middle {
2212 Some((_, ref trailing)) => trailing.is_some(),
2213 _ => false,
2214 },
2215 call!(Delimited::parse_terminated)
2216 ) >>
2217 (before, middle, after)
2218 )),
2219 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002220 let mut before: Delimited<Pat, Token![,]> = before;
2221 let after: Option<Delimited<Pat, Token![,]>> = after;
2222 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002223 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002224 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002225 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002226 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002227 }),
2228 bracket_token: brackets,
2229 middle: middle.and_then(|_| {
2230 if !before.is_empty() && !before.trailing_delim() {
2231 Some(Box::new(before.pop().unwrap().into_item()))
2232 } else {
2233 None
2234 }
2235 }),
2236 front: before,
2237 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002238 }
Alex Crichton954046c2017-05-30 21:49:42 -07002239 }
Michael Layzell92639a52017-06-01 00:07:44 -04002240 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002241 }
David Tolnay435a9a82016-10-29 13:47:20 -07002242
Michael Layzell734adb42017-06-07 16:58:31 -04002243 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002244 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002245 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002246 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002247 |
2248 epsilon!() => { |_| CaptureBy::Ref }
2249 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002250 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002251}
2252
David Tolnayf4bbbd92016-09-23 14:41:55 -07002253#[cfg(feature = "printing")]
2254mod printing {
2255 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002256 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002257 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07002258 use quote::{Tokens, ToTokens};
2259
David Tolnaybcf26022017-12-25 22:10:52 -05002260 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2261 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002262 #[cfg(feature = "full")]
2263 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
2264 if let ExprKind::Struct(_) = e.node {
2265 tokens::Paren::default().surround(tokens, |tokens| {
2266 e.to_tokens(tokens);
2267 });
2268 } else {
2269 e.to_tokens(tokens);
2270 }
2271 }
2272
David Tolnayf4bbbd92016-09-23 14:41:55 -07002273 impl ToTokens for Expr {
Michael Layzell734adb42017-06-07 16:58:31 -04002274 #[cfg(feature = "full")]
David Tolnayf4bbbd92016-09-23 14:41:55 -07002275 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07002276 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002277 self.node.to_tokens(tokens)
2278 }
Michael Layzell734adb42017-06-07 16:58:31 -04002279
2280 #[cfg(not(feature = "full"))]
2281 fn to_tokens(&self, tokens: &mut Tokens) {
2282 self.node.to_tokens(tokens)
2283 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002284 }
2285
Michael Layzell734adb42017-06-07 16:58:31 -04002286 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002287 impl ToTokens for ExprBox {
2288 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002289 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002290 self.expr.to_tokens(tokens);
2291 }
2292 }
2293
Michael Layzell734adb42017-06-07 16:58:31 -04002294 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002295 impl ToTokens for ExprInPlace {
2296 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell6a5a1642017-06-04 19:35:15 -04002297 match self.kind {
2298 InPlaceKind::Arrow(ref arrow) => {
2299 self.place.to_tokens(tokens);
2300 arrow.to_tokens(tokens);
2301 self.value.to_tokens(tokens);
2302 }
2303 InPlaceKind::In(ref _in) => {
2304 _in.to_tokens(tokens);
2305 self.place.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002306 // NOTE: The second operand must be in a block, add one if
2307 // it is not present.
2308 if let ExprKind::Block(_) = self.value.node {
2309 self.value.to_tokens(tokens);
2310 } else {
2311 tokens::Brace::default().surround(tokens, |tokens| {
2312 self.value.to_tokens(tokens);
2313 })
2314 }
Michael Layzell6a5a1642017-06-04 19:35:15 -04002315 }
2316 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002317 }
2318 }
2319
Michael Layzell734adb42017-06-07 16:58:31 -04002320 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002321 impl ToTokens for ExprArray {
2322 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002323 self.bracket_token.surround(tokens, |tokens| {
2324 self.exprs.to_tokens(tokens);
2325 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002326 }
2327 }
2328
2329 impl ToTokens for ExprCall {
2330 fn to_tokens(&self, tokens: &mut Tokens) {
2331 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002332 self.paren_token.surround(tokens, |tokens| {
2333 self.args.to_tokens(tokens);
2334 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002335 }
2336 }
2337
Michael Layzell734adb42017-06-07 16:58:31 -04002338 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002339 impl ToTokens for ExprMethodCall {
2340 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002341 self.expr.to_tokens(tokens);
2342 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002343 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002344 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002345 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2346 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002347 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002348 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002349 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002350 self.paren_token.surround(tokens, |tokens| {
2351 self.args.to_tokens(tokens);
2352 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002353 }
2354 }
2355
Michael Layzell734adb42017-06-07 16:58:31 -04002356 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002357 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002358 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002359 self.paren_token.surround(tokens, |tokens| {
2360 self.args.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002361 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002362 // distinguish ExprTuple from ExprParen.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002363 if self.args.len() == 1 && !self.args.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002364 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002365 }
2366 // XXX: Not sure how to handle this, but we never parse it yet.
2367 // Is this for an expression like (0,)? Can't we use the
2368 // trailing delimiter on Delimited for that? (,) isn't a valid
2369 // expression as far as I know.
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002370 self.lone_comma.to_tokens(tokens);
2371 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002372 }
2373 }
2374
2375 impl ToTokens for ExprBinary {
2376 fn to_tokens(&self, tokens: &mut Tokens) {
2377 self.left.to_tokens(tokens);
2378 self.op.to_tokens(tokens);
2379 self.right.to_tokens(tokens);
2380 }
2381 }
2382
2383 impl ToTokens for ExprUnary {
2384 fn to_tokens(&self, tokens: &mut Tokens) {
2385 self.op.to_tokens(tokens);
2386 self.expr.to_tokens(tokens);
2387 }
2388 }
2389
2390 impl ToTokens for ExprCast {
2391 fn to_tokens(&self, tokens: &mut Tokens) {
2392 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002393 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002394 self.ty.to_tokens(tokens);
2395 }
2396 }
2397
2398 impl ToTokens for ExprType {
2399 fn to_tokens(&self, tokens: &mut Tokens) {
2400 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002401 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002402 self.ty.to_tokens(tokens);
2403 }
2404 }
2405
Michael Layzell734adb42017-06-07 16:58:31 -04002406 #[cfg(feature = "full")]
Michael Layzell3936ceb2017-07-08 00:28:36 -04002407 fn maybe_wrap_else(tokens: &mut Tokens,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002408 else_token: &Option<Token![else]>,
Michael Layzell3936ceb2017-07-08 00:28:36 -04002409 if_false: &Option<Box<Expr>>)
2410 {
2411 if let Some(ref if_false) = *if_false {
Alex Crichton259ee532017-07-14 06:51:02 -07002412 TokensOrDefault(&else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002413
2414 // If we are not one of the valid expressions to exist in an else
2415 // clause, wrap ourselves in a block.
2416 match if_false.node {
2417 ExprKind::If(_) |
2418 ExprKind::IfLet(_) |
2419 ExprKind::Block(_) => {
2420 if_false.to_tokens(tokens);
2421 }
2422 _ => {
2423 tokens::Brace::default().surround(tokens, |tokens| {
2424 if_false.to_tokens(tokens);
2425 });
2426 }
2427 }
2428 }
2429 }
2430
2431 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002432 impl ToTokens for ExprIf {
2433 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002434 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002435 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002436 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002437 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002438 }
2439 }
2440
Michael Layzell734adb42017-06-07 16:58:31 -04002441 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002442 impl ToTokens for ExprIfLet {
2443 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002444 self.if_token.to_tokens(tokens);
2445 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002446 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002447 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002448 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002449 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002450 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002451 }
2452 }
2453
Michael Layzell734adb42017-06-07 16:58:31 -04002454 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002455 impl ToTokens for ExprWhile {
2456 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002457 if self.label.is_some() {
2458 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002459 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002460 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002461 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002462 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002463 self.body.to_tokens(tokens);
2464 }
2465 }
2466
Michael Layzell734adb42017-06-07 16:58:31 -04002467 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002468 impl ToTokens for ExprWhileLet {
2469 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002470 if self.label.is_some() {
2471 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002472 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002473 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002474 self.while_token.to_tokens(tokens);
2475 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002476 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002477 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002478 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002479 self.body.to_tokens(tokens);
2480 }
2481 }
2482
Michael Layzell734adb42017-06-07 16:58:31 -04002483 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002484 impl ToTokens for ExprForLoop {
2485 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002486 if self.label.is_some() {
2487 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002488 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002489 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002490 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002491 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002492 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002493 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002494 self.body.to_tokens(tokens);
2495 }
2496 }
2497
Michael Layzell734adb42017-06-07 16:58:31 -04002498 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002499 impl ToTokens for ExprLoop {
2500 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002501 if self.label.is_some() {
2502 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002503 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002504 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002505 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002506 self.body.to_tokens(tokens);
2507 }
2508 }
2509
Michael Layzell734adb42017-06-07 16:58:31 -04002510 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002511 impl ToTokens for ExprMatch {
2512 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002513 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002514 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002515 self.brace_token.surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002516 for (i, arm) in self.arms.iter().enumerate() {
2517 arm.to_tokens(tokens);
2518 // Ensure that we have a comma after a non-block arm, except
2519 // for the last one.
2520 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002521 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002522 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002523 }
2524 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002525 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002526 }
2527 }
2528
Michael Layzell734adb42017-06-07 16:58:31 -04002529 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002530 impl ToTokens for ExprCatch {
2531 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002532 self.do_token.to_tokens(tokens);
2533 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002534 self.block.to_tokens(tokens);
2535 }
2536 }
2537
Michael Layzell734adb42017-06-07 16:58:31 -04002538 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002539 impl ToTokens for ExprYield {
2540 fn to_tokens(&self, tokens: &mut Tokens) {
2541 self.yield_token.to_tokens(tokens);
2542 self.expr.to_tokens(tokens);
2543 }
2544 }
2545
2546 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002547 impl ToTokens for ExprClosure {
2548 fn to_tokens(&self, tokens: &mut Tokens) {
2549 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002550 self.or1_token.to_tokens(tokens);
2551 for item in self.decl.inputs.iter() {
2552 match **item.item() {
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002553 FnArg::Captured(ArgCaptured { ref pat, ty: Type::Infer(_), .. }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002554 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002555 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002556 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002557 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002558 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002559 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002560 self.or2_token.to_tokens(tokens);
2561 self.decl.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002562 self.body.to_tokens(tokens);
2563 }
2564 }
2565
Michael Layzell734adb42017-06-07 16:58:31 -04002566 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002567 impl ToTokens for ExprUnsafe {
2568 fn to_tokens(&self, tokens: &mut Tokens) {
2569 self.unsafe_token.to_tokens(tokens);
2570 self.block.to_tokens(tokens);
2571 }
2572 }
2573
2574 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002575 impl ToTokens for ExprBlock {
2576 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002577 self.block.to_tokens(tokens);
2578 }
2579 }
2580
Michael Layzell734adb42017-06-07 16:58:31 -04002581 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002582 impl ToTokens for ExprAssign {
2583 fn to_tokens(&self, tokens: &mut Tokens) {
2584 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002585 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002586 self.right.to_tokens(tokens);
2587 }
2588 }
2589
Michael Layzell734adb42017-06-07 16:58:31 -04002590 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002591 impl ToTokens for ExprAssignOp {
2592 fn to_tokens(&self, tokens: &mut Tokens) {
2593 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002594 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002595 self.right.to_tokens(tokens);
2596 }
2597 }
2598
Michael Layzell734adb42017-06-07 16:58:31 -04002599 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002600 impl ToTokens for ExprField {
2601 fn to_tokens(&self, tokens: &mut Tokens) {
2602 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002603 self.dot_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002604 // XXX: I don't think we can do anything if someone shoves a
2605 // nonsense Lit in here.
Alex Crichton62a0a592017-05-22 13:58:53 -07002606 self.field.to_tokens(tokens);
2607 }
2608 }
2609
Michael Layzell734adb42017-06-07 16:58:31 -04002610 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002611 impl ToTokens for ExprTupField {
2612 fn to_tokens(&self, tokens: &mut Tokens) {
2613 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002614 self.dot_token.to_tokens(tokens);
2615 self.field.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002616 }
2617 }
2618
2619 impl ToTokens for ExprIndex {
2620 fn to_tokens(&self, tokens: &mut Tokens) {
2621 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002622 self.bracket_token.surround(tokens, |tokens| {
2623 self.index.to_tokens(tokens);
2624 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002625 }
2626 }
2627
Michael Layzell734adb42017-06-07 16:58:31 -04002628 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002629 impl ToTokens for ExprRange {
2630 fn to_tokens(&self, tokens: &mut Tokens) {
2631 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002632 match self.limits {
2633 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2634 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2635 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002636 self.to.to_tokens(tokens);
2637 }
2638 }
2639
2640 impl ToTokens for ExprPath {
2641 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002642 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002643 }
2644 }
2645
Michael Layzell734adb42017-06-07 16:58:31 -04002646 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002647 impl ToTokens for ExprAddrOf {
2648 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002649 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002650 self.mutbl.to_tokens(tokens);
2651 self.expr.to_tokens(tokens);
2652 }
2653 }
2654
Michael Layzell734adb42017-06-07 16:58:31 -04002655 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002656 impl ToTokens for ExprBreak {
2657 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002658 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002659 self.label.to_tokens(tokens);
2660 self.expr.to_tokens(tokens);
2661 }
2662 }
2663
Michael Layzell734adb42017-06-07 16:58:31 -04002664 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002665 impl ToTokens for ExprContinue {
2666 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002667 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002668 self.label.to_tokens(tokens);
2669 }
2670 }
2671
Michael Layzell734adb42017-06-07 16:58:31 -04002672 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002673 impl ToTokens for ExprRet {
2674 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002675 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002676 self.expr.to_tokens(tokens);
2677 }
2678 }
2679
Michael Layzell734adb42017-06-07 16:58:31 -04002680 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002681 impl ToTokens for ExprStruct {
2682 fn to_tokens(&self, tokens: &mut Tokens) {
2683 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002684 self.brace_token.surround(tokens, |tokens| {
2685 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002686 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002687 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002688 self.rest.to_tokens(tokens);
2689 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002690 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002691 }
2692 }
2693
Michael Layzell734adb42017-06-07 16:58:31 -04002694 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002695 impl ToTokens for ExprRepeat {
2696 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002697 self.bracket_token.surround(tokens, |tokens| {
2698 self.expr.to_tokens(tokens);
2699 self.semi_token.to_tokens(tokens);
2700 self.amt.to_tokens(tokens);
2701 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002702 }
2703 }
2704
Michael Layzell93c36282017-06-04 20:43:14 -04002705 impl ToTokens for ExprGroup {
2706 fn to_tokens(&self, tokens: &mut Tokens) {
2707 self.group_token.surround(tokens, |tokens| {
2708 self.expr.to_tokens(tokens);
2709 });
2710 }
2711 }
2712
Alex Crichton62a0a592017-05-22 13:58:53 -07002713 impl ToTokens for ExprParen {
2714 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002715 self.paren_token.surround(tokens, |tokens| {
2716 self.expr.to_tokens(tokens);
2717 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002718 }
2719 }
2720
Michael Layzell734adb42017-06-07 16:58:31 -04002721 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002722 impl ToTokens for ExprTry {
2723 fn to_tokens(&self, tokens: &mut Tokens) {
2724 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002725 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002726 }
2727 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002728
Michael Layzell734adb42017-06-07 16:58:31 -04002729 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002730 impl ToTokens for FieldValue {
2731 fn to_tokens(&self, tokens: &mut Tokens) {
2732 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002733 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2734 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002735 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002736 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002737 self.expr.to_tokens(tokens);
2738 }
David Tolnay055a7042016-10-02 19:23:54 -07002739 }
2740 }
2741
Michael Layzell734adb42017-06-07 16:58:31 -04002742 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002743 impl ToTokens for Arm {
2744 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002745 tokens.append_all(&self.attrs);
2746 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002747 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002748 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002749 self.guard.to_tokens(tokens);
2750 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002751 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002752 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002753 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002754 }
2755 }
2756
Michael Layzell734adb42017-06-07 16:58:31 -04002757 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002758 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002759 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002760 self.underscore_token.to_tokens(tokens);
2761 }
2762 }
2763
Michael Layzell734adb42017-06-07 16:58:31 -04002764 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002765 impl ToTokens for PatIdent {
2766 fn to_tokens(&self, tokens: &mut Tokens) {
2767 self.mode.to_tokens(tokens);
2768 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002769 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002770 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002771 self.subpat.to_tokens(tokens);
2772 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002773 }
2774 }
2775
Michael Layzell734adb42017-06-07 16:58:31 -04002776 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002777 impl ToTokens for PatStruct {
2778 fn to_tokens(&self, tokens: &mut Tokens) {
2779 self.path.to_tokens(tokens);
2780 self.brace_token.surround(tokens, |tokens| {
2781 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002782 // NOTE: We need a comma before the dot2 token if it is present.
2783 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002784 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002785 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002786 self.dot2_token.to_tokens(tokens);
2787 });
2788 }
2789 }
2790
Michael Layzell734adb42017-06-07 16:58:31 -04002791 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002792 impl ToTokens for PatTupleStruct {
2793 fn to_tokens(&self, tokens: &mut Tokens) {
2794 self.path.to_tokens(tokens);
2795 self.pat.to_tokens(tokens);
2796 }
2797 }
2798
Michael Layzell734adb42017-06-07 16:58:31 -04002799 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002800 impl ToTokens for PatPath {
2801 fn to_tokens(&self, tokens: &mut Tokens) {
2802 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2803 }
2804 }
2805
Michael Layzell734adb42017-06-07 16:58:31 -04002806 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002807 impl ToTokens for PatTuple {
2808 fn to_tokens(&self, tokens: &mut Tokens) {
2809 self.paren_token.surround(tokens, |tokens| {
2810 for (i, token) in self.pats.iter().enumerate() {
2811 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07002812 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
2813 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002814 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002815 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002816 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002817
2818 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002819 // Ensure there is a comma before the .. token.
2820 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002821 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002822 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002823 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002824 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002825 });
2826 }
2827 }
2828
Michael Layzell734adb42017-06-07 16:58:31 -04002829 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002830 impl ToTokens for PatBox {
2831 fn to_tokens(&self, tokens: &mut Tokens) {
2832 self.box_token.to_tokens(tokens);
2833 self.pat.to_tokens(tokens);
2834 }
2835 }
2836
Michael Layzell734adb42017-06-07 16:58:31 -04002837 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002838 impl ToTokens for PatRef {
2839 fn to_tokens(&self, tokens: &mut Tokens) {
2840 self.and_token.to_tokens(tokens);
2841 self.mutbl.to_tokens(tokens);
2842 self.pat.to_tokens(tokens);
2843 }
2844 }
2845
Michael Layzell734adb42017-06-07 16:58:31 -04002846 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002847 impl ToTokens for PatLit {
2848 fn to_tokens(&self, tokens: &mut Tokens) {
2849 self.expr.to_tokens(tokens);
2850 }
2851 }
2852
Michael Layzell734adb42017-06-07 16:58:31 -04002853 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002854 impl ToTokens for PatRange {
2855 fn to_tokens(&self, tokens: &mut Tokens) {
2856 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002857 match self.limits {
2858 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2859 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
2860 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002861 self.hi.to_tokens(tokens);
2862 }
2863 }
2864
Michael Layzell734adb42017-06-07 16:58:31 -04002865 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002866 impl ToTokens for PatSlice {
2867 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002868 // XXX: This is a mess, and it will be so easy to screw it up. How
2869 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002870 self.bracket_token.surround(tokens, |tokens| {
2871 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002872
2873 // If we need a comma before the middle or standalone .. token,
2874 // then make sure it's present.
2875 if !self.front.empty_or_trailing() &&
2876 (self.middle.is_some() || self.dot2_token.is_some())
2877 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002878 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002879 }
2880
2881 // If we have an identifier, we always need a .. token.
2882 if self.middle.is_some() {
2883 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002884 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002885 } else if self.dot2_token.is_some() {
2886 self.dot2_token.to_tokens(tokens);
2887 }
2888
2889 // Make sure we have a comma before the back half.
2890 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002891 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002892 self.back.to_tokens(tokens);
2893 } else {
2894 self.comma_token.to_tokens(tokens);
2895 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002896 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002897 }
2898 }
2899
Michael Layzell734adb42017-06-07 16:58:31 -04002900 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07002901 impl ToTokens for FieldPat {
2902 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002903 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07002904 if !self.is_shorthand {
2905 self.ident.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002906 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002907 }
2908 self.pat.to_tokens(tokens);
2909 }
2910 }
2911
Michael Layzell734adb42017-06-07 16:58:31 -04002912 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002913 impl ToTokens for BindingMode {
2914 fn to_tokens(&self, tokens: &mut Tokens) {
2915 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002916 BindingMode::ByRef(ref t, ref m) => {
2917 t.to_tokens(tokens);
2918 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002919 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002920 BindingMode::ByValue(ref m) => {
2921 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002922 }
2923 }
2924 }
2925 }
David Tolnay42602292016-10-01 22:25:45 -07002926
Michael Layzell734adb42017-06-07 16:58:31 -04002927 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07002928 impl ToTokens for CaptureBy {
2929 fn to_tokens(&self, tokens: &mut Tokens) {
2930 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002931 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002932 CaptureBy::Ref => {
2933 // nothing
2934 }
David Tolnay89e05672016-10-02 14:39:42 -07002935 }
2936 }
2937 }
2938
Michael Layzell734adb42017-06-07 16:58:31 -04002939 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07002940 impl ToTokens for Block {
2941 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002942 self.brace_token.surround(tokens, |tokens| {
2943 tokens.append_all(&self.stmts);
2944 });
David Tolnay42602292016-10-01 22:25:45 -07002945 }
2946 }
2947
Michael Layzell734adb42017-06-07 16:58:31 -04002948 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07002949 impl ToTokens for Stmt {
2950 fn to_tokens(&self, tokens: &mut Tokens) {
2951 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002952 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002953 Stmt::Item(ref item) => item.to_tokens(tokens),
2954 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002955 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002956 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002957 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002958 }
David Tolnaydecf28d2017-11-11 11:56:45 -08002959 Stmt::Macro(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002960 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002961 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002962 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07002963 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002964 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002965 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2966 // no semicolon
2967 }
David Tolnay13b3d352016-10-03 00:31:15 -07002968 }
2969 }
David Tolnay42602292016-10-01 22:25:45 -07002970 }
2971 }
2972 }
David Tolnay191e0582016-10-02 18:31:09 -07002973
Michael Layzell734adb42017-06-07 16:58:31 -04002974 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002975 impl ToTokens for Local {
2976 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002977 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002978 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002979 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002980 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002981 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002982 self.ty.to_tokens(tokens);
2983 }
2984 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002985 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002986 self.init.to_tokens(tokens);
2987 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002988 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002989 }
2990 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002991}