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 }` |
| 42 | If(Box<Expr>, Box<Block>, Option<Box<Expr>>), |
| 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. |
| 48 | IfLet(Box<Pat>, Box<Expr>, Box<Block>, Option<Box<Expr>>), |
| 49 | /// A while loop, with an optional label |
| 50 | /// |
| 51 | /// `'label: while expr { block }` |
| 52 | While(Box<Expr>, Box<Block>, Option<Ident>), |
| 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. |
| 58 | WhileLet(Box<Pat>, Box<Expr>, Box<Block>, Option<Ident>), |
| 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. |
| 64 | ForLoop(Box<Pat>, Box<Expr>, Box<Block>, Option<Ident>), |
| 65 | /// Conditionless loop (can be exited with break, continue, or return) |
| 66 | /// |
| 67 | /// `'label: loop { block }` |
| 68 | Loop(Box<Block>, Option<Ident>), |
| 69 | /// A `match` block. |
| 70 | Match(Box<Expr>, Vec<Arm>), |
| 71 | /// A closure (for example, `move |a, b, c| {a + b + c}`) |
| 72 | Closure(CaptureBy, Box<FnDecl>, Box<Block>), |
| 73 | /// A block (`{ ... }`) |
| 74 | Block(Box<Block>), |
| 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>`. |
| 116 | Struct(Path, Vec<Field>, Option<Box<Expr>>), |
| 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 | |
| 131 | /// A Block (`{ .. }`). |
| 132 | /// |
| 133 | /// E.g. `{ .. }` as in `fn foo() { .. }` |
| 134 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 135 | pub struct Block { |
| 136 | /// Statements in a block |
| 137 | pub stmts: Vec<Stmt>, |
| 138 | /// Distinguishes between `unsafe { ... }` and `{ ... }` |
| 139 | pub rules: BlockCheckMode, |
| 140 | } |
| 141 | |
| 142 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 143 | pub enum BlockCheckMode { |
| 144 | Default, |
| 145 | Unsafe, |
| 146 | } |
| 147 | |
| 148 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 149 | pub enum Stmt { |
| 150 | /// A local (let) binding. |
| 151 | Local(Box<Local>), |
| 152 | |
| 153 | /// An item definition. |
| 154 | Item(Box<Item>), |
| 155 | |
| 156 | /// Expr without trailing semi-colon. |
| 157 | Expr(Box<Expr>), |
| 158 | |
| 159 | Semi(Box<Expr>), |
| 160 | |
| 161 | Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>), |
| 162 | } |
| 163 | |
| 164 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 165 | pub enum MacStmtStyle { |
| 166 | /// The macro statement had a trailing semicolon, e.g. `foo! { ... };` |
| 167 | /// `foo!(...);`, `foo![...];` |
| 168 | Semicolon, |
| 169 | /// The macro statement had braces; e.g. foo! { ... } |
| 170 | Braces, |
| 171 | /// The macro statement had parentheses or brackets and no semicolon; e.g. |
| 172 | /// `foo!(...)`. All of these will end up being converted into macro |
| 173 | /// expressions. |
| 174 | NoBraces, |
| 175 | } |
| 176 | |
| 177 | /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;` |
| 178 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 179 | pub struct Local { |
| 180 | pub pat: Box<Pat>, |
| 181 | pub ty: Option<Box<Ty>>, |
| 182 | /// Initializer expression to set the value, if any |
| 183 | pub init: Option<Box<Expr>>, |
| 184 | pub attrs: Vec<Attribute>, |
| 185 | } |
| 186 | |
| 187 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 188 | pub enum BinOp { |
| 189 | /// The `+` operator (addition) |
| 190 | Add, |
| 191 | /// The `-` operator (subtraction) |
| 192 | Sub, |
| 193 | /// The `*` operator (multiplication) |
| 194 | Mul, |
| 195 | /// The `/` operator (division) |
| 196 | Div, |
| 197 | /// The `%` operator (modulus) |
| 198 | Rem, |
| 199 | /// The `&&` operator (logical and) |
| 200 | And, |
| 201 | /// The `||` operator (logical or) |
| 202 | Or, |
| 203 | /// The `^` operator (bitwise xor) |
| 204 | BitXor, |
| 205 | /// The `&` operator (bitwise and) |
| 206 | BitAnd, |
| 207 | /// The `|` operator (bitwise or) |
| 208 | BitOr, |
| 209 | /// The `<<` operator (shift left) |
| 210 | Shl, |
| 211 | /// The `>>` operator (shift right) |
| 212 | Shr, |
| 213 | /// The `==` operator (equality) |
| 214 | Eq, |
| 215 | /// The `<` operator (less than) |
| 216 | Lt, |
| 217 | /// The `<=` operator (less than or equal to) |
| 218 | Le, |
| 219 | /// The `!=` operator (not equal to) |
| 220 | Ne, |
| 221 | /// The `>=` operator (greater than or equal to) |
| 222 | Ge, |
| 223 | /// The `>` operator (greater than) |
| 224 | Gt, |
| 225 | } |
| 226 | |
| 227 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 228 | pub enum UnOp { |
| 229 | /// The `*` operator for dereferencing |
| 230 | Deref, |
| 231 | /// The `!` operator for logical inversion |
| 232 | Not, |
| 233 | /// The `-` operator for negation |
| 234 | Neg, |
| 235 | } |
| 236 | |
| 237 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 238 | pub enum Pat { |
| 239 | /// Represents a wildcard pattern (`_`) |
| 240 | Wild, |
| 241 | |
David Tolnay | 432afc0 | 2016-09-24 07:37:13 -0700 | [diff] [blame] | 242 | /// 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] | 243 | /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third |
| 244 | /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens |
| 245 | /// during name resolution. |
| 246 | Ident(BindingMode, Ident, Option<Box<Pat>>), |
| 247 | |
| 248 | /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`. |
| 249 | /// The `bool` is `true` in the presence of a `..`. |
| 250 | Struct(Path, Vec<FieldPat>, bool), |
| 251 | |
| 252 | /// A tuple struct/variant pattern `Variant(x, y, .., z)`. |
| 253 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 254 | /// 0 <= position <= subpats.len() |
| 255 | TupleStruct(Path, Vec<Pat>, Option<usize>), |
| 256 | |
| 257 | /// A possibly qualified path pattern. |
| 258 | /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants |
| 259 | /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can |
| 260 | /// only legally refer to associated constants. |
| 261 | Path(Option<QSelf>, Path), |
| 262 | |
| 263 | /// A tuple pattern `(a, b)`. |
| 264 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 265 | /// 0 <= position <= subpats.len() |
| 266 | Tuple(Vec<Pat>, Option<usize>), |
| 267 | /// A `box` pattern |
| 268 | Box(Box<Pat>), |
| 269 | /// A reference pattern, e.g. `&mut (a, b)` |
| 270 | Ref(Box<Pat>, Mutability), |
| 271 | /// A literal |
| 272 | Lit(Box<Expr>), |
| 273 | /// A range pattern, e.g. `1...2` |
| 274 | Range(Box<Expr>, Box<Expr>), |
| 275 | /// `[a, b, ..i, y, z]` is represented as: |
David Tolnay | 432afc0 | 2016-09-24 07:37:13 -0700 | [diff] [blame] | 276 | /// `Pat::Vec(box [a, b], Some(i), box [y, z])` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 277 | Vec(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>), |
| 278 | /// A macro pattern; pre-expansion |
| 279 | Mac(Mac), |
| 280 | } |
| 281 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 282 | /// An arm of a 'match'. |
| 283 | /// |
| 284 | /// E.g. `0...10 => { println!("match!") }` as in |
| 285 | /// |
| 286 | /// ```rust,ignore |
| 287 | /// match n { |
| 288 | /// 0...10 => { println!("match!") }, |
| 289 | /// // .. |
| 290 | /// } |
| 291 | /// ``` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 292 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 293 | pub struct Arm { |
| 294 | pub attrs: Vec<Attribute>, |
| 295 | pub pats: Vec<Pat>, |
| 296 | pub guard: Option<Box<Expr>>, |
| 297 | pub body: Box<Expr>, |
| 298 | } |
| 299 | |
| 300 | /// A capture clause |
| 301 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 302 | pub enum CaptureBy { |
| 303 | Value, |
| 304 | Ref, |
| 305 | } |
| 306 | |
| 307 | /// Limit types of a range (inclusive or exclusive) |
| 308 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 309 | pub enum RangeLimits { |
| 310 | /// Inclusive at the beginning, exclusive at the end |
| 311 | HalfOpen, |
| 312 | /// Inclusive at the beginning and end |
| 313 | Closed, |
| 314 | } |
| 315 | |
| 316 | /// A single field in a struct pattern |
| 317 | /// |
| 318 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` |
David Tolnay | 181bac5 | 2016-09-24 00:10:05 -0700 | [diff] [blame] | 319 | /// 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] | 320 | /// except `is_shorthand` is true |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 321 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 322 | pub struct FieldPat { |
| 323 | /// The identifier for the field |
| 324 | pub ident: Ident, |
| 325 | /// The pattern the field is destructured to |
| 326 | pub pat: Box<Pat>, |
| 327 | pub is_shorthand: bool, |
| 328 | } |
| 329 | |
| 330 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 331 | pub enum BindingMode { |
| 332 | ByRef(Mutability), |
| 333 | ByValue(Mutability), |
| 334 | } |
| 335 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 336 | #[cfg(feature = "parsing")] |
| 337 | pub mod parsing { |
| 338 | use super::*; |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 339 | use {Ident, Lifetime, Ty}; |
Gregory Katz | 1b69f68 | 2016-09-27 21:06:09 -0400 | [diff] [blame] | 340 | use generics::parsing::lifetime; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 341 | use ident::parsing::ident; |
| 342 | use lit::parsing::lit; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 343 | use ty::parsing::ty; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 344 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 345 | named!(pub expr -> Expr, do_parse!( |
| 346 | mut e: alt!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 347 | expr_box |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 348 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 349 | expr_vec |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 350 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 351 | expr_tup |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 352 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 353 | expr_unary |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 354 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 355 | expr_lit |
| 356 | | |
| 357 | expr_if |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 358 | // TODO: IfLet |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 359 | | |
| 360 | expr_while |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 361 | // TODO: WhileLet |
| 362 | // TODO: ForLoop |
| 363 | // TODO: Loop |
| 364 | // TODO: ForLoop |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 365 | | |
| 366 | expr_loop |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 367 | // TODO: Match |
| 368 | // TODO: Closure |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 369 | | |
| 370 | expr_block |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 371 | // TODO: Path |
| 372 | // TODO: AddrOf |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 373 | | |
| 374 | expr_break |
| 375 | | |
| 376 | expr_continue |
| 377 | | |
| 378 | expr_ret |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 379 | // TODO: Mac |
| 380 | // TODO: Struct |
| 381 | // TODO: Repeat |
| 382 | // TODO: Pparen |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 383 | ) >> |
| 384 | many0!(alt!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 385 | tap!(args: and_call => { |
| 386 | e = Expr::Call(Box::new(e), args); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 387 | }) |
| 388 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 389 | tap!(more: and_method_call => { |
| 390 | let (method, ascript, mut args) = more; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 391 | args.insert(0, e); |
| 392 | e = Expr::MethodCall(method, ascript, args); |
| 393 | }) |
| 394 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 395 | tap!(more: and_binary => { |
| 396 | let (op, other) = more; |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 397 | e = Expr::Binary(op, Box::new(e), Box::new(other)); |
| 398 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 399 | | |
| 400 | tap!(ty: and_cast => { |
| 401 | e = Expr::Cast(Box::new(e), Box::new(ty)); |
| 402 | }) |
| 403 | | |
| 404 | tap!(ty: and_ascription => { |
| 405 | e = Expr::Type(Box::new(e), Box::new(ty)); |
| 406 | }) |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 407 | // TODO: Assign |
| 408 | // TODO: AssignOp |
| 409 | // TODO: Field |
| 410 | // TODO: TupField |
| 411 | // TODO: Index |
| 412 | // TODO: Range |
| 413 | // TODO: Try |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 414 | )) >> |
| 415 | (e) |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 416 | )); |
| 417 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 418 | named!(expr_box -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 419 | keyword!("box") >> |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 420 | inner: expr >> |
| 421 | (Expr::Box(Box::new(inner))) |
| 422 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 423 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 424 | named!(expr_vec -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 425 | punct!("[") >> |
| 426 | elems: separated_list!(punct!(","), expr) >> |
| 427 | punct!("]") >> |
| 428 | (Expr::Vec(elems)) |
| 429 | )); |
| 430 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 431 | named!(and_call -> Vec<Expr>, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 432 | punct!("(") >> |
| 433 | args: separated_list!(punct!(","), expr) >> |
| 434 | punct!(")") >> |
| 435 | (args) |
| 436 | )); |
| 437 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 438 | named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 439 | punct!(".") >> |
| 440 | method: ident >> |
| 441 | ascript: opt_vec!(delimited!( |
| 442 | punct!("<"), |
| 443 | separated_list!(punct!(","), ty), |
| 444 | punct!(">") |
| 445 | )) >> |
| 446 | punct!("(") >> |
| 447 | args: separated_list!(punct!(","), expr) >> |
| 448 | punct!(")") >> |
| 449 | (method, ascript, args) |
| 450 | )); |
| 451 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 452 | named!(expr_tup -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 453 | punct!("(") >> |
| 454 | elems: separated_list!(punct!(","), expr) >> |
| 455 | punct!(")") >> |
| 456 | (Expr::Tup(elems)) |
| 457 | )); |
| 458 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 459 | named!(and_binary -> (BinOp, Expr), tuple!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 460 | alt!( |
| 461 | punct!("&&") => { |_| BinOp::And } |
| 462 | | |
| 463 | punct!("||") => { |_| BinOp::Or } |
| 464 | | |
| 465 | punct!("<<") => { |_| BinOp::Shl } |
| 466 | | |
| 467 | punct!(">>") => { |_| BinOp::Shr } |
| 468 | | |
| 469 | punct!("==") => { |_| BinOp::Eq } |
| 470 | | |
| 471 | punct!("<=") => { |_| BinOp::Le } |
| 472 | | |
| 473 | punct!("!=") => { |_| BinOp::Ne } |
| 474 | | |
| 475 | punct!(">=") => { |_| BinOp::Ge } |
| 476 | | |
| 477 | punct!("+") => { |_| BinOp::Add } |
| 478 | | |
| 479 | punct!("-") => { |_| BinOp::Sub } |
| 480 | | |
| 481 | punct!("*") => { |_| BinOp::Mul } |
| 482 | | |
| 483 | punct!("/") => { |_| BinOp::Div } |
| 484 | | |
| 485 | punct!("%") => { |_| BinOp::Rem } |
| 486 | | |
| 487 | punct!("^") => { |_| BinOp::BitXor } |
| 488 | | |
| 489 | punct!("&") => { |_| BinOp::BitAnd } |
| 490 | | |
| 491 | punct!("|") => { |_| BinOp::BitOr } |
| 492 | | |
| 493 | punct!("<") => { |_| BinOp::Lt } |
| 494 | | |
| 495 | punct!(">") => { |_| BinOp::Gt } |
| 496 | ), |
| 497 | expr |
| 498 | )); |
| 499 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 500 | named!(expr_unary -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 501 | operator: alt!( |
| 502 | punct!("*") => { |_| UnOp::Deref } |
| 503 | | |
| 504 | punct!("!") => { |_| UnOp::Not } |
| 505 | | |
| 506 | punct!("-") => { |_| UnOp::Neg } |
| 507 | ) >> |
| 508 | operand: expr >> |
| 509 | (Expr::Unary(operator, Box::new(operand))) |
| 510 | )); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 511 | |
| 512 | named!(expr_lit -> Expr, map!(lit, Expr::Lit)); |
| 513 | |
| 514 | named!(and_cast -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 515 | keyword!("as") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 516 | ty: ty >> |
| 517 | (ty) |
| 518 | )); |
| 519 | |
| 520 | named!(and_ascription -> Ty, preceded!(punct!(":"), ty)); |
| 521 | |
| 522 | named!(expr_if -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 523 | keyword!("if") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 524 | cond: expr >> |
| 525 | punct!("{") >> |
| 526 | then_block: within_block >> |
| 527 | punct!("}") >> |
| 528 | else_block: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 529 | keyword!("else"), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 530 | alt!( |
| 531 | expr_if |
| 532 | | |
| 533 | do_parse!( |
| 534 | punct!("{") >> |
| 535 | else_block: within_block >> |
| 536 | punct!("}") >> |
| 537 | (Expr::Block(Box::new(Block { |
| 538 | stmts: else_block, |
| 539 | rules: BlockCheckMode::Default, |
| 540 | }))) |
| 541 | ) |
| 542 | ) |
| 543 | )) >> |
| 544 | (Expr::If( |
| 545 | Box::new(cond), |
| 546 | Box::new(Block { |
| 547 | stmts: then_block, |
| 548 | rules: BlockCheckMode::Default, |
| 549 | }), |
| 550 | else_block.map(Box::new), |
| 551 | )) |
| 552 | )); |
| 553 | |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 554 | named!(expr_loop -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 555 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 556 | keyword!("loop") >> |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 557 | loop_block: block >> |
| 558 | (Expr::Loop( |
| 559 | Box::new(loop_block), |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 560 | lbl, |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 561 | )) |
| 562 | )); |
| 563 | |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 564 | named!(expr_while -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 565 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 57ffbde | 2016-09-30 09:38:04 -0700 | [diff] [blame] | 566 | keyword!("while") >> |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 567 | cond: expr >> |
| 568 | while_block: block >> |
| 569 | (Expr::While( |
| 570 | Box::new(cond), |
| 571 | Box::new(while_block), |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 572 | lbl, |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 573 | )) |
| 574 | )); |
| 575 | |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 576 | named!(expr_continue -> Expr, do_parse!( |
| 577 | keyword!("continue") >> |
| 578 | lbl: option!(label) >> |
| 579 | (Expr::Continue( |
| 580 | lbl, |
| 581 | )) |
| 582 | )); |
| 583 | |
| 584 | named!(expr_break -> Expr, do_parse!( |
| 585 | keyword!("break") >> |
| 586 | lbl: option!(label) >> |
| 587 | (Expr::Break( |
| 588 | lbl, |
| 589 | )) |
| 590 | )); |
| 591 | |
| 592 | named!(expr_ret -> Expr, do_parse!( |
| 593 | keyword!("return") >> |
| 594 | ret_value: option!(expr) >> |
| 595 | (Expr::Ret( |
| 596 | ret_value.map(Box::new), |
| 597 | )) |
| 598 | )); |
| 599 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 600 | named!(expr_block -> Expr, map!(block, |b| Expr::Block(Box::new(b)))); |
| 601 | |
| 602 | named!(block -> Block, do_parse!( |
| 603 | rules: block_check_mode >> |
| 604 | punct!("{") >> |
| 605 | stmts: within_block >> |
| 606 | punct!("}") >> |
| 607 | (Block { |
| 608 | stmts: stmts, |
| 609 | rules: rules, |
| 610 | }) |
| 611 | )); |
| 612 | |
| 613 | named!(block_check_mode -> BlockCheckMode, alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 614 | keyword!("unsafe") => { |_| BlockCheckMode::Unsafe } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 615 | | |
| 616 | epsilon!() => { |_| BlockCheckMode::Default } |
| 617 | )); |
| 618 | |
| 619 | named!(within_block -> Vec<Stmt>, do_parse!( |
David Tolnay | 181bac5 | 2016-09-24 00:10:05 -0700 | [diff] [blame] | 620 | mut most: many0!(standalone_stmt) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 621 | last: option!(expr) >> |
| 622 | (match last { |
| 623 | None => most, |
| 624 | Some(last) => { |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 625 | most.push(Stmt::Expr(Box::new(last))); |
| 626 | most |
| 627 | } |
| 628 | }) |
| 629 | )); |
| 630 | |
| 631 | named!(standalone_stmt -> Stmt, alt!( |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 632 | // TODO: local |
| 633 | // TODO: item |
| 634 | // TODO: expr |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 635 | stmt_semi |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 636 | // TODO: mac |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 637 | )); |
| 638 | |
| 639 | named!(stmt_semi -> Stmt, do_parse!( |
| 640 | e: expr >> |
| 641 | punct!(";") >> |
| 642 | (Stmt::Semi(Box::new(e))) |
| 643 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 644 | |
| 645 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 646 | } |
| 647 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 648 | #[cfg(feature = "printing")] |
| 649 | mod printing { |
| 650 | use super::*; |
| 651 | use quote::{Tokens, ToTokens}; |
| 652 | |
| 653 | impl ToTokens for Expr { |
| 654 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 655 | match *self { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 656 | Expr::Box(ref _inner) => unimplemented!(), |
| 657 | Expr::Vec(ref _inner) => unimplemented!(), |
| 658 | Expr::Call(ref _func, ref _args) => unimplemented!(), |
| 659 | Expr::MethodCall(ref _ident, ref _ascript, ref _args) => unimplemented!(), |
| 660 | Expr::Tup(ref fields) => { |
| 661 | tokens.append("("); |
| 662 | tokens.append_separated(fields, ","); |
| 663 | if fields.len() == 1 { |
| 664 | tokens.append(","); |
| 665 | } |
| 666 | tokens.append(")"); |
| 667 | } |
| 668 | Expr::Binary(_op, ref _left, ref _right) => unimplemented!(), |
| 669 | Expr::Unary(_op, ref _expr) => unimplemented!(), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 670 | Expr::Lit(ref lit) => lit.to_tokens(tokens), |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 671 | Expr::Cast(ref _expr, ref _ty) => unimplemented!(), |
| 672 | Expr::Type(ref _expr, ref _ty) => unimplemented!(), |
| 673 | Expr::If(ref _cond, ref _then_block, ref _else_block) => unimplemented!(), |
| 674 | Expr::IfLet(ref _pat, ref _expr, ref _then_block, ref _else_block) => unimplemented!(), |
| 675 | Expr::While(ref _cond, ref _body, ref _label) => unimplemented!(), |
| 676 | Expr::WhileLet(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(), |
| 677 | Expr::ForLoop(ref _pat, ref _expr, ref _body, ref _label) => unimplemented!(), |
| 678 | Expr::Loop(ref _body, ref _label) => unimplemented!(), |
| 679 | Expr::Match(ref _expr, ref _arms) => unimplemented!(), |
| 680 | Expr::Closure(_capture, ref _decl, ref _body) => unimplemented!(), |
| 681 | Expr::Block(ref _block) => unimplemented!(), |
| 682 | Expr::Assign(ref _var, ref _expr) => unimplemented!(), |
| 683 | Expr::AssignOp(_op, ref _var, ref _expr) => unimplemented!(), |
| 684 | Expr::Field(ref _expr, ref _field) => unimplemented!(), |
| 685 | Expr::TupField(ref _expr, _field) => unimplemented!(), |
| 686 | Expr::Index(ref _expr, ref _index) => unimplemented!(), |
| 687 | Expr::Range(ref _from, ref _to, _limits) => unimplemented!(), |
| 688 | Expr::Path(ref _qself, ref _path) => unimplemented!(), |
| 689 | Expr::AddrOf(_mutability, ref _expr) => unimplemented!(), |
| 690 | Expr::Break(ref _label) => unimplemented!(), |
| 691 | Expr::Continue(ref _label) => unimplemented!(), |
| 692 | Expr::Ret(ref _expr) => unimplemented!(), |
| 693 | Expr::Mac(ref _mac) => unimplemented!(), |
| 694 | Expr::Struct(ref _path, ref _fields, ref _base) => unimplemented!(), |
| 695 | Expr::Repeat(ref _expr, ref _times) => unimplemented!(), |
| 696 | Expr::Paren(ref _inner) => unimplemented!(), |
| 697 | Expr::Try(ref _expr) => unimplemented!(), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |