blob: 6ec7253eb4062a6a7d82122b39532d7236d93a49 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayf4bbbd92016-09-23 14:41:55 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_struct! {
5 /// An expression.
6 pub struct Expr {
7 /// Type of the expression.
8 pub node: ExprKind,
Clar Charrd22b5702017-03-10 15:24:56 -05009
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// Attributes tagged on the expression.
11 pub attrs: Vec<Attribute>,
12 }
David Tolnay7184b132016-10-30 10:06:37 -070013}
14
15impl From<ExprKind> for Expr {
16 fn from(node: ExprKind) -> Expr {
17 Expr {
18 node: node,
19 attrs: Vec::new(),
20 }
21 }
22}
23
Alex Crichton62a0a592017-05-22 13:58:53 -070024ast_enum_of_structs! {
25 pub enum ExprKind {
26 /// A `box x` expression.
27 pub Box(ExprBox {
28 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070029 pub box_token: tokens::Box,
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }),
Clar Charrd22b5702017-03-10 15:24:56 -050031
Alex Crichton62a0a592017-05-22 13:58:53 -070032 /// E.g. 'place <- val'.
33 pub InPlace(ExprInPlace {
34 pub place: Box<Expr>,
35 pub value: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070036 pub in_token: tokens::In,
Alex Crichton62a0a592017-05-22 13:58:53 -070037 }),
Clar Charrd22b5702017-03-10 15:24:56 -050038
Alex Crichton62a0a592017-05-22 13:58:53 -070039 /// An array, e.g. `[a, b, c, d]`.
40 pub Array(ExprArray {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070041 pub exprs: Delimited<Expr, tokens::Comma>,
42 pub bracket_token: tokens::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -070043 }),
Clar Charrd22b5702017-03-10 15:24:56 -050044
Alex Crichton62a0a592017-05-22 13:58:53 -070045 /// A function call.
46 pub Call(ExprCall {
47 pub func: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070048 pub args: Delimited<Expr, tokens::Comma>,
49 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -070050 }),
Clar Charrd22b5702017-03-10 15:24:56 -050051
Alex Crichton62a0a592017-05-22 13:58:53 -070052 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
53 ///
54 /// The `Ident` is the identifier for the method name.
55 /// The vector of `Ty`s are the ascripted type parameters for the method
56 /// (within the angle brackets).
57 ///
Alex Crichton62a0a592017-05-22 13:58:53 -070058 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
59 /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
60 pub MethodCall(ExprMethodCall {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070061 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070062 pub method: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070063 pub typarams: Delimited<Ty, tokens::Comma>,
64 pub args: Delimited<Expr, tokens::Comma>,
65 pub paren_token: tokens::Paren,
66 pub dot_token: tokens::Dot,
67 pub lt_token: Option<tokens::Lt>,
68 pub colon2_token: Option<tokens::Colon2>,
69 pub gt_token: Option<tokens::Gt>,
Alex Crichton62a0a592017-05-22 13:58:53 -070070 }),
Clar Charrd22b5702017-03-10 15:24:56 -050071
Alex Crichton62a0a592017-05-22 13:58:53 -070072 /// A tuple, e.g. `(a, b, c, d)`.
73 pub Tup(ExprTup {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070074 pub args: Delimited<Expr, tokens::Comma>,
75 pub paren_token: tokens::Paren,
76 pub lone_comma: Option<tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 }),
Clar Charrd22b5702017-03-10 15:24:56 -050078
Alex Crichton62a0a592017-05-22 13:58:53 -070079 /// A binary operation, e.g. `a + b`, `a * b`.
80 pub Binary(ExprBinary {
81 pub op: BinOp,
82 pub left: Box<Expr>,
83 pub right: Box<Expr>,
84 }),
Clar Charrd22b5702017-03-10 15:24:56 -050085
Alex Crichton62a0a592017-05-22 13:58:53 -070086 /// A unary operation, e.g. `!x`, `*x`.
87 pub Unary(ExprUnary {
88 pub op: UnOp,
89 pub expr: Box<Expr>,
90 }),
Clar Charrd22b5702017-03-10 15:24:56 -050091
Alex Crichton62a0a592017-05-22 13:58:53 -070092 /// A literal, e.g. `1`, `"foo"`.
93 pub Lit(Lit),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A cast, e.g. `foo as f64`.
96 pub Cast(ExprCast {
97 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070098 pub as_token: tokens::As,
Alex Crichton62a0a592017-05-22 13:58:53 -070099 pub ty: Box<Ty>,
100 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500101
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 /// A type ascription, e.g. `foo: f64`.
103 pub Type(ExprType {
104 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700105 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub ty: Box<Ty>,
107 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500108
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 /// An `if` block, with an optional else block
110 ///
111 /// E.g., `if expr { block } else { expr }`
112 pub If(ExprIf {
113 pub cond: Box<Expr>,
114 pub if_true: Block,
115 pub if_false: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700116 pub if_token: tokens::If,
117 pub else_token: Option<tokens::Else>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700118 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500119
Alex Crichton62a0a592017-05-22 13:58:53 -0700120 /// An `if let` expression with an optional else block
121 ///
122 /// E.g., `if let pat = expr { block } else { expr }`
123 ///
124 /// This is desugared to a `match` expression.
125 pub IfLet(ExprIfLet {
126 pub pat: Box<Pat>,
127 pub expr: Box<Expr>,
128 pub if_true: Block,
129 pub if_false: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700130 pub if_token: tokens::If,
131 pub let_token: tokens::Let,
132 pub eq_token: tokens::Eq,
133 pub else_token: Option<tokens::Else>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500135
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 /// A while loop, with an optional label
137 ///
138 /// E.g., `'label: while expr { block }`
139 pub While(ExprWhile {
140 pub cond: Box<Expr>,
141 pub body: Block,
142 pub label: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700143 pub colon_token: Option<tokens::Colon>,
144 pub while_token: tokens::While,
Alex Crichton62a0a592017-05-22 13:58:53 -0700145 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500146
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 /// A while-let loop, with an optional label.
148 ///
149 /// E.g., `'label: while let pat = expr { block }`
150 ///
151 /// This is desugared to a combination of `loop` and `match` expressions.
152 pub WhileLet(ExprWhileLet {
153 pub pat: Box<Pat>,
154 pub expr: Box<Expr>,
155 pub body: Block,
156 pub label: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700157 pub colon_token: Option<tokens::Colon>,
158 pub while_token: tokens::While,
159 pub let_token: tokens::Let,
160 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500162
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 /// A for loop, with an optional label.
164 ///
165 /// E.g., `'label: for pat in expr { block }`
166 ///
167 /// This is desugared to a combination of `loop` and `match` expressions.
168 pub ForLoop(ExprForLoop {
169 pub pat: Box<Pat>,
170 pub expr: Box<Expr>,
171 pub body: Block,
172 pub label: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700173 pub for_token: tokens::For,
174 pub colon_token: Option<tokens::Colon>,
175 pub in_token: tokens::In,
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500177
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 /// Conditionless loop with an optional label.
179 ///
180 /// E.g. `'label: loop { block }`
181 pub Loop(ExprLoop {
182 pub body: Block,
183 pub label: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700184 pub loop_token: tokens::Loop,
185 pub colon_token: Option<tokens::Colon>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500187
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 /// A `match` block.
189 pub Match(ExprMatch {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700190 pub match_token: tokens::Match,
191 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 pub expr: Box<Expr>,
193 pub arms: Vec<Arm>,
194 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500195
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 /// A closure (for example, `move |a, b, c| a + b + c`)
197 pub Closure(ExprClosure {
198 pub capture: CaptureBy,
199 pub decl: Box<FnDecl>,
200 pub body: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700201 pub or1_token: tokens::Or,
202 pub or2_token: tokens::Or,
Alex Crichton62a0a592017-05-22 13:58:53 -0700203 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500204
Alex Crichton62a0a592017-05-22 13:58:53 -0700205 /// A block (`{ ... }` or `unsafe { ... }`)
206 pub Block(ExprBlock {
207 pub unsafety: Unsafety,
208 pub block: Block,
209 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700210
Alex Crichton62a0a592017-05-22 13:58:53 -0700211 /// An assignment (`a = foo()`)
212 pub Assign(ExprAssign {
213 pub left: Box<Expr>,
214 pub right: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700215 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700216 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500217
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 /// An assignment with an operator
219 ///
220 /// For example, `a += 1`.
221 pub AssignOp(ExprAssignOp {
222 pub op: BinOp,
223 pub left: Box<Expr>,
224 pub right: Box<Expr>,
225 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500226
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 /// Access of a named struct field (`obj.foo`)
228 pub Field(ExprField {
229 pub expr: Box<Expr>,
230 pub field: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700231 pub dot_token: tokens::Dot,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500233
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 /// Access of an unnamed field of a struct or tuple-struct
235 ///
236 /// For example, `foo.0`.
237 pub TupField(ExprTupField {
238 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700239 pub field: Lit,
240 pub dot_token: tokens::Dot,
Alex Crichton62a0a592017-05-22 13:58:53 -0700241 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500242
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 /// An indexing operation (`foo[2]`)
244 pub Index(ExprIndex {
245 pub expr: Box<Expr>,
246 pub index: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700247 pub bracket_token: tokens::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500249
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 /// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`)
251 pub Range(ExprRange {
252 pub from: Option<Box<Expr>>,
253 pub to: Option<Box<Expr>>,
254 pub limits: RangeLimits,
255 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700256
Alex Crichton62a0a592017-05-22 13:58:53 -0700257 /// Variable reference, possibly containing `::` and/or type
258 /// parameters, e.g. foo::bar::<baz>.
259 ///
260 /// Optionally "qualified",
261 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
262 pub Path(ExprPath {
263 pub qself: Option<QSelf>,
264 pub path: Path,
265 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700266
Alex Crichton62a0a592017-05-22 13:58:53 -0700267 /// A referencing operation (`&a` or `&mut a`)
268 pub AddrOf(ExprAddrOf {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700269 pub and_token: tokens::And,
Alex Crichton62a0a592017-05-22 13:58:53 -0700270 pub mutbl: Mutability,
271 pub expr: Box<Expr>,
272 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500273
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 /// A `break`, with an optional label to break, and an optional expression
275 pub Break(ExprBreak {
276 pub label: Option<Ident>,
277 pub expr: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700278 pub break_token: tokens::Break,
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500280
Alex Crichton62a0a592017-05-22 13:58:53 -0700281 /// A `continue`, with an optional label
282 pub Continue(ExprContinue {
283 pub label: Option<Ident>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700284 pub continue_token: tokens::Continue,
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500286
Alex Crichton62a0a592017-05-22 13:58:53 -0700287 /// A `return`, with an optional value to be returned
288 pub Ret(ExprRet {
289 pub expr: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700290 pub return_token: tokens::Return,
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700292
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 /// A macro invocation; pre-expansion
294 pub Mac(Mac),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700295
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 /// A struct literal expression.
297 ///
298 /// For example, `Foo {x: 1, y: 2}`, or
299 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
300 pub Struct(ExprStruct {
301 pub path: Path,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700302 pub fields: Delimited<FieldValue, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 pub rest: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700304 pub dot2_token: Option<tokens::Dot2>,
305 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700307
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 /// An array literal constructed from one repeated element.
309 ///
310 /// For example, `[1; 5]`. The first expression is the element
311 /// to be repeated; the second is the number of times to repeat it.
312 pub Repeat(ExprRepeat {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700313 pub bracket_token: tokens::Bracket,
314 pub semi_token: tokens::Semi,
Alex Crichton62a0a592017-05-22 13:58:53 -0700315 pub expr: Box<Expr>,
316 pub amt: Box<Expr>,
317 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700318
Alex Crichton62a0a592017-05-22 13:58:53 -0700319 /// No-op: used solely so we can pretty-print faithfully
320 pub Paren(ExprParen {
321 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700322 pub paren_token: tokens::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700323 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700324
Alex Crichton62a0a592017-05-22 13:58:53 -0700325 /// `expr?`
326 pub Try(ExprTry {
327 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700328 pub question_token: tokens::Question,
Alex Crichton62a0a592017-05-22 13:58:53 -0700329 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700330
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 /// A catch expression.
332 ///
333 /// E.g. `do catch { block }`
334 pub Catch(ExprCatch {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700335 pub do_token: tokens::Do,
336 pub catch_token: tokens::Catch,
Alex Crichton62a0a592017-05-22 13:58:53 -0700337 pub block: Block,
338 }),
339 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700340}
341
Alex Crichton62a0a592017-05-22 13:58:53 -0700342ast_struct! {
343 /// A field-value pair in a struct literal.
344 pub struct FieldValue {
345 /// Name of the field.
346 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -0500347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 /// Value of the field.
349 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500350
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 /// Whether this is a shorthand field, e.g. `Struct { x }`
352 /// instead of `Struct { x: x }`.
353 pub is_shorthand: bool,
Clar Charrd22b5702017-03-10 15:24:56 -0500354
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 /// Attributes tagged on the field.
356 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700357
358 pub colon_token: Option<tokens::Colon>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 }
David Tolnay055a7042016-10-02 19:23:54 -0700360}
361
Alex Crichton62a0a592017-05-22 13:58:53 -0700362ast_struct! {
363 /// A Block (`{ .. }`).
364 ///
365 /// E.g. `{ .. }` as in `fn foo() { .. }`
366 pub struct Block {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700367 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700368 /// Statements in a block
369 pub stmts: Vec<Stmt>,
370 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700371}
372
Alex Crichton62a0a592017-05-22 13:58:53 -0700373ast_enum! {
374 /// A statement, usually ending in a semicolon.
375 pub enum Stmt {
376 /// A local (let) binding.
377 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700378
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 /// An item definition.
380 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700381
Alex Crichton62a0a592017-05-22 13:58:53 -0700382 /// Expr without trailing semicolon.
383 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700384
Alex Crichton62a0a592017-05-22 13:58:53 -0700385 /// Expression with trailing semicolon;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700386 Semi(Box<Expr>, tokens::Semi),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700387
Alex Crichton62a0a592017-05-22 13:58:53 -0700388 /// Macro invocation.
389 Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>),
390 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700391}
392
Alex Crichton62a0a592017-05-22 13:58:53 -0700393ast_enum! {
394 /// How a macro was invoked.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700395 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700396 pub enum MacStmtStyle {
397 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
398 /// `foo!(...);`, `foo![...];`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700399 Semicolon(tokens::Semi),
Clar Charrd22b5702017-03-10 15:24:56 -0500400
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 /// The macro statement had braces; e.g. foo! { ... }
402 Braces,
Clar Charrd22b5702017-03-10 15:24:56 -0500403
Alex Crichton62a0a592017-05-22 13:58:53 -0700404 /// The macro statement had parentheses or brackets and no semicolon; e.g.
405 /// `foo!(...)`. All of these will end up being converted into macro
406 /// expressions.
407 NoBraces,
408 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700409}
410
Alex Crichton62a0a592017-05-22 13:58:53 -0700411ast_struct! {
412 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
413 pub struct Local {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700414 pub let_token: tokens::Let,
415 pub colon_token: Option<tokens::Colon>,
416 pub eq_token: Option<tokens::Eq>,
417 pub semi_token: tokens::Semi,
418
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub pat: Box<Pat>,
420 pub ty: Option<Box<Ty>>,
Clar Charrd22b5702017-03-10 15:24:56 -0500421
Alex Crichton62a0a592017-05-22 13:58:53 -0700422 /// Initializer expression to set the value, if any
423 pub init: Option<Box<Expr>>,
424 pub attrs: Vec<Attribute>,
425 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700426}
427
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700428ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 // Clippy false positive
430 // https://github.com/Manishearth/rust-clippy/issues/1241
431 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
432 pub enum Pat {
433 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700434 pub Wild(PatWild {
435 pub underscore_token: tokens::Underscore,
436 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700437
Alex Crichton62a0a592017-05-22 13:58:53 -0700438 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
439 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
440 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
441 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700442 pub Ident(PatIdent {
443 pub mode: BindingMode,
444 pub ident: Ident,
445 pub subpat: Option<Box<Pat>>,
446 pub at_token: Option<tokens::At>,
447 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700448
Alex Crichton62a0a592017-05-22 13:58:53 -0700449 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
450 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700451 pub Struct(PatStruct {
452 pub path: Path,
453 pub fields: Delimited<FieldPat, tokens::Comma>,
454 pub brace_token: tokens::Brace,
455 pub dot2_token: Option<tokens::Dot2>,
456 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700457
Alex Crichton62a0a592017-05-22 13:58:53 -0700458 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
459 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
460 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700461 pub TupleStruct(PatTupleStruct {
462 pub path: Path,
463 pub pat: PatTuple,
464 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700465
Alex Crichton62a0a592017-05-22 13:58:53 -0700466 /// A possibly qualified path pattern.
467 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
468 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
469 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700470 pub Path(PatPath {
471 pub qself: Option<QSelf>,
472 pub path: Path,
473 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700474
Alex Crichton62a0a592017-05-22 13:58:53 -0700475 /// A tuple pattern `(a, b)`.
476 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
477 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700478 pub Tuple(PatTuple {
479 pub pats: Delimited<Pat, tokens::Comma>,
480 pub dots_pos: Option<usize>,
481 pub paren_token: tokens::Paren,
482 pub dot2_token: Option<tokens::Dot2>,
483 pub comma_token: Option<tokens::Comma>,
484 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700485 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700486 pub Box(PatBox {
487 pub pat: Box<Pat>,
488 pub box_token: tokens::Box,
489 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700490 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700491 pub Ref(PatRef {
492 pub pat: Box<Pat>,
493 pub mutbl: Mutability,
494 pub and_token: tokens::And,
495 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700496 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700497 pub Lit(PatLit {
498 pub expr: Box<Expr>,
499 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 /// A range pattern, e.g. `1...2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700501 pub Range(PatRange {
502 pub lo: Box<Expr>,
503 pub hi: Box<Expr>,
504 pub limits: RangeLimits,
505 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 /// `[a, b, ..i, y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700507 pub Slice(PatSlice {
508 pub front: Delimited<Pat, tokens::Comma>,
509 pub middle: Option<Box<Pat>>,
510 pub back: Delimited<Pat, tokens::Comma>,
511 pub dot2_token: Option<tokens::Dot2>,
512 pub comma_token: Option<tokens::Comma>,
513 pub bracket_token: tokens::Bracket,
514 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 /// A macro pattern; pre-expansion
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700516 pub Mac(Mac),
Alex Crichton62a0a592017-05-22 13:58:53 -0700517 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700518}
519
Alex Crichton62a0a592017-05-22 13:58:53 -0700520ast_struct! {
521 /// An arm of a 'match'.
522 ///
523 /// E.g. `0...10 => { println!("match!") }` as in
524 ///
525 /// ```rust,ignore
526 /// match n {
527 /// 0...10 => { println!("match!") },
528 /// // ..
529 /// }
530 /// ```
531 pub struct Arm {
532 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700533 pub pats: Delimited<Pat, tokens::Or>,
534 pub if_token: Option<tokens::If>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700535 pub guard: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536 pub rocket_token: tokens::Rocket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 pub body: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700538 pub comma: Option<tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700539 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700540}
541
Alex Crichton62a0a592017-05-22 13:58:53 -0700542ast_enum! {
543 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700544 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700545 pub enum CaptureBy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700546 Value(tokens::Move),
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 Ref,
548 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700549}
550
Alex Crichton62a0a592017-05-22 13:58:53 -0700551ast_enum! {
552 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700553 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700554 pub enum RangeLimits {
555 /// Inclusive at the beginning, exclusive at the end
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 HalfOpen(tokens::Dot2),
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 /// Inclusive at the beginning and end
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700558 Closed(tokens::Dot3),
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700560}
561
Alex Crichton62a0a592017-05-22 13:58:53 -0700562ast_struct! {
563 /// A single field in a struct pattern
564 ///
565 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
566 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
567 /// except `is_shorthand` is true
568 pub struct FieldPat {
569 /// The identifier for the field
570 pub ident: Ident,
571 /// The pattern the field is destructured to
572 pub pat: Box<Pat>,
573 pub is_shorthand: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700574 pub colon_token: Option<tokens::Colon>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 pub attrs: Vec<Attribute>,
576 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700577}
578
Alex Crichton62a0a592017-05-22 13:58:53 -0700579ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700580 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700581 pub enum BindingMode {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700582 ByRef(tokens::Ref, Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 ByValue(Mutability),
584 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700585}
586
David Tolnayb9c8e322016-09-23 20:48:37 -0700587#[cfg(feature = "parsing")]
588pub mod parsing {
589 use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700590 use {BinOp, FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Mac,
Alex Crichton62a0a592017-05-22 13:58:53 -0700591 TokenTree, Ty, UnOp, Unsafety, ArgCaptured, TyInfer};
David Tolnayb4ad3b52016-10-01 21:58:13 -0700592 use attr::parsing::outer_attr;
Gregory Katz1b69f682016-09-27 21:06:09 -0400593 use generics::parsing::lifetime;
David Tolnay39039442016-10-30 11:25:21 -0700594 use ident::parsing::{ident, wordlike};
David Tolnay191e0582016-10-02 18:31:09 -0700595 use item::parsing::item;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700596 use lit::parsing::lit;
597 use mac::parsing::{mac, token_stream};
David Tolnay5fe14fc2017-01-27 16:22:08 -0800598 use synom::IResult::{self, Error};
David Tolnay438c9052016-10-07 23:24:48 -0700599 use op::parsing::{assign_op, binop, unop};
David Tolnay7b035912016-12-21 22:42:07 -0500600 use ty::parsing::{mutability, path, qpath, ty, unsafety};
Michael Layzell416724e2017-05-24 21:12:34 -0400601 use synom::{self, IResult};
David Tolnayb9c8e322016-09-23 20:48:37 -0700602
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603 use proc_macro2::{self, TokenKind, Delimiter};
604
David Tolnayaf2557e2016-10-24 11:52:21 -0700605 // Struct literals are ambiguous in certain positions
606 // https://github.com/rust-lang/rfcs/pull/92
607 macro_rules! named_ambiguous_expr {
608 ($name:ident -> $o:ty, $allow_struct:ident, $submac:ident!( $($args:tt)* )) => {
Michael Layzell416724e2017-05-24 21:12:34 -0400609 fn $name(i: &[$crate::synom::TokenTree], $allow_struct: bool)
610 -> $crate::synom::IResult<&[$crate::synom::TokenTree], $o> {
David Tolnayaf2557e2016-10-24 11:52:21 -0700611 $submac!(i, $($args)*)
612 }
613 };
614 }
615
616 macro_rules! ambiguous_expr {
617 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700618 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700619 };
620 }
621
622 named!(pub expr -> Expr, ambiguous_expr!(true));
623
624 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
625
David Tolnay02a8d472017-02-19 12:59:44 -0800626 #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
Michael Layzell416724e2017-05-24 21:12:34 -0400627 fn ambiguous_expr(i: &[synom::TokenTree],
628 allow_struct: bool,
629 allow_block: bool)
630 -> IResult<&[synom::TokenTree], Expr> {
David Tolnay54e854d2016-10-24 12:03:30 -0700631 do_parse!(
632 i,
633 mut e: alt!(
634 expr_lit // must be before expr_struct
635 |
636 cond_reduce!(allow_struct, expr_struct) // must be before expr_path
637 |
638 expr_paren // must be before expr_tup
639 |
640 expr_mac // must be before expr_path
641 |
David Tolnay5d55ef72016-12-21 20:20:04 -0500642 call!(expr_break, allow_struct) // must be before expr_path
David Tolnay54e854d2016-10-24 12:03:30 -0700643 |
644 expr_continue // must be before expr_path
645 |
646 call!(expr_ret, allow_struct) // must be before expr_path
647 |
648 call!(expr_box, allow_struct)
649 |
David Tolnay6696c3e2016-10-30 11:45:10 -0700650 expr_in_place
651 |
David Tolnay9c7ab132017-01-23 00:01:22 -0800652 expr_array
David Tolnay54e854d2016-10-24 12:03:30 -0700653 |
654 expr_tup
655 |
656 call!(expr_unary, allow_struct)
657 |
658 expr_if
659 |
660 expr_while
661 |
662 expr_for_loop
663 |
664 expr_loop
665 |
666 expr_match
667 |
Arnavion02ef13f2017-04-25 00:54:31 -0700668 expr_catch
669 |
David Tolnay54e854d2016-10-24 12:03:30 -0700670 call!(expr_closure, allow_struct)
671 |
672 cond_reduce!(allow_block, expr_block)
673 |
674 call!(expr_range, allow_struct)
675 |
676 expr_path
677 |
678 call!(expr_addr_of, allow_struct)
679 |
680 expr_repeat
681 ) >>
682 many0!(alt!(
683 tap!(args: and_call => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700684 let (args, paren) = args;
Alex Crichton62a0a592017-05-22 13:58:53 -0700685 e = ExprCall {
686 func: Box::new(e.into()),
687 args: args,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700688 paren_token: paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700690 })
691 |
692 tap!(more: and_method_call => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700693 let mut call = more;
694 call.expr = Box::new(e.into());
695 e = call.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700696 })
697 |
698 tap!(more: call!(and_binary, allow_struct) => {
699 let (op, other) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 e = ExprBinary {
701 op: op,
702 left: Box::new(e.into()),
703 right: Box::new(other),
704 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700705 })
706 |
707 tap!(ty: and_cast => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700708 let (ty, token) = ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 e = ExprCast {
710 expr: Box::new(e.into()),
711 ty: Box::new(ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700712 as_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700713 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700714 })
715 |
716 tap!(ty: and_ascription => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700717 let (ty, token) = ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 e = ExprType {
719 expr: Box::new(e.into()),
720 ty: Box::new(ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700721 colon_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700722 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700723 })
724 |
725 tap!(v: call!(and_assign, allow_struct) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700726 let (v, token) = v;
Alex Crichton62a0a592017-05-22 13:58:53 -0700727 e = ExprAssign {
728 left: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700729 eq_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700730 right: Box::new(v),
731 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700732 })
733 |
734 tap!(more: call!(and_assign_op, allow_struct) => {
735 let (op, v) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700736 e = ExprAssignOp {
737 op: op,
738 left: Box::new(e.into()),
739 right: Box::new(v),
740 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700741 })
742 |
743 tap!(field: and_field => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700744 let (field, token) = field;
Alex Crichton62a0a592017-05-22 13:58:53 -0700745 e = ExprField {
746 expr: Box::new(e.into()),
747 field: field,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700748 dot_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700750 })
751 |
752 tap!(field: and_tup_field => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700753 let (field, token) = field;
Alex Crichton62a0a592017-05-22 13:58:53 -0700754 e = ExprTupField {
755 expr: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700756 field: field,
757 dot_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700758 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700759 })
760 |
761 tap!(i: and_index => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700762 let (i, token) = i;
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 e = ExprIndex {
764 expr: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700765 bracket_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700766 index: Box::new(i),
767 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700768 })
769 |
770 tap!(more: call!(and_range, allow_struct) => {
771 let (limits, hi) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700772 e = ExprRange {
773 from: Some(Box::new(e.into())),
774 to: hi.map(Box::new),
775 limits: limits,
776 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700777 })
778 |
779 tap!(_try: punct!("?") => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700780 e = ExprTry {
781 expr: Box::new(e.into()),
782 question_token: tokens::Question::default(),
783 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700784 })
785 )) >>
David Tolnay7184b132016-10-30 10:06:37 -0700786 (e.into())
David Tolnay54e854d2016-10-24 12:03:30 -0700787 )
788 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700789
David Tolnay7184b132016-10-30 10:06:37 -0700790 named!(expr_mac -> ExprKind, map!(mac, ExprKind::Mac));
David Tolnay84aa0752016-10-02 23:01:13 -0700791
David Tolnay7184b132016-10-30 10:06:37 -0700792 named!(expr_paren -> ExprKind, do_parse!(
David Tolnay89e05672016-10-02 14:39:42 -0700793 punct!("(") >>
794 e: expr >>
795 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700796 (ExprParen {
797 expr: Box::new(e),
798 paren_token: tokens::Paren::default(),
799 }.into())
David Tolnay89e05672016-10-02 14:39:42 -0700800 ));
801
David Tolnay7184b132016-10-30 10:06:37 -0700802 named_ambiguous_expr!(expr_box -> ExprKind, allow_struct, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700803 keyword!("box") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700804 inner: ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805 (ExprBox {
806 expr: Box::new(inner),
807 box_token: tokens::Box::default(),
808 }.into())
David Tolnayb9c8e322016-09-23 20:48:37 -0700809 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700810
David Tolnay6696c3e2016-10-30 11:45:10 -0700811 named!(expr_in_place -> ExprKind, do_parse!(
812 keyword!("in") >>
813 place: expr_no_struct >>
814 punct!("{") >>
815 value: within_block >>
816 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700817 (ExprInPlace {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700818 in_token: tokens::In::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700819 place: Box::new(place),
820 value: Box::new(Expr {
821 node: ExprBlock {
822 unsafety: Unsafety::Normal,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700823 block: Block {
824 stmts: value,
825 brace_token: tokens::Brace::default(),
826 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700827 }.into(),
828 attrs: Vec::new(),
829 }),
830 }.into())
David Tolnay6696c3e2016-10-30 11:45:10 -0700831 ));
832
David Tolnay9c7ab132017-01-23 00:01:22 -0800833 named!(expr_array -> ExprKind, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700834 punct!("[") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700835 elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
836 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700837 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700838 (ExprArray {
839 exprs: elems,
840 bracket_token: tokens::Bracket::default(),
841 }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700842 ));
843
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700844 named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700845 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700846 args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
847 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700848 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700849 (args, tokens::Paren::default())
David Tolnayfa0edf22016-09-23 22:58:24 -0700850 ));
851
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700852 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700853 punct!(".") >>
854 method: ident >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700855 typarams: option!(do_parse!(
856 punct!("::") >>
857 punct!("<") >>
858 tys: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
859 ty) >>
860 punct!(">") >>
861 (tys)
David Tolnayfa0edf22016-09-23 22:58:24 -0700862 )) >>
863 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700864 args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
865 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700866 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700867 (ExprMethodCall {
868 // this expr will get overwritten after being returned
869 expr: Box::new(ExprKind::Lit(Lit {
870 span: Span::default(),
871 value: LitKind::Bool(false),
872 }).into()),
873
874 method: method,
875 args: args,
876 paren_token: tokens::Paren::default(),
877 dot_token: tokens::Dot::default(),
878 lt_token: typarams.as_ref().map(|_| tokens::Lt::default()),
879 gt_token: typarams.as_ref().map(|_| tokens::Gt::default()),
880 colon2_token: typarams.as_ref().map(|_| tokens::Colon2::default()),
881 typarams: typarams.unwrap_or_default(),
882 })
David Tolnayfa0edf22016-09-23 22:58:24 -0700883 ));
884
David Tolnay7184b132016-10-30 10:06:37 -0700885 named!(expr_tup -> ExprKind, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700886 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700887 elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
888 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700889 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700890 (ExprTup {
891 args: elems,
892 paren_token: tokens::Paren::default(),
893 lone_comma: None, // TODO: parse this
894 }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700895 ));
896
David Tolnayaf2557e2016-10-24 11:52:21 -0700897 named_ambiguous_expr!(and_binary -> (BinOp, Expr), allow_struct, tuple!(
898 binop,
899 ambiguous_expr!(allow_struct)
900 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700901
David Tolnay7184b132016-10-30 10:06:37 -0700902 named_ambiguous_expr!(expr_unary -> ExprKind, allow_struct, do_parse!(
David Tolnay3cb23a92016-10-07 23:02:21 -0700903 operator: unop >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700904 operand: ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700905 (ExprUnary { op: operator, expr: Box::new(operand) }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700906 ));
David Tolnay939766a2016-09-23 23:48:12 -0700907
David Tolnay7184b132016-10-30 10:06:37 -0700908 named!(expr_lit -> ExprKind, map!(lit, ExprKind::Lit));
David Tolnay939766a2016-09-23 23:48:12 -0700909
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700910 named!(and_cast -> (Ty, tokens::As), do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700911 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700912 ty: ty >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700913 (ty, tokens::As::default())
David Tolnay939766a2016-09-23 23:48:12 -0700914 ));
915
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700916 named!(and_ascription -> (Ty, tokens::Colon),
917 preceded!(punct!(":"), map!(ty, |t| (t, tokens::Colon::default()))));
David Tolnay939766a2016-09-23 23:48:12 -0700918
David Tolnaybb6feae2016-10-02 21:25:20 -0700919 enum Cond {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700920 Let(Pat, Expr, tokens::Eq, tokens::Let),
David Tolnay29f9ce12016-10-02 20:58:40 -0700921 Expr(Expr),
922 }
923
David Tolnaybb6feae2016-10-02 21:25:20 -0700924 named!(cond -> Cond, alt!(
925 do_parse!(
926 keyword!("let") >>
927 pat: pat >>
928 punct!("=") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700929 value: expr_no_struct >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700930 (Cond::Let(pat, value, tokens::Eq::default(), tokens::Let::default()))
David Tolnaybb6feae2016-10-02 21:25:20 -0700931 )
932 |
David Tolnayaf2557e2016-10-24 11:52:21 -0700933 map!(expr_no_struct, Cond::Expr)
David Tolnaybb6feae2016-10-02 21:25:20 -0700934 ));
935
David Tolnay7184b132016-10-30 10:06:37 -0700936 named!(expr_if -> ExprKind, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700937 keyword!("if") >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700938 cond: cond >>
Michael Layzell416724e2017-05-24 21:12:34 -0400939 then_block: delim!(Brace, within_block) >>
David Tolnay939766a2016-09-23 23:48:12 -0700940 else_block: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700941 keyword!("else"),
David Tolnay939766a2016-09-23 23:48:12 -0700942 alt!(
943 expr_if
944 |
945 do_parse!(
946 punct!("{") >>
947 else_block: within_block >>
948 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700949 (ExprKind::Block(ExprBlock {
950 unsafety: Unsafety::Normal,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700951 block: Block {
952 stmts: else_block,
953 brace_token: tokens::Brace::default(),
954 },
David Tolnay7184b132016-10-30 10:06:37 -0700955 }).into())
David Tolnay939766a2016-09-23 23:48:12 -0700956 )
957 )
958 )) >>
David Tolnay29f9ce12016-10-02 20:58:40 -0700959 (match cond {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700960 Cond::Let(pat, expr, eq_token, let_token) => ExprIfLet {
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 pat: Box::new(pat),
962 expr: Box::new(expr),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700963 eq_token: eq_token,
964 let_token: let_token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700965 if_true: Block {
David Tolnay29f9ce12016-10-02 20:58:40 -0700966 stmts: then_block,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700967 brace_token: tokens::Brace::default(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700968 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700969 if_token: tokens::If::default(),
970 else_token: else_block.as_ref().map(|_| tokens::Else::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700971 if_false: else_block.map(|els| Box::new(els.into())),
972 }.into(),
973 Cond::Expr(cond) => ExprIf {
974 cond: Box::new(cond),
975 if_true: Block {
David Tolnay29f9ce12016-10-02 20:58:40 -0700976 stmts: then_block,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700977 brace_token: tokens::Brace::default(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700978 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700979 if_token: tokens::If::default(),
980 else_token: else_block.as_ref().map(|_| tokens::Else::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700981 if_false: else_block.map(|els| Box::new(els.into())),
982 }.into(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700983 })
David Tolnay939766a2016-09-23 23:48:12 -0700984 ));
985
David Tolnay7184b132016-10-30 10:06:37 -0700986 named!(expr_for_loop -> ExprKind, do_parse!(
David Tolnaybb6feae2016-10-02 21:25:20 -0700987 lbl: option!(terminated!(label, punct!(":"))) >>
988 keyword!("for") >>
989 pat: pat >>
990 keyword!("in") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700991 expr: expr_no_struct >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700992 loop_block: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700993 (ExprForLoop {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700994 for_token: tokens::For::default(),
995 in_token: tokens::In::default(),
996 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700997 pat: Box::new(pat),
998 expr: Box::new(expr),
999 body: loop_block,
1000 label: lbl,
1001 }.into())
David Tolnaybb6feae2016-10-02 21:25:20 -07001002 ));
1003
David Tolnay7184b132016-10-30 10:06:37 -07001004 named!(expr_loop -> ExprKind, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -07001005 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -07001006 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -04001007 loop_block: block >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001008 (ExprLoop {
1009 loop_token: tokens::Loop::default(),
1010 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
1011 body: loop_block,
1012 label: lbl,
1013 }.into())
Gregory Katze5f35682016-09-27 14:20:55 -04001014 ));
1015
David Tolnay7184b132016-10-30 10:06:37 -07001016 named!(expr_match -> ExprKind, do_parse!(
David Tolnayb4ad3b52016-10-01 21:58:13 -07001017 keyword!("match") >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001018 obj: expr_no_struct >>
Michael Layzell416724e2017-05-24 21:12:34 -04001019 res: delim!(Brace, do_parse!(
1020 mut arms: many0!(do_parse!(
1021 arm: match_arm >>
1022 cond!(arm_requires_comma(&arm), punct!(",")) >>
1023 cond!(!arm_requires_comma(&arm), option!(punct!(","))) >>
1024 (arm)
1025 )) >>
1026 last_arm: option!(match_arm) >>
1027 (ExprKind::Match(Box::new(obj), {
1028 arms.extend(last_arm);
1029 arms
1030 }))
David Tolnayb4ad3b52016-10-01 21:58:13 -07001031 )) >>
David Tolnay1978c672016-10-27 22:05:52 -07001032 last_arm: option!(match_arm) >>
David Tolnayb4ad3b52016-10-01 21:58:13 -07001033 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001034 (ExprMatch {
1035 expr: Box::new(obj),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001036 match_token: tokens::Match::default(),
1037 brace_token: tokens::Brace::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001038 arms: {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001039 for arm in &mut arms {
1040 if arm_requires_comma(arm) {
1041 arm.comma = Some(tokens::Comma::default());
1042 }
1043 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001044 arms.extend(last_arm);
1045 arms
1046 },
1047 }.into())
David Tolnay1978c672016-10-27 22:05:52 -07001048 ));
1049
Arnavion02ef13f2017-04-25 00:54:31 -07001050 named!(expr_catch -> ExprKind, do_parse!(
1051 keyword!("do") >>
1052 keyword!("catch") >>
1053 catch_block: block >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001054 (ExprCatch {
1055 block: catch_block,
1056 do_token: tokens::Do::default(),
1057 catch_token: tokens::Catch::default(),
1058 }.into())
Arnavion02ef13f2017-04-25 00:54:31 -07001059 ));
1060
David Tolnay1978c672016-10-27 22:05:52 -07001061 fn arm_requires_comma(arm: &Arm) -> bool {
Alex Crichton62a0a592017-05-22 13:58:53 -07001062 if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
David Tolnay1978c672016-10-27 22:05:52 -07001063 false
1064 } else {
1065 true
1066 }
1067 }
1068
1069 named!(match_arm -> Arm, do_parse!(
1070 attrs: many0!(outer_attr) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001071 pats: separated_nonempty_list!(map!(punct!("|"), |_| tokens::Or::default()),
1072 pat) >>
David Tolnay1978c672016-10-27 22:05:52 -07001073 guard: option!(preceded!(keyword!("if"), expr)) >>
1074 punct!("=>") >>
1075 body: alt!(
Alex Crichton62a0a592017-05-22 13:58:53 -07001076 map!(block, |blk| {
1077 ExprKind::Block(ExprBlock {
1078 unsafety: Unsafety::Normal,
1079 block: blk,
1080 }).into()
1081 })
David Tolnay1978c672016-10-27 22:05:52 -07001082 |
1083 expr
1084 ) >>
1085 (Arm {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001086 rocket_token: tokens::Rocket::default(),
1087 if_token: guard.as_ref().map(|_| tokens::If::default()),
David Tolnay1978c672016-10-27 22:05:52 -07001088 attrs: attrs,
1089 pats: pats,
1090 guard: guard.map(Box::new),
1091 body: Box::new(body),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001092 comma: None,
David Tolnay1978c672016-10-27 22:05:52 -07001093 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07001094 ));
1095
David Tolnay7184b132016-10-30 10:06:37 -07001096 named_ambiguous_expr!(expr_closure -> ExprKind, allow_struct, do_parse!(
David Tolnay89e05672016-10-02 14:39:42 -07001097 capture: capture_by >>
1098 punct!("|") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001099 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1100 closure_arg) >>
David Tolnay89e05672016-10-02 14:39:42 -07001101 punct!("|") >>
1102 ret_and_body: alt!(
1103 do_parse!(
1104 punct!("->") >>
1105 ty: ty >>
1106 body: block >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001107 (FunctionRetTy::Ty(ty, tokens::RArrow::default()),
1108 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001109 unsafety: Unsafety::Normal,
1110 block: body,
1111 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001112 )
1113 |
David Tolnay58af3552016-12-22 16:58:07 -05001114 map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001115 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001116 (ExprClosure {
1117 capture: capture,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001118 or1_token: tokens::Or::default(),
1119 or2_token: tokens::Or::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001120 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -07001121 inputs: inputs,
1122 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -07001123 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001124 dot_tokens: None,
1125 fn_token: tokens::Fn::default(),
1126 generics: Generics::default(),
1127 paren_token: tokens::Paren::default(),
David Tolnay89e05672016-10-02 14:39:42 -07001128 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001129 body: Box::new(ret_and_body.1),
1130 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001131 ));
1132
1133 named!(closure_arg -> FnArg, do_parse!(
1134 pat: pat >>
1135 ty: option!(preceded!(punct!(":"), ty)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001136 (ArgCaptured {
1137 pat: pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001138 colon_token: tokens::Colon::default(),
1139 ty: ty.unwrap_or_else(|| TyInfer {
1140 underscore_token: tokens::Underscore::default(),
1141 }.into()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001142 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001143 ));
1144
David Tolnay7184b132016-10-30 10:06:37 -07001145 named!(expr_while -> ExprKind, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -07001146 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -07001147 keyword!("while") >>
David Tolnaybb6feae2016-10-02 21:25:20 -07001148 cond: cond >>
Gregory Katz3e562cc2016-09-28 18:33:02 -04001149 while_block: block >>
David Tolnaybb6feae2016-10-02 21:25:20 -07001150 (match cond {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001151 Cond::Let(pat, expr, eq_token, let_token) => ExprWhileLet {
1152 eq_token: eq_token,
1153 let_token: let_token,
1154 while_token: tokens::While::default(),
1155 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001156 pat: Box::new(pat),
1157 expr: Box::new(expr),
1158 body: while_block,
1159 label: lbl,
1160 }.into(),
1161 Cond::Expr(cond) => ExprWhile {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001162 while_token: tokens::While::default(),
1163 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001164 cond: Box::new(cond),
1165 body: while_block,
1166 label: lbl,
1167 }.into(),
David Tolnaybb6feae2016-10-02 21:25:20 -07001168 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001169 ));
1170
David Tolnay7184b132016-10-30 10:06:37 -07001171 named!(expr_continue -> ExprKind, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -04001172 keyword!("continue") >>
1173 lbl: option!(label) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001174 (ExprContinue {
1175 continue_token: tokens::Continue::default(),
1176 label: lbl,
1177 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001178 ));
1179
David Tolnay5d55ef72016-12-21 20:20:04 -05001180 named_ambiguous_expr!(expr_break -> ExprKind, allow_struct, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -04001181 keyword!("break") >>
1182 lbl: option!(label) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001183 val: option!(call!(ambiguous_expr, allow_struct, false)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001184 (ExprBreak {
1185 label: lbl,
1186 expr: val.map(Box::new),
1187 break_token: tokens::Break::default(),
1188 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001189 ));
1190
David Tolnay7184b132016-10-30 10:06:37 -07001191 named_ambiguous_expr!(expr_ret -> ExprKind, allow_struct, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -04001192 keyword!("return") >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001193 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001194 (ExprRet {
1195 expr: ret_value.map(Box::new),
1196 return_token: tokens::Return::default(),
1197 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001198 ));
1199
David Tolnay7184b132016-10-30 10:06:37 -07001200 named!(expr_struct -> ExprKind, do_parse!(
David Tolnay055a7042016-10-02 19:23:54 -07001201 path: path >>
1202 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001203 fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1204 field_value) >>
1205 base: option!(
1206 cond!(fields.is_empty() || fields.trailing_delim(),
1207 do_parse!(
1208 punct!("..") >>
1209 base: expr >>
1210 (base)
1211 )
1212 )
1213 ) >>
David Tolnay055a7042016-10-02 19:23:54 -07001214 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001215 (ExprStruct {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001216 brace_token: tokens::Brace::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001217 path: path,
1218 fields: fields,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001219 dot2_token: base.as_ref().and_then(|b| b.as_ref())
1220 .map(|_| tokens::Dot2::default()),
1221 rest: base.and_then(|b| b.map(Box::new)),
Alex Crichton62a0a592017-05-22 13:58:53 -07001222 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001223 ));
1224
David Tolnay276690f2016-10-30 12:06:59 -07001225 named!(field_value -> FieldValue, alt!(
1226 do_parse!(
1227 name: wordlike >>
1228 punct!(":") >>
1229 value: expr >>
1230 (FieldValue {
1231 ident: name,
1232 expr: value,
1233 is_shorthand: false,
David Tolnay71d93772017-01-22 23:58:24 -08001234 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001235 colon_token: Some(tokens::Colon::default()),
David Tolnay276690f2016-10-30 12:06:59 -07001236 })
1237 )
1238 |
1239 map!(ident, |name: Ident| FieldValue {
1240 ident: name.clone(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001241 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
David Tolnay276690f2016-10-30 12:06:59 -07001242 is_shorthand: true,
David Tolnay71d93772017-01-22 23:58:24 -08001243 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001244 colon_token: None,
David Tolnay055a7042016-10-02 19:23:54 -07001245 })
1246 ));
1247
Michael Layzell416724e2017-05-24 21:12:34 -04001248 named!(expr_repeat -> ExprKind, delim!(Bracket, do_parse!(
David Tolnay055a7042016-10-02 19:23:54 -07001249 value: expr >>
1250 punct!(";") >>
1251 times: expr >>
1252 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001253 (ExprRepeat {
1254 expr: Box::new(value),
1255 amt: Box::new(times),
1256 bracket_token: tokens::Bracket::default(),
1257 semi_token: tokens::Semi::default(),
1258 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001259 ));
1260
David Tolnay7184b132016-10-30 10:06:37 -07001261 named!(expr_block -> ExprKind, do_parse!(
David Tolnay7b035912016-12-21 22:42:07 -05001262 rules: unsafety >>
David Tolnay42602292016-10-01 22:25:45 -07001263 b: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001264 (ExprBlock {
1265 unsafety: rules,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001266 block: b,
Alex Crichton62a0a592017-05-22 13:58:53 -07001267 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001268 ));
1269
David Tolnay7184b132016-10-30 10:06:37 -07001270 named_ambiguous_expr!(expr_range -> ExprKind, allow_struct, do_parse!(
David Tolnay438c9052016-10-07 23:24:48 -07001271 limits: range_limits >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001272 hi: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001273 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001274 ));
1275
1276 named!(range_limits -> RangeLimits, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001277 punct!("...") => { |_| RangeLimits::Closed(tokens::Dot3::default()) }
David Tolnay438c9052016-10-07 23:24:48 -07001278 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001279 punct!("..") => { |_| RangeLimits::HalfOpen(tokens::Dot2::default()) }
David Tolnay438c9052016-10-07 23:24:48 -07001280 ));
1281
Alex Crichton62a0a592017-05-22 13:58:53 -07001282 named!(expr_path -> ExprKind, map!(qpath, |(qself, path)| {
1283 ExprPath { qself: qself, path: path }.into()
1284 }));
David Tolnay42602292016-10-01 22:25:45 -07001285
David Tolnay7184b132016-10-30 10:06:37 -07001286 named_ambiguous_expr!(expr_addr_of -> ExprKind, allow_struct, do_parse!(
David Tolnay3c2467c2016-10-02 17:55:08 -07001287 punct!("&") >>
1288 mutability: mutability >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001289 expr: ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001290 (ExprAddrOf {
1291 mutbl: mutability,
1292 expr: Box::new(expr),
1293 and_token: tokens::And::default(),
1294 }.into())
David Tolnay3c2467c2016-10-02 17:55:08 -07001295 ));
1296
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001297 named_ambiguous_expr!(and_assign -> (Expr, tokens::Eq), allow_struct, preceded!(
David Tolnayaf2557e2016-10-24 11:52:21 -07001298 punct!("="),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001299 map!(ambiguous_expr!(allow_struct), |t| (t, tokens::Eq::default()))
David Tolnayaf2557e2016-10-24 11:52:21 -07001300 ));
David Tolnay438c9052016-10-07 23:24:48 -07001301
David Tolnayaf2557e2016-10-24 11:52:21 -07001302 named_ambiguous_expr!(and_assign_op -> (BinOp, Expr), allow_struct, tuple!(
1303 assign_op,
1304 ambiguous_expr!(allow_struct)
1305 ));
David Tolnay438c9052016-10-07 23:24:48 -07001306
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001307 named!(and_field -> (Ident, tokens::Dot),
1308 preceded!(punct!("."), map!(ident, |t| (t, tokens::Dot::default()))));
David Tolnay438c9052016-10-07 23:24:48 -07001309
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001310 named!(and_tup_field -> (Lit, tokens::Dot),
1311 preceded!(punct!("."), map!(lit, |l| (l, tokens::Dot::default()))));
David Tolnay438c9052016-10-07 23:24:48 -07001312
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001313 named!(and_index -> (Expr, tokens::Bracket),
1314 map!(delimited!(punct!("["), expr, punct!("]")),
1315 |t| (t, tokens::Bracket::default())));
David Tolnay438c9052016-10-07 23:24:48 -07001316
David Tolnayaf2557e2016-10-24 11:52:21 -07001317 named_ambiguous_expr!(and_range -> (RangeLimits, Option<Expr>), allow_struct, tuple!(
1318 range_limits,
David Tolnay54e854d2016-10-24 12:03:30 -07001319 option!(call!(ambiguous_expr, allow_struct, false))
David Tolnayaf2557e2016-10-24 11:52:21 -07001320 ));
David Tolnay438c9052016-10-07 23:24:48 -07001321
David Tolnay42602292016-10-01 22:25:45 -07001322 named!(pub block -> Block, do_parse!(
Michael Layzell416724e2017-05-24 21:12:34 -04001323 stmts: delim!(Brace, within_block) >>
David Tolnay939766a2016-09-23 23:48:12 -07001324 (Block {
1325 stmts: stmts,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001326 brace_token: tokens::Brace::default(),
David Tolnay939766a2016-09-23 23:48:12 -07001327 })
1328 ));
1329
David Tolnay3b9783a2016-10-29 22:37:09 -07001330 named!(pub within_block -> Vec<Stmt>, do_parse!(
David Tolnaycfe55022016-10-02 22:02:27 -07001331 many0!(punct!(";")) >>
David Tolnay1bf19132017-02-19 22:54:25 -08001332 mut standalone: many0!(terminated!(stmt, many0!(punct!(";")))) >>
David Tolnay939766a2016-09-23 23:48:12 -07001333 last: option!(expr) >>
1334 (match last {
David Tolnaycfe55022016-10-02 22:02:27 -07001335 None => standalone,
David Tolnay939766a2016-09-23 23:48:12 -07001336 Some(last) => {
David Tolnaycfe55022016-10-02 22:02:27 -07001337 standalone.push(Stmt::Expr(Box::new(last)));
1338 standalone
David Tolnay939766a2016-09-23 23:48:12 -07001339 }
1340 })
1341 ));
1342
David Tolnay1bf19132017-02-19 22:54:25 -08001343 named!(pub stmt -> Stmt, alt!(
David Tolnay13b3d352016-10-03 00:31:15 -07001344 stmt_mac
1345 |
David Tolnay191e0582016-10-02 18:31:09 -07001346 stmt_local
1347 |
1348 stmt_item
1349 |
David Tolnaycfe55022016-10-02 22:02:27 -07001350 stmt_expr
David Tolnay939766a2016-09-23 23:48:12 -07001351 ));
1352
David Tolnay13b3d352016-10-03 00:31:15 -07001353 named!(stmt_mac -> Stmt, do_parse!(
1354 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001355 what: path >>
David Tolnayeea28d62016-10-25 20:44:08 -07001356 punct!("!") >>
1357 // Only parse braces here; paren and bracket will get parsed as
1358 // expression statements
1359 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001360 ts: token_stream >>
David Tolnayeea28d62016-10-25 20:44:08 -07001361 punct!("}") >>
David Tolnay60d48942016-10-30 14:34:52 -07001362 semi: option!(punct!(";")) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001363 (Stmt::Mac(Box::new((
1364 Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001365 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001366 bang_token: tokens::Bang::default(),
1367 tokens: vec![TokenTree(proc_macro2::TokenTree {
1368 span: Default::default(),
1369 kind: TokenKind::Sequence(Delimiter::Brace, ts),
David Tolnayeea28d62016-10-25 20:44:08 -07001370 })],
1371 },
David Tolnay60d48942016-10-30 14:34:52 -07001372 if semi.is_some() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001373 MacStmtStyle::Semicolon(tokens::Semi::default())
David Tolnay60d48942016-10-30 14:34:52 -07001374 } else {
1375 MacStmtStyle::Braces
1376 },
David Tolnayeea28d62016-10-25 20:44:08 -07001377 attrs,
1378 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001379 ));
1380
David Tolnay191e0582016-10-02 18:31:09 -07001381 named!(stmt_local -> Stmt, do_parse!(
1382 attrs: many0!(outer_attr) >>
1383 keyword!("let") >>
1384 pat: pat >>
1385 ty: option!(preceded!(punct!(":"), ty)) >>
1386 init: option!(preceded!(punct!("="), expr)) >>
1387 punct!(";") >>
1388 (Stmt::Local(Box::new(Local {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001389 let_token: tokens::Let::default(),
1390 semi_token: tokens::Semi::default(),
1391 colon_token: ty.as_ref().map(|_| tokens::Colon::default()),
1392 eq_token: init.as_ref().map(|_| tokens::Eq::default()),
David Tolnay191e0582016-10-02 18:31:09 -07001393 pat: Box::new(pat),
1394 ty: ty.map(Box::new),
1395 init: init.map(Box::new),
1396 attrs: attrs,
1397 })))
1398 ));
1399
1400 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
1401
David Tolnaycfe55022016-10-02 22:02:27 -07001402 fn requires_semi(e: &Expr) -> bool {
David Tolnay7184b132016-10-30 10:06:37 -07001403 match e.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001404 ExprKind::If(_) |
1405 ExprKind::IfLet(_) |
1406 ExprKind::While(_) |
1407 ExprKind::WhileLet(_) |
1408 ExprKind::ForLoop(_) |
1409 ExprKind::Loop(_) |
1410 ExprKind::Match(_) |
1411 ExprKind::Block(_) => false,
David Tolnaycfe55022016-10-02 22:02:27 -07001412
1413 _ => true,
1414 }
1415 }
1416
1417 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay7184b132016-10-30 10:06:37 -07001418 attrs: many0!(outer_attr) >>
1419 mut e: expr >>
David Tolnaycfe55022016-10-02 22:02:27 -07001420 semi: option!(punct!(";")) >>
David Tolnay7184b132016-10-30 10:06:37 -07001421 ({
1422 e.attrs = attrs;
1423 if semi.is_some() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001424 Stmt::Semi(Box::new(e), tokens::Semi::default())
David Tolnay092dcb02016-10-30 10:14:14 -07001425 } else if requires_semi(&e) {
Michael Layzell416724e2017-05-24 21:12:34 -04001426 return IResult::Error;
David Tolnay7184b132016-10-30 10:06:37 -07001427 } else {
1428 Stmt::Expr(Box::new(e))
1429 }
David Tolnaycfe55022016-10-02 22:02:27 -07001430 })
David Tolnay939766a2016-09-23 23:48:12 -07001431 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001432
David Tolnay42602292016-10-01 22:25:45 -07001433 named!(pub pat -> Pat, alt!(
David Tolnayfbb73232016-10-03 01:00:06 -07001434 pat_wild // must be before pat_ident
1435 |
1436 pat_box // must be before pat_ident
David Tolnayb4ad3b52016-10-01 21:58:13 -07001437 |
David Tolnay8b308c22016-10-03 01:24:10 -07001438 pat_range // must be before pat_lit
1439 |
David Tolnayaa610942016-10-03 22:22:49 -07001440 pat_tuple_struct // must be before pat_ident
1441 |
David Tolnay8d9e81a2016-10-03 22:36:32 -07001442 pat_struct // must be before pat_ident
1443 |
David Tolnayaa610942016-10-03 22:22:49 -07001444 pat_mac // must be before pat_ident
1445 |
David Tolnaybcdbdd02016-10-24 23:05:02 -07001446 pat_lit // must be before pat_ident
1447 |
David Tolnay8b308c22016-10-03 01:24:10 -07001448 pat_ident // must be before pat_path
David Tolnay9636c052016-10-02 17:11:17 -07001449 |
1450 pat_path
David Tolnayfbb73232016-10-03 01:00:06 -07001451 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001452 map!(pat_tuple, |t: PatTuple| t.into())
David Tolnayffdb97f2016-10-03 01:28:33 -07001453 |
1454 pat_ref
David Tolnay435a9a82016-10-29 13:47:20 -07001455 |
1456 pat_slice
David Tolnayb4ad3b52016-10-01 21:58:13 -07001457 ));
1458
David Tolnay84aa0752016-10-02 23:01:13 -07001459 named!(pat_mac -> Pat, map!(mac, Pat::Mac));
1460
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001461 named!(pat_wild -> Pat, map!(keyword!("_"), |_| {
1462 PatWild { underscore_token: tokens::Underscore::default() }.into()
1463 }));
David Tolnayb4ad3b52016-10-01 21:58:13 -07001464
David Tolnayfbb73232016-10-03 01:00:06 -07001465 named!(pat_box -> Pat, do_parse!(
1466 keyword!("box") >>
1467 pat: pat >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001468 (PatBox {
1469 pat: Box::new(pat),
1470 box_token: tokens::Box::default(),
1471 }.into())
David Tolnayfbb73232016-10-03 01:00:06 -07001472 ));
1473
David Tolnayb4ad3b52016-10-01 21:58:13 -07001474 named!(pat_ident -> Pat, do_parse!(
1475 mode: option!(keyword!("ref")) >>
1476 mutability: mutability >>
David Tolnay8d4d2fa2016-10-25 22:08:07 -07001477 name: alt!(
1478 ident
1479 |
1480 keyword!("self") => { Into::into }
1481 ) >>
David Tolnay1f16b602017-02-07 20:06:55 -05001482 not!(punct!("<")) >>
1483 not!(punct!("::")) >>
David Tolnayb4ad3b52016-10-01 21:58:13 -07001484 subpat: option!(preceded!(punct!("@"), pat)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001485 (PatIdent {
1486 mode: if mode.is_some() {
1487 BindingMode::ByRef(tokens::Ref::default(), mutability)
David Tolnayb4ad3b52016-10-01 21:58:13 -07001488 } else {
1489 BindingMode::ByValue(mutability)
1490 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001491 ident: name,
1492 at_token: subpat.as_ref().map(|_| tokens::At::default()),
1493 subpat: subpat.map(Box::new),
1494 }.into())
David Tolnayb4ad3b52016-10-01 21:58:13 -07001495 ));
1496
David Tolnayaa610942016-10-03 22:22:49 -07001497 named!(pat_tuple_struct -> Pat, do_parse!(
1498 path: path >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001499 tuple: pat_tuple >>
1500 (PatTupleStruct {
1501 path: path,
1502 pat: tuple,
1503 }.into())
David Tolnayaa610942016-10-03 22:22:49 -07001504 ));
1505
David Tolnay8d9e81a2016-10-03 22:36:32 -07001506 named!(pat_struct -> Pat, do_parse!(
1507 path: path >>
1508 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001509 fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1510 field_pat) >>
1511 base: option!(
1512 cond!(fields.is_empty() || fields.trailing_delim(),
1513 punct!(".."))
1514 ) >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001515 punct!("}") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001516 (PatStruct {
1517 path: path,
1518 fields: fields,
1519 brace_token: tokens::Brace::default(),
1520 dot2_token: base.and_then(|m| m).map(|_| tokens::Dot2::default()),
1521 }.into())
David Tolnay8d9e81a2016-10-03 22:36:32 -07001522 ));
1523
1524 named!(field_pat -> FieldPat, alt!(
1525 do_parse!(
David Tolnay39039442016-10-30 11:25:21 -07001526 ident: wordlike >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001527 punct!(":") >>
1528 pat: pat >>
1529 (FieldPat {
1530 ident: ident,
1531 pat: Box::new(pat),
1532 is_shorthand: false,
David Tolnay71d93772017-01-22 23:58:24 -08001533 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001534 colon_token: Some(tokens::Colon::default()),
David Tolnay8d9e81a2016-10-03 22:36:32 -07001535 })
1536 )
1537 |
1538 do_parse!(
David Tolnayda167382016-10-30 13:34:09 -07001539 boxed: option!(keyword!("box")) >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001540 mode: option!(keyword!("ref")) >>
1541 mutability: mutability >>
1542 ident: ident >>
David Tolnayda167382016-10-30 13:34:09 -07001543 ({
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001544 let mut pat: Pat = PatIdent {
1545 mode: if mode.is_some() {
1546 BindingMode::ByRef(tokens::Ref::default(), mutability)
David Tolnay8d9e81a2016-10-03 22:36:32 -07001547 } else {
1548 BindingMode::ByValue(mutability)
1549 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001550 ident: ident.clone(),
1551 subpat: None,
1552 at_token: None,
1553 }.into();
David Tolnayda167382016-10-30 13:34:09 -07001554 if boxed.is_some() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001555 pat = PatBox {
1556 pat: Box::new(pat),
1557 box_token: tokens::Box::default(),
1558 }.into();
David Tolnayda167382016-10-30 13:34:09 -07001559 }
1560 FieldPat {
1561 ident: ident,
1562 pat: Box::new(pat),
1563 is_shorthand: true,
David Tolnay71d93772017-01-22 23:58:24 -08001564 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001565 colon_token: None,
David Tolnayda167382016-10-30 13:34:09 -07001566 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001567 })
1568 )
1569 ));
1570
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001571 named!(pat_path -> Pat, map!(qpath, |(qself, path)| {
1572 PatPath { qself: qself, path: path }.into()
1573 }));
David Tolnay9636c052016-10-02 17:11:17 -07001574
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001575 named!(pat_tuple -> PatTuple, do_parse!(
David Tolnayfbb73232016-10-03 01:00:06 -07001576 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001577 mut elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1578 pat) >>
1579 dotdot: map!(cond!(
1580 elems.is_empty() || elems.trailing_delim(),
1581 option!(do_parse!(
1582 punct!("..") >>
1583 trailing: option!(punct!(",")) >>
1584 (trailing.is_some())
1585 ))
1586 ), |x: Option<_>| x.and_then(|x| x)) >>
1587 rest: cond!(dotdot == Some(true),
1588 terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1589 pat)) >>
David Tolnayfbb73232016-10-03 01:00:06 -07001590 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001591 (PatTuple {
1592 paren_token: tokens::Paren::default(),
1593 dots_pos: dotdot.map(|_| elems.len()),
1594 dot2_token: dotdot.map(|_| tokens::Dot2::default()),
1595 comma_token: dotdot.and_then(|b| {
1596 if b {
1597 Some(tokens::Comma::default())
1598 } else {
1599 None
1600 }
1601 }),
1602 pats: {
1603 if let Some(rest) = rest {
1604 for elem in rest.into_iter() {
1605 elems.push(elem);
1606 }
1607 }
1608 elems
1609 },
David Tolnayfbb73232016-10-03 01:00:06 -07001610 })
Michael Layzell416724e2017-05-24 21:12:34 -04001611 )));
David Tolnayfbb73232016-10-03 01:00:06 -07001612
David Tolnayffdb97f2016-10-03 01:28:33 -07001613 named!(pat_ref -> Pat, do_parse!(
1614 punct!("&") >>
1615 mutability: mutability >>
1616 pat: pat >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001617 (PatRef {
1618 pat: Box::new(pat),
1619 mutbl: mutability,
1620 and_token: tokens::And::default(),
1621 }.into())
David Tolnayffdb97f2016-10-03 01:28:33 -07001622 ));
1623
David Tolnayef40da42016-10-30 01:19:04 -07001624 named!(pat_lit -> Pat, do_parse!(
1625 lit: pat_lit_expr >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001626 (if let ExprKind::Path(_) = lit.node {
David Tolnayef40da42016-10-30 01:19:04 -07001627 return IResult::Error; // these need to be parsed by pat_path
1628 } else {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001629 PatLit {
1630 expr: Box::new(lit),
1631 }.into()
David Tolnayef40da42016-10-30 01:19:04 -07001632 })
1633 ));
David Tolnaye1310902016-10-29 23:40:00 -07001634
1635 named!(pat_range -> Pat, do_parse!(
David Tolnay2cfddc62016-10-30 01:03:27 -07001636 lo: pat_lit_expr >>
Arnavion1992e2f2017-04-25 01:47:46 -07001637 limits: range_limits >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001638 hi: pat_lit_expr >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001639 (PatRange {
1640 lo: Box::new(lo),
1641 hi: Box::new(hi),
1642 limits: limits,
1643 }.into())
David Tolnaye1310902016-10-29 23:40:00 -07001644 ));
1645
David Tolnay2cfddc62016-10-30 01:03:27 -07001646 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001647 neg: option!(punct!("-")) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001648 v: alt!(
David Tolnay7184b132016-10-30 10:06:37 -07001649 lit => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07001650 |
Alex Crichton62a0a592017-05-22 13:58:53 -07001651 path => { |p| ExprPath { qself: None, path: p }.into() }
David Tolnay2cfddc62016-10-30 01:03:27 -07001652 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001653 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07001654 ExprKind::Unary(ExprUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001655 op: UnOp::Neg(tokens::Sub::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001656 expr: Box::new(v.into())
1657 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001658 } else {
David Tolnay7184b132016-10-30 10:06:37 -07001659 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001660 })
1661 ));
David Tolnay8b308c22016-10-03 01:24:10 -07001662
David Tolnay435a9a82016-10-29 13:47:20 -07001663 named!(pat_slice -> Pat, do_parse!(
1664 punct!("[") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001665 mut before: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1666 pat) >>
1667 middle: option!(do_parse!(
David Tolnay435a9a82016-10-29 13:47:20 -07001668 punct!("..") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001669 trailing: option!(punct!(",")) >>
1670 (trailing.is_some())
David Tolnay435a9a82016-10-29 13:47:20 -07001671 )) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001672 after: cond!(
1673 match middle {
1674 Some(trailing) => trailing,
1675 _ => false,
1676 },
1677 terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1678 pat)
1679 ) >>
David Tolnay435a9a82016-10-29 13:47:20 -07001680 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001681 (PatSlice {
1682 dot2_token: middle.as_ref().map(|_| tokens::Dot2::default()),
1683 comma_token: {
1684 let trailing = middle.unwrap_or(false);
1685 if trailing {Some(tokens::Comma::default())} else {None}
1686 },
1687 bracket_token: tokens::Bracket::default(),
1688 middle: middle.and_then(|_| {
1689 if !before.is_empty() && !before.trailing_delim() {
1690 Some(Box::new(before.pop().unwrap().into_item()))
1691 } else {
1692 None
David Tolnaye1f13c32016-10-29 23:34:40 -07001693 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001694 }),
1695 front: before,
1696 back: after.unwrap_or_default(),
1697 }.into())
David Tolnay435a9a82016-10-29 13:47:20 -07001698 ));
1699
David Tolnay89e05672016-10-02 14:39:42 -07001700 named!(capture_by -> CaptureBy, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001701 keyword!("move") => { |_| CaptureBy::Value(tokens::Move::default()) }
David Tolnay89e05672016-10-02 14:39:42 -07001702 |
1703 epsilon!() => { |_| CaptureBy::Ref }
1704 ));
1705
David Tolnay8b07f372016-09-30 10:28:40 -07001706 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -07001707}
1708
David Tolnayf4bbbd92016-09-23 14:41:55 -07001709#[cfg(feature = "printing")]
1710mod printing {
1711 use super::*;
David Tolnay13b3d352016-10-03 00:31:15 -07001712 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07001713 use quote::{Tokens, ToTokens};
1714
1715 impl ToTokens for Expr {
1716 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07001717 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001718 self.node.to_tokens(tokens)
1719 }
1720 }
1721
1722 impl ToTokens for ExprBox {
1723 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001724 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001725 self.expr.to_tokens(tokens);
1726 }
1727 }
1728
1729 impl ToTokens for ExprInPlace {
1730 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001731 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001732 self.place.to_tokens(tokens);
1733 self.value.to_tokens(tokens);
1734 }
1735 }
1736
1737 impl ToTokens for ExprArray {
1738 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001739 self.bracket_token.surround(tokens, |tokens| {
1740 self.exprs.to_tokens(tokens);
1741 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001742 }
1743 }
1744
1745 impl ToTokens for ExprCall {
1746 fn to_tokens(&self, tokens: &mut Tokens) {
1747 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001748 self.paren_token.surround(tokens, |tokens| {
1749 self.args.to_tokens(tokens);
1750 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001751 }
1752 }
1753
1754 impl ToTokens for ExprMethodCall {
1755 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001756 self.expr.to_tokens(tokens);
1757 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001758 self.method.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001759 self.colon2_token.to_tokens(tokens);
1760 self.lt_token.to_tokens(tokens);
1761 self.typarams.to_tokens(tokens);
1762 self.gt_token.to_tokens(tokens);
1763 self.paren_token.surround(tokens, |tokens| {
1764 self.args.to_tokens(tokens);
1765 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001766 }
1767 }
1768
1769 impl ToTokens for ExprTup {
1770 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001771 self.paren_token.surround(tokens, |tokens| {
1772 self.args.to_tokens(tokens);
1773 self.lone_comma.to_tokens(tokens);
1774 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001775 }
1776 }
1777
1778 impl ToTokens for ExprBinary {
1779 fn to_tokens(&self, tokens: &mut Tokens) {
1780 self.left.to_tokens(tokens);
1781 self.op.to_tokens(tokens);
1782 self.right.to_tokens(tokens);
1783 }
1784 }
1785
1786 impl ToTokens for ExprUnary {
1787 fn to_tokens(&self, tokens: &mut Tokens) {
1788 self.op.to_tokens(tokens);
1789 self.expr.to_tokens(tokens);
1790 }
1791 }
1792
1793 impl ToTokens for ExprCast {
1794 fn to_tokens(&self, tokens: &mut Tokens) {
1795 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001796 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001797 self.ty.to_tokens(tokens);
1798 }
1799 }
1800
1801 impl ToTokens for ExprType {
1802 fn to_tokens(&self, tokens: &mut Tokens) {
1803 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001804 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001805 self.ty.to_tokens(tokens);
1806 }
1807 }
1808
1809 impl ToTokens for ExprIf {
1810 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001811 self.if_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001812 self.cond.to_tokens(tokens);
1813 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001814 self.else_token.to_tokens(tokens);
1815 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001816 }
1817 }
1818
1819 impl ToTokens for ExprIfLet {
1820 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 self.if_token.to_tokens(tokens);
1822 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001823 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001824 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001825 self.expr.to_tokens(tokens);
1826 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001827 self.else_token.to_tokens(tokens);
1828 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001829 }
1830 }
1831
1832 impl ToTokens for ExprWhile {
1833 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001834 self.label.to_tokens(tokens);
1835 self.colon_token.to_tokens(tokens);
1836 self.while_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001837 self.cond.to_tokens(tokens);
1838 self.body.to_tokens(tokens);
1839 }
1840 }
1841
1842 impl ToTokens for ExprWhileLet {
1843 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001844 self.label.to_tokens(tokens);
1845 self.colon_token.to_tokens(tokens);
1846 self.while_token.to_tokens(tokens);
1847 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001848 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001849 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001850 self.expr.to_tokens(tokens);
1851 self.body.to_tokens(tokens);
1852 }
1853 }
1854
1855 impl ToTokens for ExprForLoop {
1856 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001857 self.label.to_tokens(tokens);
1858 self.colon_token.to_tokens(tokens);
1859 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001860 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001861 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001862 self.expr.to_tokens(tokens);
1863 self.body.to_tokens(tokens);
1864 }
1865 }
1866
1867 impl ToTokens for ExprLoop {
1868 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001869 self.label.to_tokens(tokens);
1870 self.colon_token.to_tokens(tokens);
1871 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001872 self.body.to_tokens(tokens);
1873 }
1874 }
1875
1876 impl ToTokens for ExprMatch {
1877 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001878 self.match_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001879 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001880 self.brace_token.surround(tokens, |tokens| {
1881 tokens.append_all(&self.arms);
1882 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001883 }
1884 }
1885
1886 impl ToTokens for ExprCatch {
1887 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001888 self.do_token.to_tokens(tokens);
1889 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001890 self.block.to_tokens(tokens);
1891 }
1892 }
1893
1894 impl ToTokens for ExprClosure {
1895 fn to_tokens(&self, tokens: &mut Tokens) {
1896 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001897 self.or1_token.to_tokens(tokens);
1898 for item in self.decl.inputs.iter() {
1899 match **item.item() {
1900 FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001901 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07001902 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001903 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07001904 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001905 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07001906 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001907 self.or2_token.to_tokens(tokens);
1908 self.decl.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001909 self.body.to_tokens(tokens);
1910 }
1911 }
1912
1913 impl ToTokens for ExprBlock {
1914 fn to_tokens(&self, tokens: &mut Tokens) {
1915 self.unsafety.to_tokens(tokens);
1916 self.block.to_tokens(tokens);
1917 }
1918 }
1919
1920 impl ToTokens for ExprAssign {
1921 fn to_tokens(&self, tokens: &mut Tokens) {
1922 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001923 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001924 self.right.to_tokens(tokens);
1925 }
1926 }
1927
1928 impl ToTokens for ExprAssignOp {
1929 fn to_tokens(&self, tokens: &mut Tokens) {
1930 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001931 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001932 self.right.to_tokens(tokens);
1933 }
1934 }
1935
1936 impl ToTokens for ExprField {
1937 fn to_tokens(&self, tokens: &mut Tokens) {
1938 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001939 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001940 self.field.to_tokens(tokens);
1941 }
1942 }
1943
1944 impl ToTokens for ExprTupField {
1945 fn to_tokens(&self, tokens: &mut Tokens) {
1946 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001947 self.dot_token.to_tokens(tokens);
1948 self.field.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001949 }
1950 }
1951
1952 impl ToTokens for ExprIndex {
1953 fn to_tokens(&self, tokens: &mut Tokens) {
1954 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001955 self.bracket_token.surround(tokens, |tokens| {
1956 self.index.to_tokens(tokens);
1957 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001958 }
1959 }
1960
1961 impl ToTokens for ExprRange {
1962 fn to_tokens(&self, tokens: &mut Tokens) {
1963 self.from.to_tokens(tokens);
1964 self.limits.to_tokens(tokens);
1965 self.to.to_tokens(tokens);
1966 }
1967 }
1968
1969 impl ToTokens for ExprPath {
1970 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001971 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07001972 }
1973 }
1974
1975 impl ToTokens for ExprAddrOf {
1976 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001977 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001978 self.mutbl.to_tokens(tokens);
1979 self.expr.to_tokens(tokens);
1980 }
1981 }
1982
1983 impl ToTokens for ExprBreak {
1984 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001985 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001986 self.label.to_tokens(tokens);
1987 self.expr.to_tokens(tokens);
1988 }
1989 }
1990
1991 impl ToTokens for ExprContinue {
1992 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001993 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001994 self.label.to_tokens(tokens);
1995 }
1996 }
1997
1998 impl ToTokens for ExprRet {
1999 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002000 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002001 self.expr.to_tokens(tokens);
2002 }
2003 }
2004
2005 impl ToTokens for ExprStruct {
2006 fn to_tokens(&self, tokens: &mut Tokens) {
2007 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002008 self.brace_token.surround(tokens, |tokens| {
2009 self.fields.to_tokens(tokens);
2010 self.dot2_token.to_tokens(tokens);
2011 self.rest.to_tokens(tokens);
2012 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002013 }
2014 }
2015
2016 impl ToTokens for ExprRepeat {
2017 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002018 self.bracket_token.surround(tokens, |tokens| {
2019 self.expr.to_tokens(tokens);
2020 self.semi_token.to_tokens(tokens);
2021 self.amt.to_tokens(tokens);
2022 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002023 }
2024 }
2025
2026 impl ToTokens for ExprParen {
2027 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002028 self.paren_token.surround(tokens, |tokens| {
2029 self.expr.to_tokens(tokens);
2030 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002031 }
2032 }
2033
2034 impl ToTokens for ExprTry {
2035 fn to_tokens(&self, tokens: &mut Tokens) {
2036 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002037 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002038 }
2039 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002040
David Tolnay055a7042016-10-02 19:23:54 -07002041 impl ToTokens for FieldValue {
2042 fn to_tokens(&self, tokens: &mut Tokens) {
2043 self.ident.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002044 if !self.is_shorthand {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002045 self.colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002046 self.expr.to_tokens(tokens);
2047 }
David Tolnay055a7042016-10-02 19:23:54 -07002048 }
2049 }
2050
David Tolnayb4ad3b52016-10-01 21:58:13 -07002051 impl ToTokens for Arm {
2052 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002053 tokens.append_all(&self.attrs);
2054 self.pats.to_tokens(tokens);
2055 self.if_token.to_tokens(tokens);
2056 self.guard.to_tokens(tokens);
2057 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002058 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002059 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002060 }
2061 }
2062
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002063 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002064 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002065 self.underscore_token.to_tokens(tokens);
2066 }
2067 }
2068
2069 impl ToTokens for PatIdent {
2070 fn to_tokens(&self, tokens: &mut Tokens) {
2071 self.mode.to_tokens(tokens);
2072 self.ident.to_tokens(tokens);
2073 self.at_token.to_tokens(tokens);
2074 self.subpat.to_tokens(tokens);
2075 }
2076 }
2077
2078 impl ToTokens for PatStruct {
2079 fn to_tokens(&self, tokens: &mut Tokens) {
2080 self.path.to_tokens(tokens);
2081 self.brace_token.surround(tokens, |tokens| {
2082 self.fields.to_tokens(tokens);
2083 self.dot2_token.to_tokens(tokens);
2084 });
2085 }
2086 }
2087
2088 impl ToTokens for PatTupleStruct {
2089 fn to_tokens(&self, tokens: &mut Tokens) {
2090 self.path.to_tokens(tokens);
2091 self.pat.to_tokens(tokens);
2092 }
2093 }
2094
2095 impl ToTokens for PatPath {
2096 fn to_tokens(&self, tokens: &mut Tokens) {
2097 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2098 }
2099 }
2100
2101 impl ToTokens for PatTuple {
2102 fn to_tokens(&self, tokens: &mut Tokens) {
2103 self.paren_token.surround(tokens, |tokens| {
2104 for (i, token) in self.pats.iter().enumerate() {
2105 if Some(i) == self.dots_pos {
2106 self.dot2_token.to_tokens(tokens);
2107 self.comma_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002108 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002109 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002110 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002111
2112 if Some(self.pats.len()) == self.dots_pos {
2113 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002114 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002115 });
2116 }
2117 }
2118
2119 impl ToTokens for PatBox {
2120 fn to_tokens(&self, tokens: &mut Tokens) {
2121 self.box_token.to_tokens(tokens);
2122 self.pat.to_tokens(tokens);
2123 }
2124 }
2125
2126 impl ToTokens for PatRef {
2127 fn to_tokens(&self, tokens: &mut Tokens) {
2128 self.and_token.to_tokens(tokens);
2129 self.mutbl.to_tokens(tokens);
2130 self.pat.to_tokens(tokens);
2131 }
2132 }
2133
2134 impl ToTokens for PatLit {
2135 fn to_tokens(&self, tokens: &mut Tokens) {
2136 self.expr.to_tokens(tokens);
2137 }
2138 }
2139
2140 impl ToTokens for PatRange {
2141 fn to_tokens(&self, tokens: &mut Tokens) {
2142 self.lo.to_tokens(tokens);
2143 self.limits.to_tokens(tokens);
2144 self.hi.to_tokens(tokens);
2145 }
2146 }
2147
2148 impl ToTokens for PatSlice {
2149 fn to_tokens(&self, tokens: &mut Tokens) {
2150 self.bracket_token.surround(tokens, |tokens| {
2151 self.front.to_tokens(tokens);
2152 self.middle.to_tokens(tokens);
2153 self.dot2_token.to_tokens(tokens);
2154 self.comma_token.to_tokens(tokens);
2155 self.back.to_tokens(tokens);
2156 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002157 }
2158 }
2159
Arnavion1992e2f2017-04-25 01:47:46 -07002160 impl ToTokens for RangeLimits {
2161 fn to_tokens(&self, tokens: &mut Tokens) {
2162 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002163 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2164 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
Arnavion1992e2f2017-04-25 01:47:46 -07002165 }
2166 }
2167 }
2168
David Tolnay8d9e81a2016-10-03 22:36:32 -07002169 impl ToTokens for FieldPat {
2170 fn to_tokens(&self, tokens: &mut Tokens) {
2171 if !self.is_shorthand {
2172 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002173 self.colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002174 }
2175 self.pat.to_tokens(tokens);
2176 }
2177 }
2178
David Tolnayb4ad3b52016-10-01 21:58:13 -07002179 impl ToTokens for BindingMode {
2180 fn to_tokens(&self, tokens: &mut Tokens) {
2181 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002182 BindingMode::ByRef(ref t, ref m) => {
2183 t.to_tokens(tokens);
2184 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002185 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002186 BindingMode::ByValue(ref m) => {
2187 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002188 }
2189 }
2190 }
2191 }
David Tolnay42602292016-10-01 22:25:45 -07002192
David Tolnay89e05672016-10-02 14:39:42 -07002193 impl ToTokens for CaptureBy {
2194 fn to_tokens(&self, tokens: &mut Tokens) {
2195 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002196 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002197 CaptureBy::Ref => {
2198 // nothing
2199 }
David Tolnay89e05672016-10-02 14:39:42 -07002200 }
2201 }
2202 }
2203
David Tolnay42602292016-10-01 22:25:45 -07002204 impl ToTokens for Block {
2205 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002206 self.brace_token.surround(tokens, |tokens| {
2207 tokens.append_all(&self.stmts);
2208 });
David Tolnay42602292016-10-01 22:25:45 -07002209 }
2210 }
2211
David Tolnay42602292016-10-01 22:25:45 -07002212 impl ToTokens for Stmt {
2213 fn to_tokens(&self, tokens: &mut Tokens) {
2214 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002215 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002216 Stmt::Item(ref item) => item.to_tokens(tokens),
2217 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002218 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002219 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002220 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002221 }
David Tolnay13b3d352016-10-03 00:31:15 -07002222 Stmt::Mac(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002223 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002224 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002225 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07002226 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002227 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002228 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2229 // no semicolon
2230 }
David Tolnay13b3d352016-10-03 00:31:15 -07002231 }
2232 }
David Tolnay42602292016-10-01 22:25:45 -07002233 }
2234 }
2235 }
David Tolnay191e0582016-10-02 18:31:09 -07002236
2237 impl ToTokens for Local {
2238 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002239 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002240 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002241 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002242 self.colon_token.to_tokens(tokens);
2243 self.ty.to_tokens(tokens);
2244 self.eq_token.to_tokens(tokens);
2245 self.init.to_tokens(tokens);
2246 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002247 }
2248 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002249}