David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
| 3 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 4 | pub enum Expr { |
| 5 | /// A `box x` expression. |
| 6 | Box(Box<Expr>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 7 | /// 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 Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 37 | /// Type ascription (`foo: f64`) |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 38 | Type(Box<Expr>, Box<Ty>), |
| 39 | /// An `if` block, with an optional else block |
| 40 | /// |
| 41 | /// `if expr { block } else { expr }` |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 42 | If(Box<Expr>, Block, Option<Box<Expr>>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 43 | /// 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 Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 48 | IfLet(Box<Pat>, Box<Expr>, Block, Option<Box<Expr>>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 49 | /// A while loop, with an optional label |
| 50 | /// |
| 51 | /// `'label: while expr { block }` |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 52 | While(Box<Expr>, Block, Option<Ident>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 53 | /// 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 Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 58 | WhileLet(Box<Pat>, Box<Expr>, Block, Option<Ident>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 59 | /// 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 Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 64 | ForLoop(Box<Pat>, Box<Expr>, Block, Option<Ident>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 65 | /// Conditionless loop (can be exited with break, continue, or return) |
| 66 | /// |
| 67 | /// `'label: loop { block }` |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 68 | Loop(Block, Option<Ident>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 69 | /// A `match` block. |
| 70 | Match(Box<Expr>, Vec<Arm>), |
| 71 | /// A closure (for example, `move |a, b, c| {a + b + c}`) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 72 | Closure(CaptureBy, Box<FnDecl>, Block), |
| 73 | /// A block (`{ ... }` or `unsafe { ... }`) |
| 74 | Block(BlockCheckMode, Block), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 75 | |
| 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 Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 116 | Struct(Path, Vec<FieldValue>, Option<Box<Expr>>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 117 | |
| 118 | /// An array literal constructed from one repeated element. |
| 119 | /// |
| 120 | /// For example, `[1; 5]`. The first expression is the element |
| 121 | /// to be repeated; the second is the number of times to repeat it. |
| 122 | Repeat(Box<Expr>, Box<Expr>), |
| 123 | |
| 124 | /// No-op: used solely so we can pretty-print faithfully |
| 125 | Paren(Box<Expr>), |
| 126 | |
| 127 | /// `expr?` |
| 128 | Try(Box<Expr>), |
| 129 | } |
| 130 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 131 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 132 | pub struct FieldValue { |
| 133 | pub ident: Ident, |
| 134 | pub expr: Expr, |
| 135 | } |
| 136 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 137 | /// A Block (`{ .. }`). |
| 138 | /// |
| 139 | /// E.g. `{ .. }` as in `fn foo() { .. }` |
| 140 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 141 | pub struct Block { |
| 142 | /// Statements in a block |
| 143 | pub stmts: Vec<Stmt>, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 147 | pub enum BlockCheckMode { |
| 148 | Default, |
| 149 | Unsafe, |
| 150 | } |
| 151 | |
| 152 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 153 | pub 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)] |
| 169 | pub 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)] |
| 183 | pub 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 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 191 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 192 | pub enum Pat { |
| 193 | /// Represents a wildcard pattern (`_`) |
| 194 | Wild, |
| 195 | |
David Tolnay | 432afc0 | 2016-09-24 07:37:13 -0700 | [diff] [blame] | 196 | /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 197 | /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third |
| 198 | /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens |
| 199 | /// during name resolution. |
| 200 | Ident(BindingMode, Ident, Option<Box<Pat>>), |
| 201 | |
| 202 | /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`. |
| 203 | /// The `bool` is `true` in the presence of a `..`. |
| 204 | Struct(Path, Vec<FieldPat>, bool), |
| 205 | |
| 206 | /// A tuple struct/variant pattern `Variant(x, y, .., z)`. |
| 207 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 208 | /// 0 <= position <= subpats.len() |
| 209 | TupleStruct(Path, Vec<Pat>, Option<usize>), |
| 210 | |
| 211 | /// A possibly qualified path pattern. |
| 212 | /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants |
| 213 | /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can |
| 214 | /// only legally refer to associated constants. |
| 215 | Path(Option<QSelf>, Path), |
| 216 | |
| 217 | /// A tuple pattern `(a, b)`. |
| 218 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 219 | /// 0 <= position <= subpats.len() |
| 220 | Tuple(Vec<Pat>, Option<usize>), |
| 221 | /// A `box` pattern |
| 222 | Box(Box<Pat>), |
| 223 | /// A reference pattern, e.g. `&mut (a, b)` |
| 224 | Ref(Box<Pat>, Mutability), |
| 225 | /// A literal |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 226 | Lit(Box<Lit>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 227 | /// A range pattern, e.g. `1...2` |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 228 | Range(Box<Lit>, Box<Lit>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 229 | /// `[a, b, ..i, y, z]` is represented as: |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 230 | /// `Pat::Slice(box [a, b], Some(i), box [y, z])` |
| 231 | Slice(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 232 | /// A macro pattern; pre-expansion |
| 233 | Mac(Mac), |
| 234 | } |
| 235 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 236 | /// An arm of a 'match'. |
| 237 | /// |
| 238 | /// E.g. `0...10 => { println!("match!") }` as in |
| 239 | /// |
| 240 | /// ```rust,ignore |
| 241 | /// match n { |
| 242 | /// 0...10 => { println!("match!") }, |
| 243 | /// // .. |
| 244 | /// } |
| 245 | /// ``` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 246 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 247 | pub struct Arm { |
| 248 | pub attrs: Vec<Attribute>, |
| 249 | pub pats: Vec<Pat>, |
| 250 | pub guard: Option<Box<Expr>>, |
| 251 | pub body: Box<Expr>, |
| 252 | } |
| 253 | |
| 254 | /// A capture clause |
| 255 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 256 | pub enum CaptureBy { |
| 257 | Value, |
| 258 | Ref, |
| 259 | } |
| 260 | |
| 261 | /// Limit types of a range (inclusive or exclusive) |
| 262 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 263 | pub enum RangeLimits { |
| 264 | /// Inclusive at the beginning, exclusive at the end |
| 265 | HalfOpen, |
| 266 | /// Inclusive at the beginning and end |
| 267 | Closed, |
| 268 | } |
| 269 | |
| 270 | /// A single field in a struct pattern |
| 271 | /// |
| 272 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` |
David Tolnay | 181bac5 | 2016-09-24 00:10:05 -0700 | [diff] [blame] | 273 | /// are treated the same as `x: x, y: ref y, z: ref mut z`, |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 274 | /// except `is_shorthand` is true |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 275 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 276 | pub struct FieldPat { |
| 277 | /// The identifier for the field |
| 278 | pub ident: Ident, |
| 279 | /// The pattern the field is destructured to |
| 280 | pub pat: Box<Pat>, |
| 281 | pub is_shorthand: bool, |
| 282 | } |
| 283 | |
| 284 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 285 | pub enum BindingMode { |
| 286 | ByRef(Mutability), |
| 287 | ByValue(Mutability), |
| 288 | } |
| 289 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 290 | #[cfg(feature = "parsing")] |
| 291 | pub mod parsing { |
| 292 | use super::*; |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 293 | use {BinOp, Delimited, DelimToken, FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, TokenTree, Ty}; |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 294 | use attr::parsing::outer_attr; |
Gregory Katz | 1b69f68 | 2016-09-27 21:06:09 -0400 | [diff] [blame] | 295 | use generics::parsing::lifetime; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 296 | use ident::parsing::ident; |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 297 | use item::parsing::item; |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame^] | 298 | use lit::parsing::{digits, lit}; |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 299 | use mac::parsing::mac; |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 300 | use nom::IResult::Error; |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame^] | 301 | use op::parsing::{assign_op, binop, unop}; |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 302 | use ty::parsing::{mutability, path, qpath, ty}; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 303 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 304 | named!(pub expr -> Expr, do_parse!( |
| 305 | mut e: alt!( |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 306 | expr_lit // must be before expr_struct |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 307 | | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 308 | expr_struct // must be before expr_path |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 309 | | |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 310 | expr_paren // must be before expr_tup |
| 311 | | |
| 312 | expr_mac // must be before expr_path |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 313 | | |
David Tolnay | 4c9be37 | 2016-10-06 00:47:37 -0700 | [diff] [blame] | 314 | expr_break // must be before expr_path |
| 315 | | |
| 316 | expr_continue // must be before expr_path |
| 317 | | |
| 318 | expr_ret // must be before expr_path |
| 319 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 320 | expr_box |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 321 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 322 | expr_vec |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 323 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 324 | expr_tup |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 325 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 326 | expr_unary |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 327 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 328 | expr_if |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 329 | | |
| 330 | expr_while |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 331 | | |
| 332 | expr_for_loop |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 333 | | |
| 334 | expr_loop |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 335 | | |
| 336 | expr_match |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 337 | | |
| 338 | expr_closure |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 339 | | |
| 340 | expr_block |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 341 | | |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame^] | 342 | expr_range |
| 343 | | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 344 | expr_path |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 345 | | |
| 346 | expr_addr_of |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 347 | | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 348 | expr_repeat |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 349 | ) >> |
| 350 | many0!(alt!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 351 | tap!(args: and_call => { |
| 352 | e = Expr::Call(Box::new(e), args); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 353 | }) |
| 354 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 355 | tap!(more: and_method_call => { |
| 356 | let (method, ascript, mut args) = more; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 357 | args.insert(0, e); |
| 358 | e = Expr::MethodCall(method, ascript, args); |
| 359 | }) |
| 360 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 361 | tap!(more: and_binary => { |
| 362 | let (op, other) = more; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 363 | e = Expr::Binary(op, Box::new(e), Box::new(other)); |
| 364 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 365 | | |
| 366 | tap!(ty: and_cast => { |
| 367 | e = Expr::Cast(Box::new(e), Box::new(ty)); |
| 368 | }) |
| 369 | | |
| 370 | tap!(ty: and_ascription => { |
| 371 | e = Expr::Type(Box::new(e), Box::new(ty)); |
| 372 | }) |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame^] | 373 | | |
| 374 | tap!(v: and_assign => { |
| 375 | e = Expr::Assign(Box::new(e), Box::new(v)); |
| 376 | }) |
| 377 | | |
| 378 | tap!(more: and_assign_op => { |
| 379 | let (op, v) = more; |
| 380 | e = Expr::AssignOp(op, Box::new(e), Box::new(v)); |
| 381 | }) |
| 382 | | |
| 383 | tap!(field: and_field => { |
| 384 | e = Expr::Field(Box::new(e), field); |
| 385 | }) |
| 386 | | |
| 387 | tap!(field: and_tup_field => { |
| 388 | e = Expr::TupField(Box::new(e), field as usize); |
| 389 | }) |
| 390 | | |
| 391 | tap!(i: and_index => { |
| 392 | e = Expr::Index(Box::new(e), Box::new(i)); |
| 393 | }) |
| 394 | | |
| 395 | tap!(more: and_range => { |
| 396 | let (limits, hi) = more; |
| 397 | e = Expr::Range(Some(Box::new(e)), hi.map(Box::new), limits); |
| 398 | }) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 399 | | |
| 400 | tap!(_try: punct!("?") => { |
| 401 | e = Expr::Try(Box::new(e)); |
| 402 | }) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 403 | )) >> |
| 404 | (e) |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 405 | )); |
| 406 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 407 | named!(expr_mac -> Expr, map!(mac, Expr::Mac)); |
| 408 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 409 | named!(expr_paren -> Expr, do_parse!( |
| 410 | punct!("(") >> |
| 411 | e: expr >> |
| 412 | punct!(")") >> |
| 413 | (Expr::Paren(Box::new(e))) |
| 414 | )); |
| 415 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 416 | named!(expr_box -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 417 | keyword!("box") >> |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 418 | inner: expr >> |
| 419 | (Expr::Box(Box::new(inner))) |
| 420 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 421 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 422 | named!(expr_vec -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 423 | punct!("[") >> |
| 424 | elems: separated_list!(punct!(","), expr) >> |
| 425 | punct!("]") >> |
| 426 | (Expr::Vec(elems)) |
| 427 | )); |
| 428 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 429 | named!(and_call -> Vec<Expr>, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 430 | punct!("(") >> |
| 431 | args: separated_list!(punct!(","), expr) >> |
| 432 | punct!(")") >> |
| 433 | (args) |
| 434 | )); |
| 435 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 436 | named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 437 | punct!(".") >> |
| 438 | method: ident >> |
| 439 | ascript: opt_vec!(delimited!( |
| 440 | punct!("<"), |
| 441 | separated_list!(punct!(","), ty), |
| 442 | punct!(">") |
| 443 | )) >> |
| 444 | punct!("(") >> |
| 445 | args: separated_list!(punct!(","), expr) >> |
| 446 | punct!(")") >> |
| 447 | (method, ascript, args) |
| 448 | )); |
| 449 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 450 | named!(expr_tup -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 451 | punct!("(") >> |
| 452 | elems: separated_list!(punct!(","), expr) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 453 | option!(punct!(",")) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 454 | punct!(")") >> |
| 455 | (Expr::Tup(elems)) |
| 456 | )); |
| 457 | |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 458 | named!(and_binary -> (BinOp, Expr), tuple!(binop, expr)); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 459 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 460 | named!(expr_unary -> Expr, do_parse!( |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 461 | operator: unop >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 462 | operand: expr >> |
| 463 | (Expr::Unary(operator, Box::new(operand))) |
| 464 | )); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 465 | |
| 466 | named!(expr_lit -> Expr, map!(lit, Expr::Lit)); |
| 467 | |
| 468 | named!(and_cast -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 469 | keyword!("as") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 470 | ty: ty >> |
| 471 | (ty) |
| 472 | )); |
| 473 | |
| 474 | named!(and_ascription -> Ty, preceded!(punct!(":"), ty)); |
| 475 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 476 | enum Cond { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 477 | Let(Pat, Expr), |
| 478 | Expr(Expr), |
| 479 | } |
| 480 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 481 | named!(cond -> Cond, alt!( |
| 482 | do_parse!( |
| 483 | keyword!("let") >> |
| 484 | pat: pat >> |
| 485 | punct!("=") >> |
| 486 | value: expr >> |
| 487 | (Cond::Let(pat, value)) |
| 488 | ) |
| 489 | | |
| 490 | map!(expr, Cond::Expr) |
| 491 | )); |
| 492 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 493 | named!(expr_if -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 494 | keyword!("if") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 495 | cond: cond >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 496 | punct!("{") >> |
| 497 | then_block: within_block >> |
| 498 | punct!("}") >> |
| 499 | else_block: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 500 | keyword!("else"), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 501 | alt!( |
| 502 | expr_if |
| 503 | | |
| 504 | do_parse!( |
| 505 | punct!("{") >> |
| 506 | else_block: within_block >> |
| 507 | punct!("}") >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 508 | (Expr::Block(BlockCheckMode::Default, Block { |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 509 | stmts: else_block, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 510 | })) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 511 | ) |
| 512 | ) |
| 513 | )) >> |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 514 | (match cond { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 515 | Cond::Let(pat, expr) => Expr::IfLet( |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 516 | Box::new(pat), |
| 517 | Box::new(expr), |
| 518 | Block { |
| 519 | stmts: then_block, |
| 520 | }, |
| 521 | else_block.map(Box::new), |
| 522 | ), |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 523 | Cond::Expr(cond) => Expr::If( |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 524 | Box::new(cond), |
| 525 | Block { |
| 526 | stmts: then_block, |
| 527 | }, |
| 528 | else_block.map(Box::new), |
| 529 | ), |
| 530 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 531 | )); |
| 532 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 533 | named!(expr_for_loop -> Expr, do_parse!( |
| 534 | lbl: option!(terminated!(label, punct!(":"))) >> |
| 535 | keyword!("for") >> |
| 536 | pat: pat >> |
| 537 | keyword!("in") >> |
| 538 | expr: expr >> |
| 539 | loop_block: block >> |
| 540 | (Expr::ForLoop(Box::new(pat), Box::new(expr), loop_block, lbl)) |
| 541 | )); |
| 542 | |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 543 | named!(expr_loop -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 544 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 545 | keyword!("loop") >> |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 546 | loop_block: block >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 547 | (Expr::Loop(loop_block, lbl)) |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 548 | )); |
| 549 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 550 | named!(expr_match -> Expr, do_parse!( |
| 551 | keyword!("match") >> |
| 552 | obj: expr >> |
| 553 | punct!("{") >> |
| 554 | arms: many0!(do_parse!( |
| 555 | attrs: many0!(outer_attr) >> |
| 556 | pats: separated_nonempty_list!(punct!("|"), pat) >> |
| 557 | guard: option!(preceded!(keyword!("if"), expr)) >> |
| 558 | punct!("=>") >> |
| 559 | body: alt!( |
| 560 | terminated!(expr, punct!(",")) |
| 561 | | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 562 | map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk)) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 563 | ) >> |
| 564 | (Arm { |
| 565 | attrs: attrs, |
| 566 | pats: pats, |
| 567 | guard: guard.map(Box::new), |
| 568 | body: Box::new(body), |
| 569 | }) |
| 570 | )) >> |
| 571 | punct!("}") >> |
| 572 | (Expr::Match(Box::new(obj), arms)) |
| 573 | )); |
| 574 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 575 | named!(expr_closure -> Expr, do_parse!( |
| 576 | capture: capture_by >> |
| 577 | punct!("|") >> |
| 578 | inputs: separated_list!(punct!(","), closure_arg) >> |
| 579 | punct!("|") >> |
| 580 | ret_and_body: alt!( |
| 581 | do_parse!( |
| 582 | punct!("->") >> |
| 583 | ty: ty >> |
| 584 | body: block >> |
| 585 | ((FunctionRetTy::Ty(ty), body)) |
| 586 | ) |
| 587 | | |
| 588 | map!(expr, |e| ( |
| 589 | FunctionRetTy::Default, |
| 590 | Block { |
| 591 | stmts: vec![Stmt::Expr(Box::new(e))], |
| 592 | }, |
| 593 | )) |
| 594 | ) >> |
| 595 | (Expr::Closure( |
| 596 | capture, |
| 597 | Box::new(FnDecl { |
| 598 | inputs: inputs, |
| 599 | output: ret_and_body.0, |
| 600 | }), |
| 601 | ret_and_body.1, |
| 602 | )) |
| 603 | )); |
| 604 | |
| 605 | named!(closure_arg -> FnArg, do_parse!( |
| 606 | pat: pat >> |
| 607 | ty: option!(preceded!(punct!(":"), ty)) >> |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 608 | (FnArg::Captured(pat, ty.unwrap_or(Ty::Infer))) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 609 | )); |
| 610 | |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 611 | named!(expr_while -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 612 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 57ffbde | 2016-09-30 09:38:04 -0700 | [diff] [blame] | 613 | keyword!("while") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 614 | cond: cond >> |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 615 | while_block: block >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 616 | (match cond { |
| 617 | Cond::Let(pat, expr) => Expr::WhileLet( |
| 618 | Box::new(pat), |
| 619 | Box::new(expr), |
| 620 | while_block, |
| 621 | lbl, |
| 622 | ), |
| 623 | Cond::Expr(cond) => Expr::While( |
| 624 | Box::new(cond), |
| 625 | while_block, |
| 626 | lbl, |
| 627 | ), |
| 628 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 629 | )); |
| 630 | |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 631 | named!(expr_continue -> Expr, do_parse!( |
| 632 | keyword!("continue") >> |
| 633 | lbl: option!(label) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 634 | (Expr::Continue(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 635 | )); |
| 636 | |
| 637 | named!(expr_break -> Expr, do_parse!( |
| 638 | keyword!("break") >> |
| 639 | lbl: option!(label) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 640 | (Expr::Break(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 641 | )); |
| 642 | |
| 643 | named!(expr_ret -> Expr, do_parse!( |
| 644 | keyword!("return") >> |
| 645 | ret_value: option!(expr) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 646 | (Expr::Ret(ret_value.map(Box::new))) |
| 647 | )); |
| 648 | |
| 649 | named!(expr_struct -> Expr, do_parse!( |
| 650 | path: path >> |
| 651 | punct!("{") >> |
| 652 | fields: separated_list!(punct!(","), field_value) >> |
| 653 | base: option!(do_parse!( |
| 654 | cond!(!fields.is_empty(), punct!(",")) >> |
| 655 | punct!("..") >> |
| 656 | base: expr >> |
| 657 | (base) |
| 658 | )) >> |
| 659 | punct!("}") >> |
| 660 | (Expr::Struct(path, fields, base.map(Box::new))) |
| 661 | )); |
| 662 | |
| 663 | named!(field_value -> FieldValue, do_parse!( |
| 664 | name: ident >> |
| 665 | punct!(":") >> |
| 666 | value: expr >> |
| 667 | (FieldValue { |
| 668 | ident: name, |
| 669 | expr: value, |
| 670 | }) |
| 671 | )); |
| 672 | |
| 673 | named!(expr_repeat -> Expr, do_parse!( |
| 674 | punct!("[") >> |
| 675 | value: expr >> |
| 676 | punct!(";") >> |
| 677 | times: expr >> |
| 678 | punct!("]") >> |
| 679 | (Expr::Repeat(Box::new(value), Box::new(times))) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 680 | )); |
| 681 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 682 | named!(expr_block -> Expr, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 683 | rules: block_check_mode >> |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 684 | b: block >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 685 | (Expr::Block(rules, Block { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 686 | stmts: b.stmts, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 687 | })) |
| 688 | )); |
| 689 | |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame^] | 690 | named!(expr_range -> Expr, do_parse!( |
| 691 | limits: range_limits >> |
| 692 | hi: option!(expr) >> |
| 693 | (Expr::Range(None, hi.map(Box::new), limits)) |
| 694 | )); |
| 695 | |
| 696 | named!(range_limits -> RangeLimits, alt!( |
| 697 | punct!("...") => { |_| RangeLimits::Closed } |
| 698 | | |
| 699 | punct!("..") => { |_| RangeLimits::HalfOpen } |
| 700 | )); |
| 701 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 702 | named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path))); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 703 | |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 704 | named!(expr_addr_of -> Expr, do_parse!( |
| 705 | punct!("&") >> |
| 706 | mutability: mutability >> |
| 707 | expr: expr >> |
| 708 | (Expr::AddrOf(mutability, Box::new(expr))) |
| 709 | )); |
| 710 | |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame^] | 711 | named!(and_assign -> Expr, preceded!(punct!("="), expr)); |
| 712 | |
| 713 | named!(and_assign_op -> (BinOp, Expr), tuple!(assign_op, expr)); |
| 714 | |
| 715 | named!(and_field -> Ident, preceded!(punct!("."), ident)); |
| 716 | |
| 717 | named!(and_tup_field -> u64, preceded!(punct!("."), digits)); |
| 718 | |
| 719 | named!(and_index -> Expr, delimited!(punct!("["), expr, punct!("]"))); |
| 720 | |
| 721 | named!(and_range -> (RangeLimits, Option<Expr>), tuple!(range_limits, option!(expr))); |
| 722 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 723 | named!(pub block -> Block, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 724 | punct!("{") >> |
| 725 | stmts: within_block >> |
| 726 | punct!("}") >> |
| 727 | (Block { |
| 728 | stmts: stmts, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 729 | }) |
| 730 | )); |
| 731 | |
| 732 | named!(block_check_mode -> BlockCheckMode, alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 733 | keyword!("unsafe") => { |_| BlockCheckMode::Unsafe } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 734 | | |
| 735 | epsilon!() => { |_| BlockCheckMode::Default } |
| 736 | )); |
| 737 | |
| 738 | named!(within_block -> Vec<Stmt>, do_parse!( |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 739 | many0!(punct!(";")) >> |
| 740 | mut standalone: many0!(terminated!(standalone_stmt, many0!(punct!(";")))) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 741 | last: option!(expr) >> |
| 742 | (match last { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 743 | None => standalone, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 744 | Some(last) => { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 745 | standalone.push(Stmt::Expr(Box::new(last))); |
| 746 | standalone |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 747 | } |
| 748 | }) |
| 749 | )); |
| 750 | |
| 751 | named!(standalone_stmt -> Stmt, alt!( |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 752 | stmt_mac |
| 753 | | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 754 | stmt_local |
| 755 | | |
| 756 | stmt_item |
| 757 | | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 758 | stmt_expr |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 759 | )); |
| 760 | |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 761 | named!(stmt_mac -> Stmt, do_parse!( |
| 762 | attrs: many0!(outer_attr) >> |
| 763 | mac: mac >> |
| 764 | semi: option!(punct!(";")) >> |
| 765 | ({ |
| 766 | let style = if semi.is_some() { |
| 767 | MacStmtStyle::Semicolon |
| 768 | } else if let Some(&TokenTree::Delimited(Delimited { delim, .. })) = mac.tts.last() { |
| 769 | match delim { |
| 770 | DelimToken::Paren | DelimToken::Bracket => MacStmtStyle::NoBraces, |
| 771 | DelimToken::Brace => MacStmtStyle::Braces, |
| 772 | } |
| 773 | } else { |
| 774 | MacStmtStyle::NoBraces |
| 775 | }; |
| 776 | Stmt::Mac(Box::new((mac, style, attrs))) |
| 777 | }) |
| 778 | )); |
| 779 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 780 | named!(stmt_local -> Stmt, do_parse!( |
| 781 | attrs: many0!(outer_attr) >> |
| 782 | keyword!("let") >> |
| 783 | pat: pat >> |
| 784 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 785 | init: option!(preceded!(punct!("="), expr)) >> |
| 786 | punct!(";") >> |
| 787 | (Stmt::Local(Box::new(Local { |
| 788 | pat: Box::new(pat), |
| 789 | ty: ty.map(Box::new), |
| 790 | init: init.map(Box::new), |
| 791 | attrs: attrs, |
| 792 | }))) |
| 793 | )); |
| 794 | |
| 795 | named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i)))); |
| 796 | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 797 | fn requires_semi(e: &Expr) -> bool { |
| 798 | match *e { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 799 | Expr::If(_, _, _) | |
| 800 | Expr::IfLet(_, _, _, _) | |
| 801 | Expr::While(_, _, _) | |
| 802 | Expr::WhileLet(_, _, _, _) | |
| 803 | Expr::ForLoop(_, _, _, _) | |
| 804 | Expr::Loop(_, _) | |
| 805 | Expr::Match(_, _) | |
| 806 | Expr::Block(_, _) => false, |
| 807 | |
| 808 | _ => true, |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 813 | e: expr >> |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 814 | semi: option!(punct!(";")) >> |
| 815 | (if semi.is_some() { |
| 816 | Stmt::Semi(Box::new(e)) |
| 817 | } else if requires_semi(&e) { |
| 818 | return Error; |
| 819 | } else { |
| 820 | Stmt::Expr(Box::new(e)) |
| 821 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 822 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 823 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 824 | named!(pub pat -> Pat, alt!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 825 | pat_wild // must be before pat_ident |
| 826 | | |
| 827 | pat_box // must be before pat_ident |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 828 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 829 | pat_range // must be before pat_lit |
| 830 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 831 | pat_tuple_struct // must be before pat_ident |
| 832 | | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 833 | pat_struct // must be before pat_ident |
| 834 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 835 | pat_mac // must be before pat_ident |
| 836 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 837 | pat_ident // must be before pat_path |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 838 | | |
| 839 | pat_path |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 840 | | |
| 841 | pat_tuple |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 842 | | |
| 843 | pat_ref |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 844 | | |
| 845 | pat_lit |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 846 | // TODO: Vec |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 847 | )); |
| 848 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 849 | named!(pat_mac -> Pat, map!(mac, Pat::Mac)); |
| 850 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 851 | named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild)); |
| 852 | |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 853 | named!(pat_box -> Pat, do_parse!( |
| 854 | keyword!("box") >> |
| 855 | pat: pat >> |
| 856 | (Pat::Box(Box::new(pat))) |
| 857 | )); |
| 858 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 859 | named!(pat_ident -> Pat, do_parse!( |
| 860 | mode: option!(keyword!("ref")) >> |
| 861 | mutability: mutability >> |
| 862 | name: ident >> |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 863 | not!(peek!(punct!("<"))) >> |
| 864 | not!(peek!(punct!("::"))) >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 865 | subpat: option!(preceded!(punct!("@"), pat)) >> |
| 866 | (Pat::Ident( |
| 867 | if mode.is_some() { |
| 868 | BindingMode::ByRef(mutability) |
| 869 | } else { |
| 870 | BindingMode::ByValue(mutability) |
| 871 | }, |
| 872 | name, |
| 873 | subpat.map(Box::new), |
| 874 | )) |
| 875 | )); |
| 876 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 877 | named!(pat_tuple_struct -> Pat, do_parse!( |
| 878 | path: path >> |
| 879 | tuple: pat_tuple_helper >> |
| 880 | (Pat::TupleStruct(path, tuple.0, tuple.1)) |
| 881 | )); |
| 882 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 883 | named!(pat_struct -> Pat, do_parse!( |
| 884 | path: path >> |
| 885 | punct!("{") >> |
| 886 | fields: separated_list!(punct!(","), field_pat) >> |
| 887 | more: option!(preceded!( |
| 888 | cond!(!fields.is_empty(), punct!(",")), |
| 889 | punct!("..") |
| 890 | )) >> |
| 891 | punct!("}") >> |
| 892 | (Pat::Struct(path, fields, more.is_some())) |
| 893 | )); |
| 894 | |
| 895 | named!(field_pat -> FieldPat, alt!( |
| 896 | do_parse!( |
| 897 | ident: ident >> |
| 898 | punct!(":") >> |
| 899 | pat: pat >> |
| 900 | (FieldPat { |
| 901 | ident: ident, |
| 902 | pat: Box::new(pat), |
| 903 | is_shorthand: false, |
| 904 | }) |
| 905 | ) |
| 906 | | |
| 907 | do_parse!( |
| 908 | mode: option!(keyword!("ref")) >> |
| 909 | mutability: mutability >> |
| 910 | ident: ident >> |
| 911 | (FieldPat { |
| 912 | ident: ident.clone(), |
| 913 | pat: Box::new(Pat::Ident( |
| 914 | if mode.is_some() { |
| 915 | BindingMode::ByRef(mutability) |
| 916 | } else { |
| 917 | BindingMode::ByValue(mutability) |
| 918 | }, |
| 919 | ident, |
| 920 | None, |
| 921 | )), |
| 922 | is_shorthand: true, |
| 923 | }) |
| 924 | ) |
| 925 | )); |
| 926 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 927 | named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path))); |
| 928 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 929 | named!(pat_tuple -> Pat, map!( |
| 930 | pat_tuple_helper, |
| 931 | |(pats, dotdot)| Pat::Tuple(pats, dotdot) |
| 932 | )); |
| 933 | |
| 934 | named!(pat_tuple_helper -> (Vec<Pat>, Option<usize>), do_parse!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 935 | punct!("(") >> |
| 936 | mut elems: separated_list!(punct!(","), pat) >> |
| 937 | dotdot: option!(do_parse!( |
| 938 | cond!(!elems.is_empty(), punct!(",")) >> |
| 939 | punct!("..") >> |
| 940 | rest: many0!(preceded!(punct!(","), pat)) >> |
| 941 | cond!(!rest.is_empty(), option!(punct!(","))) >> |
| 942 | (rest) |
| 943 | )) >> |
| 944 | cond!(!elems.is_empty() && dotdot.is_none(), option!(punct!(","))) >> |
| 945 | punct!(")") >> |
| 946 | (match dotdot { |
| 947 | Some(rest) => { |
| 948 | let pos = elems.len(); |
| 949 | elems.extend(rest); |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 950 | (elems, Some(pos)) |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 951 | } |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 952 | None => (elems, None), |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 953 | }) |
| 954 | )); |
| 955 | |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 956 | named!(pat_ref -> Pat, do_parse!( |
| 957 | punct!("&") >> |
| 958 | mutability: mutability >> |
| 959 | pat: pat >> |
| 960 | (Pat::Ref(Box::new(pat), mutability)) |
| 961 | )); |
| 962 | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 963 | named!(pat_lit -> Pat, map!(lit, |lit| Pat::Lit(Box::new(lit)))); |
| 964 | |
| 965 | named!(pat_range -> Pat, do_parse!( |
| 966 | lo: lit >> |
| 967 | punct!("...") >> |
| 968 | hi: lit >> |
| 969 | (Pat::Range(Box::new(lo), Box::new(hi))) |
| 970 | )); |
| 971 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 972 | named!(capture_by -> CaptureBy, alt!( |
| 973 | keyword!("move") => { |_| CaptureBy::Value } |
| 974 | | |
| 975 | epsilon!() => { |_| CaptureBy::Ref } |
| 976 | )); |
| 977 | |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 978 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 979 | } |
| 980 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 981 | #[cfg(feature = "printing")] |
| 982 | mod printing { |
| 983 | use super::*; |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 984 | use {FnArg, FunctionRetTy, Mutability, Ty}; |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 985 | use attr::FilterAttrs; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 986 | use quote::{Tokens, ToTokens}; |
| 987 | |
| 988 | impl ToTokens for Expr { |
| 989 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 990 | match *self { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 991 | Expr::Box(ref inner) => { |
| 992 | tokens.append("box"); |
| 993 | inner.to_tokens(tokens); |
| 994 | } |
| 995 | Expr::Vec(ref tys) => { |
| 996 | tokens.append("["); |
| 997 | tokens.append_separated(tys, ","); |
| 998 | tokens.append("]"); |
| 999 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1000 | Expr::Call(ref func, ref args) => { |
| 1001 | func.to_tokens(tokens); |
| 1002 | tokens.append("("); |
| 1003 | tokens.append_separated(args, ","); |
| 1004 | tokens.append(")"); |
| 1005 | } |
| 1006 | Expr::MethodCall(ref ident, ref ascript, ref args) => { |
| 1007 | args[0].to_tokens(tokens); |
| 1008 | tokens.append("."); |
| 1009 | ident.to_tokens(tokens); |
| 1010 | if ascript.len() > 0 { |
| 1011 | tokens.append("::"); |
| 1012 | tokens.append("<"); |
| 1013 | tokens.append_separated(ascript, ","); |
| 1014 | tokens.append(">"); |
| 1015 | } |
| 1016 | tokens.append("("); |
| 1017 | tokens.append_separated(&args[1..], ","); |
| 1018 | tokens.append(")"); |
| 1019 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 1020 | Expr::Tup(ref fields) => { |
| 1021 | tokens.append("("); |
| 1022 | tokens.append_separated(fields, ","); |
| 1023 | if fields.len() == 1 { |
| 1024 | tokens.append(","); |
| 1025 | } |
| 1026 | tokens.append(")"); |
| 1027 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1028 | Expr::Binary(op, ref left, ref right) => { |
| 1029 | left.to_tokens(tokens); |
| 1030 | op.to_tokens(tokens); |
| 1031 | right.to_tokens(tokens); |
| 1032 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1033 | Expr::Unary(op, ref expr) => { |
| 1034 | op.to_tokens(tokens); |
| 1035 | expr.to_tokens(tokens); |
| 1036 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1037 | Expr::Lit(ref lit) => lit.to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1038 | Expr::Cast(ref expr, ref ty) => { |
| 1039 | expr.to_tokens(tokens); |
| 1040 | tokens.append("as"); |
| 1041 | ty.to_tokens(tokens); |
| 1042 | } |
| 1043 | Expr::Type(ref expr, ref ty) => { |
| 1044 | expr.to_tokens(tokens); |
| 1045 | tokens.append(":"); |
| 1046 | ty.to_tokens(tokens); |
| 1047 | } |
| 1048 | Expr::If(ref cond, ref then_block, ref else_block) => { |
| 1049 | tokens.append("if"); |
| 1050 | cond.to_tokens(tokens); |
| 1051 | then_block.to_tokens(tokens); |
| 1052 | if let Some(ref else_block) = *else_block { |
| 1053 | tokens.append("else"); |
| 1054 | else_block.to_tokens(tokens); |
| 1055 | } |
| 1056 | } |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1057 | Expr::IfLet(ref pat, ref expr, ref then_block, ref else_block) => { |
| 1058 | tokens.append("if"); |
| 1059 | tokens.append("let"); |
| 1060 | pat.to_tokens(tokens); |
| 1061 | tokens.append("="); |
| 1062 | expr.to_tokens(tokens); |
| 1063 | then_block.to_tokens(tokens); |
| 1064 | if let Some(ref else_block) = *else_block { |
| 1065 | tokens.append("else"); |
| 1066 | else_block.to_tokens(tokens); |
| 1067 | } |
| 1068 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1069 | Expr::While(ref cond, ref body, ref label) => { |
| 1070 | if let Some(ref label) = *label { |
| 1071 | label.to_tokens(tokens); |
| 1072 | tokens.append(":"); |
| 1073 | } |
| 1074 | tokens.append("while"); |
| 1075 | cond.to_tokens(tokens); |
| 1076 | body.to_tokens(tokens); |
| 1077 | } |
| 1078 | Expr::WhileLet(ref pat, ref expr, ref body, ref label) => { |
| 1079 | if let Some(ref label) = *label { |
| 1080 | label.to_tokens(tokens); |
| 1081 | tokens.append(":"); |
| 1082 | } |
| 1083 | tokens.append("while"); |
| 1084 | tokens.append("let"); |
| 1085 | pat.to_tokens(tokens); |
| 1086 | tokens.append("="); |
| 1087 | expr.to_tokens(tokens); |
| 1088 | body.to_tokens(tokens); |
| 1089 | } |
| 1090 | Expr::ForLoop(ref pat, ref expr, ref body, ref label) => { |
| 1091 | if let Some(ref label) = *label { |
| 1092 | label.to_tokens(tokens); |
| 1093 | tokens.append(":"); |
| 1094 | } |
| 1095 | tokens.append("for"); |
| 1096 | pat.to_tokens(tokens); |
| 1097 | tokens.append("in"); |
| 1098 | expr.to_tokens(tokens); |
| 1099 | body.to_tokens(tokens); |
| 1100 | } |
| 1101 | Expr::Loop(ref body, ref label) => { |
| 1102 | if let Some(ref label) = *label { |
| 1103 | label.to_tokens(tokens); |
| 1104 | tokens.append(":"); |
| 1105 | } |
| 1106 | tokens.append("loop"); |
| 1107 | body.to_tokens(tokens); |
| 1108 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1109 | Expr::Match(ref expr, ref arms) => { |
| 1110 | tokens.append("match"); |
| 1111 | expr.to_tokens(tokens); |
| 1112 | tokens.append("{"); |
| 1113 | tokens.append_separated(arms, ","); |
| 1114 | tokens.append("}"); |
| 1115 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1116 | Expr::Closure(capture, ref decl, ref body) => { |
| 1117 | capture.to_tokens(tokens); |
| 1118 | tokens.append("|"); |
| 1119 | for (i, input) in decl.inputs.iter().enumerate() { |
| 1120 | if i > 0 { |
| 1121 | tokens.append(","); |
| 1122 | } |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1123 | match *input { |
| 1124 | FnArg::Captured(ref pat, Ty::Infer) => { |
| 1125 | pat.to_tokens(tokens); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1126 | } |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1127 | _ => input.to_tokens(tokens), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | tokens.append("|"); |
| 1131 | match decl.output { |
| 1132 | FunctionRetTy::Default => { |
| 1133 | if body.stmts.len() == 1 { |
| 1134 | if let Stmt::Expr(ref expr) = body.stmts[0] { |
| 1135 | expr.to_tokens(tokens); |
| 1136 | } else { |
| 1137 | body.to_tokens(tokens); |
| 1138 | } |
| 1139 | } else { |
| 1140 | body.to_tokens(tokens); |
| 1141 | } |
| 1142 | } |
| 1143 | FunctionRetTy::Ty(ref ty) => { |
| 1144 | tokens.append("->"); |
| 1145 | ty.to_tokens(tokens); |
| 1146 | body.to_tokens(tokens); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | Expr::Block(rules, ref block) => { |
| 1151 | rules.to_tokens(tokens); |
| 1152 | block.to_tokens(tokens); |
| 1153 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1154 | Expr::Assign(ref var, ref expr) => { |
| 1155 | var.to_tokens(tokens); |
| 1156 | tokens.append("="); |
| 1157 | expr.to_tokens(tokens); |
| 1158 | } |
| 1159 | Expr::AssignOp(op, ref var, ref expr) => { |
| 1160 | var.to_tokens(tokens); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 1161 | tokens.append(op.assign_op().unwrap()); |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1162 | expr.to_tokens(tokens); |
| 1163 | } |
| 1164 | Expr::Field(ref expr, ref field) => { |
| 1165 | expr.to_tokens(tokens); |
| 1166 | tokens.append("."); |
| 1167 | field.to_tokens(tokens); |
| 1168 | } |
| 1169 | Expr::TupField(ref expr, field) => { |
| 1170 | expr.to_tokens(tokens); |
| 1171 | tokens.append("."); |
| 1172 | tokens.append(&field.to_string()); |
| 1173 | } |
| 1174 | Expr::Index(ref expr, ref index) => { |
| 1175 | expr.to_tokens(tokens); |
| 1176 | tokens.append("["); |
| 1177 | index.to_tokens(tokens); |
| 1178 | tokens.append("]"); |
| 1179 | } |
| 1180 | Expr::Range(ref from, ref to, limits) => { |
| 1181 | from.to_tokens(tokens); |
| 1182 | match limits { |
| 1183 | RangeLimits::HalfOpen => tokens.append(".."), |
| 1184 | RangeLimits::Closed => tokens.append("..."), |
| 1185 | } |
| 1186 | to.to_tokens(tokens); |
| 1187 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1188 | Expr::Path(None, ref path) => path.to_tokens(tokens), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1189 | Expr::Path(Some(ref qself), ref path) => { |
| 1190 | tokens.append("<"); |
| 1191 | qself.ty.to_tokens(tokens); |
| 1192 | if qself.position > 0 { |
| 1193 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1194 | for (i, segment) in path.segments |
| 1195 | .iter() |
| 1196 | .take(qself.position) |
| 1197 | .enumerate() { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1198 | if i > 0 || path.global { |
| 1199 | tokens.append("::"); |
| 1200 | } |
| 1201 | segment.to_tokens(tokens); |
| 1202 | } |
| 1203 | } |
| 1204 | tokens.append(">"); |
| 1205 | for segment in path.segments.iter().skip(qself.position) { |
| 1206 | tokens.append("::"); |
| 1207 | segment.to_tokens(tokens); |
| 1208 | } |
| 1209 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1210 | Expr::AddrOf(mutability, ref expr) => { |
| 1211 | tokens.append("&"); |
| 1212 | mutability.to_tokens(tokens); |
| 1213 | expr.to_tokens(tokens); |
| 1214 | } |
| 1215 | Expr::Break(ref opt_label) => { |
| 1216 | tokens.append("break"); |
| 1217 | opt_label.to_tokens(tokens); |
| 1218 | } |
| 1219 | Expr::Continue(ref opt_label) => { |
| 1220 | tokens.append("continue"); |
| 1221 | opt_label.to_tokens(tokens); |
| 1222 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1223 | Expr::Ret(ref opt_expr) => { |
| 1224 | tokens.append("return"); |
| 1225 | opt_expr.to_tokens(tokens); |
| 1226 | } |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame] | 1227 | Expr::Mac(ref mac) => mac.to_tokens(tokens), |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1228 | Expr::Struct(ref path, ref fields, ref base) => { |
| 1229 | path.to_tokens(tokens); |
| 1230 | tokens.append("{"); |
| 1231 | tokens.append_separated(fields, ","); |
| 1232 | if let Some(ref base) = *base { |
| 1233 | if !fields.is_empty() { |
| 1234 | tokens.append(","); |
| 1235 | } |
| 1236 | tokens.append(".."); |
| 1237 | base.to_tokens(tokens); |
| 1238 | } |
| 1239 | tokens.append("}"); |
| 1240 | } |
| 1241 | Expr::Repeat(ref expr, ref times) => { |
| 1242 | tokens.append("["); |
| 1243 | expr.to_tokens(tokens); |
| 1244 | tokens.append(";"); |
| 1245 | times.to_tokens(tokens); |
| 1246 | tokens.append("]"); |
| 1247 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1248 | Expr::Paren(ref expr) => { |
| 1249 | tokens.append("("); |
| 1250 | expr.to_tokens(tokens); |
| 1251 | tokens.append(")"); |
| 1252 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1253 | Expr::Try(ref expr) => { |
| 1254 | expr.to_tokens(tokens); |
| 1255 | tokens.append("?"); |
| 1256 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1260 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1261 | impl ToTokens for FieldValue { |
| 1262 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1263 | self.ident.to_tokens(tokens); |
| 1264 | tokens.append(":"); |
| 1265 | self.expr.to_tokens(tokens); |
| 1266 | } |
| 1267 | } |
| 1268 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1269 | impl ToTokens for Arm { |
| 1270 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1271 | for attr in &self.attrs { |
| 1272 | attr.to_tokens(tokens); |
| 1273 | } |
| 1274 | tokens.append_separated(&self.pats, "|"); |
| 1275 | if let Some(ref guard) = self.guard { |
| 1276 | tokens.append("if"); |
| 1277 | guard.to_tokens(tokens); |
| 1278 | } |
| 1279 | tokens.append("=>"); |
| 1280 | self.body.to_tokens(tokens); |
| 1281 | match *self.body { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1282 | Expr::Block(_, _) => { |
| 1283 | // no comma |
| 1284 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1285 | _ => tokens.append(","), |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | impl ToTokens for Pat { |
| 1291 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1292 | match *self { |
| 1293 | Pat::Wild => tokens.append("_"), |
| 1294 | Pat::Ident(mode, ref ident, ref subpat) => { |
| 1295 | mode.to_tokens(tokens); |
| 1296 | ident.to_tokens(tokens); |
| 1297 | if let Some(ref subpat) = *subpat { |
| 1298 | tokens.append("@"); |
| 1299 | subpat.to_tokens(tokens); |
| 1300 | } |
| 1301 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1302 | Pat::Struct(ref path, ref fields, dots) => { |
| 1303 | path.to_tokens(tokens); |
| 1304 | tokens.append("{"); |
| 1305 | tokens.append_separated(fields, ","); |
| 1306 | if dots { |
| 1307 | if !fields.is_empty() { |
| 1308 | tokens.append(","); |
| 1309 | } |
| 1310 | tokens.append(".."); |
| 1311 | } |
| 1312 | tokens.append("}"); |
| 1313 | } |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1314 | Pat::TupleStruct(ref path, ref pats, dotpos) => { |
| 1315 | path.to_tokens(tokens); |
| 1316 | tokens.append("("); |
| 1317 | match dotpos { |
| 1318 | Some(pos) => { |
| 1319 | if pos > 0 { |
| 1320 | tokens.append_separated(&pats[..pos], ","); |
| 1321 | tokens.append(","); |
| 1322 | } |
| 1323 | tokens.append(".."); |
| 1324 | if pos < pats.len() { |
| 1325 | tokens.append(","); |
| 1326 | tokens.append_separated(&pats[pos..], ","); |
| 1327 | } |
| 1328 | } |
| 1329 | None => tokens.append_separated(pats, ","), |
| 1330 | } |
| 1331 | tokens.append(")"); |
| 1332 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1333 | Pat::Path(None, ref path) => path.to_tokens(tokens), |
| 1334 | Pat::Path(Some(ref qself), ref path) => { |
| 1335 | tokens.append("<"); |
| 1336 | qself.ty.to_tokens(tokens); |
| 1337 | if qself.position > 0 { |
| 1338 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1339 | for (i, segment) in path.segments |
| 1340 | .iter() |
| 1341 | .take(qself.position) |
| 1342 | .enumerate() { |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1343 | if i > 0 || path.global { |
| 1344 | tokens.append("::"); |
| 1345 | } |
| 1346 | segment.to_tokens(tokens); |
| 1347 | } |
| 1348 | } |
| 1349 | tokens.append(">"); |
| 1350 | for segment in path.segments.iter().skip(qself.position) { |
| 1351 | tokens.append("::"); |
| 1352 | segment.to_tokens(tokens); |
| 1353 | } |
| 1354 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1355 | Pat::Tuple(ref pats, dotpos) => { |
| 1356 | tokens.append("("); |
| 1357 | match dotpos { |
| 1358 | Some(pos) => { |
| 1359 | if pos > 0 { |
| 1360 | tokens.append_separated(&pats[..pos], ","); |
| 1361 | tokens.append(","); |
| 1362 | } |
| 1363 | tokens.append(".."); |
| 1364 | if pos < pats.len() { |
| 1365 | tokens.append(","); |
| 1366 | tokens.append_separated(&pats[pos..], ","); |
| 1367 | } |
| 1368 | } |
| 1369 | None => tokens.append_separated(pats, ","), |
| 1370 | } |
| 1371 | tokens.append(")"); |
| 1372 | } |
| 1373 | Pat::Box(ref inner) => { |
| 1374 | tokens.append("box"); |
| 1375 | inner.to_tokens(tokens); |
| 1376 | } |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1377 | Pat::Ref(ref target, mutability) => { |
| 1378 | tokens.append("&"); |
| 1379 | mutability.to_tokens(tokens); |
| 1380 | target.to_tokens(tokens); |
| 1381 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1382 | Pat::Lit(ref lit) => lit.to_tokens(tokens), |
| 1383 | Pat::Range(ref lo, ref hi) => { |
| 1384 | lo.to_tokens(tokens); |
| 1385 | tokens.append("..."); |
| 1386 | hi.to_tokens(tokens); |
| 1387 | } |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 1388 | Pat::Slice(ref _before, ref _dots, ref _after) => unimplemented!(), |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame] | 1389 | Pat::Mac(ref mac) => mac.to_tokens(tokens), |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1394 | impl ToTokens for FieldPat { |
| 1395 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1396 | if !self.is_shorthand { |
| 1397 | self.ident.to_tokens(tokens); |
| 1398 | tokens.append(":"); |
| 1399 | } |
| 1400 | self.pat.to_tokens(tokens); |
| 1401 | } |
| 1402 | } |
| 1403 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1404 | impl ToTokens for BindingMode { |
| 1405 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1406 | match *self { |
| 1407 | BindingMode::ByRef(Mutability::Immutable) => { |
| 1408 | tokens.append("ref"); |
| 1409 | } |
| 1410 | BindingMode::ByRef(Mutability::Mutable) => { |
| 1411 | tokens.append("ref"); |
| 1412 | tokens.append("mut"); |
| 1413 | } |
| 1414 | BindingMode::ByValue(Mutability::Immutable) => {} |
| 1415 | BindingMode::ByValue(Mutability::Mutable) => { |
| 1416 | tokens.append("mut"); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1421 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1422 | impl ToTokens for CaptureBy { |
| 1423 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1424 | match *self { |
| 1425 | CaptureBy::Value => tokens.append("move"), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1426 | CaptureBy::Ref => { |
| 1427 | // nothing |
| 1428 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1433 | impl ToTokens for Block { |
| 1434 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1435 | tokens.append("{"); |
| 1436 | for stmt in &self.stmts { |
| 1437 | stmt.to_tokens(tokens); |
| 1438 | } |
| 1439 | tokens.append("}"); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | impl ToTokens for BlockCheckMode { |
| 1444 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1445 | match *self { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1446 | BlockCheckMode::Default => { |
| 1447 | // nothing |
| 1448 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1449 | BlockCheckMode::Unsafe => tokens.append("unsafe"), |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | impl ToTokens for Stmt { |
| 1455 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1456 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1457 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1458 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 1459 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
| 1460 | Stmt::Semi(ref expr) => { |
| 1461 | expr.to_tokens(tokens); |
| 1462 | tokens.append(";"); |
| 1463 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1464 | Stmt::Mac(ref mac) => { |
| 1465 | let (ref mac, style, ref attrs) = **mac; |
| 1466 | for attr in attrs.outer() { |
| 1467 | attr.to_tokens(tokens); |
| 1468 | } |
| 1469 | mac.to_tokens(tokens); |
| 1470 | match style { |
| 1471 | MacStmtStyle::Semicolon => tokens.append(";"), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1472 | MacStmtStyle::Braces | MacStmtStyle::NoBraces => { |
| 1473 | // no semicolon |
| 1474 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1475 | } |
| 1476 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1480 | |
| 1481 | impl ToTokens for Local { |
| 1482 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1483 | tokens.append("let"); |
| 1484 | self.pat.to_tokens(tokens); |
| 1485 | if let Some(ref ty) = self.ty { |
| 1486 | tokens.append(":"); |
| 1487 | ty.to_tokens(tokens); |
| 1488 | } |
| 1489 | if let Some(ref init) = self.init { |
| 1490 | tokens.append("="); |
| 1491 | init.to_tokens(tokens); |
| 1492 | } |
| 1493 | tokens.append(";"); |
| 1494 | } |
| 1495 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1496 | } |