blob: 55aa3f89425ca1a1c610b218e9a2954c9c4c4b8d [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>`.
David Tolnay055a7042016-10-02 19:23:54 -0700116 Struct(Path, Vec<FieldValue>, Option<Box<Expr>>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700117
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
David Tolnay055a7042016-10-02 19:23:54 -0700131#[derive(Debug, Clone, Eq, PartialEq)]
132pub struct FieldValue {
133 pub ident: Ident,
134 pub expr: Expr,
135}
136
David Tolnayf4bbbd92016-09-23 14:41:55 -0700137/// A Block (`{ .. }`).
138///
139/// E.g. `{ .. }` as in `fn foo() { .. }`
140#[derive(Debug, Clone, Eq, PartialEq)]
141pub struct Block {
142 /// Statements in a block
143 pub stmts: Vec<Stmt>,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700144}
145
146#[derive(Debug, Copy, Clone, Eq, PartialEq)]
147pub enum BlockCheckMode {
148 Default,
149 Unsafe,
150}
151
152#[derive(Debug, Clone, Eq, PartialEq)]
153pub enum Stmt {
154 /// A local (let) binding.
155 Local(Box<Local>),
156
157 /// An item definition.
158 Item(Box<Item>),
159
160 /// Expr without trailing semi-colon.
161 Expr(Box<Expr>),
162
163 Semi(Box<Expr>),
164
165 Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>),
166}
167
168#[derive(Debug, Copy, Clone, Eq, PartialEq)]
169pub enum MacStmtStyle {
170 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
171 /// `foo!(...);`, `foo![...];`
172 Semicolon,
173 /// The macro statement had braces; e.g. foo! { ... }
174 Braces,
175 /// The macro statement had parentheses or brackets and no semicolon; e.g.
176 /// `foo!(...)`. All of these will end up being converted into macro
177 /// expressions.
178 NoBraces,
179}
180
181/// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
182#[derive(Debug, Clone, Eq, PartialEq)]
183pub struct Local {
184 pub pat: Box<Pat>,
185 pub ty: Option<Box<Ty>>,
186 /// Initializer expression to set the value, if any
187 pub init: Option<Box<Expr>>,
188 pub attrs: Vec<Attribute>,
189}
190
191#[derive(Debug, Copy, Clone, Eq, PartialEq)]
192pub enum BinOp {
193 /// The `+` operator (addition)
194 Add,
195 /// The `-` operator (subtraction)
196 Sub,
197 /// The `*` operator (multiplication)
198 Mul,
199 /// The `/` operator (division)
200 Div,
201 /// The `%` operator (modulus)
202 Rem,
203 /// The `&&` operator (logical and)
204 And,
205 /// The `||` operator (logical or)
206 Or,
207 /// The `^` operator (bitwise xor)
208 BitXor,
209 /// The `&` operator (bitwise and)
210 BitAnd,
211 /// The `|` operator (bitwise or)
212 BitOr,
213 /// The `<<` operator (shift left)
214 Shl,
215 /// The `>>` operator (shift right)
216 Shr,
217 /// The `==` operator (equality)
218 Eq,
219 /// The `<` operator (less than)
220 Lt,
221 /// The `<=` operator (less than or equal to)
222 Le,
223 /// The `!=` operator (not equal to)
224 Ne,
225 /// The `>=` operator (greater than or equal to)
226 Ge,
227 /// The `>` operator (greater than)
228 Gt,
229}
230
231#[derive(Debug, Copy, Clone, Eq, PartialEq)]
232pub enum UnOp {
233 /// The `*` operator for dereferencing
234 Deref,
235 /// The `!` operator for logical inversion
236 Not,
237 /// The `-` operator for negation
238 Neg,
239}
240
241#[derive(Debug, Clone, Eq, PartialEq)]
242pub enum Pat {
243 /// Represents a wildcard pattern (`_`)
244 Wild,
245
David Tolnay432afc02016-09-24 07:37:13 -0700246 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700247 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
248 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
249 /// during name resolution.
250 Ident(BindingMode, Ident, Option<Box<Pat>>),
251
252 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
253 /// The `bool` is `true` in the presence of a `..`.
254 Struct(Path, Vec<FieldPat>, bool),
255
256 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
257 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
258 /// 0 <= position <= subpats.len()
259 TupleStruct(Path, Vec<Pat>, Option<usize>),
260
261 /// A possibly qualified path pattern.
262 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
263 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
264 /// only legally refer to associated constants.
265 Path(Option<QSelf>, Path),
266
267 /// A tuple pattern `(a, b)`.
268 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
269 /// 0 <= position <= subpats.len()
270 Tuple(Vec<Pat>, Option<usize>),
271 /// A `box` pattern
272 Box(Box<Pat>),
273 /// A reference pattern, e.g. `&mut (a, b)`
274 Ref(Box<Pat>, Mutability),
275 /// A literal
276 Lit(Box<Expr>),
277 /// A range pattern, e.g. `1...2`
278 Range(Box<Expr>, Box<Expr>),
279 /// `[a, b, ..i, y, z]` is represented as:
David Tolnay432afc02016-09-24 07:37:13 -0700280 /// `Pat::Vec(box [a, b], Some(i), box [y, z])`
David Tolnayf4bbbd92016-09-23 14:41:55 -0700281 Vec(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>),
282 /// A macro pattern; pre-expansion
283 Mac(Mac),
284}
285
David Tolnay771ecf42016-09-23 19:26:37 -0700286/// An arm of a 'match'.
287///
288/// E.g. `0...10 => { println!("match!") }` as in
289///
290/// ```rust,ignore
291/// match n {
292/// 0...10 => { println!("match!") },
293/// // ..
294/// }
295/// ```
David Tolnayf4bbbd92016-09-23 14:41:55 -0700296#[derive(Debug, Clone, Eq, PartialEq)]
297pub struct Arm {
298 pub attrs: Vec<Attribute>,
299 pub pats: Vec<Pat>,
300 pub guard: Option<Box<Expr>>,
301 pub body: Box<Expr>,
302}
303
304/// A capture clause
305#[derive(Debug, Copy, Clone, Eq, PartialEq)]
306pub enum CaptureBy {
307 Value,
308 Ref,
309}
310
311/// Limit types of a range (inclusive or exclusive)
312#[derive(Debug, Copy, Clone, Eq, PartialEq)]
313pub enum RangeLimits {
314 /// Inclusive at the beginning, exclusive at the end
315 HalfOpen,
316 /// Inclusive at the beginning and end
317 Closed,
318}
319
320/// A single field in a struct pattern
321///
322/// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay181bac52016-09-24 00:10:05 -0700323/// are treated the same as `x: x, y: ref y, z: ref mut z`,
David Tolnayaed77b02016-09-23 20:50:31 -0700324/// except `is_shorthand` is true
David Tolnayf4bbbd92016-09-23 14:41:55 -0700325#[derive(Debug, Clone, Eq, PartialEq)]
326pub struct FieldPat {
327 /// The identifier for the field
328 pub ident: Ident,
329 /// The pattern the field is destructured to
330 pub pat: Box<Pat>,
331 pub is_shorthand: bool,
332}
333
334#[derive(Debug, Copy, Clone, Eq, PartialEq)]
335pub enum BindingMode {
336 ByRef(Mutability),
337 ByValue(Mutability),
338}
339
David Tolnayb9c8e322016-09-23 20:48:37 -0700340#[cfg(feature = "parsing")]
341pub mod parsing {
342 use super::*;
David Tolnay9636c052016-10-02 17:11:17 -0700343 use {FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Ty};
David Tolnayb4ad3b52016-10-01 21:58:13 -0700344 use attr::parsing::outer_attr;
Gregory Katz1b69f682016-09-27 21:06:09 -0400345 use generics::parsing::lifetime;
David Tolnayfa0edf22016-09-23 22:58:24 -0700346 use ident::parsing::ident;
David Tolnay191e0582016-10-02 18:31:09 -0700347 use item::parsing::item;
David Tolnayfa0edf22016-09-23 22:58:24 -0700348 use lit::parsing::lit;
David Tolnay055a7042016-10-02 19:23:54 -0700349 use ty::parsing::{mutability, path, qpath, ty};
David Tolnayb9c8e322016-09-23 20:48:37 -0700350
David Tolnayfa0edf22016-09-23 22:58:24 -0700351 named!(pub expr -> Expr, do_parse!(
352 mut e: alt!(
David Tolnay055a7042016-10-02 19:23:54 -0700353 expr_lit // needs to be before expr_struct
354 |
355 expr_struct // needs to be before expr_path
356 |
357 expr_paren // needs to be before expr_tup
David Tolnay89e05672016-10-02 14:39:42 -0700358 |
David Tolnay939766a2016-09-23 23:48:12 -0700359 expr_box
David Tolnayfa0edf22016-09-23 22:58:24 -0700360 |
David Tolnay939766a2016-09-23 23:48:12 -0700361 expr_vec
David Tolnayfa0edf22016-09-23 22:58:24 -0700362 |
David Tolnay939766a2016-09-23 23:48:12 -0700363 expr_tup
David Tolnayfa0edf22016-09-23 22:58:24 -0700364 |
David Tolnay939766a2016-09-23 23:48:12 -0700365 expr_unary
David Tolnayfa0edf22016-09-23 22:58:24 -0700366 |
David Tolnay939766a2016-09-23 23:48:12 -0700367 expr_if
Gregory Katz3e562cc2016-09-28 18:33:02 -0400368 |
369 expr_while
David Tolnaya96a3fa2016-09-24 07:17:42 -0700370 // TODO: WhileLet
371 // TODO: ForLoop
372 // TODO: Loop
373 // TODO: ForLoop
Gregory Katze5f35682016-09-27 14:20:55 -0400374 |
375 expr_loop
David Tolnayb4ad3b52016-10-01 21:58:13 -0700376 |
377 expr_match
David Tolnay89e05672016-10-02 14:39:42 -0700378 |
379 expr_closure
David Tolnay939766a2016-09-23 23:48:12 -0700380 |
381 expr_block
David Tolnay89e05672016-10-02 14:39:42 -0700382 |
383 expr_path
David Tolnay3c2467c2016-10-02 17:55:08 -0700384 |
385 expr_addr_of
Gregory Katzfd6935d2016-09-30 22:51:25 -0400386 |
387 expr_break
388 |
389 expr_continue
390 |
391 expr_ret
David Tolnaya96a3fa2016-09-24 07:17:42 -0700392 // TODO: Mac
David Tolnay055a7042016-10-02 19:23:54 -0700393 |
394 expr_repeat
David Tolnayfa0edf22016-09-23 22:58:24 -0700395 ) >>
396 many0!(alt!(
David Tolnay939766a2016-09-23 23:48:12 -0700397 tap!(args: and_call => {
398 e = Expr::Call(Box::new(e), args);
David Tolnayfa0edf22016-09-23 22:58:24 -0700399 })
400 |
David Tolnay939766a2016-09-23 23:48:12 -0700401 tap!(more: and_method_call => {
402 let (method, ascript, mut args) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700403 args.insert(0, e);
404 e = Expr::MethodCall(method, ascript, args);
405 })
406 |
David Tolnay939766a2016-09-23 23:48:12 -0700407 tap!(more: and_binary => {
408 let (op, other) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700409 e = Expr::Binary(op, Box::new(e), Box::new(other));
410 })
David Tolnay939766a2016-09-23 23:48:12 -0700411 |
412 tap!(ty: and_cast => {
413 e = Expr::Cast(Box::new(e), Box::new(ty));
414 })
415 |
416 tap!(ty: and_ascription => {
417 e = Expr::Type(Box::new(e), Box::new(ty));
418 })
David Tolnaya96a3fa2016-09-24 07:17:42 -0700419 // TODO: Assign
420 // TODO: AssignOp
421 // TODO: Field
422 // TODO: TupField
423 // TODO: Index
424 // TODO: Range
David Tolnay055a7042016-10-02 19:23:54 -0700425 |
426 tap!(_try: punct!("?") => {
427 e = Expr::Try(Box::new(e));
428 })
David Tolnayfa0edf22016-09-23 22:58:24 -0700429 )) >>
430 (e)
David Tolnayb9c8e322016-09-23 20:48:37 -0700431 ));
432
David Tolnay89e05672016-10-02 14:39:42 -0700433 named!(expr_paren -> Expr, do_parse!(
434 punct!("(") >>
435 e: expr >>
436 punct!(")") >>
437 (Expr::Paren(Box::new(e)))
438 ));
439
David Tolnay939766a2016-09-23 23:48:12 -0700440 named!(expr_box -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700441 keyword!("box") >>
David Tolnayb9c8e322016-09-23 20:48:37 -0700442 inner: expr >>
443 (Expr::Box(Box::new(inner)))
444 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700445
David Tolnay939766a2016-09-23 23:48:12 -0700446 named!(expr_vec -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700447 punct!("[") >>
448 elems: separated_list!(punct!(","), expr) >>
449 punct!("]") >>
450 (Expr::Vec(elems))
451 ));
452
David Tolnay939766a2016-09-23 23:48:12 -0700453 named!(and_call -> Vec<Expr>, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700454 punct!("(") >>
455 args: separated_list!(punct!(","), expr) >>
456 punct!(")") >>
457 (args)
458 ));
459
David Tolnay939766a2016-09-23 23:48:12 -0700460 named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700461 punct!(".") >>
462 method: ident >>
463 ascript: opt_vec!(delimited!(
464 punct!("<"),
465 separated_list!(punct!(","), ty),
466 punct!(">")
467 )) >>
468 punct!("(") >>
469 args: separated_list!(punct!(","), expr) >>
470 punct!(")") >>
471 (method, ascript, args)
472 ));
473
David Tolnay939766a2016-09-23 23:48:12 -0700474 named!(expr_tup -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700475 punct!("(") >>
476 elems: separated_list!(punct!(","), expr) >>
David Tolnay89e05672016-10-02 14:39:42 -0700477 option!(punct!(",")) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700478 punct!(")") >>
479 (Expr::Tup(elems))
480 ));
481
David Tolnay939766a2016-09-23 23:48:12 -0700482 named!(and_binary -> (BinOp, Expr), tuple!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700483 alt!(
484 punct!("&&") => { |_| BinOp::And }
485 |
486 punct!("||") => { |_| BinOp::Or }
487 |
488 punct!("<<") => { |_| BinOp::Shl }
489 |
490 punct!(">>") => { |_| BinOp::Shr }
491 |
492 punct!("==") => { |_| BinOp::Eq }
493 |
494 punct!("<=") => { |_| BinOp::Le }
495 |
496 punct!("!=") => { |_| BinOp::Ne }
497 |
498 punct!(">=") => { |_| BinOp::Ge }
499 |
500 punct!("+") => { |_| BinOp::Add }
501 |
502 punct!("-") => { |_| BinOp::Sub }
503 |
504 punct!("*") => { |_| BinOp::Mul }
505 |
506 punct!("/") => { |_| BinOp::Div }
507 |
508 punct!("%") => { |_| BinOp::Rem }
509 |
510 punct!("^") => { |_| BinOp::BitXor }
511 |
512 punct!("&") => { |_| BinOp::BitAnd }
513 |
514 punct!("|") => { |_| BinOp::BitOr }
515 |
516 punct!("<") => { |_| BinOp::Lt }
517 |
518 punct!(">") => { |_| BinOp::Gt }
519 ),
520 expr
521 ));
522
David Tolnay939766a2016-09-23 23:48:12 -0700523 named!(expr_unary -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700524 operator: alt!(
525 punct!("*") => { |_| UnOp::Deref }
526 |
527 punct!("!") => { |_| UnOp::Not }
528 |
529 punct!("-") => { |_| UnOp::Neg }
530 ) >>
531 operand: expr >>
532 (Expr::Unary(operator, Box::new(operand)))
533 ));
David Tolnay939766a2016-09-23 23:48:12 -0700534
535 named!(expr_lit -> Expr, map!(lit, Expr::Lit));
536
537 named!(and_cast -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700538 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700539 ty: ty >>
540 (ty)
541 ));
542
543 named!(and_ascription -> Ty, preceded!(punct!(":"), ty));
544
David Tolnay29f9ce12016-10-02 20:58:40 -0700545 enum IfCond {
546 Let(Pat, Expr),
547 Expr(Expr),
548 }
549
David Tolnay939766a2016-09-23 23:48:12 -0700550 named!(expr_if -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700551 keyword!("if") >>
David Tolnay29f9ce12016-10-02 20:58:40 -0700552 cond: alt!(
553 do_parse!(
554 keyword!("let") >>
555 pat: pat >>
556 punct!("=") >>
557 value: expr >>
558 (IfCond::Let(pat, value))
559 )
560 |
561 map!(expr, IfCond::Expr)
562 ) >>
David Tolnay939766a2016-09-23 23:48:12 -0700563 punct!("{") >>
564 then_block: within_block >>
565 punct!("}") >>
566 else_block: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700567 keyword!("else"),
David Tolnay939766a2016-09-23 23:48:12 -0700568 alt!(
569 expr_if
570 |
571 do_parse!(
572 punct!("{") >>
573 else_block: within_block >>
574 punct!("}") >>
David Tolnay89e05672016-10-02 14:39:42 -0700575 (Expr::Block(BlockCheckMode::Default, Block {
David Tolnay939766a2016-09-23 23:48:12 -0700576 stmts: else_block,
David Tolnay89e05672016-10-02 14:39:42 -0700577 }))
David Tolnay939766a2016-09-23 23:48:12 -0700578 )
579 )
580 )) >>
David Tolnay29f9ce12016-10-02 20:58:40 -0700581 (match cond {
582 IfCond::Let(pat, expr) => Expr::IfLet(
583 Box::new(pat),
584 Box::new(expr),
585 Block {
586 stmts: then_block,
587 },
588 else_block.map(Box::new),
589 ),
590 IfCond::Expr(cond) => Expr::If(
591 Box::new(cond),
592 Block {
593 stmts: then_block,
594 },
595 else_block.map(Box::new),
596 ),
597 })
David Tolnay939766a2016-09-23 23:48:12 -0700598 ));
599
Gregory Katze5f35682016-09-27 14:20:55 -0400600 named!(expr_loop -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700601 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -0700602 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -0400603 loop_block: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700604 (Expr::Loop(loop_block, lbl))
Gregory Katze5f35682016-09-27 14:20:55 -0400605 ));
606
David Tolnayb4ad3b52016-10-01 21:58:13 -0700607 named!(expr_match -> Expr, do_parse!(
608 keyword!("match") >>
609 obj: expr >>
610 punct!("{") >>
611 arms: many0!(do_parse!(
612 attrs: many0!(outer_attr) >>
613 pats: separated_nonempty_list!(punct!("|"), pat) >>
614 guard: option!(preceded!(keyword!("if"), expr)) >>
615 punct!("=>") >>
616 body: alt!(
617 terminated!(expr, punct!(","))
618 |
David Tolnay89e05672016-10-02 14:39:42 -0700619 map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk))
David Tolnayb4ad3b52016-10-01 21:58:13 -0700620 ) >>
621 (Arm {
622 attrs: attrs,
623 pats: pats,
624 guard: guard.map(Box::new),
625 body: Box::new(body),
626 })
627 )) >>
628 punct!("}") >>
629 (Expr::Match(Box::new(obj), arms))
630 ));
631
David Tolnay89e05672016-10-02 14:39:42 -0700632 named!(expr_closure -> Expr, do_parse!(
633 capture: capture_by >>
634 punct!("|") >>
635 inputs: separated_list!(punct!(","), closure_arg) >>
636 punct!("|") >>
637 ret_and_body: alt!(
638 do_parse!(
639 punct!("->") >>
640 ty: ty >>
641 body: block >>
642 ((FunctionRetTy::Ty(ty), body))
643 )
644 |
645 map!(expr, |e| (
646 FunctionRetTy::Default,
647 Block {
648 stmts: vec![Stmt::Expr(Box::new(e))],
649 },
650 ))
651 ) >>
652 (Expr::Closure(
653 capture,
654 Box::new(FnDecl {
655 inputs: inputs,
656 output: ret_and_body.0,
657 }),
658 ret_and_body.1,
659 ))
660 ));
661
662 named!(closure_arg -> FnArg, do_parse!(
663 pat: pat >>
664 ty: option!(preceded!(punct!(":"), ty)) >>
665 (FnArg {
666 pat: pat,
667 ty: ty.unwrap_or(Ty::Infer),
668 })
669 ));
670
Gregory Katz3e562cc2016-09-28 18:33:02 -0400671 named!(expr_while -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700672 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -0700673 keyword!("while") >>
Gregory Katz3e562cc2016-09-28 18:33:02 -0400674 cond: expr >>
675 while_block: block >>
676 (Expr::While(
677 Box::new(cond),
David Tolnay89e05672016-10-02 14:39:42 -0700678 while_block,
David Tolnay8b07f372016-09-30 10:28:40 -0700679 lbl,
Gregory Katz3e562cc2016-09-28 18:33:02 -0400680 ))
681 ));
682
Gregory Katzfd6935d2016-09-30 22:51:25 -0400683 named!(expr_continue -> Expr, do_parse!(
684 keyword!("continue") >>
685 lbl: option!(label) >>
David Tolnay055a7042016-10-02 19:23:54 -0700686 (Expr::Continue(lbl))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400687 ));
688
689 named!(expr_break -> Expr, do_parse!(
690 keyword!("break") >>
691 lbl: option!(label) >>
David Tolnay055a7042016-10-02 19:23:54 -0700692 (Expr::Break(lbl))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400693 ));
694
695 named!(expr_ret -> Expr, do_parse!(
696 keyword!("return") >>
697 ret_value: option!(expr) >>
David Tolnay055a7042016-10-02 19:23:54 -0700698 (Expr::Ret(ret_value.map(Box::new)))
699 ));
700
701 named!(expr_struct -> Expr, do_parse!(
702 path: path >>
703 punct!("{") >>
704 fields: separated_list!(punct!(","), field_value) >>
705 base: option!(do_parse!(
706 cond!(!fields.is_empty(), punct!(",")) >>
707 punct!("..") >>
708 base: expr >>
709 (base)
710 )) >>
711 punct!("}") >>
712 (Expr::Struct(path, fields, base.map(Box::new)))
713 ));
714
715 named!(field_value -> FieldValue, do_parse!(
716 name: ident >>
717 punct!(":") >>
718 value: expr >>
719 (FieldValue {
720 ident: name,
721 expr: value,
722 })
723 ));
724
725 named!(expr_repeat -> Expr, do_parse!(
726 punct!("[") >>
727 value: expr >>
728 punct!(";") >>
729 times: expr >>
730 punct!("]") >>
731 (Expr::Repeat(Box::new(value), Box::new(times)))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400732 ));
733
David Tolnay42602292016-10-01 22:25:45 -0700734 named!(expr_block -> Expr, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700735 rules: block_check_mode >>
David Tolnay42602292016-10-01 22:25:45 -0700736 b: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700737 (Expr::Block(rules, Block {
David Tolnay42602292016-10-01 22:25:45 -0700738 stmts: b.stmts,
David Tolnay89e05672016-10-02 14:39:42 -0700739 }))
740 ));
741
David Tolnay9636c052016-10-02 17:11:17 -0700742 named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path)));
David Tolnay42602292016-10-01 22:25:45 -0700743
David Tolnay3c2467c2016-10-02 17:55:08 -0700744 named!(expr_addr_of -> Expr, do_parse!(
745 punct!("&") >>
746 mutability: mutability >>
747 expr: expr >>
748 (Expr::AddrOf(mutability, Box::new(expr)))
749 ));
750
David Tolnay42602292016-10-01 22:25:45 -0700751 named!(pub block -> Block, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700752 punct!("{") >>
753 stmts: within_block >>
754 punct!("}") >>
755 (Block {
756 stmts: stmts,
David Tolnay939766a2016-09-23 23:48:12 -0700757 })
758 ));
759
760 named!(block_check_mode -> BlockCheckMode, alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700761 keyword!("unsafe") => { |_| BlockCheckMode::Unsafe }
David Tolnay939766a2016-09-23 23:48:12 -0700762 |
763 epsilon!() => { |_| BlockCheckMode::Default }
764 ));
765
766 named!(within_block -> Vec<Stmt>, do_parse!(
David Tolnay181bac52016-09-24 00:10:05 -0700767 mut most: many0!(standalone_stmt) >>
David Tolnay939766a2016-09-23 23:48:12 -0700768 last: option!(expr) >>
769 (match last {
770 None => most,
771 Some(last) => {
David Tolnay939766a2016-09-23 23:48:12 -0700772 most.push(Stmt::Expr(Box::new(last)));
773 most
774 }
775 })
776 ));
777
778 named!(standalone_stmt -> Stmt, alt!(
David Tolnay191e0582016-10-02 18:31:09 -0700779 stmt_local
780 |
781 stmt_item
782 |
David Tolnay939766a2016-09-23 23:48:12 -0700783 stmt_semi
David Tolnaya96a3fa2016-09-24 07:17:42 -0700784 // TODO: mac
David Tolnay939766a2016-09-23 23:48:12 -0700785 ));
786
David Tolnay191e0582016-10-02 18:31:09 -0700787 named!(stmt_local -> Stmt, do_parse!(
788 attrs: many0!(outer_attr) >>
789 keyword!("let") >>
790 pat: pat >>
791 ty: option!(preceded!(punct!(":"), ty)) >>
792 init: option!(preceded!(punct!("="), expr)) >>
793 punct!(";") >>
794 (Stmt::Local(Box::new(Local {
795 pat: Box::new(pat),
796 ty: ty.map(Box::new),
797 init: init.map(Box::new),
798 attrs: attrs,
799 })))
800 ));
801
802 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
803
David Tolnay939766a2016-09-23 23:48:12 -0700804 named!(stmt_semi -> Stmt, do_parse!(
805 e: expr >>
806 punct!(";") >>
807 (Stmt::Semi(Box::new(e)))
808 ));
David Tolnay8b07f372016-09-30 10:28:40 -0700809
David Tolnay42602292016-10-01 22:25:45 -0700810 named!(pub pat -> Pat, alt!(
David Tolnayb4ad3b52016-10-01 21:58:13 -0700811 pat_wild
812 |
813 pat_ident
814 // TODO: Struct
815 // TODO: TupleStruct
David Tolnay9636c052016-10-02 17:11:17 -0700816 |
817 pat_path
David Tolnayb4ad3b52016-10-01 21:58:13 -0700818 // TODO: Tuple
819 // TODO: Box
820 // TODO: Ref
821 // TODO: Lit
822 // TODO: Range
823 // TODO: Vec
824 // TODO: Mac
825 ));
826
827 named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild));
828
829 named!(pat_ident -> Pat, do_parse!(
830 mode: option!(keyword!("ref")) >>
831 mutability: mutability >>
832 name: ident >>
833 subpat: option!(preceded!(punct!("@"), pat)) >>
834 (Pat::Ident(
835 if mode.is_some() {
836 BindingMode::ByRef(mutability)
837 } else {
838 BindingMode::ByValue(mutability)
839 },
840 name,
841 subpat.map(Box::new),
842 ))
843 ));
844
David Tolnay9636c052016-10-02 17:11:17 -0700845 named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path)));
846
David Tolnay89e05672016-10-02 14:39:42 -0700847 named!(capture_by -> CaptureBy, alt!(
848 keyword!("move") => { |_| CaptureBy::Value }
849 |
850 epsilon!() => { |_| CaptureBy::Ref }
851 ));
852
David Tolnay8b07f372016-09-30 10:28:40 -0700853 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -0700854}
855
David Tolnayf4bbbd92016-09-23 14:41:55 -0700856#[cfg(feature = "printing")]
857mod printing {
858 use super::*;
David Tolnay89e05672016-10-02 14:39:42 -0700859 use {FunctionRetTy, Mutability, Ty};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700860 use quote::{Tokens, ToTokens};
861
862 impl ToTokens for Expr {
863 fn to_tokens(&self, tokens: &mut Tokens) {
864 match *self {
David Tolnay47a877c2016-10-01 16:50:55 -0700865 Expr::Box(ref _inner) => unimplemented!(),
866 Expr::Vec(ref _inner) => unimplemented!(),
David Tolnay9636c052016-10-02 17:11:17 -0700867 Expr::Call(ref func, ref args) => {
868 func.to_tokens(tokens);
869 tokens.append("(");
870 tokens.append_separated(args, ",");
871 tokens.append(")");
872 }
873 Expr::MethodCall(ref ident, ref ascript, ref args) => {
874 args[0].to_tokens(tokens);
875 tokens.append(".");
876 ident.to_tokens(tokens);
877 if ascript.len() > 0 {
878 tokens.append("::");
879 tokens.append("<");
880 tokens.append_separated(ascript, ",");
881 tokens.append(">");
882 }
883 tokens.append("(");
884 tokens.append_separated(&args[1..], ",");
885 tokens.append(")");
886 }
David Tolnay47a877c2016-10-01 16:50:55 -0700887 Expr::Tup(ref fields) => {
888 tokens.append("(");
889 tokens.append_separated(fields, ",");
890 if fields.len() == 1 {
891 tokens.append(",");
892 }
893 tokens.append(")");
894 }
David Tolnay89e05672016-10-02 14:39:42 -0700895 Expr::Binary(op, ref left, ref right) => {
896 left.to_tokens(tokens);
897 op.to_tokens(tokens);
898 right.to_tokens(tokens);
899 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700900 Expr::Unary(op, ref expr) => {
901 op.to_tokens(tokens);
902 expr.to_tokens(tokens);
903 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700904 Expr::Lit(ref lit) => lit.to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -0700905 Expr::Cast(ref expr, ref ty) => {
906 expr.to_tokens(tokens);
907 tokens.append("as");
908 ty.to_tokens(tokens);
909 }
910 Expr::Type(ref expr, ref ty) => {
911 expr.to_tokens(tokens);
912 tokens.append(":");
913 ty.to_tokens(tokens);
914 }
915 Expr::If(ref cond, ref then_block, ref else_block) => {
916 tokens.append("if");
917 cond.to_tokens(tokens);
918 then_block.to_tokens(tokens);
919 if let Some(ref else_block) = *else_block {
920 tokens.append("else");
921 else_block.to_tokens(tokens);
922 }
923 }
David Tolnay29f9ce12016-10-02 20:58:40 -0700924 Expr::IfLet(ref pat, ref expr, ref then_block, ref else_block) => {
925 tokens.append("if");
926 tokens.append("let");
927 pat.to_tokens(tokens);
928 tokens.append("=");
929 expr.to_tokens(tokens);
930 then_block.to_tokens(tokens);
931 if let Some(ref else_block) = *else_block {
932 tokens.append("else");
933 else_block.to_tokens(tokens);
934 }
935 }
David Tolnay47a877c2016-10-01 16:50:55 -0700936 Expr::While(ref _cond, ref _body, ref _label) => unimplemented!(),
937 Expr::WhileLet(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
938 Expr::ForLoop(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
939 Expr::Loop(ref _body, ref _label) => unimplemented!(),
David Tolnayb4ad3b52016-10-01 21:58:13 -0700940 Expr::Match(ref expr, ref arms) => {
941 tokens.append("match");
942 expr.to_tokens(tokens);
943 tokens.append("{");
944 tokens.append_separated(arms, ",");
945 tokens.append("}");
946 }
David Tolnay89e05672016-10-02 14:39:42 -0700947 Expr::Closure(capture, ref decl, ref body) => {
948 capture.to_tokens(tokens);
949 tokens.append("|");
950 for (i, input) in decl.inputs.iter().enumerate() {
951 if i > 0 {
952 tokens.append(",");
953 }
954 input.pat.to_tokens(tokens);
955 match input.ty {
956 Ty::Infer => { /* nothing */ }
957 _ => {
958 tokens.append(":");
959 input.ty.to_tokens(tokens);
960 }
961 }
962 }
963 tokens.append("|");
964 match decl.output {
965 FunctionRetTy::Default => {
966 if body.stmts.len() == 1 {
967 if let Stmt::Expr(ref expr) = body.stmts[0] {
968 expr.to_tokens(tokens);
969 } else {
970 body.to_tokens(tokens);
971 }
972 } else {
973 body.to_tokens(tokens);
974 }
975 }
976 FunctionRetTy::Ty(ref ty) => {
977 tokens.append("->");
978 ty.to_tokens(tokens);
979 body.to_tokens(tokens);
980 }
981 }
982 }
983 Expr::Block(rules, ref block) => {
984 rules.to_tokens(tokens);
985 block.to_tokens(tokens);
986 }
David Tolnay47a877c2016-10-01 16:50:55 -0700987 Expr::Assign(ref _var, ref _expr) => unimplemented!(),
988 Expr::AssignOp(_op, ref _var, ref _expr) => unimplemented!(),
989 Expr::Field(ref _expr, ref _field) => unimplemented!(),
990 Expr::TupField(ref _expr, _field) => unimplemented!(),
991 Expr::Index(ref _expr, ref _index) => unimplemented!(),
992 Expr::Range(ref _from, ref _to, _limits) => unimplemented!(),
David Tolnay89e05672016-10-02 14:39:42 -0700993 Expr::Path(None, ref path) => {
994 path.to_tokens(tokens);
995 }
996 Expr::Path(Some(ref qself), ref path) => {
997 tokens.append("<");
998 qself.ty.to_tokens(tokens);
999 if qself.position > 0 {
1000 tokens.append("as");
1001 for (i, segment) in path.segments.iter()
1002 .take(qself.position)
1003 .enumerate()
1004 {
1005 if i > 0 || path.global {
1006 tokens.append("::");
1007 }
1008 segment.to_tokens(tokens);
1009 }
1010 }
1011 tokens.append(">");
1012 for segment in path.segments.iter().skip(qself.position) {
1013 tokens.append("::");
1014 segment.to_tokens(tokens);
1015 }
1016 }
David Tolnay3c2467c2016-10-02 17:55:08 -07001017 Expr::AddrOf(mutability, ref expr) => {
1018 tokens.append("&");
1019 mutability.to_tokens(tokens);
1020 expr.to_tokens(tokens);
1021 }
1022 Expr::Break(ref opt_label) => {
1023 tokens.append("break");
1024 opt_label.to_tokens(tokens);
1025 }
1026 Expr::Continue(ref opt_label) => {
1027 tokens.append("continue");
1028 opt_label.to_tokens(tokens);
1029 }
David Tolnay42602292016-10-01 22:25:45 -07001030 Expr::Ret(ref opt_expr) => {
1031 tokens.append("return");
1032 opt_expr.to_tokens(tokens);
1033 }
David Tolnay47a877c2016-10-01 16:50:55 -07001034 Expr::Mac(ref _mac) => unimplemented!(),
David Tolnay055a7042016-10-02 19:23:54 -07001035 Expr::Struct(ref path, ref fields, ref base) => {
1036 path.to_tokens(tokens);
1037 tokens.append("{");
1038 tokens.append_separated(fields, ",");
1039 if let Some(ref base) = *base {
1040 if !fields.is_empty() {
1041 tokens.append(",");
1042 }
1043 tokens.append("..");
1044 base.to_tokens(tokens);
1045 }
1046 tokens.append("}");
1047 }
1048 Expr::Repeat(ref expr, ref times) => {
1049 tokens.append("[");
1050 expr.to_tokens(tokens);
1051 tokens.append(";");
1052 times.to_tokens(tokens);
1053 tokens.append("]");
1054 }
David Tolnay89e05672016-10-02 14:39:42 -07001055 Expr::Paren(ref expr) => {
1056 tokens.append("(");
1057 expr.to_tokens(tokens);
1058 tokens.append(")");
1059 }
David Tolnay3c2467c2016-10-02 17:55:08 -07001060 Expr::Try(ref expr) => {
1061 expr.to_tokens(tokens);
1062 tokens.append("?");
1063 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001064 }
1065 }
1066 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001067
David Tolnay89e05672016-10-02 14:39:42 -07001068 impl ToTokens for BinOp {
1069 fn to_tokens(&self, tokens: &mut Tokens) {
1070 match *self {
1071 BinOp::Add => tokens.append("+"),
1072 BinOp::Sub => tokens.append("-"),
1073 BinOp::Mul => tokens.append("*"),
1074 BinOp::Div => tokens.append("/"),
1075 BinOp::Rem => tokens.append("%"),
1076 BinOp::And => tokens.append("&&"),
1077 BinOp::Or => tokens.append("||"),
1078 BinOp::BitXor => tokens.append("^"),
1079 BinOp::BitAnd => tokens.append("&"),
1080 BinOp::BitOr => tokens.append("|"),
1081 BinOp::Shl => tokens.append("<<"),
1082 BinOp::Shr => tokens.append(">>"),
1083 BinOp::Eq => tokens.append("=="),
1084 BinOp::Lt => tokens.append("<"),
1085 BinOp::Le => tokens.append("<="),
1086 BinOp::Ne => tokens.append("!="),
1087 BinOp::Ge => tokens.append(">="),
1088 BinOp::Gt => tokens.append(">"),
1089 }
1090 }
1091 }
1092
David Tolnay3c2467c2016-10-02 17:55:08 -07001093 impl ToTokens for UnOp {
1094 fn to_tokens(&self, tokens: &mut Tokens) {
1095 match *self {
1096 UnOp::Deref => tokens.append("*"),
1097 UnOp::Not => tokens.append("!"),
1098 UnOp::Neg => tokens.append("-"),
1099 }
1100 }
1101 }
1102
David Tolnay055a7042016-10-02 19:23:54 -07001103 impl ToTokens for FieldValue {
1104 fn to_tokens(&self, tokens: &mut Tokens) {
1105 self.ident.to_tokens(tokens);
1106 tokens.append(":");
1107 self.expr.to_tokens(tokens);
1108 }
1109 }
1110
David Tolnayb4ad3b52016-10-01 21:58:13 -07001111 impl ToTokens for Arm {
1112 fn to_tokens(&self, tokens: &mut Tokens) {
1113 for attr in &self.attrs {
1114 attr.to_tokens(tokens);
1115 }
1116 tokens.append_separated(&self.pats, "|");
1117 if let Some(ref guard) = self.guard {
1118 tokens.append("if");
1119 guard.to_tokens(tokens);
1120 }
1121 tokens.append("=>");
1122 self.body.to_tokens(tokens);
1123 match *self.body {
David Tolnay89e05672016-10-02 14:39:42 -07001124 Expr::Block(_, _) => { /* no comma */ }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001125 _ => tokens.append(","),
1126 }
1127 }
1128 }
1129
1130 impl ToTokens for Pat {
1131 fn to_tokens(&self, tokens: &mut Tokens) {
1132 match *self {
1133 Pat::Wild => tokens.append("_"),
1134 Pat::Ident(mode, ref ident, ref subpat) => {
1135 mode.to_tokens(tokens);
1136 ident.to_tokens(tokens);
1137 if let Some(ref subpat) = *subpat {
1138 tokens.append("@");
1139 subpat.to_tokens(tokens);
1140 }
1141 }
1142 Pat::Struct(ref _path, ref _fields, _dots) => unimplemented!(),
1143 Pat::TupleStruct(ref _path, ref _pats, _dotpos) => unimplemented!(),
1144 Pat::Path(ref _qself, ref _path) => unimplemented!(),
1145 Pat::Tuple(ref _pats, _dotpos) => unimplemented!(),
1146 Pat::Box(ref _inner) => unimplemented!(),
1147 Pat::Ref(ref _target, _mutability) => unimplemented!(),
1148 Pat::Lit(ref _expr) => unimplemented!(),
1149 Pat::Range(ref _lower, ref _upper) => unimplemented!(),
1150 Pat::Vec(ref _before, ref _dots, ref _after) => unimplemented!(),
1151 Pat::Mac(ref _mac) => unimplemented!(),
1152 }
1153 }
1154 }
1155
1156 impl ToTokens for BindingMode {
1157 fn to_tokens(&self, tokens: &mut Tokens) {
1158 match *self {
1159 BindingMode::ByRef(Mutability::Immutable) => {
1160 tokens.append("ref");
1161 }
1162 BindingMode::ByRef(Mutability::Mutable) => {
1163 tokens.append("ref");
1164 tokens.append("mut");
1165 }
1166 BindingMode::ByValue(Mutability::Immutable) => {}
1167 BindingMode::ByValue(Mutability::Mutable) => {
1168 tokens.append("mut");
1169 }
1170 }
1171 }
1172 }
David Tolnay42602292016-10-01 22:25:45 -07001173
David Tolnay89e05672016-10-02 14:39:42 -07001174 impl ToTokens for CaptureBy {
1175 fn to_tokens(&self, tokens: &mut Tokens) {
1176 match *self {
1177 CaptureBy::Value => tokens.append("move"),
1178 CaptureBy::Ref => { /* nothing */ }
1179 }
1180 }
1181 }
1182
David Tolnay42602292016-10-01 22:25:45 -07001183 impl ToTokens for Block {
1184 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay42602292016-10-01 22:25:45 -07001185 tokens.append("{");
1186 for stmt in &self.stmts {
1187 stmt.to_tokens(tokens);
1188 }
1189 tokens.append("}");
1190 }
1191 }
1192
1193 impl ToTokens for BlockCheckMode {
1194 fn to_tokens(&self, tokens: &mut Tokens) {
1195 match *self {
David Tolnay89e05672016-10-02 14:39:42 -07001196 BlockCheckMode::Default => { /* nothing */ }
David Tolnay42602292016-10-01 22:25:45 -07001197 BlockCheckMode::Unsafe => tokens.append("unsafe"),
1198 }
1199 }
1200 }
1201
1202 impl ToTokens for Stmt {
1203 fn to_tokens(&self, tokens: &mut Tokens) {
1204 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07001205 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07001206 Stmt::Item(ref item) => item.to_tokens(tokens),
1207 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
1208 Stmt::Semi(ref expr) => {
1209 expr.to_tokens(tokens);
1210 tokens.append(";");
1211 }
1212 Stmt::Mac(ref _mac) => unimplemented!(),
1213 }
1214 }
1215 }
David Tolnay191e0582016-10-02 18:31:09 -07001216
1217 impl ToTokens for Local {
1218 fn to_tokens(&self, tokens: &mut Tokens) {
1219 tokens.append("let");
1220 self.pat.to_tokens(tokens);
1221 if let Some(ref ty) = self.ty {
1222 tokens.append(":");
1223 ty.to_tokens(tokens);
1224 }
1225 if let Some(ref init) = self.init {
1226 tokens.append("=");
1227 init.to_tokens(tokens);
1228 }
1229 tokens.append(";");
1230 }
1231 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001232}