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!("}") >> |
| 875 | (Stmt::Mac(Box::new(( |
| 876 | Mac { |
| 877 | path: name.into(), |
| 878 | tts: vec![TokenTree::Delimited(Delimited { |
| 879 | delim: DelimToken::Brace, |
| 880 | tts: tts, |
| 881 | })], |
| 882 | }, |
| 883 | MacStmtStyle::Braces, |
| 884 | attrs, |
| 885 | )))) |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 886 | )); |
| 887 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 888 | named!(stmt_local -> Stmt, do_parse!( |
| 889 | attrs: many0!(outer_attr) >> |
| 890 | keyword!("let") >> |
| 891 | pat: pat >> |
| 892 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 893 | init: option!(preceded!(punct!("="), expr)) >> |
| 894 | punct!(";") >> |
| 895 | (Stmt::Local(Box::new(Local { |
| 896 | pat: Box::new(pat), |
| 897 | ty: ty.map(Box::new), |
| 898 | init: init.map(Box::new), |
| 899 | attrs: attrs, |
| 900 | }))) |
| 901 | )); |
| 902 | |
| 903 | named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i)))); |
| 904 | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 905 | fn requires_semi(e: &Expr) -> bool { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 906 | match e.node { |
| 907 | ExprKind::If(_, _, _) | |
| 908 | ExprKind::IfLet(_, _, _, _) | |
| 909 | ExprKind::While(_, _, _) | |
| 910 | ExprKind::WhileLet(_, _, _, _) | |
| 911 | ExprKind::ForLoop(_, _, _, _) | |
| 912 | ExprKind::Loop(_, _) | |
| 913 | ExprKind::Match(_, _) | |
| 914 | ExprKind::Block(_, _) => false, |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 915 | |
| 916 | _ => true, |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 921 | attrs: many0!(outer_attr) >> |
| 922 | mut e: expr >> |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 923 | semi: option!(punct!(";")) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 924 | ({ |
| 925 | e.attrs = attrs; |
| 926 | if semi.is_some() { |
| 927 | Stmt::Semi(Box::new(e)) |
David Tolnay | 092dcb0 | 2016-10-30 10:14:14 -0700 | [diff] [blame] | 928 | } else if requires_semi(&e) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 929 | return Error; |
| 930 | } else { |
| 931 | Stmt::Expr(Box::new(e)) |
| 932 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 933 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 934 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 935 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 936 | named!(pub pat -> Pat, alt!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 937 | pat_wild // must be before pat_ident |
| 938 | | |
| 939 | pat_box // must be before pat_ident |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 940 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 941 | pat_range // must be before pat_lit |
| 942 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 943 | pat_tuple_struct // must be before pat_ident |
| 944 | | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 945 | pat_struct // must be before pat_ident |
| 946 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 947 | pat_mac // must be before pat_ident |
| 948 | | |
David Tolnay | bcdbdd0 | 2016-10-24 23:05:02 -0700 | [diff] [blame] | 949 | pat_lit // must be before pat_ident |
| 950 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 951 | pat_ident // must be before pat_path |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 952 | | |
| 953 | pat_path |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 954 | | |
| 955 | pat_tuple |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 956 | | |
| 957 | pat_ref |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 958 | | |
| 959 | pat_slice |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 960 | )); |
| 961 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 962 | named!(pat_mac -> Pat, map!(mac, Pat::Mac)); |
| 963 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 964 | named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild)); |
| 965 | |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 966 | named!(pat_box -> Pat, do_parse!( |
| 967 | keyword!("box") >> |
| 968 | pat: pat >> |
| 969 | (Pat::Box(Box::new(pat))) |
| 970 | )); |
| 971 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 972 | named!(pat_ident -> Pat, do_parse!( |
| 973 | mode: option!(keyword!("ref")) >> |
| 974 | mutability: mutability >> |
David Tolnay | 8d4d2fa | 2016-10-25 22:08:07 -0700 | [diff] [blame] | 975 | name: alt!( |
| 976 | ident |
| 977 | | |
| 978 | keyword!("self") => { Into::into } |
| 979 | ) >> |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 980 | not!(peek!(punct!("<"))) >> |
| 981 | not!(peek!(punct!("::"))) >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 982 | subpat: option!(preceded!(punct!("@"), pat)) >> |
| 983 | (Pat::Ident( |
| 984 | if mode.is_some() { |
| 985 | BindingMode::ByRef(mutability) |
| 986 | } else { |
| 987 | BindingMode::ByValue(mutability) |
| 988 | }, |
| 989 | name, |
| 990 | subpat.map(Box::new), |
| 991 | )) |
| 992 | )); |
| 993 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 994 | named!(pat_tuple_struct -> Pat, do_parse!( |
| 995 | path: path >> |
| 996 | tuple: pat_tuple_helper >> |
| 997 | (Pat::TupleStruct(path, tuple.0, tuple.1)) |
| 998 | )); |
| 999 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1000 | named!(pat_struct -> Pat, do_parse!( |
| 1001 | path: path >> |
| 1002 | punct!("{") >> |
| 1003 | fields: separated_list!(punct!(","), field_pat) >> |
| 1004 | more: option!(preceded!( |
| 1005 | cond!(!fields.is_empty(), punct!(",")), |
| 1006 | punct!("..") |
| 1007 | )) >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 1008 | cond!(!fields.is_empty() && more.is_none(), option!(punct!(","))) >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1009 | punct!("}") >> |
| 1010 | (Pat::Struct(path, fields, more.is_some())) |
| 1011 | )); |
| 1012 | |
| 1013 | named!(field_pat -> FieldPat, alt!( |
| 1014 | do_parse!( |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 1015 | ident: wordlike >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1016 | punct!(":") >> |
| 1017 | pat: pat >> |
| 1018 | (FieldPat { |
| 1019 | ident: ident, |
| 1020 | pat: Box::new(pat), |
| 1021 | is_shorthand: false, |
| 1022 | }) |
| 1023 | ) |
| 1024 | | |
| 1025 | do_parse!( |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1026 | boxed: option!(keyword!("box")) >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1027 | mode: option!(keyword!("ref")) >> |
| 1028 | mutability: mutability >> |
| 1029 | ident: ident >> |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1030 | ({ |
| 1031 | let mut pat = Pat::Ident( |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1032 | if mode.is_some() { |
| 1033 | BindingMode::ByRef(mutability) |
| 1034 | } else { |
| 1035 | BindingMode::ByValue(mutability) |
| 1036 | }, |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1037 | ident.clone(), |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1038 | None, |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1039 | ); |
| 1040 | if boxed.is_some() { |
| 1041 | pat = Pat::Box(Box::new(pat)); |
| 1042 | } |
| 1043 | FieldPat { |
| 1044 | ident: ident, |
| 1045 | pat: Box::new(pat), |
| 1046 | is_shorthand: true, |
| 1047 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1048 | }) |
| 1049 | ) |
| 1050 | )); |
| 1051 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1052 | named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path))); |
| 1053 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1054 | named!(pat_tuple -> Pat, map!( |
| 1055 | pat_tuple_helper, |
| 1056 | |(pats, dotdot)| Pat::Tuple(pats, dotdot) |
| 1057 | )); |
| 1058 | |
| 1059 | named!(pat_tuple_helper -> (Vec<Pat>, Option<usize>), do_parse!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1060 | punct!("(") >> |
| 1061 | mut elems: separated_list!(punct!(","), pat) >> |
| 1062 | dotdot: option!(do_parse!( |
| 1063 | cond!(!elems.is_empty(), punct!(",")) >> |
| 1064 | punct!("..") >> |
| 1065 | rest: many0!(preceded!(punct!(","), pat)) >> |
| 1066 | cond!(!rest.is_empty(), option!(punct!(","))) >> |
| 1067 | (rest) |
| 1068 | )) >> |
| 1069 | cond!(!elems.is_empty() && dotdot.is_none(), option!(punct!(","))) >> |
| 1070 | punct!(")") >> |
| 1071 | (match dotdot { |
| 1072 | Some(rest) => { |
| 1073 | let pos = elems.len(); |
| 1074 | elems.extend(rest); |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1075 | (elems, Some(pos)) |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1076 | } |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1077 | None => (elems, None), |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1078 | }) |
| 1079 | )); |
| 1080 | |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1081 | named!(pat_ref -> Pat, do_parse!( |
| 1082 | punct!("&") >> |
| 1083 | mutability: mutability >> |
| 1084 | pat: pat >> |
| 1085 | (Pat::Ref(Box::new(pat), mutability)) |
| 1086 | )); |
| 1087 | |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1088 | named!(pat_lit -> Pat, do_parse!( |
| 1089 | lit: pat_lit_expr >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1090 | (if let ExprKind::Path(_, _) = lit.node { |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1091 | return IResult::Error; // these need to be parsed by pat_path |
| 1092 | } else { |
| 1093 | Pat::Lit(Box::new(lit)) |
| 1094 | }) |
| 1095 | )); |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1096 | |
| 1097 | named!(pat_range -> Pat, do_parse!( |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1098 | lo: pat_lit_expr >> |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1099 | punct!("...") >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1100 | hi: pat_lit_expr >> |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1101 | (Pat::Range(Box::new(lo), Box::new(hi))) |
| 1102 | )); |
| 1103 | |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1104 | named!(pat_lit_expr -> Expr, do_parse!( |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1105 | neg: option!(punct!("-")) >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1106 | v: alt!( |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1107 | lit => { ExprKind::Lit } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1108 | | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1109 | path => { |p| ExprKind::Path(None, p) } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1110 | ) >> |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1111 | (if neg.is_some() { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1112 | ExprKind::Unary(UnOp::Neg, Box::new(v.into())).into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1113 | } else { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1114 | v.into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1115 | }) |
| 1116 | )); |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1117 | |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1118 | named!(pat_slice -> Pat, do_parse!( |
| 1119 | punct!("[") >> |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1120 | mut before: separated_list!(punct!(","), pat) >> |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1121 | after: option!(do_parse!( |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1122 | comma_before_dots: option!(cond_reduce!(!before.is_empty(), punct!(","))) >> |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1123 | punct!("..") >> |
| 1124 | after: many0!(preceded!(punct!(","), pat)) >> |
| 1125 | cond!(!after.is_empty(), option!(punct!(","))) >> |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1126 | (comma_before_dots.is_some(), after) |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1127 | )) >> |
| 1128 | punct!("]") >> |
| 1129 | (match after { |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1130 | None => Pat::Slice(before, None, Vec::new()), |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1131 | Some((true, after)) => { |
| 1132 | if before.is_empty() { |
| 1133 | return IResult::Error; |
| 1134 | } |
| 1135 | Pat::Slice(before, Some(Box::new(Pat::Wild)), after) |
| 1136 | } |
| 1137 | Some((false, after)) => { |
| 1138 | let rest = before.pop().unwrap_or(Pat::Wild); |
| 1139 | Pat::Slice(before, Some(Box::new(rest)), after) |
| 1140 | } |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1141 | }) |
| 1142 | )); |
| 1143 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1144 | named!(capture_by -> CaptureBy, alt!( |
| 1145 | keyword!("move") => { |_| CaptureBy::Value } |
| 1146 | | |
| 1147 | epsilon!() => { |_| CaptureBy::Ref } |
| 1148 | )); |
| 1149 | |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1150 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1153 | #[cfg(feature = "printing")] |
| 1154 | mod printing { |
| 1155 | use super::*; |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1156 | use {FnArg, FunctionRetTy, Mutability, Ty}; |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1157 | use attr::FilterAttrs; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1158 | use quote::{Tokens, ToTokens}; |
| 1159 | |
| 1160 | impl ToTokens for Expr { |
| 1161 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1162 | tokens.append_all(self.attrs.outer()); |
| 1163 | match self.node { |
| 1164 | ExprKind::Box(ref inner) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1165 | tokens.append("box"); |
| 1166 | inner.to_tokens(tokens); |
| 1167 | } |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 1168 | ExprKind::InPlace(ref place, ref value) => { |
| 1169 | tokens.append("in"); |
| 1170 | place.to_tokens(tokens); |
| 1171 | value.to_tokens(tokens); |
| 1172 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1173 | ExprKind::Vec(ref tys) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1174 | tokens.append("["); |
| 1175 | tokens.append_separated(tys, ","); |
| 1176 | tokens.append("]"); |
| 1177 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1178 | ExprKind::Call(ref func, ref args) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1179 | func.to_tokens(tokens); |
| 1180 | tokens.append("("); |
| 1181 | tokens.append_separated(args, ","); |
| 1182 | tokens.append(")"); |
| 1183 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1184 | ExprKind::MethodCall(ref ident, ref ascript, ref args) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1185 | args[0].to_tokens(tokens); |
| 1186 | tokens.append("."); |
| 1187 | ident.to_tokens(tokens); |
David Tolnay | 58f6f67 | 2016-10-19 08:44:25 -0700 | [diff] [blame] | 1188 | if !ascript.is_empty() { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1189 | tokens.append("::"); |
| 1190 | tokens.append("<"); |
| 1191 | tokens.append_separated(ascript, ","); |
| 1192 | tokens.append(">"); |
| 1193 | } |
| 1194 | tokens.append("("); |
| 1195 | tokens.append_separated(&args[1..], ","); |
| 1196 | tokens.append(")"); |
| 1197 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1198 | ExprKind::Tup(ref fields) => { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 1199 | tokens.append("("); |
| 1200 | tokens.append_separated(fields, ","); |
| 1201 | if fields.len() == 1 { |
| 1202 | tokens.append(","); |
| 1203 | } |
| 1204 | tokens.append(")"); |
| 1205 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1206 | ExprKind::Binary(op, ref left, ref right) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1207 | left.to_tokens(tokens); |
| 1208 | op.to_tokens(tokens); |
| 1209 | right.to_tokens(tokens); |
| 1210 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1211 | ExprKind::Unary(op, ref expr) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1212 | op.to_tokens(tokens); |
| 1213 | expr.to_tokens(tokens); |
| 1214 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1215 | ExprKind::Lit(ref lit) => lit.to_tokens(tokens), |
| 1216 | ExprKind::Cast(ref expr, ref ty) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1217 | expr.to_tokens(tokens); |
| 1218 | tokens.append("as"); |
| 1219 | ty.to_tokens(tokens); |
| 1220 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1221 | ExprKind::Type(ref expr, ref ty) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1222 | expr.to_tokens(tokens); |
| 1223 | tokens.append(":"); |
| 1224 | ty.to_tokens(tokens); |
| 1225 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1226 | ExprKind::If(ref cond, ref then_block, ref else_block) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1227 | tokens.append("if"); |
| 1228 | cond.to_tokens(tokens); |
| 1229 | then_block.to_tokens(tokens); |
| 1230 | if let Some(ref else_block) = *else_block { |
| 1231 | tokens.append("else"); |
| 1232 | else_block.to_tokens(tokens); |
| 1233 | } |
| 1234 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1235 | ExprKind::IfLet(ref pat, ref expr, ref then_block, ref else_block) => { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1236 | tokens.append("if"); |
| 1237 | tokens.append("let"); |
| 1238 | pat.to_tokens(tokens); |
| 1239 | tokens.append("="); |
| 1240 | expr.to_tokens(tokens); |
| 1241 | then_block.to_tokens(tokens); |
| 1242 | if let Some(ref else_block) = *else_block { |
| 1243 | tokens.append("else"); |
| 1244 | else_block.to_tokens(tokens); |
| 1245 | } |
| 1246 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1247 | ExprKind::While(ref cond, ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1248 | if let Some(ref label) = *label { |
| 1249 | label.to_tokens(tokens); |
| 1250 | tokens.append(":"); |
| 1251 | } |
| 1252 | tokens.append("while"); |
| 1253 | cond.to_tokens(tokens); |
| 1254 | body.to_tokens(tokens); |
| 1255 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1256 | ExprKind::WhileLet(ref pat, ref expr, ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1257 | if let Some(ref label) = *label { |
| 1258 | label.to_tokens(tokens); |
| 1259 | tokens.append(":"); |
| 1260 | } |
| 1261 | tokens.append("while"); |
| 1262 | tokens.append("let"); |
| 1263 | pat.to_tokens(tokens); |
| 1264 | tokens.append("="); |
| 1265 | expr.to_tokens(tokens); |
| 1266 | body.to_tokens(tokens); |
| 1267 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1268 | ExprKind::ForLoop(ref pat, ref expr, ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1269 | if let Some(ref label) = *label { |
| 1270 | label.to_tokens(tokens); |
| 1271 | tokens.append(":"); |
| 1272 | } |
| 1273 | tokens.append("for"); |
| 1274 | pat.to_tokens(tokens); |
| 1275 | tokens.append("in"); |
| 1276 | expr.to_tokens(tokens); |
| 1277 | body.to_tokens(tokens); |
| 1278 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1279 | ExprKind::Loop(ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1280 | if let Some(ref label) = *label { |
| 1281 | label.to_tokens(tokens); |
| 1282 | tokens.append(":"); |
| 1283 | } |
| 1284 | tokens.append("loop"); |
| 1285 | body.to_tokens(tokens); |
| 1286 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1287 | ExprKind::Match(ref expr, ref arms) => { |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1288 | tokens.append("match"); |
| 1289 | expr.to_tokens(tokens); |
| 1290 | tokens.append("{"); |
David Tolnay | 2165b04 | 2016-10-08 00:04:23 -0700 | [diff] [blame] | 1291 | tokens.append_all(arms); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1292 | tokens.append("}"); |
| 1293 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1294 | ExprKind::Closure(capture, ref decl, ref body) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1295 | capture.to_tokens(tokens); |
| 1296 | tokens.append("|"); |
| 1297 | for (i, input) in decl.inputs.iter().enumerate() { |
| 1298 | if i > 0 { |
| 1299 | tokens.append(","); |
| 1300 | } |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1301 | match *input { |
| 1302 | FnArg::Captured(ref pat, Ty::Infer) => { |
| 1303 | pat.to_tokens(tokens); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1304 | } |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1305 | _ => input.to_tokens(tokens), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | tokens.append("|"); |
| 1309 | match decl.output { |
| 1310 | FunctionRetTy::Default => { |
| 1311 | if body.stmts.len() == 1 { |
| 1312 | if let Stmt::Expr(ref expr) = body.stmts[0] { |
| 1313 | expr.to_tokens(tokens); |
| 1314 | } else { |
| 1315 | body.to_tokens(tokens); |
| 1316 | } |
| 1317 | } else { |
| 1318 | body.to_tokens(tokens); |
| 1319 | } |
| 1320 | } |
| 1321 | FunctionRetTy::Ty(ref ty) => { |
| 1322 | tokens.append("->"); |
| 1323 | ty.to_tokens(tokens); |
| 1324 | body.to_tokens(tokens); |
| 1325 | } |
| 1326 | } |
| 1327 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1328 | ExprKind::Block(rules, ref block) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1329 | rules.to_tokens(tokens); |
| 1330 | block.to_tokens(tokens); |
| 1331 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1332 | ExprKind::Assign(ref var, ref expr) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1333 | var.to_tokens(tokens); |
| 1334 | tokens.append("="); |
| 1335 | expr.to_tokens(tokens); |
| 1336 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1337 | ExprKind::AssignOp(op, ref var, ref expr) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1338 | var.to_tokens(tokens); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 1339 | tokens.append(op.assign_op().unwrap()); |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1340 | expr.to_tokens(tokens); |
| 1341 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1342 | ExprKind::Field(ref expr, ref field) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1343 | expr.to_tokens(tokens); |
| 1344 | tokens.append("."); |
| 1345 | field.to_tokens(tokens); |
| 1346 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1347 | ExprKind::TupField(ref expr, field) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1348 | expr.to_tokens(tokens); |
| 1349 | tokens.append("."); |
| 1350 | tokens.append(&field.to_string()); |
| 1351 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1352 | ExprKind::Index(ref expr, ref index) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1353 | expr.to_tokens(tokens); |
| 1354 | tokens.append("["); |
| 1355 | index.to_tokens(tokens); |
| 1356 | tokens.append("]"); |
| 1357 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1358 | ExprKind::Range(ref from, ref to, limits) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1359 | from.to_tokens(tokens); |
| 1360 | match limits { |
| 1361 | RangeLimits::HalfOpen => tokens.append(".."), |
| 1362 | RangeLimits::Closed => tokens.append("..."), |
| 1363 | } |
| 1364 | to.to_tokens(tokens); |
| 1365 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1366 | ExprKind::Path(None, ref path) => path.to_tokens(tokens), |
| 1367 | ExprKind::Path(Some(ref qself), ref path) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1368 | tokens.append("<"); |
| 1369 | qself.ty.to_tokens(tokens); |
| 1370 | if qself.position > 0 { |
| 1371 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1372 | for (i, segment) in path.segments |
| 1373 | .iter() |
| 1374 | .take(qself.position) |
| 1375 | .enumerate() { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1376 | if i > 0 || path.global { |
| 1377 | tokens.append("::"); |
| 1378 | } |
| 1379 | segment.to_tokens(tokens); |
| 1380 | } |
| 1381 | } |
| 1382 | tokens.append(">"); |
| 1383 | for segment in path.segments.iter().skip(qself.position) { |
| 1384 | tokens.append("::"); |
| 1385 | segment.to_tokens(tokens); |
| 1386 | } |
| 1387 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1388 | ExprKind::AddrOf(mutability, ref expr) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1389 | tokens.append("&"); |
| 1390 | mutability.to_tokens(tokens); |
| 1391 | expr.to_tokens(tokens); |
| 1392 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1393 | ExprKind::Break(ref opt_label) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1394 | tokens.append("break"); |
| 1395 | opt_label.to_tokens(tokens); |
| 1396 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1397 | ExprKind::Continue(ref opt_label) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1398 | tokens.append("continue"); |
| 1399 | opt_label.to_tokens(tokens); |
| 1400 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1401 | ExprKind::Ret(ref opt_expr) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1402 | tokens.append("return"); |
| 1403 | opt_expr.to_tokens(tokens); |
| 1404 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1405 | ExprKind::Mac(ref mac) => mac.to_tokens(tokens), |
| 1406 | ExprKind::Struct(ref path, ref fields, ref base) => { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1407 | path.to_tokens(tokens); |
| 1408 | tokens.append("{"); |
| 1409 | tokens.append_separated(fields, ","); |
| 1410 | if let Some(ref base) = *base { |
| 1411 | if !fields.is_empty() { |
| 1412 | tokens.append(","); |
| 1413 | } |
| 1414 | tokens.append(".."); |
| 1415 | base.to_tokens(tokens); |
| 1416 | } |
| 1417 | tokens.append("}"); |
| 1418 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1419 | ExprKind::Repeat(ref expr, ref times) => { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1420 | tokens.append("["); |
| 1421 | expr.to_tokens(tokens); |
| 1422 | tokens.append(";"); |
| 1423 | times.to_tokens(tokens); |
| 1424 | tokens.append("]"); |
| 1425 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1426 | ExprKind::Paren(ref expr) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1427 | tokens.append("("); |
| 1428 | expr.to_tokens(tokens); |
| 1429 | tokens.append(")"); |
| 1430 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1431 | ExprKind::Try(ref expr) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1432 | expr.to_tokens(tokens); |
| 1433 | tokens.append("?"); |
| 1434 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1438 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1439 | impl ToTokens for FieldValue { |
| 1440 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1441 | self.ident.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 1442 | if !self.is_shorthand { |
| 1443 | tokens.append(":"); |
| 1444 | self.expr.to_tokens(tokens); |
| 1445 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1449 | impl ToTokens for Arm { |
| 1450 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1451 | for attr in &self.attrs { |
| 1452 | attr.to_tokens(tokens); |
| 1453 | } |
| 1454 | tokens.append_separated(&self.pats, "|"); |
| 1455 | if let Some(ref guard) = self.guard { |
| 1456 | tokens.append("if"); |
| 1457 | guard.to_tokens(tokens); |
| 1458 | } |
| 1459 | tokens.append("=>"); |
| 1460 | self.body.to_tokens(tokens); |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1461 | match self.body.node { |
| 1462 | ExprKind::Block(BlockCheckMode::Default, _) => { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1463 | // no comma |
| 1464 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1465 | _ => tokens.append(","), |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | impl ToTokens for Pat { |
| 1471 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1472 | match *self { |
| 1473 | Pat::Wild => tokens.append("_"), |
| 1474 | Pat::Ident(mode, ref ident, ref subpat) => { |
| 1475 | mode.to_tokens(tokens); |
| 1476 | ident.to_tokens(tokens); |
| 1477 | if let Some(ref subpat) = *subpat { |
| 1478 | tokens.append("@"); |
| 1479 | subpat.to_tokens(tokens); |
| 1480 | } |
| 1481 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1482 | Pat::Struct(ref path, ref fields, dots) => { |
| 1483 | path.to_tokens(tokens); |
| 1484 | tokens.append("{"); |
| 1485 | tokens.append_separated(fields, ","); |
| 1486 | if dots { |
| 1487 | if !fields.is_empty() { |
| 1488 | tokens.append(","); |
| 1489 | } |
| 1490 | tokens.append(".."); |
| 1491 | } |
| 1492 | tokens.append("}"); |
| 1493 | } |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1494 | Pat::TupleStruct(ref path, ref pats, dotpos) => { |
| 1495 | path.to_tokens(tokens); |
| 1496 | tokens.append("("); |
| 1497 | match dotpos { |
| 1498 | Some(pos) => { |
| 1499 | if pos > 0 { |
| 1500 | tokens.append_separated(&pats[..pos], ","); |
| 1501 | tokens.append(","); |
| 1502 | } |
| 1503 | tokens.append(".."); |
| 1504 | if pos < pats.len() { |
| 1505 | tokens.append(","); |
| 1506 | tokens.append_separated(&pats[pos..], ","); |
| 1507 | } |
| 1508 | } |
| 1509 | None => tokens.append_separated(pats, ","), |
| 1510 | } |
| 1511 | tokens.append(")"); |
| 1512 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1513 | Pat::Path(None, ref path) => path.to_tokens(tokens), |
| 1514 | Pat::Path(Some(ref qself), ref path) => { |
| 1515 | tokens.append("<"); |
| 1516 | qself.ty.to_tokens(tokens); |
| 1517 | if qself.position > 0 { |
| 1518 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1519 | for (i, segment) in path.segments |
| 1520 | .iter() |
| 1521 | .take(qself.position) |
| 1522 | .enumerate() { |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1523 | if i > 0 || path.global { |
| 1524 | tokens.append("::"); |
| 1525 | } |
| 1526 | segment.to_tokens(tokens); |
| 1527 | } |
| 1528 | } |
| 1529 | tokens.append(">"); |
| 1530 | for segment in path.segments.iter().skip(qself.position) { |
| 1531 | tokens.append("::"); |
| 1532 | segment.to_tokens(tokens); |
| 1533 | } |
| 1534 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1535 | Pat::Tuple(ref pats, dotpos) => { |
| 1536 | tokens.append("("); |
| 1537 | match dotpos { |
| 1538 | Some(pos) => { |
| 1539 | if pos > 0 { |
| 1540 | tokens.append_separated(&pats[..pos], ","); |
| 1541 | tokens.append(","); |
| 1542 | } |
| 1543 | tokens.append(".."); |
| 1544 | if pos < pats.len() { |
| 1545 | tokens.append(","); |
| 1546 | tokens.append_separated(&pats[pos..], ","); |
| 1547 | } |
| 1548 | } |
David Tolnay | 37aa296 | 2016-10-30 10:11:12 -0700 | [diff] [blame] | 1549 | None => { |
| 1550 | tokens.append_separated(pats, ","); |
| 1551 | if pats.len() == 1 { |
| 1552 | tokens.append(","); |
| 1553 | } |
| 1554 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1555 | } |
| 1556 | tokens.append(")"); |
| 1557 | } |
| 1558 | Pat::Box(ref inner) => { |
| 1559 | tokens.append("box"); |
| 1560 | inner.to_tokens(tokens); |
| 1561 | } |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1562 | Pat::Ref(ref target, mutability) => { |
| 1563 | tokens.append("&"); |
| 1564 | mutability.to_tokens(tokens); |
| 1565 | target.to_tokens(tokens); |
| 1566 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1567 | Pat::Lit(ref lit) => lit.to_tokens(tokens), |
| 1568 | Pat::Range(ref lo, ref hi) => { |
| 1569 | lo.to_tokens(tokens); |
| 1570 | tokens.append("..."); |
| 1571 | hi.to_tokens(tokens); |
| 1572 | } |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1573 | Pat::Slice(ref before, ref rest, ref after) => { |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1574 | tokens.append("["); |
| 1575 | tokens.append_separated(before, ","); |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1576 | if let Some(ref rest) = *rest { |
| 1577 | if !before.is_empty() { |
| 1578 | tokens.append(","); |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1579 | } |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1580 | if **rest != Pat::Wild { |
| 1581 | rest.to_tokens(tokens); |
| 1582 | } |
| 1583 | tokens.append(".."); |
| 1584 | if !after.is_empty() { |
| 1585 | tokens.append(","); |
| 1586 | } |
| 1587 | tokens.append_separated(after, ","); |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1588 | } |
| 1589 | tokens.append("]"); |
| 1590 | } |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame] | 1591 | Pat::Mac(ref mac) => mac.to_tokens(tokens), |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1596 | impl ToTokens for FieldPat { |
| 1597 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1598 | if !self.is_shorthand { |
| 1599 | self.ident.to_tokens(tokens); |
| 1600 | tokens.append(":"); |
| 1601 | } |
| 1602 | self.pat.to_tokens(tokens); |
| 1603 | } |
| 1604 | } |
| 1605 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1606 | impl ToTokens for BindingMode { |
| 1607 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1608 | match *self { |
| 1609 | BindingMode::ByRef(Mutability::Immutable) => { |
| 1610 | tokens.append("ref"); |
| 1611 | } |
| 1612 | BindingMode::ByRef(Mutability::Mutable) => { |
| 1613 | tokens.append("ref"); |
| 1614 | tokens.append("mut"); |
| 1615 | } |
| 1616 | BindingMode::ByValue(Mutability::Immutable) => {} |
| 1617 | BindingMode::ByValue(Mutability::Mutable) => { |
| 1618 | tokens.append("mut"); |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1623 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1624 | impl ToTokens for CaptureBy { |
| 1625 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1626 | match *self { |
| 1627 | CaptureBy::Value => tokens.append("move"), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1628 | CaptureBy::Ref => { |
| 1629 | // nothing |
| 1630 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1635 | impl ToTokens for Block { |
| 1636 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1637 | tokens.append("{"); |
David Tolnay | 3b9783a | 2016-10-29 22:37:09 -0700 | [diff] [blame] | 1638 | tokens.append_all(&self.stmts); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1639 | tokens.append("}"); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | impl ToTokens for BlockCheckMode { |
| 1644 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1645 | match *self { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1646 | BlockCheckMode::Default => { |
| 1647 | // nothing |
| 1648 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1649 | BlockCheckMode::Unsafe => tokens.append("unsafe"), |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | impl ToTokens for Stmt { |
| 1655 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1656 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1657 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1658 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 1659 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
| 1660 | Stmt::Semi(ref expr) => { |
| 1661 | expr.to_tokens(tokens); |
| 1662 | tokens.append(";"); |
| 1663 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1664 | Stmt::Mac(ref mac) => { |
| 1665 | let (ref mac, style, ref attrs) = **mac; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1666 | tokens.append_all(attrs.outer()); |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1667 | mac.to_tokens(tokens); |
| 1668 | match style { |
| 1669 | MacStmtStyle::Semicolon => tokens.append(";"), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1670 | MacStmtStyle::Braces | MacStmtStyle::NoBraces => { |
| 1671 | // no semicolon |
| 1672 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1678 | |
| 1679 | impl ToTokens for Local { |
| 1680 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4e3158d | 2016-10-30 00:30:01 -0700 | [diff] [blame] | 1681 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1682 | tokens.append("let"); |
| 1683 | self.pat.to_tokens(tokens); |
| 1684 | if let Some(ref ty) = self.ty { |
| 1685 | tokens.append(":"); |
| 1686 | ty.to_tokens(tokens); |
| 1687 | } |
| 1688 | if let Some(ref init) = self.init { |
| 1689 | tokens.append("="); |
| 1690 | init.to_tokens(tokens); |
| 1691 | } |
| 1692 | tokens.append(";"); |
| 1693 | } |
| 1694 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1695 | } |