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 | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 343 | use {Delimited, DelimToken, FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, TokenTree, 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 | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 349 | use mac::parsing::mac; |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 350 | use nom::IResult::Error; |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 351 | use ty::parsing::{mutability, path, qpath, ty}; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 352 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 353 | named!(pub expr -> Expr, do_parse!( |
| 354 | mut e: alt!( |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 355 | expr_lit // needs to be before expr_struct |
| 356 | | |
| 357 | expr_struct // needs to be before expr_path |
| 358 | | |
| 359 | expr_paren // needs to be before expr_tup |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 360 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 361 | expr_box |
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_vec |
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_tup |
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_unary |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 368 | | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 369 | expr_if |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 370 | | |
| 371 | expr_while |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 372 | | |
| 373 | expr_for_loop |
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 | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 392 | | |
| 393 | expr_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 | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 434 | named!(expr_mac -> Expr, map!(mac, Expr::Mac)); |
| 435 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 436 | named!(expr_paren -> Expr, do_parse!( |
| 437 | punct!("(") >> |
| 438 | e: expr >> |
| 439 | punct!(")") >> |
| 440 | (Expr::Paren(Box::new(e))) |
| 441 | )); |
| 442 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 443 | named!(expr_box -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 444 | keyword!("box") >> |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 445 | inner: expr >> |
| 446 | (Expr::Box(Box::new(inner))) |
| 447 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 448 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 449 | named!(expr_vec -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 450 | punct!("[") >> |
| 451 | elems: separated_list!(punct!(","), expr) >> |
| 452 | punct!("]") >> |
| 453 | (Expr::Vec(elems)) |
| 454 | )); |
| 455 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 456 | named!(and_call -> Vec<Expr>, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 457 | punct!("(") >> |
| 458 | args: separated_list!(punct!(","), expr) >> |
| 459 | punct!(")") >> |
| 460 | (args) |
| 461 | )); |
| 462 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 463 | named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 464 | punct!(".") >> |
| 465 | method: ident >> |
| 466 | ascript: opt_vec!(delimited!( |
| 467 | punct!("<"), |
| 468 | separated_list!(punct!(","), ty), |
| 469 | punct!(">") |
| 470 | )) >> |
| 471 | punct!("(") >> |
| 472 | args: separated_list!(punct!(","), expr) >> |
| 473 | punct!(")") >> |
| 474 | (method, ascript, args) |
| 475 | )); |
| 476 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 477 | named!(expr_tup -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 478 | punct!("(") >> |
| 479 | elems: separated_list!(punct!(","), expr) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 480 | option!(punct!(",")) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 481 | punct!(")") >> |
| 482 | (Expr::Tup(elems)) |
| 483 | )); |
| 484 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 485 | named!(and_binary -> (BinOp, Expr), tuple!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 486 | alt!( |
| 487 | punct!("&&") => { |_| BinOp::And } |
| 488 | | |
| 489 | punct!("||") => { |_| BinOp::Or } |
| 490 | | |
| 491 | punct!("<<") => { |_| BinOp::Shl } |
| 492 | | |
| 493 | punct!(">>") => { |_| BinOp::Shr } |
| 494 | | |
| 495 | punct!("==") => { |_| BinOp::Eq } |
| 496 | | |
| 497 | punct!("<=") => { |_| BinOp::Le } |
| 498 | | |
| 499 | punct!("!=") => { |_| BinOp::Ne } |
| 500 | | |
| 501 | punct!(">=") => { |_| BinOp::Ge } |
| 502 | | |
| 503 | punct!("+") => { |_| BinOp::Add } |
| 504 | | |
| 505 | punct!("-") => { |_| BinOp::Sub } |
| 506 | | |
| 507 | punct!("*") => { |_| BinOp::Mul } |
| 508 | | |
| 509 | punct!("/") => { |_| BinOp::Div } |
| 510 | | |
| 511 | punct!("%") => { |_| BinOp::Rem } |
| 512 | | |
| 513 | punct!("^") => { |_| BinOp::BitXor } |
| 514 | | |
| 515 | punct!("&") => { |_| BinOp::BitAnd } |
| 516 | | |
| 517 | punct!("|") => { |_| BinOp::BitOr } |
| 518 | | |
| 519 | punct!("<") => { |_| BinOp::Lt } |
| 520 | | |
| 521 | punct!(">") => { |_| BinOp::Gt } |
| 522 | ), |
| 523 | expr |
| 524 | )); |
| 525 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 526 | named!(expr_unary -> Expr, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 527 | operator: alt!( |
| 528 | punct!("*") => { |_| UnOp::Deref } |
| 529 | | |
| 530 | punct!("!") => { |_| UnOp::Not } |
| 531 | | |
| 532 | punct!("-") => { |_| UnOp::Neg } |
| 533 | ) >> |
| 534 | operand: expr >> |
| 535 | (Expr::Unary(operator, Box::new(operand))) |
| 536 | )); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 537 | |
| 538 | named!(expr_lit -> Expr, map!(lit, Expr::Lit)); |
| 539 | |
| 540 | named!(and_cast -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 541 | keyword!("as") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 542 | ty: ty >> |
| 543 | (ty) |
| 544 | )); |
| 545 | |
| 546 | named!(and_ascription -> Ty, preceded!(punct!(":"), ty)); |
| 547 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 548 | enum Cond { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 549 | Let(Pat, Expr), |
| 550 | Expr(Expr), |
| 551 | } |
| 552 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 553 | named!(cond -> Cond, alt!( |
| 554 | do_parse!( |
| 555 | keyword!("let") >> |
| 556 | pat: pat >> |
| 557 | punct!("=") >> |
| 558 | value: expr >> |
| 559 | (Cond::Let(pat, value)) |
| 560 | ) |
| 561 | | |
| 562 | map!(expr, Cond::Expr) |
| 563 | )); |
| 564 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 565 | named!(expr_if -> Expr, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 566 | keyword!("if") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 567 | cond: cond >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 568 | punct!("{") >> |
| 569 | then_block: within_block >> |
| 570 | punct!("}") >> |
| 571 | else_block: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 572 | keyword!("else"), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 573 | alt!( |
| 574 | expr_if |
| 575 | | |
| 576 | do_parse!( |
| 577 | punct!("{") >> |
| 578 | else_block: within_block >> |
| 579 | punct!("}") >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 580 | (Expr::Block(BlockCheckMode::Default, Block { |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 581 | stmts: else_block, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 582 | })) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 583 | ) |
| 584 | ) |
| 585 | )) >> |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 586 | (match cond { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 587 | Cond::Let(pat, expr) => Expr::IfLet( |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 588 | Box::new(pat), |
| 589 | Box::new(expr), |
| 590 | Block { |
| 591 | stmts: then_block, |
| 592 | }, |
| 593 | else_block.map(Box::new), |
| 594 | ), |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 595 | Cond::Expr(cond) => Expr::If( |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 596 | Box::new(cond), |
| 597 | Block { |
| 598 | stmts: then_block, |
| 599 | }, |
| 600 | else_block.map(Box::new), |
| 601 | ), |
| 602 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 603 | )); |
| 604 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 605 | named!(expr_for_loop -> Expr, do_parse!( |
| 606 | lbl: option!(terminated!(label, punct!(":"))) >> |
| 607 | keyword!("for") >> |
| 608 | pat: pat >> |
| 609 | keyword!("in") >> |
| 610 | expr: expr >> |
| 611 | loop_block: block >> |
| 612 | (Expr::ForLoop(Box::new(pat), Box::new(expr), loop_block, lbl)) |
| 613 | )); |
| 614 | |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 615 | named!(expr_loop -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 616 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 617 | keyword!("loop") >> |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 618 | loop_block: block >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 619 | (Expr::Loop(loop_block, lbl)) |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 620 | )); |
| 621 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 622 | named!(expr_match -> Expr, do_parse!( |
| 623 | keyword!("match") >> |
| 624 | obj: expr >> |
| 625 | punct!("{") >> |
| 626 | arms: many0!(do_parse!( |
| 627 | attrs: many0!(outer_attr) >> |
| 628 | pats: separated_nonempty_list!(punct!("|"), pat) >> |
| 629 | guard: option!(preceded!(keyword!("if"), expr)) >> |
| 630 | punct!("=>") >> |
| 631 | body: alt!( |
| 632 | terminated!(expr, punct!(",")) |
| 633 | | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 634 | map!(block, |blk| Expr::Block(BlockCheckMode::Default, blk)) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 635 | ) >> |
| 636 | (Arm { |
| 637 | attrs: attrs, |
| 638 | pats: pats, |
| 639 | guard: guard.map(Box::new), |
| 640 | body: Box::new(body), |
| 641 | }) |
| 642 | )) >> |
| 643 | punct!("}") >> |
| 644 | (Expr::Match(Box::new(obj), arms)) |
| 645 | )); |
| 646 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 647 | named!(expr_closure -> Expr, do_parse!( |
| 648 | capture: capture_by >> |
| 649 | punct!("|") >> |
| 650 | inputs: separated_list!(punct!(","), closure_arg) >> |
| 651 | punct!("|") >> |
| 652 | ret_and_body: alt!( |
| 653 | do_parse!( |
| 654 | punct!("->") >> |
| 655 | ty: ty >> |
| 656 | body: block >> |
| 657 | ((FunctionRetTy::Ty(ty), body)) |
| 658 | ) |
| 659 | | |
| 660 | map!(expr, |e| ( |
| 661 | FunctionRetTy::Default, |
| 662 | Block { |
| 663 | stmts: vec![Stmt::Expr(Box::new(e))], |
| 664 | }, |
| 665 | )) |
| 666 | ) >> |
| 667 | (Expr::Closure( |
| 668 | capture, |
| 669 | Box::new(FnDecl { |
| 670 | inputs: inputs, |
| 671 | output: ret_and_body.0, |
| 672 | }), |
| 673 | ret_and_body.1, |
| 674 | )) |
| 675 | )); |
| 676 | |
| 677 | named!(closure_arg -> FnArg, do_parse!( |
| 678 | pat: pat >> |
| 679 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 680 | (FnArg { |
| 681 | pat: pat, |
| 682 | ty: ty.unwrap_or(Ty::Infer), |
| 683 | }) |
| 684 | )); |
| 685 | |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 686 | named!(expr_while -> Expr, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 687 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 57ffbde | 2016-09-30 09:38:04 -0700 | [diff] [blame] | 688 | keyword!("while") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 689 | cond: cond >> |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 690 | while_block: block >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 691 | (match cond { |
| 692 | Cond::Let(pat, expr) => Expr::WhileLet( |
| 693 | Box::new(pat), |
| 694 | Box::new(expr), |
| 695 | while_block, |
| 696 | lbl, |
| 697 | ), |
| 698 | Cond::Expr(cond) => Expr::While( |
| 699 | Box::new(cond), |
| 700 | while_block, |
| 701 | lbl, |
| 702 | ), |
| 703 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 704 | )); |
| 705 | |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 706 | named!(expr_continue -> Expr, do_parse!( |
| 707 | keyword!("continue") >> |
| 708 | lbl: option!(label) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 709 | (Expr::Continue(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 710 | )); |
| 711 | |
| 712 | named!(expr_break -> Expr, do_parse!( |
| 713 | keyword!("break") >> |
| 714 | lbl: option!(label) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 715 | (Expr::Break(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 716 | )); |
| 717 | |
| 718 | named!(expr_ret -> Expr, do_parse!( |
| 719 | keyword!("return") >> |
| 720 | ret_value: option!(expr) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 721 | (Expr::Ret(ret_value.map(Box::new))) |
| 722 | )); |
| 723 | |
| 724 | named!(expr_struct -> Expr, do_parse!( |
| 725 | path: path >> |
| 726 | punct!("{") >> |
| 727 | fields: separated_list!(punct!(","), field_value) >> |
| 728 | base: option!(do_parse!( |
| 729 | cond!(!fields.is_empty(), punct!(",")) >> |
| 730 | punct!("..") >> |
| 731 | base: expr >> |
| 732 | (base) |
| 733 | )) >> |
| 734 | punct!("}") >> |
| 735 | (Expr::Struct(path, fields, base.map(Box::new))) |
| 736 | )); |
| 737 | |
| 738 | named!(field_value -> FieldValue, do_parse!( |
| 739 | name: ident >> |
| 740 | punct!(":") >> |
| 741 | value: expr >> |
| 742 | (FieldValue { |
| 743 | ident: name, |
| 744 | expr: value, |
| 745 | }) |
| 746 | )); |
| 747 | |
| 748 | named!(expr_repeat -> Expr, do_parse!( |
| 749 | punct!("[") >> |
| 750 | value: expr >> |
| 751 | punct!(";") >> |
| 752 | times: expr >> |
| 753 | punct!("]") >> |
| 754 | (Expr::Repeat(Box::new(value), Box::new(times))) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 755 | )); |
| 756 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 757 | named!(expr_block -> Expr, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 758 | rules: block_check_mode >> |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 759 | b: block >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 760 | (Expr::Block(rules, Block { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 761 | stmts: b.stmts, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 762 | })) |
| 763 | )); |
| 764 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 765 | named!(expr_path -> Expr, map!(qpath, |(qself, path)| Expr::Path(qself, path))); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 766 | |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 767 | named!(expr_addr_of -> Expr, do_parse!( |
| 768 | punct!("&") >> |
| 769 | mutability: mutability >> |
| 770 | expr: expr >> |
| 771 | (Expr::AddrOf(mutability, Box::new(expr))) |
| 772 | )); |
| 773 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 774 | named!(pub block -> Block, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 775 | punct!("{") >> |
| 776 | stmts: within_block >> |
| 777 | punct!("}") >> |
| 778 | (Block { |
| 779 | stmts: stmts, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 780 | }) |
| 781 | )); |
| 782 | |
| 783 | named!(block_check_mode -> BlockCheckMode, alt!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 784 | keyword!("unsafe") => { |_| BlockCheckMode::Unsafe } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 785 | | |
| 786 | epsilon!() => { |_| BlockCheckMode::Default } |
| 787 | )); |
| 788 | |
| 789 | named!(within_block -> Vec<Stmt>, do_parse!( |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 790 | many0!(punct!(";")) >> |
| 791 | mut standalone: many0!(terminated!(standalone_stmt, many0!(punct!(";")))) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 792 | last: option!(expr) >> |
| 793 | (match last { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 794 | None => standalone, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 795 | Some(last) => { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 796 | standalone.push(Stmt::Expr(Box::new(last))); |
| 797 | standalone |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 798 | } |
| 799 | }) |
| 800 | )); |
| 801 | |
| 802 | named!(standalone_stmt -> Stmt, alt!( |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 803 | stmt_local |
| 804 | | |
| 805 | stmt_item |
| 806 | | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 807 | stmt_expr |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 808 | // TODO: mac |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 809 | )); |
| 810 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 811 | named!(stmt_local -> Stmt, do_parse!( |
| 812 | attrs: many0!(outer_attr) >> |
| 813 | keyword!("let") >> |
| 814 | pat: pat >> |
| 815 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 816 | init: option!(preceded!(punct!("="), expr)) >> |
| 817 | punct!(";") >> |
| 818 | (Stmt::Local(Box::new(Local { |
| 819 | pat: Box::new(pat), |
| 820 | ty: ty.map(Box::new), |
| 821 | init: init.map(Box::new), |
| 822 | attrs: attrs, |
| 823 | }))) |
| 824 | )); |
| 825 | |
| 826 | named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i)))); |
| 827 | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 828 | fn requires_semi(e: &Expr) -> bool { |
| 829 | match *e { |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame^] | 830 | Expr::Mac(ref mac) => match mac.tts.last() { |
| 831 | Some(&TokenTree::Delimited( |
| 832 | Delimited { delim: DelimToken::Brace, .. } |
| 833 | )) => false, |
| 834 | _ => true, |
| 835 | }, |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 836 | |
| 837 | Expr::If(_, _, _) | |
| 838 | Expr::IfLet(_, _, _, _) | |
| 839 | Expr::While(_, _, _) | |
| 840 | Expr::WhileLet(_, _, _, _) | |
| 841 | Expr::ForLoop(_, _, _, _) | |
| 842 | Expr::Loop(_, _) | |
| 843 | Expr::Match(_, _) | |
| 844 | Expr::Block(_, _) => false, |
| 845 | |
| 846 | _ => true, |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 851 | e: expr >> |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 852 | semi: option!(punct!(";")) >> |
| 853 | (if semi.is_some() { |
| 854 | Stmt::Semi(Box::new(e)) |
| 855 | } else if requires_semi(&e) { |
| 856 | return Error; |
| 857 | } else { |
| 858 | Stmt::Expr(Box::new(e)) |
| 859 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 860 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 861 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 862 | named!(pub pat -> Pat, alt!( |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 863 | pat_wild |
| 864 | | |
| 865 | pat_ident |
| 866 | // TODO: Struct |
| 867 | // TODO: TupleStruct |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 868 | | |
| 869 | pat_path |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 870 | // TODO: Tuple |
| 871 | // TODO: Box |
| 872 | // TODO: Ref |
| 873 | // TODO: Lit |
| 874 | // TODO: Range |
| 875 | // TODO: Vec |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 876 | | |
| 877 | pat_mac |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 878 | )); |
| 879 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 880 | named!(pat_mac -> Pat, map!(mac, Pat::Mac)); |
| 881 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 882 | named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild)); |
| 883 | |
| 884 | named!(pat_ident -> Pat, do_parse!( |
| 885 | mode: option!(keyword!("ref")) >> |
| 886 | mutability: mutability >> |
| 887 | name: ident >> |
| 888 | subpat: option!(preceded!(punct!("@"), pat)) >> |
| 889 | (Pat::Ident( |
| 890 | if mode.is_some() { |
| 891 | BindingMode::ByRef(mutability) |
| 892 | } else { |
| 893 | BindingMode::ByValue(mutability) |
| 894 | }, |
| 895 | name, |
| 896 | subpat.map(Box::new), |
| 897 | )) |
| 898 | )); |
| 899 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 900 | named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path))); |
| 901 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 902 | named!(capture_by -> CaptureBy, alt!( |
| 903 | keyword!("move") => { |_| CaptureBy::Value } |
| 904 | | |
| 905 | epsilon!() => { |_| CaptureBy::Ref } |
| 906 | )); |
| 907 | |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 908 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 909 | } |
| 910 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 911 | #[cfg(feature = "printing")] |
| 912 | mod printing { |
| 913 | use super::*; |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 914 | use {FunctionRetTy, Mutability, Ty}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 915 | use quote::{Tokens, ToTokens}; |
| 916 | |
| 917 | impl ToTokens for Expr { |
| 918 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 919 | match *self { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 920 | Expr::Box(ref inner) => { |
| 921 | tokens.append("box"); |
| 922 | inner.to_tokens(tokens); |
| 923 | } |
| 924 | Expr::Vec(ref tys) => { |
| 925 | tokens.append("["); |
| 926 | tokens.append_separated(tys, ","); |
| 927 | tokens.append("]"); |
| 928 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 929 | Expr::Call(ref func, ref args) => { |
| 930 | func.to_tokens(tokens); |
| 931 | tokens.append("("); |
| 932 | tokens.append_separated(args, ","); |
| 933 | tokens.append(")"); |
| 934 | } |
| 935 | Expr::MethodCall(ref ident, ref ascript, ref args) => { |
| 936 | args[0].to_tokens(tokens); |
| 937 | tokens.append("."); |
| 938 | ident.to_tokens(tokens); |
| 939 | if ascript.len() > 0 { |
| 940 | tokens.append("::"); |
| 941 | tokens.append("<"); |
| 942 | tokens.append_separated(ascript, ","); |
| 943 | tokens.append(">"); |
| 944 | } |
| 945 | tokens.append("("); |
| 946 | tokens.append_separated(&args[1..], ","); |
| 947 | tokens.append(")"); |
| 948 | } |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 949 | Expr::Tup(ref fields) => { |
| 950 | tokens.append("("); |
| 951 | tokens.append_separated(fields, ","); |
| 952 | if fields.len() == 1 { |
| 953 | tokens.append(","); |
| 954 | } |
| 955 | tokens.append(")"); |
| 956 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 957 | Expr::Binary(op, ref left, ref right) => { |
| 958 | left.to_tokens(tokens); |
| 959 | op.to_tokens(tokens); |
| 960 | right.to_tokens(tokens); |
| 961 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 962 | Expr::Unary(op, ref expr) => { |
| 963 | op.to_tokens(tokens); |
| 964 | expr.to_tokens(tokens); |
| 965 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 966 | Expr::Lit(ref lit) => lit.to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 967 | Expr::Cast(ref expr, ref ty) => { |
| 968 | expr.to_tokens(tokens); |
| 969 | tokens.append("as"); |
| 970 | ty.to_tokens(tokens); |
| 971 | } |
| 972 | Expr::Type(ref expr, ref ty) => { |
| 973 | expr.to_tokens(tokens); |
| 974 | tokens.append(":"); |
| 975 | ty.to_tokens(tokens); |
| 976 | } |
| 977 | Expr::If(ref cond, ref then_block, ref else_block) => { |
| 978 | tokens.append("if"); |
| 979 | cond.to_tokens(tokens); |
| 980 | then_block.to_tokens(tokens); |
| 981 | if let Some(ref else_block) = *else_block { |
| 982 | tokens.append("else"); |
| 983 | else_block.to_tokens(tokens); |
| 984 | } |
| 985 | } |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 986 | Expr::IfLet(ref pat, ref expr, ref then_block, ref else_block) => { |
| 987 | tokens.append("if"); |
| 988 | tokens.append("let"); |
| 989 | pat.to_tokens(tokens); |
| 990 | tokens.append("="); |
| 991 | expr.to_tokens(tokens); |
| 992 | then_block.to_tokens(tokens); |
| 993 | if let Some(ref else_block) = *else_block { |
| 994 | tokens.append("else"); |
| 995 | else_block.to_tokens(tokens); |
| 996 | } |
| 997 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 998 | Expr::While(ref cond, ref body, ref label) => { |
| 999 | if let Some(ref label) = *label { |
| 1000 | label.to_tokens(tokens); |
| 1001 | tokens.append(":"); |
| 1002 | } |
| 1003 | tokens.append("while"); |
| 1004 | cond.to_tokens(tokens); |
| 1005 | body.to_tokens(tokens); |
| 1006 | } |
| 1007 | Expr::WhileLet(ref pat, ref expr, ref body, ref label) => { |
| 1008 | if let Some(ref label) = *label { |
| 1009 | label.to_tokens(tokens); |
| 1010 | tokens.append(":"); |
| 1011 | } |
| 1012 | tokens.append("while"); |
| 1013 | tokens.append("let"); |
| 1014 | pat.to_tokens(tokens); |
| 1015 | tokens.append("="); |
| 1016 | expr.to_tokens(tokens); |
| 1017 | body.to_tokens(tokens); |
| 1018 | } |
| 1019 | Expr::ForLoop(ref pat, ref expr, ref body, ref label) => { |
| 1020 | if let Some(ref label) = *label { |
| 1021 | label.to_tokens(tokens); |
| 1022 | tokens.append(":"); |
| 1023 | } |
| 1024 | tokens.append("for"); |
| 1025 | pat.to_tokens(tokens); |
| 1026 | tokens.append("in"); |
| 1027 | expr.to_tokens(tokens); |
| 1028 | body.to_tokens(tokens); |
| 1029 | } |
| 1030 | Expr::Loop(ref body, ref label) => { |
| 1031 | if let Some(ref label) = *label { |
| 1032 | label.to_tokens(tokens); |
| 1033 | tokens.append(":"); |
| 1034 | } |
| 1035 | tokens.append("loop"); |
| 1036 | body.to_tokens(tokens); |
| 1037 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1038 | Expr::Match(ref expr, ref arms) => { |
| 1039 | tokens.append("match"); |
| 1040 | expr.to_tokens(tokens); |
| 1041 | tokens.append("{"); |
| 1042 | tokens.append_separated(arms, ","); |
| 1043 | tokens.append("}"); |
| 1044 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1045 | Expr::Closure(capture, ref decl, ref body) => { |
| 1046 | capture.to_tokens(tokens); |
| 1047 | tokens.append("|"); |
| 1048 | for (i, input) in decl.inputs.iter().enumerate() { |
| 1049 | if i > 0 { |
| 1050 | tokens.append(","); |
| 1051 | } |
| 1052 | input.pat.to_tokens(tokens); |
| 1053 | match input.ty { |
| 1054 | Ty::Infer => { /* nothing */ } |
| 1055 | _ => { |
| 1056 | tokens.append(":"); |
| 1057 | input.ty.to_tokens(tokens); |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | tokens.append("|"); |
| 1062 | match decl.output { |
| 1063 | FunctionRetTy::Default => { |
| 1064 | if body.stmts.len() == 1 { |
| 1065 | if let Stmt::Expr(ref expr) = body.stmts[0] { |
| 1066 | expr.to_tokens(tokens); |
| 1067 | } else { |
| 1068 | body.to_tokens(tokens); |
| 1069 | } |
| 1070 | } else { |
| 1071 | body.to_tokens(tokens); |
| 1072 | } |
| 1073 | } |
| 1074 | FunctionRetTy::Ty(ref ty) => { |
| 1075 | tokens.append("->"); |
| 1076 | ty.to_tokens(tokens); |
| 1077 | body.to_tokens(tokens); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | Expr::Block(rules, ref block) => { |
| 1082 | rules.to_tokens(tokens); |
| 1083 | block.to_tokens(tokens); |
| 1084 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1085 | Expr::Assign(ref var, ref expr) => { |
| 1086 | var.to_tokens(tokens); |
| 1087 | tokens.append("="); |
| 1088 | expr.to_tokens(tokens); |
| 1089 | } |
| 1090 | Expr::AssignOp(op, ref var, ref expr) => { |
| 1091 | var.to_tokens(tokens); |
| 1092 | tokens.append(op.assign_op()); |
| 1093 | expr.to_tokens(tokens); |
| 1094 | } |
| 1095 | Expr::Field(ref expr, ref field) => { |
| 1096 | expr.to_tokens(tokens); |
| 1097 | tokens.append("."); |
| 1098 | field.to_tokens(tokens); |
| 1099 | } |
| 1100 | Expr::TupField(ref expr, field) => { |
| 1101 | expr.to_tokens(tokens); |
| 1102 | tokens.append("."); |
| 1103 | tokens.append(&field.to_string()); |
| 1104 | } |
| 1105 | Expr::Index(ref expr, ref index) => { |
| 1106 | expr.to_tokens(tokens); |
| 1107 | tokens.append("["); |
| 1108 | index.to_tokens(tokens); |
| 1109 | tokens.append("]"); |
| 1110 | } |
| 1111 | Expr::Range(ref from, ref to, limits) => { |
| 1112 | from.to_tokens(tokens); |
| 1113 | match limits { |
| 1114 | RangeLimits::HalfOpen => tokens.append(".."), |
| 1115 | RangeLimits::Closed => tokens.append("..."), |
| 1116 | } |
| 1117 | to.to_tokens(tokens); |
| 1118 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1119 | Expr::Path(None, ref path) => { |
| 1120 | path.to_tokens(tokens); |
| 1121 | } |
| 1122 | Expr::Path(Some(ref qself), ref path) => { |
| 1123 | tokens.append("<"); |
| 1124 | qself.ty.to_tokens(tokens); |
| 1125 | if qself.position > 0 { |
| 1126 | tokens.append("as"); |
| 1127 | for (i, segment) in path.segments.iter() |
| 1128 | .take(qself.position) |
| 1129 | .enumerate() |
| 1130 | { |
| 1131 | if i > 0 || path.global { |
| 1132 | tokens.append("::"); |
| 1133 | } |
| 1134 | segment.to_tokens(tokens); |
| 1135 | } |
| 1136 | } |
| 1137 | tokens.append(">"); |
| 1138 | for segment in path.segments.iter().skip(qself.position) { |
| 1139 | tokens.append("::"); |
| 1140 | segment.to_tokens(tokens); |
| 1141 | } |
| 1142 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1143 | Expr::AddrOf(mutability, ref expr) => { |
| 1144 | tokens.append("&"); |
| 1145 | mutability.to_tokens(tokens); |
| 1146 | expr.to_tokens(tokens); |
| 1147 | } |
| 1148 | Expr::Break(ref opt_label) => { |
| 1149 | tokens.append("break"); |
| 1150 | opt_label.to_tokens(tokens); |
| 1151 | } |
| 1152 | Expr::Continue(ref opt_label) => { |
| 1153 | tokens.append("continue"); |
| 1154 | opt_label.to_tokens(tokens); |
| 1155 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1156 | Expr::Ret(ref opt_expr) => { |
| 1157 | tokens.append("return"); |
| 1158 | opt_expr.to_tokens(tokens); |
| 1159 | } |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame^] | 1160 | Expr::Mac(ref mac) => mac.to_tokens(tokens), |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1161 | Expr::Struct(ref path, ref fields, ref base) => { |
| 1162 | path.to_tokens(tokens); |
| 1163 | tokens.append("{"); |
| 1164 | tokens.append_separated(fields, ","); |
| 1165 | if let Some(ref base) = *base { |
| 1166 | if !fields.is_empty() { |
| 1167 | tokens.append(","); |
| 1168 | } |
| 1169 | tokens.append(".."); |
| 1170 | base.to_tokens(tokens); |
| 1171 | } |
| 1172 | tokens.append("}"); |
| 1173 | } |
| 1174 | Expr::Repeat(ref expr, ref times) => { |
| 1175 | tokens.append("["); |
| 1176 | expr.to_tokens(tokens); |
| 1177 | tokens.append(";"); |
| 1178 | times.to_tokens(tokens); |
| 1179 | tokens.append("]"); |
| 1180 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1181 | Expr::Paren(ref expr) => { |
| 1182 | tokens.append("("); |
| 1183 | expr.to_tokens(tokens); |
| 1184 | tokens.append(")"); |
| 1185 | } |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1186 | Expr::Try(ref expr) => { |
| 1187 | expr.to_tokens(tokens); |
| 1188 | tokens.append("?"); |
| 1189 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1193 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1194 | impl BinOp { |
| 1195 | fn op(&self) -> &'static str { |
| 1196 | match *self { |
| 1197 | BinOp::Add => "+", |
| 1198 | BinOp::Sub => "-", |
| 1199 | BinOp::Mul => "*", |
| 1200 | BinOp::Div => "/", |
| 1201 | BinOp::Rem => "%", |
| 1202 | BinOp::And => "&&", |
| 1203 | BinOp::Or => "||", |
| 1204 | BinOp::BitXor => "^", |
| 1205 | BinOp::BitAnd => "&", |
| 1206 | BinOp::BitOr => "|", |
| 1207 | BinOp::Shl => "<<", |
| 1208 | BinOp::Shr => ">>", |
| 1209 | BinOp::Eq => "==", |
| 1210 | BinOp::Lt => "<", |
| 1211 | BinOp::Le => "<=", |
| 1212 | BinOp::Ne => "!=", |
| 1213 | BinOp::Ge => ">=", |
| 1214 | BinOp::Gt => ">", |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | fn assign_op(&self) -> &'static str { |
| 1219 | match *self { |
| 1220 | BinOp::Add => "+=", |
| 1221 | BinOp::Sub => "-=", |
| 1222 | BinOp::Mul => "*=", |
| 1223 | BinOp::Div => "/=", |
| 1224 | BinOp::Rem => "%=", |
| 1225 | BinOp::BitXor => "^=", |
| 1226 | BinOp::BitAnd => "&=", |
| 1227 | BinOp::BitOr => "|=", |
| 1228 | BinOp::Shl => "<<=", |
| 1229 | BinOp::Shr => ">>=", |
| 1230 | _ => panic!("bad assignment operator"), |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1235 | impl ToTokens for BinOp { |
| 1236 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1237 | tokens.append(self.op()); |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | impl UnOp { |
| 1242 | fn op(&self) -> &'static str { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1243 | match *self { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1244 | UnOp::Deref => "*", |
| 1245 | UnOp::Not => "!", |
| 1246 | UnOp::Neg => "-", |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1251 | impl ToTokens for UnOp { |
| 1252 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1253 | tokens.append(self.op()); |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1257 | impl ToTokens for FieldValue { |
| 1258 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1259 | self.ident.to_tokens(tokens); |
| 1260 | tokens.append(":"); |
| 1261 | self.expr.to_tokens(tokens); |
| 1262 | } |
| 1263 | } |
| 1264 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1265 | impl ToTokens for Arm { |
| 1266 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1267 | for attr in &self.attrs { |
| 1268 | attr.to_tokens(tokens); |
| 1269 | } |
| 1270 | tokens.append_separated(&self.pats, "|"); |
| 1271 | if let Some(ref guard) = self.guard { |
| 1272 | tokens.append("if"); |
| 1273 | guard.to_tokens(tokens); |
| 1274 | } |
| 1275 | tokens.append("=>"); |
| 1276 | self.body.to_tokens(tokens); |
| 1277 | match *self.body { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1278 | Expr::Block(_, _) => { /* no comma */ } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1279 | _ => tokens.append(","), |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | impl ToTokens for Pat { |
| 1285 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1286 | match *self { |
| 1287 | Pat::Wild => tokens.append("_"), |
| 1288 | Pat::Ident(mode, ref ident, ref subpat) => { |
| 1289 | mode.to_tokens(tokens); |
| 1290 | ident.to_tokens(tokens); |
| 1291 | if let Some(ref subpat) = *subpat { |
| 1292 | tokens.append("@"); |
| 1293 | subpat.to_tokens(tokens); |
| 1294 | } |
| 1295 | } |
| 1296 | Pat::Struct(ref _path, ref _fields, _dots) => unimplemented!(), |
| 1297 | Pat::TupleStruct(ref _path, ref _pats, _dotpos) => unimplemented!(), |
| 1298 | Pat::Path(ref _qself, ref _path) => unimplemented!(), |
| 1299 | Pat::Tuple(ref _pats, _dotpos) => unimplemented!(), |
| 1300 | Pat::Box(ref _inner) => unimplemented!(), |
| 1301 | Pat::Ref(ref _target, _mutability) => unimplemented!(), |
| 1302 | Pat::Lit(ref _expr) => unimplemented!(), |
| 1303 | Pat::Range(ref _lower, ref _upper) => unimplemented!(), |
| 1304 | Pat::Vec(ref _before, ref _dots, ref _after) => unimplemented!(), |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame^] | 1305 | Pat::Mac(ref mac) => mac.to_tokens(tokens), |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | impl ToTokens for BindingMode { |
| 1311 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1312 | match *self { |
| 1313 | BindingMode::ByRef(Mutability::Immutable) => { |
| 1314 | tokens.append("ref"); |
| 1315 | } |
| 1316 | BindingMode::ByRef(Mutability::Mutable) => { |
| 1317 | tokens.append("ref"); |
| 1318 | tokens.append("mut"); |
| 1319 | } |
| 1320 | BindingMode::ByValue(Mutability::Immutable) => {} |
| 1321 | BindingMode::ByValue(Mutability::Mutable) => { |
| 1322 | tokens.append("mut"); |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1327 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1328 | impl ToTokens for CaptureBy { |
| 1329 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1330 | match *self { |
| 1331 | CaptureBy::Value => tokens.append("move"), |
| 1332 | CaptureBy::Ref => { /* nothing */ } |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1337 | impl ToTokens for Block { |
| 1338 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1339 | tokens.append("{"); |
| 1340 | for stmt in &self.stmts { |
| 1341 | stmt.to_tokens(tokens); |
| 1342 | } |
| 1343 | tokens.append("}"); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | impl ToTokens for BlockCheckMode { |
| 1348 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1349 | match *self { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1350 | BlockCheckMode::Default => { /* nothing */ } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1351 | BlockCheckMode::Unsafe => tokens.append("unsafe"), |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | impl ToTokens for Stmt { |
| 1357 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1358 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1359 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1360 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 1361 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
| 1362 | Stmt::Semi(ref expr) => { |
| 1363 | expr.to_tokens(tokens); |
| 1364 | tokens.append(";"); |
| 1365 | } |
| 1366 | Stmt::Mac(ref _mac) => unimplemented!(), |
| 1367 | } |
| 1368 | } |
| 1369 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1370 | |
| 1371 | impl ToTokens for Local { |
| 1372 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1373 | tokens.append("let"); |
| 1374 | self.pat.to_tokens(tokens); |
| 1375 | if let Some(ref ty) = self.ty { |
| 1376 | tokens.append(":"); |
| 1377 | ty.to_tokens(tokens); |
| 1378 | } |
| 1379 | if let Some(ref init) = self.init { |
| 1380 | tokens.append("="); |
| 1381 | init.to_tokens(tokens); |
| 1382 | } |
| 1383 | tokens.append(";"); |
| 1384 | } |
| 1385 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1386 | } |