blob: 32783e11b16e41e8560b8790168c968b4856e2e9 [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
Michael Layzellb78f3b52017-06-04 19:03:03 -040032 /// E.g. 'place <- val' or `in place { val }`.
Alex Crichton62a0a592017-05-22 13:58:53 -070033 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,
David Tolnay63e3dee2017-06-03 20:13:17 -0700142 pub label: Option<Lifetime>,
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,
David Tolnay63e3dee2017-06-03 20:13:17 -0700156 pub label: Option<Lifetime>,
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,
David Tolnay63e3dee2017-06-03 20:13:17 -0700172 pub label: Option<Lifetime>,
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,
David Tolnay63e3dee2017-06-03 20:13:17 -0700183 pub label: Option<Lifetime>,
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 {
David Tolnay63e3dee2017-06-03 20:13:17 -0700276 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 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 {
David Tolnay63e3dee2017-06-03 20:13:17 -0700283 pub label: Option<Lifetime>,
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
Michael Layzell92639a52017-06-01 00:07:44 -0400592 use proc_macro2::{TokenStream, TokenKind, Delimiter};
593 use synom::{PResult, Cursor, Synom, parse_error};
Alex Crichton954046c2017-05-30 21:49:42 -0700594 use synom::tokens::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595
Michael Layzellb78f3b52017-06-04 19:03:03 -0400596 /// When we're parsing expressions which occur before blocks, like in
597 /// an if statement's condition, we cannot parse a struct literal.
598 ///
599 /// Struct literals are ambiguous in certain positions
600 /// https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700601 macro_rules! ambiguous_expr {
602 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700603 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700604 };
605 }
606
Michael Layzellb78f3b52017-06-04 19:03:03 -0400607 /// When we are parsing an optional suffix expression, we cannot allow
608 /// blocks if structs are not allowed.
609 ///
610 /// Example:
611 /// ```ignore
612 /// if break { } { }
613 /// // is ambiguous between:
614 /// if (break { }) { }
615 /// // - or -
616 /// if (break) { } { }
617 /// ```
618 macro_rules! opt_ambiguous_expr {
619 ($i:expr, $allow_struct:ident) => {
620 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
621 };
622 }
623
Alex Crichton954046c2017-05-30 21:49:42 -0700624 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400625 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700626
627 fn description() -> Option<&'static str> {
628 Some("expression")
629 }
630 }
631
David Tolnayaf2557e2016-10-24 11:52:21 -0700632
633 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
634
Michael Layzellb78f3b52017-06-04 19:03:03 -0400635 /// Parse an arbitrary expression.
636 pub fn ambiguous_expr(i: Cursor,
637 allow_struct: bool,
638 allow_block: bool)
639 -> PResult<Expr> {
640 map!(
David Tolnay54e854d2016-10-24 12:03:30 -0700641 i,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400642 call!(assign_expr, allow_struct, allow_block),
643 ExprKind::into
644 )
645 }
646
647 /// Parse a left-associative binary operator.
648 macro_rules! binop {
649 (
650 $name: ident,
651 $next: ident,
652 $submac: ident!( $($args:tt)* )
653 ) => {
654 named!($name(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
655 mut e: call!($next, allow_struct, allow_block) >>
656 many0!(do_parse!(
657 op: $submac!($($args)*) >>
658 rhs: call!($next, allow_struct, true) >>
659 ({
660 e = ExprBinary {
661 left: Box::new(e.into()),
662 op: op,
663 right: Box::new(rhs.into()),
664 }.into();
665 })
666 )) >>
667 (e)
668 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700669 }
David Tolnay54e854d2016-10-24 12:03:30 -0700670 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700671
Michael Layzellb78f3b52017-06-04 19:03:03 -0400672 /// ```ignore
673 /// <placement> = <placement> ..
674 /// <placement> += <placement> ..
675 /// <placement> -= <placement> ..
676 /// <placement> *= <placement> ..
677 /// <placement> /= <placement> ..
678 /// <placement> %= <placement> ..
679 /// <placement> ^= <placement> ..
680 /// <placement> &= <placement> ..
681 /// <placement> |= <placement> ..
682 /// <placement> <<= <placement> ..
683 /// <placement> >>= <placement> ..
684 /// ```
685 ///
686 /// NOTE: This operator is right-associative.
687 named!(assign_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
688 mut e: call!(placement_expr, allow_struct, allow_block) >>
689 alt!(
690 do_parse!(
691 eq: syn!(Eq) >>
692 // Recurse into self to parse right-associative operator.
693 rhs: call!(assign_expr, allow_struct, true) >>
694 ({
695 e = ExprAssign {
696 left: Box::new(e.into()),
697 eq_token: eq,
698 right: Box::new(rhs.into()),
699 }.into();
700 })
701 )
702 |
703 do_parse!(
704 op: call!(BinOp::parse_assign_op) >>
705 // Recurse into self to parse right-associative operator.
706 rhs: call!(assign_expr, allow_struct, true) >>
707 ({
708 e = ExprAssignOp {
709 left: Box::new(e.into()),
710 op: op,
711 right: Box::new(rhs.into()),
712 }.into();
713 })
714 )
715 |
716 epsilon!()
717 ) >>
718 (e)
719 ));
720
721 /// ```ignore
722 /// <range> <- <range> ..
723 /// ```
724 ///
725 /// NOTE: The `in place { expr }` version of this syntax is parsed in
726 /// `atom_expr`, not here.
727 ///
728 /// NOTE: This operator is right-associative.
729 named!(placement_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
730 mut e: call!(range_expr, allow_struct, allow_block) >>
731 alt!(
732 do_parse!(
733 syn!(LArrow) >>
734 // Recurse into self to parse right-associative operator.
735 rhs: call!(placement_expr, allow_struct, true) >>
736 ({
737 // XXX: Stop transforming the <- syntax into the InPlace
738 // syntax.
739 e = ExprInPlace {
740 // op: BinOp::Place(larrow),
741 place: Box::new(e.into()),
742 value: Box::new(rhs.into()),
743 in_token: tokens::In::default(),
744 }.into();
745 })
746 )
747 |
748 epsilon!()
749 ) >>
750 (e)
751 ));
752
753 /// ```ignore
754 /// <or> ... <or> ..
755 /// <or> .. <or> ..
756 /// <or> ..
757 /// ```
758 ///
759 /// NOTE: This is currently parsed oddly - I'm not sure of what the exact
760 /// rules are for parsing these expressions are, but this is not correct.
761 /// For example, `a .. b .. c` is not a legal expression. It should not
762 /// be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
763 ///
764 /// NOTE: The form of ranges which don't include a preceding expression are
765 /// parsed by `atom_expr`, rather than by this function.
766 named!(range_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
767 mut e: call!(or_expr, allow_struct, allow_block) >>
768 many0!(do_parse!(
769 limits: syn!(RangeLimits) >>
770 // We don't want to allow blocks here if we don't allow structs. See
771 // the reasoning for `opt_ambiguous_expr!` above.
772 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
773 ({
774 e = ExprRange {
775 from: Some(Box::new(e.into())),
776 limits: limits,
777 to: hi.map(|e| Box::new(e.into())),
778 }.into();
779 })
780 )) >>
781 (e)
782 ));
783
784 /// ```ignore
785 /// <and> || <and> ...
786 /// ```
787 binop!(or_expr, and_expr, map!(syn!(OrOr), BinOp::Or));
788
789 /// ```ignore
790 /// <compare> && <compare> ...
791 /// ```
792 binop!(and_expr, compare_expr, map!(syn!(AndAnd), BinOp::And));
793
794 /// ```ignore
795 /// <bitor> == <bitor> ...
796 /// <bitor> != <bitor> ...
797 /// <bitor> >= <bitor> ...
798 /// <bitor> <= <bitor> ...
799 /// <bitor> > <bitor> ...
800 /// <bitor> < <bitor> ...
801 /// ```
802 ///
803 /// NOTE: This operator appears to be parsed as left-associative, but errors
804 /// if it is used in a non-associative manner.
805 binop!(compare_expr, bitor_expr, alt!(
806 syn!(EqEq) => { BinOp::Eq }
807 |
808 syn!(Ne) => { BinOp::Ne }
809 |
810 // must be above Lt
811 syn!(Le) => { BinOp::Le }
812 |
813 // must be above Gt
814 syn!(Ge) => { BinOp::Ge }
815 |
816 syn!(Lt) => { BinOp::Lt }
817 |
818 syn!(Gt) => { BinOp::Gt }
819 ));
820
821 /// ```ignore
822 /// <bitxor> | <bitxor> ...
823 /// ```
824 binop!(bitor_expr, bitxor_expr, do_parse!(
825 not!(syn!(OrOr)) >>
826 not!(syn!(OrEq)) >>
827 t: syn!(Or) >>
828 (BinOp::BitOr(t))
829 ));
830
831 /// ```ignore
832 /// <bitand> ^ <bitand> ...
833 /// ```
834 binop!(bitxor_expr, bitand_expr, do_parse!(
835 // NOTE: Make sure we aren't looking at ^=.
836 not!(syn!(CaretEq)) >>
837 t: syn!(Caret) >>
838 (BinOp::BitXor(t))
839 ));
840
841 /// ```ignore
842 /// <shift> & <shift> ...
843 /// ```
844 binop!(bitand_expr, shift_expr, do_parse!(
845 // NOTE: Make sure we aren't looking at && or &=.
846 not!(syn!(AndAnd)) >>
847 not!(syn!(AndEq)) >>
848 t: syn!(And) >>
849 (BinOp::BitAnd(t))
850 ));
851
852 /// ```ignore
853 /// <arith> << <arith> ...
854 /// <arith> >> <arith> ...
855 /// ```
856 binop!(shift_expr, arith_expr, alt!(
857 syn!(Shl) => { BinOp::Shl }
858 |
859 syn!(Shr) => { BinOp::Shr }
860 ));
861
862 /// ```ignore
863 /// <term> + <term> ...
864 /// <term> - <term> ...
865 /// ```
866 binop!(arith_expr, term_expr, alt!(
867 syn!(Add) => { BinOp::Add }
868 |
869 syn!(Sub) => { BinOp::Sub }
870 ));
871
872 /// ```ignore
873 /// <cast> * <cast> ...
874 /// <cast> / <cast> ...
875 /// <cast> % <cast> ...
876 /// ```
877 binop!(term_expr, cast_expr, alt!(
878 syn!(Star) => { BinOp::Mul }
879 |
880 syn!(Div) => { BinOp::Div }
881 |
882 syn!(Rem) => { BinOp::Rem }
883 ));
884
885 /// ```ignore
886 /// <unary> as <ty>
887 /// <unary> : <ty>
888 /// ```
889 named!(cast_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
890 mut e: call!(unary_expr, allow_struct, allow_block) >>
891 many0!(alt!(
892 do_parse!(
893 as_: syn!(As) >>
894 // We can't accept `A + B` in cast expressions, as it's
895 // ambiguous with the + expression.
896 ty: call!(Ty::without_plus) >>
897 ({
898 e = ExprCast {
899 expr: Box::new(e.into()),
900 as_token: as_,
901 ty: Box::new(ty),
902 }.into();
903 })
904 )
905 |
906 do_parse!(
907 colon: syn!(Colon) >>
908 // We can't accept `A + B` in cast expressions, as it's
909 // ambiguous with the + expression.
910 ty: call!(Ty::without_plus) >>
911 ({
912 e = ExprType {
913 expr: Box::new(e.into()),
914 colon_token: colon,
915 ty: Box::new(ty),
916 }.into();
917 })
918 )
919 )) >>
920 (e)
921 ));
922
923 /// ```
924 /// <UnOp> <trailer>
925 /// & <trailer>
926 /// &mut <trailer>
927 /// box <trailer>
928 /// ```
929 named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
930 do_parse!(
931 op: syn!(UnOp) >>
932 expr: call!(unary_expr, allow_struct, true) >>
933 (ExprUnary {
934 op: op,
935 expr: Box::new(expr.into()),
936 }.into())
937 )
938 |
939 do_parse!(
940 and: syn!(And) >>
941 mutability: syn!(Mutability) >>
942 expr: call!(unary_expr, allow_struct, true) >>
943 (ExprAddrOf {
944 and_token: and,
945 mutbl: mutability,
946 expr: Box::new(expr.into()),
947 }.into())
948 )
949 |
950 do_parse!(
951 box_: syn!(Box_) >>
952 expr: call!(unary_expr, allow_struct, true) >>
953 (ExprBox {
954 box_token: box_,
955 expr: Box::new(expr.into()),
956 }.into())
957 )
958 |
959 call!(trailer_expr, allow_struct, allow_block)
960 ));
961
962 /// ```ignore
963 /// <atom> (..<args>) ...
964 /// <atom> . <ident> (..<args>) ...
965 /// <atom> . <ident> ...
966 /// <atom> . <lit> ...
967 /// <atom> [ <expr> ] ...
968 /// <atom> ? ...
969 /// ```
970 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
971 mut e: call!(atom_expr, allow_struct, allow_block) >>
972 many0!(alt!(
973 tap!(args: and_call => {
974 let (args, paren) = args;
975 e = ExprCall {
976 func: Box::new(e.into()),
977 args: args,
978 paren_token: paren,
979 }.into();
980 })
981 |
982 tap!(more: and_method_call => {
983 let mut call = more;
984 call.expr = Box::new(e.into());
985 e = call.into();
986 })
987 |
988 tap!(field: and_field => {
989 let (field, token) = field;
990 e = ExprField {
991 expr: Box::new(e.into()),
992 field: field,
993 dot_token: token,
994 }.into();
995 })
996 |
997 tap!(field: and_tup_field => {
998 let (field, token) = field;
999 e = ExprTupField {
1000 expr: Box::new(e.into()),
1001 field: field,
1002 dot_token: token,
1003 }.into();
1004 })
1005 |
1006 tap!(i: and_index => {
1007 let (i, token) = i;
1008 e = ExprIndex {
1009 expr: Box::new(e.into()),
1010 bracket_token: token,
1011 index: Box::new(i),
1012 }.into();
1013 })
1014 |
1015 tap!(question: syn!(Question) => {
1016 e = ExprTry {
1017 expr: Box::new(e.into()),
1018 question_token: question,
1019 }.into();
1020 })
1021 )) >>
1022 (e)
1023 ));
1024
1025 /// Parse all atomic expressions which don't have to worry about precidence
1026 /// interactions, as they are fully contained.
1027 named!(atom_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
1028 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
1029 |
1030 // must be before expr_path
1031 cond_reduce!(allow_struct, map!(syn!(ExprStruct), ExprKind::Struct))
1032 |
1033 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
1034 |
1035 syn!(Mac) => { ExprKind::Mac } // must be before expr_path
1036 |
1037 call!(expr_break, allow_struct) // must be before expr_path
1038 |
1039 syn!(ExprContinue) => { ExprKind::Continue } // must be before expr_path
1040 |
1041 call!(expr_ret, allow_struct) // must be before expr_path
1042 |
1043 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
1044 syn!(ExprInPlace) => { ExprKind::InPlace }
1045 |
1046 syn!(ExprArray) => { ExprKind::Array }
1047 |
1048 syn!(ExprTup) => { ExprKind::Tup }
1049 |
1050 syn!(ExprIf) => { ExprKind::If }
1051 |
1052 syn!(ExprIfLet) => { ExprKind::IfLet }
1053 |
1054 syn!(ExprWhile) => { ExprKind::While }
1055 |
1056 syn!(ExprWhileLet) => { ExprKind::WhileLet }
1057 |
1058 syn!(ExprForLoop) => { ExprKind::ForLoop }
1059 |
1060 syn!(ExprLoop) => { ExprKind::Loop }
1061 |
1062 syn!(ExprMatch) => { ExprKind::Match }
1063 |
1064 syn!(ExprCatch) => { ExprKind::Catch }
1065 |
1066 call!(expr_closure, allow_struct)
1067 |
1068 cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
1069 |
1070 // NOTE: This is the prefix-form of range
1071 call!(expr_range, allow_struct)
1072 |
1073 syn!(ExprPath) => { ExprKind::Path }
1074 |
1075 syn!(ExprRepeat) => { ExprKind::Repeat }
1076 ));
1077
Alex Crichton954046c2017-05-30 21:49:42 -07001078 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001079 named!(parse -> Self, do_parse!(
1080 e: parens!(syn!(Expr)) >>
1081 (ExprParen {
1082 expr: Box::new(e.0),
1083 paren_token: e.1,
1084 }.into())
1085 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001086 }
David Tolnay89e05672016-10-02 14:39:42 -07001087
Alex Crichton954046c2017-05-30 21:49:42 -07001088 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001089 named!(parse -> Self, do_parse!(
1090 in_: syn!(In) >>
1091 place: expr_no_struct >>
1092 value: braces!(call!(Block::parse_within)) >>
1093 (ExprInPlace {
1094 in_token: in_,
1095 place: Box::new(place),
1096 value: Box::new(Expr {
1097 node: ExprBlock {
1098 unsafety: Unsafety::Normal,
1099 block: Block {
1100 stmts: value.0,
1101 brace_token: value.1,
1102 },
1103 }.into(),
1104 attrs: Vec::new(),
1105 }),
1106 })
1107 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001108 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001109
Alex Crichton954046c2017-05-30 21:49:42 -07001110 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001111 named!(parse -> Self, do_parse!(
1112 elems: brackets!(call!(Delimited::parse_terminated)) >>
1113 (ExprArray {
1114 exprs: elems.0,
1115 bracket_token: elems.1,
1116 })
1117 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001118 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001119
Alex Crichton954046c2017-05-30 21:49:42 -07001120 named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren),
1121 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001122
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001123 named!(and_method_call -> ExprMethodCall, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001124 dot: syn!(Dot) >>
1125 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001126 typarams: option!(do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001127 colon2: syn!(Colon2) >>
1128 lt: syn!(Lt) >>
1129 tys: call!(Delimited::parse_terminated) >>
1130 gt: syn!(Gt) >>
1131 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001132 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001133 args: parens!(call!(Delimited::parse_terminated)) >>
1134 ({
1135 let (colon2, lt, tys, gt) = match typarams {
1136 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1137 None => (None, None, None, None),
1138 };
1139 ExprMethodCall {
1140 // this expr will get overwritten after being returned
1141 expr: Box::new(ExprKind::Lit(Lit {
1142 span: Span::default(),
1143 value: LitKind::Bool(false),
1144 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001145
Alex Crichton954046c2017-05-30 21:49:42 -07001146 method: method,
1147 args: args.0,
1148 paren_token: args.1,
1149 dot_token: dot,
1150 lt_token: lt,
1151 gt_token: gt,
1152 colon2_token: colon2,
1153 typarams: tys.unwrap_or_default(),
1154 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001155 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001156 ));
1157
Alex Crichton954046c2017-05-30 21:49:42 -07001158 impl Synom for ExprTup {
Michael Layzell92639a52017-06-01 00:07:44 -04001159 named!(parse -> Self, do_parse!(
1160 elems: parens!(call!(Delimited::parse_terminated)) >>
1161 (ExprTup {
1162 args: elems.0,
1163 paren_token: elems.1,
1164 lone_comma: None, // TODO: parse this
1165 })
1166 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001167 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001168
Alex Crichton954046c2017-05-30 21:49:42 -07001169 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001170 named!(parse -> Self, do_parse!(
1171 if_: syn!(If) >>
1172 let_: syn!(Let) >>
1173 pat: syn!(Pat) >>
1174 eq: syn!(Eq) >>
1175 cond: expr_no_struct >>
1176 then_block: braces!(call!(Block::parse_within)) >>
1177 else_block: option!(else_block) >>
1178 (ExprIfLet {
1179 pat: Box::new(pat),
1180 let_token: let_,
1181 eq_token: eq,
1182 expr: Box::new(cond),
1183 if_true: Block {
1184 stmts: then_block.0,
1185 brace_token: then_block.1,
1186 },
1187 if_token: if_,
1188 else_token: else_block.as_ref().map(|p| Else((p.0).0)),
1189 if_false: else_block.map(|p| Box::new(p.1.into())),
1190 })
1191 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001192 }
1193
Alex Crichton954046c2017-05-30 21:49:42 -07001194 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001195 named!(parse -> Self, do_parse!(
1196 if_: syn!(If) >>
1197 cond: expr_no_struct >>
1198 then_block: braces!(call!(Block::parse_within)) >>
1199 else_block: option!(else_block) >>
1200 (ExprIf {
1201 cond: Box::new(cond),
1202 if_true: Block {
1203 stmts: then_block.0,
1204 brace_token: then_block.1,
1205 },
1206 if_token: if_,
1207 else_token: else_block.as_ref().map(|p| Else((p.0).0)),
1208 if_false: else_block.map(|p| Box::new(p.1.into())),
1209 })
1210 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001211 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001212
Alex Crichton954046c2017-05-30 21:49:42 -07001213 named!(else_block -> (Else, ExprKind), do_parse!(
1214 else_: syn!(Else) >>
1215 expr: alt!(
1216 syn!(ExprIf) => { ExprKind::If }
1217 |
1218 syn!(ExprIfLet) => { ExprKind::IfLet }
1219 |
1220 do_parse!(
1221 else_block: braces!(call!(Block::parse_within)) >>
1222 (ExprKind::Block(ExprBlock {
1223 unsafety: Unsafety::Normal,
1224 block: Block {
1225 stmts: else_block.0,
1226 brace_token: else_block.1,
1227 },
1228 }))
David Tolnay939766a2016-09-23 23:48:12 -07001229 )
Alex Crichton954046c2017-05-30 21:49:42 -07001230 ) >>
1231 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001232 ));
1233
David Tolnaybb6feae2016-10-02 21:25:20 -07001234
Alex Crichton954046c2017-05-30 21:49:42 -07001235 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001236 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001237 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001238 for_: syn!(For) >>
1239 pat: syn!(Pat) >>
1240 in_: syn!(In) >>
1241 expr: expr_no_struct >>
1242 loop_block: syn!(Block) >>
1243 (ExprForLoop {
1244 for_token: for_,
1245 in_token: in_,
1246 pat: Box::new(pat),
1247 expr: Box::new(expr),
1248 body: loop_block,
1249 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1250 label: lbl.map(|p| p.0),
1251 })
1252 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001253 }
Gregory Katze5f35682016-09-27 14:20:55 -04001254
Alex Crichton954046c2017-05-30 21:49:42 -07001255 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001256 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001257 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001258 loop_: syn!(Loop) >>
1259 loop_block: syn!(Block) >>
1260 (ExprLoop {
1261 loop_token: loop_,
1262 body: loop_block,
1263 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1264 label: lbl.map(|p| p.0),
1265 })
1266 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001267 }
1268
1269 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001270 named!(parse -> Self, do_parse!(
1271 match_: syn!(Match) >>
1272 obj: expr_no_struct >>
1273 res: braces!(do_parse!(
1274 mut arms: many0!(do_parse!(
1275 arm: syn!(Arm) >>
1276 cond!(arm_requires_comma(&arm), syn!(Comma)) >>
1277 cond!(!arm_requires_comma(&arm), option!(syn!(Comma))) >>
1278 (arm)
Alex Crichton954046c2017-05-30 21:49:42 -07001279 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001280 last_arm: option!(syn!(Arm)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001281 ({
Michael Layzell92639a52017-06-01 00:07:44 -04001282 arms.extend(last_arm);
1283 arms
Alex Crichton954046c2017-05-30 21:49:42 -07001284 })
Michael Layzell92639a52017-06-01 00:07:44 -04001285 )) >>
1286 ({
1287 let (mut arms, brace) = res;
1288 ExprMatch {
1289 expr: Box::new(obj),
1290 match_token: match_,
1291 brace_token: brace,
1292 arms: {
1293 for arm in &mut arms {
1294 if arm_requires_comma(arm) {
1295 arm.comma = Some(tokens::Comma::default());
1296 }
1297 }
1298 arms
1299 },
1300 }
1301 })
1302 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001303 }
David Tolnay1978c672016-10-27 22:05:52 -07001304
Alex Crichton954046c2017-05-30 21:49:42 -07001305 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001306 named!(parse -> Self, do_parse!(
1307 do_: syn!(Do) >>
1308 catch_: syn!(Catch) >>
1309 catch_block: syn!(Block) >>
1310 (ExprCatch {
1311 block: catch_block,
1312 do_token: do_,
1313 catch_token: catch_,
1314 }.into())
1315 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001316 }
Arnavion02ef13f2017-04-25 00:54:31 -07001317
David Tolnay1978c672016-10-27 22:05:52 -07001318 fn arm_requires_comma(arm: &Arm) -> bool {
Alex Crichton62a0a592017-05-22 13:58:53 -07001319 if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
David Tolnay1978c672016-10-27 22:05:52 -07001320 false
1321 } else {
1322 true
1323 }
1324 }
1325
Alex Crichton954046c2017-05-30 21:49:42 -07001326 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001327 named!(parse -> Self, do_parse!(
1328 attrs: many0!(call!(Attribute::parse_outer)) >>
1329 pats: call!(Delimited::parse_separated_nonempty) >>
1330 guard: option!(tuple!(syn!(If), syn!(Expr))) >>
1331 rocket: syn!(Rocket) >>
1332 body: alt!(
1333 map!(syn!(Block), |blk| {
1334 ExprKind::Block(ExprBlock {
1335 unsafety: Unsafety::Normal,
1336 block: blk,
1337 }).into()
Alex Crichton954046c2017-05-30 21:49:42 -07001338 })
Michael Layzell92639a52017-06-01 00:07:44 -04001339 |
1340 syn!(Expr)
1341 ) >>
1342 (Arm {
1343 rocket_token: rocket,
1344 if_token: guard.as_ref().map(|p| If((p.0).0)),
1345 attrs: attrs,
1346 pats: pats,
1347 guard: guard.map(|p| Box::new(p.1)),
1348 body: Box::new(body),
1349 comma: None,
1350 })
1351 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001352 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001353
Michael Layzellb78f3b52017-06-04 19:03:03 -04001354 named!(expr_closure(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001355 capture: syn!(CaptureBy) >>
1356 or1: syn!(Or) >>
1357 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
1358 or2: syn!(Or) >>
David Tolnay89e05672016-10-02 14:39:42 -07001359 ret_and_body: alt!(
1360 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001361 arrow: syn!(RArrow) >>
1362 ty: syn!(Ty) >>
1363 body: syn!(Block) >>
1364 (FunctionRetTy::Ty(ty, arrow),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001365 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001366 unsafety: Unsafety::Normal,
1367 block: body,
1368 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001369 )
1370 |
David Tolnay58af3552016-12-22 16:58:07 -05001371 map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001372 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001373 (ExprClosure {
1374 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001375 or1_token: or1,
1376 or2_token: or2,
Alex Crichton62a0a592017-05-22 13:58:53 -07001377 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -07001378 inputs: inputs,
1379 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -07001380 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001381 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001382 fn_token: tokens::Fn_::default(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001383 generics: Generics::default(),
1384 paren_token: tokens::Paren::default(),
David Tolnay89e05672016-10-02 14:39:42 -07001385 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001386 body: Box::new(ret_and_body.1),
1387 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001388 ));
1389
Alex Crichton954046c2017-05-30 21:49:42 -07001390 named!(fn_arg -> FnArg, do_parse!(
1391 pat: syn!(Pat) >>
1392 ty: option!(tuple!(syn!(Colon), syn!(Ty))) >>
1393 ({
1394 let (colon, ty) = ty.unwrap_or_else(|| {
1395 (Colon::default(), TyInfer {
1396 underscore_token: Underscore::default(),
1397 }.into())
1398 });
1399 ArgCaptured {
1400 pat: pat,
1401 colon_token: colon,
1402 ty: ty,
1403 }.into()
David Tolnaybb6feae2016-10-02 21:25:20 -07001404 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001405 ));
1406
Alex Crichton954046c2017-05-30 21:49:42 -07001407 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001408 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001409 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001410 while_: syn!(While) >>
1411 cond: expr_no_struct >>
1412 while_block: syn!(Block) >>
1413 (ExprWhile {
1414 while_token: while_,
1415 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1416 cond: Box::new(cond),
1417 body: while_block,
1418 label: lbl.map(|p| p.0),
1419 })
1420 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001421 }
1422
1423 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001424 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001425 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001426 while_: syn!(While) >>
1427 let_: syn!(Let) >>
1428 pat: syn!(Pat) >>
1429 eq: syn!(Eq) >>
1430 value: expr_no_struct >>
1431 while_block: syn!(Block) >>
1432 (ExprWhileLet {
1433 eq_token: eq,
1434 let_token: let_,
1435 while_token: while_,
1436 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1437 pat: Box::new(pat),
1438 expr: Box::new(value),
1439 body: while_block,
1440 label: lbl.map(|p| p.0),
1441 })
1442 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001443 }
1444
1445 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001446 named!(parse -> Self, do_parse!(
1447 cont: syn!(Continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001448 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001449 (ExprContinue {
1450 continue_token: cont,
1451 label: lbl,
1452 })
1453 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001454 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001455
Michael Layzellb78f3b52017-06-04 19:03:03 -04001456 named!(expr_break(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001457 break_: syn!(Break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001458 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001459 // We can't allow blocks after a `break` expression when we wouldn't
1460 // allow structs, as this expression is ambiguous.
1461 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001462 (ExprBreak {
1463 label: lbl,
1464 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001465 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001466 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001467 ));
1468
Michael Layzellb78f3b52017-06-04 19:03:03 -04001469 named!(expr_ret(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001470 return_: syn!(Return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001471 // NOTE: return is greedy and eats blocks after it even when in a
1472 // position where structs are not allowed, such as in if statement
1473 // conditions. For example:
1474 //
1475 // if return { println!("A") } { } // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001476 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001477 (ExprRet {
1478 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001479 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001480 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001481 ));
1482
Alex Crichton954046c2017-05-30 21:49:42 -07001483 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001484 named!(parse -> Self, do_parse!(
1485 path: syn!(Path) >>
1486 data: braces!(do_parse!(
1487 fields: call!(Delimited::parse_terminated) >>
1488 base: option!(
1489 cond!(fields.is_empty() || fields.trailing_delim(),
1490 do_parse!(
1491 dots: syn!(Dot2) >>
1492 base: syn!(Expr) >>
1493 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001494 )
Michael Layzell92639a52017-06-01 00:07:44 -04001495 )
1496 ) >>
1497 (fields, base)
1498 )) >>
1499 ({
1500 let ((fields, base), brace) = data;
1501 let (dots, rest) = match base.and_then(|b| b) {
1502 Some((dots, base)) => (Some(dots), Some(base)),
1503 None => (None, None),
1504 };
1505 ExprStruct {
1506 brace_token: brace,
1507 path: path,
1508 fields: fields,
1509 dot2_token: dots,
1510 rest: rest.map(Box::new),
1511 }
1512 })
1513 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001514 }
1515
1516 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001517 named!(parse -> Self, alt!(
1518 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -07001519 ident: field_ident >>
Michael Layzell92639a52017-06-01 00:07:44 -04001520 colon: syn!(Colon) >>
1521 value: syn!(Expr) >>
1522 (FieldValue {
David Tolnay570695e2017-06-03 16:15:13 -07001523 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04001524 expr: value,
1525 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001526 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001527 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001528 })
Michael Layzell92639a52017-06-01 00:07:44 -04001529 )
1530 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001531 map!(syn!(Ident), |name| FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001532 ident: name.clone(),
1533 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
1534 is_shorthand: true,
1535 attrs: Vec::new(),
1536 colon_token: None,
1537 })
1538 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001539 }
David Tolnay055a7042016-10-02 19:23:54 -07001540
Alex Crichton954046c2017-05-30 21:49:42 -07001541 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001542 named!(parse -> Self, do_parse!(
1543 data: brackets!(do_parse!(
1544 value: syn!(Expr) >>
1545 semi: syn!(Semi) >>
1546 times: syn!(Expr) >>
1547 (value, semi, times)
1548 )) >>
1549 (ExprRepeat {
1550 expr: Box::new((data.0).0),
1551 amt: Box::new((data.0).2),
1552 bracket_token: data.1,
1553 semi_token: (data.0).1,
1554 })
1555 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001556 }
David Tolnay055a7042016-10-02 19:23:54 -07001557
Alex Crichton954046c2017-05-30 21:49:42 -07001558 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001559 named!(parse -> Self, do_parse!(
1560 rules: syn!(Unsafety) >>
1561 b: syn!(Block) >>
1562 (ExprBlock {
1563 unsafety: rules,
1564 block: b,
1565 })
1566 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001567 }
David Tolnay89e05672016-10-02 14:39:42 -07001568
Michael Layzellb78f3b52017-06-04 19:03:03 -04001569 named!(expr_range(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001570 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001571 hi: opt_ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001572 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001573 ));
1574
Alex Crichton954046c2017-05-30 21:49:42 -07001575 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001576 named!(parse -> Self, alt!(
1577 // Must come before Dot2
1578 syn!(Dot3) => { RangeLimits::Closed }
1579 |
1580 syn!(Dot2) => { RangeLimits::HalfOpen }
1581 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001582 }
David Tolnay438c9052016-10-07 23:24:48 -07001583
Alex Crichton954046c2017-05-30 21:49:42 -07001584 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001585 named!(parse -> Self, do_parse!(
1586 pair: qpath >>
1587 (ExprPath {
1588 qself: pair.0,
1589 path: pair.1,
1590 })
1591 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001592 }
David Tolnay42602292016-10-01 22:25:45 -07001593
Alex Crichton954046c2017-05-30 21:49:42 -07001594 named!(and_field -> (Ident, Dot),
1595 map!(tuple!(syn!(Dot), syn!(Ident)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001596
Alex Crichton954046c2017-05-30 21:49:42 -07001597 named!(and_tup_field -> (Lit, Dot),
1598 map!(tuple!(syn!(Dot), syn!(Lit)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001599
Alex Crichton954046c2017-05-30 21:49:42 -07001600 named!(and_index -> (Expr, tokens::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001601
Alex Crichton954046c2017-05-30 21:49:42 -07001602 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001603 named!(parse -> Self, do_parse!(
1604 stmts: braces!(call!(Block::parse_within)) >>
1605 (Block {
1606 stmts: stmts.0,
1607 brace_token: stmts.1,
1608 })
1609 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001610 }
David Tolnay939766a2016-09-23 23:48:12 -07001611
Alex Crichton954046c2017-05-30 21:49:42 -07001612 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001613 named!(pub parse_within -> Vec<Stmt>, do_parse!(
1614 many0!(syn!(Semi)) >>
1615 mut standalone: many0!(terminated!(syn!(Stmt), many0!(syn!(Semi)))) >>
1616 last: option!(syn!(Expr)) >>
1617 (match last {
1618 None => standalone,
1619 Some(last) => {
1620 standalone.push(Stmt::Expr(Box::new(last)));
1621 standalone
1622 }
1623 })
1624 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001625 }
1626
1627 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001628 named!(parse -> Self, alt!(
1629 stmt_mac
1630 |
1631 stmt_local
1632 |
1633 stmt_item
1634 |
1635 stmt_expr
1636 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001637 }
David Tolnay939766a2016-09-23 23:48:12 -07001638
David Tolnay13b3d352016-10-03 00:31:15 -07001639 named!(stmt_mac -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001640 attrs: many0!(call!(Attribute::parse_outer)) >>
1641 what: syn!(Path) >>
1642 bang: syn!(Bang) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001643 // Only parse braces here; paren and bracket will get parsed as
1644 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001645 data: braces!(syn!(TokenStream)) >>
1646 semi: option!(syn!(Semi)) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001647 (Stmt::Mac(Box::new((
1648 Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001649 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001650 bang_token: bang,
David Tolnay570695e2017-06-03 16:15:13 -07001651 ident: None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001652 tokens: vec![TokenTree(proc_macro2::TokenTree {
Alex Crichton954046c2017-05-30 21:49:42 -07001653 span: ((data.1).0).0,
1654 kind: TokenKind::Sequence(Delimiter::Brace, data.0),
David Tolnayeea28d62016-10-25 20:44:08 -07001655 })],
1656 },
Alex Crichton954046c2017-05-30 21:49:42 -07001657 match semi {
1658 Some(semi) => MacStmtStyle::Semicolon(semi),
1659 None => MacStmtStyle::Braces,
David Tolnay60d48942016-10-30 14:34:52 -07001660 },
David Tolnayeea28d62016-10-25 20:44:08 -07001661 attrs,
1662 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001663 ));
1664
David Tolnay191e0582016-10-02 18:31:09 -07001665 named!(stmt_local -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001666 attrs: many0!(call!(Attribute::parse_outer)) >>
1667 let_: syn!(Let) >>
1668 pat: syn!(Pat) >>
1669 ty: option!(tuple!(syn!(Colon), syn!(Ty))) >>
1670 init: option!(tuple!(syn!(Eq), syn!(Expr))) >>
1671 semi: syn!(Semi) >>
David Tolnay191e0582016-10-02 18:31:09 -07001672 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001673 let_token: let_,
1674 semi_token: semi,
1675 colon_token: ty.as_ref().map(|p| Colon((p.0).0)),
1676 eq_token: init.as_ref().map(|p| Eq((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001677 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001678 ty: ty.map(|p| Box::new(p.1)),
1679 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001680 attrs: attrs,
1681 })))
1682 ));
1683
Alex Crichton954046c2017-05-30 21:49:42 -07001684 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07001685
David Tolnaycfe55022016-10-02 22:02:27 -07001686 fn requires_semi(e: &Expr) -> bool {
David Tolnay7184b132016-10-30 10:06:37 -07001687 match e.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001688 ExprKind::If(_) |
1689 ExprKind::IfLet(_) |
1690 ExprKind::While(_) |
1691 ExprKind::WhileLet(_) |
1692 ExprKind::ForLoop(_) |
1693 ExprKind::Loop(_) |
1694 ExprKind::Match(_) |
1695 ExprKind::Block(_) => false,
David Tolnaycfe55022016-10-02 22:02:27 -07001696
1697 _ => true,
1698 }
1699 }
1700
1701 named!(stmt_expr -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001702 attrs: many0!(call!(Attribute::parse_outer)) >>
1703 mut e: syn!(Expr) >>
1704 semi: option!(syn!(Semi)) >>
David Tolnay7184b132016-10-30 10:06:37 -07001705 ({
1706 e.attrs = attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001707 if let Some(s) = semi {
1708 Stmt::Semi(Box::new(e), s)
David Tolnay092dcb02016-10-30 10:14:14 -07001709 } else if requires_semi(&e) {
Michael Layzell92639a52017-06-01 00:07:44 -04001710 return parse_error();
David Tolnay7184b132016-10-30 10:06:37 -07001711 } else {
1712 Stmt::Expr(Box::new(e))
1713 }
David Tolnaycfe55022016-10-02 22:02:27 -07001714 })
David Tolnay939766a2016-09-23 23:48:12 -07001715 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001716
Alex Crichton954046c2017-05-30 21:49:42 -07001717 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04001718 named!(parse -> Self, alt!(
1719 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
1720 |
1721 syn!(PatBox) => { Pat::Box } // must be before pat_ident
1722 |
1723 syn!(PatRange) => { Pat::Range } // must be before pat_lit
1724 |
1725 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
1726 |
1727 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
1728 |
1729 syn!(Mac) => { Pat::Mac } // must be before pat_ident
1730 |
1731 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
1732 |
1733 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
1734 |
1735 syn!(PatPath) => { Pat::Path }
1736 |
1737 syn!(PatTuple) => { Pat::Tuple }
1738 |
1739 syn!(PatRef) => { Pat::Ref }
1740 |
1741 syn!(PatSlice) => { Pat::Slice }
1742 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001743 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001744
Alex Crichton954046c2017-05-30 21:49:42 -07001745 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04001746 named!(parse -> Self, map!(
1747 syn!(Underscore),
1748 |u| PatWild { underscore_token: u }
1749 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001750 }
David Tolnay84aa0752016-10-02 23:01:13 -07001751
Alex Crichton954046c2017-05-30 21:49:42 -07001752 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04001753 named!(parse -> Self, do_parse!(
1754 boxed: syn!(Box_) >>
1755 pat: syn!(Pat) >>
1756 (PatBox {
1757 pat: Box::new(pat),
1758 box_token: boxed,
1759 })
1760 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001761 }
1762
1763 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04001764 named!(parse -> Self, do_parse!(
1765 mode: option!(syn!(Ref)) >>
1766 mutability: syn!(Mutability) >>
1767 name: alt!(
1768 syn!(Ident)
1769 |
1770 syn!(Self_) => { Into::into }
1771 ) >>
1772 not!(syn!(Lt)) >>
1773 not!(syn!(Colon2)) >>
1774 subpat: option!(tuple!(syn!(At), syn!(Pat))) >>
1775 (PatIdent {
1776 mode: match mode {
1777 Some(mode) => BindingMode::ByRef(mode, mutability),
1778 None => BindingMode::ByValue(mutability),
1779 },
1780 ident: name,
1781 at_token: subpat.as_ref().map(|p| At((p.0).0)),
1782 subpat: subpat.map(|p| Box::new(p.1)),
1783 })
1784 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001785 }
1786
1787 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001788 named!(parse -> Self, do_parse!(
1789 path: syn!(Path) >>
1790 tuple: syn!(PatTuple) >>
1791 (PatTupleStruct {
1792 path: path,
1793 pat: tuple,
1794 })
1795 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001796 }
1797
1798 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001799 named!(parse -> Self, do_parse!(
1800 path: syn!(Path) >>
1801 data: braces!(do_parse!(
1802 fields: call!(Delimited::parse_terminated) >>
1803 base: option!(
1804 cond!(fields.is_empty() || fields.trailing_delim(),
1805 syn!(Dot2))
1806 ) >>
1807 (fields, base)
1808 )) >>
1809 (PatStruct {
1810 path: path,
1811 fields: (data.0).0,
1812 brace_token: data.1,
1813 dot2_token: (data.0).1.and_then(|m| m),
1814 })
1815 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001816 }
1817
1818 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04001819 named!(parse -> Self, alt!(
1820 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -07001821 ident: field_ident >>
Michael Layzell92639a52017-06-01 00:07:44 -04001822 colon: syn!(Colon) >>
1823 pat: syn!(Pat) >>
1824 (FieldPat {
1825 ident: ident,
1826 pat: Box::new(pat),
1827 is_shorthand: false,
1828 attrs: Vec::new(),
1829 colon_token: Some(colon),
1830 })
1831 )
1832 |
1833 do_parse!(
1834 boxed: option!(syn!(Box_)) >>
1835 mode: option!(syn!(Ref)) >>
1836 mutability: syn!(Mutability) >>
1837 ident: syn!(Ident) >>
1838 ({
1839 let mut pat: Pat = PatIdent {
1840 mode: if let Some(mode) = mode {
1841 BindingMode::ByRef(mode, mutability)
1842 } else {
1843 BindingMode::ByValue(mutability)
1844 },
1845 ident: ident.clone(),
1846 subpat: None,
1847 at_token: None,
1848 }.into();
1849 if let Some(boxed) = boxed {
1850 pat = PatBox {
1851 pat: Box::new(pat),
1852 box_token: boxed,
1853 }.into();
1854 }
1855 FieldPat {
Alex Crichton954046c2017-05-30 21:49:42 -07001856 ident: ident,
1857 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04001858 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07001859 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001860 colon_token: None,
1861 }
1862 })
1863 )
1864 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001865 }
1866
David Tolnay570695e2017-06-03 16:15:13 -07001867 named!(field_ident -> Ident, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07001868 syn!(Ident)
1869 |
1870 do_parse!(
1871 lit: syn!(Lit) >>
1872 ({
David Tolnay570695e2017-06-03 16:15:13 -07001873 let s = lit.to_string();
1874 if s.parse::<usize>().is_ok() {
Alex Crichton954046c2017-05-30 21:49:42 -07001875 Ident::new(s.into(), lit.span)
1876 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04001877 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07001878 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001879 })
1880 )
1881 ));
1882
Alex Crichton954046c2017-05-30 21:49:42 -07001883 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001884 named!(parse -> Self, map!(
1885 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07001886 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04001887 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001888 }
David Tolnay9636c052016-10-02 17:11:17 -07001889
Alex Crichton954046c2017-05-30 21:49:42 -07001890 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001891 named!(parse -> Self, do_parse!(
1892 data: parens!(do_parse!(
1893 elems: call!(Delimited::parse_terminated) >>
1894 dotdot: map!(cond!(
1895 elems.is_empty() || elems.trailing_delim(),
1896 option!(do_parse!(
1897 dots: syn!(Dot2) >>
1898 trailing: option!(syn!(Comma)) >>
1899 (dots, trailing)
1900 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07001901 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001902 rest: cond!(match dotdot {
1903 Some((_, Some(_))) => true,
1904 _ => false,
1905 },
1906 call!(Delimited::parse_terminated)) >>
1907 (elems, dotdot, rest)
1908 )) >>
1909 ({
1910 let ((mut elems, dotdot, rest), parens) = data;
1911 let (dotdot, trailing) = match dotdot {
1912 Some((a, b)) => (Some(a), Some(b)),
1913 None => (None, None),
1914 };
1915 PatTuple {
1916 paren_token: parens,
1917 dots_pos: dotdot.as_ref().map(|_| elems.len()),
1918 dot2_token: dotdot,
1919 comma_token: trailing.and_then(|b| b),
1920 pats: {
1921 if let Some(rest) = rest {
1922 for elem in rest {
1923 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07001924 }
Michael Layzell92639a52017-06-01 00:07:44 -04001925 }
1926 elems
1927 },
1928 }
1929 })
1930 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001931 }
David Tolnayfbb73232016-10-03 01:00:06 -07001932
Alex Crichton954046c2017-05-30 21:49:42 -07001933 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04001934 named!(parse -> Self, do_parse!(
1935 and: syn!(And) >>
1936 mutability: syn!(Mutability) >>
1937 pat: syn!(Pat) >>
1938 (PatRef {
1939 pat: Box::new(pat),
1940 mutbl: mutability,
1941 and_token: and,
1942 })
1943 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001944 }
David Tolnayffdb97f2016-10-03 01:28:33 -07001945
Alex Crichton954046c2017-05-30 21:49:42 -07001946 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04001947 named!(parse -> Self, do_parse!(
1948 lit: pat_lit_expr >>
1949 (if let ExprKind::Path(_) = lit.node {
1950 return parse_error(); // these need to be parsed by pat_path
1951 } else {
1952 PatLit {
1953 expr: Box::new(lit),
1954 }
1955 })
1956 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001957 }
David Tolnaye1310902016-10-29 23:40:00 -07001958
Alex Crichton954046c2017-05-30 21:49:42 -07001959 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04001960 named!(parse -> Self, do_parse!(
1961 lo: pat_lit_expr >>
1962 limits: syn!(RangeLimits) >>
1963 hi: pat_lit_expr >>
1964 (PatRange {
1965 lo: Box::new(lo),
1966 hi: Box::new(hi),
1967 limits: limits,
1968 })
1969 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001970 }
David Tolnaye1310902016-10-29 23:40:00 -07001971
David Tolnay2cfddc62016-10-30 01:03:27 -07001972 named!(pat_lit_expr -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001973 neg: option!(syn!(Sub)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07001974 v: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07001975 syn!(Lit) => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07001976 |
Alex Crichton954046c2017-05-30 21:49:42 -07001977 syn!(ExprPath) => { ExprKind::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07001978 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001979 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07001980 ExprKind::Unary(ExprUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001981 op: UnOp::Neg(tokens::Sub::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07001982 expr: Box::new(v.into())
1983 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001984 } else {
David Tolnay7184b132016-10-30 10:06:37 -07001985 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07001986 })
1987 ));
David Tolnay8b308c22016-10-03 01:24:10 -07001988
Alex Crichton954046c2017-05-30 21:49:42 -07001989 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04001990 named!(parse -> Self, map!(
1991 brackets!(do_parse!(
1992 before: call!(Delimited::parse_terminated) >>
1993 middle: option!(do_parse!(
1994 dots: syn!(Dot2) >>
1995 trailing: option!(syn!(Comma)) >>
1996 (dots, trailing)
1997 )) >>
1998 after: cond!(
1999 match middle {
2000 Some((_, ref trailing)) => trailing.is_some(),
2001 _ => false,
2002 },
2003 call!(Delimited::parse_terminated)
2004 ) >>
2005 (before, middle, after)
2006 )),
2007 |((before, middle, after), brackets)| {
2008 let mut before: Delimited<Pat, tokens::Comma> = before;
2009 let after: Option<Delimited<Pat, tokens::Comma>> = after;
2010 let middle: Option<(Dot2, Option<Comma>)> = middle;
2011 PatSlice {
2012 dot2_token: middle.as_ref().map(|m| Dot2((m.0).0)),
2013 comma_token: middle.as_ref().and_then(|m| {
2014 m.1.as_ref().map(|m| Comma(m.0))
2015 }),
2016 bracket_token: brackets,
2017 middle: middle.and_then(|_| {
2018 if !before.is_empty() && !before.trailing_delim() {
2019 Some(Box::new(before.pop().unwrap().into_item()))
2020 } else {
2021 None
2022 }
2023 }),
2024 front: before,
2025 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002026 }
Alex Crichton954046c2017-05-30 21:49:42 -07002027 }
Michael Layzell92639a52017-06-01 00:07:44 -04002028 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002029 }
David Tolnay435a9a82016-10-29 13:47:20 -07002030
Alex Crichton954046c2017-05-30 21:49:42 -07002031 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002032 named!(parse -> Self, alt!(
2033 syn!(Move) => { CaptureBy::Value }
2034 |
2035 epsilon!() => { |_| CaptureBy::Ref }
2036 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002037 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002038}
2039
David Tolnayf4bbbd92016-09-23 14:41:55 -07002040#[cfg(feature = "printing")]
2041mod printing {
2042 use super::*;
David Tolnay13b3d352016-10-03 00:31:15 -07002043 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07002044 use quote::{Tokens, ToTokens};
2045
2046 impl ToTokens for Expr {
2047 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07002048 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002049 self.node.to_tokens(tokens)
2050 }
2051 }
2052
2053 impl ToTokens for ExprBox {
2054 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002055 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002056 self.expr.to_tokens(tokens);
2057 }
2058 }
2059
2060 impl ToTokens for ExprInPlace {
2061 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002062 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002063 self.place.to_tokens(tokens);
2064 self.value.to_tokens(tokens);
2065 }
2066 }
2067
2068 impl ToTokens for ExprArray {
2069 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002070 self.bracket_token.surround(tokens, |tokens| {
2071 self.exprs.to_tokens(tokens);
2072 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002073 }
2074 }
2075
2076 impl ToTokens for ExprCall {
2077 fn to_tokens(&self, tokens: &mut Tokens) {
2078 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002079 self.paren_token.surround(tokens, |tokens| {
2080 self.args.to_tokens(tokens);
2081 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002082 }
2083 }
2084
2085 impl ToTokens for ExprMethodCall {
2086 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002087 self.expr.to_tokens(tokens);
2088 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002089 self.method.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002090 self.colon2_token.to_tokens(tokens);
2091 self.lt_token.to_tokens(tokens);
2092 self.typarams.to_tokens(tokens);
2093 self.gt_token.to_tokens(tokens);
2094 self.paren_token.surround(tokens, |tokens| {
2095 self.args.to_tokens(tokens);
2096 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002097 }
2098 }
2099
2100 impl ToTokens for ExprTup {
2101 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002102 self.paren_token.surround(tokens, |tokens| {
2103 self.args.to_tokens(tokens);
2104 self.lone_comma.to_tokens(tokens);
2105 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002106 }
2107 }
2108
2109 impl ToTokens for ExprBinary {
2110 fn to_tokens(&self, tokens: &mut Tokens) {
2111 self.left.to_tokens(tokens);
2112 self.op.to_tokens(tokens);
2113 self.right.to_tokens(tokens);
2114 }
2115 }
2116
2117 impl ToTokens for ExprUnary {
2118 fn to_tokens(&self, tokens: &mut Tokens) {
2119 self.op.to_tokens(tokens);
2120 self.expr.to_tokens(tokens);
2121 }
2122 }
2123
2124 impl ToTokens for ExprCast {
2125 fn to_tokens(&self, tokens: &mut Tokens) {
2126 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002127 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002128 self.ty.to_tokens(tokens);
2129 }
2130 }
2131
2132 impl ToTokens for ExprType {
2133 fn to_tokens(&self, tokens: &mut Tokens) {
2134 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002135 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002136 self.ty.to_tokens(tokens);
2137 }
2138 }
2139
2140 impl ToTokens for ExprIf {
2141 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002142 self.if_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002143 self.cond.to_tokens(tokens);
2144 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002145 self.else_token.to_tokens(tokens);
2146 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002147 }
2148 }
2149
2150 impl ToTokens for ExprIfLet {
2151 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002152 self.if_token.to_tokens(tokens);
2153 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002154 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002155 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002156 self.expr.to_tokens(tokens);
2157 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002158 self.else_token.to_tokens(tokens);
2159 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002160 }
2161 }
2162
2163 impl ToTokens for ExprWhile {
2164 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002165 self.label.to_tokens(tokens);
2166 self.colon_token.to_tokens(tokens);
2167 self.while_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002168 self.cond.to_tokens(tokens);
2169 self.body.to_tokens(tokens);
2170 }
2171 }
2172
2173 impl ToTokens for ExprWhileLet {
2174 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002175 self.label.to_tokens(tokens);
2176 self.colon_token.to_tokens(tokens);
2177 self.while_token.to_tokens(tokens);
2178 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002179 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002180 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002181 self.expr.to_tokens(tokens);
2182 self.body.to_tokens(tokens);
2183 }
2184 }
2185
2186 impl ToTokens for ExprForLoop {
2187 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002188 self.label.to_tokens(tokens);
2189 self.colon_token.to_tokens(tokens);
2190 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002191 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002192 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002193 self.expr.to_tokens(tokens);
2194 self.body.to_tokens(tokens);
2195 }
2196 }
2197
2198 impl ToTokens for ExprLoop {
2199 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002200 self.label.to_tokens(tokens);
2201 self.colon_token.to_tokens(tokens);
2202 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002203 self.body.to_tokens(tokens);
2204 }
2205 }
2206
2207 impl ToTokens for ExprMatch {
2208 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002209 self.match_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002210 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002211 self.brace_token.surround(tokens, |tokens| {
2212 tokens.append_all(&self.arms);
2213 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002214 }
2215 }
2216
2217 impl ToTokens for ExprCatch {
2218 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002219 self.do_token.to_tokens(tokens);
2220 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002221 self.block.to_tokens(tokens);
2222 }
2223 }
2224
2225 impl ToTokens for ExprClosure {
2226 fn to_tokens(&self, tokens: &mut Tokens) {
2227 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002228 self.or1_token.to_tokens(tokens);
2229 for item in self.decl.inputs.iter() {
2230 match **item.item() {
2231 FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002232 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002233 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002234 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002235 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002236 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002237 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002238 self.or2_token.to_tokens(tokens);
2239 self.decl.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002240 self.body.to_tokens(tokens);
2241 }
2242 }
2243
2244 impl ToTokens for ExprBlock {
2245 fn to_tokens(&self, tokens: &mut Tokens) {
2246 self.unsafety.to_tokens(tokens);
2247 self.block.to_tokens(tokens);
2248 }
2249 }
2250
2251 impl ToTokens for ExprAssign {
2252 fn to_tokens(&self, tokens: &mut Tokens) {
2253 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002254 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002255 self.right.to_tokens(tokens);
2256 }
2257 }
2258
2259 impl ToTokens for ExprAssignOp {
2260 fn to_tokens(&self, tokens: &mut Tokens) {
2261 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002262 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002263 self.right.to_tokens(tokens);
2264 }
2265 }
2266
2267 impl ToTokens for ExprField {
2268 fn to_tokens(&self, tokens: &mut Tokens) {
2269 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002270 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002271 self.field.to_tokens(tokens);
2272 }
2273 }
2274
2275 impl ToTokens for ExprTupField {
2276 fn to_tokens(&self, tokens: &mut Tokens) {
2277 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002278 self.dot_token.to_tokens(tokens);
2279 self.field.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002280 }
2281 }
2282
2283 impl ToTokens for ExprIndex {
2284 fn to_tokens(&self, tokens: &mut Tokens) {
2285 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002286 self.bracket_token.surround(tokens, |tokens| {
2287 self.index.to_tokens(tokens);
2288 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002289 }
2290 }
2291
2292 impl ToTokens for ExprRange {
2293 fn to_tokens(&self, tokens: &mut Tokens) {
2294 self.from.to_tokens(tokens);
2295 self.limits.to_tokens(tokens);
2296 self.to.to_tokens(tokens);
2297 }
2298 }
2299
2300 impl ToTokens for ExprPath {
2301 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002302 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002303 }
2304 }
2305
2306 impl ToTokens for ExprAddrOf {
2307 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002308 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002309 self.mutbl.to_tokens(tokens);
2310 self.expr.to_tokens(tokens);
2311 }
2312 }
2313
2314 impl ToTokens for ExprBreak {
2315 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002316 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002317 self.label.to_tokens(tokens);
2318 self.expr.to_tokens(tokens);
2319 }
2320 }
2321
2322 impl ToTokens for ExprContinue {
2323 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002324 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002325 self.label.to_tokens(tokens);
2326 }
2327 }
2328
2329 impl ToTokens for ExprRet {
2330 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002331 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002332 self.expr.to_tokens(tokens);
2333 }
2334 }
2335
2336 impl ToTokens for ExprStruct {
2337 fn to_tokens(&self, tokens: &mut Tokens) {
2338 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002339 self.brace_token.surround(tokens, |tokens| {
2340 self.fields.to_tokens(tokens);
2341 self.dot2_token.to_tokens(tokens);
2342 self.rest.to_tokens(tokens);
2343 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002344 }
2345 }
2346
2347 impl ToTokens for ExprRepeat {
2348 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002349 self.bracket_token.surround(tokens, |tokens| {
2350 self.expr.to_tokens(tokens);
2351 self.semi_token.to_tokens(tokens);
2352 self.amt.to_tokens(tokens);
2353 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002354 }
2355 }
2356
2357 impl ToTokens for ExprParen {
2358 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002359 self.paren_token.surround(tokens, |tokens| {
2360 self.expr.to_tokens(tokens);
2361 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002362 }
2363 }
2364
2365 impl ToTokens for ExprTry {
2366 fn to_tokens(&self, tokens: &mut Tokens) {
2367 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002368 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002369 }
2370 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002371
David Tolnay055a7042016-10-02 19:23:54 -07002372 impl ToTokens for FieldValue {
2373 fn to_tokens(&self, tokens: &mut Tokens) {
2374 self.ident.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002375 if !self.is_shorthand {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002376 self.colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002377 self.expr.to_tokens(tokens);
2378 }
David Tolnay055a7042016-10-02 19:23:54 -07002379 }
2380 }
2381
David Tolnayb4ad3b52016-10-01 21:58:13 -07002382 impl ToTokens for Arm {
2383 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002384 tokens.append_all(&self.attrs);
2385 self.pats.to_tokens(tokens);
2386 self.if_token.to_tokens(tokens);
2387 self.guard.to_tokens(tokens);
2388 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002389 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002390 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002391 }
2392 }
2393
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002394 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002395 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002396 self.underscore_token.to_tokens(tokens);
2397 }
2398 }
2399
2400 impl ToTokens for PatIdent {
2401 fn to_tokens(&self, tokens: &mut Tokens) {
2402 self.mode.to_tokens(tokens);
2403 self.ident.to_tokens(tokens);
2404 self.at_token.to_tokens(tokens);
2405 self.subpat.to_tokens(tokens);
2406 }
2407 }
2408
2409 impl ToTokens for PatStruct {
2410 fn to_tokens(&self, tokens: &mut Tokens) {
2411 self.path.to_tokens(tokens);
2412 self.brace_token.surround(tokens, |tokens| {
2413 self.fields.to_tokens(tokens);
2414 self.dot2_token.to_tokens(tokens);
2415 });
2416 }
2417 }
2418
2419 impl ToTokens for PatTupleStruct {
2420 fn to_tokens(&self, tokens: &mut Tokens) {
2421 self.path.to_tokens(tokens);
2422 self.pat.to_tokens(tokens);
2423 }
2424 }
2425
2426 impl ToTokens for PatPath {
2427 fn to_tokens(&self, tokens: &mut Tokens) {
2428 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2429 }
2430 }
2431
2432 impl ToTokens for PatTuple {
2433 fn to_tokens(&self, tokens: &mut Tokens) {
2434 self.paren_token.surround(tokens, |tokens| {
2435 for (i, token) in self.pats.iter().enumerate() {
2436 if Some(i) == self.dots_pos {
2437 self.dot2_token.to_tokens(tokens);
2438 self.comma_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002439 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002440 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002441 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002442
2443 if Some(self.pats.len()) == self.dots_pos {
2444 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002445 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002446 });
2447 }
2448 }
2449
2450 impl ToTokens for PatBox {
2451 fn to_tokens(&self, tokens: &mut Tokens) {
2452 self.box_token.to_tokens(tokens);
2453 self.pat.to_tokens(tokens);
2454 }
2455 }
2456
2457 impl ToTokens for PatRef {
2458 fn to_tokens(&self, tokens: &mut Tokens) {
2459 self.and_token.to_tokens(tokens);
2460 self.mutbl.to_tokens(tokens);
2461 self.pat.to_tokens(tokens);
2462 }
2463 }
2464
2465 impl ToTokens for PatLit {
2466 fn to_tokens(&self, tokens: &mut Tokens) {
2467 self.expr.to_tokens(tokens);
2468 }
2469 }
2470
2471 impl ToTokens for PatRange {
2472 fn to_tokens(&self, tokens: &mut Tokens) {
2473 self.lo.to_tokens(tokens);
2474 self.limits.to_tokens(tokens);
2475 self.hi.to_tokens(tokens);
2476 }
2477 }
2478
2479 impl ToTokens for PatSlice {
2480 fn to_tokens(&self, tokens: &mut Tokens) {
2481 self.bracket_token.surround(tokens, |tokens| {
2482 self.front.to_tokens(tokens);
2483 self.middle.to_tokens(tokens);
2484 self.dot2_token.to_tokens(tokens);
2485 self.comma_token.to_tokens(tokens);
2486 self.back.to_tokens(tokens);
2487 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002488 }
2489 }
2490
Arnavion1992e2f2017-04-25 01:47:46 -07002491 impl ToTokens for RangeLimits {
2492 fn to_tokens(&self, tokens: &mut Tokens) {
2493 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002494 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2495 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
Arnavion1992e2f2017-04-25 01:47:46 -07002496 }
2497 }
2498 }
2499
David Tolnay8d9e81a2016-10-03 22:36:32 -07002500 impl ToTokens for FieldPat {
2501 fn to_tokens(&self, tokens: &mut Tokens) {
2502 if !self.is_shorthand {
2503 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002504 self.colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002505 }
2506 self.pat.to_tokens(tokens);
2507 }
2508 }
2509
David Tolnayb4ad3b52016-10-01 21:58:13 -07002510 impl ToTokens for BindingMode {
2511 fn to_tokens(&self, tokens: &mut Tokens) {
2512 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002513 BindingMode::ByRef(ref t, ref m) => {
2514 t.to_tokens(tokens);
2515 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002516 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002517 BindingMode::ByValue(ref m) => {
2518 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002519 }
2520 }
2521 }
2522 }
David Tolnay42602292016-10-01 22:25:45 -07002523
David Tolnay89e05672016-10-02 14:39:42 -07002524 impl ToTokens for CaptureBy {
2525 fn to_tokens(&self, tokens: &mut Tokens) {
2526 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002527 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002528 CaptureBy::Ref => {
2529 // nothing
2530 }
David Tolnay89e05672016-10-02 14:39:42 -07002531 }
2532 }
2533 }
2534
David Tolnay42602292016-10-01 22:25:45 -07002535 impl ToTokens for Block {
2536 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002537 self.brace_token.surround(tokens, |tokens| {
2538 tokens.append_all(&self.stmts);
2539 });
David Tolnay42602292016-10-01 22:25:45 -07002540 }
2541 }
2542
David Tolnay42602292016-10-01 22:25:45 -07002543 impl ToTokens for Stmt {
2544 fn to_tokens(&self, tokens: &mut Tokens) {
2545 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002546 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002547 Stmt::Item(ref item) => item.to_tokens(tokens),
2548 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002549 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002550 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002551 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002552 }
David Tolnay13b3d352016-10-03 00:31:15 -07002553 Stmt::Mac(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002554 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002555 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002556 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07002557 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002558 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002559 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2560 // no semicolon
2561 }
David Tolnay13b3d352016-10-03 00:31:15 -07002562 }
2563 }
David Tolnay42602292016-10-01 22:25:45 -07002564 }
2565 }
2566 }
David Tolnay191e0582016-10-02 18:31:09 -07002567
2568 impl ToTokens for Local {
2569 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002570 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002571 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002572 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002573 self.colon_token.to_tokens(tokens);
2574 self.ty.to_tokens(tokens);
2575 self.eq_token.to_tokens(tokens);
2576 self.init.to_tokens(tokens);
2577 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002578 }
2579 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002580}