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