blob: 2b73998a19f40a683f75fe52c1b5ad046e05dfab [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>,
Michael Layzell6a5a1642017-06-04 19:35:15 -040035 pub kind: InPlaceKind,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub value: Box<Expr>,
37 }),
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
Michael Layzell93c36282017-06-04 20:43:14 -0400325 /// No-op: used solely so we can pretty-print faithfully
326 ///
327 /// A `group` represents a `None`-delimited span in the input
328 /// `TokenStream` which affects the precidence of the resulting
329 /// expression. They are used for macro hygiene.
330 pub Group(ExprGroup {
331 pub expr: Box<Expr>,
332 pub group_token: tokens::Group,
333 }),
334
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 /// `expr?`
336 pub Try(ExprTry {
337 pub expr: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700338 pub question_token: tokens::Question,
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700340
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 /// A catch expression.
342 ///
343 /// E.g. `do catch { block }`
344 pub Catch(ExprCatch {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700345 pub do_token: tokens::Do,
346 pub catch_token: tokens::Catch,
Alex Crichton62a0a592017-05-22 13:58:53 -0700347 pub block: Block,
348 }),
349 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700350}
351
Alex Crichton62a0a592017-05-22 13:58:53 -0700352ast_struct! {
353 /// A field-value pair in a struct literal.
354 pub struct FieldValue {
355 /// Name of the field.
356 pub ident: Ident,
Clar Charrd22b5702017-03-10 15:24:56 -0500357
Alex Crichton62a0a592017-05-22 13:58:53 -0700358 /// Value of the field.
359 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500360
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 /// Whether this is a shorthand field, e.g. `Struct { x }`
362 /// instead of `Struct { x: x }`.
363 pub is_shorthand: bool,
Clar Charrd22b5702017-03-10 15:24:56 -0500364
Alex Crichton62a0a592017-05-22 13:58:53 -0700365 /// Attributes tagged on the field.
366 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700367
368 pub colon_token: Option<tokens::Colon>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700369 }
David Tolnay055a7042016-10-02 19:23:54 -0700370}
371
Alex Crichton62a0a592017-05-22 13:58:53 -0700372ast_struct! {
373 /// A Block (`{ .. }`).
374 ///
375 /// E.g. `{ .. }` as in `fn foo() { .. }`
376 pub struct Block {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700377 pub brace_token: tokens::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700378 /// Statements in a block
379 pub stmts: Vec<Stmt>,
380 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700381}
382
Alex Crichton62a0a592017-05-22 13:58:53 -0700383ast_enum! {
384 /// A statement, usually ending in a semicolon.
385 pub enum Stmt {
386 /// A local (let) binding.
387 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700388
Alex Crichton62a0a592017-05-22 13:58:53 -0700389 /// An item definition.
390 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700391
Alex Crichton62a0a592017-05-22 13:58:53 -0700392 /// Expr without trailing semicolon.
393 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700394
Alex Crichton62a0a592017-05-22 13:58:53 -0700395 /// Expression with trailing semicolon;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700396 Semi(Box<Expr>, tokens::Semi),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700397
Alex Crichton62a0a592017-05-22 13:58:53 -0700398 /// Macro invocation.
399 Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>),
400 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700401}
402
Alex Crichton62a0a592017-05-22 13:58:53 -0700403ast_enum! {
404 /// How a macro was invoked.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700405 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700406 pub enum MacStmtStyle {
407 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
408 /// `foo!(...);`, `foo![...];`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700409 Semicolon(tokens::Semi),
Clar Charrd22b5702017-03-10 15:24:56 -0500410
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 /// The macro statement had braces; e.g. foo! { ... }
412 Braces,
Clar Charrd22b5702017-03-10 15:24:56 -0500413
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 /// The macro statement had parentheses or brackets and no semicolon; e.g.
415 /// `foo!(...)`. All of these will end up being converted into macro
416 /// expressions.
417 NoBraces,
418 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700419}
420
Alex Crichton62a0a592017-05-22 13:58:53 -0700421ast_struct! {
422 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
423 pub struct Local {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700424 pub let_token: tokens::Let,
425 pub colon_token: Option<tokens::Colon>,
426 pub eq_token: Option<tokens::Eq>,
427 pub semi_token: tokens::Semi,
428
Alex Crichton62a0a592017-05-22 13:58:53 -0700429 pub pat: Box<Pat>,
430 pub ty: Option<Box<Ty>>,
Clar Charrd22b5702017-03-10 15:24:56 -0500431
Alex Crichton62a0a592017-05-22 13:58:53 -0700432 /// Initializer expression to set the value, if any
433 pub init: Option<Box<Expr>>,
434 pub attrs: Vec<Attribute>,
435 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700436}
437
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700438ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 // Clippy false positive
440 // https://github.com/Manishearth/rust-clippy/issues/1241
441 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
442 pub enum Pat {
443 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700444 pub Wild(PatWild {
445 pub underscore_token: tokens::Underscore,
446 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700447
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
449 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
450 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
451 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700452 pub Ident(PatIdent {
453 pub mode: BindingMode,
454 pub ident: Ident,
455 pub subpat: Option<Box<Pat>>,
456 pub at_token: Option<tokens::At>,
457 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700458
Alex Crichton62a0a592017-05-22 13:58:53 -0700459 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
460 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700461 pub Struct(PatStruct {
462 pub path: Path,
463 pub fields: Delimited<FieldPat, tokens::Comma>,
464 pub brace_token: tokens::Brace,
465 pub dot2_token: Option<tokens::Dot2>,
466 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700467
Alex Crichton62a0a592017-05-22 13:58:53 -0700468 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
469 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
470 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700471 pub TupleStruct(PatTupleStruct {
472 pub path: Path,
473 pub pat: PatTuple,
474 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700475
Alex Crichton62a0a592017-05-22 13:58:53 -0700476 /// A possibly qualified path pattern.
477 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
478 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
479 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700480 pub Path(PatPath {
481 pub qself: Option<QSelf>,
482 pub path: Path,
483 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700484
Alex Crichton62a0a592017-05-22 13:58:53 -0700485 /// A tuple pattern `(a, b)`.
486 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
487 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700488 pub Tuple(PatTuple {
489 pub pats: Delimited<Pat, tokens::Comma>,
490 pub dots_pos: Option<usize>,
491 pub paren_token: tokens::Paren,
492 pub dot2_token: Option<tokens::Dot2>,
493 pub comma_token: Option<tokens::Comma>,
494 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700495 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700496 pub Box(PatBox {
497 pub pat: Box<Pat>,
Alex Crichton954046c2017-05-30 21:49:42 -0700498 pub box_token: tokens::Box_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700499 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700500 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700501 pub Ref(PatRef {
502 pub pat: Box<Pat>,
503 pub mutbl: Mutability,
504 pub and_token: tokens::And,
505 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700506 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700507 pub Lit(PatLit {
508 pub expr: Box<Expr>,
509 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700510 /// A range pattern, e.g. `1...2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700511 pub Range(PatRange {
512 pub lo: Box<Expr>,
513 pub hi: Box<Expr>,
514 pub limits: RangeLimits,
515 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700516 /// `[a, b, ..i, y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700517 pub Slice(PatSlice {
518 pub front: Delimited<Pat, tokens::Comma>,
519 pub middle: Option<Box<Pat>>,
520 pub back: Delimited<Pat, tokens::Comma>,
521 pub dot2_token: Option<tokens::Dot2>,
522 pub comma_token: Option<tokens::Comma>,
523 pub bracket_token: tokens::Bracket,
524 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 /// A macro pattern; pre-expansion
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700526 pub Mac(Mac),
Alex Crichton62a0a592017-05-22 13:58:53 -0700527 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700528}
529
Alex Crichton62a0a592017-05-22 13:58:53 -0700530ast_struct! {
531 /// An arm of a 'match'.
532 ///
533 /// E.g. `0...10 => { println!("match!") }` as in
534 ///
535 /// ```rust,ignore
536 /// match n {
537 /// 0...10 => { println!("match!") },
538 /// // ..
539 /// }
540 /// ```
541 pub struct Arm {
542 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700543 pub pats: Delimited<Pat, tokens::Or>,
544 pub if_token: Option<tokens::If>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700545 pub guard: Option<Box<Expr>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700546 pub rocket_token: tokens::Rocket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 pub body: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700548 pub comma: Option<tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700549 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700550}
551
Alex Crichton62a0a592017-05-22 13:58:53 -0700552ast_enum! {
553 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700554 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700555 pub enum CaptureBy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 Value(tokens::Move),
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 Ref,
558 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700559}
560
Alex Crichton62a0a592017-05-22 13:58:53 -0700561ast_enum! {
562 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700563 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700564 pub enum RangeLimits {
565 /// Inclusive at the beginning, exclusive at the end
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700566 HalfOpen(tokens::Dot2),
Alex Crichton62a0a592017-05-22 13:58:53 -0700567 /// Inclusive at the beginning and end
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700568 Closed(tokens::Dot3),
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700570}
571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572ast_struct! {
573 /// A single field in a struct pattern
574 ///
575 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
576 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
577 /// except `is_shorthand` is true
578 pub struct FieldPat {
579 /// The identifier for the field
580 pub ident: Ident,
581 /// The pattern the field is destructured to
582 pub pat: Box<Pat>,
583 pub is_shorthand: bool,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700584 pub colon_token: Option<tokens::Colon>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700585 pub attrs: Vec<Attribute>,
586 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700587}
588
Alex Crichton62a0a592017-05-22 13:58:53 -0700589ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700590 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700591 pub enum BindingMode {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700592 ByRef(tokens::Ref, Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700593 ByValue(Mutability),
594 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700595}
596
Michael Layzell6a5a1642017-06-04 19:35:15 -0400597ast_enum! {
598 #[cfg_attr(feature = "clone-impls", derive(Copy))]
599 pub enum InPlaceKind {
600 Arrow(tokens::LArrow),
601 In(tokens::In),
602 }
603}
604
David Tolnayb9c8e322016-09-23 20:48:37 -0700605#[cfg(feature = "parsing")]
606pub mod parsing {
607 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700608 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700609
Michael Layzell92639a52017-06-01 00:07:44 -0400610 use proc_macro2::{TokenStream, TokenKind, Delimiter};
611 use synom::{PResult, Cursor, Synom, parse_error};
Alex Crichton954046c2017-05-30 21:49:42 -0700612 use synom::tokens::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700613
Michael Layzellb78f3b52017-06-04 19:03:03 -0400614 /// When we're parsing expressions which occur before blocks, like in
615 /// an if statement's condition, we cannot parse a struct literal.
616 ///
617 /// Struct literals are ambiguous in certain positions
618 /// https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700619 macro_rules! ambiguous_expr {
620 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700621 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700622 };
623 }
624
Michael Layzellb78f3b52017-06-04 19:03:03 -0400625 /// When we are parsing an optional suffix expression, we cannot allow
626 /// blocks if structs are not allowed.
627 ///
628 /// Example:
629 /// ```ignore
630 /// if break { } { }
631 /// // is ambiguous between:
632 /// if (break { }) { }
633 /// // - or -
634 /// if (break) { } { }
635 /// ```
636 macro_rules! opt_ambiguous_expr {
637 ($i:expr, $allow_struct:ident) => {
638 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
639 };
640 }
641
Alex Crichton954046c2017-05-30 21:49:42 -0700642 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400643 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700644
645 fn description() -> Option<&'static str> {
646 Some("expression")
647 }
648 }
649
David Tolnayaf2557e2016-10-24 11:52:21 -0700650
651 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
652
Michael Layzellb78f3b52017-06-04 19:03:03 -0400653 /// Parse an arbitrary expression.
654 pub fn ambiguous_expr(i: Cursor,
655 allow_struct: bool,
656 allow_block: bool)
657 -> PResult<Expr> {
658 map!(
David Tolnay54e854d2016-10-24 12:03:30 -0700659 i,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400660 call!(assign_expr, allow_struct, allow_block),
661 ExprKind::into
662 )
663 }
664
665 /// Parse a left-associative binary operator.
666 macro_rules! binop {
667 (
668 $name: ident,
669 $next: ident,
670 $submac: ident!( $($args:tt)* )
671 ) => {
672 named!($name(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
673 mut e: call!($next, allow_struct, allow_block) >>
674 many0!(do_parse!(
675 op: $submac!($($args)*) >>
676 rhs: call!($next, allow_struct, true) >>
677 ({
678 e = ExprBinary {
679 left: Box::new(e.into()),
680 op: op,
681 right: Box::new(rhs.into()),
682 }.into();
683 })
684 )) >>
685 (e)
686 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700687 }
David Tolnay54e854d2016-10-24 12:03:30 -0700688 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700689
Michael Layzellb78f3b52017-06-04 19:03:03 -0400690 /// ```ignore
691 /// <placement> = <placement> ..
692 /// <placement> += <placement> ..
693 /// <placement> -= <placement> ..
694 /// <placement> *= <placement> ..
695 /// <placement> /= <placement> ..
696 /// <placement> %= <placement> ..
697 /// <placement> ^= <placement> ..
698 /// <placement> &= <placement> ..
699 /// <placement> |= <placement> ..
700 /// <placement> <<= <placement> ..
701 /// <placement> >>= <placement> ..
702 /// ```
703 ///
704 /// NOTE: This operator is right-associative.
705 named!(assign_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
706 mut e: call!(placement_expr, allow_struct, allow_block) >>
707 alt!(
708 do_parse!(
709 eq: syn!(Eq) >>
710 // Recurse into self to parse right-associative operator.
711 rhs: call!(assign_expr, allow_struct, true) >>
712 ({
713 e = ExprAssign {
714 left: Box::new(e.into()),
715 eq_token: eq,
716 right: Box::new(rhs.into()),
717 }.into();
718 })
719 )
720 |
721 do_parse!(
722 op: call!(BinOp::parse_assign_op) >>
723 // Recurse into self to parse right-associative operator.
724 rhs: call!(assign_expr, allow_struct, true) >>
725 ({
726 e = ExprAssignOp {
727 left: Box::new(e.into()),
728 op: op,
729 right: Box::new(rhs.into()),
730 }.into();
731 })
732 )
733 |
734 epsilon!()
735 ) >>
736 (e)
737 ));
738
739 /// ```ignore
740 /// <range> <- <range> ..
741 /// ```
742 ///
743 /// NOTE: The `in place { expr }` version of this syntax is parsed in
744 /// `atom_expr`, not here.
745 ///
746 /// NOTE: This operator is right-associative.
747 named!(placement_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
748 mut e: call!(range_expr, allow_struct, allow_block) >>
749 alt!(
750 do_parse!(
Michael Layzell6a5a1642017-06-04 19:35:15 -0400751 arrow: syn!(LArrow) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400752 // Recurse into self to parse right-associative operator.
753 rhs: call!(placement_expr, allow_struct, true) >>
754 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400755 e = ExprInPlace {
756 // op: BinOp::Place(larrow),
757 place: Box::new(e.into()),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400758 kind: InPlaceKind::Arrow(arrow),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400759 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400760 }.into();
761 })
762 )
763 |
764 epsilon!()
765 ) >>
766 (e)
767 ));
768
769 /// ```ignore
770 /// <or> ... <or> ..
771 /// <or> .. <or> ..
772 /// <or> ..
773 /// ```
774 ///
775 /// NOTE: This is currently parsed oddly - I'm not sure of what the exact
776 /// rules are for parsing these expressions are, but this is not correct.
777 /// For example, `a .. b .. c` is not a legal expression. It should not
778 /// be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
779 ///
780 /// NOTE: The form of ranges which don't include a preceding expression are
781 /// parsed by `atom_expr`, rather than by this function.
782 named!(range_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
783 mut e: call!(or_expr, allow_struct, allow_block) >>
784 many0!(do_parse!(
785 limits: syn!(RangeLimits) >>
786 // We don't want to allow blocks here if we don't allow structs. See
787 // the reasoning for `opt_ambiguous_expr!` above.
788 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
789 ({
790 e = ExprRange {
791 from: Some(Box::new(e.into())),
792 limits: limits,
793 to: hi.map(|e| Box::new(e.into())),
794 }.into();
795 })
796 )) >>
797 (e)
798 ));
799
800 /// ```ignore
801 /// <and> || <and> ...
802 /// ```
803 binop!(or_expr, and_expr, map!(syn!(OrOr), BinOp::Or));
804
805 /// ```ignore
806 /// <compare> && <compare> ...
807 /// ```
808 binop!(and_expr, compare_expr, map!(syn!(AndAnd), BinOp::And));
809
810 /// ```ignore
811 /// <bitor> == <bitor> ...
812 /// <bitor> != <bitor> ...
813 /// <bitor> >= <bitor> ...
814 /// <bitor> <= <bitor> ...
815 /// <bitor> > <bitor> ...
816 /// <bitor> < <bitor> ...
817 /// ```
818 ///
819 /// NOTE: This operator appears to be parsed as left-associative, but errors
820 /// if it is used in a non-associative manner.
821 binop!(compare_expr, bitor_expr, alt!(
822 syn!(EqEq) => { BinOp::Eq }
823 |
824 syn!(Ne) => { BinOp::Ne }
825 |
826 // must be above Lt
827 syn!(Le) => { BinOp::Le }
828 |
829 // must be above Gt
830 syn!(Ge) => { BinOp::Ge }
831 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400832 do_parse!(
833 // Make sure that we don't eat the < part of a <- operator
834 not!(syn!(LArrow)) >>
835 t: syn!(Lt) >>
836 (BinOp::Lt(t))
837 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400838 |
839 syn!(Gt) => { BinOp::Gt }
840 ));
841
842 /// ```ignore
843 /// <bitxor> | <bitxor> ...
844 /// ```
845 binop!(bitor_expr, bitxor_expr, do_parse!(
846 not!(syn!(OrOr)) >>
847 not!(syn!(OrEq)) >>
848 t: syn!(Or) >>
849 (BinOp::BitOr(t))
850 ));
851
852 /// ```ignore
853 /// <bitand> ^ <bitand> ...
854 /// ```
855 binop!(bitxor_expr, bitand_expr, do_parse!(
856 // NOTE: Make sure we aren't looking at ^=.
857 not!(syn!(CaretEq)) >>
858 t: syn!(Caret) >>
859 (BinOp::BitXor(t))
860 ));
861
862 /// ```ignore
863 /// <shift> & <shift> ...
864 /// ```
865 binop!(bitand_expr, shift_expr, do_parse!(
866 // NOTE: Make sure we aren't looking at && or &=.
867 not!(syn!(AndAnd)) >>
868 not!(syn!(AndEq)) >>
869 t: syn!(And) >>
870 (BinOp::BitAnd(t))
871 ));
872
873 /// ```ignore
874 /// <arith> << <arith> ...
875 /// <arith> >> <arith> ...
876 /// ```
877 binop!(shift_expr, arith_expr, alt!(
878 syn!(Shl) => { BinOp::Shl }
879 |
880 syn!(Shr) => { BinOp::Shr }
881 ));
882
883 /// ```ignore
884 /// <term> + <term> ...
885 /// <term> - <term> ...
886 /// ```
887 binop!(arith_expr, term_expr, alt!(
888 syn!(Add) => { BinOp::Add }
889 |
890 syn!(Sub) => { BinOp::Sub }
891 ));
892
893 /// ```ignore
894 /// <cast> * <cast> ...
895 /// <cast> / <cast> ...
896 /// <cast> % <cast> ...
897 /// ```
898 binop!(term_expr, cast_expr, alt!(
899 syn!(Star) => { BinOp::Mul }
900 |
901 syn!(Div) => { BinOp::Div }
902 |
903 syn!(Rem) => { BinOp::Rem }
904 ));
905
906 /// ```ignore
907 /// <unary> as <ty>
908 /// <unary> : <ty>
909 /// ```
910 named!(cast_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
911 mut e: call!(unary_expr, allow_struct, allow_block) >>
912 many0!(alt!(
913 do_parse!(
914 as_: syn!(As) >>
915 // We can't accept `A + B` in cast expressions, as it's
916 // ambiguous with the + expression.
917 ty: call!(Ty::without_plus) >>
918 ({
919 e = ExprCast {
920 expr: Box::new(e.into()),
921 as_token: as_,
922 ty: Box::new(ty),
923 }.into();
924 })
925 )
926 |
927 do_parse!(
928 colon: syn!(Colon) >>
929 // We can't accept `A + B` in cast expressions, as it's
930 // ambiguous with the + expression.
931 ty: call!(Ty::without_plus) >>
932 ({
933 e = ExprType {
934 expr: Box::new(e.into()),
935 colon_token: colon,
936 ty: Box::new(ty),
937 }.into();
938 })
939 )
940 )) >>
941 (e)
942 ));
943
944 /// ```
945 /// <UnOp> <trailer>
946 /// & <trailer>
947 /// &mut <trailer>
948 /// box <trailer>
949 /// ```
950 named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
951 do_parse!(
952 op: syn!(UnOp) >>
953 expr: call!(unary_expr, allow_struct, true) >>
954 (ExprUnary {
955 op: op,
956 expr: Box::new(expr.into()),
957 }.into())
958 )
959 |
960 do_parse!(
961 and: syn!(And) >>
962 mutability: syn!(Mutability) >>
963 expr: call!(unary_expr, allow_struct, true) >>
964 (ExprAddrOf {
965 and_token: and,
966 mutbl: mutability,
967 expr: Box::new(expr.into()),
968 }.into())
969 )
970 |
971 do_parse!(
972 box_: syn!(Box_) >>
973 expr: call!(unary_expr, allow_struct, true) >>
974 (ExprBox {
975 box_token: box_,
976 expr: Box::new(expr.into()),
977 }.into())
978 )
979 |
980 call!(trailer_expr, allow_struct, allow_block)
981 ));
982
983 /// ```ignore
984 /// <atom> (..<args>) ...
985 /// <atom> . <ident> (..<args>) ...
986 /// <atom> . <ident> ...
987 /// <atom> . <lit> ...
988 /// <atom> [ <expr> ] ...
989 /// <atom> ? ...
990 /// ```
991 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
992 mut e: call!(atom_expr, allow_struct, allow_block) >>
993 many0!(alt!(
994 tap!(args: and_call => {
995 let (args, paren) = args;
996 e = ExprCall {
997 func: Box::new(e.into()),
998 args: args,
999 paren_token: paren,
1000 }.into();
1001 })
1002 |
1003 tap!(more: and_method_call => {
1004 let mut call = more;
1005 call.expr = Box::new(e.into());
1006 e = call.into();
1007 })
1008 |
1009 tap!(field: and_field => {
1010 let (field, token) = field;
1011 e = ExprField {
1012 expr: Box::new(e.into()),
1013 field: field,
1014 dot_token: token,
1015 }.into();
1016 })
1017 |
1018 tap!(field: and_tup_field => {
1019 let (field, token) = field;
1020 e = ExprTupField {
1021 expr: Box::new(e.into()),
1022 field: field,
1023 dot_token: token,
1024 }.into();
1025 })
1026 |
1027 tap!(i: and_index => {
1028 let (i, token) = i;
1029 e = ExprIndex {
1030 expr: Box::new(e.into()),
1031 bracket_token: token,
1032 index: Box::new(i),
1033 }.into();
1034 })
1035 |
1036 tap!(question: syn!(Question) => {
1037 e = ExprTry {
1038 expr: Box::new(e.into()),
1039 question_token: question,
1040 }.into();
1041 })
1042 )) >>
1043 (e)
1044 ));
1045
1046 /// Parse all atomic expressions which don't have to worry about precidence
1047 /// interactions, as they are fully contained.
1048 named!(atom_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
Michael Layzell93c36282017-06-04 20:43:14 -04001049 syn!(ExprGroup) => { ExprKind::Group } // must be placed first
1050 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001051 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
1052 |
1053 // must be before expr_path
1054 cond_reduce!(allow_struct, map!(syn!(ExprStruct), ExprKind::Struct))
1055 |
1056 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
1057 |
1058 syn!(Mac) => { ExprKind::Mac } // must be before expr_path
1059 |
1060 call!(expr_break, allow_struct) // must be before expr_path
1061 |
1062 syn!(ExprContinue) => { ExprKind::Continue } // must be before expr_path
1063 |
1064 call!(expr_ret, allow_struct) // must be before expr_path
1065 |
1066 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
1067 syn!(ExprInPlace) => { ExprKind::InPlace }
1068 |
1069 syn!(ExprArray) => { ExprKind::Array }
1070 |
1071 syn!(ExprTup) => { ExprKind::Tup }
1072 |
1073 syn!(ExprIf) => { ExprKind::If }
1074 |
1075 syn!(ExprIfLet) => { ExprKind::IfLet }
1076 |
1077 syn!(ExprWhile) => { ExprKind::While }
1078 |
1079 syn!(ExprWhileLet) => { ExprKind::WhileLet }
1080 |
1081 syn!(ExprForLoop) => { ExprKind::ForLoop }
1082 |
1083 syn!(ExprLoop) => { ExprKind::Loop }
1084 |
1085 syn!(ExprMatch) => { ExprKind::Match }
1086 |
1087 syn!(ExprCatch) => { ExprKind::Catch }
1088 |
1089 call!(expr_closure, allow_struct)
1090 |
1091 cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
1092 |
1093 // NOTE: This is the prefix-form of range
1094 call!(expr_range, allow_struct)
1095 |
1096 syn!(ExprPath) => { ExprKind::Path }
1097 |
1098 syn!(ExprRepeat) => { ExprKind::Repeat }
1099 ));
1100
Michael Layzell93c36282017-06-04 20:43:14 -04001101 impl Synom for ExprGroup {
1102 named!(parse -> Self, do_parse!(
1103 e: grouped!(syn!(Expr)) >>
1104 (ExprGroup {
1105 expr: Box::new(e.0),
1106 group_token: e.1,
1107 }.into())
1108 ));
1109 }
1110
Alex Crichton954046c2017-05-30 21:49:42 -07001111 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001112 named!(parse -> Self, do_parse!(
1113 e: parens!(syn!(Expr)) >>
1114 (ExprParen {
1115 expr: Box::new(e.0),
1116 paren_token: e.1,
1117 }.into())
1118 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001119 }
David Tolnay89e05672016-10-02 14:39:42 -07001120
Alex Crichton954046c2017-05-30 21:49:42 -07001121 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001122 named!(parse -> Self, do_parse!(
1123 in_: syn!(In) >>
1124 place: expr_no_struct >>
1125 value: braces!(call!(Block::parse_within)) >>
1126 (ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001127 place: Box::new(place),
Michael Layzell6a5a1642017-06-04 19:35:15 -04001128 kind: InPlaceKind::In(in_),
Michael Layzell92639a52017-06-01 00:07:44 -04001129 value: Box::new(Expr {
1130 node: ExprBlock {
1131 unsafety: Unsafety::Normal,
1132 block: Block {
1133 stmts: value.0,
1134 brace_token: value.1,
1135 },
1136 }.into(),
1137 attrs: Vec::new(),
1138 }),
1139 })
1140 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001141 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001142
Alex Crichton954046c2017-05-30 21:49:42 -07001143 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001144 named!(parse -> Self, do_parse!(
1145 elems: brackets!(call!(Delimited::parse_terminated)) >>
1146 (ExprArray {
1147 exprs: elems.0,
1148 bracket_token: elems.1,
1149 })
1150 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001151 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001152
Alex Crichton954046c2017-05-30 21:49:42 -07001153 named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren),
1154 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001155
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001156 named!(and_method_call -> ExprMethodCall, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001157 dot: syn!(Dot) >>
1158 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001159 typarams: option!(do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001160 colon2: syn!(Colon2) >>
1161 lt: syn!(Lt) >>
1162 tys: call!(Delimited::parse_terminated) >>
1163 gt: syn!(Gt) >>
1164 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001165 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001166 args: parens!(call!(Delimited::parse_terminated)) >>
1167 ({
1168 let (colon2, lt, tys, gt) = match typarams {
1169 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1170 None => (None, None, None, None),
1171 };
1172 ExprMethodCall {
1173 // this expr will get overwritten after being returned
1174 expr: Box::new(ExprKind::Lit(Lit {
1175 span: Span::default(),
1176 value: LitKind::Bool(false),
1177 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001178
Alex Crichton954046c2017-05-30 21:49:42 -07001179 method: method,
1180 args: args.0,
1181 paren_token: args.1,
1182 dot_token: dot,
1183 lt_token: lt,
1184 gt_token: gt,
1185 colon2_token: colon2,
1186 typarams: tys.unwrap_or_default(),
1187 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001188 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001189 ));
1190
Alex Crichton954046c2017-05-30 21:49:42 -07001191 impl Synom for ExprTup {
Michael Layzell92639a52017-06-01 00:07:44 -04001192 named!(parse -> Self, do_parse!(
1193 elems: parens!(call!(Delimited::parse_terminated)) >>
1194 (ExprTup {
1195 args: elems.0,
1196 paren_token: elems.1,
1197 lone_comma: None, // TODO: parse this
1198 })
1199 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001200 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001201
Alex Crichton954046c2017-05-30 21:49:42 -07001202 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001203 named!(parse -> Self, do_parse!(
1204 if_: syn!(If) >>
1205 let_: syn!(Let) >>
1206 pat: syn!(Pat) >>
1207 eq: syn!(Eq) >>
1208 cond: expr_no_struct >>
1209 then_block: braces!(call!(Block::parse_within)) >>
1210 else_block: option!(else_block) >>
1211 (ExprIfLet {
1212 pat: Box::new(pat),
1213 let_token: let_,
1214 eq_token: eq,
1215 expr: Box::new(cond),
1216 if_true: Block {
1217 stmts: then_block.0,
1218 brace_token: then_block.1,
1219 },
1220 if_token: if_,
1221 else_token: else_block.as_ref().map(|p| Else((p.0).0)),
1222 if_false: else_block.map(|p| Box::new(p.1.into())),
1223 })
1224 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001225 }
1226
Alex Crichton954046c2017-05-30 21:49:42 -07001227 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001228 named!(parse -> Self, do_parse!(
1229 if_: syn!(If) >>
1230 cond: expr_no_struct >>
1231 then_block: braces!(call!(Block::parse_within)) >>
1232 else_block: option!(else_block) >>
1233 (ExprIf {
1234 cond: Box::new(cond),
1235 if_true: Block {
1236 stmts: then_block.0,
1237 brace_token: then_block.1,
1238 },
1239 if_token: if_,
1240 else_token: else_block.as_ref().map(|p| Else((p.0).0)),
1241 if_false: else_block.map(|p| Box::new(p.1.into())),
1242 })
1243 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001244 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001245
Alex Crichton954046c2017-05-30 21:49:42 -07001246 named!(else_block -> (Else, ExprKind), do_parse!(
1247 else_: syn!(Else) >>
1248 expr: alt!(
1249 syn!(ExprIf) => { ExprKind::If }
1250 |
1251 syn!(ExprIfLet) => { ExprKind::IfLet }
1252 |
1253 do_parse!(
1254 else_block: braces!(call!(Block::parse_within)) >>
1255 (ExprKind::Block(ExprBlock {
1256 unsafety: Unsafety::Normal,
1257 block: Block {
1258 stmts: else_block.0,
1259 brace_token: else_block.1,
1260 },
1261 }))
David Tolnay939766a2016-09-23 23:48:12 -07001262 )
Alex Crichton954046c2017-05-30 21:49:42 -07001263 ) >>
1264 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001265 ));
1266
David Tolnaybb6feae2016-10-02 21:25:20 -07001267
Alex Crichton954046c2017-05-30 21:49:42 -07001268 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001269 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001270 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001271 for_: syn!(For) >>
1272 pat: syn!(Pat) >>
1273 in_: syn!(In) >>
1274 expr: expr_no_struct >>
1275 loop_block: syn!(Block) >>
1276 (ExprForLoop {
1277 for_token: for_,
1278 in_token: in_,
1279 pat: Box::new(pat),
1280 expr: Box::new(expr),
1281 body: loop_block,
1282 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1283 label: lbl.map(|p| p.0),
1284 })
1285 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001286 }
Gregory Katze5f35682016-09-27 14:20:55 -04001287
Alex Crichton954046c2017-05-30 21:49:42 -07001288 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001289 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001290 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001291 loop_: syn!(Loop) >>
1292 loop_block: syn!(Block) >>
1293 (ExprLoop {
1294 loop_token: loop_,
1295 body: loop_block,
1296 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1297 label: lbl.map(|p| p.0),
1298 })
1299 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001300 }
1301
1302 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001303 named!(parse -> Self, do_parse!(
1304 match_: syn!(Match) >>
1305 obj: expr_no_struct >>
1306 res: braces!(do_parse!(
1307 mut arms: many0!(do_parse!(
1308 arm: syn!(Arm) >>
1309 cond!(arm_requires_comma(&arm), syn!(Comma)) >>
1310 cond!(!arm_requires_comma(&arm), option!(syn!(Comma))) >>
1311 (arm)
Alex Crichton954046c2017-05-30 21:49:42 -07001312 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001313 last_arm: option!(syn!(Arm)) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001314 ({
Michael Layzell92639a52017-06-01 00:07:44 -04001315 arms.extend(last_arm);
1316 arms
Alex Crichton954046c2017-05-30 21:49:42 -07001317 })
Michael Layzell92639a52017-06-01 00:07:44 -04001318 )) >>
1319 ({
1320 let (mut arms, brace) = res;
1321 ExprMatch {
1322 expr: Box::new(obj),
1323 match_token: match_,
1324 brace_token: brace,
1325 arms: {
1326 for arm in &mut arms {
1327 if arm_requires_comma(arm) {
1328 arm.comma = Some(tokens::Comma::default());
1329 }
1330 }
1331 arms
1332 },
1333 }
1334 })
1335 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001336 }
David Tolnay1978c672016-10-27 22:05:52 -07001337
Alex Crichton954046c2017-05-30 21:49:42 -07001338 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001339 named!(parse -> Self, do_parse!(
1340 do_: syn!(Do) >>
1341 catch_: syn!(Catch) >>
1342 catch_block: syn!(Block) >>
1343 (ExprCatch {
1344 block: catch_block,
1345 do_token: do_,
1346 catch_token: catch_,
1347 }.into())
1348 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001349 }
Arnavion02ef13f2017-04-25 00:54:31 -07001350
David Tolnay1978c672016-10-27 22:05:52 -07001351 fn arm_requires_comma(arm: &Arm) -> bool {
Alex Crichton62a0a592017-05-22 13:58:53 -07001352 if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
David Tolnay1978c672016-10-27 22:05:52 -07001353 false
1354 } else {
1355 true
1356 }
1357 }
1358
Alex Crichton954046c2017-05-30 21:49:42 -07001359 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001360 named!(parse -> Self, do_parse!(
1361 attrs: many0!(call!(Attribute::parse_outer)) >>
1362 pats: call!(Delimited::parse_separated_nonempty) >>
1363 guard: option!(tuple!(syn!(If), syn!(Expr))) >>
1364 rocket: syn!(Rocket) >>
1365 body: alt!(
1366 map!(syn!(Block), |blk| {
1367 ExprKind::Block(ExprBlock {
1368 unsafety: Unsafety::Normal,
1369 block: blk,
1370 }).into()
Alex Crichton954046c2017-05-30 21:49:42 -07001371 })
Michael Layzell92639a52017-06-01 00:07:44 -04001372 |
1373 syn!(Expr)
1374 ) >>
1375 (Arm {
1376 rocket_token: rocket,
1377 if_token: guard.as_ref().map(|p| If((p.0).0)),
1378 attrs: attrs,
1379 pats: pats,
1380 guard: guard.map(|p| Box::new(p.1)),
1381 body: Box::new(body),
1382 comma: None,
1383 })
1384 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001385 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001386
Michael Layzellb78f3b52017-06-04 19:03:03 -04001387 named!(expr_closure(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001388 capture: syn!(CaptureBy) >>
1389 or1: syn!(Or) >>
1390 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
1391 or2: syn!(Or) >>
David Tolnay89e05672016-10-02 14:39:42 -07001392 ret_and_body: alt!(
1393 do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001394 arrow: syn!(RArrow) >>
1395 ty: syn!(Ty) >>
1396 body: syn!(Block) >>
1397 (FunctionRetTy::Ty(ty, arrow),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001398 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001399 unsafety: Unsafety::Normal,
1400 block: body,
1401 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001402 )
1403 |
David Tolnay58af3552016-12-22 16:58:07 -05001404 map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001405 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001406 (ExprClosure {
1407 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001408 or1_token: or1,
1409 or2_token: or2,
Alex Crichton62a0a592017-05-22 13:58:53 -07001410 decl: Box::new(FnDecl {
David Tolnay89e05672016-10-02 14:39:42 -07001411 inputs: inputs,
1412 output: ret_and_body.0,
David Tolnay292e6002016-10-29 22:03:51 -07001413 variadic: false,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001414 dot_tokens: None,
Alex Crichton954046c2017-05-30 21:49:42 -07001415 fn_token: tokens::Fn_::default(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001416 generics: Generics::default(),
1417 paren_token: tokens::Paren::default(),
David Tolnay89e05672016-10-02 14:39:42 -07001418 }),
Alex Crichton62a0a592017-05-22 13:58:53 -07001419 body: Box::new(ret_and_body.1),
1420 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001421 ));
1422
Alex Crichton954046c2017-05-30 21:49:42 -07001423 named!(fn_arg -> FnArg, do_parse!(
1424 pat: syn!(Pat) >>
1425 ty: option!(tuple!(syn!(Colon), syn!(Ty))) >>
1426 ({
1427 let (colon, ty) = ty.unwrap_or_else(|| {
1428 (Colon::default(), TyInfer {
1429 underscore_token: Underscore::default(),
1430 }.into())
1431 });
1432 ArgCaptured {
1433 pat: pat,
1434 colon_token: colon,
1435 ty: ty,
1436 }.into()
David Tolnaybb6feae2016-10-02 21:25:20 -07001437 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001438 ));
1439
Alex Crichton954046c2017-05-30 21:49:42 -07001440 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001441 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001442 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001443 while_: syn!(While) >>
1444 cond: expr_no_struct >>
1445 while_block: syn!(Block) >>
1446 (ExprWhile {
1447 while_token: while_,
1448 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1449 cond: Box::new(cond),
1450 body: while_block,
1451 label: lbl.map(|p| p.0),
1452 })
1453 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001454 }
1455
1456 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001457 named!(parse -> Self, do_parse!(
David Tolnay63e3dee2017-06-03 20:13:17 -07001458 lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001459 while_: syn!(While) >>
1460 let_: syn!(Let) >>
1461 pat: syn!(Pat) >>
1462 eq: syn!(Eq) >>
1463 value: expr_no_struct >>
1464 while_block: syn!(Block) >>
1465 (ExprWhileLet {
1466 eq_token: eq,
1467 let_token: let_,
1468 while_token: while_,
1469 colon_token: lbl.as_ref().map(|p| Colon((p.1).0)),
1470 pat: Box::new(pat),
1471 expr: Box::new(value),
1472 body: while_block,
1473 label: lbl.map(|p| p.0),
1474 })
1475 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001476 }
1477
1478 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001479 named!(parse -> Self, do_parse!(
1480 cont: syn!(Continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001481 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001482 (ExprContinue {
1483 continue_token: cont,
1484 label: lbl,
1485 })
1486 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001487 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001488
Michael Layzellb78f3b52017-06-04 19:03:03 -04001489 named!(expr_break(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001490 break_: syn!(Break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001491 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001492 // We can't allow blocks after a `break` expression when we wouldn't
1493 // allow structs, as this expression is ambiguous.
1494 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001495 (ExprBreak {
1496 label: lbl,
1497 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001498 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001499 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001500 ));
1501
Michael Layzellb78f3b52017-06-04 19:03:03 -04001502 named!(expr_ret(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001503 return_: syn!(Return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001504 // NOTE: return is greedy and eats blocks after it even when in a
1505 // position where structs are not allowed, such as in if statement
1506 // conditions. For example:
1507 //
1508 // if return { println!("A") } { } // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001509 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001510 (ExprRet {
1511 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001512 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001513 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001514 ));
1515
Alex Crichton954046c2017-05-30 21:49:42 -07001516 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001517 named!(parse -> Self, do_parse!(
1518 path: syn!(Path) >>
1519 data: braces!(do_parse!(
1520 fields: call!(Delimited::parse_terminated) >>
1521 base: option!(
1522 cond!(fields.is_empty() || fields.trailing_delim(),
1523 do_parse!(
1524 dots: syn!(Dot2) >>
1525 base: syn!(Expr) >>
1526 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001527 )
Michael Layzell92639a52017-06-01 00:07:44 -04001528 )
1529 ) >>
1530 (fields, base)
1531 )) >>
1532 ({
1533 let ((fields, base), brace) = data;
1534 let (dots, rest) = match base.and_then(|b| b) {
1535 Some((dots, base)) => (Some(dots), Some(base)),
1536 None => (None, None),
1537 };
1538 ExprStruct {
1539 brace_token: brace,
1540 path: path,
1541 fields: fields,
1542 dot2_token: dots,
1543 rest: rest.map(Box::new),
1544 }
1545 })
1546 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001547 }
1548
1549 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001550 named!(parse -> Self, alt!(
1551 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -07001552 ident: field_ident >>
Michael Layzell92639a52017-06-01 00:07:44 -04001553 colon: syn!(Colon) >>
1554 value: syn!(Expr) >>
1555 (FieldValue {
David Tolnay570695e2017-06-03 16:15:13 -07001556 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04001557 expr: value,
1558 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001559 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001560 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001561 })
Michael Layzell92639a52017-06-01 00:07:44 -04001562 )
1563 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001564 map!(syn!(Ident), |name| FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001565 ident: name.clone(),
1566 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
1567 is_shorthand: true,
1568 attrs: Vec::new(),
1569 colon_token: None,
1570 })
1571 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001572 }
David Tolnay055a7042016-10-02 19:23:54 -07001573
Alex Crichton954046c2017-05-30 21:49:42 -07001574 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001575 named!(parse -> Self, do_parse!(
1576 data: brackets!(do_parse!(
1577 value: syn!(Expr) >>
1578 semi: syn!(Semi) >>
1579 times: syn!(Expr) >>
1580 (value, semi, times)
1581 )) >>
1582 (ExprRepeat {
1583 expr: Box::new((data.0).0),
1584 amt: Box::new((data.0).2),
1585 bracket_token: data.1,
1586 semi_token: (data.0).1,
1587 })
1588 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001589 }
David Tolnay055a7042016-10-02 19:23:54 -07001590
Alex Crichton954046c2017-05-30 21:49:42 -07001591 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001592 named!(parse -> Self, do_parse!(
1593 rules: syn!(Unsafety) >>
1594 b: syn!(Block) >>
1595 (ExprBlock {
1596 unsafety: rules,
1597 block: b,
1598 })
1599 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001600 }
David Tolnay89e05672016-10-02 14:39:42 -07001601
Michael Layzellb78f3b52017-06-04 19:03:03 -04001602 named!(expr_range(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001603 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001604 hi: opt_ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001605 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001606 ));
1607
Alex Crichton954046c2017-05-30 21:49:42 -07001608 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001609 named!(parse -> Self, alt!(
1610 // Must come before Dot2
1611 syn!(Dot3) => { RangeLimits::Closed }
1612 |
1613 syn!(Dot2) => { RangeLimits::HalfOpen }
1614 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001615 }
David Tolnay438c9052016-10-07 23:24:48 -07001616
Alex Crichton954046c2017-05-30 21:49:42 -07001617 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001618 named!(parse -> Self, do_parse!(
1619 pair: qpath >>
1620 (ExprPath {
1621 qself: pair.0,
1622 path: pair.1,
1623 })
1624 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001625 }
David Tolnay42602292016-10-01 22:25:45 -07001626
Alex Crichton954046c2017-05-30 21:49:42 -07001627 named!(and_field -> (Ident, Dot),
1628 map!(tuple!(syn!(Dot), syn!(Ident)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001629
Alex Crichton954046c2017-05-30 21:49:42 -07001630 named!(and_tup_field -> (Lit, Dot),
1631 map!(tuple!(syn!(Dot), syn!(Lit)), |(a, b)| (b, a)));
David Tolnay438c9052016-10-07 23:24:48 -07001632
Alex Crichton954046c2017-05-30 21:49:42 -07001633 named!(and_index -> (Expr, tokens::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001634
Alex Crichton954046c2017-05-30 21:49:42 -07001635 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001636 named!(parse -> Self, do_parse!(
1637 stmts: braces!(call!(Block::parse_within)) >>
1638 (Block {
1639 stmts: stmts.0,
1640 brace_token: stmts.1,
1641 })
1642 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001643 }
David Tolnay939766a2016-09-23 23:48:12 -07001644
Alex Crichton954046c2017-05-30 21:49:42 -07001645 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001646 named!(pub parse_within -> Vec<Stmt>, do_parse!(
1647 many0!(syn!(Semi)) >>
1648 mut standalone: many0!(terminated!(syn!(Stmt), many0!(syn!(Semi)))) >>
1649 last: option!(syn!(Expr)) >>
1650 (match last {
1651 None => standalone,
1652 Some(last) => {
1653 standalone.push(Stmt::Expr(Box::new(last)));
1654 standalone
1655 }
1656 })
1657 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001658 }
1659
1660 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001661 named!(parse -> Self, alt!(
1662 stmt_mac
1663 |
1664 stmt_local
1665 |
1666 stmt_item
1667 |
1668 stmt_expr
1669 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001670 }
David Tolnay939766a2016-09-23 23:48:12 -07001671
David Tolnay13b3d352016-10-03 00:31:15 -07001672 named!(stmt_mac -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001673 attrs: many0!(call!(Attribute::parse_outer)) >>
1674 what: syn!(Path) >>
1675 bang: syn!(Bang) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001676 // Only parse braces here; paren and bracket will get parsed as
1677 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001678 data: braces!(syn!(TokenStream)) >>
1679 semi: option!(syn!(Semi)) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001680 (Stmt::Mac(Box::new((
1681 Mac {
David Tolnay5d55ef72016-12-21 20:20:04 -05001682 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001683 bang_token: bang,
David Tolnay570695e2017-06-03 16:15:13 -07001684 ident: None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001685 tokens: vec![TokenTree(proc_macro2::TokenTree {
Alex Crichton954046c2017-05-30 21:49:42 -07001686 span: ((data.1).0).0,
1687 kind: TokenKind::Sequence(Delimiter::Brace, data.0),
David Tolnayeea28d62016-10-25 20:44:08 -07001688 })],
1689 },
Alex Crichton954046c2017-05-30 21:49:42 -07001690 match semi {
1691 Some(semi) => MacStmtStyle::Semicolon(semi),
1692 None => MacStmtStyle::Braces,
David Tolnay60d48942016-10-30 14:34:52 -07001693 },
David Tolnayeea28d62016-10-25 20:44:08 -07001694 attrs,
1695 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001696 ));
1697
David Tolnay191e0582016-10-02 18:31:09 -07001698 named!(stmt_local -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001699 attrs: many0!(call!(Attribute::parse_outer)) >>
1700 let_: syn!(Let) >>
1701 pat: syn!(Pat) >>
1702 ty: option!(tuple!(syn!(Colon), syn!(Ty))) >>
1703 init: option!(tuple!(syn!(Eq), syn!(Expr))) >>
1704 semi: syn!(Semi) >>
David Tolnay191e0582016-10-02 18:31:09 -07001705 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001706 let_token: let_,
1707 semi_token: semi,
1708 colon_token: ty.as_ref().map(|p| Colon((p.0).0)),
1709 eq_token: init.as_ref().map(|p| Eq((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001710 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001711 ty: ty.map(|p| Box::new(p.1)),
1712 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001713 attrs: attrs,
1714 })))
1715 ));
1716
Alex Crichton954046c2017-05-30 21:49:42 -07001717 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07001718
David Tolnaycfe55022016-10-02 22:02:27 -07001719 fn requires_semi(e: &Expr) -> bool {
David Tolnay7184b132016-10-30 10:06:37 -07001720 match e.node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001721 ExprKind::If(_) |
1722 ExprKind::IfLet(_) |
1723 ExprKind::While(_) |
1724 ExprKind::WhileLet(_) |
1725 ExprKind::ForLoop(_) |
1726 ExprKind::Loop(_) |
1727 ExprKind::Match(_) |
1728 ExprKind::Block(_) => false,
David Tolnaycfe55022016-10-02 22:02:27 -07001729
1730 _ => true,
1731 }
1732 }
1733
1734 named!(stmt_expr -> Stmt, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001735 attrs: many0!(call!(Attribute::parse_outer)) >>
1736 mut e: syn!(Expr) >>
1737 semi: option!(syn!(Semi)) >>
David Tolnay7184b132016-10-30 10:06:37 -07001738 ({
1739 e.attrs = attrs;
Alex Crichton954046c2017-05-30 21:49:42 -07001740 if let Some(s) = semi {
1741 Stmt::Semi(Box::new(e), s)
David Tolnay092dcb02016-10-30 10:14:14 -07001742 } else if requires_semi(&e) {
Michael Layzell92639a52017-06-01 00:07:44 -04001743 return parse_error();
David Tolnay7184b132016-10-30 10:06:37 -07001744 } else {
1745 Stmt::Expr(Box::new(e))
1746 }
David Tolnaycfe55022016-10-02 22:02:27 -07001747 })
David Tolnay939766a2016-09-23 23:48:12 -07001748 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001749
Alex Crichton954046c2017-05-30 21:49:42 -07001750 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04001751 named!(parse -> Self, alt!(
1752 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
1753 |
1754 syn!(PatBox) => { Pat::Box } // must be before pat_ident
1755 |
1756 syn!(PatRange) => { Pat::Range } // must be before pat_lit
1757 |
1758 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
1759 |
1760 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
1761 |
1762 syn!(Mac) => { Pat::Mac } // must be before pat_ident
1763 |
1764 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
1765 |
1766 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
1767 |
1768 syn!(PatPath) => { Pat::Path }
1769 |
1770 syn!(PatTuple) => { Pat::Tuple }
1771 |
1772 syn!(PatRef) => { Pat::Ref }
1773 |
1774 syn!(PatSlice) => { Pat::Slice }
1775 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001776 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001777
Alex Crichton954046c2017-05-30 21:49:42 -07001778 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04001779 named!(parse -> Self, map!(
1780 syn!(Underscore),
1781 |u| PatWild { underscore_token: u }
1782 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001783 }
David Tolnay84aa0752016-10-02 23:01:13 -07001784
Alex Crichton954046c2017-05-30 21:49:42 -07001785 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04001786 named!(parse -> Self, do_parse!(
1787 boxed: syn!(Box_) >>
1788 pat: syn!(Pat) >>
1789 (PatBox {
1790 pat: Box::new(pat),
1791 box_token: boxed,
1792 })
1793 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001794 }
1795
1796 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04001797 named!(parse -> Self, do_parse!(
1798 mode: option!(syn!(Ref)) >>
1799 mutability: syn!(Mutability) >>
1800 name: alt!(
1801 syn!(Ident)
1802 |
1803 syn!(Self_) => { Into::into }
1804 ) >>
1805 not!(syn!(Lt)) >>
1806 not!(syn!(Colon2)) >>
1807 subpat: option!(tuple!(syn!(At), syn!(Pat))) >>
1808 (PatIdent {
1809 mode: match mode {
1810 Some(mode) => BindingMode::ByRef(mode, mutability),
1811 None => BindingMode::ByValue(mutability),
1812 },
1813 ident: name,
1814 at_token: subpat.as_ref().map(|p| At((p.0).0)),
1815 subpat: subpat.map(|p| Box::new(p.1)),
1816 })
1817 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001818 }
1819
1820 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001821 named!(parse -> Self, do_parse!(
1822 path: syn!(Path) >>
1823 tuple: syn!(PatTuple) >>
1824 (PatTupleStruct {
1825 path: path,
1826 pat: tuple,
1827 })
1828 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001829 }
1830
1831 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001832 named!(parse -> Self, do_parse!(
1833 path: syn!(Path) >>
1834 data: braces!(do_parse!(
1835 fields: call!(Delimited::parse_terminated) >>
1836 base: option!(
1837 cond!(fields.is_empty() || fields.trailing_delim(),
1838 syn!(Dot2))
1839 ) >>
1840 (fields, base)
1841 )) >>
1842 (PatStruct {
1843 path: path,
1844 fields: (data.0).0,
1845 brace_token: data.1,
1846 dot2_token: (data.0).1.and_then(|m| m),
1847 })
1848 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001849 }
1850
1851 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04001852 named!(parse -> Self, alt!(
1853 do_parse!(
David Tolnay570695e2017-06-03 16:15:13 -07001854 ident: field_ident >>
Michael Layzell92639a52017-06-01 00:07:44 -04001855 colon: syn!(Colon) >>
1856 pat: syn!(Pat) >>
1857 (FieldPat {
1858 ident: ident,
1859 pat: Box::new(pat),
1860 is_shorthand: false,
1861 attrs: Vec::new(),
1862 colon_token: Some(colon),
1863 })
1864 )
1865 |
1866 do_parse!(
1867 boxed: option!(syn!(Box_)) >>
1868 mode: option!(syn!(Ref)) >>
1869 mutability: syn!(Mutability) >>
1870 ident: syn!(Ident) >>
1871 ({
1872 let mut pat: Pat = PatIdent {
1873 mode: if let Some(mode) = mode {
1874 BindingMode::ByRef(mode, mutability)
1875 } else {
1876 BindingMode::ByValue(mutability)
1877 },
1878 ident: ident.clone(),
1879 subpat: None,
1880 at_token: None,
1881 }.into();
1882 if let Some(boxed) = boxed {
1883 pat = PatBox {
1884 pat: Box::new(pat),
1885 box_token: boxed,
1886 }.into();
1887 }
1888 FieldPat {
Alex Crichton954046c2017-05-30 21:49:42 -07001889 ident: ident,
1890 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04001891 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07001892 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001893 colon_token: None,
1894 }
1895 })
1896 )
1897 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001898 }
1899
David Tolnay570695e2017-06-03 16:15:13 -07001900 named!(field_ident -> Ident, alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07001901 syn!(Ident)
1902 |
1903 do_parse!(
1904 lit: syn!(Lit) >>
1905 ({
David Tolnay570695e2017-06-03 16:15:13 -07001906 let s = lit.to_string();
1907 if s.parse::<usize>().is_ok() {
Alex Crichton954046c2017-05-30 21:49:42 -07001908 Ident::new(s.into(), lit.span)
1909 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04001910 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07001911 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07001912 })
1913 )
1914 ));
1915
Alex Crichton954046c2017-05-30 21:49:42 -07001916 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001917 named!(parse -> Self, map!(
1918 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07001919 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04001920 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001921 }
David Tolnay9636c052016-10-02 17:11:17 -07001922
Alex Crichton954046c2017-05-30 21:49:42 -07001923 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001924 named!(parse -> Self, do_parse!(
1925 data: parens!(do_parse!(
1926 elems: call!(Delimited::parse_terminated) >>
1927 dotdot: map!(cond!(
1928 elems.is_empty() || elems.trailing_delim(),
1929 option!(do_parse!(
1930 dots: syn!(Dot2) >>
1931 trailing: option!(syn!(Comma)) >>
1932 (dots, trailing)
1933 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07001934 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001935 rest: cond!(match dotdot {
1936 Some((_, Some(_))) => true,
1937 _ => false,
1938 },
1939 call!(Delimited::parse_terminated)) >>
1940 (elems, dotdot, rest)
1941 )) >>
1942 ({
1943 let ((mut elems, dotdot, rest), parens) = data;
1944 let (dotdot, trailing) = match dotdot {
1945 Some((a, b)) => (Some(a), Some(b)),
1946 None => (None, None),
1947 };
1948 PatTuple {
1949 paren_token: parens,
1950 dots_pos: dotdot.as_ref().map(|_| elems.len()),
1951 dot2_token: dotdot,
1952 comma_token: trailing.and_then(|b| b),
1953 pats: {
1954 if let Some(rest) = rest {
1955 for elem in rest {
1956 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07001957 }
Michael Layzell92639a52017-06-01 00:07:44 -04001958 }
1959 elems
1960 },
1961 }
1962 })
1963 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001964 }
David Tolnayfbb73232016-10-03 01:00:06 -07001965
Alex Crichton954046c2017-05-30 21:49:42 -07001966 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04001967 named!(parse -> Self, do_parse!(
1968 and: syn!(And) >>
1969 mutability: syn!(Mutability) >>
1970 pat: syn!(Pat) >>
1971 (PatRef {
1972 pat: Box::new(pat),
1973 mutbl: mutability,
1974 and_token: and,
1975 })
1976 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001977 }
David Tolnayffdb97f2016-10-03 01:28:33 -07001978
Alex Crichton954046c2017-05-30 21:49:42 -07001979 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04001980 named!(parse -> Self, do_parse!(
1981 lit: pat_lit_expr >>
1982 (if let ExprKind::Path(_) = lit.node {
1983 return parse_error(); // these need to be parsed by pat_path
1984 } else {
1985 PatLit {
1986 expr: Box::new(lit),
1987 }
1988 })
1989 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001990 }
David Tolnaye1310902016-10-29 23:40:00 -07001991
Alex Crichton954046c2017-05-30 21:49:42 -07001992 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04001993 named!(parse -> Self, do_parse!(
1994 lo: pat_lit_expr >>
1995 limits: syn!(RangeLimits) >>
1996 hi: pat_lit_expr >>
1997 (PatRange {
1998 lo: Box::new(lo),
1999 hi: Box::new(hi),
2000 limits: limits,
2001 })
2002 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002003 }
David Tolnaye1310902016-10-29 23:40:00 -07002004
David Tolnay2cfddc62016-10-30 01:03:27 -07002005 named!(pat_lit_expr -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002006 neg: option!(syn!(Sub)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002007 v: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07002008 syn!(Lit) => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002009 |
Alex Crichton954046c2017-05-30 21:49:42 -07002010 syn!(ExprPath) => { ExprKind::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002011 ) >>
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002012 (if neg.is_some() {
Alex Crichton62a0a592017-05-22 13:58:53 -07002013 ExprKind::Unary(ExprUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002014 op: UnOp::Neg(tokens::Sub::default()),
Alex Crichton62a0a592017-05-22 13:58:53 -07002015 expr: Box::new(v.into())
2016 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002017 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002018 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002019 })
2020 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002021
Alex Crichton954046c2017-05-30 21:49:42 -07002022 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002023 named!(parse -> Self, map!(
2024 brackets!(do_parse!(
2025 before: call!(Delimited::parse_terminated) >>
2026 middle: option!(do_parse!(
2027 dots: syn!(Dot2) >>
2028 trailing: option!(syn!(Comma)) >>
2029 (dots, trailing)
2030 )) >>
2031 after: cond!(
2032 match middle {
2033 Some((_, ref trailing)) => trailing.is_some(),
2034 _ => false,
2035 },
2036 call!(Delimited::parse_terminated)
2037 ) >>
2038 (before, middle, after)
2039 )),
2040 |((before, middle, after), brackets)| {
2041 let mut before: Delimited<Pat, tokens::Comma> = before;
2042 let after: Option<Delimited<Pat, tokens::Comma>> = after;
2043 let middle: Option<(Dot2, Option<Comma>)> = middle;
2044 PatSlice {
2045 dot2_token: middle.as_ref().map(|m| Dot2((m.0).0)),
2046 comma_token: middle.as_ref().and_then(|m| {
2047 m.1.as_ref().map(|m| Comma(m.0))
2048 }),
2049 bracket_token: brackets,
2050 middle: middle.and_then(|_| {
2051 if !before.is_empty() && !before.trailing_delim() {
2052 Some(Box::new(before.pop().unwrap().into_item()))
2053 } else {
2054 None
2055 }
2056 }),
2057 front: before,
2058 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002059 }
Alex Crichton954046c2017-05-30 21:49:42 -07002060 }
Michael Layzell92639a52017-06-01 00:07:44 -04002061 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002062 }
David Tolnay435a9a82016-10-29 13:47:20 -07002063
Alex Crichton954046c2017-05-30 21:49:42 -07002064 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002065 named!(parse -> Self, alt!(
2066 syn!(Move) => { CaptureBy::Value }
2067 |
2068 epsilon!() => { |_| CaptureBy::Ref }
2069 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002070 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002071}
2072
David Tolnayf4bbbd92016-09-23 14:41:55 -07002073#[cfg(feature = "printing")]
2074mod printing {
2075 use super::*;
David Tolnay13b3d352016-10-03 00:31:15 -07002076 use attr::FilterAttrs;
David Tolnayf4bbbd92016-09-23 14:41:55 -07002077 use quote::{Tokens, ToTokens};
2078
2079 impl ToTokens for Expr {
2080 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07002081 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002082 self.node.to_tokens(tokens)
2083 }
2084 }
2085
2086 impl ToTokens for ExprBox {
2087 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002088 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002089 self.expr.to_tokens(tokens);
2090 }
2091 }
2092
2093 impl ToTokens for ExprInPlace {
2094 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell6a5a1642017-06-04 19:35:15 -04002095 match self.kind {
2096 InPlaceKind::Arrow(ref arrow) => {
2097 self.place.to_tokens(tokens);
2098 arrow.to_tokens(tokens);
2099 self.value.to_tokens(tokens);
2100 }
2101 InPlaceKind::In(ref _in) => {
2102 _in.to_tokens(tokens);
2103 self.place.to_tokens(tokens);
2104 self.value.to_tokens(tokens);
2105 }
2106 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002107 }
2108 }
2109
2110 impl ToTokens for ExprArray {
2111 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002112 self.bracket_token.surround(tokens, |tokens| {
2113 self.exprs.to_tokens(tokens);
2114 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002115 }
2116 }
2117
2118 impl ToTokens for ExprCall {
2119 fn to_tokens(&self, tokens: &mut Tokens) {
2120 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002121 self.paren_token.surround(tokens, |tokens| {
2122 self.args.to_tokens(tokens);
2123 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002124 }
2125 }
2126
2127 impl ToTokens for ExprMethodCall {
2128 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002129 self.expr.to_tokens(tokens);
2130 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002131 self.method.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002132 self.colon2_token.to_tokens(tokens);
2133 self.lt_token.to_tokens(tokens);
2134 self.typarams.to_tokens(tokens);
2135 self.gt_token.to_tokens(tokens);
2136 self.paren_token.surround(tokens, |tokens| {
2137 self.args.to_tokens(tokens);
2138 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002139 }
2140 }
2141
2142 impl ToTokens for ExprTup {
2143 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002144 self.paren_token.surround(tokens, |tokens| {
2145 self.args.to_tokens(tokens);
2146 self.lone_comma.to_tokens(tokens);
2147 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002148 }
2149 }
2150
2151 impl ToTokens for ExprBinary {
2152 fn to_tokens(&self, tokens: &mut Tokens) {
2153 self.left.to_tokens(tokens);
2154 self.op.to_tokens(tokens);
2155 self.right.to_tokens(tokens);
2156 }
2157 }
2158
2159 impl ToTokens for ExprUnary {
2160 fn to_tokens(&self, tokens: &mut Tokens) {
2161 self.op.to_tokens(tokens);
2162 self.expr.to_tokens(tokens);
2163 }
2164 }
2165
2166 impl ToTokens for ExprCast {
2167 fn to_tokens(&self, tokens: &mut Tokens) {
2168 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002169 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002170 self.ty.to_tokens(tokens);
2171 }
2172 }
2173
2174 impl ToTokens for ExprType {
2175 fn to_tokens(&self, tokens: &mut Tokens) {
2176 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002177 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002178 self.ty.to_tokens(tokens);
2179 }
2180 }
2181
2182 impl ToTokens for ExprIf {
2183 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002184 self.if_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002185 self.cond.to_tokens(tokens);
2186 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002187 self.else_token.to_tokens(tokens);
2188 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002189 }
2190 }
2191
2192 impl ToTokens for ExprIfLet {
2193 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002194 self.if_token.to_tokens(tokens);
2195 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002196 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002197 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002198 self.expr.to_tokens(tokens);
2199 self.if_true.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002200 self.else_token.to_tokens(tokens);
2201 self.if_false.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002202 }
2203 }
2204
2205 impl ToTokens for ExprWhile {
2206 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002207 self.label.to_tokens(tokens);
2208 self.colon_token.to_tokens(tokens);
2209 self.while_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002210 self.cond.to_tokens(tokens);
2211 self.body.to_tokens(tokens);
2212 }
2213 }
2214
2215 impl ToTokens for ExprWhileLet {
2216 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002217 self.label.to_tokens(tokens);
2218 self.colon_token.to_tokens(tokens);
2219 self.while_token.to_tokens(tokens);
2220 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002221 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002222 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002223 self.expr.to_tokens(tokens);
2224 self.body.to_tokens(tokens);
2225 }
2226 }
2227
2228 impl ToTokens for ExprForLoop {
2229 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002230 self.label.to_tokens(tokens);
2231 self.colon_token.to_tokens(tokens);
2232 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002233 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002234 self.in_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002235 self.expr.to_tokens(tokens);
2236 self.body.to_tokens(tokens);
2237 }
2238 }
2239
2240 impl ToTokens for ExprLoop {
2241 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002242 self.label.to_tokens(tokens);
2243 self.colon_token.to_tokens(tokens);
2244 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002245 self.body.to_tokens(tokens);
2246 }
2247 }
2248
2249 impl ToTokens for ExprMatch {
2250 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002251 self.match_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002252 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002253 self.brace_token.surround(tokens, |tokens| {
2254 tokens.append_all(&self.arms);
2255 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002256 }
2257 }
2258
2259 impl ToTokens for ExprCatch {
2260 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002261 self.do_token.to_tokens(tokens);
2262 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002263 self.block.to_tokens(tokens);
2264 }
2265 }
2266
2267 impl ToTokens for ExprClosure {
2268 fn to_tokens(&self, tokens: &mut Tokens) {
2269 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002270 self.or1_token.to_tokens(tokens);
2271 for item in self.decl.inputs.iter() {
2272 match **item.item() {
2273 FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002274 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002275 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002276 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002277 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002278 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002279 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002280 self.or2_token.to_tokens(tokens);
2281 self.decl.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002282 self.body.to_tokens(tokens);
2283 }
2284 }
2285
2286 impl ToTokens for ExprBlock {
2287 fn to_tokens(&self, tokens: &mut Tokens) {
2288 self.unsafety.to_tokens(tokens);
2289 self.block.to_tokens(tokens);
2290 }
2291 }
2292
2293 impl ToTokens for ExprAssign {
2294 fn to_tokens(&self, tokens: &mut Tokens) {
2295 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002296 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002297 self.right.to_tokens(tokens);
2298 }
2299 }
2300
2301 impl ToTokens for ExprAssignOp {
2302 fn to_tokens(&self, tokens: &mut Tokens) {
2303 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002304 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002305 self.right.to_tokens(tokens);
2306 }
2307 }
2308
2309 impl ToTokens for ExprField {
2310 fn to_tokens(&self, tokens: &mut Tokens) {
2311 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002312 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002313 self.field.to_tokens(tokens);
2314 }
2315 }
2316
2317 impl ToTokens for ExprTupField {
2318 fn to_tokens(&self, tokens: &mut Tokens) {
2319 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002320 self.dot_token.to_tokens(tokens);
2321 self.field.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002322 }
2323 }
2324
2325 impl ToTokens for ExprIndex {
2326 fn to_tokens(&self, tokens: &mut Tokens) {
2327 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002328 self.bracket_token.surround(tokens, |tokens| {
2329 self.index.to_tokens(tokens);
2330 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002331 }
2332 }
2333
2334 impl ToTokens for ExprRange {
2335 fn to_tokens(&self, tokens: &mut Tokens) {
2336 self.from.to_tokens(tokens);
2337 self.limits.to_tokens(tokens);
2338 self.to.to_tokens(tokens);
2339 }
2340 }
2341
2342 impl ToTokens for ExprPath {
2343 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002344 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002345 }
2346 }
2347
2348 impl ToTokens for ExprAddrOf {
2349 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002350 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002351 self.mutbl.to_tokens(tokens);
2352 self.expr.to_tokens(tokens);
2353 }
2354 }
2355
2356 impl ToTokens for ExprBreak {
2357 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002358 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002359 self.label.to_tokens(tokens);
2360 self.expr.to_tokens(tokens);
2361 }
2362 }
2363
2364 impl ToTokens for ExprContinue {
2365 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002366 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002367 self.label.to_tokens(tokens);
2368 }
2369 }
2370
2371 impl ToTokens for ExprRet {
2372 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002373 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002374 self.expr.to_tokens(tokens);
2375 }
2376 }
2377
2378 impl ToTokens for ExprStruct {
2379 fn to_tokens(&self, tokens: &mut Tokens) {
2380 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002381 self.brace_token.surround(tokens, |tokens| {
2382 self.fields.to_tokens(tokens);
2383 self.dot2_token.to_tokens(tokens);
2384 self.rest.to_tokens(tokens);
2385 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002386 }
2387 }
2388
2389 impl ToTokens for ExprRepeat {
2390 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002391 self.bracket_token.surround(tokens, |tokens| {
2392 self.expr.to_tokens(tokens);
2393 self.semi_token.to_tokens(tokens);
2394 self.amt.to_tokens(tokens);
2395 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002396 }
2397 }
2398
Michael Layzell93c36282017-06-04 20:43:14 -04002399 impl ToTokens for ExprGroup {
2400 fn to_tokens(&self, tokens: &mut Tokens) {
2401 self.group_token.surround(tokens, |tokens| {
2402 self.expr.to_tokens(tokens);
2403 });
2404 }
2405 }
2406
Alex Crichton62a0a592017-05-22 13:58:53 -07002407 impl ToTokens for ExprParen {
2408 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002409 self.paren_token.surround(tokens, |tokens| {
2410 self.expr.to_tokens(tokens);
2411 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002412 }
2413 }
2414
2415 impl ToTokens for ExprTry {
2416 fn to_tokens(&self, tokens: &mut Tokens) {
2417 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002418 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002419 }
2420 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002421
David Tolnay055a7042016-10-02 19:23:54 -07002422 impl ToTokens for FieldValue {
2423 fn to_tokens(&self, tokens: &mut Tokens) {
2424 self.ident.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002425 if !self.is_shorthand {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002426 self.colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002427 self.expr.to_tokens(tokens);
2428 }
David Tolnay055a7042016-10-02 19:23:54 -07002429 }
2430 }
2431
David Tolnayb4ad3b52016-10-01 21:58:13 -07002432 impl ToTokens for Arm {
2433 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002434 tokens.append_all(&self.attrs);
2435 self.pats.to_tokens(tokens);
2436 self.if_token.to_tokens(tokens);
2437 self.guard.to_tokens(tokens);
2438 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002439 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002440 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002441 }
2442 }
2443
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002444 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002445 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002446 self.underscore_token.to_tokens(tokens);
2447 }
2448 }
2449
2450 impl ToTokens for PatIdent {
2451 fn to_tokens(&self, tokens: &mut Tokens) {
2452 self.mode.to_tokens(tokens);
2453 self.ident.to_tokens(tokens);
2454 self.at_token.to_tokens(tokens);
2455 self.subpat.to_tokens(tokens);
2456 }
2457 }
2458
2459 impl ToTokens for PatStruct {
2460 fn to_tokens(&self, tokens: &mut Tokens) {
2461 self.path.to_tokens(tokens);
2462 self.brace_token.surround(tokens, |tokens| {
2463 self.fields.to_tokens(tokens);
2464 self.dot2_token.to_tokens(tokens);
2465 });
2466 }
2467 }
2468
2469 impl ToTokens for PatTupleStruct {
2470 fn to_tokens(&self, tokens: &mut Tokens) {
2471 self.path.to_tokens(tokens);
2472 self.pat.to_tokens(tokens);
2473 }
2474 }
2475
2476 impl ToTokens for PatPath {
2477 fn to_tokens(&self, tokens: &mut Tokens) {
2478 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2479 }
2480 }
2481
2482 impl ToTokens for PatTuple {
2483 fn to_tokens(&self, tokens: &mut Tokens) {
2484 self.paren_token.surround(tokens, |tokens| {
2485 for (i, token) in self.pats.iter().enumerate() {
2486 if Some(i) == self.dots_pos {
2487 self.dot2_token.to_tokens(tokens);
2488 self.comma_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002489 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002490 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002491 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002492
2493 if Some(self.pats.len()) == self.dots_pos {
2494 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002495 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002496 });
2497 }
2498 }
2499
2500 impl ToTokens for PatBox {
2501 fn to_tokens(&self, tokens: &mut Tokens) {
2502 self.box_token.to_tokens(tokens);
2503 self.pat.to_tokens(tokens);
2504 }
2505 }
2506
2507 impl ToTokens for PatRef {
2508 fn to_tokens(&self, tokens: &mut Tokens) {
2509 self.and_token.to_tokens(tokens);
2510 self.mutbl.to_tokens(tokens);
2511 self.pat.to_tokens(tokens);
2512 }
2513 }
2514
2515 impl ToTokens for PatLit {
2516 fn to_tokens(&self, tokens: &mut Tokens) {
2517 self.expr.to_tokens(tokens);
2518 }
2519 }
2520
2521 impl ToTokens for PatRange {
2522 fn to_tokens(&self, tokens: &mut Tokens) {
2523 self.lo.to_tokens(tokens);
2524 self.limits.to_tokens(tokens);
2525 self.hi.to_tokens(tokens);
2526 }
2527 }
2528
2529 impl ToTokens for PatSlice {
2530 fn to_tokens(&self, tokens: &mut Tokens) {
2531 self.bracket_token.surround(tokens, |tokens| {
2532 self.front.to_tokens(tokens);
2533 self.middle.to_tokens(tokens);
2534 self.dot2_token.to_tokens(tokens);
2535 self.comma_token.to_tokens(tokens);
2536 self.back.to_tokens(tokens);
2537 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002538 }
2539 }
2540
Arnavion1992e2f2017-04-25 01:47:46 -07002541 impl ToTokens for RangeLimits {
2542 fn to_tokens(&self, tokens: &mut Tokens) {
2543 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002544 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2545 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
Arnavion1992e2f2017-04-25 01:47:46 -07002546 }
2547 }
2548 }
2549
David Tolnay8d9e81a2016-10-03 22:36:32 -07002550 impl ToTokens for FieldPat {
2551 fn to_tokens(&self, tokens: &mut Tokens) {
2552 if !self.is_shorthand {
2553 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002554 self.colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002555 }
2556 self.pat.to_tokens(tokens);
2557 }
2558 }
2559
David Tolnayb4ad3b52016-10-01 21:58:13 -07002560 impl ToTokens for BindingMode {
2561 fn to_tokens(&self, tokens: &mut Tokens) {
2562 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002563 BindingMode::ByRef(ref t, ref m) => {
2564 t.to_tokens(tokens);
2565 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002566 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002567 BindingMode::ByValue(ref m) => {
2568 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002569 }
2570 }
2571 }
2572 }
David Tolnay42602292016-10-01 22:25:45 -07002573
David Tolnay89e05672016-10-02 14:39:42 -07002574 impl ToTokens for CaptureBy {
2575 fn to_tokens(&self, tokens: &mut Tokens) {
2576 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002577 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002578 CaptureBy::Ref => {
2579 // nothing
2580 }
David Tolnay89e05672016-10-02 14:39:42 -07002581 }
2582 }
2583 }
2584
David Tolnay42602292016-10-01 22:25:45 -07002585 impl ToTokens for Block {
2586 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002587 self.brace_token.surround(tokens, |tokens| {
2588 tokens.append_all(&self.stmts);
2589 });
David Tolnay42602292016-10-01 22:25:45 -07002590 }
2591 }
2592
David Tolnay42602292016-10-01 22:25:45 -07002593 impl ToTokens for Stmt {
2594 fn to_tokens(&self, tokens: &mut Tokens) {
2595 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002596 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002597 Stmt::Item(ref item) => item.to_tokens(tokens),
2598 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002599 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002600 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002601 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002602 }
David Tolnay13b3d352016-10-03 00:31:15 -07002603 Stmt::Mac(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002604 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002605 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07002606 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07002607 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002608 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002609 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
2610 // no semicolon
2611 }
David Tolnay13b3d352016-10-03 00:31:15 -07002612 }
2613 }
David Tolnay42602292016-10-01 22:25:45 -07002614 }
2615 }
2616 }
David Tolnay191e0582016-10-02 18:31:09 -07002617
2618 impl ToTokens for Local {
2619 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07002620 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002621 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002622 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002623 self.colon_token.to_tokens(tokens);
2624 self.ty.to_tokens(tokens);
2625 self.eq_token.to_tokens(tokens);
2626 self.init.to_tokens(tokens);
2627 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07002628 }
2629 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07002630}