blob: e3a022c17d9d1c1bc55480cf1bed047d85910250 [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 Tolnaybb6feae2016-10-02 21:25:20 -0700370 |
371 expr_for_loop
Gregory Katze5f35682016-09-27 14:20:55 -0400372 |
373 expr_loop
David Tolnayb4ad3b52016-10-01 21:58:13 -0700374 |
375 expr_match
David Tolnay89e05672016-10-02 14:39:42 -0700376 |
377 expr_closure
David Tolnay939766a2016-09-23 23:48:12 -0700378 |
379 expr_block
David Tolnay89e05672016-10-02 14:39:42 -0700380 |
381 expr_path
David Tolnay3c2467c2016-10-02 17:55:08 -0700382 |
383 expr_addr_of
Gregory Katzfd6935d2016-09-30 22:51:25 -0400384 |
385 expr_break
386 |
387 expr_continue
388 |
389 expr_ret
David Tolnaya96a3fa2016-09-24 07:17:42 -0700390 // TODO: Mac
David Tolnay055a7042016-10-02 19:23:54 -0700391 |
392 expr_repeat
David Tolnayfa0edf22016-09-23 22:58:24 -0700393 ) >>
394 many0!(alt!(
David Tolnay939766a2016-09-23 23:48:12 -0700395 tap!(args: and_call => {
396 e = Expr::Call(Box::new(e), args);
David Tolnayfa0edf22016-09-23 22:58:24 -0700397 })
398 |
David Tolnay939766a2016-09-23 23:48:12 -0700399 tap!(more: and_method_call => {
400 let (method, ascript, mut args) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700401 args.insert(0, e);
402 e = Expr::MethodCall(method, ascript, args);
403 })
404 |
David Tolnay939766a2016-09-23 23:48:12 -0700405 tap!(more: and_binary => {
406 let (op, other) = more;
David Tolnayfa0edf22016-09-23 22:58:24 -0700407 e = Expr::Binary(op, Box::new(e), Box::new(other));
408 })
David Tolnay939766a2016-09-23 23:48:12 -0700409 |
410 tap!(ty: and_cast => {
411 e = Expr::Cast(Box::new(e), Box::new(ty));
412 })
413 |
414 tap!(ty: and_ascription => {
415 e = Expr::Type(Box::new(e), Box::new(ty));
416 })
David Tolnaya96a3fa2016-09-24 07:17:42 -0700417 // TODO: Assign
418 // TODO: AssignOp
419 // TODO: Field
420 // TODO: TupField
421 // TODO: Index
422 // TODO: Range
David Tolnay055a7042016-10-02 19:23:54 -0700423 |
424 tap!(_try: punct!("?") => {
425 e = Expr::Try(Box::new(e));
426 })
David Tolnayfa0edf22016-09-23 22:58:24 -0700427 )) >>
428 (e)
David Tolnayb9c8e322016-09-23 20:48:37 -0700429 ));
430
David Tolnay89e05672016-10-02 14:39:42 -0700431 named!(expr_paren -> Expr, do_parse!(
432 punct!("(") >>
433 e: expr >>
434 punct!(")") >>
435 (Expr::Paren(Box::new(e)))
436 ));
437
David Tolnay939766a2016-09-23 23:48:12 -0700438 named!(expr_box -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700439 keyword!("box") >>
David Tolnayb9c8e322016-09-23 20:48:37 -0700440 inner: expr >>
441 (Expr::Box(Box::new(inner)))
442 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700443
David Tolnay939766a2016-09-23 23:48:12 -0700444 named!(expr_vec -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700445 punct!("[") >>
446 elems: separated_list!(punct!(","), expr) >>
447 punct!("]") >>
448 (Expr::Vec(elems))
449 ));
450
David Tolnay939766a2016-09-23 23:48:12 -0700451 named!(and_call -> Vec<Expr>, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700452 punct!("(") >>
453 args: separated_list!(punct!(","), expr) >>
454 punct!(")") >>
455 (args)
456 ));
457
David Tolnay939766a2016-09-23 23:48:12 -0700458 named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700459 punct!(".") >>
460 method: ident >>
461 ascript: opt_vec!(delimited!(
462 punct!("<"),
463 separated_list!(punct!(","), ty),
464 punct!(">")
465 )) >>
466 punct!("(") >>
467 args: separated_list!(punct!(","), expr) >>
468 punct!(")") >>
469 (method, ascript, args)
470 ));
471
David Tolnay939766a2016-09-23 23:48:12 -0700472 named!(expr_tup -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700473 punct!("(") >>
474 elems: separated_list!(punct!(","), expr) >>
David Tolnay89e05672016-10-02 14:39:42 -0700475 option!(punct!(",")) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700476 punct!(")") >>
477 (Expr::Tup(elems))
478 ));
479
David Tolnay939766a2016-09-23 23:48:12 -0700480 named!(and_binary -> (BinOp, Expr), tuple!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700481 alt!(
482 punct!("&&") => { |_| BinOp::And }
483 |
484 punct!("||") => { |_| BinOp::Or }
485 |
486 punct!("<<") => { |_| BinOp::Shl }
487 |
488 punct!(">>") => { |_| BinOp::Shr }
489 |
490 punct!("==") => { |_| BinOp::Eq }
491 |
492 punct!("<=") => { |_| BinOp::Le }
493 |
494 punct!("!=") => { |_| BinOp::Ne }
495 |
496 punct!(">=") => { |_| BinOp::Ge }
497 |
498 punct!("+") => { |_| BinOp::Add }
499 |
500 punct!("-") => { |_| BinOp::Sub }
501 |
502 punct!("*") => { |_| BinOp::Mul }
503 |
504 punct!("/") => { |_| BinOp::Div }
505 |
506 punct!("%") => { |_| BinOp::Rem }
507 |
508 punct!("^") => { |_| BinOp::BitXor }
509 |
510 punct!("&") => { |_| BinOp::BitAnd }
511 |
512 punct!("|") => { |_| BinOp::BitOr }
513 |
514 punct!("<") => { |_| BinOp::Lt }
515 |
516 punct!(">") => { |_| BinOp::Gt }
517 ),
518 expr
519 ));
520
David Tolnay939766a2016-09-23 23:48:12 -0700521 named!(expr_unary -> Expr, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700522 operator: alt!(
523 punct!("*") => { |_| UnOp::Deref }
524 |
525 punct!("!") => { |_| UnOp::Not }
526 |
527 punct!("-") => { |_| UnOp::Neg }
528 ) >>
529 operand: expr >>
530 (Expr::Unary(operator, Box::new(operand)))
531 ));
David Tolnay939766a2016-09-23 23:48:12 -0700532
533 named!(expr_lit -> Expr, map!(lit, Expr::Lit));
534
535 named!(and_cast -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700536 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700537 ty: ty >>
538 (ty)
539 ));
540
541 named!(and_ascription -> Ty, preceded!(punct!(":"), ty));
542
David Tolnaybb6feae2016-10-02 21:25:20 -0700543 enum Cond {
David Tolnay29f9ce12016-10-02 20:58:40 -0700544 Let(Pat, Expr),
545 Expr(Expr),
546 }
547
David Tolnaybb6feae2016-10-02 21:25:20 -0700548 named!(cond -> Cond, alt!(
549 do_parse!(
550 keyword!("let") >>
551 pat: pat >>
552 punct!("=") >>
553 value: expr >>
554 (Cond::Let(pat, value))
555 )
556 |
557 map!(expr, Cond::Expr)
558 ));
559
David Tolnay939766a2016-09-23 23:48:12 -0700560 named!(expr_if -> Expr, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700561 keyword!("if") >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700562 cond: cond >>
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 {
David Tolnaybb6feae2016-10-02 21:25:20 -0700582 Cond::Let(pat, expr) => Expr::IfLet(
David Tolnay29f9ce12016-10-02 20:58:40 -0700583 Box::new(pat),
584 Box::new(expr),
585 Block {
586 stmts: then_block,
587 },
588 else_block.map(Box::new),
589 ),
David Tolnaybb6feae2016-10-02 21:25:20 -0700590 Cond::Expr(cond) => Expr::If(
David Tolnay29f9ce12016-10-02 20:58:40 -0700591 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
David Tolnaybb6feae2016-10-02 21:25:20 -0700600 named!(expr_for_loop -> Expr, do_parse!(
601 lbl: option!(terminated!(label, punct!(":"))) >>
602 keyword!("for") >>
603 pat: pat >>
604 keyword!("in") >>
605 expr: expr >>
606 loop_block: block >>
607 (Expr::ForLoop(Box::new(pat), Box::new(expr), loop_block, lbl))
608 ));
609
Gregory Katze5f35682016-09-27 14:20:55 -0400610 named!(expr_loop -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700611 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -0700612 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -0400613 loop_block: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700614 (Expr::Loop(loop_block, lbl))
Gregory Katze5f35682016-09-27 14:20:55 -0400615 ));
616
David Tolnayb4ad3b52016-10-01 21:58:13 -0700617 named!(expr_match -> Expr, do_parse!(
618 keyword!("match") >>
619 obj: expr >>
620 punct!("{") >>
621 arms: many0!(do_parse!(
622 attrs: many0!(outer_attr) >>
623 pats: separated_nonempty_list!(punct!("|"), pat) >>
624 guard: option!(preceded!(keyword!("if"), expr)) >>
625 punct!("=>") >>
626 body: alt!(
627 terminated!(expr, punct!(","))
628 |
David Tolnay89e05672016-10-02 14:39:42 -0700629 map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk))
David Tolnayb4ad3b52016-10-01 21:58:13 -0700630 ) >>
631 (Arm {
632 attrs: attrs,
633 pats: pats,
634 guard: guard.map(Box::new),
635 body: Box::new(body),
636 })
637 )) >>
638 punct!("}") >>
639 (Expr::Match(Box::new(obj), arms))
640 ));
641
David Tolnay89e05672016-10-02 14:39:42 -0700642 named!(expr_closure -> Expr, do_parse!(
643 capture: capture_by >>
644 punct!("|") >>
645 inputs: separated_list!(punct!(","), closure_arg) >>
646 punct!("|") >>
647 ret_and_body: alt!(
648 do_parse!(
649 punct!("->") >>
650 ty: ty >>
651 body: block >>
652 ((FunctionRetTy::Ty(ty), body))
653 )
654 |
655 map!(expr, |e| (
656 FunctionRetTy::Default,
657 Block {
658 stmts: vec![Stmt::Expr(Box::new(e))],
659 },
660 ))
661 ) >>
662 (Expr::Closure(
663 capture,
664 Box::new(FnDecl {
665 inputs: inputs,
666 output: ret_and_body.0,
667 }),
668 ret_and_body.1,
669 ))
670 ));
671
672 named!(closure_arg -> FnArg, do_parse!(
673 pat: pat >>
674 ty: option!(preceded!(punct!(":"), ty)) >>
675 (FnArg {
676 pat: pat,
677 ty: ty.unwrap_or(Ty::Infer),
678 })
679 ));
680
Gregory Katz3e562cc2016-09-28 18:33:02 -0400681 named!(expr_while -> Expr, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -0700682 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -0700683 keyword!("while") >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700684 cond: cond >>
Gregory Katz3e562cc2016-09-28 18:33:02 -0400685 while_block: block >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700686 (match cond {
687 Cond::Let(pat, expr) => Expr::WhileLet(
688 Box::new(pat),
689 Box::new(expr),
690 while_block,
691 lbl,
692 ),
693 Cond::Expr(cond) => Expr::While(
694 Box::new(cond),
695 while_block,
696 lbl,
697 ),
698 })
Gregory Katz3e562cc2016-09-28 18:33:02 -0400699 ));
700
Gregory Katzfd6935d2016-09-30 22:51:25 -0400701 named!(expr_continue -> Expr, do_parse!(
702 keyword!("continue") >>
703 lbl: option!(label) >>
David Tolnay055a7042016-10-02 19:23:54 -0700704 (Expr::Continue(lbl))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400705 ));
706
707 named!(expr_break -> Expr, do_parse!(
708 keyword!("break") >>
709 lbl: option!(label) >>
David Tolnay055a7042016-10-02 19:23:54 -0700710 (Expr::Break(lbl))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400711 ));
712
713 named!(expr_ret -> Expr, do_parse!(
714 keyword!("return") >>
715 ret_value: option!(expr) >>
David Tolnay055a7042016-10-02 19:23:54 -0700716 (Expr::Ret(ret_value.map(Box::new)))
717 ));
718
719 named!(expr_struct -> Expr, do_parse!(
720 path: path >>
721 punct!("{") >>
722 fields: separated_list!(punct!(","), field_value) >>
723 base: option!(do_parse!(
724 cond!(!fields.is_empty(), punct!(",")) >>
725 punct!("..") >>
726 base: expr >>
727 (base)
728 )) >>
729 punct!("}") >>
730 (Expr::Struct(path, fields, base.map(Box::new)))
731 ));
732
733 named!(field_value -> FieldValue, do_parse!(
734 name: ident >>
735 punct!(":") >>
736 value: expr >>
737 (FieldValue {
738 ident: name,
739 expr: value,
740 })
741 ));
742
743 named!(expr_repeat -> Expr, do_parse!(
744 punct!("[") >>
745 value: expr >>
746 punct!(";") >>
747 times: expr >>
748 punct!("]") >>
749 (Expr::Repeat(Box::new(value), Box::new(times)))
Gregory Katzfd6935d2016-09-30 22:51:25 -0400750 ));
751
David Tolnay42602292016-10-01 22:25:45 -0700752 named!(expr_block -> Expr, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700753 rules: block_check_mode >>
David Tolnay42602292016-10-01 22:25:45 -0700754 b: block >>
David Tolnay89e05672016-10-02 14:39:42 -0700755 (Expr::Block(rules, Block {
David Tolnay42602292016-10-01 22:25:45 -0700756 stmts: b.stmts,
David Tolnay89e05672016-10-02 14:39:42 -0700757 }))
758 ));
759
David Tolnay9636c052016-10-02 17:11:17 -0700760 named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path)));
David Tolnay42602292016-10-01 22:25:45 -0700761
David Tolnay3c2467c2016-10-02 17:55:08 -0700762 named!(expr_addr_of -> Expr, do_parse!(
763 punct!("&") >>
764 mutability: mutability >>
765 expr: expr >>
766 (Expr::AddrOf(mutability, Box::new(expr)))
767 ));
768
David Tolnay42602292016-10-01 22:25:45 -0700769 named!(pub block -> Block, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -0700770 punct!("{") >>
771 stmts: within_block >>
772 punct!("}") >>
773 (Block {
774 stmts: stmts,
David Tolnay939766a2016-09-23 23:48:12 -0700775 })
776 ));
777
778 named!(block_check_mode -> BlockCheckMode, alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700779 keyword!("unsafe") => { |_| BlockCheckMode::Unsafe }
David Tolnay939766a2016-09-23 23:48:12 -0700780 |
781 epsilon!() => { |_| BlockCheckMode::Default }
782 ));
783
784 named!(within_block -> Vec<Stmt>, do_parse!(
David Tolnay181bac52016-09-24 00:10:05 -0700785 mut most: many0!(standalone_stmt) >>
David Tolnay939766a2016-09-23 23:48:12 -0700786 last: option!(expr) >>
787 (match last {
788 None => most,
789 Some(last) => {
David Tolnay939766a2016-09-23 23:48:12 -0700790 most.push(Stmt::Expr(Box::new(last)));
791 most
792 }
793 })
794 ));
795
796 named!(standalone_stmt -> Stmt, alt!(
David Tolnay191e0582016-10-02 18:31:09 -0700797 stmt_local
798 |
799 stmt_item
800 |
David Tolnay939766a2016-09-23 23:48:12 -0700801 stmt_semi
David Tolnaya96a3fa2016-09-24 07:17:42 -0700802 // TODO: mac
David Tolnay939766a2016-09-23 23:48:12 -0700803 ));
804
David Tolnay191e0582016-10-02 18:31:09 -0700805 named!(stmt_local -> Stmt, do_parse!(
806 attrs: many0!(outer_attr) >>
807 keyword!("let") >>
808 pat: pat >>
809 ty: option!(preceded!(punct!(":"), ty)) >>
810 init: option!(preceded!(punct!("="), expr)) >>
811 punct!(";") >>
812 (Stmt::Local(Box::new(Local {
813 pat: Box::new(pat),
814 ty: ty.map(Box::new),
815 init: init.map(Box::new),
816 attrs: attrs,
817 })))
818 ));
819
820 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
821
David Tolnay939766a2016-09-23 23:48:12 -0700822 named!(stmt_semi -> Stmt, do_parse!(
823 e: expr >>
824 punct!(";") >>
825 (Stmt::Semi(Box::new(e)))
826 ));
David Tolnay8b07f372016-09-30 10:28:40 -0700827
David Tolnay42602292016-10-01 22:25:45 -0700828 named!(pub pat -> Pat, alt!(
David Tolnayb4ad3b52016-10-01 21:58:13 -0700829 pat_wild
830 |
831 pat_ident
832 // TODO: Struct
833 // TODO: TupleStruct
David Tolnay9636c052016-10-02 17:11:17 -0700834 |
835 pat_path
David Tolnayb4ad3b52016-10-01 21:58:13 -0700836 // TODO: Tuple
837 // TODO: Box
838 // TODO: Ref
839 // TODO: Lit
840 // TODO: Range
841 // TODO: Vec
842 // TODO: Mac
843 ));
844
845 named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild));
846
847 named!(pat_ident -> Pat, do_parse!(
848 mode: option!(keyword!("ref")) >>
849 mutability: mutability >>
850 name: ident >>
851 subpat: option!(preceded!(punct!("@"), pat)) >>
852 (Pat::Ident(
853 if mode.is_some() {
854 BindingMode::ByRef(mutability)
855 } else {
856 BindingMode::ByValue(mutability)
857 },
858 name,
859 subpat.map(Box::new),
860 ))
861 ));
862
David Tolnay9636c052016-10-02 17:11:17 -0700863 named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path)));
864
David Tolnay89e05672016-10-02 14:39:42 -0700865 named!(capture_by -> CaptureBy, alt!(
866 keyword!("move") => { |_| CaptureBy::Value }
867 |
868 epsilon!() => { |_| CaptureBy::Ref }
869 ));
870
David Tolnay8b07f372016-09-30 10:28:40 -0700871 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -0700872}
873
David Tolnayf4bbbd92016-09-23 14:41:55 -0700874#[cfg(feature = "printing")]
875mod printing {
876 use super::*;
David Tolnay89e05672016-10-02 14:39:42 -0700877 use {FunctionRetTy, Mutability, Ty};
David Tolnayf4bbbd92016-09-23 14:41:55 -0700878 use quote::{Tokens, ToTokens};
879
880 impl ToTokens for Expr {
881 fn to_tokens(&self, tokens: &mut Tokens) {
882 match *self {
David Tolnaybb6feae2016-10-02 21:25:20 -0700883 Expr::Box(ref inner) => {
884 tokens.append("box");
885 inner.to_tokens(tokens);
886 }
887 Expr::Vec(ref tys) => {
888 tokens.append("[");
889 tokens.append_separated(tys, ",");
890 tokens.append("]");
891 }
David Tolnay9636c052016-10-02 17:11:17 -0700892 Expr::Call(ref func, ref args) => {
893 func.to_tokens(tokens);
894 tokens.append("(");
895 tokens.append_separated(args, ",");
896 tokens.append(")");
897 }
898 Expr::MethodCall(ref ident, ref ascript, ref args) => {
899 args[0].to_tokens(tokens);
900 tokens.append(".");
901 ident.to_tokens(tokens);
902 if ascript.len() > 0 {
903 tokens.append("::");
904 tokens.append("<");
905 tokens.append_separated(ascript, ",");
906 tokens.append(">");
907 }
908 tokens.append("(");
909 tokens.append_separated(&args[1..], ",");
910 tokens.append(")");
911 }
David Tolnay47a877c2016-10-01 16:50:55 -0700912 Expr::Tup(ref fields) => {
913 tokens.append("(");
914 tokens.append_separated(fields, ",");
915 if fields.len() == 1 {
916 tokens.append(",");
917 }
918 tokens.append(")");
919 }
David Tolnay89e05672016-10-02 14:39:42 -0700920 Expr::Binary(op, ref left, ref right) => {
921 left.to_tokens(tokens);
922 op.to_tokens(tokens);
923 right.to_tokens(tokens);
924 }
David Tolnay3c2467c2016-10-02 17:55:08 -0700925 Expr::Unary(op, ref expr) => {
926 op.to_tokens(tokens);
927 expr.to_tokens(tokens);
928 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700929 Expr::Lit(ref lit) => lit.to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -0700930 Expr::Cast(ref expr, ref ty) => {
931 expr.to_tokens(tokens);
932 tokens.append("as");
933 ty.to_tokens(tokens);
934 }
935 Expr::Type(ref expr, ref ty) => {
936 expr.to_tokens(tokens);
937 tokens.append(":");
938 ty.to_tokens(tokens);
939 }
940 Expr::If(ref cond, ref then_block, ref else_block) => {
941 tokens.append("if");
942 cond.to_tokens(tokens);
943 then_block.to_tokens(tokens);
944 if let Some(ref else_block) = *else_block {
945 tokens.append("else");
946 else_block.to_tokens(tokens);
947 }
948 }
David Tolnay29f9ce12016-10-02 20:58:40 -0700949 Expr::IfLet(ref pat, ref expr, ref then_block, ref else_block) => {
950 tokens.append("if");
951 tokens.append("let");
952 pat.to_tokens(tokens);
953 tokens.append("=");
954 expr.to_tokens(tokens);
955 then_block.to_tokens(tokens);
956 if let Some(ref else_block) = *else_block {
957 tokens.append("else");
958 else_block.to_tokens(tokens);
959 }
960 }
David Tolnaybb6feae2016-10-02 21:25:20 -0700961 Expr::While(ref cond, ref body, ref label) => {
962 if let Some(ref label) = *label {
963 label.to_tokens(tokens);
964 tokens.append(":");
965 }
966 tokens.append("while");
967 cond.to_tokens(tokens);
968 body.to_tokens(tokens);
969 }
970 Expr::WhileLet(ref pat, ref expr, ref body, ref label) => {
971 if let Some(ref label) = *label {
972 label.to_tokens(tokens);
973 tokens.append(":");
974 }
975 tokens.append("while");
976 tokens.append("let");
977 pat.to_tokens(tokens);
978 tokens.append("=");
979 expr.to_tokens(tokens);
980 body.to_tokens(tokens);
981 }
982 Expr::ForLoop(ref pat, ref expr, ref body, ref label) => {
983 if let Some(ref label) = *label {
984 label.to_tokens(tokens);
985 tokens.append(":");
986 }
987 tokens.append("for");
988 pat.to_tokens(tokens);
989 tokens.append("in");
990 expr.to_tokens(tokens);
991 body.to_tokens(tokens);
992 }
993 Expr::Loop(ref body, ref label) => {
994 if let Some(ref label) = *label {
995 label.to_tokens(tokens);
996 tokens.append(":");
997 }
998 tokens.append("loop");
999 body.to_tokens(tokens);
1000 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001001 Expr::Match(ref expr, ref arms) => {
1002 tokens.append("match");
1003 expr.to_tokens(tokens);
1004 tokens.append("{");
1005 tokens.append_separated(arms, ",");
1006 tokens.append("}");
1007 }
David Tolnay89e05672016-10-02 14:39:42 -07001008 Expr::Closure(capture, ref decl, ref body) => {
1009 capture.to_tokens(tokens);
1010 tokens.append("|");
1011 for (i, input) in decl.inputs.iter().enumerate() {
1012 if i > 0 {
1013 tokens.append(",");
1014 }
1015 input.pat.to_tokens(tokens);
1016 match input.ty {
1017 Ty::Infer => { /* nothing */ }
1018 _ => {
1019 tokens.append(":");
1020 input.ty.to_tokens(tokens);
1021 }
1022 }
1023 }
1024 tokens.append("|");
1025 match decl.output {
1026 FunctionRetTy::Default => {
1027 if body.stmts.len() == 1 {
1028 if let Stmt::Expr(ref expr) = body.stmts[0] {
1029 expr.to_tokens(tokens);
1030 } else {
1031 body.to_tokens(tokens);
1032 }
1033 } else {
1034 body.to_tokens(tokens);
1035 }
1036 }
1037 FunctionRetTy::Ty(ref ty) => {
1038 tokens.append("->");
1039 ty.to_tokens(tokens);
1040 body.to_tokens(tokens);
1041 }
1042 }
1043 }
1044 Expr::Block(rules, ref block) => {
1045 rules.to_tokens(tokens);
1046 block.to_tokens(tokens);
1047 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001048 Expr::Assign(ref var, ref expr) => {
1049 var.to_tokens(tokens);
1050 tokens.append("=");
1051 expr.to_tokens(tokens);
1052 }
1053 Expr::AssignOp(op, ref var, ref expr) => {
1054 var.to_tokens(tokens);
1055 tokens.append(op.assign_op());
1056 expr.to_tokens(tokens);
1057 }
1058 Expr::Field(ref expr, ref field) => {
1059 expr.to_tokens(tokens);
1060 tokens.append(".");
1061 field.to_tokens(tokens);
1062 }
1063 Expr::TupField(ref expr, field) => {
1064 expr.to_tokens(tokens);
1065 tokens.append(".");
1066 tokens.append(&field.to_string());
1067 }
1068 Expr::Index(ref expr, ref index) => {
1069 expr.to_tokens(tokens);
1070 tokens.append("[");
1071 index.to_tokens(tokens);
1072 tokens.append("]");
1073 }
1074 Expr::Range(ref from, ref to, limits) => {
1075 from.to_tokens(tokens);
1076 match limits {
1077 RangeLimits::HalfOpen => tokens.append(".."),
1078 RangeLimits::Closed => tokens.append("..."),
1079 }
1080 to.to_tokens(tokens);
1081 }
David Tolnay89e05672016-10-02 14:39:42 -07001082 Expr::Path(None, ref path) => {
1083 path.to_tokens(tokens);
1084 }
1085 Expr::Path(Some(ref qself), ref path) => {
1086 tokens.append("<");
1087 qself.ty.to_tokens(tokens);
1088 if qself.position > 0 {
1089 tokens.append("as");
1090 for (i, segment) in path.segments.iter()
1091 .take(qself.position)
1092 .enumerate()
1093 {
1094 if i > 0 || path.global {
1095 tokens.append("::");
1096 }
1097 segment.to_tokens(tokens);
1098 }
1099 }
1100 tokens.append(">");
1101 for segment in path.segments.iter().skip(qself.position) {
1102 tokens.append("::");
1103 segment.to_tokens(tokens);
1104 }
1105 }
David Tolnay3c2467c2016-10-02 17:55:08 -07001106 Expr::AddrOf(mutability, ref expr) => {
1107 tokens.append("&");
1108 mutability.to_tokens(tokens);
1109 expr.to_tokens(tokens);
1110 }
1111 Expr::Break(ref opt_label) => {
1112 tokens.append("break");
1113 opt_label.to_tokens(tokens);
1114 }
1115 Expr::Continue(ref opt_label) => {
1116 tokens.append("continue");
1117 opt_label.to_tokens(tokens);
1118 }
David Tolnay42602292016-10-01 22:25:45 -07001119 Expr::Ret(ref opt_expr) => {
1120 tokens.append("return");
1121 opt_expr.to_tokens(tokens);
1122 }
David Tolnay47a877c2016-10-01 16:50:55 -07001123 Expr::Mac(ref _mac) => unimplemented!(),
David Tolnay055a7042016-10-02 19:23:54 -07001124 Expr::Struct(ref path, ref fields, ref base) => {
1125 path.to_tokens(tokens);
1126 tokens.append("{");
1127 tokens.append_separated(fields, ",");
1128 if let Some(ref base) = *base {
1129 if !fields.is_empty() {
1130 tokens.append(",");
1131 }
1132 tokens.append("..");
1133 base.to_tokens(tokens);
1134 }
1135 tokens.append("}");
1136 }
1137 Expr::Repeat(ref expr, ref times) => {
1138 tokens.append("[");
1139 expr.to_tokens(tokens);
1140 tokens.append(";");
1141 times.to_tokens(tokens);
1142 tokens.append("]");
1143 }
David Tolnay89e05672016-10-02 14:39:42 -07001144 Expr::Paren(ref expr) => {
1145 tokens.append("(");
1146 expr.to_tokens(tokens);
1147 tokens.append(")");
1148 }
David Tolnay3c2467c2016-10-02 17:55:08 -07001149 Expr::Try(ref expr) => {
1150 expr.to_tokens(tokens);
1151 tokens.append("?");
1152 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001153 }
1154 }
1155 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001156
David Tolnaybb6feae2016-10-02 21:25:20 -07001157 impl BinOp {
1158 fn op(&self) -> &'static str {
1159 match *self {
1160 BinOp::Add => "+",
1161 BinOp::Sub => "-",
1162 BinOp::Mul => "*",
1163 BinOp::Div => "/",
1164 BinOp::Rem => "%",
1165 BinOp::And => "&&",
1166 BinOp::Or => "||",
1167 BinOp::BitXor => "^",
1168 BinOp::BitAnd => "&",
1169 BinOp::BitOr => "|",
1170 BinOp::Shl => "<<",
1171 BinOp::Shr => ">>",
1172 BinOp::Eq => "==",
1173 BinOp::Lt => "<",
1174 BinOp::Le => "<=",
1175 BinOp::Ne => "!=",
1176 BinOp::Ge => ">=",
1177 BinOp::Gt => ">",
1178 }
1179 }
1180
1181 fn assign_op(&self) -> &'static str {
1182 match *self {
1183 BinOp::Add => "+=",
1184 BinOp::Sub => "-=",
1185 BinOp::Mul => "*=",
1186 BinOp::Div => "/=",
1187 BinOp::Rem => "%=",
1188 BinOp::BitXor => "^=",
1189 BinOp::BitAnd => "&=",
1190 BinOp::BitOr => "|=",
1191 BinOp::Shl => "<<=",
1192 BinOp::Shr => ">>=",
1193 _ => panic!("bad assignment operator"),
1194 }
1195 }
1196 }
1197
David Tolnay89e05672016-10-02 14:39:42 -07001198 impl ToTokens for BinOp {
1199 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb6feae2016-10-02 21:25:20 -07001200 tokens.append(self.op());
1201 }
1202 }
1203
1204 impl UnOp {
1205 fn op(&self) -> &'static str {
David Tolnay89e05672016-10-02 14:39:42 -07001206 match *self {
David Tolnaybb6feae2016-10-02 21:25:20 -07001207 UnOp::Deref => "*",
1208 UnOp::Not => "!",
1209 UnOp::Neg => "-",
David Tolnay89e05672016-10-02 14:39:42 -07001210 }
1211 }
1212 }
1213
David Tolnay3c2467c2016-10-02 17:55:08 -07001214 impl ToTokens for UnOp {
1215 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaybb6feae2016-10-02 21:25:20 -07001216 tokens.append(self.op());
David Tolnay3c2467c2016-10-02 17:55:08 -07001217 }
1218 }
1219
David Tolnay055a7042016-10-02 19:23:54 -07001220 impl ToTokens for FieldValue {
1221 fn to_tokens(&self, tokens: &mut Tokens) {
1222 self.ident.to_tokens(tokens);
1223 tokens.append(":");
1224 self.expr.to_tokens(tokens);
1225 }
1226 }
1227
David Tolnayb4ad3b52016-10-01 21:58:13 -07001228 impl ToTokens for Arm {
1229 fn to_tokens(&self, tokens: &mut Tokens) {
1230 for attr in &self.attrs {
1231 attr.to_tokens(tokens);
1232 }
1233 tokens.append_separated(&self.pats, "|");
1234 if let Some(ref guard) = self.guard {
1235 tokens.append("if");
1236 guard.to_tokens(tokens);
1237 }
1238 tokens.append("=>");
1239 self.body.to_tokens(tokens);
1240 match *self.body {
David Tolnay89e05672016-10-02 14:39:42 -07001241 Expr::Block(_, _) => { /* no comma */ }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001242 _ => tokens.append(","),
1243 }
1244 }
1245 }
1246
1247 impl ToTokens for Pat {
1248 fn to_tokens(&self, tokens: &mut Tokens) {
1249 match *self {
1250 Pat::Wild => tokens.append("_"),
1251 Pat::Ident(mode, ref ident, ref subpat) => {
1252 mode.to_tokens(tokens);
1253 ident.to_tokens(tokens);
1254 if let Some(ref subpat) = *subpat {
1255 tokens.append("@");
1256 subpat.to_tokens(tokens);
1257 }
1258 }
1259 Pat::Struct(ref _path, ref _fields, _dots) => unimplemented!(),
1260 Pat::TupleStruct(ref _path, ref _pats, _dotpos) => unimplemented!(),
1261 Pat::Path(ref _qself, ref _path) => unimplemented!(),
1262 Pat::Tuple(ref _pats, _dotpos) => unimplemented!(),
1263 Pat::Box(ref _inner) => unimplemented!(),
1264 Pat::Ref(ref _target, _mutability) => unimplemented!(),
1265 Pat::Lit(ref _expr) => unimplemented!(),
1266 Pat::Range(ref _lower, ref _upper) => unimplemented!(),
1267 Pat::Vec(ref _before, ref _dots, ref _after) => unimplemented!(),
1268 Pat::Mac(ref _mac) => unimplemented!(),
1269 }
1270 }
1271 }
1272
1273 impl ToTokens for BindingMode {
1274 fn to_tokens(&self, tokens: &mut Tokens) {
1275 match *self {
1276 BindingMode::ByRef(Mutability::Immutable) => {
1277 tokens.append("ref");
1278 }
1279 BindingMode::ByRef(Mutability::Mutable) => {
1280 tokens.append("ref");
1281 tokens.append("mut");
1282 }
1283 BindingMode::ByValue(Mutability::Immutable) => {}
1284 BindingMode::ByValue(Mutability::Mutable) => {
1285 tokens.append("mut");
1286 }
1287 }
1288 }
1289 }
David Tolnay42602292016-10-01 22:25:45 -07001290
David Tolnay89e05672016-10-02 14:39:42 -07001291 impl ToTokens for CaptureBy {
1292 fn to_tokens(&self, tokens: &mut Tokens) {
1293 match *self {
1294 CaptureBy::Value => tokens.append("move"),
1295 CaptureBy::Ref => { /* nothing */ }
1296 }
1297 }
1298 }
1299
David Tolnay42602292016-10-01 22:25:45 -07001300 impl ToTokens for Block {
1301 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay42602292016-10-01 22:25:45 -07001302 tokens.append("{");
1303 for stmt in &self.stmts {
1304 stmt.to_tokens(tokens);
1305 }
1306 tokens.append("}");
1307 }
1308 }
1309
1310 impl ToTokens for BlockCheckMode {
1311 fn to_tokens(&self, tokens: &mut Tokens) {
1312 match *self {
David Tolnay89e05672016-10-02 14:39:42 -07001313 BlockCheckMode::Default => { /* nothing */ }
David Tolnay42602292016-10-01 22:25:45 -07001314 BlockCheckMode::Unsafe => tokens.append("unsafe"),
1315 }
1316 }
1317 }
1318
1319 impl ToTokens for Stmt {
1320 fn to_tokens(&self, tokens: &mut Tokens) {
1321 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07001322 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07001323 Stmt::Item(ref item) => item.to_tokens(tokens),
1324 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
1325 Stmt::Semi(ref expr) => {
1326 expr.to_tokens(tokens);
1327 tokens.append(";");
1328 }
1329 Stmt::Mac(ref _mac) => unimplemented!(),
1330 }
1331 }
1332 }
David Tolnay191e0582016-10-02 18:31:09 -07001333
1334 impl ToTokens for Local {
1335 fn to_tokens(&self, tokens: &mut Tokens) {
1336 tokens.append("let");
1337 self.pat.to_tokens(tokens);
1338 if let Some(ref ty) = self.ty {
1339 tokens.append(":");
1340 ty.to_tokens(tokens);
1341 }
1342 if let Some(ref init) = self.init {
1343 tokens.append("=");
1344 init.to_tokens(tokens);
1345 }
1346 tokens.append(";");
1347 }
1348 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07001349}