blob: 4e72a6f40e96ca8abc8edefeb269c75b0dc3419a [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
2
3#[derive(Debug, Clone, Eq, PartialEq)]
4pub enum Expr {
5 /// A `box x` expression.
6 Box(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -07007 /// An array (`[a, b, c, d]`)
8 Vec(Vec<Expr>),
9 /// A function call
10 ///
11 /// The first field resolves to the function itself,
12 /// and the second field is the list of arguments
13 Call(Box<Expr>, Vec<Expr>),
14 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
15 ///
16 /// The `Ident` is the identifier for the method name.
17 /// The vector of `Ty`s are the ascripted type parameters for the method
18 /// (within the angle brackets).
19 ///
20 /// The first element of the vector of `Expr`s is the expression that evaluates
21 /// to the object on which the method is being called on (the receiver),
22 /// and the remaining elements are the rest of the arguments.
23 ///
24 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
25 /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
26 MethodCall(Ident, Vec<Ty>, Vec<Expr>),
27 /// A tuple (`(a, b, c, d)`)
28 Tup(Vec<Expr>),
29 /// A binary operation (For example: `a + b`, `a * b`)
30 Binary(BinOp, Box<Expr>, Box<Expr>),
31 /// A unary operation (For example: `!x`, `*x`)
32 Unary(UnOp, Box<Expr>),
33 /// A literal (For example: `1`, `"foo"`)
34 Lit(Lit),
35 /// A cast (`foo as f64`)
36 Cast(Box<Expr>, Box<Ty>),
David Tolnay939766a2016-09-23 23:48:12 -070037 /// Type ascription (`foo: f64`)
David Tolnayf4bbbd92016-09-23 14:41:55 -070038 Type(Box<Expr>, Box<Ty>),
39 /// An `if` block, with an optional else block
40 ///
41 /// `if expr { block } else { expr }`
David Tolnay89e05672016-10-02 14:39:42 -070042 If(Box<Expr>, Block, Option<Box<Expr>>),
David Tolnayf4bbbd92016-09-23 14:41:55 -070043 /// An `if let` expression with an optional else block
44 ///
45 /// `if let pat = expr { block } else { expr }`
46 ///
47 /// This is desugared to a `match` expression.
David Tolnay89e05672016-10-02 14:39:42 -070048 IfLet(Box<Pat>, Box<Expr>, Block, Option<Box<Expr>>),
David Tolnayf4bbbd92016-09-23 14:41:55 -070049 /// A while loop, with an optional label
50 ///
51 /// `'label: while expr { block }`
David Tolnay89e05672016-10-02 14:39:42 -070052 While(Box<Expr>, Block, Option<Ident>),
David Tolnayf4bbbd92016-09-23 14:41:55 -070053 /// A while-let loop, with an optional label
54 ///
55 /// `'label: while let pat = expr { block }`
56 ///
57 /// This is desugared to a combination of `loop` and `match` expressions.
David Tolnay89e05672016-10-02 14:39:42 -070058 WhileLet(Box<Pat>, Box<Expr>, Block, Option<Ident>),
David Tolnayf4bbbd92016-09-23 14:41:55 -070059 /// A for loop, with an optional label
60 ///
61 /// `'label: for pat in expr { block }`
62 ///
63 /// This is desugared to a combination of `loop` and `match` expressions.
David Tolnay89e05672016-10-02 14:39:42 -070064 ForLoop(Box<Pat>, Box<Expr>, Block, Option<Ident>),
David Tolnayf4bbbd92016-09-23 14:41:55 -070065 /// Conditionless loop (can be exited with break, continue, or return)
66 ///
67 /// `'label: loop { block }`
David Tolnay89e05672016-10-02 14:39:42 -070068 Loop(Block, Option<Ident>),
David Tolnayf4bbbd92016-09-23 14:41:55 -070069 /// A `match` block.
70 Match(Box<Expr>, Vec<Arm>),
71 /// A closure (for example, `move |a, b, c| {a + b + c}`)
David Tolnay89e05672016-10-02 14:39:42 -070072 Closure(CaptureBy, Box<FnDecl>, Block),
73 /// A block (`{ ... }` or `unsafe { ... }`)
74 Block(BlockCheckMode, Block),
David Tolnayf4bbbd92016-09-23 14:41:55 -070075
76 /// An assignment (`a = foo()`)
77 Assign(Box<Expr>, Box<Expr>),
78 /// An assignment with an operator
79 ///
80 /// For example, `a += 1`.
81 AssignOp(BinOp, Box<Expr>, Box<Expr>),
82 /// Access of a named struct field (`obj.foo`)
83 Field(Box<Expr>, Ident),
84 /// Access of an unnamed field of a struct or tuple-struct
85 ///
86 /// For example, `foo.0`.
87 TupField(Box<Expr>, usize),
88 /// An indexing operation (`foo[2]`)
89 Index(Box<Expr>, Box<Expr>),
90 /// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`)
91 Range(Option<Box<Expr>>, Option<Box<Expr>>, RangeLimits),
92
93 /// Variable reference, possibly containing `::` and/or type
94 /// parameters, e.g. foo::bar::<baz>.
95 ///
96 /// Optionally "qualified",
97 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
98 Path(Option<QSelf>, Path),
99
100 /// A referencing operation (`&a` or `&mut a`)
101 AddrOf(Mutability, Box<Expr>),
102 /// A `break`, with an optional label to break
103 Break(Option<Ident>),
104 /// A `continue`, with an optional label
105 Continue(Option<Ident>),
106 /// A `return`, with an optional value to be returned
107 Ret(Option<Box<Expr>>),
108
109 /// A macro invocation; pre-expansion
110 Mac(Mac),
111
112 /// A struct literal expression.
113 ///
114 /// For example, `Foo {x: 1, y: 2}`, or
115 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
116 Struct(Path, Vec<Field>, Option<Box<Expr>>),
117
118 /// An array literal constructed from one repeated element.
119 ///
120 /// For example, `[1; 5]`. The first expression is the element
121 /// to be repeated; the second is the number of times to repeat it.
122 Repeat(Box<Expr>, Box<Expr>),
123
124 /// No-op: used solely so we can pretty-print faithfully
125 Paren(Box<Expr>),
126
127 /// `expr?`
128 Try(Box<Expr>),
129}
130
131/// A Block (`{ .. }`).
132///
133/// E.g. `{ .. }` as in `fn foo() { .. }`
134#[derive(Debug, Clone, Eq, PartialEq)]
135pub struct Block {
136 /// Statements in a block
137 pub stmts: Vec<Stmt>,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700138}
139
140#[derive(Debug, Copy, Clone, Eq, PartialEq)]
141pub enum BlockCheckMode {
142 Default,
143 Unsafe,
144}
145
146#[derive(Debug, Clone, Eq, PartialEq)]
147pub enum Stmt {
148 /// A local (let) binding.
149 Local(Box<Local>),
150
151 /// An item definition.
152 Item(Box<Item>),
153
154 /// Expr without trailing semi-colon.
155 Expr(Box<Expr>),
156
157 Semi(Box<Expr>),
158
159 Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>),
160}
161
162#[derive(Debug, Copy, Clone, Eq, PartialEq)]
163pub enum MacStmtStyle {
164 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
165 /// `foo!(...);`, `foo![...];`
166 Semicolon,
167 /// The macro statement had braces; e.g. foo! { ... }
168 Braces,
169 /// The macro statement had parentheses or brackets and no semicolon; e.g.
170 /// `foo!(...)`. All of these will end up being converted into macro
171 /// expressions.
172 NoBraces,
173}
174
175/// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
176#[derive(Debug, Clone, Eq, PartialEq)]
177pub struct Local {
178 pub pat: Box<Pat>,
179 pub ty: Option<Box<Ty>>,
180 /// Initializer expression to set the value, if any
181 pub init: Option<Box<Expr>>,
182 pub attrs: Vec<Attribute>,
183}
184
185#[derive(Debug, Copy, Clone, Eq, PartialEq)]
186pub enum BinOp {
187 /// The `+` operator (addition)
188 Add,
189 /// The `-` operator (subtraction)
190 Sub,
191 /// The `*` operator (multiplication)
192 Mul,
193 /// The `/` operator (division)
194 Div,
195 /// The `%` operator (modulus)
196 Rem,
197 /// The `&&` operator (logical and)
198 And,
199 /// The `||` operator (logical or)
200 Or,
201 /// The `^` operator (bitwise xor)
202 BitXor,
203 /// The `&` operator (bitwise and)
204 BitAnd,
205 /// The `|` operator (bitwise or)
206 BitOr,
207 /// The `<<` operator (shift left)
208 Shl,
209 /// The `>>` operator (shift right)
210 Shr,
211 /// The `==` operator (equality)
212 Eq,
213 /// The `<` operator (less than)
214 Lt,
215 /// The `<=` operator (less than or equal to)
216 Le,
217 /// The `!=` operator (not equal to)
218 Ne,
219 /// The `>=` operator (greater than or equal to)
220 Ge,
221 /// The `>` operator (greater than)
222 Gt,
223}
224
225#[derive(Debug, Copy, Clone, Eq, PartialEq)]
226pub enum UnOp {
227 /// The `*` operator for dereferencing
228 Deref,
229 /// The `!` operator for logical inversion
230 Not,
231 /// The `-` operator for negation
232 Neg,
233}
234
235#[derive(Debug, Clone, Eq, PartialEq)]
236pub enum Pat {
237 /// Represents a wildcard pattern (`_`)
238 Wild,
239
David Tolnay432afc02016-09-24 07:37:13 -0700240 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700241 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
242 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
243 /// during name resolution.
244 Ident(BindingMode, Ident, Option<Box<Pat>>),
245
246 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
247 /// The `bool` is `true` in the presence of a `..`.
248 Struct(Path, Vec<FieldPat>, bool),
249
250 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
251 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
252 /// 0 <= position <= subpats.len()
253 TupleStruct(Path, Vec<Pat>, Option<usize>),
254
255 /// A possibly qualified path pattern.
256 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
257 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
258 /// only legally refer to associated constants.
259 Path(Option<QSelf>, Path),
260
261 /// A tuple pattern `(a, b)`.
262 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
263 /// 0 <= position <= subpats.len()
264 Tuple(Vec<Pat>, Option<usize>),
265 /// A `box` pattern
266 Box(Box<Pat>),
267 /// A reference pattern, e.g. `&mut (a, b)`
268 Ref(Box<Pat>, Mutability),
269 /// A literal
270 Lit(Box<Expr>),
271 /// A range pattern, e.g. `1...2`
272 Range(Box<Expr>, Box<Expr>),
273 /// `[a, b, ..i, y, z]` is represented as:
David Tolnay432afc02016-09-24 07:37:13 -0700274 /// `Pat::Vec(box [a, b], Some(i), box [y, z])`
David Tolnayf4bbbd92016-09-23 14:41:55 -0700275 Vec(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>),
276 /// A macro pattern; pre-expansion
277 Mac(Mac),
278}
279
David Tolnay771ecf42016-09-23 19:26:37 -0700280/// An arm of a 'match'.
281///
282/// E.g. `0...10 => { println!("match!") }` as in
283///
284/// ```rust,ignore
285/// match n {
286/// 0...10 => { println!("match!") },
287/// // ..
288/// }
289/// ```
David Tolnayf4bbbd92016-09-23 14:41:55 -0700290#[derive(Debug, Clone, Eq, PartialEq)]
291pub struct Arm {
292 pub attrs: Vec<Attribute>,
293 pub pats: Vec<Pat>,
294 pub guard: Option<Box<Expr>>,
295 pub body: Box<Expr>,
296}
297
298/// A capture clause
299#[derive(Debug, Copy, Clone, Eq, PartialEq)]
300pub enum CaptureBy {
301 Value,
302 Ref,
303}
304
305/// Limit types of a range (inclusive or exclusive)
306#[derive(Debug, Copy, Clone, Eq, PartialEq)]
307pub enum RangeLimits {
308 /// Inclusive at the beginning, exclusive at the end
309 HalfOpen,
310 /// Inclusive at the beginning and end
311 Closed,
312}
313
314/// A single field in a struct pattern
315///
316/// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay181bac52016-09-24 00:10:05 -0700317/// are treated the same as `x: x, y: ref y, z: ref mut z`,
David Tolnayaed77b02016-09-23 20:50:31 -0700318/// except `is_shorthand` is true
David Tolnayf4bbbd92016-09-23 14:41:55 -0700319#[derive(Debug, Clone, Eq, PartialEq)]
320pub struct FieldPat {
321 /// The identifier for the field
322 pub ident: Ident,
323 /// The pattern the field is destructured to
324 pub pat: Box<Pat>,
325 pub is_shorthand: bool,
326}
327
328#[derive(Debug, Copy, Clone, Eq, PartialEq)]
329pub enum BindingMode {
330 ByRef(Mutability),
331 ByValue(Mutability),
332}
333
David Tolnayb9c8e322016-09-23 20:48:37 -0700334#[cfg(feature = "parsing")]
335pub mod parsing {
336 use super::*;
David Tolnay9636c052016-10-02 17:11:17 -0700337 use {FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Ty};
David Tolnayb4ad3b52016-10-01 21:58:13 -0700338 use attr::parsing::outer_attr;
Gregory Katz1b69f682016-09-27 21:06:09 -0400339 use generics::parsing::lifetime;
David Tolnayfa0edf22016-09-23 22:58:24 -0700340 use ident::parsing::ident;
David Tolnay191e0582016-10-02 18:31:09 -0700341 use item::parsing::item;
David Tolnayfa0edf22016-09-23 22:58:24 -0700342 use lit::parsing::lit;
David Tolnay9636c052016-10-02 17:11:17 -0700343 use ty::parsing::{mutability, qpath, ty};
David Tolnayb9c8e322016-09-23 20:48:37 -0700344
David Tolnayfa0edf22016-09-23 22:58:24 -0700345 named!(pub expr -> Expr, do_parse!(
346 mut e: alt!(
David Tolnay89e05672016-10-02 14:39:42 -0700347 expr_paren
348 |
David Tolnay939766a2016-09-23 23:48:12 -0700349 expr_box
David Tolnayfa0edf22016-09-23 22:58:24 -0700350 |
David Tolnay939766a2016-09-23 23:48:12 -0700351 expr_vec
David Tolnayfa0edf22016-09-23 22:58:24 -0700352 |
David Tolnay939766a2016-09-23 23:48:12 -0700353 expr_tup
David Tolnayfa0edf22016-09-23 22:58:24 -0700354 |
David Tolnay939766a2016-09-23 23:48:12 -0700355 expr_unary
David Tolnayfa0edf22016-09-23 22:58:24 -0700356 |
David Tolnay939766a2016-09-23 23:48:12 -0700357 expr_lit
358 |
359 expr_if
David Tolnaya96a3fa2016-09-24 07:17:42 -0700360 // TODO: IfLet
Gregory Katz3e562cc2016-09-28 18:33:02 -0400361 |
362 expr_while
David Tolnaya96a3fa2016-09-24 07:17:42 -0700363 // TODO: WhileLet
364 // TODO: ForLoop
365 // TODO: Loop
366 // TODO: ForLoop
Gregory Katze5f35682016-09-27 14:20:55 -0400367 |
368 expr_loop
David Tolnayb4ad3b52016-10-01 21:58:13 -0700369 |
370 expr_match
David Tolnay89e05672016-10-02 14:39:42 -0700371 |
372 expr_closure
David Tolnay939766a2016-09-23 23:48:12 -0700373 |
374 expr_block
David Tolnay89e05672016-10-02 14:39:42 -0700375 |
376 expr_path
David Tolnay3c2467c2016-10-02 17:55:08 -0700377 |
378 expr_addr_of
Gregory Katzfd6935d2016-09-30 22:51:25 -0400379 |
380 expr_break
381 |
382 expr_continue
383 |
384 expr_ret
David Tolnaya96a3fa2016-09-24 07:17:42 -0700385 // TODO: Mac
386 // TODO: Struct
387 // TODO: Repeat
David Tolnayfa0edf22016-09-23 22:58:24 -0700388 ) >>
389 many0!(alt!(
David Tolnay939766a2016-09-23 23:48:12 -0700390 tap!(args: and_call => {
391 e = Expr::Call(Box::new(e), args);
David Tolnayfa0edf22016-09-23 22:58:24 -0700392 })
393 |
David Tolnay939766a2016-09-23 23:48:12 -0700394 tap!(more: and_method_call => {
395 let (method, ascript, mut args) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700396 args.insert(0, e);
397 e = Expr::MethodCall(method, ascript, args);
398 })
399 |
David Tolnay939766a2016-09-23 23:48:12 -0700400 tap!(more: and_binary => {
401 let (op, other) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700402 e = Expr::Binary(op, Box::new(e), Box::new(other));
403 })
David Tolnay939766a2016-09-23 23:48:12 -0700404 |
405 tap!(ty: and_cast => {
406 e = Expr::Cast(Box::new(e), Box::new(ty));
407 })
408 |
409 tap!(ty: and_ascription => {
410 e = Expr::Type(Box::new(e), Box::new(ty));
411 })
David Tolnaya96a3fa2016-09-24 07:17:42 -0700412 // TODO: Assign
413 // TODO: AssignOp
414 // TODO: Field
415 // TODO: TupField
416 // TODO: Index
417 // TODO: Range
418 // TODO: Try
David Tolnayfa0edf22016-09-23 22:58:24 -0700419 )) >>
420 (e)
David Tolnayb9c8e322016-09-23 20:48:37 -0700421 ));
422
David Tolnay89e05672016-10-02 14:39:42 -0700423 named!(expr_paren -> Expr, do_parse!(
424 punct!("(") >>
425 e: expr >>
426 punct!(")") >>
427 (Expr::Paren(Box::new(e)))
428 ));
429
David Tolnay939766a2016-09-23 23:48:12 -0700430 named!(expr_box -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700431 keyword!("box") >>
David Tolnayb9c8e322016-09-23 20:48:37 -0700432 inner: expr >>
433 (Expr::Box(Box::new(inner)))
434 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700435
David Tolnay939766a2016-09-23 23:48:12 -0700436 named!(expr_vec -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700437 punct!("[") >>
438 elems: separated_list!(punct!(","), expr) >>
439 punct!("]") >>
440 (Expr::Vec(elems))
441 ));
442
David Tolnay939766a2016-09-23 23:48:12 -0700443 named!(and_call -> Vec<Expr>, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700444 punct!("(") >>
445 args: separated_list!(punct!(","), expr) >>
446 punct!(")") >>
447 (args)
448 ));
449
David Tolnay939766a2016-09-23 23:48:12 -0700450 named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700451 punct!(".") >>
452 method: ident >>
453 ascript: opt_vec!(delimited!(
454 punct!("<"),
455 separated_list!(punct!(","), ty),
456 punct!(">")
457 )) >>
458 punct!("(") >>
459 args: separated_list!(punct!(","), expr) >>
460 punct!(")") >>
461 (method, ascript, args)
462 ));
463
David Tolnay939766a2016-09-23 23:48:12 -0700464 named!(expr_tup -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700465 punct!("(") >>
466 elems: separated_list!(punct!(","), expr) >>
David Tolnay89e05672016-10-02 14:39:42 -0700467 option!(punct!(",")) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700468 punct!(")") >>
469 (Expr::Tup(elems))
470 ));
471
David Tolnay939766a2016-09-23 23:48:12 -0700472 named!(and_binary -> (BinOp, Expr), tuple!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700473 alt!(
474 punct!("&&") => { |_| BinOp::And }
475 |
476 punct!("||") => { |_| BinOp::Or }
477 |
478 punct!("<<") => { |_| BinOp::Shl }
479 |
480 punct!(">>") => { |_| BinOp::Shr }
481 |
482 punct!("==") => { |_| BinOp::Eq }
483 |
484 punct!("<=") => { |_| BinOp::Le }
485 |
486 punct!("!=") => { |_| BinOp::Ne }
487 |
488 punct!(">=") => { |_| BinOp::Ge }
489 |
490 punct!("+") => { |_| BinOp::Add }
491 |
492 punct!("-") => { |_| BinOp::Sub }
493 |
494 punct!("*") => { |_| BinOp::Mul }
495 |
496 punct!("/") => { |_| BinOp::Div }
497 |
498 punct!("%") => { |_| BinOp::Rem }
499 |
500 punct!("^") => { |_| BinOp::BitXor }
501 |
502 punct!("&") => { |_| BinOp::BitAnd }
503 |
504 punct!("|") => { |_| BinOp::BitOr }
505 |
506 punct!("<") => { |_| BinOp::Lt }
507 |
508 punct!(">") => { |_| BinOp::Gt }
509 ),
510 expr
511 ));
512
David Tolnay939766a2016-09-23 23:48:12 -0700513 named!(expr_unary -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700514 operator: alt!(
515 punct!("*") => { |_| UnOp::Deref }
516 |
517 punct!("!") => { |_| UnOp::Not }
518 |
519 punct!("-") => { |_| UnOp::Neg }
520 ) >>
521 operand: expr >>
522 (Expr::Unary(operator, Box::new(operand)))
523 ));
David Tolnay939766a2016-09-23 23:48:12 -0700524
525 named!(expr_lit -> Expr, map!(lit, Expr::Lit));
526
527 named!(and_cast -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700528 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700529 ty: ty >>
530 (ty)
531 ));
532
533 named!(and_ascription -> Ty, preceded!(punct!(":"), ty));
534
535 named!(expr_if -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700536 keyword!("if") >>
David Tolnay939766a2016-09-23 23:48:12 -0700537 cond: expr >>
538 punct!("{") >>
539 then_block: within_block >>
540 punct!("}") >>
541 else_block: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700542 keyword!("else"),
David Tolnay939766a2016-09-23 23:48:12 -0700543 alt!(
544 expr_if
545 |
546 do_parse!(
547 punct!("{") >>
548 else_block: within_block >>
549 punct!("}") >>
David Tolnay89e05672016-10-02 14:39:42 -0700550 (Expr::Block(BlockCheckMode::Default, Block {
David Tolnay939766a2016-09-23 23:48:12 -0700551 stmts: else_block,
David Tolnay89e05672016-10-02 14:39:42 -0700552 }))
David Tolnay939766a2016-09-23 23:48:12 -0700553 )
554 )
555 )) >>
556 (Expr::If(
557 Box::new(cond),
David Tolnay89e05672016-10-02 14:39:42 -0700558 Block {
David Tolnay939766a2016-09-23 23:48:12 -0700559 stmts: then_block,
David Tolnay89e05672016-10-02 14:39:42 -0700560 },
David Tolnay939766a2016-09-23 23:48:12 -0700561 else_block.map(Box::new),
562 ))
563 ));
564
Gregory Katze5f35682016-09-27 14:20:55 -0400565 named!(expr_loop -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700566 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -0700567 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -0400568 loop_block: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700569 (Expr::Loop(loop_block, lbl))
Gregory Katze5f35682016-09-27 14:20:55 -0400570 ));
571
David Tolnayb4ad3b52016-10-01 21:58:13 -0700572 named!(expr_match -> Expr, do_parse!(
573 keyword!("match") >>
574 obj: expr >>
575 punct!("{") >>
576 arms: many0!(do_parse!(
577 attrs: many0!(outer_attr) >>
578 pats: separated_nonempty_list!(punct!("|"), pat) >>
579 guard: option!(preceded!(keyword!("if"), expr)) >>
580 punct!("=>") >>
581 body: alt!(
582 terminated!(expr, punct!(","))
583 |
David Tolnay89e05672016-10-02 14:39:42 -0700584 map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk))
David Tolnayb4ad3b52016-10-01 21:58:13 -0700585 ) >>
586 (Arm {
587 attrs: attrs,
588 pats: pats,
589 guard: guard.map(Box::new),
590 body: Box::new(body),
591 })
592 )) >>
593 punct!("}") >>
594 (Expr::Match(Box::new(obj), arms))
595 ));
596
David Tolnay89e05672016-10-02 14:39:42 -0700597 named!(expr_closure -> Expr, do_parse!(
598 capture: capture_by >>
599 punct!("|") >>
600 inputs: separated_list!(punct!(","), closure_arg) >>
601 punct!("|") >>
602 ret_and_body: alt!(
603 do_parse!(
604 punct!("->") >>
605 ty: ty >>
606 body: block >>
607 ((FunctionRetTy::Ty(ty), body))
608 )
609 |
610 map!(expr, |e| (
611 FunctionRetTy::Default,
612 Block {
613 stmts: vec![Stmt::Expr(Box::new(e))],
614 },
615 ))
616 ) >>
617 (Expr::Closure(
618 capture,
619 Box::new(FnDecl {
620 inputs: inputs,
621 output: ret_and_body.0,
622 }),
623 ret_and_body.1,
624 ))
625 ));
626
627 named!(closure_arg -> FnArg, do_parse!(
628 pat: pat >>
629 ty: option!(preceded!(punct!(":"), ty)) >>
630 (FnArg {
631 pat: pat,
632 ty: ty.unwrap_or(Ty::Infer),
633 })
634 ));
635
Gregory Katz3e562cc2016-09-28 18:33:02 -0400636 named!(expr_while -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700637 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -0700638 keyword!("while") >>
Gregory Katz3e562cc2016-09-28 18:33:02 -0400639 cond: expr >>
640 while_block: block >>
641 (Expr::While(
642 Box::new(cond),
David Tolnay89e05672016-10-02 14:39:42 -0700643 while_block,
David Tolnay8b07f372016-09-30 10:28:40 -0700644 lbl,
Gregory Katz3e562cc2016-09-28 18:33:02 -0400645 ))
646 ));
647
Gregory Katzfd6935d2016-09-30 22:51:25 -0400648 named!(expr_continue -> Expr, do_parse!(
649 keyword!("continue") >>
650 lbl: option!(label) >>
651 (Expr::Continue(
652 lbl,
653 ))
654 ));
655
656 named!(expr_break -> Expr, do_parse!(
657 keyword!("break") >>
658 lbl: option!(label) >>
659 (Expr::Break(
660 lbl,
661 ))
662 ));
663
664 named!(expr_ret -> Expr, do_parse!(
665 keyword!("return") >>
666 ret_value: option!(expr) >>
667 (Expr::Ret(
668 ret_value.map(Box::new),
669 ))
670 ));
671
David Tolnay42602292016-10-01 22:25:45 -0700672 named!(expr_block -> Expr, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700673 rules: block_check_mode >>
David Tolnay42602292016-10-01 22:25:45 -0700674 b: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700675 (Expr::Block(rules, Block {
David Tolnay42602292016-10-01 22:25:45 -0700676 stmts: b.stmts,
David Tolnay89e05672016-10-02 14:39:42 -0700677 }))
678 ));
679
David Tolnay9636c052016-10-02 17:11:17 -0700680 named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path)));
David Tolnay42602292016-10-01 22:25:45 -0700681
David Tolnay3c2467c2016-10-02 17:55:08 -0700682 named!(expr_addr_of -> Expr, do_parse!(
683 punct!("&") >>
684 mutability: mutability >>
685 expr: expr >>
686 (Expr::AddrOf(mutability, Box::new(expr)))
687 ));
688
David Tolnay42602292016-10-01 22:25:45 -0700689 named!(pub block -> Block, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700690 punct!("{") >>
691 stmts: within_block >>
692 punct!("}") >>
693 (Block {
694 stmts: stmts,
David Tolnay939766a2016-09-23 23:48:12 -0700695 })
696 ));
697
698 named!(block_check_mode -> BlockCheckMode, alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700699 keyword!("unsafe") => { |_| BlockCheckMode::Unsafe }
David Tolnay939766a2016-09-23 23:48:12 -0700700 |
701 epsilon!() => { |_| BlockCheckMode::Default }
702 ));
703
704 named!(within_block -> Vec<Stmt>, do_parse!(
David Tolnay181bac52016-09-24 00:10:05 -0700705 mut most: many0!(standalone_stmt) >>
David Tolnay939766a2016-09-23 23:48:12 -0700706 last: option!(expr) >>
707 (match last {
708 None => most,
709 Some(last) => {
David Tolnay939766a2016-09-23 23:48:12 -0700710 most.push(Stmt::Expr(Box::new(last)));
711 most
712 }
713 })
714 ));
715
716 named!(standalone_stmt -> Stmt, alt!(
David Tolnay191e0582016-10-02 18:31:09 -0700717 stmt_local
718 |
719 stmt_item
720 |
David Tolnay939766a2016-09-23 23:48:12 -0700721 stmt_semi
David Tolnaya96a3fa2016-09-24 07:17:42 -0700722 // TODO: mac
David Tolnay939766a2016-09-23 23:48:12 -0700723 ));
724
David Tolnay191e0582016-10-02 18:31:09 -0700725 named!(stmt_local -> Stmt, do_parse!(
726 attrs: many0!(outer_attr) >>
727 keyword!("let") >>
728 pat: pat >>
729 ty: option!(preceded!(punct!(":"), ty)) >>
730 init: option!(preceded!(punct!("="), expr)) >>
731 punct!(";") >>
732 (Stmt::Local(Box::new(Local {
733 pat: Box::new(pat),
734 ty: ty.map(Box::new),
735 init: init.map(Box::new),
736 attrs: attrs,
737 })))
738 ));
739
740 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
741
David Tolnay939766a2016-09-23 23:48:12 -0700742 named!(stmt_semi -> Stmt, do_parse!(
743 e: expr >>
744 punct!(";") >>
745 (Stmt::Semi(Box::new(e)))
746 ));
David Tolnay8b07f372016-09-30 10:28:40 -0700747
David Tolnay42602292016-10-01 22:25:45 -0700748 named!(pub pat -> Pat, alt!(
David Tolnayb4ad3b52016-10-01 21:58:13 -0700749 pat_wild
750 |
751 pat_ident
752 // TODO: Struct
753 // TODO: TupleStruct
David Tolnay9636c052016-10-02 17:11:17 -0700754 |
755 pat_path
David Tolnayb4ad3b52016-10-01 21:58:13 -0700756 // TODO: Tuple
757 // TODO: Box
758 // TODO: Ref
759 // TODO: Lit
760 // TODO: Range
761 // TODO: Vec
762 // TODO: Mac
763 ));
764
765 named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild));
766
767 named!(pat_ident -> Pat, do_parse!(
768 mode: option!(keyword!("ref")) >>
769 mutability: mutability >>
770 name: ident >>
771 subpat: option!(preceded!(punct!("@"), pat)) >>
772 (Pat::Ident(
773 if mode.is_some() {
774 BindingMode::ByRef(mutability)
775 } else {
776 BindingMode::ByValue(mutability)
777 },
778 name,
779 subpat.map(Box::new),
780 ))
781 ));
782
David Tolnay9636c052016-10-02 17:11:17 -0700783 named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path)));
784
David Tolnay89e05672016-10-02 14:39:42 -0700785 named!(capture_by -> CaptureBy, alt!(
786 keyword!("move") => { |_| CaptureBy::Value }
787 |
788 epsilon!() => { |_| CaptureBy::Ref }
789 ));
790
David Tolnay8b07f372016-09-30 10:28:40 -0700791 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -0700792}
793
David Tolnayf4bbbd92016-09-23 14:41:55 -0700794#[cfg(feature = "printing")]
795mod printing {
796 use super::*;
David Tolnay89e05672016-10-02 14:39:42 -0700797 use {FunctionRetTy, Mutability, Ty};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700798 use quote::{Tokens, ToTokens};
799
800 impl ToTokens for Expr {
801 fn to_tokens(&self, tokens: &mut Tokens) {
802 match *self {
David Tolnay47a877c2016-10-01 16:50:55 -0700803 Expr::Box(ref _inner) => unimplemented!(),
804 Expr::Vec(ref _inner) => unimplemented!(),
David Tolnay9636c052016-10-02 17:11:17 -0700805 Expr::Call(ref func, ref args) => {
806 func.to_tokens(tokens);
807 tokens.append("(");
808 tokens.append_separated(args, ",");
809 tokens.append(")");
810 }
811 Expr::MethodCall(ref ident, ref ascript, ref args) => {
812 args[0].to_tokens(tokens);
813 tokens.append(".");
814 ident.to_tokens(tokens);
815 if ascript.len() > 0 {
816 tokens.append("::");
817 tokens.append("<");
818 tokens.append_separated(ascript, ",");
819 tokens.append(">");
820 }
821 tokens.append("(");
822 tokens.append_separated(&args[1..], ",");
823 tokens.append(")");
824 }
David Tolnay47a877c2016-10-01 16:50:55 -0700825 Expr::Tup(ref fields) => {
826 tokens.append("(");
827 tokens.append_separated(fields, ",");
828 if fields.len() == 1 {
829 tokens.append(",");
830 }
831 tokens.append(")");
832 }
David Tolnay89e05672016-10-02 14:39:42 -0700833 Expr::Binary(op, ref left, ref right) => {
834 left.to_tokens(tokens);
835 op.to_tokens(tokens);
836 right.to_tokens(tokens);
837 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700838 Expr::Unary(op, ref expr) => {
839 op.to_tokens(tokens);
840 expr.to_tokens(tokens);
841 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700842 Expr::Lit(ref lit) => lit.to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -0700843 Expr::Cast(ref expr, ref ty) => {
844 expr.to_tokens(tokens);
845 tokens.append("as");
846 ty.to_tokens(tokens);
847 }
848 Expr::Type(ref expr, ref ty) => {
849 expr.to_tokens(tokens);
850 tokens.append(":");
851 ty.to_tokens(tokens);
852 }
853 Expr::If(ref cond, ref then_block, ref else_block) => {
854 tokens.append("if");
855 cond.to_tokens(tokens);
856 then_block.to_tokens(tokens);
857 if let Some(ref else_block) = *else_block {
858 tokens.append("else");
859 else_block.to_tokens(tokens);
860 }
861 }
David Tolnay47a877c2016-10-01 16:50:55 -0700862 Expr::IfLet(ref _pat, ref _expr, ref _then_block, ref _else_block) => unimplemented!(),
863 Expr::While(ref _cond, ref _body, ref _label) => unimplemented!(),
864 Expr::WhileLet(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
865 Expr::ForLoop(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
866 Expr::Loop(ref _body, ref _label) => unimplemented!(),
David Tolnayb4ad3b52016-10-01 21:58:13 -0700867 Expr::Match(ref expr, ref arms) => {
868 tokens.append("match");
869 expr.to_tokens(tokens);
870 tokens.append("{");
871 tokens.append_separated(arms, ",");
872 tokens.append("}");
873 }
David Tolnay89e05672016-10-02 14:39:42 -0700874 Expr::Closure(capture, ref decl, ref body) => {
875 capture.to_tokens(tokens);
876 tokens.append("|");
877 for (i, input) in decl.inputs.iter().enumerate() {
878 if i > 0 {
879 tokens.append(",");
880 }
881 input.pat.to_tokens(tokens);
882 match input.ty {
883 Ty::Infer => { /* nothing */ }
884 _ => {
885 tokens.append(":");
886 input.ty.to_tokens(tokens);
887 }
888 }
889 }
890 tokens.append("|");
891 match decl.output {
892 FunctionRetTy::Default => {
893 if body.stmts.len() == 1 {
894 if let Stmt::Expr(ref expr) = body.stmts[0] {
895 expr.to_tokens(tokens);
896 } else {
897 body.to_tokens(tokens);
898 }
899 } else {
900 body.to_tokens(tokens);
901 }
902 }
903 FunctionRetTy::Ty(ref ty) => {
904 tokens.append("->");
905 ty.to_tokens(tokens);
906 body.to_tokens(tokens);
907 }
908 }
909 }
910 Expr::Block(rules, ref block) => {
911 rules.to_tokens(tokens);
912 block.to_tokens(tokens);
913 }
David Tolnay47a877c2016-10-01 16:50:55 -0700914 Expr::Assign(ref _var, ref _expr) => unimplemented!(),
915 Expr::AssignOp(_op, ref _var, ref _expr) => unimplemented!(),
916 Expr::Field(ref _expr, ref _field) => unimplemented!(),
917 Expr::TupField(ref _expr, _field) => unimplemented!(),
918 Expr::Index(ref _expr, ref _index) => unimplemented!(),
919 Expr::Range(ref _from, ref _to, _limits) => unimplemented!(),
David Tolnay89e05672016-10-02 14:39:42 -0700920 Expr::Path(None, ref path) => {
921 path.to_tokens(tokens);
922 }
923 Expr::Path(Some(ref qself), ref path) => {
924 tokens.append("<");
925 qself.ty.to_tokens(tokens);
926 if qself.position > 0 {
927 tokens.append("as");
928 for (i, segment) in path.segments.iter()
929 .take(qself.position)
930 .enumerate()
931 {
932 if i > 0 || path.global {
933 tokens.append("::");
934 }
935 segment.to_tokens(tokens);
936 }
937 }
938 tokens.append(">");
939 for segment in path.segments.iter().skip(qself.position) {
940 tokens.append("::");
941 segment.to_tokens(tokens);
942 }
943 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700944 Expr::AddrOf(mutability, ref expr) => {
945 tokens.append("&");
946 mutability.to_tokens(tokens);
947 expr.to_tokens(tokens);
948 }
949 Expr::Break(ref opt_label) => {
950 tokens.append("break");
951 opt_label.to_tokens(tokens);
952 }
953 Expr::Continue(ref opt_label) => {
954 tokens.append("continue");
955 opt_label.to_tokens(tokens);
956 }
David Tolnay42602292016-10-01 22:25:45 -0700957 Expr::Ret(ref opt_expr) => {
958 tokens.append("return");
959 opt_expr.to_tokens(tokens);
960 }
David Tolnay47a877c2016-10-01 16:50:55 -0700961 Expr::Mac(ref _mac) => unimplemented!(),
962 Expr::Struct(ref _path, ref _fields, ref _base) => unimplemented!(),
963 Expr::Repeat(ref _expr, ref _times) => unimplemented!(),
David Tolnay89e05672016-10-02 14:39:42 -0700964 Expr::Paren(ref expr) => {
965 tokens.append("(");
966 expr.to_tokens(tokens);
967 tokens.append(")");
968 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700969 Expr::Try(ref expr) => {
970 expr.to_tokens(tokens);
971 tokens.append("?");
972 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700973 }
974 }
975 }
David Tolnayb4ad3b52016-10-01 21:58:13 -0700976
David Tolnay89e05672016-10-02 14:39:42 -0700977 impl ToTokens for BinOp {
978 fn to_tokens(&self, tokens: &mut Tokens) {
979 match *self {
980 BinOp::Add => tokens.append("+"),
981 BinOp::Sub => tokens.append("-"),
982 BinOp::Mul => tokens.append("*"),
983 BinOp::Div => tokens.append("/"),
984 BinOp::Rem => tokens.append("%"),
985 BinOp::And => tokens.append("&&"),
986 BinOp::Or => tokens.append("||"),
987 BinOp::BitXor => tokens.append("^"),
988 BinOp::BitAnd => tokens.append("&"),
989 BinOp::BitOr => tokens.append("|"),
990 BinOp::Shl => tokens.append("<<"),
991 BinOp::Shr => tokens.append(">>"),
992 BinOp::Eq => tokens.append("=="),
993 BinOp::Lt => tokens.append("<"),
994 BinOp::Le => tokens.append("<="),
995 BinOp::Ne => tokens.append("!="),
996 BinOp::Ge => tokens.append(">="),
997 BinOp::Gt => tokens.append(">"),
998 }
999 }
1000 }
1001
David Tolnay3c2467c2016-10-02 17:55:08 -07001002 impl ToTokens for UnOp {
1003 fn to_tokens(&self, tokens: &mut Tokens) {
1004 match *self {
1005 UnOp::Deref => tokens.append("*"),
1006 UnOp::Not => tokens.append("!"),
1007 UnOp::Neg => tokens.append("-"),
1008 }
1009 }
1010 }
1011
David Tolnayb4ad3b52016-10-01 21:58:13 -07001012 impl ToTokens for Arm {
1013 fn to_tokens(&self, tokens: &mut Tokens) {
1014 for attr in &self.attrs {
1015 attr.to_tokens(tokens);
1016 }
1017 tokens.append_separated(&self.pats, "|");
1018 if let Some(ref guard) = self.guard {
1019 tokens.append("if");
1020 guard.to_tokens(tokens);
1021 }
1022 tokens.append("=>");
1023 self.body.to_tokens(tokens);
1024 match *self.body {
David Tolnay89e05672016-10-02 14:39:42 -07001025 Expr::Block(_, _) => { /* no comma */ }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001026 _ => tokens.append(","),
1027 }
1028 }
1029 }
1030
1031 impl ToTokens for Pat {
1032 fn to_tokens(&self, tokens: &mut Tokens) {
1033 match *self {
1034 Pat::Wild => tokens.append("_"),
1035 Pat::Ident(mode, ref ident, ref subpat) => {
1036 mode.to_tokens(tokens);
1037 ident.to_tokens(tokens);
1038 if let Some(ref subpat) = *subpat {
1039 tokens.append("@");
1040 subpat.to_tokens(tokens);
1041 }
1042 }
1043 Pat::Struct(ref _path, ref _fields, _dots) => unimplemented!(),
1044 Pat::TupleStruct(ref _path, ref _pats, _dotpos) => unimplemented!(),
1045 Pat::Path(ref _qself, ref _path) => unimplemented!(),
1046 Pat::Tuple(ref _pats, _dotpos) => unimplemented!(),
1047 Pat::Box(ref _inner) => unimplemented!(),
1048 Pat::Ref(ref _target, _mutability) => unimplemented!(),
1049 Pat::Lit(ref _expr) => unimplemented!(),
1050 Pat::Range(ref _lower, ref _upper) => unimplemented!(),
1051 Pat::Vec(ref _before, ref _dots, ref _after) => unimplemented!(),
1052 Pat::Mac(ref _mac) => unimplemented!(),
1053 }
1054 }
1055 }
1056
1057 impl ToTokens for BindingMode {
1058 fn to_tokens(&self, tokens: &mut Tokens) {
1059 match *self {
1060 BindingMode::ByRef(Mutability::Immutable) => {
1061 tokens.append("ref");
1062 }
1063 BindingMode::ByRef(Mutability::Mutable) => {
1064 tokens.append("ref");
1065 tokens.append("mut");
1066 }
1067 BindingMode::ByValue(Mutability::Immutable) => {}
1068 BindingMode::ByValue(Mutability::Mutable) => {
1069 tokens.append("mut");
1070 }
1071 }
1072 }
1073 }
David Tolnay42602292016-10-01 22:25:45 -07001074
David Tolnay89e05672016-10-02 14:39:42 -07001075 impl ToTokens for CaptureBy {
1076 fn to_tokens(&self, tokens: &mut Tokens) {
1077 match *self {
1078 CaptureBy::Value => tokens.append("move"),
1079 CaptureBy::Ref => { /* nothing */ }
1080 }
1081 }
1082 }
1083
David Tolnay42602292016-10-01 22:25:45 -07001084 impl ToTokens for Block {
1085 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay42602292016-10-01 22:25:45 -07001086 tokens.append("{");
1087 for stmt in &self.stmts {
1088 stmt.to_tokens(tokens);
1089 }
1090 tokens.append("}");
1091 }
1092 }
1093
1094 impl ToTokens for BlockCheckMode {
1095 fn to_tokens(&self, tokens: &mut Tokens) {
1096 match *self {
David Tolnay89e05672016-10-02 14:39:42 -07001097 BlockCheckMode::Default => { /* nothing */ }
David Tolnay42602292016-10-01 22:25:45 -07001098 BlockCheckMode::Unsafe => tokens.append("unsafe"),
1099 }
1100 }
1101 }
1102
1103 impl ToTokens for Stmt {
1104 fn to_tokens(&self, tokens: &mut Tokens) {
1105 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07001106 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07001107 Stmt::Item(ref item) => item.to_tokens(tokens),
1108 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
1109 Stmt::Semi(ref expr) => {
1110 expr.to_tokens(tokens);
1111 tokens.append(";");
1112 }
1113 Stmt::Mac(ref _mac) => unimplemented!(),
1114 }
1115 }
1116 }
David Tolnay191e0582016-10-02 18:31:09 -07001117
1118 impl ToTokens for Local {
1119 fn to_tokens(&self, tokens: &mut Tokens) {
1120 tokens.append("let");
1121 self.pat.to_tokens(tokens);
1122 if let Some(ref ty) = self.ty {
1123 tokens.append(":");
1124 ty.to_tokens(tokens);
1125 }
1126 if let Some(ref init) = self.init {
1127 tokens.append("=");
1128 init.to_tokens(tokens);
1129 }
1130 tokens.append(";");
1131 }
1132 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001133}