blob: 6adc26faecd727f629ab528470fdc4fe9c09dbfb [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
David Tolnaya96a3fa2016-09-24 07:17:42 -0700368 // TODO: IfLet
Gregory Katz3e562cc2016-09-28 18:33:02 -0400369 |
370 expr_while
David Tolnaya96a3fa2016-09-24 07:17:42 -0700371 // TODO: WhileLet
372 // TODO: ForLoop
373 // TODO: Loop
374 // TODO: ForLoop
Gregory Katze5f35682016-09-27 14:20:55 -0400375 |
376 expr_loop
David Tolnayb4ad3b52016-10-01 21:58:13 -0700377 |
378 expr_match
David Tolnay89e05672016-10-02 14:39:42 -0700379 |
380 expr_closure
David Tolnay939766a2016-09-23 23:48:12 -0700381 |
382 expr_block
David Tolnay89e05672016-10-02 14:39:42 -0700383 |
384 expr_path
David Tolnay3c2467c2016-10-02 17:55:08 -0700385 |
386 expr_addr_of
Gregory Katzfd6935d2016-09-30 22:51:25 -0400387 |
388 expr_break
389 |
390 expr_continue
391 |
392 expr_ret
David Tolnaya96a3fa2016-09-24 07:17:42 -0700393 // TODO: Mac
David Tolnay055a7042016-10-02 19:23:54 -0700394 |
395 expr_repeat
David Tolnayfa0edf22016-09-23 22:58:24 -0700396 ) >>
397 many0!(alt!(
David Tolnay939766a2016-09-23 23:48:12 -0700398 tap!(args: and_call => {
399 e = Expr::Call(Box::new(e), args);
David Tolnayfa0edf22016-09-23 22:58:24 -0700400 })
401 |
David Tolnay939766a2016-09-23 23:48:12 -0700402 tap!(more: and_method_call => {
403 let (method, ascript, mut args) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700404 args.insert(0, e);
405 e = Expr::MethodCall(method, ascript, args);
406 })
407 |
David Tolnay939766a2016-09-23 23:48:12 -0700408 tap!(more: and_binary => {
409 let (op, other) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700410 e = Expr::Binary(op, Box::new(e), Box::new(other));
411 })
David Tolnay939766a2016-09-23 23:48:12 -0700412 |
413 tap!(ty: and_cast => {
414 e = Expr::Cast(Box::new(e), Box::new(ty));
415 })
416 |
417 tap!(ty: and_ascription => {
418 e = Expr::Type(Box::new(e), Box::new(ty));
419 })
David Tolnaya96a3fa2016-09-24 07:17:42 -0700420 // TODO: Assign
421 // TODO: AssignOp
422 // TODO: Field
423 // TODO: TupField
424 // TODO: Index
425 // TODO: Range
David Tolnay055a7042016-10-02 19:23:54 -0700426 |
427 tap!(_try: punct!("?") => {
428 e = Expr::Try(Box::new(e));
429 })
David Tolnayfa0edf22016-09-23 22:58:24 -0700430 )) >>
431 (e)
David Tolnayb9c8e322016-09-23 20:48:37 -0700432 ));
433
David Tolnay89e05672016-10-02 14:39:42 -0700434 named!(expr_paren -> Expr, do_parse!(
435 punct!("(") >>
436 e: expr >>
437 punct!(")") >>
438 (Expr::Paren(Box::new(e)))
439 ));
440
David Tolnay939766a2016-09-23 23:48:12 -0700441 named!(expr_box -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700442 keyword!("box") >>
David Tolnayb9c8e322016-09-23 20:48:37 -0700443 inner: expr >>
444 (Expr::Box(Box::new(inner)))
445 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700446
David Tolnay939766a2016-09-23 23:48:12 -0700447 named!(expr_vec -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700448 punct!("[") >>
449 elems: separated_list!(punct!(","), expr) >>
450 punct!("]") >>
451 (Expr::Vec(elems))
452 ));
453
David Tolnay939766a2016-09-23 23:48:12 -0700454 named!(and_call -> Vec<Expr>, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700455 punct!("(") >>
456 args: separated_list!(punct!(","), expr) >>
457 punct!(")") >>
458 (args)
459 ));
460
David Tolnay939766a2016-09-23 23:48:12 -0700461 named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700462 punct!(".") >>
463 method: ident >>
464 ascript: opt_vec!(delimited!(
465 punct!("<"),
466 separated_list!(punct!(","), ty),
467 punct!(">")
468 )) >>
469 punct!("(") >>
470 args: separated_list!(punct!(","), expr) >>
471 punct!(")") >>
472 (method, ascript, args)
473 ));
474
David Tolnay939766a2016-09-23 23:48:12 -0700475 named!(expr_tup -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700476 punct!("(") >>
477 elems: separated_list!(punct!(","), expr) >>
David Tolnay89e05672016-10-02 14:39:42 -0700478 option!(punct!(",")) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700479 punct!(")") >>
480 (Expr::Tup(elems))
481 ));
482
David Tolnay939766a2016-09-23 23:48:12 -0700483 named!(and_binary -> (BinOp, Expr), tuple!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700484 alt!(
485 punct!("&&") => { |_| BinOp::And }
486 |
487 punct!("||") => { |_| BinOp::Or }
488 |
489 punct!("<<") => { |_| BinOp::Shl }
490 |
491 punct!(">>") => { |_| BinOp::Shr }
492 |
493 punct!("==") => { |_| BinOp::Eq }
494 |
495 punct!("<=") => { |_| BinOp::Le }
496 |
497 punct!("!=") => { |_| BinOp::Ne }
498 |
499 punct!(">=") => { |_| BinOp::Ge }
500 |
501 punct!("+") => { |_| BinOp::Add }
502 |
503 punct!("-") => { |_| BinOp::Sub }
504 |
505 punct!("*") => { |_| BinOp::Mul }
506 |
507 punct!("/") => { |_| BinOp::Div }
508 |
509 punct!("%") => { |_| BinOp::Rem }
510 |
511 punct!("^") => { |_| BinOp::BitXor }
512 |
513 punct!("&") => { |_| BinOp::BitAnd }
514 |
515 punct!("|") => { |_| BinOp::BitOr }
516 |
517 punct!("<") => { |_| BinOp::Lt }
518 |
519 punct!(">") => { |_| BinOp::Gt }
520 ),
521 expr
522 ));
523
David Tolnay939766a2016-09-23 23:48:12 -0700524 named!(expr_unary -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700525 operator: alt!(
526 punct!("*") => { |_| UnOp::Deref }
527 |
528 punct!("!") => { |_| UnOp::Not }
529 |
530 punct!("-") => { |_| UnOp::Neg }
531 ) >>
532 operand: expr >>
533 (Expr::Unary(operator, Box::new(operand)))
534 ));
David Tolnay939766a2016-09-23 23:48:12 -0700535
536 named!(expr_lit -> Expr, map!(lit, Expr::Lit));
537
538 named!(and_cast -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700539 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700540 ty: ty >>
541 (ty)
542 ));
543
544 named!(and_ascription -> Ty, preceded!(punct!(":"), ty));
545
546 named!(expr_if -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700547 keyword!("if") >>
David Tolnay939766a2016-09-23 23:48:12 -0700548 cond: expr >>
549 punct!("{") >>
550 then_block: within_block >>
551 punct!("}") >>
552 else_block: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700553 keyword!("else"),
David Tolnay939766a2016-09-23 23:48:12 -0700554 alt!(
555 expr_if
556 |
557 do_parse!(
558 punct!("{") >>
559 else_block: within_block >>
560 punct!("}") >>
David Tolnay89e05672016-10-02 14:39:42 -0700561 (Expr::Block(BlockCheckMode::Default, Block {
David Tolnay939766a2016-09-23 23:48:12 -0700562 stmts: else_block,
David Tolnay89e05672016-10-02 14:39:42 -0700563 }))
David Tolnay939766a2016-09-23 23:48:12 -0700564 )
565 )
566 )) >>
567 (Expr::If(
568 Box::new(cond),
David Tolnay89e05672016-10-02 14:39:42 -0700569 Block {
David Tolnay939766a2016-09-23 23:48:12 -0700570 stmts: then_block,
David Tolnay89e05672016-10-02 14:39:42 -0700571 },
David Tolnay939766a2016-09-23 23:48:12 -0700572 else_block.map(Box::new),
573 ))
574 ));
575
Gregory Katze5f35682016-09-27 14:20:55 -0400576 named!(expr_loop -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700577 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -0700578 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -0400579 loop_block: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700580 (Expr::Loop(loop_block, lbl))
Gregory Katze5f35682016-09-27 14:20:55 -0400581 ));
582
David Tolnayb4ad3b52016-10-01 21:58:13 -0700583 named!(expr_match -> Expr, do_parse!(
584 keyword!("match") >>
585 obj: expr >>
586 punct!("{") >>
587 arms: many0!(do_parse!(
588 attrs: many0!(outer_attr) >>
589 pats: separated_nonempty_list!(punct!("|"), pat) >>
590 guard: option!(preceded!(keyword!("if"), expr)) >>
591 punct!("=>") >>
592 body: alt!(
593 terminated!(expr, punct!(","))
594 |
David Tolnay89e05672016-10-02 14:39:42 -0700595 map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk))
David Tolnayb4ad3b52016-10-01 21:58:13 -0700596 ) >>
597 (Arm {
598 attrs: attrs,
599 pats: pats,
600 guard: guard.map(Box::new),
601 body: Box::new(body),
602 })
603 )) >>
604 punct!("}") >>
605 (Expr::Match(Box::new(obj), arms))
606 ));
607
David Tolnay89e05672016-10-02 14:39:42 -0700608 named!(expr_closure -> Expr, do_parse!(
609 capture: capture_by >>
610 punct!("|") >>
611 inputs: separated_list!(punct!(","), closure_arg) >>
612 punct!("|") >>
613 ret_and_body: alt!(
614 do_parse!(
615 punct!("->") >>
616 ty: ty >>
617 body: block >>
618 ((FunctionRetTy::Ty(ty), body))
619 )
620 |
621 map!(expr, |e| (
622 FunctionRetTy::Default,
623 Block {
624 stmts: vec![Stmt::Expr(Box::new(e))],
625 },
626 ))
627 ) >>
628 (Expr::Closure(
629 capture,
630 Box::new(FnDecl {
631 inputs: inputs,
632 output: ret_and_body.0,
633 }),
634 ret_and_body.1,
635 ))
636 ));
637
638 named!(closure_arg -> FnArg, do_parse!(
639 pat: pat >>
640 ty: option!(preceded!(punct!(":"), ty)) >>
641 (FnArg {
642 pat: pat,
643 ty: ty.unwrap_or(Ty::Infer),
644 })
645 ));
646
Gregory Katz3e562cc2016-09-28 18:33:02 -0400647 named!(expr_while -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700648 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -0700649 keyword!("while") >>
Gregory Katz3e562cc2016-09-28 18:33:02 -0400650 cond: expr >>
651 while_block: block >>
652 (Expr::While(
653 Box::new(cond),
David Tolnay89e05672016-10-02 14:39:42 -0700654 while_block,
David Tolnay8b07f372016-09-30 10:28:40 -0700655 lbl,
Gregory Katz3e562cc2016-09-28 18:33:02 -0400656 ))
657 ));
658
Gregory Katzfd6935d2016-09-30 22:51:25 -0400659 named!(expr_continue -> Expr, do_parse!(
660 keyword!("continue") >>
661 lbl: option!(label) >>
David Tolnay055a7042016-10-02 19:23:54 -0700662 (Expr::Continue(lbl))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400663 ));
664
665 named!(expr_break -> Expr, do_parse!(
666 keyword!("break") >>
667 lbl: option!(label) >>
David Tolnay055a7042016-10-02 19:23:54 -0700668 (Expr::Break(lbl))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400669 ));
670
671 named!(expr_ret -> Expr, do_parse!(
672 keyword!("return") >>
673 ret_value: option!(expr) >>
David Tolnay055a7042016-10-02 19:23:54 -0700674 (Expr::Ret(ret_value.map(Box::new)))
675 ));
676
677 named!(expr_struct -> Expr, do_parse!(
678 path: path >>
679 punct!("{") >>
680 fields: separated_list!(punct!(","), field_value) >>
681 base: option!(do_parse!(
682 cond!(!fields.is_empty(), punct!(",")) >>
683 punct!("..") >>
684 base: expr >>
685 (base)
686 )) >>
687 punct!("}") >>
688 (Expr::Struct(path, fields, base.map(Box::new)))
689 ));
690
691 named!(field_value -> FieldValue, do_parse!(
692 name: ident >>
693 punct!(":") >>
694 value: expr >>
695 (FieldValue {
696 ident: name,
697 expr: value,
698 })
699 ));
700
701 named!(expr_repeat -> Expr, do_parse!(
702 punct!("[") >>
703 value: expr >>
704 punct!(";") >>
705 times: expr >>
706 punct!("]") >>
707 (Expr::Repeat(Box::new(value), Box::new(times)))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400708 ));
709
David Tolnay42602292016-10-01 22:25:45 -0700710 named!(expr_block -> Expr, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700711 rules: block_check_mode >>
David Tolnay42602292016-10-01 22:25:45 -0700712 b: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700713 (Expr::Block(rules, Block {
David Tolnay42602292016-10-01 22:25:45 -0700714 stmts: b.stmts,
David Tolnay89e05672016-10-02 14:39:42 -0700715 }))
716 ));
717
David Tolnay9636c052016-10-02 17:11:17 -0700718 named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path)));
David Tolnay42602292016-10-01 22:25:45 -0700719
David Tolnay3c2467c2016-10-02 17:55:08 -0700720 named!(expr_addr_of -> Expr, do_parse!(
721 punct!("&") >>
722 mutability: mutability >>
723 expr: expr >>
724 (Expr::AddrOf(mutability, Box::new(expr)))
725 ));
726
David Tolnay42602292016-10-01 22:25:45 -0700727 named!(pub block -> Block, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700728 punct!("{") >>
729 stmts: within_block >>
730 punct!("}") >>
731 (Block {
732 stmts: stmts,
David Tolnay939766a2016-09-23 23:48:12 -0700733 })
734 ));
735
736 named!(block_check_mode -> BlockCheckMode, alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700737 keyword!("unsafe") => { |_| BlockCheckMode::Unsafe }
David Tolnay939766a2016-09-23 23:48:12 -0700738 |
739 epsilon!() => { |_| BlockCheckMode::Default }
740 ));
741
742 named!(within_block -> Vec<Stmt>, do_parse!(
David Tolnay181bac52016-09-24 00:10:05 -0700743 mut most: many0!(standalone_stmt) >>
David Tolnay939766a2016-09-23 23:48:12 -0700744 last: option!(expr) >>
745 (match last {
746 None => most,
747 Some(last) => {
David Tolnay939766a2016-09-23 23:48:12 -0700748 most.push(Stmt::Expr(Box::new(last)));
749 most
750 }
751 })
752 ));
753
754 named!(standalone_stmt -> Stmt, alt!(
David Tolnay191e0582016-10-02 18:31:09 -0700755 stmt_local
756 |
757 stmt_item
758 |
David Tolnay939766a2016-09-23 23:48:12 -0700759 stmt_semi
David Tolnaya96a3fa2016-09-24 07:17:42 -0700760 // TODO: mac
David Tolnay939766a2016-09-23 23:48:12 -0700761 ));
762
David Tolnay191e0582016-10-02 18:31:09 -0700763 named!(stmt_local -> Stmt, do_parse!(
764 attrs: many0!(outer_attr) >>
765 keyword!("let") >>
766 pat: pat >>
767 ty: option!(preceded!(punct!(":"), ty)) >>
768 init: option!(preceded!(punct!("="), expr)) >>
769 punct!(";") >>
770 (Stmt::Local(Box::new(Local {
771 pat: Box::new(pat),
772 ty: ty.map(Box::new),
773 init: init.map(Box::new),
774 attrs: attrs,
775 })))
776 ));
777
778 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
779
David Tolnay939766a2016-09-23 23:48:12 -0700780 named!(stmt_semi -> Stmt, do_parse!(
781 e: expr >>
782 punct!(";") >>
783 (Stmt::Semi(Box::new(e)))
784 ));
David Tolnay8b07f372016-09-30 10:28:40 -0700785
David Tolnay42602292016-10-01 22:25:45 -0700786 named!(pub pat -> Pat, alt!(
David Tolnayb4ad3b52016-10-01 21:58:13 -0700787 pat_wild
788 |
789 pat_ident
790 // TODO: Struct
791 // TODO: TupleStruct
David Tolnay9636c052016-10-02 17:11:17 -0700792 |
793 pat_path
David Tolnayb4ad3b52016-10-01 21:58:13 -0700794 // TODO: Tuple
795 // TODO: Box
796 // TODO: Ref
797 // TODO: Lit
798 // TODO: Range
799 // TODO: Vec
800 // TODO: Mac
801 ));
802
803 named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild));
804
805 named!(pat_ident -> Pat, do_parse!(
806 mode: option!(keyword!("ref")) >>
807 mutability: mutability >>
808 name: ident >>
809 subpat: option!(preceded!(punct!("@"), pat)) >>
810 (Pat::Ident(
811 if mode.is_some() {
812 BindingMode::ByRef(mutability)
813 } else {
814 BindingMode::ByValue(mutability)
815 },
816 name,
817 subpat.map(Box::new),
818 ))
819 ));
820
David Tolnay9636c052016-10-02 17:11:17 -0700821 named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path)));
822
David Tolnay89e05672016-10-02 14:39:42 -0700823 named!(capture_by -> CaptureBy, alt!(
824 keyword!("move") => { |_| CaptureBy::Value }
825 |
826 epsilon!() => { |_| CaptureBy::Ref }
827 ));
828
David Tolnay8b07f372016-09-30 10:28:40 -0700829 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -0700830}
831
David Tolnayf4bbbd92016-09-23 14:41:55 -0700832#[cfg(feature = "printing")]
833mod printing {
834 use super::*;
David Tolnay89e05672016-10-02 14:39:42 -0700835 use {FunctionRetTy, Mutability, Ty};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700836 use quote::{Tokens, ToTokens};
837
838 impl ToTokens for Expr {
839 fn to_tokens(&self, tokens: &mut Tokens) {
840 match *self {
David Tolnay47a877c2016-10-01 16:50:55 -0700841 Expr::Box(ref _inner) => unimplemented!(),
842 Expr::Vec(ref _inner) => unimplemented!(),
David Tolnay9636c052016-10-02 17:11:17 -0700843 Expr::Call(ref func, ref args) => {
844 func.to_tokens(tokens);
845 tokens.append("(");
846 tokens.append_separated(args, ",");
847 tokens.append(")");
848 }
849 Expr::MethodCall(ref ident, ref ascript, ref args) => {
850 args[0].to_tokens(tokens);
851 tokens.append(".");
852 ident.to_tokens(tokens);
853 if ascript.len() > 0 {
854 tokens.append("::");
855 tokens.append("<");
856 tokens.append_separated(ascript, ",");
857 tokens.append(">");
858 }
859 tokens.append("(");
860 tokens.append_separated(&args[1..], ",");
861 tokens.append(")");
862 }
David Tolnay47a877c2016-10-01 16:50:55 -0700863 Expr::Tup(ref fields) => {
864 tokens.append("(");
865 tokens.append_separated(fields, ",");
866 if fields.len() == 1 {
867 tokens.append(",");
868 }
869 tokens.append(")");
870 }
David Tolnay89e05672016-10-02 14:39:42 -0700871 Expr::Binary(op, ref left, ref right) => {
872 left.to_tokens(tokens);
873 op.to_tokens(tokens);
874 right.to_tokens(tokens);
875 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700876 Expr::Unary(op, ref expr) => {
877 op.to_tokens(tokens);
878 expr.to_tokens(tokens);
879 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700880 Expr::Lit(ref lit) => lit.to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -0700881 Expr::Cast(ref expr, ref ty) => {
882 expr.to_tokens(tokens);
883 tokens.append("as");
884 ty.to_tokens(tokens);
885 }
886 Expr::Type(ref expr, ref ty) => {
887 expr.to_tokens(tokens);
888 tokens.append(":");
889 ty.to_tokens(tokens);
890 }
891 Expr::If(ref cond, ref then_block, ref else_block) => {
892 tokens.append("if");
893 cond.to_tokens(tokens);
894 then_block.to_tokens(tokens);
895 if let Some(ref else_block) = *else_block {
896 tokens.append("else");
897 else_block.to_tokens(tokens);
898 }
899 }
David Tolnay47a877c2016-10-01 16:50:55 -0700900 Expr::IfLet(ref _pat, ref _expr, ref _then_block, ref _else_block) => unimplemented!(),
901 Expr::While(ref _cond, ref _body, ref _label) => unimplemented!(),
902 Expr::WhileLet(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
903 Expr::ForLoop(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(),
904 Expr::Loop(ref _body, ref _label) => unimplemented!(),
David Tolnayb4ad3b52016-10-01 21:58:13 -0700905 Expr::Match(ref expr, ref arms) => {
906 tokens.append("match");
907 expr.to_tokens(tokens);
908 tokens.append("{");
909 tokens.append_separated(arms, ",");
910 tokens.append("}");
911 }
David Tolnay89e05672016-10-02 14:39:42 -0700912 Expr::Closure(capture, ref decl, ref body) => {
913 capture.to_tokens(tokens);
914 tokens.append("|");
915 for (i, input) in decl.inputs.iter().enumerate() {
916 if i > 0 {
917 tokens.append(",");
918 }
919 input.pat.to_tokens(tokens);
920 match input.ty {
921 Ty::Infer => { /* nothing */ }
922 _ => {
923 tokens.append(":");
924 input.ty.to_tokens(tokens);
925 }
926 }
927 }
928 tokens.append("|");
929 match decl.output {
930 FunctionRetTy::Default => {
931 if body.stmts.len() == 1 {
932 if let Stmt::Expr(ref expr) = body.stmts[0] {
933 expr.to_tokens(tokens);
934 } else {
935 body.to_tokens(tokens);
936 }
937 } else {
938 body.to_tokens(tokens);
939 }
940 }
941 FunctionRetTy::Ty(ref ty) => {
942 tokens.append("->");
943 ty.to_tokens(tokens);
944 body.to_tokens(tokens);
945 }
946 }
947 }
948 Expr::Block(rules, ref block) => {
949 rules.to_tokens(tokens);
950 block.to_tokens(tokens);
951 }
David Tolnay47a877c2016-10-01 16:50:55 -0700952 Expr::Assign(ref _var, ref _expr) => unimplemented!(),
953 Expr::AssignOp(_op, ref _var, ref _expr) => unimplemented!(),
954 Expr::Field(ref _expr, ref _field) => unimplemented!(),
955 Expr::TupField(ref _expr, _field) => unimplemented!(),
956 Expr::Index(ref _expr, ref _index) => unimplemented!(),
957 Expr::Range(ref _from, ref _to, _limits) => unimplemented!(),
David Tolnay89e05672016-10-02 14:39:42 -0700958 Expr::Path(None, ref path) => {
959 path.to_tokens(tokens);
960 }
961 Expr::Path(Some(ref qself), ref path) => {
962 tokens.append("<");
963 qself.ty.to_tokens(tokens);
964 if qself.position > 0 {
965 tokens.append("as");
966 for (i, segment) in path.segments.iter()
967 .take(qself.position)
968 .enumerate()
969 {
970 if i > 0 || path.global {
971 tokens.append("::");
972 }
973 segment.to_tokens(tokens);
974 }
975 }
976 tokens.append(">");
977 for segment in path.segments.iter().skip(qself.position) {
978 tokens.append("::");
979 segment.to_tokens(tokens);
980 }
981 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700982 Expr::AddrOf(mutability, ref expr) => {
983 tokens.append("&");
984 mutability.to_tokens(tokens);
985 expr.to_tokens(tokens);
986 }
987 Expr::Break(ref opt_label) => {
988 tokens.append("break");
989 opt_label.to_tokens(tokens);
990 }
991 Expr::Continue(ref opt_label) => {
992 tokens.append("continue");
993 opt_label.to_tokens(tokens);
994 }
David Tolnay42602292016-10-01 22:25:45 -0700995 Expr::Ret(ref opt_expr) => {
996 tokens.append("return");
997 opt_expr.to_tokens(tokens);
998 }
David Tolnay47a877c2016-10-01 16:50:55 -0700999 Expr::Mac(ref _mac) => unimplemented!(),
David Tolnay055a7042016-10-02 19:23:54 -07001000 Expr::Struct(ref path, ref fields, ref base) => {
1001 path.to_tokens(tokens);
1002 tokens.append("{");
1003 tokens.append_separated(fields, ",");
1004 if let Some(ref base) = *base {
1005 if !fields.is_empty() {
1006 tokens.append(",");
1007 }
1008 tokens.append("..");
1009 base.to_tokens(tokens);
1010 }
1011 tokens.append("}");
1012 }
1013 Expr::Repeat(ref expr, ref times) => {
1014 tokens.append("[");
1015 expr.to_tokens(tokens);
1016 tokens.append(";");
1017 times.to_tokens(tokens);
1018 tokens.append("]");
1019 }
David Tolnay89e05672016-10-02 14:39:42 -07001020 Expr::Paren(ref expr) => {
1021 tokens.append("(");
1022 expr.to_tokens(tokens);
1023 tokens.append(")");
1024 }
David Tolnay3c2467c2016-10-02 17:55:08 -07001025 Expr::Try(ref expr) => {
1026 expr.to_tokens(tokens);
1027 tokens.append("?");
1028 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001029 }
1030 }
1031 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001032
David Tolnay89e05672016-10-02 14:39:42 -07001033 impl ToTokens for BinOp {
1034 fn to_tokens(&self, tokens: &mut Tokens) {
1035 match *self {
1036 BinOp::Add => tokens.append("+"),
1037 BinOp::Sub => tokens.append("-"),
1038 BinOp::Mul => tokens.append("*"),
1039 BinOp::Div => tokens.append("/"),
1040 BinOp::Rem => tokens.append("%"),
1041 BinOp::And => tokens.append("&&"),
1042 BinOp::Or => tokens.append("||"),
1043 BinOp::BitXor => tokens.append("^"),
1044 BinOp::BitAnd => tokens.append("&"),
1045 BinOp::BitOr => tokens.append("|"),
1046 BinOp::Shl => tokens.append("<<"),
1047 BinOp::Shr => tokens.append(">>"),
1048 BinOp::Eq => tokens.append("=="),
1049 BinOp::Lt => tokens.append("<"),
1050 BinOp::Le => tokens.append("<="),
1051 BinOp::Ne => tokens.append("!="),
1052 BinOp::Ge => tokens.append(">="),
1053 BinOp::Gt => tokens.append(">"),
1054 }
1055 }
1056 }
1057
David Tolnay3c2467c2016-10-02 17:55:08 -07001058 impl ToTokens for UnOp {
1059 fn to_tokens(&self, tokens: &mut Tokens) {
1060 match *self {
1061 UnOp::Deref => tokens.append("*"),
1062 UnOp::Not => tokens.append("!"),
1063 UnOp::Neg => tokens.append("-"),
1064 }
1065 }
1066 }
1067
David Tolnay055a7042016-10-02 19:23:54 -07001068 impl ToTokens for FieldValue {
1069 fn to_tokens(&self, tokens: &mut Tokens) {
1070 self.ident.to_tokens(tokens);
1071 tokens.append(":");
1072 self.expr.to_tokens(tokens);
1073 }
1074 }
1075
David Tolnayb4ad3b52016-10-01 21:58:13 -07001076 impl ToTokens for Arm {
1077 fn to_tokens(&self, tokens: &mut Tokens) {
1078 for attr in &self.attrs {
1079 attr.to_tokens(tokens);
1080 }
1081 tokens.append_separated(&self.pats, "|");
1082 if let Some(ref guard) = self.guard {
1083 tokens.append("if");
1084 guard.to_tokens(tokens);
1085 }
1086 tokens.append("=>");
1087 self.body.to_tokens(tokens);
1088 match *self.body {
David Tolnay89e05672016-10-02 14:39:42 -07001089 Expr::Block(_, _) => { /* no comma */ }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001090 _ => tokens.append(","),
1091 }
1092 }
1093 }
1094
1095 impl ToTokens for Pat {
1096 fn to_tokens(&self, tokens: &mut Tokens) {
1097 match *self {
1098 Pat::Wild => tokens.append("_"),
1099 Pat::Ident(mode, ref ident, ref subpat) => {
1100 mode.to_tokens(tokens);
1101 ident.to_tokens(tokens);
1102 if let Some(ref subpat) = *subpat {
1103 tokens.append("@");
1104 subpat.to_tokens(tokens);
1105 }
1106 }
1107 Pat::Struct(ref _path, ref _fields, _dots) => unimplemented!(),
1108 Pat::TupleStruct(ref _path, ref _pats, _dotpos) => unimplemented!(),
1109 Pat::Path(ref _qself, ref _path) => unimplemented!(),
1110 Pat::Tuple(ref _pats, _dotpos) => unimplemented!(),
1111 Pat::Box(ref _inner) => unimplemented!(),
1112 Pat::Ref(ref _target, _mutability) => unimplemented!(),
1113 Pat::Lit(ref _expr) => unimplemented!(),
1114 Pat::Range(ref _lower, ref _upper) => unimplemented!(),
1115 Pat::Vec(ref _before, ref _dots, ref _after) => unimplemented!(),
1116 Pat::Mac(ref _mac) => unimplemented!(),
1117 }
1118 }
1119 }
1120
1121 impl ToTokens for BindingMode {
1122 fn to_tokens(&self, tokens: &mut Tokens) {
1123 match *self {
1124 BindingMode::ByRef(Mutability::Immutable) => {
1125 tokens.append("ref");
1126 }
1127 BindingMode::ByRef(Mutability::Mutable) => {
1128 tokens.append("ref");
1129 tokens.append("mut");
1130 }
1131 BindingMode::ByValue(Mutability::Immutable) => {}
1132 BindingMode::ByValue(Mutability::Mutable) => {
1133 tokens.append("mut");
1134 }
1135 }
1136 }
1137 }
David Tolnay42602292016-10-01 22:25:45 -07001138
David Tolnay89e05672016-10-02 14:39:42 -07001139 impl ToTokens for CaptureBy {
1140 fn to_tokens(&self, tokens: &mut Tokens) {
1141 match *self {
1142 CaptureBy::Value => tokens.append("move"),
1143 CaptureBy::Ref => { /* nothing */ }
1144 }
1145 }
1146 }
1147
David Tolnay42602292016-10-01 22:25:45 -07001148 impl ToTokens for Block {
1149 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay42602292016-10-01 22:25:45 -07001150 tokens.append("{");
1151 for stmt in &self.stmts {
1152 stmt.to_tokens(tokens);
1153 }
1154 tokens.append("}");
1155 }
1156 }
1157
1158 impl ToTokens for BlockCheckMode {
1159 fn to_tokens(&self, tokens: &mut Tokens) {
1160 match *self {
David Tolnay89e05672016-10-02 14:39:42 -07001161 BlockCheckMode::Default => { /* nothing */ }
David Tolnay42602292016-10-01 22:25:45 -07001162 BlockCheckMode::Unsafe => tokens.append("unsafe"),
1163 }
1164 }
1165 }
1166
1167 impl ToTokens for Stmt {
1168 fn to_tokens(&self, tokens: &mut Tokens) {
1169 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07001170 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07001171 Stmt::Item(ref item) => item.to_tokens(tokens),
1172 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
1173 Stmt::Semi(ref expr) => {
1174 expr.to_tokens(tokens);
1175 tokens.append(";");
1176 }
1177 Stmt::Mac(ref _mac) => unimplemented!(),
1178 }
1179 }
1180 }
David Tolnay191e0582016-10-02 18:31:09 -07001181
1182 impl ToTokens for Local {
1183 fn to_tokens(&self, tokens: &mut Tokens) {
1184 tokens.append("let");
1185 self.pat.to_tokens(tokens);
1186 if let Some(ref ty) = self.ty {
1187 tokens.append(":");
1188 ty.to_tokens(tokens);
1189 }
1190 if let Some(ref init) = self.init {
1191 tokens.append("=");
1192 init.to_tokens(tokens);
1193 }
1194 tokens.append(";");
1195 }
1196 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001197}