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