blob: 39ab6c79fb39515eebe8a0944c7ab124cf6e7cf1 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
Alex Crichton62a0a592017-05-22 13:58:53 -07003ast_struct! {
4 /// An expression.
5 pub struct Expr {
6 /// Type of the expression.
7 pub node: ExprKind,
Clar Charrd22b5702017-03-10 15:24:56 -05008
Alex Crichton62a0a592017-05-22 13:58:53 -07009 /// Attributes tagged on the expression.
10 pub attrs: Vec<Attribute>,
11 }
David Tolnay7184b132016-10-30 10:06:37 -070012}
13
14impl From<ExprKind> for Expr {
15 fn from(node: ExprKind) -> Expr {
16 Expr {
17 node: node,
18 attrs: Vec::new(),
19 }
20 }
21}
22
Alex Crichton62a0a592017-05-22 13:58:53 -070023ast_enum_of_structs! {
24 pub enum ExprKind {
25 /// A `box x` expression.
26 pub Box(ExprBox {
27 pub expr: Box<Expr>,
28 }),
Clar Charrd22b5702017-03-10 15:24:56 -050029
Alex Crichton62a0a592017-05-22 13:58:53 -070030 /// E.g. 'place <- val'.
31 pub InPlace(ExprInPlace {
32 pub place: Box<Expr>,
33 pub value: Box<Expr>,
34 }),
Clar Charrd22b5702017-03-10 15:24:56 -050035
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// An array, e.g. `[a, b, c, d]`.
37 pub Array(ExprArray {
38 pub exprs: Vec<Expr>,
39 }),
Clar Charrd22b5702017-03-10 15:24:56 -050040
Alex Crichton62a0a592017-05-22 13:58:53 -070041 /// A function call.
42 pub Call(ExprCall {
43 pub func: Box<Expr>,
44 pub args: Vec<Expr>,
45 }),
Clar Charrd22b5702017-03-10 15:24:56 -050046
Alex Crichton62a0a592017-05-22 13:58:53 -070047 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
48 ///
49 /// The `Ident` is the identifier for the method name.
50 /// The vector of `Ty`s are the ascripted type parameters for the method
51 /// (within the angle brackets).
52 ///
53 /// The first element of the vector of `Expr`s is the expression that evaluates
54 /// to the object on which the method is being called on (the receiver),
55 /// and the remaining elements are the rest of the arguments.
56 ///
57 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
58 /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
59 pub MethodCall(ExprMethodCall {
60 pub method: Ident,
61 pub typarams: Vec<Ty>,
62 pub args: Vec<Expr>,
63 }),
Clar Charrd22b5702017-03-10 15:24:56 -050064
Alex Crichton62a0a592017-05-22 13:58:53 -070065 /// A tuple, e.g. `(a, b, c, d)`.
66 pub Tup(ExprTup {
67 pub args: Vec<Expr>,
68 }),
Clar Charrd22b5702017-03-10 15:24:56 -050069
Alex Crichton62a0a592017-05-22 13:58:53 -070070 /// A binary operation, e.g. `a + b`, `a * b`.
71 pub Binary(ExprBinary {
72 pub op: BinOp,
73 pub left: Box<Expr>,
74 pub right: Box<Expr>,
75 }),
Clar Charrd22b5702017-03-10 15:24:56 -050076
Alex Crichton62a0a592017-05-22 13:58:53 -070077 /// A unary operation, e.g. `!x`, `*x`.
78 pub Unary(ExprUnary {
79 pub op: UnOp,
80 pub expr: Box<Expr>,
81 }),
Clar Charrd22b5702017-03-10 15:24:56 -050082
Alex Crichton62a0a592017-05-22 13:58:53 -070083 /// A literal, e.g. `1`, `"foo"`.
84 pub Lit(Lit),
Clar Charrd22b5702017-03-10 15:24:56 -050085
Alex Crichton62a0a592017-05-22 13:58:53 -070086 /// A cast, e.g. `foo as f64`.
87 pub Cast(ExprCast {
88 pub expr: Box<Expr>,
89 pub ty: Box<Ty>,
90 }),
Clar Charrd22b5702017-03-10 15:24:56 -050091
Alex Crichton62a0a592017-05-22 13:58:53 -070092 /// A type ascription, e.g. `foo: f64`.
93 pub Type(ExprType {
94 pub expr: Box<Expr>,
95 pub ty: Box<Ty>,
96 }),
Clar Charrd22b5702017-03-10 15:24:56 -050097
Alex Crichton62a0a592017-05-22 13:58:53 -070098 /// An `if` block, with an optional else block
99 ///
100 /// E.g., `if expr { block } else { expr }`
101 pub If(ExprIf {
102 pub cond: Box<Expr>,
103 pub if_true: Block,
104 pub if_false: Option<Box<Expr>>,
105 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500106
Alex Crichton62a0a592017-05-22 13:58:53 -0700107 /// An `if let` expression with an optional else block
108 ///
109 /// E.g., `if let pat = expr { block } else { expr }`
110 ///
111 /// This is desugared to a `match` expression.
112 pub IfLet(ExprIfLet {
113 pub pat: Box<Pat>,
114 pub expr: Box<Expr>,
115 pub if_true: Block,
116 pub if_false: Option<Box<Expr>>,
117 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500118
Alex Crichton62a0a592017-05-22 13:58:53 -0700119 /// A while loop, with an optional label
120 ///
121 /// E.g., `'label: while expr { block }`
122 pub While(ExprWhile {
123 pub cond: Box<Expr>,
124 pub body: Block,
125 pub label: Option<Ident>,
126 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500127
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 /// A while-let loop, with an optional label.
129 ///
130 /// E.g., `'label: while let pat = expr { block }`
131 ///
132 /// This is desugared to a combination of `loop` and `match` expressions.
133 pub WhileLet(ExprWhileLet {
134 pub pat: Box<Pat>,
135 pub expr: Box<Expr>,
136 pub body: Block,
137 pub label: Option<Ident>,
138 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500139
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 /// A for loop, with an optional label.
141 ///
142 /// E.g., `'label: for pat in expr { block }`
143 ///
144 /// This is desugared to a combination of `loop` and `match` expressions.
145 pub ForLoop(ExprForLoop {
146 pub pat: Box<Pat>,
147 pub expr: Box<Expr>,
148 pub body: Block,
149 pub label: Option<Ident>,
150 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500151
Alex Crichton62a0a592017-05-22 13:58:53 -0700152 /// Conditionless loop with an optional label.
153 ///
154 /// E.g. `'label: loop { block }`
155 pub Loop(ExprLoop {
156 pub body: Block,
157 pub label: Option<Ident>,
158 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500159
Alex Crichton62a0a592017-05-22 13:58:53 -0700160 /// A `match` block.
161 pub Match(ExprMatch {
162 pub expr: Box<Expr>,
163 pub arms: Vec<Arm>,
164 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500165
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 /// A closure (for example, `move |a, b, c| a + b + c`)
167 pub Closure(ExprClosure {
168 pub capture: CaptureBy,
169 pub decl: Box<FnDecl>,
170 pub body: Box<Expr>,
171 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500172
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 /// A block (`{ ... }` or `unsafe { ... }`)
174 pub Block(ExprBlock {
175 pub unsafety: Unsafety,
176 pub block: Block,
177 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700178
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 /// An assignment (`a = foo()`)
180 pub Assign(ExprAssign {
181 pub left: Box<Expr>,
182 pub right: Box<Expr>,
183 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500184
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 /// An assignment with an operator
186 ///
187 /// For example, `a += 1`.
188 pub AssignOp(ExprAssignOp {
189 pub op: BinOp,
190 pub left: Box<Expr>,
191 pub right: Box<Expr>,
192 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500193
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 /// Access of a named struct field (`obj.foo`)
195 pub Field(ExprField {
196 pub expr: Box<Expr>,
197 pub field: Ident,
198 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500199
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 /// Access of an unnamed field of a struct or tuple-struct
201 ///
202 /// For example, `foo.0`.
203 pub TupField(ExprTupField {
204 pub expr: Box<Expr>,
205 pub field: usize,
206 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500207
Alex Crichton62a0a592017-05-22 13:58:53 -0700208 /// An indexing operation (`foo[2]`)
209 pub Index(ExprIndex {
210 pub expr: Box<Expr>,
211 pub index: Box<Expr>,
212 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500213
Alex Crichton62a0a592017-05-22 13:58:53 -0700214 /// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`)
215 pub Range(ExprRange {
216 pub from: Option<Box<Expr>>,
217 pub to: Option<Box<Expr>>,
218 pub limits: RangeLimits,
219 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700220
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 /// Variable reference, possibly containing `::` and/or type
222 /// parameters, e.g. foo::bar::<baz>.
223 ///
224 /// Optionally "qualified",
225 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
226 pub Path(ExprPath {
227 pub qself: Option<QSelf>,
228 pub path: Path,
229 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700230
Alex Crichton62a0a592017-05-22 13:58:53 -0700231 /// A referencing operation (`&a` or `&mut a`)
232 pub AddrOf(ExprAddrOf {
233 pub mutbl: Mutability,
234 pub expr: Box<Expr>,
235 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500236
Alex Crichton62a0a592017-05-22 13:58:53 -0700237 /// A `break`, with an optional label to break, and an optional expression
238 pub Break(ExprBreak {
239 pub label: Option<Ident>,
240 pub expr: Option<Box<Expr>>,
241 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500242
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 /// A `continue`, with an optional label
244 pub Continue(ExprContinue {
245 pub label: Option<Ident>,
246 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500247
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 /// A `return`, with an optional value to be returned
249 pub Ret(ExprRet {
250 pub expr: Option<Box<Expr>>,
251 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700252
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 /// A macro invocation; pre-expansion
254 pub Mac(Mac),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700255
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 /// A struct literal expression.
257 ///
258 /// For example, `Foo {x: 1, y: 2}`, or
259 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
260 pub Struct(ExprStruct {
261 pub path: Path,
262 pub fields: Vec<FieldValue>,
263 pub rest: Option<Box<Expr>>,
264 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700265
Alex Crichton62a0a592017-05-22 13:58:53 -0700266 /// An array literal constructed from one repeated element.
267 ///
268 /// For example, `[1; 5]`. The first expression is the element
269 /// to be repeated; the second is the number of times to repeat it.
270 pub Repeat(ExprRepeat {
271 pub expr: Box<Expr>,
272 pub amt: Box<Expr>,
273 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700274
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 /// No-op: used solely so we can pretty-print faithfully
276 pub Paren(ExprParen {
277 pub expr: Box<Expr>,
278 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700279
Alex Crichton62a0a592017-05-22 13:58:53 -0700280 /// `expr?`
281 pub Try(ExprTry {
282 pub expr: Box<Expr>,
283 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700284
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 /// A catch expression.
286 ///
287 /// E.g. `do catch { block }`
288 pub Catch(ExprCatch {
289 pub block: Block,
290 }),
291 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700292}
293
Alex Crichton62a0a592017-05-22 13:58:53 -0700294ast_struct! {
295 /// A field-value pair in a struct literal.
296 pub struct FieldValue {
297 /// Name of the field.
298 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -0500299
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 /// Value of the field.
301 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500302
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 /// Whether this is a shorthand field, e.g. `Struct { x }`
304 /// instead of `Struct { x: x }`.
305 pub is_shorthand: bool,
Clar Charrd22b5702017-03-10 15:24:56 -0500306
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 /// Attributes tagged on the field.
308 pub attrs: Vec<Attribute>,
309 }
David Tolnay055a7042016-10-02 19:23:54 -0700310}
311
Alex Crichton62a0a592017-05-22 13:58:53 -0700312ast_struct! {
313 /// A Block (`{ .. }`).
314 ///
315 /// E.g. `{ .. }` as in `fn foo() { .. }`
316 pub struct Block {
317 /// Statements in a block
318 pub stmts: Vec<Stmt>,
319 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700320}
321
Alex Crichton62a0a592017-05-22 13:58:53 -0700322ast_enum! {
323 /// A statement, usually ending in a semicolon.
324 pub enum Stmt {
325 /// A local (let) binding.
326 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700327
Alex Crichton62a0a592017-05-22 13:58:53 -0700328 /// An item definition.
329 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700330
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 /// Expr without trailing semicolon.
332 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700333
Alex Crichton62a0a592017-05-22 13:58:53 -0700334 /// Expression with trailing semicolon;
335 Semi(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700336
Alex Crichton62a0a592017-05-22 13:58:53 -0700337 /// Macro invocation.
338 Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>),
339 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700340}
341
Alex Crichton62a0a592017-05-22 13:58:53 -0700342ast_enum! {
343 /// How a macro was invoked.
344 #[derive(Copy)]
345 pub enum MacStmtStyle {
346 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
347 /// `foo!(...);`, `foo![...];`
348 Semicolon,
Clar Charrd22b5702017-03-10 15:24:56 -0500349
Alex Crichton62a0a592017-05-22 13:58:53 -0700350 /// The macro statement had braces; e.g. foo! { ... }
351 Braces,
Clar Charrd22b5702017-03-10 15:24:56 -0500352
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 /// The macro statement had parentheses or brackets and no semicolon; e.g.
354 /// `foo!(...)`. All of these will end up being converted into macro
355 /// expressions.
356 NoBraces,
357 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700358}
359
Alex Crichton62a0a592017-05-22 13:58:53 -0700360ast_struct! {
361 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
362 pub struct Local {
363 pub pat: Box<Pat>,
364 pub ty: Option<Box<Ty>>,
Clar Charrd22b5702017-03-10 15:24:56 -0500365
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 /// Initializer expression to set the value, if any
367 pub init: Option<Box<Expr>>,
368 pub attrs: Vec<Attribute>,
369 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700370}
371
Alex Crichton62a0a592017-05-22 13:58:53 -0700372ast_enum! {
373 // Clippy false positive
374 // https://github.com/Manishearth/rust-clippy/issues/1241
375 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
376 pub enum Pat {
377 /// Represents a wildcard pattern (`_`)
378 Wild,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700379
Alex Crichton62a0a592017-05-22 13:58:53 -0700380 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
381 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
382 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
383 /// during name resolution.
384 Ident(BindingMode, Ident, Option<Box<Pat>>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700385
Alex Crichton62a0a592017-05-22 13:58:53 -0700386 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
387 /// The `bool` is `true` in the presence of a `..`.
388 Struct(Path, Vec<FieldPat>, bool),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700389
Alex Crichton62a0a592017-05-22 13:58:53 -0700390 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
391 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
392 /// 0 <= position <= subpats.len()
393 TupleStruct(Path, Vec<Pat>, Option<usize>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700394
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 /// A possibly qualified path pattern.
396 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
397 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
398 /// only legally refer to associated constants.
399 Path(Option<QSelf>, Path),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 /// A tuple pattern `(a, b)`.
402 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
403 /// 0 <= position <= subpats.len()
404 Tuple(Vec<Pat>, Option<usize>),
405 /// A `box` pattern
406 Box(Box<Pat>),
407 /// A reference pattern, e.g. `&mut (a, b)`
408 Ref(Box<Pat>, Mutability),
409 /// A literal
410 Lit(Box<Expr>),
411 /// A range pattern, e.g. `1...2`
412 Range(Box<Expr>, Box<Expr>, RangeLimits),
413 /// `[a, b, ..i, y, z]` is represented as:
414 /// `Pat::Slice(box [a, b], Some(i), box [y, z])`
415 Slice(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>),
416 /// A macro pattern; pre-expansion
417 Mac(Mac),
418 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700419}
420
Alex Crichton62a0a592017-05-22 13:58:53 -0700421ast_struct! {
422 /// An arm of a 'match'.
423 ///
424 /// E.g. `0...10 => { println!("match!") }` as in
425 ///
426 /// ```rust,ignore
427 /// match n {
428 /// 0...10 => { println!("match!") },
429 /// // ..
430 /// }
431 /// ```
432 pub struct Arm {
433 pub attrs: Vec<Attribute>,
434 pub pats: Vec<Pat>,
435 pub guard: Option<Box<Expr>>,
436 pub body: Box<Expr>,
437 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700438}
439
Alex Crichton62a0a592017-05-22 13:58:53 -0700440ast_enum! {
441 /// A capture clause
442 #[derive(Copy)]
443 pub enum CaptureBy {
444 Value,
445 Ref,
446 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700447}
448
Alex Crichton62a0a592017-05-22 13:58:53 -0700449ast_enum! {
450 /// Limit types of a range (inclusive or exclusive)
451 #[derive(Copy)]
452 pub enum RangeLimits {
453 /// Inclusive at the beginning, exclusive at the end
454 HalfOpen,
455 /// Inclusive at the beginning and end
456 Closed,
457 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700458}
459
Alex Crichton62a0a592017-05-22 13:58:53 -0700460ast_struct! {
461 /// A single field in a struct pattern
462 ///
463 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
464 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
465 /// except `is_shorthand` is true
466 pub struct FieldPat {
467 /// The identifier for the field
468 pub ident: Ident,
469 /// The pattern the field is destructured to
470 pub pat: Box<Pat>,
471 pub is_shorthand: bool,
472 pub attrs: Vec<Attribute>,
473 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700474}
475
Alex Crichton62a0a592017-05-22 13:58:53 -0700476ast_enum! {
477 #[derive(Copy)]
478 pub enum BindingMode {
479 ByRef(Mutability),
480 ByValue(Mutability),
481 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700482}
483
David Tolnayb9c8e322016-09-23 20:48:37 -0700484#[cfg(feature = "parsing")]
485pub mod parsing {
486 use super::*;
David Tolnayeea28d62016-10-25 20:44:08 -0700487 use {BinOp, Delimited, DelimToken, FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Mac,
Alex Crichton62a0a592017-05-22 13:58:53 -0700488 TokenTree, Ty, UnOp, Unsafety, ArgCaptured, TyInfer};
David Tolnayb4ad3b52016-10-01 21:58:13 -0700489 use attr::parsing::outer_attr;
Gregory Katz1b69f682016-09-27 21:06:09 -0400490 use generics::parsing::lifetime;
David Tolnay39039442016-10-30 11:25:21 -0700491 use ident::parsing::{ident, wordlike};
David Tolnay191e0582016-10-02 18:31:09 -0700492 use item::parsing::item;
David Tolnay438c9052016-10-07 23:24:48 -0700493 use lit::parsing::{digits, lit};
David Tolnayeea28d62016-10-25 20:44:08 -0700494 use mac::parsing::{mac, token_trees};
David Tolnay5fe14fc2017-01-27 16:22:08 -0800495 use synom::IResult::{self, Error};
David Tolnay438c9052016-10-07 23:24:48 -0700496 use op::parsing::{assign_op, binop, unop};
David Tolnay7b035912016-12-21 22:42:07 -0500497 use ty::parsing::{mutability, path, qpath, ty, unsafety};
David Tolnayb9c8e322016-09-23 20:48:37 -0700498
David Tolnayaf2557e2016-10-24 11:52:21 -0700499 // Struct literals are ambiguous in certain positions
500 // https://github.com/rust-lang/rfcs/pull/92
501 macro_rules! named_ambiguous_expr {
502 ($name:ident -> $o:ty, $allow_struct:ident, $submac:ident!( $($args:tt)* )) => {
David Tolnay5fe14fc2017-01-27 16:22:08 -0800503 fn $name(i: &str, $allow_struct: bool) -> $crate::synom::IResult<&str, $o> {
David Tolnayaf2557e2016-10-24 11:52:21 -0700504 $submac!(i, $($args)*)
505 }
506 };
507 }
508
509 macro_rules! ambiguous_expr {
510 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700511 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700512 };
513 }
514
515 named!(pub expr -> Expr, ambiguous_expr!(true));
516
517 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
518
David Tolnay02a8d472017-02-19 12:59:44 -0800519 #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
David Tolnay54e854d2016-10-24 12:03:30 -0700520 fn ambiguous_expr(i: &str, allow_struct: bool, allow_block: bool) -> IResult<&str, Expr> {
521 do_parse!(
522 i,
523 mut e: alt!(
524 expr_lit // must be before expr_struct
525 |
526 cond_reduce!(allow_struct, expr_struct) // must be before expr_path
527 |
528 expr_paren // must be before expr_tup
529 |
530 expr_mac // must be before expr_path
531 |
David Tolnay5d55ef72016-12-21 20:20:04 -0500532 call!(expr_break, allow_struct) // must be before expr_path
David Tolnay54e854d2016-10-24 12:03:30 -0700533 |
534 expr_continue // must be before expr_path
535 |
536 call!(expr_ret, allow_struct) // must be before expr_path
537 |
538 call!(expr_box, allow_struct)
539 |
David Tolnay6696c3e2016-10-30 11:45:10 -0700540 expr_in_place
541 |
David Tolnay9c7ab132017-01-23 00:01:22 -0800542 expr_array
David Tolnay54e854d2016-10-24 12:03:30 -0700543 |
544 expr_tup
545 |
546 call!(expr_unary, allow_struct)
547 |
548 expr_if
549 |
550 expr_while
551 |
552 expr_for_loop
553 |
554 expr_loop
555 |
556 expr_match
557 |
Arnavion02ef13f2017-04-25 00:54:31 -0700558 expr_catch
559 |
David Tolnay54e854d2016-10-24 12:03:30 -0700560 call!(expr_closure, allow_struct)
561 |
562 cond_reduce!(allow_block, expr_block)
563 |
564 call!(expr_range, allow_struct)
565 |
566 expr_path
567 |
568 call!(expr_addr_of, allow_struct)
569 |
570 expr_repeat
571 ) >>
572 many0!(alt!(
573 tap!(args: and_call => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700574 e = ExprCall {
575 func: Box::new(e.into()),
576 args: args,
577 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700578 })
579 |
580 tap!(more: and_method_call => {
581 let (method, ascript, mut args) = more;
David Tolnay7184b132016-10-30 10:06:37 -0700582 args.insert(0, e.into());
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 e = ExprMethodCall {
584 method: method,
585 typarams: ascript,
586 args: args,
587 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700588 })
589 |
590 tap!(more: call!(and_binary, allow_struct) => {
591 let (op, other) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700592 e = ExprBinary {
593 op: op,
594 left: Box::new(e.into()),
595 right: Box::new(other),
596 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700597 })
598 |
599 tap!(ty: and_cast => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700600 e = ExprCast {
601 expr: Box::new(e.into()),
602 ty: Box::new(ty),
603 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700604 })
605 |
606 tap!(ty: and_ascription => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700607 e = ExprType {
608 expr: Box::new(e.into()),
609 ty: Box::new(ty),
610 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700611 })
612 |
613 tap!(v: call!(and_assign, allow_struct) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700614 e = ExprAssign {
615 left: Box::new(e.into()),
616 right: Box::new(v),
617 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700618 })
619 |
620 tap!(more: call!(and_assign_op, allow_struct) => {
621 let (op, v) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700622 e = ExprAssignOp {
623 op: op,
624 left: Box::new(e.into()),
625 right: Box::new(v),
626 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700627 })
628 |
629 tap!(field: and_field => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700630 e = ExprField {
631 expr: Box::new(e.into()),
632 field: field,
633 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700634 })
635 |
636 tap!(field: and_tup_field => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700637 e = ExprTupField {
638 expr: Box::new(e.into()),
639 field: field as usize,
640 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700641 })
642 |
643 tap!(i: and_index => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 e = ExprIndex {
645 expr: Box::new(e.into()),
646 index: Box::new(i),
647 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700648 })
649 |
650 tap!(more: call!(and_range, allow_struct) => {
651 let (limits, hi) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 e = ExprRange {
653 from: Some(Box::new(e.into())),
654 to: hi.map(Box::new),
655 limits: limits,
656 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700657 })
658 |
659 tap!(_try: punct!("?") => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700660 e = ExprTry { expr: Box::new(e.into()) }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700661 })
662 )) >>
David Tolnay7184b132016-10-30 10:06:37 -0700663 (e.into())
David Tolnay54e854d2016-10-24 12:03:30 -0700664 )
665 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700666
David Tolnay7184b132016-10-30 10:06:37 -0700667 named!(expr_mac -> ExprKind, map!(mac, ExprKind::Mac));
David Tolnay84aa0752016-10-02 23:01:13 -0700668
David Tolnay7184b132016-10-30 10:06:37 -0700669 named!(expr_paren -> ExprKind, do_parse!(
David Tolnay89e05672016-10-02 14:39:42 -0700670 punct!("(") >>
671 e: expr >>
672 punct!(")") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700673 (ExprParen { expr: Box::new(e) }.into())
David Tolnay89e05672016-10-02 14:39:42 -0700674 ));
675
David Tolnay7184b132016-10-30 10:06:37 -0700676 named_ambiguous_expr!(expr_box -> ExprKind, allow_struct, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700677 keyword!("box") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700678 inner: ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700679 (ExprBox { expr: Box::new(inner) }.into())
David Tolnayb9c8e322016-09-23 20:48:37 -0700680 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700681
David Tolnay6696c3e2016-10-30 11:45:10 -0700682 named!(expr_in_place -> ExprKind, do_parse!(
683 keyword!("in") >>
684 place: expr_no_struct >>
685 punct!("{") >>
686 value: within_block >>
687 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 (ExprInPlace {
689 place: Box::new(place),
690 value: Box::new(Expr {
691 node: ExprBlock {
692 unsafety: Unsafety::Normal,
693 block: Block { stmts: value, },
694 }.into(),
695 attrs: Vec::new(),
696 }),
697 }.into())
David Tolnay6696c3e2016-10-30 11:45:10 -0700698 ));
699
David Tolnay9c7ab132017-01-23 00:01:22 -0800700 named!(expr_array -> ExprKind, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700701 punct!("[") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700702 elems: terminated_list!(punct!(","), expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700703 punct!("]") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 (ExprArray { exprs: elems }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700705 ));
706
David Tolnay939766a2016-09-23 23:48:12 -0700707 named!(and_call -> Vec<Expr>, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700708 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700709 args: terminated_list!(punct!(","), expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700710 punct!(")") >>
711 (args)
712 ));
713
David Tolnay939766a2016-09-23 23:48:12 -0700714 named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700715 punct!(".") >>
716 method: ident >>
David Tolnayf6c74402016-10-08 02:31:26 -0700717 ascript: opt_vec!(preceded!(
718 punct!("::"),
719 delimited!(
720 punct!("<"),
David Tolnayff46fd22016-10-08 13:53:28 -0700721 terminated_list!(punct!(","), ty),
David Tolnayf6c74402016-10-08 02:31:26 -0700722 punct!(">")
723 )
David Tolnayfa0edf22016-09-23 22:58:24 -0700724 )) >>
725 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700726 args: terminated_list!(punct!(","), expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700727 punct!(")") >>
728 (method, ascript, args)
729 ));
730
David Tolnay7184b132016-10-30 10:06:37 -0700731 named!(expr_tup -> ExprKind, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700732 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700733 elems: terminated_list!(punct!(","), expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700734 punct!(")") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 (ExprTup { args: elems }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700736 ));
737
David Tolnayaf2557e2016-10-24 11:52:21 -0700738 named_ambiguous_expr!(and_binary -> (BinOp, Expr), allow_struct, tuple!(
739 binop,
740 ambiguous_expr!(allow_struct)
741 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700742
David Tolnay7184b132016-10-30 10:06:37 -0700743 named_ambiguous_expr!(expr_unary -> ExprKind, allow_struct, do_parse!(
David Tolnay3cb23a92016-10-07 23:02:21 -0700744 operator: unop >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700745 operand: ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 (ExprUnary { op: operator, expr: Box::new(operand) }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700747 ));
David Tolnay939766a2016-09-23 23:48:12 -0700748
David Tolnay7184b132016-10-30 10:06:37 -0700749 named!(expr_lit -> ExprKind, map!(lit, ExprKind::Lit));
David Tolnay939766a2016-09-23 23:48:12 -0700750
751 named!(and_cast -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700752 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700753 ty: ty >>
754 (ty)
755 ));
756
757 named!(and_ascription -> Ty, preceded!(punct!(":"), ty));
758
David Tolnaybb6feae2016-10-02 21:25:20 -0700759 enum Cond {
David Tolnay29f9ce12016-10-02 20:58:40 -0700760 Let(Pat, Expr),
761 Expr(Expr),
762 }
763
David Tolnaybb6feae2016-10-02 21:25:20 -0700764 named!(cond -> Cond, alt!(
765 do_parse!(
766 keyword!("let") >>
767 pat: pat >>
768 punct!("=") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700769 value: expr_no_struct >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700770 (Cond::Let(pat, value))
771 )
772 |
David Tolnayaf2557e2016-10-24 11:52:21 -0700773 map!(expr_no_struct, Cond::Expr)
David Tolnaybb6feae2016-10-02 21:25:20 -0700774 ));
775
David Tolnay7184b132016-10-30 10:06:37 -0700776 named!(expr_if -> ExprKind, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700777 keyword!("if") >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700778 cond: cond >>
David Tolnay939766a2016-09-23 23:48:12 -0700779 punct!("{") >>
780 then_block: within_block >>
781 punct!("}") >>
782 else_block: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700783 keyword!("else"),
David Tolnay939766a2016-09-23 23:48:12 -0700784 alt!(
785 expr_if
786 |
787 do_parse!(
788 punct!("{") >>
789 else_block: within_block >>
790 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700791 (ExprKind::Block(ExprBlock {
792 unsafety: Unsafety::Normal,
793 block: Block { stmts: else_block },
David Tolnay7184b132016-10-30 10:06:37 -0700794 }).into())
David Tolnay939766a2016-09-23 23:48:12 -0700795 )
796 )
797 )) >>
David Tolnay29f9ce12016-10-02 20:58:40 -0700798 (match cond {
Alex Crichton62a0a592017-05-22 13:58:53 -0700799 Cond::Let(pat, expr) => ExprIfLet {
800 pat: Box::new(pat),
801 expr: Box::new(expr),
802 if_true: Block {
David Tolnay29f9ce12016-10-02 20:58:40 -0700803 stmts: then_block,
804 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700805 if_false: else_block.map(|els| Box::new(els.into())),
806 }.into(),
807 Cond::Expr(cond) => ExprIf {
808 cond: Box::new(cond),
809 if_true: Block {
David Tolnay29f9ce12016-10-02 20:58:40 -0700810 stmts: then_block,
811 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700812 if_false: else_block.map(|els| Box::new(els.into())),
813 }.into(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700814 })
David Tolnay939766a2016-09-23 23:48:12 -0700815 ));
816
David Tolnay7184b132016-10-30 10:06:37 -0700817 named!(expr_for_loop -> ExprKind, do_parse!(
David Tolnaybb6feae2016-10-02 21:25:20 -0700818 lbl: option!(terminated!(label, punct!(":"))) >>
819 keyword!("for") >>
820 pat: pat >>
821 keyword!("in") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700822 expr: expr_no_struct >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700823 loop_block: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700824 (ExprForLoop {
825 pat: Box::new(pat),
826 expr: Box::new(expr),
827 body: loop_block,
828 label: lbl,
829 }.into())
David Tolnaybb6feae2016-10-02 21:25:20 -0700830 ));
831
David Tolnay7184b132016-10-30 10:06:37 -0700832 named!(expr_loop -> ExprKind, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700833 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -0700834 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -0400835 loop_block: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700836 (ExprLoop { body: loop_block, label: lbl }.into())
Gregory Katze5f35682016-09-27 14:20:55 -0400837 ));
838
David Tolnay7184b132016-10-30 10:06:37 -0700839 named!(expr_match -> ExprKind, do_parse!(
David Tolnayb4ad3b52016-10-01 21:58:13 -0700840 keyword!("match") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700841 obj: expr_no_struct >>
David Tolnayb4ad3b52016-10-01 21:58:13 -0700842 punct!("{") >>
David Tolnay1978c672016-10-27 22:05:52 -0700843 mut arms: many0!(do_parse!(
844 arm: match_arm >>
845 cond!(arm_requires_comma(&arm), punct!(",")) >>
846 cond!(!arm_requires_comma(&arm), option!(punct!(","))) >>
847 (arm)
David Tolnayb4ad3b52016-10-01 21:58:13 -0700848 )) >>
David Tolnay1978c672016-10-27 22:05:52 -0700849 last_arm: option!(match_arm) >>
David Tolnayb4ad3b52016-10-01 21:58:13 -0700850 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700851 (ExprMatch {
852 expr: Box::new(obj),
853 arms: {
854 arms.extend(last_arm);
855 arms
856 },
857 }.into())
David Tolnay1978c672016-10-27 22:05:52 -0700858 ));
859
Arnavion02ef13f2017-04-25 00:54:31 -0700860 named!(expr_catch -> ExprKind, do_parse!(
861 keyword!("do") >>
862 keyword!("catch") >>
863 catch_block: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700864 (ExprCatch { block: catch_block }.into())
Arnavion02ef13f2017-04-25 00:54:31 -0700865 ));
866
David Tolnay1978c672016-10-27 22:05:52 -0700867 fn arm_requires_comma(arm: &Arm) -> bool {
Alex Crichton62a0a592017-05-22 13:58:53 -0700868 if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
David Tolnay1978c672016-10-27 22:05:52 -0700869 false
870 } else {
871 true
872 }
873 }
874
875 named!(match_arm -> Arm, do_parse!(
876 attrs: many0!(outer_attr) >>
877 pats: separated_nonempty_list!(punct!("|"), pat) >>
878 guard: option!(preceded!(keyword!("if"), expr)) >>
879 punct!("=>") >>
880 body: alt!(
Alex Crichton62a0a592017-05-22 13:58:53 -0700881 map!(block, |blk| {
882 ExprKind::Block(ExprBlock {
883 unsafety: Unsafety::Normal,
884 block: blk,
885 }).into()
886 })
David Tolnay1978c672016-10-27 22:05:52 -0700887 |
888 expr
889 ) >>
890 (Arm {
891 attrs: attrs,
892 pats: pats,
893 guard: guard.map(Box::new),
894 body: Box::new(body),
895 })
David Tolnayb4ad3b52016-10-01 21:58:13 -0700896 ));
897
David Tolnay7184b132016-10-30 10:06:37 -0700898 named_ambiguous_expr!(expr_closure -> ExprKind, allow_struct, do_parse!(
David Tolnay89e05672016-10-02 14:39:42 -0700899 capture: capture_by >>
900 punct!("|") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700901 inputs: terminated_list!(punct!(","), closure_arg) >>
David Tolnay89e05672016-10-02 14:39:42 -0700902 punct!("|") >>
903 ret_and_body: alt!(
904 do_parse!(
905 punct!("->") >>
906 ty: ty >>
907 body: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700908 (FunctionRetTy::Ty(ty), ExprKind::Block(ExprBlock {
909 unsafety: Unsafety::Normal,
910 block: body,
911 }).into())
David Tolnay89e05672016-10-02 14:39:42 -0700912 )
913 |
David Tolnay58af3552016-12-22 16:58:07 -0500914 map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -0700915 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700916 (ExprClosure {
917 capture: capture,
918 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -0700919 inputs: inputs,
920 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -0700921 variadic: false,
David Tolnay89e05672016-10-02 14:39:42 -0700922 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700923 body: Box::new(ret_and_body.1),
924 }.into())
David Tolnay89e05672016-10-02 14:39:42 -0700925 ));
926
927 named!(closure_arg -> FnArg, do_parse!(
928 pat: pat >>
929 ty: option!(preceded!(punct!(":"), ty)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700930 (ArgCaptured {
931 pat: pat,
932 ty: ty.unwrap_or_else(|| TyInfer {}.into()),
933 }.into())
David Tolnay89e05672016-10-02 14:39:42 -0700934 ));
935
David Tolnay7184b132016-10-30 10:06:37 -0700936 named!(expr_while -> ExprKind, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700937 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -0700938 keyword!("while") >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700939 cond: cond >>
Gregory Katz3e562cc2016-09-28 18:33:02 -0400940 while_block: block >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700941 (match cond {
Alex Crichton62a0a592017-05-22 13:58:53 -0700942 Cond::Let(pat, expr) => ExprWhileLet {
943 pat: Box::new(pat),
944 expr: Box::new(expr),
945 body: while_block,
946 label: lbl,
947 }.into(),
948 Cond::Expr(cond) => ExprWhile {
949 cond: Box::new(cond),
950 body: while_block,
951 label: lbl,
952 }.into(),
David Tolnaybb6feae2016-10-02 21:25:20 -0700953 })
Gregory Katz3e562cc2016-09-28 18:33:02 -0400954 ));
955
David Tolnay7184b132016-10-30 10:06:37 -0700956 named!(expr_continue -> ExprKind, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -0400957 keyword!("continue") >>
958 lbl: option!(label) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700959 (ExprContinue { label: lbl }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -0400960 ));
961
David Tolnay5d55ef72016-12-21 20:20:04 -0500962 named_ambiguous_expr!(expr_break -> ExprKind, allow_struct, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -0400963 keyword!("break") >>
964 lbl: option!(label) >>
David Tolnay5d55ef72016-12-21 20:20:04 -0500965 val: option!(call!(ambiguous_expr, allow_struct, false)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700966 (ExprBreak { label: lbl, expr: val.map(Box::new) }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -0400967 ));
968
David Tolnay7184b132016-10-30 10:06:37 -0700969 named_ambiguous_expr!(expr_ret -> ExprKind, allow_struct, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -0400970 keyword!("return") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700971 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700972 (ExprRet { expr: ret_value.map(Box::new) }.into())
David Tolnay055a7042016-10-02 19:23:54 -0700973 ));
974
David Tolnay7184b132016-10-30 10:06:37 -0700975 named!(expr_struct -> ExprKind, do_parse!(
David Tolnay055a7042016-10-02 19:23:54 -0700976 path: path >>
977 punct!("{") >>
978 fields: separated_list!(punct!(","), field_value) >>
979 base: option!(do_parse!(
980 cond!(!fields.is_empty(), punct!(",")) >>
981 punct!("..") >>
982 base: expr >>
983 (base)
984 )) >>
David Tolnayf6c74402016-10-08 02:31:26 -0700985 cond!(!fields.is_empty() && base.is_none(), option!(punct!(","))) >>
David Tolnay055a7042016-10-02 19:23:54 -0700986 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700987 (ExprStruct {
988 path: path,
989 fields: fields,
990 rest: base.map(Box::new),
991 }.into())
David Tolnay055a7042016-10-02 19:23:54 -0700992 ));
993
David Tolnay276690f2016-10-30 12:06:59 -0700994 named!(field_value -> FieldValue, alt!(
995 do_parse!(
996 name: wordlike >>
997 punct!(":") >>
998 value: expr >>
999 (FieldValue {
1000 ident: name,
1001 expr: value,
1002 is_shorthand: false,
David Tolnay71d93772017-01-22 23:58:24 -08001003 attrs: Vec::new(),
David Tolnay276690f2016-10-30 12:06:59 -07001004 })
1005 )
1006 |
1007 map!(ident, |name: Ident| FieldValue {
1008 ident: name.clone(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001009 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
David Tolnay276690f2016-10-30 12:06:59 -07001010 is_shorthand: true,
David Tolnay71d93772017-01-22 23:58:24 -08001011 attrs: Vec::new(),
David Tolnay055a7042016-10-02 19:23:54 -07001012 })
1013 ));
1014
David Tolnay7184b132016-10-30 10:06:37 -07001015 named!(expr_repeat -> ExprKind, do_parse!(
David Tolnay055a7042016-10-02 19:23:54 -07001016 punct!("[") >>
1017 value: expr >>
1018 punct!(";") >>
1019 times: expr >>
1020 punct!("]") >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001021 (ExprRepeat { expr: Box::new(value), amt: Box::new(times) }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001022 ));
1023
David Tolnay7184b132016-10-30 10:06:37 -07001024 named!(expr_block -> ExprKind, do_parse!(
David Tolnay7b035912016-12-21 22:42:07 -05001025 rules: unsafety >>
David Tolnay42602292016-10-01 22:25:45 -07001026 b: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001027 (ExprBlock {
1028 unsafety: rules,
1029 block: Block { stmts: b.stmts },
1030 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001031 ));
1032
David Tolnay7184b132016-10-30 10:06:37 -07001033 named_ambiguous_expr!(expr_range -> ExprKind, allow_struct, do_parse!(
David Tolnay438c9052016-10-07 23:24:48 -07001034 limits: range_limits >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001035 hi: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001036 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001037 ));
1038
1039 named!(range_limits -> RangeLimits, alt!(
1040 punct!("...") => { |_| RangeLimits::Closed }
1041 |
1042 punct!("..") => { |_| RangeLimits::HalfOpen }
1043 ));
1044
Alex Crichton62a0a592017-05-22 13:58:53 -07001045 named!(expr_path -> ExprKind, map!(qpath, |(qself, path)| {
1046 ExprPath { qself: qself, path: path }.into()
1047 }));
David Tolnay42602292016-10-01 22:25:45 -07001048
David Tolnay7184b132016-10-30 10:06:37 -07001049 named_ambiguous_expr!(expr_addr_of -> ExprKind, allow_struct, do_parse!(
David Tolnay3c2467c2016-10-02 17:55:08 -07001050 punct!("&") >>
1051 mutability: mutability >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001052 expr: ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001053 (ExprAddrOf { mutbl: mutability, expr: Box::new(expr) }.into())
David Tolnay3c2467c2016-10-02 17:55:08 -07001054 ));
1055
David Tolnayaf2557e2016-10-24 11:52:21 -07001056 named_ambiguous_expr!(and_assign -> Expr, allow_struct, preceded!(
1057 punct!("="),
1058 ambiguous_expr!(allow_struct)
1059 ));
David Tolnay438c9052016-10-07 23:24:48 -07001060
David Tolnayaf2557e2016-10-24 11:52:21 -07001061 named_ambiguous_expr!(and_assign_op -> (BinOp, Expr), allow_struct, tuple!(
1062 assign_op,
1063 ambiguous_expr!(allow_struct)
1064 ));
David Tolnay438c9052016-10-07 23:24:48 -07001065
1066 named!(and_field -> Ident, preceded!(punct!("."), ident));
1067
1068 named!(and_tup_field -> u64, preceded!(punct!("."), digits));
1069
1070 named!(and_index -> Expr, delimited!(punct!("["), expr, punct!("]")));
1071
David Tolnayaf2557e2016-10-24 11:52:21 -07001072 named_ambiguous_expr!(and_range -> (RangeLimits, Option<Expr>), allow_struct, tuple!(
1073 range_limits,
David Tolnay54e854d2016-10-24 12:03:30 -07001074 option!(call!(ambiguous_expr, allow_struct, false))
David Tolnayaf2557e2016-10-24 11:52:21 -07001075 ));
David Tolnay438c9052016-10-07 23:24:48 -07001076
David Tolnay42602292016-10-01 22:25:45 -07001077 named!(pub block -> Block, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -07001078 punct!("{") >>
1079 stmts: within_block >>
1080 punct!("}") >>
1081 (Block {
1082 stmts: stmts,
David Tolnay939766a2016-09-23 23:48:12 -07001083 })
1084 ));
1085
David Tolnay3b9783a2016-10-29 22:37:09 -07001086 named!(pub within_block -> Vec<Stmt>, do_parse!(
David Tolnaycfe55022016-10-02 22:02:27 -07001087 many0!(punct!(";")) >>
David Tolnay1bf19132017-02-19 22:54:25 -08001088 mut standalone: many0!(terminated!(stmt, many0!(punct!(";")))) >>
David Tolnay939766a2016-09-23 23:48:12 -07001089 last: option!(expr) >>
1090 (match last {
David Tolnaycfe55022016-10-02 22:02:27 -07001091 None => standalone,
David Tolnay939766a2016-09-23 23:48:12 -07001092 Some(last) => {
David Tolnaycfe55022016-10-02 22:02:27 -07001093 standalone.push(Stmt::Expr(Box::new(last)));
1094 standalone
David Tolnay939766a2016-09-23 23:48:12 -07001095 }
1096 })
1097 ));
1098
David Tolnay1bf19132017-02-19 22:54:25 -08001099 named!(pub stmt -> Stmt, alt!(
David Tolnay13b3d352016-10-03 00:31:15 -07001100 stmt_mac
1101 |
David Tolnay191e0582016-10-02 18:31:09 -07001102 stmt_local
1103 |
1104 stmt_item
1105 |
David Tolnaycfe55022016-10-02 22:02:27 -07001106 stmt_expr
David Tolnay939766a2016-09-23 23:48:12 -07001107 ));
1108
David Tolnay13b3d352016-10-03 00:31:15 -07001109 named!(stmt_mac -> Stmt, do_parse!(
1110 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001111 what: path >>
David Tolnayeea28d62016-10-25 20:44:08 -07001112 punct!("!") >>
1113 // Only parse braces here; paren and bracket will get parsed as
1114 // expression statements
1115 punct!("{") >>
1116 tts: token_trees >>
1117 punct!("}") >>
David Tolnay60d48942016-10-30 14:34:52 -07001118 semi: option!(punct!(";")) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001119 (Stmt::Mac(Box::new((
1120 Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001121 path: what,
David Tolnayeea28d62016-10-25 20:44:08 -07001122 tts: vec![TokenTree::Delimited(Delimited {
1123 delim: DelimToken::Brace,
1124 tts: tts,
1125 })],
1126 },
David Tolnay60d48942016-10-30 14:34:52 -07001127 if semi.is_some() {
1128 MacStmtStyle::Semicolon
1129 } else {
1130 MacStmtStyle::Braces
1131 },
David Tolnayeea28d62016-10-25 20:44:08 -07001132 attrs,
1133 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001134 ));
1135
David Tolnay191e0582016-10-02 18:31:09 -07001136 named!(stmt_local -> Stmt, do_parse!(
1137 attrs: many0!(outer_attr) >>
1138 keyword!("let") >>
1139 pat: pat >>
1140 ty: option!(preceded!(punct!(":"), ty)) >>
1141 init: option!(preceded!(punct!("="), expr)) >>
1142 punct!(";") >>
1143 (Stmt::Local(Box::new(Local {
1144 pat: Box::new(pat),
1145 ty: ty.map(Box::new),
1146 init: init.map(Box::new),
1147 attrs: attrs,
1148 })))
1149 ));
1150
1151 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
1152
David Tolnaycfe55022016-10-02 22:02:27 -07001153 fn requires_semi(e: &Expr) -> bool {
David Tolnay7184b132016-10-30 10:06:37 -07001154 match e.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001155 ExprKind::If(_) |
1156 ExprKind::IfLet(_) |
1157 ExprKind::While(_) |
1158 ExprKind::WhileLet(_) |
1159 ExprKind::ForLoop(_) |
1160 ExprKind::Loop(_) |
1161 ExprKind::Match(_) |
1162 ExprKind::Block(_) => false,
David Tolnaycfe55022016-10-02 22:02:27 -07001163
1164 _ => true,
1165 }
1166 }
1167
1168 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay7184b132016-10-30 10:06:37 -07001169 attrs: many0!(outer_attr) >>
1170 mut e: expr >>
David Tolnaycfe55022016-10-02 22:02:27 -07001171 semi: option!(punct!(";")) >>
David Tolnay7184b132016-10-30 10:06:37 -07001172 ({
1173 e.attrs = attrs;
1174 if semi.is_some() {
1175 Stmt::Semi(Box::new(e))
David Tolnay092dcb02016-10-30 10:14:14 -07001176 } else if requires_semi(&e) {
David Tolnay7184b132016-10-30 10:06:37 -07001177 return Error;
1178 } else {
1179 Stmt::Expr(Box::new(e))
1180 }
David Tolnaycfe55022016-10-02 22:02:27 -07001181 })
David Tolnay939766a2016-09-23 23:48:12 -07001182 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001183
David Tolnay42602292016-10-01 22:25:45 -07001184 named!(pub pat -> Pat, alt!(
David Tolnayfbb73232016-10-03 01:00:06 -07001185 pat_wild // must be before pat_ident
1186 |
1187 pat_box // must be before pat_ident
David Tolnayb4ad3b52016-10-01 21:58:13 -07001188 |
David Tolnay8b308c22016-10-03 01:24:10 -07001189 pat_range // must be before pat_lit
1190 |
David Tolnayaa610942016-10-03 22:22:49 -07001191 pat_tuple_struct // must be before pat_ident
1192 |
David Tolnay8d9e81a2016-10-03 22:36:32 -07001193 pat_struct // must be before pat_ident
1194 |
David Tolnayaa610942016-10-03 22:22:49 -07001195 pat_mac // must be before pat_ident
1196 |
David Tolnaybcdbdd02016-10-24 23:05:02 -07001197 pat_lit // must be before pat_ident
1198 |
David Tolnay8b308c22016-10-03 01:24:10 -07001199 pat_ident // must be before pat_path
David Tolnay9636c052016-10-02 17:11:17 -07001200 |
1201 pat_path
David Tolnayfbb73232016-10-03 01:00:06 -07001202 |
1203 pat_tuple
David Tolnayffdb97f2016-10-03 01:28:33 -07001204 |
1205 pat_ref
David Tolnay435a9a82016-10-29 13:47:20 -07001206 |
1207 pat_slice
David Tolnayb4ad3b52016-10-01 21:58:13 -07001208 ));
1209
David Tolnay84aa0752016-10-02 23:01:13 -07001210 named!(pat_mac -> Pat, map!(mac, Pat::Mac));
1211
David Tolnayb4ad3b52016-10-01 21:58:13 -07001212 named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild));
1213
David Tolnayfbb73232016-10-03 01:00:06 -07001214 named!(pat_box -> Pat, do_parse!(
1215 keyword!("box") >>
1216 pat: pat >>
1217 (Pat::Box(Box::new(pat)))
1218 ));
1219
David Tolnayb4ad3b52016-10-01 21:58:13 -07001220 named!(pat_ident -> Pat, do_parse!(
1221 mode: option!(keyword!("ref")) >>
1222 mutability: mutability >>
David Tolnay8d4d2fa2016-10-25 22:08:07 -07001223 name: alt!(
1224 ident
1225 |
1226 keyword!("self") => { Into::into }
1227 ) >>
David Tolnay1f16b602017-02-07 20:06:55 -05001228 not!(punct!("<")) >>
1229 not!(punct!("::")) >>
David Tolnayb4ad3b52016-10-01 21:58:13 -07001230 subpat: option!(preceded!(punct!("@"), pat)) >>
1231 (Pat::Ident(
1232 if mode.is_some() {
1233 BindingMode::ByRef(mutability)
1234 } else {
1235 BindingMode::ByValue(mutability)
1236 },
1237 name,
1238 subpat.map(Box::new),
1239 ))
1240 ));
1241
David Tolnayaa610942016-10-03 22:22:49 -07001242 named!(pat_tuple_struct -> Pat, do_parse!(
1243 path: path >>
1244 tuple: pat_tuple_helper >>
1245 (Pat::TupleStruct(path, tuple.0, tuple.1))
1246 ));
1247
David Tolnay8d9e81a2016-10-03 22:36:32 -07001248 named!(pat_struct -> Pat, do_parse!(
1249 path: path >>
1250 punct!("{") >>
1251 fields: separated_list!(punct!(","), field_pat) >>
1252 more: option!(preceded!(
1253 cond!(!fields.is_empty(), punct!(",")),
1254 punct!("..")
1255 )) >>
David Tolnayff46fd22016-10-08 13:53:28 -07001256 cond!(!fields.is_empty() && more.is_none(), option!(punct!(","))) >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001257 punct!("}") >>
1258 (Pat::Struct(path, fields, more.is_some()))
1259 ));
1260
1261 named!(field_pat -> FieldPat, alt!(
1262 do_parse!(
David Tolnay39039442016-10-30 11:25:21 -07001263 ident: wordlike >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001264 punct!(":") >>
1265 pat: pat >>
1266 (FieldPat {
1267 ident: ident,
1268 pat: Box::new(pat),
1269 is_shorthand: false,
David Tolnay71d93772017-01-22 23:58:24 -08001270 attrs: Vec::new(),
David Tolnay8d9e81a2016-10-03 22:36:32 -07001271 })
1272 )
1273 |
1274 do_parse!(
David Tolnayda167382016-10-30 13:34:09 -07001275 boxed: option!(keyword!("box")) >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001276 mode: option!(keyword!("ref")) >>
1277 mutability: mutability >>
1278 ident: ident >>
David Tolnayda167382016-10-30 13:34:09 -07001279 ({
1280 let mut pat = Pat::Ident(
David Tolnay8d9e81a2016-10-03 22:36:32 -07001281 if mode.is_some() {
1282 BindingMode::ByRef(mutability)
1283 } else {
1284 BindingMode::ByValue(mutability)
1285 },
David Tolnayda167382016-10-30 13:34:09 -07001286 ident.clone(),
David Tolnay8d9e81a2016-10-03 22:36:32 -07001287 None,
David Tolnayda167382016-10-30 13:34:09 -07001288 );
1289 if boxed.is_some() {
1290 pat = Pat::Box(Box::new(pat));
1291 }
1292 FieldPat {
1293 ident: ident,
1294 pat: Box::new(pat),
1295 is_shorthand: true,
David Tolnay71d93772017-01-22 23:58:24 -08001296 attrs: Vec::new(),
David Tolnayda167382016-10-30 13:34:09 -07001297 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001298 })
1299 )
1300 ));
1301
David Tolnay9636c052016-10-02 17:11:17 -07001302 named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path)));
1303
David Tolnayaa610942016-10-03 22:22:49 -07001304 named!(pat_tuple -> Pat, map!(
1305 pat_tuple_helper,
1306 |(pats, dotdot)| Pat::Tuple(pats, dotdot)
1307 ));
1308
1309 named!(pat_tuple_helper -> (Vec<Pat>, Option<usize>), do_parse!(
David Tolnayfbb73232016-10-03 01:00:06 -07001310 punct!("(") >>
1311 mut elems: separated_list!(punct!(","), pat) >>
1312 dotdot: option!(do_parse!(
1313 cond!(!elems.is_empty(), punct!(",")) >>
1314 punct!("..") >>
1315 rest: many0!(preceded!(punct!(","), pat)) >>
1316 cond!(!rest.is_empty(), option!(punct!(","))) >>
1317 (rest)
1318 )) >>
1319 cond!(!elems.is_empty() && dotdot.is_none(), option!(punct!(","))) >>
1320 punct!(")") >>
1321 (match dotdot {
1322 Some(rest) => {
1323 let pos = elems.len();
1324 elems.extend(rest);
David Tolnayaa610942016-10-03 22:22:49 -07001325 (elems, Some(pos))
David Tolnayfbb73232016-10-03 01:00:06 -07001326 }
David Tolnayaa610942016-10-03 22:22:49 -07001327 None => (elems, None),
David Tolnayfbb73232016-10-03 01:00:06 -07001328 })
1329 ));
1330
David Tolnayffdb97f2016-10-03 01:28:33 -07001331 named!(pat_ref -> Pat, do_parse!(
1332 punct!("&") >>
1333 mutability: mutability >>
1334 pat: pat >>
1335 (Pat::Ref(Box::new(pat), mutability))
1336 ));
1337
David Tolnayef40da42016-10-30 01:19:04 -07001338 named!(pat_lit -> Pat, do_parse!(
1339 lit: pat_lit_expr >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001340 (if let ExprKind::Path(_) = lit.node {
David Tolnayef40da42016-10-30 01:19:04 -07001341 return IResult::Error; // these need to be parsed by pat_path
1342 } else {
1343 Pat::Lit(Box::new(lit))
1344 })
1345 ));
David Tolnaye1310902016-10-29 23:40:00 -07001346
1347 named!(pat_range -> Pat, do_parse!(
David Tolnay2cfddc62016-10-30 01:03:27 -07001348 lo: pat_lit_expr >>
Arnavion1992e2f2017-04-25 01:47:46 -07001349 limits: range_limits >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001350 hi: pat_lit_expr >>
Arnavion1992e2f2017-04-25 01:47:46 -07001351 (Pat::Range(Box::new(lo), Box::new(hi), limits))
David Tolnaye1310902016-10-29 23:40:00 -07001352 ));
1353
David Tolnay2cfddc62016-10-30 01:03:27 -07001354 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001355 neg: option!(punct!("-")) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001356 v: alt!(
David Tolnay7184b132016-10-30 10:06:37 -07001357 lit => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07001358 |
Alex Crichton62a0a592017-05-22 13:58:53 -07001359 path => { |p| ExprPath { qself: None, path: p }.into() }
David Tolnay2cfddc62016-10-30 01:03:27 -07001360 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001361 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07001362 ExprKind::Unary(ExprUnary {
1363 op: UnOp::Neg,
1364 expr: Box::new(v.into())
1365 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001366 } else {
David Tolnay7184b132016-10-30 10:06:37 -07001367 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001368 })
1369 ));
David Tolnay8b308c22016-10-03 01:24:10 -07001370
David Tolnay435a9a82016-10-29 13:47:20 -07001371 named!(pat_slice -> Pat, do_parse!(
1372 punct!("[") >>
David Tolnaye1f13c32016-10-29 23:34:40 -07001373 mut before: separated_list!(punct!(","), pat) >>
David Tolnay435a9a82016-10-29 13:47:20 -07001374 after: option!(do_parse!(
David Tolnaye1f13c32016-10-29 23:34:40 -07001375 comma_before_dots: option!(cond_reduce!(!before.is_empty(), punct!(","))) >>
David Tolnay435a9a82016-10-29 13:47:20 -07001376 punct!("..") >>
1377 after: many0!(preceded!(punct!(","), pat)) >>
1378 cond!(!after.is_empty(), option!(punct!(","))) >>
David Tolnaye1f13c32016-10-29 23:34:40 -07001379 (comma_before_dots.is_some(), after)
David Tolnay435a9a82016-10-29 13:47:20 -07001380 )) >>
David Tolnay61735272016-10-30 17:34:23 -07001381 cond!(after.is_none(), option!(punct!(","))) >>
David Tolnay435a9a82016-10-29 13:47:20 -07001382 punct!("]") >>
1383 (match after {
David Tolnay435a9a82016-10-29 13:47:20 -07001384 None => Pat::Slice(before, None, Vec::new()),
David Tolnaye1f13c32016-10-29 23:34:40 -07001385 Some((true, after)) => {
1386 if before.is_empty() {
1387 return IResult::Error;
1388 }
1389 Pat::Slice(before, Some(Box::new(Pat::Wild)), after)
1390 }
1391 Some((false, after)) => {
1392 let rest = before.pop().unwrap_or(Pat::Wild);
1393 Pat::Slice(before, Some(Box::new(rest)), after)
1394 }
David Tolnay435a9a82016-10-29 13:47:20 -07001395 })
1396 ));
1397
David Tolnay89e05672016-10-02 14:39:42 -07001398 named!(capture_by -> CaptureBy, alt!(
1399 keyword!("move") => { |_| CaptureBy::Value }
1400 |
1401 epsilon!() => { |_| CaptureBy::Ref }
1402 ));
1403
David Tolnay8b07f372016-09-30 10:28:40 -07001404 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -07001405}
1406
David Tolnayf4bbbd92016-09-23 14:41:55 -07001407#[cfg(feature = "printing")]
1408mod printing {
1409 use super::*;
Alex Crichton62a0a592017-05-22 13:58:53 -07001410 use {FnArg, FunctionRetTy, Mutability, Ty, Unsafety, ArgCaptured};
David Tolnay13b3d352016-10-03 00:31:15 -07001411 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07001412 use quote::{Tokens, ToTokens};
1413
1414 impl ToTokens for Expr {
1415 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07001416 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001417 self.node.to_tokens(tokens)
1418 }
1419 }
1420
1421 impl ToTokens for ExprBox {
1422 fn to_tokens(&self, tokens: &mut Tokens) {
1423 tokens.append("box");
1424 self.expr.to_tokens(tokens);
1425 }
1426 }
1427
1428 impl ToTokens for ExprInPlace {
1429 fn to_tokens(&self, tokens: &mut Tokens) {
1430 tokens.append("in");
1431 self.place.to_tokens(tokens);
1432 self.value.to_tokens(tokens);
1433 }
1434 }
1435
1436 impl ToTokens for ExprArray {
1437 fn to_tokens(&self, tokens: &mut Tokens) {
1438 tokens.append("[");
1439 tokens.append_separated(&self.exprs, ",");
1440 tokens.append("]");
1441 }
1442 }
1443
1444 impl ToTokens for ExprCall {
1445 fn to_tokens(&self, tokens: &mut Tokens) {
1446 self.func.to_tokens(tokens);
1447 tokens.append("(");
1448 tokens.append_separated(&self.args, ",");
1449 tokens.append(")");
1450 }
1451 }
1452
1453 impl ToTokens for ExprMethodCall {
1454 fn to_tokens(&self, tokens: &mut Tokens) {
1455 self.args[0].to_tokens(tokens);
1456 tokens.append(".");
1457 self.method.to_tokens(tokens);
1458 if !self.typarams.is_empty() {
1459 tokens.append("::");
1460 tokens.append("<");
1461 tokens.append_separated(&self.typarams, ",");
1462 tokens.append(">");
1463 }
1464 tokens.append("(");
1465 tokens.append_separated(&self.args[1..], ",");
1466 tokens.append(")");
1467 }
1468 }
1469
1470 impl ToTokens for ExprTup {
1471 fn to_tokens(&self, tokens: &mut Tokens) {
1472 tokens.append("(");
1473 tokens.append_separated(&self.args, ",");
1474 if self.args.len() == 1 {
1475 tokens.append(",");
1476 }
1477 tokens.append(")");
1478 }
1479 }
1480
1481 impl ToTokens for ExprBinary {
1482 fn to_tokens(&self, tokens: &mut Tokens) {
1483 self.left.to_tokens(tokens);
1484 self.op.to_tokens(tokens);
1485 self.right.to_tokens(tokens);
1486 }
1487 }
1488
1489 impl ToTokens for ExprUnary {
1490 fn to_tokens(&self, tokens: &mut Tokens) {
1491 self.op.to_tokens(tokens);
1492 self.expr.to_tokens(tokens);
1493 }
1494 }
1495
1496 impl ToTokens for ExprCast {
1497 fn to_tokens(&self, tokens: &mut Tokens) {
1498 self.expr.to_tokens(tokens);
1499 tokens.append("as");
1500 self.ty.to_tokens(tokens);
1501 }
1502 }
1503
1504 impl ToTokens for ExprType {
1505 fn to_tokens(&self, tokens: &mut Tokens) {
1506 self.expr.to_tokens(tokens);
1507 tokens.append(":");
1508 self.ty.to_tokens(tokens);
1509 }
1510 }
1511
1512 impl ToTokens for ExprIf {
1513 fn to_tokens(&self, tokens: &mut Tokens) {
1514 tokens.append("if");
1515 self.cond.to_tokens(tokens);
1516 self.if_true.to_tokens(tokens);
1517 if let Some(ref else_block) = self.if_false {
1518 tokens.append("else");
1519 else_block.to_tokens(tokens);
1520 }
1521 }
1522 }
1523
1524 impl ToTokens for ExprIfLet {
1525 fn to_tokens(&self, tokens: &mut Tokens) {
1526 tokens.append("if");
1527 tokens.append("let");
1528 self.pat.to_tokens(tokens);
1529 tokens.append("=");
1530 self.expr.to_tokens(tokens);
1531 self.if_true.to_tokens(tokens);
1532 if let Some(ref else_block) = self.if_false {
1533 tokens.append("else");
1534 else_block.to_tokens(tokens);
1535 }
1536 }
1537 }
1538
1539 impl ToTokens for ExprWhile {
1540 fn to_tokens(&self, tokens: &mut Tokens) {
1541 if let Some(ref label) = self.label {
1542 label.to_tokens(tokens);
1543 tokens.append(":");
1544 }
1545 tokens.append("while");
1546 self.cond.to_tokens(tokens);
1547 self.body.to_tokens(tokens);
1548 }
1549 }
1550
1551 impl ToTokens for ExprWhileLet {
1552 fn to_tokens(&self, tokens: &mut Tokens) {
1553 if let Some(ref label) = self.label {
1554 label.to_tokens(tokens);
1555 tokens.append(":");
1556 }
1557 tokens.append("while");
1558 tokens.append("let");
1559 self.pat.to_tokens(tokens);
1560 tokens.append("=");
1561 self.expr.to_tokens(tokens);
1562 self.body.to_tokens(tokens);
1563 }
1564 }
1565
1566 impl ToTokens for ExprForLoop {
1567 fn to_tokens(&self, tokens: &mut Tokens) {
1568 if let Some(ref label) = self.label {
1569 label.to_tokens(tokens);
1570 tokens.append(":");
1571 }
1572 tokens.append("for");
1573 self.pat.to_tokens(tokens);
1574 tokens.append("in");
1575 self.expr.to_tokens(tokens);
1576 self.body.to_tokens(tokens);
1577 }
1578 }
1579
1580 impl ToTokens for ExprLoop {
1581 fn to_tokens(&self, tokens: &mut Tokens) {
1582 if let Some(ref label) = self.label {
1583 label.to_tokens(tokens);
1584 tokens.append(":");
1585 }
1586 tokens.append("loop");
1587 self.body.to_tokens(tokens);
1588 }
1589 }
1590
1591 impl ToTokens for ExprMatch {
1592 fn to_tokens(&self, tokens: &mut Tokens) {
1593 tokens.append("match");
1594 self.expr.to_tokens(tokens);
1595 tokens.append("{");
1596 tokens.append_all(&self.arms);
1597 tokens.append("}");
1598 }
1599 }
1600
1601 impl ToTokens for ExprCatch {
1602 fn to_tokens(&self, tokens: &mut Tokens) {
1603 tokens.append("do");
1604 tokens.append("catch");
1605 self.block.to_tokens(tokens);
1606 }
1607 }
1608
1609 impl ToTokens for ExprClosure {
1610 fn to_tokens(&self, tokens: &mut Tokens) {
1611 self.capture.to_tokens(tokens);
1612 tokens.append("|");
1613 for (i, input) in self.decl.inputs.iter().enumerate() {
1614 if i > 0 {
1615 tokens.append(",");
David Tolnaybb6feae2016-10-02 21:25:20 -07001616 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001617 match *input {
1618 FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_) }) => {
1619 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07001620 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001621 _ => input.to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07001622 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001623 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001624 tokens.append("|");
1625 match self.decl.output {
1626 FunctionRetTy::Default => { /* nothing */ }
1627 FunctionRetTy::Ty(ref ty) => {
1628 tokens.append("->");
1629 ty.to_tokens(tokens);
1630 }
1631 }
1632 self.body.to_tokens(tokens);
1633 }
1634 }
1635
1636 impl ToTokens for ExprBlock {
1637 fn to_tokens(&self, tokens: &mut Tokens) {
1638 self.unsafety.to_tokens(tokens);
1639 self.block.to_tokens(tokens);
1640 }
1641 }
1642
1643 impl ToTokens for ExprAssign {
1644 fn to_tokens(&self, tokens: &mut Tokens) {
1645 self.left.to_tokens(tokens);
1646 tokens.append("=");
1647 self.right.to_tokens(tokens);
1648 }
1649 }
1650
1651 impl ToTokens for ExprAssignOp {
1652 fn to_tokens(&self, tokens: &mut Tokens) {
1653 self.left.to_tokens(tokens);
1654 tokens.append(self.op.assign_op().unwrap());
1655 self.right.to_tokens(tokens);
1656 }
1657 }
1658
1659 impl ToTokens for ExprField {
1660 fn to_tokens(&self, tokens: &mut Tokens) {
1661 self.expr.to_tokens(tokens);
1662 tokens.append(".");
1663 self.field.to_tokens(tokens);
1664 }
1665 }
1666
1667 impl ToTokens for ExprTupField {
1668 fn to_tokens(&self, tokens: &mut Tokens) {
1669 self.expr.to_tokens(tokens);
1670 tokens.append(".");
1671 tokens.append(&self.field.to_string());
1672 }
1673 }
1674
1675 impl ToTokens for ExprIndex {
1676 fn to_tokens(&self, tokens: &mut Tokens) {
1677 self.expr.to_tokens(tokens);
1678 tokens.append("[");
1679 self.index.to_tokens(tokens);
1680 tokens.append("]");
1681 }
1682 }
1683
1684 impl ToTokens for ExprRange {
1685 fn to_tokens(&self, tokens: &mut Tokens) {
1686 self.from.to_tokens(tokens);
1687 self.limits.to_tokens(tokens);
1688 self.to.to_tokens(tokens);
1689 }
1690 }
1691
1692 impl ToTokens for ExprPath {
1693 fn to_tokens(&self, tokens: &mut Tokens) {
1694 let qself = match self.qself {
1695 Some(ref qself) => qself,
1696 None => return self.path.to_tokens(tokens),
1697 };
1698 tokens.append("<");
1699 qself.ty.to_tokens(tokens);
1700 if qself.position > 0 {
1701 tokens.append("as");
1702 for (i, segment) in self.path.segments
1703 .iter()
1704 .take(qself.position)
1705 .enumerate() {
1706 if i > 0 || self.path.global {
1707 tokens.append("::");
1708 }
1709 segment.to_tokens(tokens);
1710 }
1711 }
1712 tokens.append(">");
1713 for segment in self.path.segments.iter().skip(qself.position) {
1714 tokens.append("::");
1715 segment.to_tokens(tokens);
1716 }
1717 }
1718 }
1719
1720 impl ToTokens for ExprAddrOf {
1721 fn to_tokens(&self, tokens: &mut Tokens) {
1722 tokens.append("&");
1723 self.mutbl.to_tokens(tokens);
1724 self.expr.to_tokens(tokens);
1725 }
1726 }
1727
1728 impl ToTokens for ExprBreak {
1729 fn to_tokens(&self, tokens: &mut Tokens) {
1730 tokens.append("break");
1731 self.label.to_tokens(tokens);
1732 self.expr.to_tokens(tokens);
1733 }
1734 }
1735
1736 impl ToTokens for ExprContinue {
1737 fn to_tokens(&self, tokens: &mut Tokens) {
1738 tokens.append("continue");
1739 self.label.to_tokens(tokens);
1740 }
1741 }
1742
1743 impl ToTokens for ExprRet {
1744 fn to_tokens(&self, tokens: &mut Tokens) {
1745 tokens.append("return");
1746 self.expr.to_tokens(tokens);
1747 }
1748 }
1749
1750 impl ToTokens for ExprStruct {
1751 fn to_tokens(&self, tokens: &mut Tokens) {
1752 self.path.to_tokens(tokens);
1753 tokens.append("{");
1754 tokens.append_separated(&self.fields, ",");
1755 if let Some(ref base) = self.rest {
1756 if !self.fields.is_empty() {
1757 tokens.append(",");
1758 }
1759 tokens.append("..");
1760 base.to_tokens(tokens);
1761 }
1762 tokens.append("}");
1763 }
1764 }
1765
1766 impl ToTokens for ExprRepeat {
1767 fn to_tokens(&self, tokens: &mut Tokens) {
1768 tokens.append("[");
1769 self.expr.to_tokens(tokens);
1770 tokens.append(";");
1771 self.amt.to_tokens(tokens);
1772 tokens.append("]");
1773 }
1774 }
1775
1776 impl ToTokens for ExprParen {
1777 fn to_tokens(&self, tokens: &mut Tokens) {
1778 tokens.append("(");
1779 self.expr.to_tokens(tokens);
1780 tokens.append(")");
1781 }
1782 }
1783
1784 impl ToTokens for ExprTry {
1785 fn to_tokens(&self, tokens: &mut Tokens) {
1786 self.expr.to_tokens(tokens);
1787 tokens.append("?");
David Tolnayf4bbbd92016-09-23 14:41:55 -07001788 }
1789 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001790
David Tolnay055a7042016-10-02 19:23:54 -07001791 impl ToTokens for FieldValue {
1792 fn to_tokens(&self, tokens: &mut Tokens) {
1793 self.ident.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07001794 if !self.is_shorthand {
1795 tokens.append(":");
1796 self.expr.to_tokens(tokens);
1797 }
David Tolnay055a7042016-10-02 19:23:54 -07001798 }
1799 }
1800
David Tolnayb4ad3b52016-10-01 21:58:13 -07001801 impl ToTokens for Arm {
1802 fn to_tokens(&self, tokens: &mut Tokens) {
1803 for attr in &self.attrs {
1804 attr.to_tokens(tokens);
1805 }
1806 tokens.append_separated(&self.pats, "|");
1807 if let Some(ref guard) = self.guard {
1808 tokens.append("if");
1809 guard.to_tokens(tokens);
1810 }
1811 tokens.append("=>");
1812 self.body.to_tokens(tokens);
David Tolnay7184b132016-10-30 10:06:37 -07001813 match self.body.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001814 ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) => {
David Tolnaydaaf7742016-10-03 11:11:43 -07001815 // no comma
1816 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001817 _ => tokens.append(","),
1818 }
1819 }
1820 }
1821
1822 impl ToTokens for Pat {
1823 fn to_tokens(&self, tokens: &mut Tokens) {
1824 match *self {
1825 Pat::Wild => tokens.append("_"),
1826 Pat::Ident(mode, ref ident, ref subpat) => {
1827 mode.to_tokens(tokens);
1828 ident.to_tokens(tokens);
1829 if let Some(ref subpat) = *subpat {
1830 tokens.append("@");
1831 subpat.to_tokens(tokens);
1832 }
1833 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001834 Pat::Struct(ref path, ref fields, dots) => {
1835 path.to_tokens(tokens);
1836 tokens.append("{");
1837 tokens.append_separated(fields, ",");
1838 if dots {
1839 if !fields.is_empty() {
1840 tokens.append(",");
1841 }
1842 tokens.append("..");
1843 }
1844 tokens.append("}");
1845 }
David Tolnayaa610942016-10-03 22:22:49 -07001846 Pat::TupleStruct(ref path, ref pats, dotpos) => {
1847 path.to_tokens(tokens);
1848 tokens.append("(");
1849 match dotpos {
1850 Some(pos) => {
1851 if pos > 0 {
1852 tokens.append_separated(&pats[..pos], ",");
1853 tokens.append(",");
1854 }
1855 tokens.append("..");
1856 if pos < pats.len() {
1857 tokens.append(",");
1858 tokens.append_separated(&pats[pos..], ",");
1859 }
1860 }
1861 None => tokens.append_separated(pats, ","),
1862 }
1863 tokens.append(")");
1864 }
David Tolnay8b308c22016-10-03 01:24:10 -07001865 Pat::Path(None, ref path) => path.to_tokens(tokens),
1866 Pat::Path(Some(ref qself), ref path) => {
1867 tokens.append("<");
1868 qself.ty.to_tokens(tokens);
1869 if qself.position > 0 {
1870 tokens.append("as");
David Tolnaydaaf7742016-10-03 11:11:43 -07001871 for (i, segment) in path.segments
David Tolnay05120ef2017-03-12 18:29:26 -07001872 .iter()
1873 .take(qself.position)
1874 .enumerate() {
David Tolnay8b308c22016-10-03 01:24:10 -07001875 if i > 0 || path.global {
1876 tokens.append("::");
1877 }
1878 segment.to_tokens(tokens);
1879 }
1880 }
1881 tokens.append(">");
1882 for segment in path.segments.iter().skip(qself.position) {
1883 tokens.append("::");
1884 segment.to_tokens(tokens);
1885 }
1886 }
David Tolnayfbb73232016-10-03 01:00:06 -07001887 Pat::Tuple(ref pats, dotpos) => {
1888 tokens.append("(");
1889 match dotpos {
1890 Some(pos) => {
1891 if pos > 0 {
1892 tokens.append_separated(&pats[..pos], ",");
1893 tokens.append(",");
1894 }
1895 tokens.append("..");
1896 if pos < pats.len() {
1897 tokens.append(",");
1898 tokens.append_separated(&pats[pos..], ",");
1899 }
1900 }
David Tolnay37aa2962016-10-30 10:11:12 -07001901 None => {
1902 tokens.append_separated(pats, ",");
1903 if pats.len() == 1 {
1904 tokens.append(",");
1905 }
1906 }
David Tolnayfbb73232016-10-03 01:00:06 -07001907 }
1908 tokens.append(")");
1909 }
1910 Pat::Box(ref inner) => {
1911 tokens.append("box");
1912 inner.to_tokens(tokens);
1913 }
David Tolnayffdb97f2016-10-03 01:28:33 -07001914 Pat::Ref(ref target, mutability) => {
1915 tokens.append("&");
1916 mutability.to_tokens(tokens);
1917 target.to_tokens(tokens);
1918 }
David Tolnay8b308c22016-10-03 01:24:10 -07001919 Pat::Lit(ref lit) => lit.to_tokens(tokens),
Arnavion1992e2f2017-04-25 01:47:46 -07001920 Pat::Range(ref lo, ref hi, ref limits) => {
David Tolnay8b308c22016-10-03 01:24:10 -07001921 lo.to_tokens(tokens);
Arnavion1992e2f2017-04-25 01:47:46 -07001922 limits.to_tokens(tokens);
David Tolnay8b308c22016-10-03 01:24:10 -07001923 hi.to_tokens(tokens);
1924 }
David Tolnaye1f13c32016-10-29 23:34:40 -07001925 Pat::Slice(ref before, ref rest, ref after) => {
David Tolnay435a9a82016-10-29 13:47:20 -07001926 tokens.append("[");
1927 tokens.append_separated(before, ",");
David Tolnaye1f13c32016-10-29 23:34:40 -07001928 if let Some(ref rest) = *rest {
1929 if !before.is_empty() {
1930 tokens.append(",");
David Tolnay435a9a82016-10-29 13:47:20 -07001931 }
David Tolnaye1f13c32016-10-29 23:34:40 -07001932 if **rest != Pat::Wild {
1933 rest.to_tokens(tokens);
1934 }
1935 tokens.append("..");
1936 if !after.is_empty() {
1937 tokens.append(",");
1938 }
1939 tokens.append_separated(after, ",");
David Tolnay435a9a82016-10-29 13:47:20 -07001940 }
1941 tokens.append("]");
1942 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001943 Pat::Mac(ref mac) => mac.to_tokens(tokens),
David Tolnayb4ad3b52016-10-01 21:58:13 -07001944 }
1945 }
1946 }
1947
Arnavion1992e2f2017-04-25 01:47:46 -07001948 impl ToTokens for RangeLimits {
1949 fn to_tokens(&self, tokens: &mut Tokens) {
1950 match *self {
1951 RangeLimits::HalfOpen => tokens.append(".."),
1952 RangeLimits::Closed => tokens.append("..."),
1953 }
1954 }
1955 }
1956
David Tolnay8d9e81a2016-10-03 22:36:32 -07001957 impl ToTokens for FieldPat {
1958 fn to_tokens(&self, tokens: &mut Tokens) {
1959 if !self.is_shorthand {
1960 self.ident.to_tokens(tokens);
1961 tokens.append(":");
1962 }
1963 self.pat.to_tokens(tokens);
1964 }
1965 }
1966
David Tolnayb4ad3b52016-10-01 21:58:13 -07001967 impl ToTokens for BindingMode {
1968 fn to_tokens(&self, tokens: &mut Tokens) {
1969 match *self {
1970 BindingMode::ByRef(Mutability::Immutable) => {
1971 tokens.append("ref");
1972 }
1973 BindingMode::ByRef(Mutability::Mutable) => {
1974 tokens.append("ref");
1975 tokens.append("mut");
1976 }
1977 BindingMode::ByValue(Mutability::Immutable) => {}
1978 BindingMode::ByValue(Mutability::Mutable) => {
1979 tokens.append("mut");
1980 }
1981 }
1982 }
1983 }
David Tolnay42602292016-10-01 22:25:45 -07001984
David Tolnay89e05672016-10-02 14:39:42 -07001985 impl ToTokens for CaptureBy {
1986 fn to_tokens(&self, tokens: &mut Tokens) {
1987 match *self {
1988 CaptureBy::Value => tokens.append("move"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001989 CaptureBy::Ref => {
1990 // nothing
1991 }
David Tolnay89e05672016-10-02 14:39:42 -07001992 }
1993 }
1994 }
1995
David Tolnay42602292016-10-01 22:25:45 -07001996 impl ToTokens for Block {
1997 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay42602292016-10-01 22:25:45 -07001998 tokens.append("{");
David Tolnay3b9783a2016-10-29 22:37:09 -07001999 tokens.append_all(&self.stmts);
David Tolnay42602292016-10-01 22:25:45 -07002000 tokens.append("}");
2001 }
2002 }
2003
David Tolnay42602292016-10-01 22:25:45 -07002004 impl ToTokens for Stmt {
2005 fn to_tokens(&self, tokens: &mut Tokens) {
2006 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002007 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002008 Stmt::Item(ref item) => item.to_tokens(tokens),
2009 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
2010 Stmt::Semi(ref expr) => {
2011 expr.to_tokens(tokens);
2012 tokens.append(";");
2013 }
David Tolnay13b3d352016-10-03 00:31:15 -07002014 Stmt::Mac(ref mac) => {
2015 let (ref mac, style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002016 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002017 mac.to_tokens(tokens);
2018 match style {
2019 MacStmtStyle::Semicolon => tokens.append(";"),
David Tolnaydaaf7742016-10-03 11:11:43 -07002020 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2021 // no semicolon
2022 }
David Tolnay13b3d352016-10-03 00:31:15 -07002023 }
2024 }
David Tolnay42602292016-10-01 22:25:45 -07002025 }
2026 }
2027 }
David Tolnay191e0582016-10-02 18:31:09 -07002028
2029 impl ToTokens for Local {
2030 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002031 tokens.append_all(self.attrs.outer());
David Tolnay191e0582016-10-02 18:31:09 -07002032 tokens.append("let");
2033 self.pat.to_tokens(tokens);
2034 if let Some(ref ty) = self.ty {
2035 tokens.append(":");
2036 ty.to_tokens(tokens);
2037 }
2038 if let Some(ref init) = self.init {
2039 tokens.append("=");
2040 init.to_tokens(tokens);
2041 }
2042 tokens.append(";");
2043 }
2044 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002045}