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 | |
| 191 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 192 | pub 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)] |
| 232 | pub 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)] |
| 242 | pub enum Pat { |
| 243 | /// Represents a wildcard pattern (`_`) |
| 244 | Wild, |
| 245 | |
David Tolnay | 432afc0 | 2016-09-24 07:37:13 -0700 | [diff] [blame] | 246 | /// 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] | 247 | /// 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 Tolnay | 432afc0 | 2016-09-24 07:37:13 -0700 | [diff] [blame] | 280 | /// `Pat::Vec(box [a, b], Some(i), box [y, z])` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 281 | Vec(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>), |
| 282 | /// A macro pattern; pre-expansion |
| 283 | Mac(Mac), |
| 284 | } |
| 285 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 286 | /// 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 Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 296 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 297 | pub 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)] |
| 306 | pub enum CaptureBy { |
| 307 | Value, |
| 308 | Ref, |
| 309 | } |
| 310 | |
| 311 | /// Limit types of a range (inclusive or exclusive) |
| 312 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 313 | pub 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 Tolnay | 181bac5 | 2016-09-24 00:10:05 -0700 | [diff] [blame] | 323 | /// 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] | 324 | /// except `is_shorthand` is true |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 325 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 326 | pub 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)] |
| 335 | pub enum BindingMode { |
| 336 | ByRef(Mutability), |
| 337 | ByValue(Mutability), |
| 338 | } |
| 339 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 340 | #[cfg(feature = "parsing")] |
| 341 | pub mod parsing { |
| 342 | use super::*; |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 343 | use {FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Ty}; |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 344 | use attr::parsing::outer_attr; |
Gregory Katz | 1b69f68 | 2016-09-27 21:06:09 -0400 | [diff] [blame] | 345 | use generics::parsing::lifetime; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 346 | use ident::parsing::ident; |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 347 | use item::parsing::item; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 348 | use lit::parsing::lit; |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 349 | use ty::parsing::{mutability, path, qpath, ty}; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 350 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 351 | named!(pub expr -> Expr, do_parse!( |
| 352 | mut e: alt!( |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 353 | 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 Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 358 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 359 | expr_box |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 360 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 361 | expr_vec |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 362 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 363 | expr_tup |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 364 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 365 | expr_unary |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 366 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 367 | expr_if |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 368 | | |
| 369 | expr_while |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 370 | // TODO: WhileLet |
| 371 | // TODO: ForLoop |
| 372 | // TODO: Loop |
| 373 | // TODO: ForLoop |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 374 | | |
| 375 | expr_loop |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 376 | | |
| 377 | expr_match |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 378 | | |
| 379 | expr_closure |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 380 | | |
| 381 | expr_block |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 382 | | |
| 383 | expr_path |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 384 | | |
| 385 | expr_addr_of |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 386 | | |
| 387 | expr_break |
| 388 | | |
| 389 | expr_continue |
| 390 | | |
| 391 | expr_ret |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 392 | // TODO: Mac |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 393 | | |
| 394 | expr_repeat |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 395 | ) >> |
| 396 | many0!(alt!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 397 | tap!(args: and_call => { |
| 398 | e = Expr::Call(Box::new(e), args); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 399 | }) |
| 400 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 401 | tap!(more: and_method_call => { |
| 402 | let (method, ascript, mut args) = more; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 403 | args.insert(0, e); |
| 404 | e = Expr::MethodCall(method, ascript, args); |
| 405 | }) |
| 406 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 407 | tap!(more: and_binary => { |
| 408 | let (op, other) = more; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 409 | e = Expr::Binary(op, Box::new(e), Box::new(other)); |
| 410 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 411 | | |
| 412 | tap!(ty: and_cast => { |
| 413 | e = Expr::Cast(Box::new(e), Box::new(ty)); |
| 414 | }) |
| 415 | | |
| 416 | tap!(ty: and_ascription => { |
| 417 | e = Expr::Type(Box::new(e), Box::new(ty)); |
| 418 | }) |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 419 | // TODO: Assign |
| 420 | // TODO: AssignOp |
| 421 | // TODO: Field |
| 422 | // TODO: TupField |
| 423 | // TODO: Index |
| 424 | // TODO: Range |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 425 | | |
| 426 | tap!(_try: punct!("?") => { |
| 427 | e = Expr::Try(Box::new(e)); |
| 428 | }) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 429 | )) >> |
| 430 | (e) |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 431 | )); |
| 432 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 433 | named!(expr_paren -> Expr, do_parse!( |
| 434 | punct!("(") >> |
| 435 | e: expr >> |
| 436 | punct!(")") >> |
| 437 | (Expr::Paren(Box::new(e))) |
| 438 | )); |
| 439 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 440 | named!(expr_box -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 441 | keyword!("box") >> |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 442 | inner: expr >> |
| 443 | (Expr::Box(Box::new(inner))) |
| 444 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 445 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 446 | named!(expr_vec -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 447 | punct!("[") >> |
| 448 | elems: separated_list!(punct!(","), expr) >> |
| 449 | punct!("]") >> |
| 450 | (Expr::Vec(elems)) |
| 451 | )); |
| 452 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 453 | named!(and_call -> Vec<Expr>, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 454 | punct!("(") >> |
| 455 | args: separated_list!(punct!(","), expr) >> |
| 456 | punct!(")") >> |
| 457 | (args) |
| 458 | )); |
| 459 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 460 | named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 461 | punct!(".") >> |
| 462 | method: ident >> |
| 463 | ascript: opt_vec!(delimited!( |
| 464 | punct!("<"), |
| 465 | separated_list!(punct!(","), ty), |
| 466 | punct!(">") |
| 467 | )) >> |
| 468 | punct!("(") >> |
| 469 | args: separated_list!(punct!(","), expr) >> |
| 470 | punct!(")") >> |
| 471 | (method, ascript, args) |
| 472 | )); |
| 473 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 474 | named!(expr_tup -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 475 | punct!("(") >> |
| 476 | elems: separated_list!(punct!(","), expr) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 477 | option!(punct!(",")) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 478 | punct!(")") >> |
| 479 | (Expr::Tup(elems)) |
| 480 | )); |
| 481 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 482 | named!(and_binary -> (BinOp, Expr), tuple!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 483 | alt!( |
| 484 | punct!("&&") => { |_| BinOp::And } |
| 485 | | |
| 486 | punct!("||") => { |_| BinOp::Or } |
| 487 | | |
| 488 | punct!("<<") => { |_| BinOp::Shl } |
| 489 | | |
| 490 | punct!(">>") => { |_| BinOp::Shr } |
| 491 | | |
| 492 | punct!("==") => { |_| BinOp::Eq } |
| 493 | | |
| 494 | punct!("<=") => { |_| BinOp::Le } |
| 495 | | |
| 496 | punct!("!=") => { |_| BinOp::Ne } |
| 497 | | |
| 498 | punct!(">=") => { |_| BinOp::Ge } |
| 499 | | |
| 500 | punct!("+") => { |_| BinOp::Add } |
| 501 | | |
| 502 | punct!("-") => { |_| BinOp::Sub } |
| 503 | | |
| 504 | punct!("*") => { |_| BinOp::Mul } |
| 505 | | |
| 506 | punct!("/") => { |_| BinOp::Div } |
| 507 | | |
| 508 | punct!("%") => { |_| BinOp::Rem } |
| 509 | | |
| 510 | punct!("^") => { |_| BinOp::BitXor } |
| 511 | | |
| 512 | punct!("&") => { |_| BinOp::BitAnd } |
| 513 | | |
| 514 | punct!("|") => { |_| BinOp::BitOr } |
| 515 | | |
| 516 | punct!("<") => { |_| BinOp::Lt } |
| 517 | | |
| 518 | punct!(">") => { |_| BinOp::Gt } |
| 519 | ), |
| 520 | expr |
| 521 | )); |
| 522 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 523 | named!(expr_unary -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 524 | operator: alt!( |
| 525 | punct!("*") => { |_| UnOp::Deref } |
| 526 | | |
| 527 | punct!("!") => { |_| UnOp::Not } |
| 528 | | |
| 529 | punct!("-") => { |_| UnOp::Neg } |
| 530 | ) >> |
| 531 | operand: expr >> |
| 532 | (Expr::Unary(operator, Box::new(operand))) |
| 533 | )); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 534 | |
| 535 | named!(expr_lit -> Expr, map!(lit, Expr::Lit)); |
| 536 | |
| 537 | named!(and_cast -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 538 | keyword!("as") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 539 | ty: ty >> |
| 540 | (ty) |
| 541 | )); |
| 542 | |
| 543 | named!(and_ascription -> Ty, preceded!(punct!(":"), ty)); |
| 544 | |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame^] | 545 | enum IfCond { |
| 546 | Let(Pat, Expr), |
| 547 | Expr(Expr), |
| 548 | } |
| 549 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 550 | named!(expr_if -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 551 | keyword!("if") >> |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame^] | 552 | cond: alt!( |
| 553 | do_parse!( |
| 554 | keyword!("let") >> |
| 555 | pat: pat >> |
| 556 | punct!("=") >> |
| 557 | value: expr >> |
| 558 | (IfCond::Let(pat, value)) |
| 559 | ) |
| 560 | | |
| 561 | map!(expr, IfCond::Expr) |
| 562 | ) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 563 | punct!("{") >> |
| 564 | then_block: within_block >> |
| 565 | punct!("}") >> |
| 566 | else_block: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 567 | keyword!("else"), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 568 | alt!( |
| 569 | expr_if |
| 570 | | |
| 571 | do_parse!( |
| 572 | punct!("{") >> |
| 573 | else_block: within_block >> |
| 574 | punct!("}") >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 575 | (Expr::Block(BlockCheckMode::Default, Block { |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 576 | stmts: else_block, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 577 | })) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 578 | ) |
| 579 | ) |
| 580 | )) >> |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame^] | 581 | (match cond { |
| 582 | IfCond::Let(pat, expr) => Expr::IfLet( |
| 583 | Box::new(pat), |
| 584 | Box::new(expr), |
| 585 | Block { |
| 586 | stmts: then_block, |
| 587 | }, |
| 588 | else_block.map(Box::new), |
| 589 | ), |
| 590 | IfCond::Expr(cond) => Expr::If( |
| 591 | Box::new(cond), |
| 592 | Block { |
| 593 | stmts: then_block, |
| 594 | }, |
| 595 | else_block.map(Box::new), |
| 596 | ), |
| 597 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 598 | )); |
| 599 | |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 600 | named!(expr_loop -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 601 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 602 | keyword!("loop") >> |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 603 | loop_block: block >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 604 | (Expr::Loop(loop_block, lbl)) |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 605 | )); |
| 606 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 607 | named!(expr_match -> Expr, do_parse!( |
| 608 | keyword!("match") >> |
| 609 | obj: expr >> |
| 610 | punct!("{") >> |
| 611 | arms: many0!(do_parse!( |
| 612 | attrs: many0!(outer_attr) >> |
| 613 | pats: separated_nonempty_list!(punct!("|"), pat) >> |
| 614 | guard: option!(preceded!(keyword!("if"), expr)) >> |
| 615 | punct!("=>") >> |
| 616 | body: alt!( |
| 617 | terminated!(expr, punct!(",")) |
| 618 | | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 619 | map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk)) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 620 | ) >> |
| 621 | (Arm { |
| 622 | attrs: attrs, |
| 623 | pats: pats, |
| 624 | guard: guard.map(Box::new), |
| 625 | body: Box::new(body), |
| 626 | }) |
| 627 | )) >> |
| 628 | punct!("}") >> |
| 629 | (Expr::Match(Box::new(obj), arms)) |
| 630 | )); |
| 631 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 632 | named!(expr_closure -> Expr, do_parse!( |
| 633 | capture: capture_by >> |
| 634 | punct!("|") >> |
| 635 | inputs: separated_list!(punct!(","), closure_arg) >> |
| 636 | punct!("|") >> |
| 637 | ret_and_body: alt!( |
| 638 | do_parse!( |
| 639 | punct!("->") >> |
| 640 | ty: ty >> |
| 641 | body: block >> |
| 642 | ((FunctionRetTy::Ty(ty), body)) |
| 643 | ) |
| 644 | | |
| 645 | map!(expr, |e| ( |
| 646 | FunctionRetTy::Default, |
| 647 | Block { |
| 648 | stmts: vec![Stmt::Expr(Box::new(e))], |
| 649 | }, |
| 650 | )) |
| 651 | ) >> |
| 652 | (Expr::Closure( |
| 653 | capture, |
| 654 | Box::new(FnDecl { |
| 655 | inputs: inputs, |
| 656 | output: ret_and_body.0, |
| 657 | }), |
| 658 | ret_and_body.1, |
| 659 | )) |
| 660 | )); |
| 661 | |
| 662 | named!(closure_arg -> FnArg, do_parse!( |
| 663 | pat: pat >> |
| 664 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 665 | (FnArg { |
| 666 | pat: pat, |
| 667 | ty: ty.unwrap_or(Ty::Infer), |
| 668 | }) |
| 669 | )); |
| 670 | |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 671 | named!(expr_while -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 672 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 57ffbde | 2016-09-30 09:38:04 -0700 | [diff] [blame] | 673 | keyword!("while") >> |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 674 | cond: expr >> |
| 675 | while_block: block >> |
| 676 | (Expr::While( |
| 677 | Box::new(cond), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 678 | while_block, |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 679 | lbl, |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 680 | )) |
| 681 | )); |
| 682 | |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 683 | named!(expr_continue -> Expr, do_parse!( |
| 684 | keyword!("continue") >> |
| 685 | lbl: option!(label) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 686 | (Expr::Continue(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 687 | )); |
| 688 | |
| 689 | named!(expr_break -> Expr, do_parse!( |
| 690 | keyword!("break") >> |
| 691 | lbl: option!(label) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 692 | (Expr::Break(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 693 | )); |
| 694 | |
| 695 | named!(expr_ret -> Expr, do_parse!( |
| 696 | keyword!("return") >> |
| 697 | ret_value: option!(expr) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 698 | (Expr::Ret(ret_value.map(Box::new))) |
| 699 | )); |
| 700 | |
| 701 | named!(expr_struct -> Expr, do_parse!( |
| 702 | path: path >> |
| 703 | punct!("{") >> |
| 704 | fields: separated_list!(punct!(","), field_value) >> |
| 705 | base: option!(do_parse!( |
| 706 | cond!(!fields.is_empty(), punct!(",")) >> |
| 707 | punct!("..") >> |
| 708 | base: expr >> |
| 709 | (base) |
| 710 | )) >> |
| 711 | punct!("}") >> |
| 712 | (Expr::Struct(path, fields, base.map(Box::new))) |
| 713 | )); |
| 714 | |
| 715 | named!(field_value -> FieldValue, do_parse!( |
| 716 | name: ident >> |
| 717 | punct!(":") >> |
| 718 | value: expr >> |
| 719 | (FieldValue { |
| 720 | ident: name, |
| 721 | expr: value, |
| 722 | }) |
| 723 | )); |
| 724 | |
| 725 | named!(expr_repeat -> Expr, do_parse!( |
| 726 | punct!("[") >> |
| 727 | value: expr >> |
| 728 | punct!(";") >> |
| 729 | times: expr >> |
| 730 | punct!("]") >> |
| 731 | (Expr::Repeat(Box::new(value), Box::new(times))) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 732 | )); |
| 733 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 734 | named!(expr_block -> Expr, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 735 | rules: block_check_mode >> |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 736 | b: block >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 737 | (Expr::Block(rules, Block { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 738 | stmts: b.stmts, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 739 | })) |
| 740 | )); |
| 741 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 742 | named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path))); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 743 | |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 744 | named!(expr_addr_of -> Expr, do_parse!( |
| 745 | punct!("&") >> |
| 746 | mutability: mutability >> |
| 747 | expr: expr >> |
| 748 | (Expr::AddrOf(mutability, Box::new(expr))) |
| 749 | )); |
| 750 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 751 | named!(pub block -> Block, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 752 | punct!("{") >> |
| 753 | stmts: within_block >> |
| 754 | punct!("}") >> |
| 755 | (Block { |
| 756 | stmts: stmts, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 757 | }) |
| 758 | )); |
| 759 | |
| 760 | named!(block_check_mode -> BlockCheckMode, alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 761 | keyword!("unsafe") => { |_| BlockCheckMode::Unsafe } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 762 | | |
| 763 | epsilon!() => { |_| BlockCheckMode::Default } |
| 764 | )); |
| 765 | |
| 766 | named!(within_block -> Vec<Stmt>, do_parse!( |
David Tolnay | 181bac5 | 2016-09-24 00:10:05 -0700 | [diff] [blame] | 767 | mut most: many0!(standalone_stmt) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 768 | last: option!(expr) >> |
| 769 | (match last { |
| 770 | None => most, |
| 771 | Some(last) => { |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 772 | most.push(Stmt::Expr(Box::new(last))); |
| 773 | most |
| 774 | } |
| 775 | }) |
| 776 | )); |
| 777 | |
| 778 | named!(standalone_stmt -> Stmt, alt!( |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 779 | stmt_local |
| 780 | | |
| 781 | stmt_item |
| 782 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 783 | stmt_semi |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 784 | // TODO: mac |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 785 | )); |
| 786 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 787 | named!(stmt_local -> Stmt, do_parse!( |
| 788 | attrs: many0!(outer_attr) >> |
| 789 | keyword!("let") >> |
| 790 | pat: pat >> |
| 791 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 792 | init: option!(preceded!(punct!("="), expr)) >> |
| 793 | punct!(";") >> |
| 794 | (Stmt::Local(Box::new(Local { |
| 795 | pat: Box::new(pat), |
| 796 | ty: ty.map(Box::new), |
| 797 | init: init.map(Box::new), |
| 798 | attrs: attrs, |
| 799 | }))) |
| 800 | )); |
| 801 | |
| 802 | named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i)))); |
| 803 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 804 | named!(stmt_semi -> Stmt, do_parse!( |
| 805 | e: expr >> |
| 806 | punct!(";") >> |
| 807 | (Stmt::Semi(Box::new(e))) |
| 808 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 809 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 810 | named!(pub pat -> Pat, alt!( |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 811 | pat_wild |
| 812 | | |
| 813 | pat_ident |
| 814 | // TODO: Struct |
| 815 | // TODO: TupleStruct |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 816 | | |
| 817 | pat_path |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 818 | // TODO: Tuple |
| 819 | // TODO: Box |
| 820 | // TODO: Ref |
| 821 | // TODO: Lit |
| 822 | // TODO: Range |
| 823 | // TODO: Vec |
| 824 | // TODO: Mac |
| 825 | )); |
| 826 | |
| 827 | named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild)); |
| 828 | |
| 829 | named!(pat_ident -> Pat, do_parse!( |
| 830 | mode: option!(keyword!("ref")) >> |
| 831 | mutability: mutability >> |
| 832 | name: ident >> |
| 833 | subpat: option!(preceded!(punct!("@"), pat)) >> |
| 834 | (Pat::Ident( |
| 835 | if mode.is_some() { |
| 836 | BindingMode::ByRef(mutability) |
| 837 | } else { |
| 838 | BindingMode::ByValue(mutability) |
| 839 | }, |
| 840 | name, |
| 841 | subpat.map(Box::new), |
| 842 | )) |
| 843 | )); |
| 844 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 845 | named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path))); |
| 846 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 847 | named!(capture_by -> CaptureBy, alt!( |
| 848 | keyword!("move") => { |_| CaptureBy::Value } |
| 849 | | |
| 850 | epsilon!() => { |_| CaptureBy::Ref } |
| 851 | )); |
| 852 | |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 853 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 854 | } |
| 855 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 856 | #[cfg(feature = "printing")] |
| 857 | mod printing { |
| 858 | use super::*; |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 859 | use {FunctionRetTy, Mutability, Ty}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 860 | use quote::{Tokens, ToTokens}; |
| 861 | |
| 862 | impl ToTokens for Expr { |
| 863 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 864 | match *self { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 865 | Expr::Box(ref _inner) => unimplemented!(), |
| 866 | Expr::Vec(ref _inner) => unimplemented!(), |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 867 | Expr::Call(ref func, ref args) => { |
| 868 | func.to_tokens(tokens); |
| 869 | tokens.append("("); |
| 870 | tokens.append_separated(args, ","); |
| 871 | tokens.append(")"); |
| 872 | } |
| 873 | Expr::MethodCall(ref ident, ref ascript, ref args) => { |
| 874 | args[0].to_tokens(tokens); |
| 875 | tokens.append("."); |
| 876 | ident.to_tokens(tokens); |
| 877 | if ascript.len() > 0 { |
| 878 | tokens.append("::"); |
| 879 | tokens.append("<"); |
| 880 | tokens.append_separated(ascript, ","); |
| 881 | tokens.append(">"); |
| 882 | } |
| 883 | tokens.append("("); |
| 884 | tokens.append_separated(&args[1..], ","); |
| 885 | tokens.append(")"); |
| 886 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 887 | Expr::Tup(ref fields) => { |
| 888 | tokens.append("("); |
| 889 | tokens.append_separated(fields, ","); |
| 890 | if fields.len() == 1 { |
| 891 | tokens.append(","); |
| 892 | } |
| 893 | tokens.append(")"); |
| 894 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 895 | Expr::Binary(op, ref left, ref right) => { |
| 896 | left.to_tokens(tokens); |
| 897 | op.to_tokens(tokens); |
| 898 | right.to_tokens(tokens); |
| 899 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 900 | Expr::Unary(op, ref expr) => { |
| 901 | op.to_tokens(tokens); |
| 902 | expr.to_tokens(tokens); |
| 903 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 904 | Expr::Lit(ref lit) => lit.to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 905 | Expr::Cast(ref expr, ref ty) => { |
| 906 | expr.to_tokens(tokens); |
| 907 | tokens.append("as"); |
| 908 | ty.to_tokens(tokens); |
| 909 | } |
| 910 | Expr::Type(ref expr, ref ty) => { |
| 911 | expr.to_tokens(tokens); |
| 912 | tokens.append(":"); |
| 913 | ty.to_tokens(tokens); |
| 914 | } |
| 915 | Expr::If(ref cond, ref then_block, ref else_block) => { |
| 916 | tokens.append("if"); |
| 917 | cond.to_tokens(tokens); |
| 918 | then_block.to_tokens(tokens); |
| 919 | if let Some(ref else_block) = *else_block { |
| 920 | tokens.append("else"); |
| 921 | else_block.to_tokens(tokens); |
| 922 | } |
| 923 | } |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame^] | 924 | Expr::IfLet(ref pat, ref expr, ref then_block, ref else_block) => { |
| 925 | tokens.append("if"); |
| 926 | tokens.append("let"); |
| 927 | pat.to_tokens(tokens); |
| 928 | tokens.append("="); |
| 929 | expr.to_tokens(tokens); |
| 930 | then_block.to_tokens(tokens); |
| 931 | if let Some(ref else_block) = *else_block { |
| 932 | tokens.append("else"); |
| 933 | else_block.to_tokens(tokens); |
| 934 | } |
| 935 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 936 | Expr::While(ref _cond, ref _body, ref _label) => unimplemented!(), |
| 937 | Expr::WhileLet(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(), |
| 938 | Expr::ForLoop(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(), |
| 939 | Expr::Loop(ref _body, ref _label) => unimplemented!(), |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 940 | Expr::Match(ref expr, ref arms) => { |
| 941 | tokens.append("match"); |
| 942 | expr.to_tokens(tokens); |
| 943 | tokens.append("{"); |
| 944 | tokens.append_separated(arms, ","); |
| 945 | tokens.append("}"); |
| 946 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 947 | Expr::Closure(capture, ref decl, ref body) => { |
| 948 | capture.to_tokens(tokens); |
| 949 | tokens.append("|"); |
| 950 | for (i, input) in decl.inputs.iter().enumerate() { |
| 951 | if i > 0 { |
| 952 | tokens.append(","); |
| 953 | } |
| 954 | input.pat.to_tokens(tokens); |
| 955 | match input.ty { |
| 956 | Ty::Infer => { /* nothing */ } |
| 957 | _ => { |
| 958 | tokens.append(":"); |
| 959 | input.ty.to_tokens(tokens); |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | tokens.append("|"); |
| 964 | match decl.output { |
| 965 | FunctionRetTy::Default => { |
| 966 | if body.stmts.len() == 1 { |
| 967 | if let Stmt::Expr(ref expr) = body.stmts[0] { |
| 968 | expr.to_tokens(tokens); |
| 969 | } else { |
| 970 | body.to_tokens(tokens); |
| 971 | } |
| 972 | } else { |
| 973 | body.to_tokens(tokens); |
| 974 | } |
| 975 | } |
| 976 | FunctionRetTy::Ty(ref ty) => { |
| 977 | tokens.append("->"); |
| 978 | ty.to_tokens(tokens); |
| 979 | body.to_tokens(tokens); |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | Expr::Block(rules, ref block) => { |
| 984 | rules.to_tokens(tokens); |
| 985 | block.to_tokens(tokens); |
| 986 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 987 | Expr::Assign(ref _var, ref _expr) => unimplemented!(), |
| 988 | Expr::AssignOp(_op, ref _var, ref _expr) => unimplemented!(), |
| 989 | Expr::Field(ref _expr, ref _field) => unimplemented!(), |
| 990 | Expr::TupField(ref _expr, _field) => unimplemented!(), |
| 991 | Expr::Index(ref _expr, ref _index) => unimplemented!(), |
| 992 | Expr::Range(ref _from, ref _to, _limits) => unimplemented!(), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 993 | Expr::Path(None, ref path) => { |
| 994 | path.to_tokens(tokens); |
| 995 | } |
| 996 | Expr::Path(Some(ref qself), ref path) => { |
| 997 | tokens.append("<"); |
| 998 | qself.ty.to_tokens(tokens); |
| 999 | if qself.position > 0 { |
| 1000 | tokens.append("as"); |
| 1001 | for (i, segment) in path.segments.iter() |
| 1002 | .take(qself.position) |
| 1003 | .enumerate() |
| 1004 | { |
| 1005 | if i > 0 || path.global { |
| 1006 | tokens.append("::"); |
| 1007 | } |
| 1008 | segment.to_tokens(tokens); |
| 1009 | } |
| 1010 | } |
| 1011 | tokens.append(">"); |
| 1012 | for segment in path.segments.iter().skip(qself.position) { |
| 1013 | tokens.append("::"); |
| 1014 | segment.to_tokens(tokens); |
| 1015 | } |
| 1016 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1017 | Expr::AddrOf(mutability, ref expr) => { |
| 1018 | tokens.append("&"); |
| 1019 | mutability.to_tokens(tokens); |
| 1020 | expr.to_tokens(tokens); |
| 1021 | } |
| 1022 | Expr::Break(ref opt_label) => { |
| 1023 | tokens.append("break"); |
| 1024 | opt_label.to_tokens(tokens); |
| 1025 | } |
| 1026 | Expr::Continue(ref opt_label) => { |
| 1027 | tokens.append("continue"); |
| 1028 | opt_label.to_tokens(tokens); |
| 1029 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1030 | Expr::Ret(ref opt_expr) => { |
| 1031 | tokens.append("return"); |
| 1032 | opt_expr.to_tokens(tokens); |
| 1033 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 1034 | Expr::Mac(ref _mac) => unimplemented!(), |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1035 | Expr::Struct(ref path, ref fields, ref base) => { |
| 1036 | path.to_tokens(tokens); |
| 1037 | tokens.append("{"); |
| 1038 | tokens.append_separated(fields, ","); |
| 1039 | if let Some(ref base) = *base { |
| 1040 | if !fields.is_empty() { |
| 1041 | tokens.append(","); |
| 1042 | } |
| 1043 | tokens.append(".."); |
| 1044 | base.to_tokens(tokens); |
| 1045 | } |
| 1046 | tokens.append("}"); |
| 1047 | } |
| 1048 | Expr::Repeat(ref expr, ref times) => { |
| 1049 | tokens.append("["); |
| 1050 | expr.to_tokens(tokens); |
| 1051 | tokens.append(";"); |
| 1052 | times.to_tokens(tokens); |
| 1053 | tokens.append("]"); |
| 1054 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1055 | Expr::Paren(ref expr) => { |
| 1056 | tokens.append("("); |
| 1057 | expr.to_tokens(tokens); |
| 1058 | tokens.append(")"); |
| 1059 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1060 | Expr::Try(ref expr) => { |
| 1061 | expr.to_tokens(tokens); |
| 1062 | tokens.append("?"); |
| 1063 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1067 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1068 | impl ToTokens for BinOp { |
| 1069 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1070 | match *self { |
| 1071 | BinOp::Add => tokens.append("+"), |
| 1072 | BinOp::Sub => tokens.append("-"), |
| 1073 | BinOp::Mul => tokens.append("*"), |
| 1074 | BinOp::Div => tokens.append("/"), |
| 1075 | BinOp::Rem => tokens.append("%"), |
| 1076 | BinOp::And => tokens.append("&&"), |
| 1077 | BinOp::Or => tokens.append("||"), |
| 1078 | BinOp::BitXor => tokens.append("^"), |
| 1079 | BinOp::BitAnd => tokens.append("&"), |
| 1080 | BinOp::BitOr => tokens.append("|"), |
| 1081 | BinOp::Shl => tokens.append("<<"), |
| 1082 | BinOp::Shr => tokens.append(">>"), |
| 1083 | BinOp::Eq => tokens.append("=="), |
| 1084 | BinOp::Lt => tokens.append("<"), |
| 1085 | BinOp::Le => tokens.append("<="), |
| 1086 | BinOp::Ne => tokens.append("!="), |
| 1087 | BinOp::Ge => tokens.append(">="), |
| 1088 | BinOp::Gt => tokens.append(">"), |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1093 | impl ToTokens for UnOp { |
| 1094 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1095 | match *self { |
| 1096 | UnOp::Deref => tokens.append("*"), |
| 1097 | UnOp::Not => tokens.append("!"), |
| 1098 | UnOp::Neg => tokens.append("-"), |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1103 | impl ToTokens for FieldValue { |
| 1104 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1105 | self.ident.to_tokens(tokens); |
| 1106 | tokens.append(":"); |
| 1107 | self.expr.to_tokens(tokens); |
| 1108 | } |
| 1109 | } |
| 1110 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1111 | impl ToTokens for Arm { |
| 1112 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1113 | for attr in &self.attrs { |
| 1114 | attr.to_tokens(tokens); |
| 1115 | } |
| 1116 | tokens.append_separated(&self.pats, "|"); |
| 1117 | if let Some(ref guard) = self.guard { |
| 1118 | tokens.append("if"); |
| 1119 | guard.to_tokens(tokens); |
| 1120 | } |
| 1121 | tokens.append("=>"); |
| 1122 | self.body.to_tokens(tokens); |
| 1123 | match *self.body { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1124 | Expr::Block(_, _) => { /* no comma */ } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1125 | _ => tokens.append(","), |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | impl ToTokens for Pat { |
| 1131 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1132 | match *self { |
| 1133 | Pat::Wild => tokens.append("_"), |
| 1134 | Pat::Ident(mode, ref ident, ref subpat) => { |
| 1135 | mode.to_tokens(tokens); |
| 1136 | ident.to_tokens(tokens); |
| 1137 | if let Some(ref subpat) = *subpat { |
| 1138 | tokens.append("@"); |
| 1139 | subpat.to_tokens(tokens); |
| 1140 | } |
| 1141 | } |
| 1142 | Pat::Struct(ref _path, ref _fields, _dots) => unimplemented!(), |
| 1143 | Pat::TupleStruct(ref _path, ref _pats, _dotpos) => unimplemented!(), |
| 1144 | Pat::Path(ref _qself, ref _path) => unimplemented!(), |
| 1145 | Pat::Tuple(ref _pats, _dotpos) => unimplemented!(), |
| 1146 | Pat::Box(ref _inner) => unimplemented!(), |
| 1147 | Pat::Ref(ref _target, _mutability) => unimplemented!(), |
| 1148 | Pat::Lit(ref _expr) => unimplemented!(), |
| 1149 | Pat::Range(ref _lower, ref _upper) => unimplemented!(), |
| 1150 | Pat::Vec(ref _before, ref _dots, ref _after) => unimplemented!(), |
| 1151 | Pat::Mac(ref _mac) => unimplemented!(), |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | impl ToTokens for BindingMode { |
| 1157 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1158 | match *self { |
| 1159 | BindingMode::ByRef(Mutability::Immutable) => { |
| 1160 | tokens.append("ref"); |
| 1161 | } |
| 1162 | BindingMode::ByRef(Mutability::Mutable) => { |
| 1163 | tokens.append("ref"); |
| 1164 | tokens.append("mut"); |
| 1165 | } |
| 1166 | BindingMode::ByValue(Mutability::Immutable) => {} |
| 1167 | BindingMode::ByValue(Mutability::Mutable) => { |
| 1168 | tokens.append("mut"); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1173 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1174 | impl ToTokens for CaptureBy { |
| 1175 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1176 | match *self { |
| 1177 | CaptureBy::Value => tokens.append("move"), |
| 1178 | CaptureBy::Ref => { /* nothing */ } |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1183 | impl ToTokens for Block { |
| 1184 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1185 | tokens.append("{"); |
| 1186 | for stmt in &self.stmts { |
| 1187 | stmt.to_tokens(tokens); |
| 1188 | } |
| 1189 | tokens.append("}"); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | impl ToTokens for BlockCheckMode { |
| 1194 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1195 | match *self { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1196 | BlockCheckMode::Default => { /* nothing */ } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1197 | BlockCheckMode::Unsafe => tokens.append("unsafe"), |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | impl ToTokens for Stmt { |
| 1203 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1204 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1205 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1206 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 1207 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
| 1208 | Stmt::Semi(ref expr) => { |
| 1209 | expr.to_tokens(tokens); |
| 1210 | tokens.append(";"); |
| 1211 | } |
| 1212 | Stmt::Mac(ref _mac) => unimplemented!(), |
| 1213 | } |
| 1214 | } |
| 1215 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1216 | |
| 1217 | impl ToTokens for Local { |
| 1218 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1219 | tokens.append("let"); |
| 1220 | self.pat.to_tokens(tokens); |
| 1221 | if let Some(ref ty) = self.ty { |
| 1222 | tokens.append(":"); |
| 1223 | ty.to_tokens(tokens); |
| 1224 | } |
| 1225 | if let Some(ref init) = self.init { |
| 1226 | tokens.append("="); |
| 1227 | init.to_tokens(tokens); |
| 1228 | } |
| 1229 | tokens.append(";"); |
| 1230 | } |
| 1231 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1232 | } |