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