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