blob: 2956ec9a74ffa2d0138fb53f2ab96163cb90812e [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};
David Tolnayb9c8e322016-09-23 20:48:37 -0700601
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700602 use proc_macro2::{self, TokenKind, Delimiter};
603
David Tolnayaf2557e2016-10-24 11:52:21 -0700604 // Struct literals are ambiguous in certain positions
605 // https://github.com/rust-lang/rfcs/pull/92
606 macro_rules! named_ambiguous_expr {
607 ($name:ident -> $o:ty, $allow_struct:ident, $submac:ident!( $($args:tt)* )) => {
David Tolnay5fe14fc2017-01-27 16:22:08 -0800608 fn $name(i: &str, $allow_struct: bool) -> $crate::synom::IResult<&str, $o> {
David Tolnayaf2557e2016-10-24 11:52:21 -0700609 $submac!(i, $($args)*)
610 }
611 };
612 }
613
614 macro_rules! ambiguous_expr {
615 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700616 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700617 };
618 }
619
620 named!(pub expr -> Expr, ambiguous_expr!(true));
621
622 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
623
David Tolnay02a8d472017-02-19 12:59:44 -0800624 #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
David Tolnay54e854d2016-10-24 12:03:30 -0700625 fn ambiguous_expr(i: &str, allow_struct: bool, allow_block: bool) -> IResult<&str, Expr> {
626 do_parse!(
627 i,
628 mut e: alt!(
629 expr_lit // must be before expr_struct
630 |
631 cond_reduce!(allow_struct, expr_struct) // must be before expr_path
632 |
633 expr_paren // must be before expr_tup
634 |
635 expr_mac // must be before expr_path
636 |
David Tolnay5d55ef72016-12-21 20:20:04 -0500637 call!(expr_break, allow_struct) // must be before expr_path
David Tolnay54e854d2016-10-24 12:03:30 -0700638 |
639 expr_continue // must be before expr_path
640 |
641 call!(expr_ret, allow_struct) // must be before expr_path
642 |
643 call!(expr_box, allow_struct)
644 |
David Tolnay6696c3e2016-10-30 11:45:10 -0700645 expr_in_place
646 |
David Tolnay9c7ab132017-01-23 00:01:22 -0800647 expr_array
David Tolnay54e854d2016-10-24 12:03:30 -0700648 |
649 expr_tup
650 |
651 call!(expr_unary, allow_struct)
652 |
653 expr_if
654 |
655 expr_while
656 |
657 expr_for_loop
658 |
659 expr_loop
660 |
661 expr_match
662 |
Arnavion02ef13f2017-04-25 00:54:31 -0700663 expr_catch
664 |
David Tolnay54e854d2016-10-24 12:03:30 -0700665 call!(expr_closure, allow_struct)
666 |
667 cond_reduce!(allow_block, expr_block)
668 |
669 call!(expr_range, allow_struct)
670 |
671 expr_path
672 |
673 call!(expr_addr_of, allow_struct)
674 |
675 expr_repeat
676 ) >>
677 many0!(alt!(
678 tap!(args: and_call => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700679 let (args, paren) = args;
Alex Crichton62a0a592017-05-22 13:58:53 -0700680 e = ExprCall {
681 func: Box::new(e.into()),
682 args: args,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700683 paren_token: paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700684 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700685 })
686 |
687 tap!(more: and_method_call => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700688 let mut call = more;
689 call.expr = Box::new(e.into());
690 e = call.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700691 })
692 |
693 tap!(more: call!(and_binary, allow_struct) => {
694 let (op, other) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 e = ExprBinary {
696 op: op,
697 left: Box::new(e.into()),
698 right: Box::new(other),
699 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700700 })
701 |
702 tap!(ty: and_cast => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700703 let (ty, token) = ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 e = ExprCast {
705 expr: Box::new(e.into()),
706 ty: Box::new(ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700707 as_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700708 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700709 })
710 |
711 tap!(ty: and_ascription => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700712 let (ty, token) = ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700713 e = ExprType {
714 expr: Box::new(e.into()),
715 ty: Box::new(ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700716 colon_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700717 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700718 })
719 |
720 tap!(v: call!(and_assign, allow_struct) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700721 let (v, token) = v;
Alex Crichton62a0a592017-05-22 13:58:53 -0700722 e = ExprAssign {
723 left: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700724 eq_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 right: Box::new(v),
726 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700727 })
728 |
729 tap!(more: call!(and_assign_op, allow_struct) => {
730 let (op, v) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700731 e = ExprAssignOp {
732 op: op,
733 left: Box::new(e.into()),
734 right: Box::new(v),
735 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700736 })
737 |
738 tap!(field: and_field => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700739 let (field, token) = field;
Alex Crichton62a0a592017-05-22 13:58:53 -0700740 e = ExprField {
741 expr: Box::new(e.into()),
742 field: field,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700743 dot_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700745 })
746 |
747 tap!(field: and_tup_field => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700748 let (field, token) = field;
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 e = ExprTupField {
750 expr: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700751 field: field,
752 dot_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700753 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700754 })
755 |
756 tap!(i: and_index => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700757 let (i, token) = i;
Alex Crichton62a0a592017-05-22 13:58:53 -0700758 e = ExprIndex {
759 expr: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700760 bracket_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700761 index: Box::new(i),
762 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700763 })
764 |
765 tap!(more: call!(and_range, allow_struct) => {
766 let (limits, hi) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700767 e = ExprRange {
768 from: Some(Box::new(e.into())),
769 to: hi.map(Box::new),
770 limits: limits,
771 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700772 })
773 |
774 tap!(_try: punct!("?") => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700775 e = ExprTry {
776 expr: Box::new(e.into()),
777 question_token: tokens::Question::default(),
778 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700779 })
780 )) >>
David Tolnay7184b132016-10-30 10:06:37 -0700781 (e.into())
David Tolnay54e854d2016-10-24 12:03:30 -0700782 )
783 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700784
David Tolnay7184b132016-10-30 10:06:37 -0700785 named!(expr_mac -> ExprKind, map!(mac, ExprKind::Mac));
David Tolnay84aa0752016-10-02 23:01:13 -0700786
David Tolnay7184b132016-10-30 10:06:37 -0700787 named!(expr_paren -> ExprKind, do_parse!(
David Tolnay89e05672016-10-02 14:39:42 -0700788 punct!("(") >>
789 e: expr >>
790 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700791 (ExprParen {
792 expr: Box::new(e),
793 paren_token: tokens::Paren::default(),
794 }.into())
David Tolnay89e05672016-10-02 14:39:42 -0700795 ));
796
David Tolnay7184b132016-10-30 10:06:37 -0700797 named_ambiguous_expr!(expr_box -> ExprKind, allow_struct, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700798 keyword!("box") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700799 inner: ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700800 (ExprBox {
801 expr: Box::new(inner),
802 box_token: tokens::Box::default(),
803 }.into())
David Tolnayb9c8e322016-09-23 20:48:37 -0700804 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700805
David Tolnay6696c3e2016-10-30 11:45:10 -0700806 named!(expr_in_place -> ExprKind, do_parse!(
807 keyword!("in") >>
808 place: expr_no_struct >>
809 punct!("{") >>
810 value: within_block >>
811 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700812 (ExprInPlace {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700813 in_token: tokens::In::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -0700814 place: Box::new(place),
815 value: Box::new(Expr {
816 node: ExprBlock {
817 unsafety: Unsafety::Normal,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700818 block: Block {
819 stmts: value,
820 brace_token: tokens::Brace::default(),
821 },
Alex Crichton62a0a592017-05-22 13:58:53 -0700822 }.into(),
823 attrs: Vec::new(),
824 }),
825 }.into())
David Tolnay6696c3e2016-10-30 11:45:10 -0700826 ));
827
David Tolnay9c7ab132017-01-23 00:01:22 -0800828 named!(expr_array -> ExprKind, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700829 punct!("[") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700830 elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
831 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700832 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700833 (ExprArray {
834 exprs: elems,
835 bracket_token: tokens::Bracket::default(),
836 }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700837 ));
838
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700839 named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren), do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700840 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700841 args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
842 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700843 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700844 (args, tokens::Paren::default())
David Tolnayfa0edf22016-09-23 22:58:24 -0700845 ));
846
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700847 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700848 punct!(".") >>
849 method: ident >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700850 typarams: option!(do_parse!(
851 punct!("::") >>
852 punct!("<") >>
853 tys: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
854 ty) >>
855 punct!(">") >>
856 (tys)
David Tolnayfa0edf22016-09-23 22:58:24 -0700857 )) >>
858 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700859 args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
860 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700861 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700862 (ExprMethodCall {
863 // this expr will get overwritten after being returned
864 expr: Box::new(ExprKind::Lit(Lit {
865 span: Span::default(),
866 value: LitKind::Bool(false),
867 }).into()),
868
869 method: method,
870 args: args,
871 paren_token: tokens::Paren::default(),
872 dot_token: tokens::Dot::default(),
873 lt_token: typarams.as_ref().map(|_| tokens::Lt::default()),
874 gt_token: typarams.as_ref().map(|_| tokens::Gt::default()),
875 colon2_token: typarams.as_ref().map(|_| tokens::Colon2::default()),
876 typarams: typarams.unwrap_or_default(),
877 })
David Tolnayfa0edf22016-09-23 22:58:24 -0700878 ));
879
David Tolnay7184b132016-10-30 10:06:37 -0700880 named!(expr_tup -> ExprKind, do_parse!(
David Tolnayfa0edf22016-09-23 22:58:24 -0700881 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700882 elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
883 expr) >>
David Tolnayfa0edf22016-09-23 22:58:24 -0700884 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700885 (ExprTup {
886 args: elems,
887 paren_token: tokens::Paren::default(),
888 lone_comma: None, // TODO: parse this
889 }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700890 ));
891
David Tolnayaf2557e2016-10-24 11:52:21 -0700892 named_ambiguous_expr!(and_binary -> (BinOp, Expr), allow_struct, tuple!(
893 binop,
894 ambiguous_expr!(allow_struct)
895 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700896
David Tolnay7184b132016-10-30 10:06:37 -0700897 named_ambiguous_expr!(expr_unary -> ExprKind, allow_struct, do_parse!(
David Tolnay3cb23a92016-10-07 23:02:21 -0700898 operator: unop >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700899 operand: ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700900 (ExprUnary { op: operator, expr: Box::new(operand) }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700901 ));
David Tolnay939766a2016-09-23 23:48:12 -0700902
David Tolnay7184b132016-10-30 10:06:37 -0700903 named!(expr_lit -> ExprKind, map!(lit, ExprKind::Lit));
David Tolnay939766a2016-09-23 23:48:12 -0700904
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700905 named!(and_cast -> (Ty, tokens::As), do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700906 keyword!("as") >>
David Tolnay939766a2016-09-23 23:48:12 -0700907 ty: ty >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700908 (ty, tokens::As::default())
David Tolnay939766a2016-09-23 23:48:12 -0700909 ));
910
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700911 named!(and_ascription -> (Ty, tokens::Colon),
912 preceded!(punct!(":"), map!(ty, |t| (t, tokens::Colon::default()))));
David Tolnay939766a2016-09-23 23:48:12 -0700913
David Tolnaybb6feae2016-10-02 21:25:20 -0700914 enum Cond {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700915 Let(Pat, Expr, tokens::Eq, tokens::Let),
David Tolnay29f9ce12016-10-02 20:58:40 -0700916 Expr(Expr),
917 }
918
David Tolnaybb6feae2016-10-02 21:25:20 -0700919 named!(cond -> Cond, alt!(
920 do_parse!(
921 keyword!("let") >>
922 pat: pat >>
923 punct!("=") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700924 value: expr_no_struct >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700925 (Cond::Let(pat, value, tokens::Eq::default(), tokens::Let::default()))
David Tolnaybb6feae2016-10-02 21:25:20 -0700926 )
927 |
David Tolnayaf2557e2016-10-24 11:52:21 -0700928 map!(expr_no_struct, Cond::Expr)
David Tolnaybb6feae2016-10-02 21:25:20 -0700929 ));
930
David Tolnay7184b132016-10-30 10:06:37 -0700931 named!(expr_if -> ExprKind, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700932 keyword!("if") >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700933 cond: cond >>
David Tolnay939766a2016-09-23 23:48:12 -0700934 punct!("{") >>
935 then_block: within_block >>
936 punct!("}") >>
937 else_block: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700938 keyword!("else"),
David Tolnay939766a2016-09-23 23:48:12 -0700939 alt!(
940 expr_if
941 |
942 do_parse!(
943 punct!("{") >>
944 else_block: within_block >>
945 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700946 (ExprKind::Block(ExprBlock {
947 unsafety: Unsafety::Normal,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700948 block: Block {
949 stmts: else_block,
950 brace_token: tokens::Brace::default(),
951 },
David Tolnay7184b132016-10-30 10:06:37 -0700952 }).into())
David Tolnay939766a2016-09-23 23:48:12 -0700953 )
954 )
955 )) >>
David Tolnay29f9ce12016-10-02 20:58:40 -0700956 (match cond {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700957 Cond::Let(pat, expr, eq_token, let_token) => ExprIfLet {
Alex Crichton62a0a592017-05-22 13:58:53 -0700958 pat: Box::new(pat),
959 expr: Box::new(expr),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700960 eq_token: eq_token,
961 let_token: let_token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700962 if_true: Block {
David Tolnay29f9ce12016-10-02 20:58:40 -0700963 stmts: then_block,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700964 brace_token: tokens::Brace::default(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700965 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700966 if_token: tokens::If::default(),
967 else_token: else_block.as_ref().map(|_| tokens::Else::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700968 if_false: else_block.map(|els| Box::new(els.into())),
969 }.into(),
970 Cond::Expr(cond) => ExprIf {
971 cond: Box::new(cond),
972 if_true: Block {
David Tolnay29f9ce12016-10-02 20:58:40 -0700973 stmts: then_block,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700974 brace_token: tokens::Brace::default(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700975 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700976 if_token: tokens::If::default(),
977 else_token: else_block.as_ref().map(|_| tokens::Else::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700978 if_false: else_block.map(|els| Box::new(els.into())),
979 }.into(),
David Tolnay29f9ce12016-10-02 20:58:40 -0700980 })
David Tolnay939766a2016-09-23 23:48:12 -0700981 ));
982
David Tolnay7184b132016-10-30 10:06:37 -0700983 named!(expr_for_loop -> ExprKind, do_parse!(
David Tolnaybb6feae2016-10-02 21:25:20 -0700984 lbl: option!(terminated!(label, punct!(":"))) >>
985 keyword!("for") >>
986 pat: pat >>
987 keyword!("in") >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700988 expr: expr_no_struct >>
David Tolnaybb6feae2016-10-02 21:25:20 -0700989 loop_block: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700990 (ExprForLoop {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700991 for_token: tokens::For::default(),
992 in_token: tokens::In::default(),
993 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -0700994 pat: Box::new(pat),
995 expr: Box::new(expr),
996 body: loop_block,
997 label: lbl,
998 }.into())
David Tolnaybb6feae2016-10-02 21:25:20 -0700999 ));
1000
David Tolnay7184b132016-10-30 10:06:37 -07001001 named!(expr_loop -> ExprKind, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -07001002 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay10413f02016-09-30 09:12:02 -07001003 keyword!("loop") >>
Gregory Katze5f35682016-09-27 14:20:55 -04001004 loop_block: block >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001005 (ExprLoop {
1006 loop_token: tokens::Loop::default(),
1007 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
1008 body: loop_block,
1009 label: lbl,
1010 }.into())
Gregory Katze5f35682016-09-27 14:20:55 -04001011 ));
1012
David Tolnay7184b132016-10-30 10:06:37 -07001013 named!(expr_match -> ExprKind, do_parse!(
David Tolnayb4ad3b52016-10-01 21:58:13 -07001014 keyword!("match") >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001015 obj: expr_no_struct >>
David Tolnayb4ad3b52016-10-01 21:58:13 -07001016 punct!("{") >>
David Tolnay1978c672016-10-27 22:05:52 -07001017 mut arms: many0!(do_parse!(
1018 arm: match_arm >>
1019 cond!(arm_requires_comma(&arm), punct!(",")) >>
1020 cond!(!arm_requires_comma(&arm), option!(punct!(","))) >>
1021 (arm)
David Tolnayb4ad3b52016-10-01 21:58:13 -07001022 )) >>
David Tolnay1978c672016-10-27 22:05:52 -07001023 last_arm: option!(match_arm) >>
David Tolnayb4ad3b52016-10-01 21:58:13 -07001024 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001025 (ExprMatch {
1026 expr: Box::new(obj),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001027 match_token: tokens::Match::default(),
1028 brace_token: tokens::Brace::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001029 arms: {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001030 for arm in &mut arms {
1031 if arm_requires_comma(arm) {
1032 arm.comma = Some(tokens::Comma::default());
1033 }
1034 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001035 arms.extend(last_arm);
1036 arms
1037 },
1038 }.into())
David Tolnay1978c672016-10-27 22:05:52 -07001039 ));
1040
Arnavion02ef13f2017-04-25 00:54:31 -07001041 named!(expr_catch -> ExprKind, do_parse!(
1042 keyword!("do") >>
1043 keyword!("catch") >>
1044 catch_block: block >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001045 (ExprCatch {
1046 block: catch_block,
1047 do_token: tokens::Do::default(),
1048 catch_token: tokens::Catch::default(),
1049 }.into())
Arnavion02ef13f2017-04-25 00:54:31 -07001050 ));
1051
David Tolnay1978c672016-10-27 22:05:52 -07001052 fn arm_requires_comma(arm: &Arm) -> bool {
Alex Crichton62a0a592017-05-22 13:58:53 -07001053 if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
David Tolnay1978c672016-10-27 22:05:52 -07001054 false
1055 } else {
1056 true
1057 }
1058 }
1059
1060 named!(match_arm -> Arm, do_parse!(
1061 attrs: many0!(outer_attr) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001062 pats: separated_nonempty_list!(map!(punct!("|"), |_| tokens::Or::default()),
1063 pat) >>
David Tolnay1978c672016-10-27 22:05:52 -07001064 guard: option!(preceded!(keyword!("if"), expr)) >>
1065 punct!("=>") >>
1066 body: alt!(
Alex Crichton62a0a592017-05-22 13:58:53 -07001067 map!(block, |blk| {
1068 ExprKind::Block(ExprBlock {
1069 unsafety: Unsafety::Normal,
1070 block: blk,
1071 }).into()
1072 })
David Tolnay1978c672016-10-27 22:05:52 -07001073 |
1074 expr
1075 ) >>
1076 (Arm {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001077 rocket_token: tokens::Rocket::default(),
1078 if_token: guard.as_ref().map(|_| tokens::If::default()),
David Tolnay1978c672016-10-27 22:05:52 -07001079 attrs: attrs,
1080 pats: pats,
1081 guard: guard.map(Box::new),
1082 body: Box::new(body),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001083 comma: None,
David Tolnay1978c672016-10-27 22:05:52 -07001084 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07001085 ));
1086
David Tolnay7184b132016-10-30 10:06:37 -07001087 named_ambiguous_expr!(expr_closure -> ExprKind, allow_struct, do_parse!(
David Tolnay89e05672016-10-02 14:39:42 -07001088 capture: capture_by >>
1089 punct!("|") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1091 closure_arg) >>
David Tolnay89e05672016-10-02 14:39:42 -07001092 punct!("|") >>
1093 ret_and_body: alt!(
1094 do_parse!(
1095 punct!("->") >>
1096 ty: ty >>
1097 body: block >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001098 (FunctionRetTy::Ty(ty, tokens::RArrow::default()),
1099 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001100 unsafety: Unsafety::Normal,
1101 block: body,
1102 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001103 )
1104 |
David Tolnay58af3552016-12-22 16:58:07 -05001105 map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001106 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001107 (ExprClosure {
1108 capture: capture,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001109 or1_token: tokens::Or::default(),
1110 or2_token: tokens::Or::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001111 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -07001112 inputs: inputs,
1113 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -07001114 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001115 dot_tokens: None,
1116 fn_token: tokens::Fn::default(),
1117 generics: Generics::default(),
1118 paren_token: tokens::Paren::default(),
David Tolnay89e05672016-10-02 14:39:42 -07001119 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001120 body: Box::new(ret_and_body.1),
1121 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001122 ));
1123
1124 named!(closure_arg -> FnArg, do_parse!(
1125 pat: pat >>
1126 ty: option!(preceded!(punct!(":"), ty)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001127 (ArgCaptured {
1128 pat: pat,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001129 colon_token: tokens::Colon::default(),
1130 ty: ty.unwrap_or_else(|| TyInfer {
1131 underscore_token: tokens::Underscore::default(),
1132 }.into()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001133 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001134 ));
1135
David Tolnay7184b132016-10-30 10:06:37 -07001136 named!(expr_while -> ExprKind, do_parse!(
David Tolnay8b07f372016-09-30 10:28:40 -07001137 lbl: option!(terminated!(label, punct!(":"))) >>
David Tolnay57ffbde2016-09-30 09:38:04 -07001138 keyword!("while") >>
David Tolnaybb6feae2016-10-02 21:25:20 -07001139 cond: cond >>
Gregory Katz3e562cc2016-09-28 18:33:02 -04001140 while_block: block >>
David Tolnaybb6feae2016-10-02 21:25:20 -07001141 (match cond {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001142 Cond::Let(pat, expr, eq_token, let_token) => ExprWhileLet {
1143 eq_token: eq_token,
1144 let_token: let_token,
1145 while_token: tokens::While::default(),
1146 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001147 pat: Box::new(pat),
1148 expr: Box::new(expr),
1149 body: while_block,
1150 label: lbl,
1151 }.into(),
1152 Cond::Expr(cond) => ExprWhile {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001153 while_token: tokens::While::default(),
1154 colon_token: lbl.as_ref().map(|_| tokens::Colon::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001155 cond: Box::new(cond),
1156 body: while_block,
1157 label: lbl,
1158 }.into(),
David Tolnaybb6feae2016-10-02 21:25:20 -07001159 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001160 ));
1161
David Tolnay7184b132016-10-30 10:06:37 -07001162 named!(expr_continue -> ExprKind, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -04001163 keyword!("continue") >>
1164 lbl: option!(label) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001165 (ExprContinue {
1166 continue_token: tokens::Continue::default(),
1167 label: lbl,
1168 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001169 ));
1170
David Tolnay5d55ef72016-12-21 20:20:04 -05001171 named_ambiguous_expr!(expr_break -> ExprKind, allow_struct, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -04001172 keyword!("break") >>
1173 lbl: option!(label) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001174 val: option!(call!(ambiguous_expr, allow_struct, false)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001175 (ExprBreak {
1176 label: lbl,
1177 expr: val.map(Box::new),
1178 break_token: tokens::Break::default(),
1179 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001180 ));
1181
David Tolnay7184b132016-10-30 10:06:37 -07001182 named_ambiguous_expr!(expr_ret -> ExprKind, allow_struct, do_parse!(
Gregory Katzfd6935d2016-09-30 22:51:25 -04001183 keyword!("return") >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001184 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001185 (ExprRet {
1186 expr: ret_value.map(Box::new),
1187 return_token: tokens::Return::default(),
1188 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001189 ));
1190
David Tolnay7184b132016-10-30 10:06:37 -07001191 named!(expr_struct -> ExprKind, do_parse!(
David Tolnay055a7042016-10-02 19:23:54 -07001192 path: path >>
1193 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001194 fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1195 field_value) >>
1196 base: option!(
1197 cond!(fields.is_empty() || fields.trailing_delim(),
1198 do_parse!(
1199 punct!("..") >>
1200 base: expr >>
1201 (base)
1202 )
1203 )
1204 ) >>
David Tolnay055a7042016-10-02 19:23:54 -07001205 punct!("}") >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001206 (ExprStruct {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001207 brace_token: tokens::Brace::default(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001208 path: path,
1209 fields: fields,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001210 dot2_token: base.as_ref().and_then(|b| b.as_ref())
1211 .map(|_| tokens::Dot2::default()),
1212 rest: base.and_then(|b| b.map(Box::new)),
Alex Crichton62a0a592017-05-22 13:58:53 -07001213 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001214 ));
1215
David Tolnay276690f2016-10-30 12:06:59 -07001216 named!(field_value -> FieldValue, alt!(
1217 do_parse!(
1218 name: wordlike >>
1219 punct!(":") >>
1220 value: expr >>
1221 (FieldValue {
1222 ident: name,
1223 expr: value,
1224 is_shorthand: false,
David Tolnay71d93772017-01-22 23:58:24 -08001225 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001226 colon_token: Some(tokens::Colon::default()),
David Tolnay276690f2016-10-30 12:06:59 -07001227 })
1228 )
1229 |
1230 map!(ident, |name: Ident| FieldValue {
1231 ident: name.clone(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001232 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
David Tolnay276690f2016-10-30 12:06:59 -07001233 is_shorthand: true,
David Tolnay71d93772017-01-22 23:58:24 -08001234 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001235 colon_token: None,
David Tolnay055a7042016-10-02 19:23:54 -07001236 })
1237 ));
1238
David Tolnay7184b132016-10-30 10:06:37 -07001239 named!(expr_repeat -> ExprKind, do_parse!(
David Tolnay055a7042016-10-02 19:23:54 -07001240 punct!("[") >>
1241 value: expr >>
1242 punct!(";") >>
1243 times: expr >>
1244 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001245 (ExprRepeat {
1246 expr: Box::new(value),
1247 amt: Box::new(times),
1248 bracket_token: tokens::Bracket::default(),
1249 semi_token: tokens::Semi::default(),
1250 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001251 ));
1252
David Tolnay7184b132016-10-30 10:06:37 -07001253 named!(expr_block -> ExprKind, do_parse!(
David Tolnay7b035912016-12-21 22:42:07 -05001254 rules: unsafety >>
David Tolnay42602292016-10-01 22:25:45 -07001255 b: block >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001256 (ExprBlock {
1257 unsafety: rules,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001258 block: b,
Alex Crichton62a0a592017-05-22 13:58:53 -07001259 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001260 ));
1261
David Tolnay7184b132016-10-30 10:06:37 -07001262 named_ambiguous_expr!(expr_range -> ExprKind, allow_struct, do_parse!(
David Tolnay438c9052016-10-07 23:24:48 -07001263 limits: range_limits >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001264 hi: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001265 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001266 ));
1267
1268 named!(range_limits -> RangeLimits, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001269 punct!("...") => { |_| RangeLimits::Closed(tokens::Dot3::default()) }
David Tolnay438c9052016-10-07 23:24:48 -07001270 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001271 punct!("..") => { |_| RangeLimits::HalfOpen(tokens::Dot2::default()) }
David Tolnay438c9052016-10-07 23:24:48 -07001272 ));
1273
Alex Crichton62a0a592017-05-22 13:58:53 -07001274 named!(expr_path -> ExprKind, map!(qpath, |(qself, path)| {
1275 ExprPath { qself: qself, path: path }.into()
1276 }));
David Tolnay42602292016-10-01 22:25:45 -07001277
David Tolnay7184b132016-10-30 10:06:37 -07001278 named_ambiguous_expr!(expr_addr_of -> ExprKind, allow_struct, do_parse!(
David Tolnay3c2467c2016-10-02 17:55:08 -07001279 punct!("&") >>
1280 mutability: mutability >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001281 expr: ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001282 (ExprAddrOf {
1283 mutbl: mutability,
1284 expr: Box::new(expr),
1285 and_token: tokens::And::default(),
1286 }.into())
David Tolnay3c2467c2016-10-02 17:55:08 -07001287 ));
1288
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001289 named_ambiguous_expr!(and_assign -> (Expr, tokens::Eq), allow_struct, preceded!(
David Tolnayaf2557e2016-10-24 11:52:21 -07001290 punct!("="),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001291 map!(ambiguous_expr!(allow_struct), |t| (t, tokens::Eq::default()))
David Tolnayaf2557e2016-10-24 11:52:21 -07001292 ));
David Tolnay438c9052016-10-07 23:24:48 -07001293
David Tolnayaf2557e2016-10-24 11:52:21 -07001294 named_ambiguous_expr!(and_assign_op -> (BinOp, Expr), allow_struct, tuple!(
1295 assign_op,
1296 ambiguous_expr!(allow_struct)
1297 ));
David Tolnay438c9052016-10-07 23:24:48 -07001298
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001299 named!(and_field -> (Ident, tokens::Dot),
1300 preceded!(punct!("."), map!(ident, |t| (t, tokens::Dot::default()))));
David Tolnay438c9052016-10-07 23:24:48 -07001301
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001302 named!(and_tup_field -> (Lit, tokens::Dot),
1303 preceded!(punct!("."), map!(lit, |l| (l, tokens::Dot::default()))));
David Tolnay438c9052016-10-07 23:24:48 -07001304
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001305 named!(and_index -> (Expr, tokens::Bracket),
1306 map!(delimited!(punct!("["), expr, punct!("]")),
1307 |t| (t, tokens::Bracket::default())));
David Tolnay438c9052016-10-07 23:24:48 -07001308
David Tolnayaf2557e2016-10-24 11:52:21 -07001309 named_ambiguous_expr!(and_range -> (RangeLimits, Option<Expr>), allow_struct, tuple!(
1310 range_limits,
David Tolnay54e854d2016-10-24 12:03:30 -07001311 option!(call!(ambiguous_expr, allow_struct, false))
David Tolnayaf2557e2016-10-24 11:52:21 -07001312 ));
David Tolnay438c9052016-10-07 23:24:48 -07001313
David Tolnay42602292016-10-01 22:25:45 -07001314 named!(pub block -> Block, do_parse!(
David Tolnay939766a2016-09-23 23:48:12 -07001315 punct!("{") >>
1316 stmts: within_block >>
1317 punct!("}") >>
1318 (Block {
1319 stmts: stmts,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001320 brace_token: tokens::Brace::default(),
David Tolnay939766a2016-09-23 23:48:12 -07001321 })
1322 ));
1323
David Tolnay3b9783a2016-10-29 22:37:09 -07001324 named!(pub within_block -> Vec<Stmt>, do_parse!(
David Tolnaycfe55022016-10-02 22:02:27 -07001325 many0!(punct!(";")) >>
David Tolnay1bf19132017-02-19 22:54:25 -08001326 mut standalone: many0!(terminated!(stmt, many0!(punct!(";")))) >>
David Tolnay939766a2016-09-23 23:48:12 -07001327 last: option!(expr) >>
1328 (match last {
David Tolnaycfe55022016-10-02 22:02:27 -07001329 None => standalone,
David Tolnay939766a2016-09-23 23:48:12 -07001330 Some(last) => {
David Tolnaycfe55022016-10-02 22:02:27 -07001331 standalone.push(Stmt::Expr(Box::new(last)));
1332 standalone
David Tolnay939766a2016-09-23 23:48:12 -07001333 }
1334 })
1335 ));
1336
David Tolnay1bf19132017-02-19 22:54:25 -08001337 named!(pub stmt -> Stmt, alt!(
David Tolnay13b3d352016-10-03 00:31:15 -07001338 stmt_mac
1339 |
David Tolnay191e0582016-10-02 18:31:09 -07001340 stmt_local
1341 |
1342 stmt_item
1343 |
David Tolnaycfe55022016-10-02 22:02:27 -07001344 stmt_expr
David Tolnay939766a2016-09-23 23:48:12 -07001345 ));
1346
David Tolnay13b3d352016-10-03 00:31:15 -07001347 named!(stmt_mac -> Stmt, do_parse!(
1348 attrs: many0!(outer_attr) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001349 what: path >>
David Tolnayeea28d62016-10-25 20:44:08 -07001350 punct!("!") >>
1351 // Only parse braces here; paren and bracket will get parsed as
1352 // expression statements
1353 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001354 ts: token_stream >>
David Tolnayeea28d62016-10-25 20:44:08 -07001355 punct!("}") >>
David Tolnay60d48942016-10-30 14:34:52 -07001356 semi: option!(punct!(";")) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001357 (Stmt::Mac(Box::new((
1358 Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001359 path: what,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001360 bang_token: tokens::Bang::default(),
1361 tokens: vec![TokenTree(proc_macro2::TokenTree {
1362 span: Default::default(),
1363 kind: TokenKind::Sequence(Delimiter::Brace, ts),
David Tolnayeea28d62016-10-25 20:44:08 -07001364 })],
1365 },
David Tolnay60d48942016-10-30 14:34:52 -07001366 if semi.is_some() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001367 MacStmtStyle::Semicolon(tokens::Semi::default())
David Tolnay60d48942016-10-30 14:34:52 -07001368 } else {
1369 MacStmtStyle::Braces
1370 },
David Tolnayeea28d62016-10-25 20:44:08 -07001371 attrs,
1372 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001373 ));
1374
David Tolnay191e0582016-10-02 18:31:09 -07001375 named!(stmt_local -> Stmt, do_parse!(
1376 attrs: many0!(outer_attr) >>
1377 keyword!("let") >>
1378 pat: pat >>
1379 ty: option!(preceded!(punct!(":"), ty)) >>
1380 init: option!(preceded!(punct!("="), expr)) >>
1381 punct!(";") >>
1382 (Stmt::Local(Box::new(Local {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001383 let_token: tokens::Let::default(),
1384 semi_token: tokens::Semi::default(),
1385 colon_token: ty.as_ref().map(|_| tokens::Colon::default()),
1386 eq_token: init.as_ref().map(|_| tokens::Eq::default()),
David Tolnay191e0582016-10-02 18:31:09 -07001387 pat: Box::new(pat),
1388 ty: ty.map(Box::new),
1389 init: init.map(Box::new),
1390 attrs: attrs,
1391 })))
1392 ));
1393
1394 named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i))));
1395
David Tolnaycfe55022016-10-02 22:02:27 -07001396 fn requires_semi(e: &Expr) -> bool {
David Tolnay7184b132016-10-30 10:06:37 -07001397 match e.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001398 ExprKind::If(_) |
1399 ExprKind::IfLet(_) |
1400 ExprKind::While(_) |
1401 ExprKind::WhileLet(_) |
1402 ExprKind::ForLoop(_) |
1403 ExprKind::Loop(_) |
1404 ExprKind::Match(_) |
1405 ExprKind::Block(_) => false,
David Tolnaycfe55022016-10-02 22:02:27 -07001406
1407 _ => true,
1408 }
1409 }
1410
1411 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay7184b132016-10-30 10:06:37 -07001412 attrs: many0!(outer_attr) >>
1413 mut e: expr >>
David Tolnaycfe55022016-10-02 22:02:27 -07001414 semi: option!(punct!(";")) >>
David Tolnay7184b132016-10-30 10:06:37 -07001415 ({
1416 e.attrs = attrs;
1417 if semi.is_some() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001418 Stmt::Semi(Box::new(e), tokens::Semi::default())
David Tolnay092dcb02016-10-30 10:14:14 -07001419 } else if requires_semi(&e) {
David Tolnay7184b132016-10-30 10:06:37 -07001420 return Error;
1421 } else {
1422 Stmt::Expr(Box::new(e))
1423 }
David Tolnaycfe55022016-10-02 22:02:27 -07001424 })
David Tolnay939766a2016-09-23 23:48:12 -07001425 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001426
David Tolnay42602292016-10-01 22:25:45 -07001427 named!(pub pat -> Pat, alt!(
David Tolnayfbb73232016-10-03 01:00:06 -07001428 pat_wild // must be before pat_ident
1429 |
1430 pat_box // must be before pat_ident
David Tolnayb4ad3b52016-10-01 21:58:13 -07001431 |
David Tolnay8b308c22016-10-03 01:24:10 -07001432 pat_range // must be before pat_lit
1433 |
David Tolnayaa610942016-10-03 22:22:49 -07001434 pat_tuple_struct // must be before pat_ident
1435 |
David Tolnay8d9e81a2016-10-03 22:36:32 -07001436 pat_struct // must be before pat_ident
1437 |
David Tolnayaa610942016-10-03 22:22:49 -07001438 pat_mac // must be before pat_ident
1439 |
David Tolnaybcdbdd02016-10-24 23:05:02 -07001440 pat_lit // must be before pat_ident
1441 |
David Tolnay8b308c22016-10-03 01:24:10 -07001442 pat_ident // must be before pat_path
David Tolnay9636c052016-10-02 17:11:17 -07001443 |
1444 pat_path
David Tolnayfbb73232016-10-03 01:00:06 -07001445 |
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001446 map!(pat_tuple, |t: PatTuple| t.into())
David Tolnayffdb97f2016-10-03 01:28:33 -07001447 |
1448 pat_ref
David Tolnay435a9a82016-10-29 13:47:20 -07001449 |
1450 pat_slice
David Tolnayb4ad3b52016-10-01 21:58:13 -07001451 ));
1452
David Tolnay84aa0752016-10-02 23:01:13 -07001453 named!(pat_mac -> Pat, map!(mac, Pat::Mac));
1454
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001455 named!(pat_wild -> Pat, map!(keyword!("_"), |_| {
1456 PatWild { underscore_token: tokens::Underscore::default() }.into()
1457 }));
David Tolnayb4ad3b52016-10-01 21:58:13 -07001458
David Tolnayfbb73232016-10-03 01:00:06 -07001459 named!(pat_box -> Pat, do_parse!(
1460 keyword!("box") >>
1461 pat: pat >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001462 (PatBox {
1463 pat: Box::new(pat),
1464 box_token: tokens::Box::default(),
1465 }.into())
David Tolnayfbb73232016-10-03 01:00:06 -07001466 ));
1467
David Tolnayb4ad3b52016-10-01 21:58:13 -07001468 named!(pat_ident -> Pat, do_parse!(
1469 mode: option!(keyword!("ref")) >>
1470 mutability: mutability >>
David Tolnay8d4d2fa2016-10-25 22:08:07 -07001471 name: alt!(
1472 ident
1473 |
1474 keyword!("self") => { Into::into }
1475 ) >>
David Tolnay1f16b602017-02-07 20:06:55 -05001476 not!(punct!("<")) >>
1477 not!(punct!("::")) >>
David Tolnayb4ad3b52016-10-01 21:58:13 -07001478 subpat: option!(preceded!(punct!("@"), pat)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001479 (PatIdent {
1480 mode: if mode.is_some() {
1481 BindingMode::ByRef(tokens::Ref::default(), mutability)
David Tolnayb4ad3b52016-10-01 21:58:13 -07001482 } else {
1483 BindingMode::ByValue(mutability)
1484 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001485 ident: name,
1486 at_token: subpat.as_ref().map(|_| tokens::At::default()),
1487 subpat: subpat.map(Box::new),
1488 }.into())
David Tolnayb4ad3b52016-10-01 21:58:13 -07001489 ));
1490
David Tolnayaa610942016-10-03 22:22:49 -07001491 named!(pat_tuple_struct -> Pat, do_parse!(
1492 path: path >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001493 tuple: pat_tuple >>
1494 (PatTupleStruct {
1495 path: path,
1496 pat: tuple,
1497 }.into())
David Tolnayaa610942016-10-03 22:22:49 -07001498 ));
1499
David Tolnay8d9e81a2016-10-03 22:36:32 -07001500 named!(pat_struct -> Pat, do_parse!(
1501 path: path >>
1502 punct!("{") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001503 fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1504 field_pat) >>
1505 base: option!(
1506 cond!(fields.is_empty() || fields.trailing_delim(),
1507 punct!(".."))
1508 ) >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001509 punct!("}") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001510 (PatStruct {
1511 path: path,
1512 fields: fields,
1513 brace_token: tokens::Brace::default(),
1514 dot2_token: base.and_then(|m| m).map(|_| tokens::Dot2::default()),
1515 }.into())
David Tolnay8d9e81a2016-10-03 22:36:32 -07001516 ));
1517
1518 named!(field_pat -> FieldPat, alt!(
1519 do_parse!(
David Tolnay39039442016-10-30 11:25:21 -07001520 ident: wordlike >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001521 punct!(":") >>
1522 pat: pat >>
1523 (FieldPat {
1524 ident: ident,
1525 pat: Box::new(pat),
1526 is_shorthand: false,
David Tolnay71d93772017-01-22 23:58:24 -08001527 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001528 colon_token: Some(tokens::Colon::default()),
David Tolnay8d9e81a2016-10-03 22:36:32 -07001529 })
1530 )
1531 |
1532 do_parse!(
David Tolnayda167382016-10-30 13:34:09 -07001533 boxed: option!(keyword!("box")) >>
David Tolnay8d9e81a2016-10-03 22:36:32 -07001534 mode: option!(keyword!("ref")) >>
1535 mutability: mutability >>
1536 ident: ident >>
David Tolnayda167382016-10-30 13:34:09 -07001537 ({
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001538 let mut pat: Pat = PatIdent {
1539 mode: if mode.is_some() {
1540 BindingMode::ByRef(tokens::Ref::default(), mutability)
David Tolnay8d9e81a2016-10-03 22:36:32 -07001541 } else {
1542 BindingMode::ByValue(mutability)
1543 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001544 ident: ident.clone(),
1545 subpat: None,
1546 at_token: None,
1547 }.into();
David Tolnayda167382016-10-30 13:34:09 -07001548 if boxed.is_some() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001549 pat = PatBox {
1550 pat: Box::new(pat),
1551 box_token: tokens::Box::default(),
1552 }.into();
David Tolnayda167382016-10-30 13:34:09 -07001553 }
1554 FieldPat {
1555 ident: ident,
1556 pat: Box::new(pat),
1557 is_shorthand: true,
David Tolnay71d93772017-01-22 23:58:24 -08001558 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001559 colon_token: None,
David Tolnayda167382016-10-30 13:34:09 -07001560 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001561 })
1562 )
1563 ));
1564
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001565 named!(pat_path -> Pat, map!(qpath, |(qself, path)| {
1566 PatPath { qself: qself, path: path }.into()
1567 }));
David Tolnay9636c052016-10-02 17:11:17 -07001568
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001569 named!(pat_tuple -> PatTuple, do_parse!(
David Tolnayfbb73232016-10-03 01:00:06 -07001570 punct!("(") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001571 mut elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1572 pat) >>
1573 dotdot: map!(cond!(
1574 elems.is_empty() || elems.trailing_delim(),
1575 option!(do_parse!(
1576 punct!("..") >>
1577 trailing: option!(punct!(",")) >>
1578 (trailing.is_some())
1579 ))
1580 ), |x: Option<_>| x.and_then(|x| x)) >>
1581 rest: cond!(dotdot == Some(true),
1582 terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1583 pat)) >>
David Tolnayfbb73232016-10-03 01:00:06 -07001584 punct!(")") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001585 (PatTuple {
1586 paren_token: tokens::Paren::default(),
1587 dots_pos: dotdot.map(|_| elems.len()),
1588 dot2_token: dotdot.map(|_| tokens::Dot2::default()),
1589 comma_token: dotdot.and_then(|b| {
1590 if b {
1591 Some(tokens::Comma::default())
1592 } else {
1593 None
1594 }
1595 }),
1596 pats: {
1597 if let Some(rest) = rest {
1598 for elem in rest.into_iter() {
1599 elems.push(elem);
1600 }
1601 }
1602 elems
1603 },
David Tolnayfbb73232016-10-03 01:00:06 -07001604 })
1605 ));
1606
David Tolnayffdb97f2016-10-03 01:28:33 -07001607 named!(pat_ref -> Pat, do_parse!(
1608 punct!("&") >>
1609 mutability: mutability >>
1610 pat: pat >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001611 (PatRef {
1612 pat: Box::new(pat),
1613 mutbl: mutability,
1614 and_token: tokens::And::default(),
1615 }.into())
David Tolnayffdb97f2016-10-03 01:28:33 -07001616 ));
1617
David Tolnayef40da42016-10-30 01:19:04 -07001618 named!(pat_lit -> Pat, do_parse!(
1619 lit: pat_lit_expr >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001620 (if let ExprKind::Path(_) = lit.node {
David Tolnayef40da42016-10-30 01:19:04 -07001621 return IResult::Error; // these need to be parsed by pat_path
1622 } else {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001623 PatLit {
1624 expr: Box::new(lit),
1625 }.into()
David Tolnayef40da42016-10-30 01:19:04 -07001626 })
1627 ));
David Tolnaye1310902016-10-29 23:40:00 -07001628
1629 named!(pat_range -> Pat, do_parse!(
David Tolnay2cfddc62016-10-30 01:03:27 -07001630 lo: pat_lit_expr >>
Arnavion1992e2f2017-04-25 01:47:46 -07001631 limits: range_limits >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001632 hi: pat_lit_expr >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001633 (PatRange {
1634 lo: Box::new(lo),
1635 hi: Box::new(hi),
1636 limits: limits,
1637 }.into())
David Tolnaye1310902016-10-29 23:40:00 -07001638 ));
1639
David Tolnay2cfddc62016-10-30 01:03:27 -07001640 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001641 neg: option!(punct!("-")) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001642 v: alt!(
David Tolnay7184b132016-10-30 10:06:37 -07001643 lit => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07001644 |
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 path => { |p| ExprPath { qself: None, path: p }.into() }
David Tolnay2cfddc62016-10-30 01:03:27 -07001646 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001647 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07001648 ExprKind::Unary(ExprUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001649 op: UnOp::Neg(tokens::Sub::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001650 expr: Box::new(v.into())
1651 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001652 } else {
David Tolnay7184b132016-10-30 10:06:37 -07001653 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001654 })
1655 ));
David Tolnay8b308c22016-10-03 01:24:10 -07001656
David Tolnay435a9a82016-10-29 13:47:20 -07001657 named!(pat_slice -> Pat, do_parse!(
1658 punct!("[") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001659 mut before: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1660 pat) >>
1661 middle: option!(do_parse!(
David Tolnay435a9a82016-10-29 13:47:20 -07001662 punct!("..") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001663 trailing: option!(punct!(",")) >>
1664 (trailing.is_some())
David Tolnay435a9a82016-10-29 13:47:20 -07001665 )) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001666 after: cond!(
1667 match middle {
1668 Some(trailing) => trailing,
1669 _ => false,
1670 },
1671 terminated_list!(map!(punct!(","), |_| tokens::Comma::default()),
1672 pat)
1673 ) >>
David Tolnay435a9a82016-10-29 13:47:20 -07001674 punct!("]") >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001675 (PatSlice {
1676 dot2_token: middle.as_ref().map(|_| tokens::Dot2::default()),
1677 comma_token: {
1678 let trailing = middle.unwrap_or(false);
1679 if trailing {Some(tokens::Comma::default())} else {None}
1680 },
1681 bracket_token: tokens::Bracket::default(),
1682 middle: middle.and_then(|_| {
1683 if !before.is_empty() && !before.trailing_delim() {
1684 Some(Box::new(before.pop().unwrap().into_item()))
1685 } else {
1686 None
David Tolnaye1f13c32016-10-29 23:34:40 -07001687 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001688 }),
1689 front: before,
1690 back: after.unwrap_or_default(),
1691 }.into())
David Tolnay435a9a82016-10-29 13:47:20 -07001692 ));
1693
David Tolnay89e05672016-10-02 14:39:42 -07001694 named!(capture_by -> CaptureBy, alt!(
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001695 keyword!("move") => { |_| CaptureBy::Value(tokens::Move::default()) }
David Tolnay89e05672016-10-02 14:39:42 -07001696 |
1697 epsilon!() => { |_| CaptureBy::Ref }
1698 ));
1699
David Tolnay8b07f372016-09-30 10:28:40 -07001700 named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -07001701}
1702
David Tolnayf4bbbd92016-09-23 14:41:55 -07001703#[cfg(feature = "printing")]
1704mod printing {
1705 use super::*;
David Tolnay13b3d352016-10-03 00:31:15 -07001706 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07001707 use quote::{Tokens, ToTokens};
1708
1709 impl ToTokens for Expr {
1710 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07001711 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001712 self.node.to_tokens(tokens)
1713 }
1714 }
1715
1716 impl ToTokens for ExprBox {
1717 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001718 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001719 self.expr.to_tokens(tokens);
1720 }
1721 }
1722
1723 impl ToTokens for ExprInPlace {
1724 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001725 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001726 self.place.to_tokens(tokens);
1727 self.value.to_tokens(tokens);
1728 }
1729 }
1730
1731 impl ToTokens for ExprArray {
1732 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001733 self.bracket_token.surround(tokens, |tokens| {
1734 self.exprs.to_tokens(tokens);
1735 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001736 }
1737 }
1738
1739 impl ToTokens for ExprCall {
1740 fn to_tokens(&self, tokens: &mut Tokens) {
1741 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001742 self.paren_token.surround(tokens, |tokens| {
1743 self.args.to_tokens(tokens);
1744 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001745 }
1746 }
1747
1748 impl ToTokens for ExprMethodCall {
1749 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001750 self.expr.to_tokens(tokens);
1751 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001752 self.method.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001753 self.colon2_token.to_tokens(tokens);
1754 self.lt_token.to_tokens(tokens);
1755 self.typarams.to_tokens(tokens);
1756 self.gt_token.to_tokens(tokens);
1757 self.paren_token.surround(tokens, |tokens| {
1758 self.args.to_tokens(tokens);
1759 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001760 }
1761 }
1762
1763 impl ToTokens for ExprTup {
1764 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001765 self.paren_token.surround(tokens, |tokens| {
1766 self.args.to_tokens(tokens);
1767 self.lone_comma.to_tokens(tokens);
1768 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001769 }
1770 }
1771
1772 impl ToTokens for ExprBinary {
1773 fn to_tokens(&self, tokens: &mut Tokens) {
1774 self.left.to_tokens(tokens);
1775 self.op.to_tokens(tokens);
1776 self.right.to_tokens(tokens);
1777 }
1778 }
1779
1780 impl ToTokens for ExprUnary {
1781 fn to_tokens(&self, tokens: &mut Tokens) {
1782 self.op.to_tokens(tokens);
1783 self.expr.to_tokens(tokens);
1784 }
1785 }
1786
1787 impl ToTokens for ExprCast {
1788 fn to_tokens(&self, tokens: &mut Tokens) {
1789 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001790 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001791 self.ty.to_tokens(tokens);
1792 }
1793 }
1794
1795 impl ToTokens for ExprType {
1796 fn to_tokens(&self, tokens: &mut Tokens) {
1797 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001798 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001799 self.ty.to_tokens(tokens);
1800 }
1801 }
1802
1803 impl ToTokens for ExprIf {
1804 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001805 self.if_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001806 self.cond.to_tokens(tokens);
1807 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001808 self.else_token.to_tokens(tokens);
1809 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001810 }
1811 }
1812
1813 impl ToTokens for ExprIfLet {
1814 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001815 self.if_token.to_tokens(tokens);
1816 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001817 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001818 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001819 self.expr.to_tokens(tokens);
1820 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 self.else_token.to_tokens(tokens);
1822 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001823 }
1824 }
1825
1826 impl ToTokens for ExprWhile {
1827 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001828 self.label.to_tokens(tokens);
1829 self.colon_token.to_tokens(tokens);
1830 self.while_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001831 self.cond.to_tokens(tokens);
1832 self.body.to_tokens(tokens);
1833 }
1834 }
1835
1836 impl ToTokens for ExprWhileLet {
1837 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001838 self.label.to_tokens(tokens);
1839 self.colon_token.to_tokens(tokens);
1840 self.while_token.to_tokens(tokens);
1841 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001842 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001843 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001844 self.expr.to_tokens(tokens);
1845 self.body.to_tokens(tokens);
1846 }
1847 }
1848
1849 impl ToTokens for ExprForLoop {
1850 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001851 self.label.to_tokens(tokens);
1852 self.colon_token.to_tokens(tokens);
1853 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001854 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001855 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001856 self.expr.to_tokens(tokens);
1857 self.body.to_tokens(tokens);
1858 }
1859 }
1860
1861 impl ToTokens for ExprLoop {
1862 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001863 self.label.to_tokens(tokens);
1864 self.colon_token.to_tokens(tokens);
1865 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001866 self.body.to_tokens(tokens);
1867 }
1868 }
1869
1870 impl ToTokens for ExprMatch {
1871 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001872 self.match_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001873 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001874 self.brace_token.surround(tokens, |tokens| {
1875 tokens.append_all(&self.arms);
1876 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001877 }
1878 }
1879
1880 impl ToTokens for ExprCatch {
1881 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001882 self.do_token.to_tokens(tokens);
1883 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001884 self.block.to_tokens(tokens);
1885 }
1886 }
1887
1888 impl ToTokens for ExprClosure {
1889 fn to_tokens(&self, tokens: &mut Tokens) {
1890 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001891 self.or1_token.to_tokens(tokens);
1892 for item in self.decl.inputs.iter() {
1893 match **item.item() {
1894 FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001895 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07001896 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001897 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07001898 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001899 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07001900 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001901 self.or2_token.to_tokens(tokens);
1902 self.decl.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001903 self.body.to_tokens(tokens);
1904 }
1905 }
1906
1907 impl ToTokens for ExprBlock {
1908 fn to_tokens(&self, tokens: &mut Tokens) {
1909 self.unsafety.to_tokens(tokens);
1910 self.block.to_tokens(tokens);
1911 }
1912 }
1913
1914 impl ToTokens for ExprAssign {
1915 fn to_tokens(&self, tokens: &mut Tokens) {
1916 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001917 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001918 self.right.to_tokens(tokens);
1919 }
1920 }
1921
1922 impl ToTokens for ExprAssignOp {
1923 fn to_tokens(&self, tokens: &mut Tokens) {
1924 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001925 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001926 self.right.to_tokens(tokens);
1927 }
1928 }
1929
1930 impl ToTokens for ExprField {
1931 fn to_tokens(&self, tokens: &mut Tokens) {
1932 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001933 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001934 self.field.to_tokens(tokens);
1935 }
1936 }
1937
1938 impl ToTokens for ExprTupField {
1939 fn to_tokens(&self, tokens: &mut Tokens) {
1940 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001941 self.dot_token.to_tokens(tokens);
1942 self.field.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001943 }
1944 }
1945
1946 impl ToTokens for ExprIndex {
1947 fn to_tokens(&self, tokens: &mut Tokens) {
1948 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001949 self.bracket_token.surround(tokens, |tokens| {
1950 self.index.to_tokens(tokens);
1951 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001952 }
1953 }
1954
1955 impl ToTokens for ExprRange {
1956 fn to_tokens(&self, tokens: &mut Tokens) {
1957 self.from.to_tokens(tokens);
1958 self.limits.to_tokens(tokens);
1959 self.to.to_tokens(tokens);
1960 }
1961 }
1962
1963 impl ToTokens for ExprPath {
1964 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001965 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07001966 }
1967 }
1968
1969 impl ToTokens for ExprAddrOf {
1970 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001971 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001972 self.mutbl.to_tokens(tokens);
1973 self.expr.to_tokens(tokens);
1974 }
1975 }
1976
1977 impl ToTokens for ExprBreak {
1978 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001979 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001980 self.label.to_tokens(tokens);
1981 self.expr.to_tokens(tokens);
1982 }
1983 }
1984
1985 impl ToTokens for ExprContinue {
1986 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001987 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001988 self.label.to_tokens(tokens);
1989 }
1990 }
1991
1992 impl ToTokens for ExprRet {
1993 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001994 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001995 self.expr.to_tokens(tokens);
1996 }
1997 }
1998
1999 impl ToTokens for ExprStruct {
2000 fn to_tokens(&self, tokens: &mut Tokens) {
2001 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002002 self.brace_token.surround(tokens, |tokens| {
2003 self.fields.to_tokens(tokens);
2004 self.dot2_token.to_tokens(tokens);
2005 self.rest.to_tokens(tokens);
2006 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002007 }
2008 }
2009
2010 impl ToTokens for ExprRepeat {
2011 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002012 self.bracket_token.surround(tokens, |tokens| {
2013 self.expr.to_tokens(tokens);
2014 self.semi_token.to_tokens(tokens);
2015 self.amt.to_tokens(tokens);
2016 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002017 }
2018 }
2019
2020 impl ToTokens for ExprParen {
2021 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002022 self.paren_token.surround(tokens, |tokens| {
2023 self.expr.to_tokens(tokens);
2024 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002025 }
2026 }
2027
2028 impl ToTokens for ExprTry {
2029 fn to_tokens(&self, tokens: &mut Tokens) {
2030 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002031 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002032 }
2033 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002034
David Tolnay055a7042016-10-02 19:23:54 -07002035 impl ToTokens for FieldValue {
2036 fn to_tokens(&self, tokens: &mut Tokens) {
2037 self.ident.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002038 if !self.is_shorthand {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002039 self.colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002040 self.expr.to_tokens(tokens);
2041 }
David Tolnay055a7042016-10-02 19:23:54 -07002042 }
2043 }
2044
David Tolnayb4ad3b52016-10-01 21:58:13 -07002045 impl ToTokens for Arm {
2046 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002047 tokens.append_all(&self.attrs);
2048 self.pats.to_tokens(tokens);
2049 self.if_token.to_tokens(tokens);
2050 self.guard.to_tokens(tokens);
2051 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002052 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002053 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002054 }
2055 }
2056
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002057 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002058 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002059 self.underscore_token.to_tokens(tokens);
2060 }
2061 }
2062
2063 impl ToTokens for PatIdent {
2064 fn to_tokens(&self, tokens: &mut Tokens) {
2065 self.mode.to_tokens(tokens);
2066 self.ident.to_tokens(tokens);
2067 self.at_token.to_tokens(tokens);
2068 self.subpat.to_tokens(tokens);
2069 }
2070 }
2071
2072 impl ToTokens for PatStruct {
2073 fn to_tokens(&self, tokens: &mut Tokens) {
2074 self.path.to_tokens(tokens);
2075 self.brace_token.surround(tokens, |tokens| {
2076 self.fields.to_tokens(tokens);
2077 self.dot2_token.to_tokens(tokens);
2078 });
2079 }
2080 }
2081
2082 impl ToTokens for PatTupleStruct {
2083 fn to_tokens(&self, tokens: &mut Tokens) {
2084 self.path.to_tokens(tokens);
2085 self.pat.to_tokens(tokens);
2086 }
2087 }
2088
2089 impl ToTokens for PatPath {
2090 fn to_tokens(&self, tokens: &mut Tokens) {
2091 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2092 }
2093 }
2094
2095 impl ToTokens for PatTuple {
2096 fn to_tokens(&self, tokens: &mut Tokens) {
2097 self.paren_token.surround(tokens, |tokens| {
2098 for (i, token) in self.pats.iter().enumerate() {
2099 if Some(i) == self.dots_pos {
2100 self.dot2_token.to_tokens(tokens);
2101 self.comma_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002102 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002103 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002104 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002105
2106 if Some(self.pats.len()) == self.dots_pos {
2107 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002108 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002109 });
2110 }
2111 }
2112
2113 impl ToTokens for PatBox {
2114 fn to_tokens(&self, tokens: &mut Tokens) {
2115 self.box_token.to_tokens(tokens);
2116 self.pat.to_tokens(tokens);
2117 }
2118 }
2119
2120 impl ToTokens for PatRef {
2121 fn to_tokens(&self, tokens: &mut Tokens) {
2122 self.and_token.to_tokens(tokens);
2123 self.mutbl.to_tokens(tokens);
2124 self.pat.to_tokens(tokens);
2125 }
2126 }
2127
2128 impl ToTokens for PatLit {
2129 fn to_tokens(&self, tokens: &mut Tokens) {
2130 self.expr.to_tokens(tokens);
2131 }
2132 }
2133
2134 impl ToTokens for PatRange {
2135 fn to_tokens(&self, tokens: &mut Tokens) {
2136 self.lo.to_tokens(tokens);
2137 self.limits.to_tokens(tokens);
2138 self.hi.to_tokens(tokens);
2139 }
2140 }
2141
2142 impl ToTokens for PatSlice {
2143 fn to_tokens(&self, tokens: &mut Tokens) {
2144 self.bracket_token.surround(tokens, |tokens| {
2145 self.front.to_tokens(tokens);
2146 self.middle.to_tokens(tokens);
2147 self.dot2_token.to_tokens(tokens);
2148 self.comma_token.to_tokens(tokens);
2149 self.back.to_tokens(tokens);
2150 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002151 }
2152 }
2153
Arnavion1992e2f2017-04-25 01:47:46 -07002154 impl ToTokens for RangeLimits {
2155 fn to_tokens(&self, tokens: &mut Tokens) {
2156 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002157 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2158 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
Arnavion1992e2f2017-04-25 01:47:46 -07002159 }
2160 }
2161 }
2162
David Tolnay8d9e81a2016-10-03 22:36:32 -07002163 impl ToTokens for FieldPat {
2164 fn to_tokens(&self, tokens: &mut Tokens) {
2165 if !self.is_shorthand {
2166 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002167 self.colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002168 }
2169 self.pat.to_tokens(tokens);
2170 }
2171 }
2172
David Tolnayb4ad3b52016-10-01 21:58:13 -07002173 impl ToTokens for BindingMode {
2174 fn to_tokens(&self, tokens: &mut Tokens) {
2175 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002176 BindingMode::ByRef(ref t, ref m) => {
2177 t.to_tokens(tokens);
2178 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002179 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002180 BindingMode::ByValue(ref m) => {
2181 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002182 }
2183 }
2184 }
2185 }
David Tolnay42602292016-10-01 22:25:45 -07002186
David Tolnay89e05672016-10-02 14:39:42 -07002187 impl ToTokens for CaptureBy {
2188 fn to_tokens(&self, tokens: &mut Tokens) {
2189 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002190 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002191 CaptureBy::Ref => {
2192 // nothing
2193 }
David Tolnay89e05672016-10-02 14:39:42 -07002194 }
2195 }
2196 }
2197
David Tolnay42602292016-10-01 22:25:45 -07002198 impl ToTokens for Block {
2199 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002200 self.brace_token.surround(tokens, |tokens| {
2201 tokens.append_all(&self.stmts);
2202 });
David Tolnay42602292016-10-01 22:25:45 -07002203 }
2204 }
2205
David Tolnay42602292016-10-01 22:25:45 -07002206 impl ToTokens for Stmt {
2207 fn to_tokens(&self, tokens: &mut Tokens) {
2208 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002209 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002210 Stmt::Item(ref item) => item.to_tokens(tokens),
2211 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002212 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002213 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002214 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002215 }
David Tolnay13b3d352016-10-03 00:31:15 -07002216 Stmt::Mac(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002217 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002218 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002219 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07002220 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002221 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002222 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2223 // no semicolon
2224 }
David Tolnay13b3d352016-10-03 00:31:15 -07002225 }
2226 }
David Tolnay42602292016-10-01 22:25:45 -07002227 }
2228 }
2229 }
David Tolnay191e0582016-10-02 18:31:09 -07002230
2231 impl ToTokens for Local {
2232 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002233 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002234 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002235 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002236 self.colon_token.to_tokens(tokens);
2237 self.ty.to_tokens(tokens);
2238 self.eq_token.to_tokens(tokens);
2239 self.init.to_tokens(tokens);
2240 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002241 }
2242 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002243}