blob: 23f2faa09e1e6366709dc40d36cf742a84fb0613 [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 Crichton954046c2017-05-30 21:49:42 -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>,
Alex Crichton954046c2017-05-30 21:49:42 -0700488 pub box_token: tokens::Box_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700489 }),
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 Crichton954046c2017-05-30 21:49:42 -0700590 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700591
Alex Crichton954046c2017-05-30 21:49:42 -0700592 use proc_macro2::{TokenTree, TokenStream, TokenKind, Delimiter};
593 use synom::{IResult, Synom};
594 use synom::tokens::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595
David Tolnayaf2557e2016-10-24 11:52:21 -0700596 // Struct literals are ambiguous in certain positions
597 // https://github.com/rust-lang/rfcs/pull/92
598 macro_rules! named_ambiguous_expr {
599 ($name:ident -> $o:ty, $allow_struct:ident, $submac:ident!( $($args:tt)* )) => {
Michael Layzell416724e2017-05-24 21:12:34 -0400600 fn $name(i: &[$crate::synom::TokenTree], $allow_struct: bool)
601 -> $crate::synom::IResult<&[$crate::synom::TokenTree], $o> {
David Tolnayaf2557e2016-10-24 11:52:21 -0700602 $submac!(i, $($args)*)
603 }
604 };
605 }
606
607 macro_rules! ambiguous_expr {
608 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700609 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700610 };
611 }
612
Alex Crichton954046c2017-05-30 21:49:42 -0700613 impl Synom for Expr {
614 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
615 ambiguous_expr!(input, true)
616 }
617
618 fn description() -> Option<&'static str> {
619 Some("expression")
620 }
621 }
622
David Tolnayaf2557e2016-10-24 11:52:21 -0700623
624 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
625
David Tolnay02a8d472017-02-19 12:59:44 -0800626 #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
Michael Layzell416724e2017-05-24 21:12:34 -0400627 fn ambiguous_expr(i: &[synom::TokenTree],
628 allow_struct: bool,
629 allow_block: bool)
630 -> IResult<&[synom::TokenTree], Expr> {
Alex Crichton954046c2017-05-30 21:49:42 -0700631 do_parse! {
David Tolnay54e854d2016-10-24 12:03:30 -0700632 i,
633 mut e: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -0700634 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
David Tolnay54e854d2016-10-24 12:03:30 -0700635 |
Alex Crichton954046c2017-05-30 21:49:42 -0700636 // must be before expr_path
637 cond_reduce!(allow_struct, map!(syn!(ExprStruct), ExprKind::Struct))
David Tolnay54e854d2016-10-24 12:03:30 -0700638 |
Alex Crichton954046c2017-05-30 21:49:42 -0700639 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
David Tolnay54e854d2016-10-24 12:03:30 -0700640 |
Alex Crichton954046c2017-05-30 21:49:42 -0700641 syn!(Mac) => { ExprKind::Mac } // must be before expr_path
David Tolnay54e854d2016-10-24 12:03:30 -0700642 |
David Tolnay5d55ef72016-12-21 20:20:04 -0500643 call!(expr_break, allow_struct) // must be before expr_path
David Tolnay54e854d2016-10-24 12:03:30 -0700644 |
Alex Crichton954046c2017-05-30 21:49:42 -0700645 syn!(ExprContinue) => { ExprKind::Continue } // must be before expr_path
David Tolnay54e854d2016-10-24 12:03:30 -0700646 |
647 call!(expr_ret, allow_struct) // must be before expr_path
648 |
649 call!(expr_box, allow_struct)
650 |
Alex Crichton954046c2017-05-30 21:49:42 -0700651 syn!(ExprInPlace) => { ExprKind::InPlace }
David Tolnay6696c3e2016-10-30 11:45:10 -0700652 |
Alex Crichton954046c2017-05-30 21:49:42 -0700653 syn!(ExprArray) => { ExprKind::Array }
David Tolnay54e854d2016-10-24 12:03:30 -0700654 |
Alex Crichton954046c2017-05-30 21:49:42 -0700655 syn!(ExprTup) => { ExprKind::Tup }
David Tolnay54e854d2016-10-24 12:03:30 -0700656 |
657 call!(expr_unary, allow_struct)
658 |
Alex Crichton954046c2017-05-30 21:49:42 -0700659 syn!(ExprIf) => { ExprKind::If }
David Tolnay54e854d2016-10-24 12:03:30 -0700660 |
Alex Crichton954046c2017-05-30 21:49:42 -0700661 syn!(ExprIfLet) => { ExprKind::IfLet }
David Tolnay54e854d2016-10-24 12:03:30 -0700662 |
Alex Crichton954046c2017-05-30 21:49:42 -0700663 syn!(ExprWhile) => { ExprKind::While }
David Tolnay54e854d2016-10-24 12:03:30 -0700664 |
Alex Crichton954046c2017-05-30 21:49:42 -0700665 syn!(ExprWhileLet) => { ExprKind::WhileLet }
David Tolnay54e854d2016-10-24 12:03:30 -0700666 |
Alex Crichton954046c2017-05-30 21:49:42 -0700667 syn!(ExprForLoop) => { ExprKind::ForLoop }
David Tolnay54e854d2016-10-24 12:03:30 -0700668 |
Alex Crichton954046c2017-05-30 21:49:42 -0700669 syn!(ExprLoop) => { ExprKind::Loop }
670 |
671 syn!(ExprMatch) => { ExprKind::Match }
672 |
673 syn!(ExprCatch) => { ExprKind::Catch }
Arnavion02ef13f2017-04-25 00:54:31 -0700674 |
David Tolnay54e854d2016-10-24 12:03:30 -0700675 call!(expr_closure, allow_struct)
676 |
Alex Crichton954046c2017-05-30 21:49:42 -0700677 cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
David Tolnay54e854d2016-10-24 12:03:30 -0700678 |
679 call!(expr_range, allow_struct)
680 |
Alex Crichton954046c2017-05-30 21:49:42 -0700681 syn!(ExprPath) => { ExprKind::Path }
David Tolnay54e854d2016-10-24 12:03:30 -0700682 |
683 call!(expr_addr_of, allow_struct)
684 |
Alex Crichton954046c2017-05-30 21:49:42 -0700685 syn!(ExprRepeat) => { ExprKind::Repeat }
David Tolnay54e854d2016-10-24 12:03:30 -0700686 ) >>
687 many0!(alt!(
688 tap!(args: and_call => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700689 let (args, paren) = args;
Alex Crichton62a0a592017-05-22 13:58:53 -0700690 e = ExprCall {
691 func: Box::new(e.into()),
692 args: args,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700693 paren_token: paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700694 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700695 })
696 |
697 tap!(more: and_method_call => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700698 let mut call = more;
699 call.expr = Box::new(e.into());
700 e = call.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700701 })
702 |
703 tap!(more: call!(and_binary, allow_struct) => {
704 let (op, other) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700705 e = ExprBinary {
706 op: op,
707 left: Box::new(e.into()),
708 right: Box::new(other),
709 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700710 })
711 |
712 tap!(ty: and_cast => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700713 let (ty, token) = ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700714 e = ExprCast {
715 expr: Box::new(e.into()),
716 ty: Box::new(ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700717 as_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700719 })
720 |
721 tap!(ty: and_ascription => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700722 let (ty, token) = ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 e = ExprType {
724 expr: Box::new(e.into()),
725 ty: Box::new(ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700726 colon_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700727 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700728 })
729 |
730 tap!(v: call!(and_assign, allow_struct) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700731 let (v, token) = v;
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 e = ExprAssign {
733 left: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700734 eq_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 right: Box::new(v),
736 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700737 })
738 |
739 tap!(more: call!(and_assign_op, allow_struct) => {
740 let (op, v) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700741 e = ExprAssignOp {
742 op: op,
743 left: Box::new(e.into()),
744 right: Box::new(v),
745 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700746 })
747 |
748 tap!(field: and_field => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700749 let (field, token) = field;
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 e = ExprField {
751 expr: Box::new(e.into()),
752 field: field,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700753 dot_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700754 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700755 })
756 |
757 tap!(field: and_tup_field => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700758 let (field, token) = field;
Alex Crichton62a0a592017-05-22 13:58:53 -0700759 e = ExprTupField {
760 expr: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700761 field: field,
762 dot_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700764 })
765 |
766 tap!(i: and_index => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700767 let (i, token) = i;
Alex Crichton62a0a592017-05-22 13:58:53 -0700768 e = ExprIndex {
769 expr: Box::new(e.into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700770 bracket_token: token,
Alex Crichton62a0a592017-05-22 13:58:53 -0700771 index: Box::new(i),
772 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700773 })
774 |
775 tap!(more: call!(and_range, allow_struct) => {
776 let (limits, hi) = more;
Alex Crichton62a0a592017-05-22 13:58:53 -0700777 e = ExprRange {
778 from: Some(Box::new(e.into())),
779 to: hi.map(Box::new),
780 limits: limits,
781 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700782 })
783 |
Alex Crichton954046c2017-05-30 21:49:42 -0700784 tap!(question: syn!(Question) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700785 e = ExprTry {
786 expr: Box::new(e.into()),
Alex Crichton954046c2017-05-30 21:49:42 -0700787 question_token: question,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700788 }.into();
David Tolnay54e854d2016-10-24 12:03:30 -0700789 })
790 )) >>
David Tolnay7184b132016-10-30 10:06:37 -0700791 (e.into())
Alex Crichton954046c2017-05-30 21:49:42 -0700792 }
David Tolnay54e854d2016-10-24 12:03:30 -0700793 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700794
Alex Crichton954046c2017-05-30 21:49:42 -0700795 impl Synom for ExprParen {
796 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
797 do_parse! {
798 input,
799 e: parens!(syn!(Expr)) >>
800 (ExprParen {
801 expr: Box::new(e.0),
802 paren_token: e.1,
803 }.into())
804 }
805 }
806 }
David Tolnay89e05672016-10-02 14:39:42 -0700807
David Tolnay7184b132016-10-30 10:06:37 -0700808 named_ambiguous_expr!(expr_box -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700809 box_: syn!(Box_) >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700810 inner: ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700811 (ExprBox {
812 expr: Box::new(inner),
Alex Crichton954046c2017-05-30 21:49:42 -0700813 box_token: box_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700814 }.into())
David Tolnayb9c8e322016-09-23 20:48:37 -0700815 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700816
Alex Crichton954046c2017-05-30 21:49:42 -0700817 impl Synom for ExprInPlace {
818 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
819 do_parse! {
820 input,
821 in_: syn!(In) >>
822 place: expr_no_struct >>
823 value: braces!(call!(Block::parse_within)) >>
824 (ExprInPlace {
825 in_token: in_,
826 place: Box::new(place),
827 value: Box::new(Expr {
828 node: ExprBlock {
829 unsafety: Unsafety::Normal,
830 block: Block {
831 stmts: value.0,
832 brace_token: value.1,
833 },
834 }.into(),
835 attrs: Vec::new(),
836 }),
837 })
838 }
839 }
840 }
David Tolnay6696c3e2016-10-30 11:45:10 -0700841
Alex Crichton954046c2017-05-30 21:49:42 -0700842 impl Synom for ExprArray {
843 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
844 do_parse! {
845 input,
846 elems: brackets!(call!(Delimited::parse_terminated)) >>
847 (ExprArray {
848 exprs: elems.0,
849 bracket_token: elems.1,
850 })
851 }
852 }
853 }
David Tolnayfa0edf22016-09-23 22:58:24 -0700854
Alex Crichton954046c2017-05-30 21:49:42 -0700855 named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren),
856 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -0700857
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700858 named!(and_method_call -> ExprMethodCall, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700859 dot: syn!(Dot) >>
860 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700861 typarams: option!(do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700862 colon2: syn!(Colon2) >>
863 lt: syn!(Lt) >>
864 tys: call!(Delimited::parse_terminated) >>
865 gt: syn!(Gt) >>
866 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -0700867 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -0700868 args: parens!(call!(Delimited::parse_terminated)) >>
869 ({
870 let (colon2, lt, tys, gt) = match typarams {
871 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
872 None => (None, None, None, None),
873 };
874 ExprMethodCall {
875 // this expr will get overwritten after being returned
876 expr: Box::new(ExprKind::Lit(Lit {
877 span: Span::default(),
878 value: LitKind::Bool(false),
879 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880
Alex Crichton954046c2017-05-30 21:49:42 -0700881 method: method,
882 args: args.0,
883 paren_token: args.1,
884 dot_token: dot,
885 lt_token: lt,
886 gt_token: gt,
887 colon2_token: colon2,
888 typarams: tys.unwrap_or_default(),
889 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700890 })
David Tolnayfa0edf22016-09-23 22:58:24 -0700891 ));
892
Alex Crichton954046c2017-05-30 21:49:42 -0700893 impl Synom for ExprTup {
894 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
895 do_parse! {
896 input,
897 elems: parens!(call!(Delimited::parse_terminated)) >>
898 (ExprTup {
899 args: elems.0,
900 paren_token: elems.1,
901 lone_comma: None, // TODO: parse this
902 })
903 }
904 }
905 }
David Tolnayfa0edf22016-09-23 22:58:24 -0700906
David Tolnayaf2557e2016-10-24 11:52:21 -0700907 named_ambiguous_expr!(and_binary -> (BinOp, Expr), allow_struct, tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -0700908 call!(BinOp::parse_binop),
David Tolnayaf2557e2016-10-24 11:52:21 -0700909 ambiguous_expr!(allow_struct)
910 ));
David Tolnayfa0edf22016-09-23 22:58:24 -0700911
David Tolnay7184b132016-10-30 10:06:37 -0700912 named_ambiguous_expr!(expr_unary -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -0700913 operator: syn!(UnOp) >>
David Tolnayaf2557e2016-10-24 11:52:21 -0700914 operand: ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -0700915 (ExprUnary { op: operator, expr: Box::new(operand) }.into())
David Tolnayfa0edf22016-09-23 22:58:24 -0700916 ));
David Tolnay939766a2016-09-23 23:48:12 -0700917
Alex Crichton954046c2017-05-30 21:49:42 -0700918 named!(and_cast -> (Ty, As), do_parse!(
919 as_: syn!(As) >>
920 ty: syn!(Ty) >>
921 (ty, as_)
David Tolnay939766a2016-09-23 23:48:12 -0700922 ));
923
Alex Crichton954046c2017-05-30 21:49:42 -0700924 named!(and_ascription -> (Ty, Colon),
925 map!(tuple!(syn!(Colon), syn!(Ty)), |(a, b)| (b, a)));
David Tolnay939766a2016-09-23 23:48:12 -0700926
Alex Crichton954046c2017-05-30 21:49:42 -0700927 impl Synom for ExprIfLet {
928 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
929 do_parse! {
930 input,
931 if_: syn!(If) >>
932 let_: syn!(Let) >>
933 pat: syn!(Pat) >>
934 eq: syn!(Eq) >>
935 cond: expr_no_struct >>
936 then_block: braces!(call!(Block::parse_within)) >>
937 else_block: option!(else_block) >>
938 (ExprIfLet {
939 pat: Box::new(pat),
940 let_token: let_,
941 eq_token: eq,
942 expr: Box::new(cond),
943 if_true: Block {
944 stmts: then_block.0,
945 brace_token: then_block.1,
946 },
947 if_token: if_,
948 else_token: else_block.as_ref().map(|p| Else((p.0).0)),
949 if_false: else_block.map(|p| Box::new(p.1.into())),
950 })
951 }
952 }
David Tolnay29f9ce12016-10-02 20:58:40 -0700953 }
954
Alex Crichton954046c2017-05-30 21:49:42 -0700955 impl Synom for ExprIf {
956 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
957 do_parse! {
958 input,
959 if_: syn!(If) >>
960 cond: expr_no_struct >>
961 then_block: braces!(call!(Block::parse_within)) >>
962 else_block: option!(else_block) >>
963 (ExprIf {
964 cond: Box::new(cond),
965 if_true: Block {
966 stmts: then_block.0,
967 brace_token: then_block.1,
968 },
969 if_token: if_,
970 else_token: else_block.as_ref().map(|p| Else((p.0).0)),
971 if_false: else_block.map(|p| Box::new(p.1.into())),
972 })
973 }
974 }
975 }
David Tolnaybb6feae2016-10-02 21:25:20 -0700976
Alex Crichton954046c2017-05-30 21:49:42 -0700977 named!(else_block -> (Else, ExprKind), do_parse!(
978 else_: syn!(Else) >>
979 expr: alt!(
980 syn!(ExprIf) => { ExprKind::If }
981 |
982 syn!(ExprIfLet) => { ExprKind::IfLet }
983 |
984 do_parse!(
985 else_block: braces!(call!(Block::parse_within)) >>
986 (ExprKind::Block(ExprBlock {
987 unsafety: Unsafety::Normal,
988 block: Block {
989 stmts: else_block.0,
990 brace_token: else_block.1,
991 },
992 }))
David Tolnay939766a2016-09-23 23:48:12 -0700993 )
Alex Crichton954046c2017-05-30 21:49:42 -0700994 ) >>
995 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -0700996 ));
997
David Tolnaybb6feae2016-10-02 21:25:20 -0700998
Alex Crichton954046c2017-05-30 21:49:42 -0700999 impl Synom for ExprForLoop {
1000 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1001 do_parse! {
1002 input,
1003 lbl: option!(tuple!(label, syn!(Colon))) >>
1004 for_: syn!(For) >>
1005 pat: syn!(Pat) >>
1006 in_: syn!(In) >>
1007 expr: expr_no_struct >>
1008 loop_block: syn!(Block) >>
1009 (ExprForLoop {
1010 for_token: for_,
1011 in_token: in_,
1012 pat: Box::new(pat),
1013 expr: Box::new(expr),
1014 body: loop_block,
1015 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1016 label: lbl.map(|p| p.0),
1017 })
1018 }
1019 }
1020 }
Gregory Katze5f35682016-09-27 14:20:55 -04001021
Alex Crichton954046c2017-05-30 21:49:42 -07001022 impl Synom for ExprLoop {
1023 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1024 do_parse! {
1025 input,
1026 lbl: option!(tuple!(label, syn!(Colon))) >>
1027 loop_: syn!(Loop) >>
1028 loop_block: syn!(Block) >>
1029 (ExprLoop {
1030 loop_token: loop_,
1031 body: loop_block,
1032 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1033 label: lbl.map(|p| p.0),
1034 })
1035 }
1036 }
1037 }
1038
1039 impl Synom for ExprMatch {
1040 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1041 do_parse! {
1042 input,
1043 match_: syn!(Match) >>
1044 obj: expr_no_struct >>
1045 res: braces!(do_parse!(
1046 mut arms: many0!(do_parse!(
1047 arm: syn!(Arm) >>
1048 cond!(arm_requires_comma(&arm), syn!(Comma)) >>
1049 cond!(!arm_requires_comma(&arm), option!(syn!(Comma))) >>
1050 (arm)
1051 )) >>
1052 last_arm: option!(syn!(Arm)) >>
1053 ({
1054 arms.extend(last_arm);
1055 arms
1056 })
1057 )) >>
1058 ({
1059 let (mut arms, brace) = res;
1060 ExprMatch {
1061 expr: Box::new(obj),
1062 match_token: match_,
1063 brace_token: brace,
1064 arms: {
1065 for arm in &mut arms {
1066 if arm_requires_comma(arm) {
1067 arm.comma = Some(tokens::Comma::default());
1068 }
1069 }
1070 arms
1071 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001072 }
Alex Crichton954046c2017-05-30 21:49:42 -07001073 })
1074 }
1075 }
1076 }
David Tolnay1978c672016-10-27 22:05:52 -07001077
Alex Crichton954046c2017-05-30 21:49:42 -07001078 impl Synom for ExprCatch {
1079 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1080 do_parse! {
1081 input,
1082 do_: syn!(Do) >>
1083 catch_: syn!(Catch) >>
1084 catch_block: syn!(Block) >>
1085 (ExprCatch {
1086 block: catch_block,
1087 do_token: do_,
1088 catch_token: catch_,
1089 }.into())
1090 }
1091 }
1092 }
Arnavion02ef13f2017-04-25 00:54:31 -07001093
David Tolnay1978c672016-10-27 22:05:52 -07001094 fn arm_requires_comma(arm: &Arm) -> bool {
Alex Crichton62a0a592017-05-22 13:58:53 -07001095 if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
David Tolnay1978c672016-10-27 22:05:52 -07001096 false
1097 } else {
1098 true
1099 }
1100 }
1101
Alex Crichton954046c2017-05-30 21:49:42 -07001102 impl Synom for Arm {
1103 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1104 do_parse! {
1105 input,
1106 attrs: many0!(call!(Attribute::parse_outer)) >>
1107 pats: call!(Delimited::parse_separated_nonempty) >>
1108 guard: option!(tuple!(syn!(If), syn!(Expr))) >>
1109 rocket: syn!(Rocket) >>
1110 body: alt!(
1111 map!(syn!(Block), |blk| {
1112 ExprKind::Block(ExprBlock {
1113 unsafety: Unsafety::Normal,
1114 block: blk,
1115 }).into()
1116 })
1117 |
1118 syn!(Expr)
1119 ) >>
1120 (Arm {
1121 rocket_token: rocket,
1122 if_token: guard.as_ref().map(|p| If((p.0).0)),
1123 attrs: attrs,
1124 pats: pats,
1125 guard: guard.map(|p| Box::new(p.1)),
1126 body: Box::new(body),
1127 comma: None,
1128 })
1129 }
1130 }
1131 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001132
David Tolnay7184b132016-10-30 10:06:37 -07001133 named_ambiguous_expr!(expr_closure -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001134 capture: syn!(CaptureBy) >>
1135 or1: syn!(Or) >>
1136 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
1137 or2: syn!(Or) >>
David Tolnay89e05672016-10-02 14:39:42 -07001138 ret_and_body: alt!(
1139 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001140 arrow: syn!(RArrow) >>
1141 ty: syn!(Ty) >>
1142 body: syn!(Block) >>
1143 (FunctionRetTy::Ty(ty, arrow),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001144 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001145 unsafety: Unsafety::Normal,
1146 block: body,
1147 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001148 )
1149 |
David Tolnay58af3552016-12-22 16:58:07 -05001150 map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001151 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001152 (ExprClosure {
1153 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001154 or1_token: or1,
1155 or2_token: or2,
Alex Crichton62a0a592017-05-22 13:58:53 -07001156 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -07001157 inputs: inputs,
1158 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -07001159 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001160 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001161 fn_token: tokens::Fn_::default(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001162 generics: Generics::default(),
1163 paren_token: tokens::Paren::default(),
David Tolnay89e05672016-10-02 14:39:42 -07001164 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001165 body: Box::new(ret_and_body.1),
1166 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001167 ));
1168
Alex Crichton954046c2017-05-30 21:49:42 -07001169 named!(fn_arg -> FnArg, do_parse!(
1170 pat: syn!(Pat) >>
1171 ty: option!(tuple!(syn!(Colon), syn!(Ty))) >>
1172 ({
1173 let (colon, ty) = ty.unwrap_or_else(|| {
1174 (Colon::default(), TyInfer {
1175 underscore_token: Underscore::default(),
1176 }.into())
1177 });
1178 ArgCaptured {
1179 pat: pat,
1180 colon_token: colon,
1181 ty: ty,
1182 }.into()
David Tolnaybb6feae2016-10-02 21:25:20 -07001183 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001184 ));
1185
Alex Crichton954046c2017-05-30 21:49:42 -07001186 impl Synom for ExprWhile {
1187 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1188 do_parse! {
1189 input,
1190 lbl: option!(tuple!(label, syn!(Colon))) >>
1191 while_: syn!(While) >>
1192 cond: expr_no_struct >>
1193 while_block: syn!(Block) >>
1194 (ExprWhile {
1195 while_token: while_,
1196 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1197 cond: Box::new(cond),
1198 body: while_block,
1199 label: lbl.map(|p| p.0),
1200 })
1201 }
1202 }
1203 }
1204
1205 impl Synom for ExprWhileLet {
1206 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1207 do_parse! {
1208 input,
1209 lbl: option!(tuple!(label, syn!(Colon))) >>
1210 while_: syn!(While) >>
1211 let_: syn!(Let) >>
1212 pat: syn!(Pat) >>
1213 eq: syn!(Eq) >>
1214 value: expr_no_struct >>
1215 while_block: syn!(Block) >>
1216 (ExprWhileLet {
1217 eq_token: eq,
1218 let_token: let_,
1219 while_token: while_,
1220 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1221 pat: Box::new(pat),
1222 expr: Box::new(value),
1223 body: while_block,
1224 label: lbl.map(|p| p.0),
1225 })
1226 }
1227 }
1228 }
1229
1230 impl Synom for ExprContinue {
1231 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1232 do_parse! {
1233 input,
1234 cont: syn!(Continue) >>
1235 lbl: option!(label) >>
1236 (ExprContinue {
1237 continue_token: cont,
1238 label: lbl,
1239 })
1240 }
1241 }
1242 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001243
David Tolnay5d55ef72016-12-21 20:20:04 -05001244 named_ambiguous_expr!(expr_break -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001245 break_: syn!(Break) >>
Gregory Katzfd6935d2016-09-30 22:51:25 -04001246 lbl: option!(label) >>
David Tolnay5d55ef72016-12-21 20:20:04 -05001247 val: option!(call!(ambiguous_expr, allow_struct, false)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001248 (ExprBreak {
1249 label: lbl,
1250 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001251 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001252 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001253 ));
1254
David Tolnay7184b132016-10-30 10:06:37 -07001255 named_ambiguous_expr!(expr_ret -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001256 return_: syn!(Return) >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001257 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001258 (ExprRet {
1259 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001260 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001261 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001262 ));
1263
Alex Crichton954046c2017-05-30 21:49:42 -07001264 impl Synom for ExprStruct {
1265 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1266 do_parse! {
1267 input,
1268 path: syn!(Path) >>
1269 data: braces!(do_parse!(
1270 fields: call!(Delimited::parse_terminated) >>
1271 base: option!(
1272 cond!(fields.is_empty() || fields.trailing_delim(),
1273 do_parse!(
1274 dots: syn!(Dot2) >>
1275 base: syn!(Expr) >>
1276 (dots, base)
1277 )
1278 )
1279 ) >>
1280 (fields, base)
1281 )) >>
1282 ({
1283 let ((fields, base), brace) = data;
1284 let (dots, rest) = match base.and_then(|b| b) {
1285 Some((dots, base)) => (Some(dots), Some(base)),
1286 None => (None, None),
1287 };
1288 ExprStruct {
1289 brace_token: brace,
1290 path: path,
1291 fields: fields,
1292 dot2_token: dots,
1293 rest: rest.map(Box::new),
1294 }
1295 })
1296 }
1297 }
1298 }
1299
1300 impl Synom for FieldValue {
1301 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1302 alt! {
1303 input,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001304 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001305 name: wordlike >>
1306 colon: syn!(Colon) >>
1307 value: syn!(Expr) >>
1308 (FieldValue {
1309 ident: name,
1310 expr: value,
1311 is_shorthand: false,
1312 attrs: Vec::new(),
1313 colon_token: Some(colon),
1314 })
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001315 )
Alex Crichton954046c2017-05-30 21:49:42 -07001316 |
1317 map!(syn!(Ident), |name: Ident| FieldValue {
1318 ident: name.clone(),
1319 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
1320 is_shorthand: true,
1321 attrs: Vec::new(),
1322 colon_token: None,
1323 })
1324 }
1325 }
1326 }
David Tolnay055a7042016-10-02 19:23:54 -07001327
Alex Crichton954046c2017-05-30 21:49:42 -07001328 impl Synom for ExprRepeat {
1329 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1330 do_parse! {
1331 input,
1332 data: brackets!(do_parse!(
1333 value: syn!(Expr) >>
1334 semi: syn!(Semi) >>
1335 times: syn!(Expr) >>
1336 (value, semi, times)
1337 )) >>
1338 (ExprRepeat {
1339 expr: Box::new((data.0).0),
1340 amt: Box::new((data.0).2),
1341 bracket_token: data.1,
1342 semi_token: (data.0).1,
1343 })
1344 }
1345 }
1346 }
David Tolnay055a7042016-10-02 19:23:54 -07001347
Alex Crichton954046c2017-05-30 21:49:42 -07001348 impl Synom for ExprBlock {
1349 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1350 do_parse! {
1351 input,
1352 rules: syn!(Unsafety) >>
1353 b: syn!(Block) >>
1354 (ExprBlock {
1355 unsafety: rules,
1356 block: b,
1357 })
1358 }
1359 }
1360 }
David Tolnay89e05672016-10-02 14:39:42 -07001361
David Tolnay7184b132016-10-30 10:06:37 -07001362 named_ambiguous_expr!(expr_range -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001363 limits: syn!(RangeLimits) >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001364 hi: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001365 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001366 ));
1367
Alex Crichton954046c2017-05-30 21:49:42 -07001368 impl Synom for RangeLimits {
1369 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1370 alt! {
1371 input,
1372 syn!(Dot3) => { RangeLimits::Closed }
1373 |
1374 syn!(Dot2) => { RangeLimits::HalfOpen }
1375 }
1376 }
1377 }
David Tolnay438c9052016-10-07 23:24:48 -07001378
Alex Crichton954046c2017-05-30 21:49:42 -07001379 impl Synom for ExprPath {
1380 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1381 do_parse! {
1382 input,
1383 pair: qpath >>
1384 (ExprPath {
1385 qself: pair.0,
1386 path: pair.1,
1387 })
1388 }
1389 }
1390 }
David Tolnay42602292016-10-01 22:25:45 -07001391
David Tolnay7184b132016-10-30 10:06:37 -07001392 named_ambiguous_expr!(expr_addr_of -> ExprKind, allow_struct, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001393 and: syn!(And) >>
1394 mutability: syn!(Mutability) >>
David Tolnayaf2557e2016-10-24 11:52:21 -07001395 expr: ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001396 (ExprAddrOf {
1397 mutbl: mutability,
1398 expr: Box::new(expr),
Alex Crichton954046c2017-05-30 21:49:42 -07001399 and_token: and,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001400 }.into())
David Tolnay3c2467c2016-10-02 17:55:08 -07001401 ));
1402
Alex Crichton954046c2017-05-30 21:49:42 -07001403 named_ambiguous_expr!(and_assign -> (Expr, Eq), allow_struct,
1404 map!(
1405 tuple!(syn!(Eq), ambiguous_expr!(allow_struct)),
1406 |(a, b)| (b, a)
1407 )
1408 );
David Tolnay438c9052016-10-07 23:24:48 -07001409
David Tolnayaf2557e2016-10-24 11:52:21 -07001410 named_ambiguous_expr!(and_assign_op -> (BinOp, Expr), allow_struct, tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -07001411 call!(BinOp::parse_assign_op),
David Tolnayaf2557e2016-10-24 11:52:21 -07001412 ambiguous_expr!(allow_struct)
1413 ));
David Tolnay438c9052016-10-07 23:24:48 -07001414
Alex Crichton954046c2017-05-30 21:49:42 -07001415 named!(and_field -> (Ident, Dot),
1416 map!(tuple!(syn!(Dot), syn!(Ident)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001417
Alex Crichton954046c2017-05-30 21:49:42 -07001418 named!(and_tup_field -> (Lit, Dot),
1419 map!(tuple!(syn!(Dot), syn!(Lit)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001420
Alex Crichton954046c2017-05-30 21:49:42 -07001421 named!(and_index -> (Expr, tokens::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001422
David Tolnayaf2557e2016-10-24 11:52:21 -07001423 named_ambiguous_expr!(and_range -> (RangeLimits, Option<Expr>), allow_struct, tuple!(
Alex Crichton954046c2017-05-30 21:49:42 -07001424 syn!(RangeLimits),
David Tolnay54e854d2016-10-24 12:03:30 -07001425 option!(call!(ambiguous_expr, allow_struct, false))
David Tolnayaf2557e2016-10-24 11:52:21 -07001426 ));
David Tolnay438c9052016-10-07 23:24:48 -07001427
Alex Crichton954046c2017-05-30 21:49:42 -07001428 impl Synom for Block {
1429 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1430 do_parse! {
1431 input,
1432 stmts: braces!(call!(Block::parse_within)) >>
1433 (Block {
1434 stmts: stmts.0,
1435 brace_token: stmts.1,
1436 })
David Tolnay939766a2016-09-23 23:48:12 -07001437 }
Alex Crichton954046c2017-05-30 21:49:42 -07001438 }
1439 }
David Tolnay939766a2016-09-23 23:48:12 -07001440
Alex Crichton954046c2017-05-30 21:49:42 -07001441 impl Block {
1442 pub fn parse_within(input: &[TokenTree]) -> IResult<&[TokenTree], Vec<Stmt>> {
1443 do_parse! {
1444 input,
1445 many0!(syn!(Semi)) >>
1446 mut standalone: many0!(terminated!(syn!(Stmt), many0!(syn!(Semi)))) >>
1447 last: option!(syn!(Expr)) >>
1448 (match last {
1449 None => standalone,
1450 Some(last) => {
1451 standalone.push(Stmt::Expr(Box::new(last)));
1452 standalone
1453 }
1454 })
1455 }
1456 }
1457 }
1458
1459 impl Synom for Stmt {
1460 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1461 alt! {
1462 input,
1463 stmt_mac
1464 |
1465 stmt_local
1466 |
1467 stmt_item
1468 |
1469 stmt_expr
1470 }
1471 }
1472 }
David Tolnay939766a2016-09-23 23:48:12 -07001473
David Tolnay13b3d352016-10-03 00:31:15 -07001474 named!(stmt_mac -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001475 attrs: many0!(call!(Attribute::parse_outer)) >>
1476 what: syn!(Path) >>
1477 bang: syn!(Bang) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001478 // Only parse braces here; paren and bracket will get parsed as
1479 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001480 data: braces!(syn!(TokenStream)) >>
1481 semi: option!(syn!(Semi)) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001482 (Stmt::Mac(Box::new((
1483 Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001484 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001485 bang_token: bang,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001486 tokens: vec![TokenTree(proc_macro2::TokenTree {
Alex Crichton954046c2017-05-30 21:49:42 -07001487 span: ((data.1).0).0,
1488 kind: TokenKind::Sequence(Delimiter::Brace, data.0),
David Tolnayeea28d62016-10-25 20:44:08 -07001489 })],
1490 },
Alex Crichton954046c2017-05-30 21:49:42 -07001491 match semi {
1492 Some(semi) => MacStmtStyle::Semicolon(semi),
1493 None => MacStmtStyle::Braces,
David Tolnay60d48942016-10-30 14:34:52 -07001494 },
David Tolnayeea28d62016-10-25 20:44:08 -07001495 attrs,
1496 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001497 ));
1498
David Tolnay191e0582016-10-02 18:31:09 -07001499 named!(stmt_local -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001500 attrs: many0!(call!(Attribute::parse_outer)) >>
1501 let_: syn!(Let) >>
1502 pat: syn!(Pat) >>
1503 ty: option!(tuple!(syn!(Colon), syn!(Ty))) >>
1504 init: option!(tuple!(syn!(Eq), syn!(Expr))) >>
1505 semi: syn!(Semi) >>
David Tolnay191e0582016-10-02 18:31:09 -07001506 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001507 let_token: let_,
1508 semi_token: semi,
1509 colon_token: ty.as_ref().map(|p| Colon((p.0).0)),
1510 eq_token: init.as_ref().map(|p| Eq((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001511 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001512 ty: ty.map(|p| Box::new(p.1)),
1513 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001514 attrs: attrs,
1515 })))
1516 ));
1517
Alex Crichton954046c2017-05-30 21:49:42 -07001518 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07001519
David Tolnaycfe55022016-10-02 22:02:27 -07001520 fn requires_semi(e: &Expr) -> bool {
David Tolnay7184b132016-10-30 10:06:37 -07001521 match e.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001522 ExprKind::If(_) |
1523 ExprKind::IfLet(_) |
1524 ExprKind::While(_) |
1525 ExprKind::WhileLet(_) |
1526 ExprKind::ForLoop(_) |
1527 ExprKind::Loop(_) |
1528 ExprKind::Match(_) |
1529 ExprKind::Block(_) => false,
David Tolnaycfe55022016-10-02 22:02:27 -07001530
1531 _ => true,
1532 }
1533 }
1534
1535 named!(stmt_expr -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001536 attrs: many0!(call!(Attribute::parse_outer)) >>
1537 mut e: syn!(Expr) >>
1538 semi: option!(syn!(Semi)) >>
David Tolnay7184b132016-10-30 10:06:37 -07001539 ({
1540 e.attrs = attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001541 if let Some(s) = semi {
1542 Stmt::Semi(Box::new(e), s)
David Tolnay092dcb02016-10-30 10:14:14 -07001543 } else if requires_semi(&e) {
Michael Layzell416724e2017-05-24 21:12:34 -04001544 return IResult::Error;
David Tolnay7184b132016-10-30 10:06:37 -07001545 } else {
1546 Stmt::Expr(Box::new(e))
1547 }
David Tolnaycfe55022016-10-02 22:02:27 -07001548 })
David Tolnay939766a2016-09-23 23:48:12 -07001549 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001550
Alex Crichton954046c2017-05-30 21:49:42 -07001551 impl Synom for Pat {
1552 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1553 alt! {
1554 input,
1555 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
1556 |
1557 syn!(PatBox) => { Pat::Box } // must be before pat_ident
1558 |
1559 syn!(PatRange) => { Pat::Range } // must be before pat_lit
1560 |
1561 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
1562 |
1563 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
1564 |
1565 syn!(Mac) => { Pat::Mac } // must be before pat_ident
1566 |
1567 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
1568 |
1569 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
1570 |
1571 syn!(PatPath) => { Pat::Path }
1572 |
1573 syn!(PatTuple) => { Pat::Tuple }
1574 |
1575 syn!(PatRef) => { Pat::Ref }
1576 |
1577 syn!(PatSlice) => { Pat::Slice }
1578 }
1579 }
1580 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001581
Alex Crichton954046c2017-05-30 21:49:42 -07001582 impl Synom for PatWild {
1583 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1584 map! {
1585 input,
1586 syn!(Underscore),
1587 |u| PatWild { underscore_token: u }
1588 }
1589 }
1590 }
David Tolnay84aa0752016-10-02 23:01:13 -07001591
Alex Crichton954046c2017-05-30 21:49:42 -07001592 impl Synom for PatBox {
1593 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1594 do_parse! {
1595 input,
1596 boxed: syn!(Box_) >>
1597 pat: syn!(Pat) >>
1598 (PatBox {
David Tolnayda167382016-10-30 13:34:09 -07001599 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001600 box_token: boxed,
1601 })
1602 }
1603 }
1604 }
1605
1606 impl Synom for PatIdent {
1607 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1608 do_parse! {
1609 input,
1610 mode: option!(syn!(Ref)) >>
1611 mutability: syn!(Mutability) >>
1612 name: alt!(
1613 syn!(Ident)
1614 |
1615 syn!(Self_) => { Into::into }
1616 ) >>
1617 not!(syn!(Lt)) >>
1618 not!(syn!(Colon2)) >>
1619 subpat: option!(tuple!(syn!(At), syn!(Pat))) >>
1620 (PatIdent {
1621 mode: match mode {
1622 Some(mode) => BindingMode::ByRef(mode, mutability),
1623 None => BindingMode::ByValue(mutability),
1624 },
1625 ident: name,
1626 at_token: subpat.as_ref().map(|p| At((p.0).0)),
1627 subpat: subpat.map(|p| Box::new(p.1)),
1628 })
1629 }
1630 }
1631 }
1632
1633 impl Synom for PatTupleStruct {
1634 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1635 do_parse! {
1636 input,
1637 path: syn!(Path) >>
1638 tuple: syn!(PatTuple) >>
1639 (PatTupleStruct {
1640 path: path,
1641 pat: tuple,
1642 })
1643 }
1644 }
1645 }
1646
1647 impl Synom for PatStruct {
1648 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1649 do_parse! {
1650 input,
1651 path: syn!(Path) >>
1652 data: braces!(do_parse!(
1653 fields: call!(Delimited::parse_terminated) >>
1654 base: option!(
1655 cond!(fields.is_empty() || fields.trailing_delim(),
1656 syn!(Dot2))
1657 ) >>
1658 (fields, base)
1659 )) >>
1660 (PatStruct {
1661 path: path,
1662 fields: (data.0).0,
1663 brace_token: data.1,
1664 dot2_token: (data.0).1.and_then(|m| m),
1665 })
1666 }
1667 }
1668 }
1669
1670 impl Synom for FieldPat {
1671 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1672 alt! {
1673 input,
1674 do_parse!(
1675 ident: wordlike >>
1676 colon: syn!(Colon) >>
1677 pat: syn!(Pat) >>
1678 (FieldPat {
1679 ident: ident,
1680 pat: Box::new(pat),
1681 is_shorthand: false,
1682 attrs: Vec::new(),
1683 colon_token: Some(colon),
1684 })
1685 )
1686 |
1687 do_parse!(
1688 boxed: option!(syn!(Box_)) >>
1689 mode: option!(syn!(Ref)) >>
1690 mutability: syn!(Mutability) >>
1691 ident: syn!(Ident) >>
1692 ({
1693 let mut pat: Pat = PatIdent {
1694 mode: if let Some(mode) = mode {
1695 BindingMode::ByRef(mode, mutability)
1696 } else {
1697 BindingMode::ByValue(mutability)
1698 },
1699 ident: ident.clone(),
1700 subpat: None,
1701 at_token: None,
1702 }.into();
1703 if let Some(boxed) = boxed {
1704 pat = PatBox {
1705 pat: Box::new(pat),
1706 box_token: boxed,
1707 }.into();
1708 }
1709 FieldPat {
1710 ident: ident,
1711 pat: Box::new(pat),
1712 is_shorthand: true,
1713 attrs: Vec::new(),
1714 colon_token: None,
1715 }
1716 })
1717 )
1718 }
1719 }
1720 }
1721
1722 named!(wordlike -> Ident, alt!(
1723 syn!(Ident)
1724 |
1725 do_parse!(
1726 lit: syn!(Lit) >>
1727 ({
1728 let s = lit.value.to_string();
1729 if s.parse::<u32>().is_ok() {
1730 Ident::new(s.into(), lit.span)
1731 } else {
1732 return IResult::Error
David Tolnayda167382016-10-30 13:34:09 -07001733 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001734 })
1735 )
1736 ));
1737
Alex Crichton954046c2017-05-30 21:49:42 -07001738 impl Synom for PatPath {
1739 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1740 map! {
1741 input,
1742 syn!(ExprPath),
1743 |p: ExprPath| PatPath { qself: p.qself, path: p.path }
1744 }
1745 }
1746 }
David Tolnay9636c052016-10-02 17:11:17 -07001747
Alex Crichton954046c2017-05-30 21:49:42 -07001748 impl Synom for PatTuple {
1749 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1750 do_parse! {
1751 input,
1752 data: parens!(do_parse!(
1753 elems: call!(Delimited::parse_terminated) >>
1754 dotdot: map!(cond!(
1755 elems.is_empty() || elems.trailing_delim(),
1756 option!(do_parse!(
1757 dots: syn!(Dot2) >>
1758 trailing: option!(syn!(Comma)) >>
1759 (dots, trailing)
1760 ))
1761 ), |x: Option<_>| x.and_then(|x| x)) >>
1762 rest: cond!(match dotdot {
1763 Some((_, Some(_))) => true,
1764 _ => false,
1765 },
1766 call!(Delimited::parse_terminated)) >>
1767 (elems, dotdot, rest)
1768 )) >>
1769 ({
1770 let ((mut elems, dotdot, rest), parens) = data;
1771 let (dotdot, trailing) = match dotdot {
1772 Some((a, b)) => (Some(a), Some(b)),
1773 None => (None, None),
1774 };
1775 PatTuple {
1776 paren_token: parens,
1777 dots_pos: dotdot.as_ref().map(|_| elems.len()),
1778 dot2_token: dotdot,
1779 comma_token: trailing.and_then(|b| b),
1780 pats: {
1781 if let Some(rest) = rest {
1782 for elem in rest {
1783 elems.push(elem);
1784 }
1785 }
1786 elems
1787 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001788 }
Alex Crichton954046c2017-05-30 21:49:42 -07001789 })
1790 }
1791 }
1792 }
David Tolnayfbb73232016-10-03 01:00:06 -07001793
Alex Crichton954046c2017-05-30 21:49:42 -07001794 impl Synom for PatRef {
1795 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1796 do_parse! {
1797 input,
1798 and: syn!(And) >>
1799 mutability: syn!(Mutability) >>
1800 pat: syn!(Pat) >>
1801 (PatRef {
1802 pat: Box::new(pat),
1803 mutbl: mutability,
1804 and_token: and,
1805 })
1806 }
1807 }
1808 }
David Tolnayffdb97f2016-10-03 01:28:33 -07001809
Alex Crichton954046c2017-05-30 21:49:42 -07001810 impl Synom for PatLit {
1811 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1812 do_parse! {
1813 input,
1814 lit: pat_lit_expr >>
1815 (if let ExprKind::Path(_) = lit.node {
1816 return IResult::Error; // these need to be parsed by pat_path
1817 } else {
1818 PatLit {
1819 expr: Box::new(lit),
1820 }
1821 })
1822 }
1823 }
1824 }
David Tolnaye1310902016-10-29 23:40:00 -07001825
Alex Crichton954046c2017-05-30 21:49:42 -07001826 impl Synom for PatRange {
1827 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1828 do_parse! {
1829 input,
1830 lo: pat_lit_expr >>
1831 limits: syn!(RangeLimits) >>
1832 hi: pat_lit_expr >>
1833 (PatRange {
1834 lo: Box::new(lo),
1835 hi: Box::new(hi),
1836 limits: limits,
1837 })
1838 }
1839 }
1840 }
David Tolnaye1310902016-10-29 23:40:00 -07001841
David Tolnay2cfddc62016-10-30 01:03:27 -07001842 named!(pat_lit_expr -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001843 neg: option!(syn!(Sub)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001844 v: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07001845 syn!(Lit) => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07001846 |
Alex Crichton954046c2017-05-30 21:49:42 -07001847 syn!(ExprPath) => { ExprKind::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07001848 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001849 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07001850 ExprKind::Unary(ExprUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001851 op: UnOp::Neg(tokens::Sub::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001852 expr: Box::new(v.into())
1853 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001854 } else {
David Tolnay7184b132016-10-30 10:06:37 -07001855 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001856 })
1857 ));
David Tolnay8b308c22016-10-03 01:24:10 -07001858
Alex Crichton954046c2017-05-30 21:49:42 -07001859 impl Synom for PatSlice {
1860 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1861 map! {
1862 input,
1863 brackets!(do_parse!(
1864 before: call!(Delimited::parse_terminated) >>
1865 middle: option!(do_parse!(
1866 dots: syn!(Dot2) >>
1867 trailing: option!(syn!(Comma)) >>
1868 (dots, trailing)
1869 )) >>
1870 after: cond!(
1871 match middle {
1872 Some((_, ref trailing)) => trailing.is_some(),
1873 _ => false,
1874 },
1875 call!(Delimited::parse_terminated)
1876 ) >>
1877 (before, middle, after)
1878 )),
1879 |((before, middle, after), brackets)| {
1880 let mut before: Delimited<Pat, tokens::Comma> = before;
1881 let after: Option<Delimited<Pat, tokens::Comma>> = after;
1882 let middle: Option<(Dot2, Option<Comma>)> = middle;
1883 PatSlice {
1884 dot2_token: middle.as_ref().map(|m| Dot2((m.0).0)),
1885 comma_token: middle.as_ref().and_then(|m| {
1886 m.1.as_ref().map(|m| Comma(m.0))
1887 }),
1888 bracket_token: brackets,
1889 middle: middle.and_then(|_| {
1890 if !before.is_empty() && !before.trailing_delim() {
1891 Some(Box::new(before.pop().unwrap().into_item()))
1892 } else {
1893 None
1894 }
1895 }),
1896 front: before,
1897 back: after.unwrap_or_default(),
1898 }
David Tolnaye1f13c32016-10-29 23:34:40 -07001899 }
Alex Crichton954046c2017-05-30 21:49:42 -07001900 }
1901 }
1902 }
David Tolnay435a9a82016-10-29 13:47:20 -07001903
Alex Crichton954046c2017-05-30 21:49:42 -07001904 impl Synom for CaptureBy {
1905 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
1906 alt! {
1907 input,
1908 syn!(Move) => { CaptureBy::Value }
1909 |
1910 epsilon!() => { |_| CaptureBy::Ref }
1911 }
1912 }
1913 }
David Tolnay89e05672016-10-02 14:39:42 -07001914
Alex Crichton954046c2017-05-30 21:49:42 -07001915 named!(label -> Ident, map!(syn!(Lifetime), |lt: Lifetime| lt.ident));
David Tolnayb9c8e322016-09-23 20:48:37 -07001916}
1917
David Tolnayf4bbbd92016-09-23 14:41:55 -07001918#[cfg(feature = "printing")]
1919mod printing {
1920 use super::*;
David Tolnay13b3d352016-10-03 00:31:15 -07001921 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07001922 use quote::{Tokens, ToTokens};
1923
1924 impl ToTokens for Expr {
1925 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07001926 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07001927 self.node.to_tokens(tokens)
1928 }
1929 }
1930
1931 impl ToTokens for ExprBox {
1932 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001933 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001934 self.expr.to_tokens(tokens);
1935 }
1936 }
1937
1938 impl ToTokens for ExprInPlace {
1939 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001940 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001941 self.place.to_tokens(tokens);
1942 self.value.to_tokens(tokens);
1943 }
1944 }
1945
1946 impl ToTokens for ExprArray {
1947 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001948 self.bracket_token.surround(tokens, |tokens| {
1949 self.exprs.to_tokens(tokens);
1950 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001951 }
1952 }
1953
1954 impl ToTokens for ExprCall {
1955 fn to_tokens(&self, tokens: &mut Tokens) {
1956 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001957 self.paren_token.surround(tokens, |tokens| {
1958 self.args.to_tokens(tokens);
1959 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001960 }
1961 }
1962
1963 impl ToTokens for ExprMethodCall {
1964 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001965 self.expr.to_tokens(tokens);
1966 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07001967 self.method.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001968 self.colon2_token.to_tokens(tokens);
1969 self.lt_token.to_tokens(tokens);
1970 self.typarams.to_tokens(tokens);
1971 self.gt_token.to_tokens(tokens);
1972 self.paren_token.surround(tokens, |tokens| {
1973 self.args.to_tokens(tokens);
1974 });
Alex Crichton62a0a592017-05-22 13:58:53 -07001975 }
1976 }
1977
1978 impl ToTokens for ExprTup {
1979 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001980 self.paren_token.surround(tokens, |tokens| {
1981 self.args.to_tokens(tokens);
1982 self.lone_comma.to_tokens(tokens);
1983 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001984 }
1985 }
1986
1987 impl ToTokens for ExprBinary {
1988 fn to_tokens(&self, tokens: &mut Tokens) {
1989 self.left.to_tokens(tokens);
1990 self.op.to_tokens(tokens);
1991 self.right.to_tokens(tokens);
1992 }
1993 }
1994
1995 impl ToTokens for ExprUnary {
1996 fn to_tokens(&self, tokens: &mut Tokens) {
1997 self.op.to_tokens(tokens);
1998 self.expr.to_tokens(tokens);
1999 }
2000 }
2001
2002 impl ToTokens for ExprCast {
2003 fn to_tokens(&self, tokens: &mut Tokens) {
2004 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002005 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002006 self.ty.to_tokens(tokens);
2007 }
2008 }
2009
2010 impl ToTokens for ExprType {
2011 fn to_tokens(&self, tokens: &mut Tokens) {
2012 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002013 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002014 self.ty.to_tokens(tokens);
2015 }
2016 }
2017
2018 impl ToTokens for ExprIf {
2019 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002020 self.if_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002021 self.cond.to_tokens(tokens);
2022 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002023 self.else_token.to_tokens(tokens);
2024 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002025 }
2026 }
2027
2028 impl ToTokens for ExprIfLet {
2029 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002030 self.if_token.to_tokens(tokens);
2031 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002032 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002033 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002034 self.expr.to_tokens(tokens);
2035 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002036 self.else_token.to_tokens(tokens);
2037 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002038 }
2039 }
2040
2041 impl ToTokens for ExprWhile {
2042 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002043 self.label.to_tokens(tokens);
2044 self.colon_token.to_tokens(tokens);
2045 self.while_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002046 self.cond.to_tokens(tokens);
2047 self.body.to_tokens(tokens);
2048 }
2049 }
2050
2051 impl ToTokens for ExprWhileLet {
2052 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002053 self.label.to_tokens(tokens);
2054 self.colon_token.to_tokens(tokens);
2055 self.while_token.to_tokens(tokens);
2056 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002057 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002058 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002059 self.expr.to_tokens(tokens);
2060 self.body.to_tokens(tokens);
2061 }
2062 }
2063
2064 impl ToTokens for ExprForLoop {
2065 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002066 self.label.to_tokens(tokens);
2067 self.colon_token.to_tokens(tokens);
2068 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002069 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002070 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002071 self.expr.to_tokens(tokens);
2072 self.body.to_tokens(tokens);
2073 }
2074 }
2075
2076 impl ToTokens for ExprLoop {
2077 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002078 self.label.to_tokens(tokens);
2079 self.colon_token.to_tokens(tokens);
2080 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002081 self.body.to_tokens(tokens);
2082 }
2083 }
2084
2085 impl ToTokens for ExprMatch {
2086 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002087 self.match_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002088 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002089 self.brace_token.surround(tokens, |tokens| {
2090 tokens.append_all(&self.arms);
2091 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002092 }
2093 }
2094
2095 impl ToTokens for ExprCatch {
2096 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002097 self.do_token.to_tokens(tokens);
2098 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002099 self.block.to_tokens(tokens);
2100 }
2101 }
2102
2103 impl ToTokens for ExprClosure {
2104 fn to_tokens(&self, tokens: &mut Tokens) {
2105 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002106 self.or1_token.to_tokens(tokens);
2107 for item in self.decl.inputs.iter() {
2108 match **item.item() {
2109 FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002110 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002111 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002112 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002113 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002114 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002115 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002116 self.or2_token.to_tokens(tokens);
2117 self.decl.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002118 self.body.to_tokens(tokens);
2119 }
2120 }
2121
2122 impl ToTokens for ExprBlock {
2123 fn to_tokens(&self, tokens: &mut Tokens) {
2124 self.unsafety.to_tokens(tokens);
2125 self.block.to_tokens(tokens);
2126 }
2127 }
2128
2129 impl ToTokens for ExprAssign {
2130 fn to_tokens(&self, tokens: &mut Tokens) {
2131 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002132 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002133 self.right.to_tokens(tokens);
2134 }
2135 }
2136
2137 impl ToTokens for ExprAssignOp {
2138 fn to_tokens(&self, tokens: &mut Tokens) {
2139 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002140 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002141 self.right.to_tokens(tokens);
2142 }
2143 }
2144
2145 impl ToTokens for ExprField {
2146 fn to_tokens(&self, tokens: &mut Tokens) {
2147 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002148 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002149 self.field.to_tokens(tokens);
2150 }
2151 }
2152
2153 impl ToTokens for ExprTupField {
2154 fn to_tokens(&self, tokens: &mut Tokens) {
2155 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002156 self.dot_token.to_tokens(tokens);
2157 self.field.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002158 }
2159 }
2160
2161 impl ToTokens for ExprIndex {
2162 fn to_tokens(&self, tokens: &mut Tokens) {
2163 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002164 self.bracket_token.surround(tokens, |tokens| {
2165 self.index.to_tokens(tokens);
2166 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002167 }
2168 }
2169
2170 impl ToTokens for ExprRange {
2171 fn to_tokens(&self, tokens: &mut Tokens) {
2172 self.from.to_tokens(tokens);
2173 self.limits.to_tokens(tokens);
2174 self.to.to_tokens(tokens);
2175 }
2176 }
2177
2178 impl ToTokens for ExprPath {
2179 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002180 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002181 }
2182 }
2183
2184 impl ToTokens for ExprAddrOf {
2185 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002186 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002187 self.mutbl.to_tokens(tokens);
2188 self.expr.to_tokens(tokens);
2189 }
2190 }
2191
2192 impl ToTokens for ExprBreak {
2193 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002194 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002195 self.label.to_tokens(tokens);
2196 self.expr.to_tokens(tokens);
2197 }
2198 }
2199
2200 impl ToTokens for ExprContinue {
2201 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002202 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002203 self.label.to_tokens(tokens);
2204 }
2205 }
2206
2207 impl ToTokens for ExprRet {
2208 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002209 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002210 self.expr.to_tokens(tokens);
2211 }
2212 }
2213
2214 impl ToTokens for ExprStruct {
2215 fn to_tokens(&self, tokens: &mut Tokens) {
2216 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002217 self.brace_token.surround(tokens, |tokens| {
2218 self.fields.to_tokens(tokens);
2219 self.dot2_token.to_tokens(tokens);
2220 self.rest.to_tokens(tokens);
2221 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002222 }
2223 }
2224
2225 impl ToTokens for ExprRepeat {
2226 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002227 self.bracket_token.surround(tokens, |tokens| {
2228 self.expr.to_tokens(tokens);
2229 self.semi_token.to_tokens(tokens);
2230 self.amt.to_tokens(tokens);
2231 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002232 }
2233 }
2234
2235 impl ToTokens for ExprParen {
2236 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002237 self.paren_token.surround(tokens, |tokens| {
2238 self.expr.to_tokens(tokens);
2239 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002240 }
2241 }
2242
2243 impl ToTokens for ExprTry {
2244 fn to_tokens(&self, tokens: &mut Tokens) {
2245 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002246 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002247 }
2248 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002249
David Tolnay055a7042016-10-02 19:23:54 -07002250 impl ToTokens for FieldValue {
2251 fn to_tokens(&self, tokens: &mut Tokens) {
2252 self.ident.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002253 if !self.is_shorthand {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002254 self.colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002255 self.expr.to_tokens(tokens);
2256 }
David Tolnay055a7042016-10-02 19:23:54 -07002257 }
2258 }
2259
David Tolnayb4ad3b52016-10-01 21:58:13 -07002260 impl ToTokens for Arm {
2261 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002262 tokens.append_all(&self.attrs);
2263 self.pats.to_tokens(tokens);
2264 self.if_token.to_tokens(tokens);
2265 self.guard.to_tokens(tokens);
2266 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002267 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002268 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002269 }
2270 }
2271
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002272 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002273 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002274 self.underscore_token.to_tokens(tokens);
2275 }
2276 }
2277
2278 impl ToTokens for PatIdent {
2279 fn to_tokens(&self, tokens: &mut Tokens) {
2280 self.mode.to_tokens(tokens);
2281 self.ident.to_tokens(tokens);
2282 self.at_token.to_tokens(tokens);
2283 self.subpat.to_tokens(tokens);
2284 }
2285 }
2286
2287 impl ToTokens for PatStruct {
2288 fn to_tokens(&self, tokens: &mut Tokens) {
2289 self.path.to_tokens(tokens);
2290 self.brace_token.surround(tokens, |tokens| {
2291 self.fields.to_tokens(tokens);
2292 self.dot2_token.to_tokens(tokens);
2293 });
2294 }
2295 }
2296
2297 impl ToTokens for PatTupleStruct {
2298 fn to_tokens(&self, tokens: &mut Tokens) {
2299 self.path.to_tokens(tokens);
2300 self.pat.to_tokens(tokens);
2301 }
2302 }
2303
2304 impl ToTokens for PatPath {
2305 fn to_tokens(&self, tokens: &mut Tokens) {
2306 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2307 }
2308 }
2309
2310 impl ToTokens for PatTuple {
2311 fn to_tokens(&self, tokens: &mut Tokens) {
2312 self.paren_token.surround(tokens, |tokens| {
2313 for (i, token) in self.pats.iter().enumerate() {
2314 if Some(i) == self.dots_pos {
2315 self.dot2_token.to_tokens(tokens);
2316 self.comma_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002317 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002318 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002319 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002320
2321 if Some(self.pats.len()) == self.dots_pos {
2322 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002323 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002324 });
2325 }
2326 }
2327
2328 impl ToTokens for PatBox {
2329 fn to_tokens(&self, tokens: &mut Tokens) {
2330 self.box_token.to_tokens(tokens);
2331 self.pat.to_tokens(tokens);
2332 }
2333 }
2334
2335 impl ToTokens for PatRef {
2336 fn to_tokens(&self, tokens: &mut Tokens) {
2337 self.and_token.to_tokens(tokens);
2338 self.mutbl.to_tokens(tokens);
2339 self.pat.to_tokens(tokens);
2340 }
2341 }
2342
2343 impl ToTokens for PatLit {
2344 fn to_tokens(&self, tokens: &mut Tokens) {
2345 self.expr.to_tokens(tokens);
2346 }
2347 }
2348
2349 impl ToTokens for PatRange {
2350 fn to_tokens(&self, tokens: &mut Tokens) {
2351 self.lo.to_tokens(tokens);
2352 self.limits.to_tokens(tokens);
2353 self.hi.to_tokens(tokens);
2354 }
2355 }
2356
2357 impl ToTokens for PatSlice {
2358 fn to_tokens(&self, tokens: &mut Tokens) {
2359 self.bracket_token.surround(tokens, |tokens| {
2360 self.front.to_tokens(tokens);
2361 self.middle.to_tokens(tokens);
2362 self.dot2_token.to_tokens(tokens);
2363 self.comma_token.to_tokens(tokens);
2364 self.back.to_tokens(tokens);
2365 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002366 }
2367 }
2368
Arnavion1992e2f2017-04-25 01:47:46 -07002369 impl ToTokens for RangeLimits {
2370 fn to_tokens(&self, tokens: &mut Tokens) {
2371 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002372 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2373 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
Arnavion1992e2f2017-04-25 01:47:46 -07002374 }
2375 }
2376 }
2377
David Tolnay8d9e81a2016-10-03 22:36:32 -07002378 impl ToTokens for FieldPat {
2379 fn to_tokens(&self, tokens: &mut Tokens) {
2380 if !self.is_shorthand {
2381 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002382 self.colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002383 }
2384 self.pat.to_tokens(tokens);
2385 }
2386 }
2387
David Tolnayb4ad3b52016-10-01 21:58:13 -07002388 impl ToTokens for BindingMode {
2389 fn to_tokens(&self, tokens: &mut Tokens) {
2390 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002391 BindingMode::ByRef(ref t, ref m) => {
2392 t.to_tokens(tokens);
2393 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002394 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002395 BindingMode::ByValue(ref m) => {
2396 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002397 }
2398 }
2399 }
2400 }
David Tolnay42602292016-10-01 22:25:45 -07002401
David Tolnay89e05672016-10-02 14:39:42 -07002402 impl ToTokens for CaptureBy {
2403 fn to_tokens(&self, tokens: &mut Tokens) {
2404 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002405 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002406 CaptureBy::Ref => {
2407 // nothing
2408 }
David Tolnay89e05672016-10-02 14:39:42 -07002409 }
2410 }
2411 }
2412
David Tolnay42602292016-10-01 22:25:45 -07002413 impl ToTokens for Block {
2414 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002415 self.brace_token.surround(tokens, |tokens| {
2416 tokens.append_all(&self.stmts);
2417 });
David Tolnay42602292016-10-01 22:25:45 -07002418 }
2419 }
2420
David Tolnay42602292016-10-01 22:25:45 -07002421 impl ToTokens for Stmt {
2422 fn to_tokens(&self, tokens: &mut Tokens) {
2423 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002424 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002425 Stmt::Item(ref item) => item.to_tokens(tokens),
2426 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002427 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002428 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002429 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002430 }
David Tolnay13b3d352016-10-03 00:31:15 -07002431 Stmt::Mac(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002432 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002433 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002434 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07002435 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002436 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002437 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2438 // no semicolon
2439 }
David Tolnay13b3d352016-10-03 00:31:15 -07002440 }
2441 }
David Tolnay42602292016-10-01 22:25:45 -07002442 }
2443 }
2444 }
David Tolnay191e0582016-10-02 18:31:09 -07002445
2446 impl ToTokens for Local {
2447 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002448 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002449 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002450 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002451 self.colon_token.to_tokens(tokens);
2452 self.ty.to_tokens(tokens);
2453 self.eq_token.to_tokens(tokens);
2454 self.init.to_tokens(tokens);
2455 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002456 }
2457 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002458}