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