blob: f8adefbd5be100757d6ff8377ebdff8e7e62a303 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay85b69a42017-12-27 20:43:10 -05003#[cfg(feature = "full")]
4use proc_macro2::Span;
5#[cfg(feature = "full")]
6use std::hash::{Hash, Hasher};
David Tolnayf4bbbd92016-09-23 14:41:55 -07007
Alex Crichton62a0a592017-05-22 13:58:53 -07008ast_struct! {
9 /// An expression.
10 pub struct Expr {
11 /// Type of the expression.
12 pub node: ExprKind,
Clar Charrd22b5702017-03-10 15:24:56 -050013
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// Attributes tagged on the expression.
15 pub attrs: Vec<Attribute>,
16 }
David Tolnay7184b132016-10-30 10:06:37 -070017}
18
19impl From<ExprKind> for Expr {
20 fn from(node: ExprKind) -> Expr {
21 Expr {
22 node: node,
23 attrs: Vec::new(),
24 }
25 }
26}
27
Alex Crichton62a0a592017-05-22 13:58:53 -070028ast_enum_of_structs! {
29 pub enum ExprKind {
30 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040031 pub Box(ExprBox #full {
Alex Crichton62a0a592017-05-22 13:58:53 -070032 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080033 pub box_token: Token![box],
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
Clar Charrd22b5702017-03-10 15:24:56 -050035
Michael Layzellb78f3b52017-06-04 19:03:03 -040036 /// E.g. 'place <- val' or `in place { val }`.
Michael Layzell734adb42017-06-07 16:58:31 -040037 pub InPlace(ExprInPlace #full {
Alex Crichton62a0a592017-05-22 13:58:53 -070038 pub place: Box<Expr>,
Michael Layzell6a5a1642017-06-04 19:35:15 -040039 pub kind: InPlaceKind,
Alex Crichton62a0a592017-05-22 13:58:53 -070040 pub value: Box<Expr>,
41 }),
Clar Charrd22b5702017-03-10 15:24:56 -050042
Alex Crichton62a0a592017-05-22 13:58:53 -070043 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040044 pub Array(ExprArray #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub exprs: Delimited<Expr, Token![,]>,
David Tolnay32954ef2017-12-26 22:43:16 -050046 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -070047 }),
Clar Charrd22b5702017-03-10 15:24:56 -050048
Alex Crichton62a0a592017-05-22 13:58:53 -070049 /// A function call.
50 pub Call(ExprCall {
51 pub func: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub args: Delimited<Expr, Token![,]>,
David Tolnay32954ef2017-12-26 22:43:16 -050053 pub paren_token: token::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -070054 }),
Clar Charrd22b5702017-03-10 15:24:56 -050055
Alex Crichton62a0a592017-05-22 13:58:53 -070056 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
57 ///
58 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080059 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070060 /// (within the angle brackets).
61 ///
Alex Crichton62a0a592017-05-22 13:58:53 -070062 /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
63 /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`.
Michael Layzell734adb42017-06-07 16:58:31 -040064 pub MethodCall(ExprMethodCall #full {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070065 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub method: Ident,
David Tolnayfd6bf5c2017-11-12 09:41:14 -080067 pub typarams: Delimited<Type, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080068 pub args: Delimited<Expr, Token![,]>,
David Tolnay32954ef2017-12-26 22:43:16 -050069 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -080070 pub dot_token: Token![.],
71 pub lt_token: Option<Token![<]>,
72 pub colon2_token: Option<Token![::]>,
73 pub gt_token: Option<Token![>]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070074 }),
Clar Charrd22b5702017-03-10 15:24:56 -050075
Alex Crichton62a0a592017-05-22 13:58:53 -070076 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050077 pub Tuple(ExprTuple #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -080078 pub args: Delimited<Expr, Token![,]>,
David Tolnay32954ef2017-12-26 22:43:16 -050079 pub paren_token: token::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -070080 }),
Clar Charrd22b5702017-03-10 15:24:56 -050081
Alex Crichton62a0a592017-05-22 13:58:53 -070082 /// A binary operation, e.g. `a + b`, `a * b`.
83 pub Binary(ExprBinary {
84 pub op: BinOp,
85 pub left: Box<Expr>,
86 pub right: Box<Expr>,
87 }),
Clar Charrd22b5702017-03-10 15:24:56 -050088
Alex Crichton62a0a592017-05-22 13:58:53 -070089 /// A unary operation, e.g. `!x`, `*x`.
90 pub Unary(ExprUnary {
91 pub op: UnOp,
92 pub expr: Box<Expr>,
93 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A literal, e.g. `1`, `"foo"`.
96 pub Lit(Lit),
Clar Charrd22b5702017-03-10 15:24:56 -050097
Alex Crichton62a0a592017-05-22 13:58:53 -070098 /// A cast, e.g. `foo as f64`.
99 pub Cast(ExprCast {
100 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800101 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800102 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500104
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 /// A type ascription, e.g. `foo: f64`.
106 pub Type(ExprType {
107 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800108 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800109 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700110 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500111
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 /// An `if` block, with an optional else block
113 ///
114 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400115 pub If(ExprIf #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700116 pub cond: Box<Expr>,
117 pub if_true: Block,
118 pub if_false: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800119 pub if_token: Token![if],
120 pub else_token: Option<Token![else]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700121 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500122
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 /// An `if let` expression with an optional else block
124 ///
125 /// E.g., `if let pat = expr { block } else { expr }`
126 ///
127 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400128 pub IfLet(ExprIfLet #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700129 pub pat: Box<Pat>,
130 pub expr: Box<Expr>,
131 pub if_true: Block,
132 pub if_false: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800133 pub if_token: Token![if],
134 pub let_token: Token![let],
135 pub eq_token: Token![=],
136 pub else_token: Option<Token![else]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700137 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500138
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 /// A while loop, with an optional label
140 ///
141 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400142 pub While(ExprWhile #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700143 pub cond: Box<Expr>,
144 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700145 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800146 pub colon_token: Option<Token![:]>,
147 pub while_token: Token![while],
Alex Crichton62a0a592017-05-22 13:58:53 -0700148 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500149
Alex Crichton62a0a592017-05-22 13:58:53 -0700150 /// A while-let loop, with an optional label.
151 ///
152 /// E.g., `'label: while let pat = expr { block }`
153 ///
154 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400155 pub WhileLet(ExprWhileLet #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700156 pub pat: Box<Pat>,
157 pub expr: Box<Expr>,
158 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700159 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800160 pub colon_token: Option<Token![:]>,
161 pub while_token: Token![while],
162 pub let_token: Token![let],
163 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700164 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500165
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 /// A for loop, with an optional label.
167 ///
168 /// E.g., `'label: for pat in expr { block }`
169 ///
170 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400171 pub ForLoop(ExprForLoop #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 pub pat: Box<Pat>,
173 pub expr: Box<Expr>,
174 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700175 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800176 pub for_token: Token![for],
177 pub colon_token: Option<Token![:]>,
178 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700179 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500180
Alex Crichton62a0a592017-05-22 13:58:53 -0700181 /// Conditionless loop with an optional label.
182 ///
183 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400184 pub Loop(ExprLoop #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700185 pub body: Block,
David Tolnay63e3dee2017-06-03 20:13:17 -0700186 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800187 pub loop_token: Token![loop],
188 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500190
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400192 pub Match(ExprMatch #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800193 pub match_token: Token![match],
David Tolnay32954ef2017-12-26 22:43:16 -0500194 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 pub expr: Box<Expr>,
196 pub arms: Vec<Arm>,
197 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500198
Alex Crichton62a0a592017-05-22 13:58:53 -0700199 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400200 pub Closure(ExprClosure #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700201 pub capture: CaptureBy,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800202 pub or1_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500203 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800204 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500205 pub output: ReturnType,
206 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500208
Nika Layzell640832a2017-12-04 13:37:09 -0500209 /// An unsafe block (`unsafe { ... }`)
210 pub Unsafe(ExprUnsafe #full {
211 pub unsafe_token: Token![unsafe],
212 pub block: Block,
213 }),
214
215 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400216 pub Block(ExprBlock #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700217 pub block: Block,
218 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700219
Alex Crichton62a0a592017-05-22 13:58:53 -0700220 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400221 pub Assign(ExprAssign #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700222 pub left: Box<Expr>,
223 pub right: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800224 pub eq_token: Token![=],
Alex Crichton62a0a592017-05-22 13:58:53 -0700225 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500226
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 /// An assignment with an operator
228 ///
229 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400230 pub AssignOp(ExprAssignOp #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700231 pub op: BinOp,
232 pub left: Box<Expr>,
233 pub right: Box<Expr>,
234 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500235
David Tolnay85b69a42017-12-27 20:43:10 -0500236 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
237 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400238 pub Field(ExprField #full {
David Tolnay85b69a42017-12-27 20:43:10 -0500239 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800240 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500241 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500243
Alex Crichton62a0a592017-05-22 13:58:53 -0700244 /// An indexing operation (`foo[2]`)
245 pub Index(ExprIndex {
246 pub expr: Box<Expr>,
247 pub index: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500248 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500250
David Tolnaybe55d7b2017-12-17 23:41:20 -0800251 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400252 pub Range(ExprRange #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 pub from: Option<Box<Expr>>,
254 pub to: Option<Box<Expr>>,
255 pub limits: RangeLimits,
256 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700257
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 /// Variable reference, possibly containing `::` and/or type
259 /// parameters, e.g. foo::bar::<baz>.
260 ///
261 /// Optionally "qualified",
262 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
263 pub Path(ExprPath {
264 pub qself: Option<QSelf>,
265 pub path: Path,
266 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700267
Alex Crichton62a0a592017-05-22 13:58:53 -0700268 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400269 pub AddrOf(ExprAddrOf #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800270 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700271 pub mutbl: Mutability,
272 pub expr: Box<Expr>,
273 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500274
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400276 pub Break(ExprBreak #full {
David Tolnay63e3dee2017-06-03 20:13:17 -0700277 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700278 pub expr: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800279 pub break_token: Token![break],
Alex Crichton62a0a592017-05-22 13:58:53 -0700280 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500281
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400283 pub Continue(ExprContinue #full {
David Tolnay63e3dee2017-06-03 20:13:17 -0700284 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800285 pub continue_token: Token![continue],
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500287
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 /// A `return`, with an optional value to be returned
Michael Layzell734adb42017-06-07 16:58:31 -0400289 pub Ret(ExprRet #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 pub expr: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800291 pub return_token: Token![return],
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700293
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 /// A macro invocation; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800295 pub Macro(Macro),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700296
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 /// A struct literal expression.
298 ///
299 /// For example, `Foo {x: 1, y: 2}`, or
300 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400301 pub Struct(ExprStruct #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700302 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800303 pub fields: Delimited<FieldValue, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700304 pub rest: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800305 pub dot2_token: Option<Token![..]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500306 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700308
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 /// An array literal constructed from one repeated element.
310 ///
311 /// For example, `[1; 5]`. The first expression is the element
312 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400313 pub Repeat(ExprRepeat #full {
David Tolnay32954ef2017-12-26 22:43:16 -0500314 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800315 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700316 pub expr: Box<Expr>,
317 pub amt: Box<Expr>,
318 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700319
Alex Crichton62a0a592017-05-22 13:58:53 -0700320 /// No-op: used solely so we can pretty-print faithfully
321 pub Paren(ExprParen {
322 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500323 pub paren_token: token::Paren,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700325
Michael Layzell93c36282017-06-04 20:43:14 -0400326 /// No-op: used solely so we can pretty-print faithfully
327 ///
328 /// A `group` represents a `None`-delimited span in the input
329 /// `TokenStream` which affects the precidence of the resulting
330 /// expression. They are used for macro hygiene.
331 pub Group(ExprGroup {
332 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500333 pub group_token: token::Group,
Michael Layzell93c36282017-06-04 20:43:14 -0400334 }),
335
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400337 pub Try(ExprTry #full {
Alex Crichton62a0a592017-05-22 13:58:53 -0700338 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800339 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700341
Alex Crichton62a0a592017-05-22 13:58:53 -0700342 /// A catch expression.
343 ///
344 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400345 pub Catch(ExprCatch #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800346 pub do_token: Token![do],
347 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 pub block: Block,
349 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700350
351 /// A yield expression.
352 ///
353 /// E.g. `yield expr`
354 pub Yield(ExprYield #full {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800355 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700356 pub expr: Option<Box<Expr>>,
357 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700358 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700359}
360
Michael Layzell734adb42017-06-07 16:58:31 -0400361#[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500362ast_enum! {
363 /// A struct or tuple struct field accessed in a struct literal or field
364 /// expression.
365 pub enum Member {
366 /// A named field like `self.x`.
367 Named(Ident),
368 /// An unnamed field like `self.0`.
369 Unnamed(Index),
370 }
371}
372
373#[cfg(feature = "full")]
374ast_struct! {
375 /// The index of an unnamed tuple struct field.
376 pub struct Index #manual_extra_traits {
377 pub index: u32,
378 pub span: Span,
379 }
380}
381
382#[cfg(feature = "full")]
383impl Eq for Index {}
384
385#[cfg(feature = "full")]
386impl PartialEq for Index {
387 fn eq(&self, other: &Self) -> bool {
388 self.index == other.index
389 }
390}
391
392#[cfg(feature = "full")]
393impl Hash for Index {
394 fn hash<H: Hasher>(&self, state: &mut H) {
395 self.index.hash(state);
396 }
397}
398
399#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700400ast_struct! {
401 /// A field-value pair in a struct literal.
402 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500403 /// Attributes tagged on the field.
404 pub attrs: Vec<Attribute>,
405
406 /// Name or index of the field.
407 pub member: Member,
408
409 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500410
Alex Crichton62a0a592017-05-22 13:58:53 -0700411 /// Value of the field.
412 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500413
Alex Crichton62a0a592017-05-22 13:58:53 -0700414 /// Whether this is a shorthand field, e.g. `Struct { x }`
415 /// instead of `Struct { x: x }`.
416 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700417 }
David Tolnay055a7042016-10-02 19:23:54 -0700418}
419
Michael Layzell734adb42017-06-07 16:58:31 -0400420#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700421ast_struct! {
422 /// A Block (`{ .. }`).
423 ///
424 /// E.g. `{ .. }` as in `fn foo() { .. }`
425 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500426 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 /// Statements in a block
428 pub stmts: Vec<Stmt>,
429 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700430}
431
Michael Layzell734adb42017-06-07 16:58:31 -0400432#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700433ast_enum! {
434 /// A statement, usually ending in a semicolon.
435 pub enum Stmt {
436 /// A local (let) binding.
437 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700438
Alex Crichton62a0a592017-05-22 13:58:53 -0700439 /// An item definition.
440 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700441
Alex Crichton62a0a592017-05-22 13:58:53 -0700442 /// Expr without trailing semicolon.
443 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700444
Alex Crichton62a0a592017-05-22 13:58:53 -0700445 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800446 Semi(Box<Expr>, Token![;]),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700447
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 /// Macro invocation.
David Tolnaydecf28d2017-11-11 11:56:45 -0800449 Macro(Box<(Macro, MacStmtStyle, Vec<Attribute>)>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700450 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700451}
452
Michael Layzell734adb42017-06-07 16:58:31 -0400453#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700454ast_enum! {
455 /// How a macro was invoked.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700456 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700457 pub enum MacStmtStyle {
458 /// The macro statement had a trailing semicolon, e.g. `foo! { ... };`
459 /// `foo!(...);`, `foo![...];`
David Tolnayf8db7ba2017-11-11 22:52:16 -0800460 Semicolon(Token![;]),
Clar Charrd22b5702017-03-10 15:24:56 -0500461
Alex Crichton62a0a592017-05-22 13:58:53 -0700462 /// The macro statement had braces; e.g. foo! { ... }
463 Braces,
Clar Charrd22b5702017-03-10 15:24:56 -0500464
Alex Crichton62a0a592017-05-22 13:58:53 -0700465 /// The macro statement had parentheses or brackets and no semicolon; e.g.
466 /// `foo!(...)`. All of these will end up being converted into macro
467 /// expressions.
468 NoBraces,
469 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700470}
471
Michael Layzell734adb42017-06-07 16:58:31 -0400472#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700473ast_struct! {
474 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
475 pub struct Local {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800476 pub let_token: Token![let],
477 pub colon_token: Option<Token![:]>,
478 pub eq_token: Option<Token![=]>,
479 pub semi_token: Token![;],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700480
Alex Crichton62a0a592017-05-22 13:58:53 -0700481 pub pat: Box<Pat>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800482 pub ty: Option<Box<Type>>,
Clar Charrd22b5702017-03-10 15:24:56 -0500483
Alex Crichton62a0a592017-05-22 13:58:53 -0700484 /// Initializer expression to set the value, if any
485 pub init: Option<Box<Expr>>,
486 pub attrs: Vec<Attribute>,
487 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700488}
489
Michael Layzell734adb42017-06-07 16:58:31 -0400490#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700491ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700492 // Clippy false positive
493 // https://github.com/Manishearth/rust-clippy/issues/1241
494 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
495 pub enum Pat {
496 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700497 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800498 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700499 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700500
Alex Crichton62a0a592017-05-22 13:58:53 -0700501 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
502 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
503 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
504 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700505 pub Ident(PatIdent {
506 pub mode: BindingMode,
507 pub ident: Ident,
508 pub subpat: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800509 pub at_token: Option<Token![@]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700510 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700511
Alex Crichton62a0a592017-05-22 13:58:53 -0700512 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
513 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700514 pub Struct(PatStruct {
515 pub path: Path,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500517 pub brace_token: token::Brace,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800518 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700519 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700520
Alex Crichton62a0a592017-05-22 13:58:53 -0700521 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
522 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
523 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700524 pub TupleStruct(PatTupleStruct {
525 pub path: Path,
526 pub pat: PatTuple,
527 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700528
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 /// A possibly qualified path pattern.
530 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
531 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
532 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700533 pub Path(PatPath {
534 pub qself: Option<QSelf>,
535 pub path: Path,
536 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700537
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 /// A tuple pattern `(a, b)`.
539 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
540 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700541 pub Tuple(PatTuple {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800542 pub pats: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700543 pub dots_pos: Option<usize>,
David Tolnay32954ef2017-12-26 22:43:16 -0500544 pub paren_token: token::Paren,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800545 pub dot2_token: Option<Token![..]>,
546 pub comma_token: Option<Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700547 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700548 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700549 pub Box(PatBox {
550 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800551 pub box_token: Token![box],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700552 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700553 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700554 pub Ref(PatRef {
555 pub pat: Box<Pat>,
556 pub mutbl: Mutability,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800557 pub and_token: Token![&],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700558 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700560 pub Lit(PatLit {
561 pub expr: Box<Expr>,
562 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800563 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700564 pub Range(PatRange {
565 pub lo: Box<Expr>,
566 pub hi: Box<Expr>,
567 pub limits: RangeLimits,
568 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400569 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700570 pub Slice(PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800571 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700572 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800573 pub back: Delimited<Pat, Token![,]>,
574 pub dot2_token: Option<Token![..]>,
575 pub comma_token: Option<Token![,]>,
David Tolnay32954ef2017-12-26 22:43:16 -0500576 pub bracket_token: token::Bracket,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700577 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700578 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800579 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700580 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700581}
582
Michael Layzell734adb42017-06-07 16:58:31 -0400583#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700584ast_struct! {
585 /// An arm of a 'match'.
586 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800587 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500589 /// ```rust
590 /// # #![feature(dotdoteq_in_patterns)]
591 /// #
592 /// # fn main() {
593 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700594 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500595 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700596 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500597 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700598 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500599 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700600 /// ```
601 pub struct Arm {
602 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800603 pub pats: Delimited<Pat, Token![|]>,
604 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700605 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700607 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800608 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700609 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700610}
611
Michael Layzell734adb42017-06-07 16:58:31 -0400612#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700613ast_enum! {
614 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700615 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700616 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700618 Ref,
619 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700620}
621
Michael Layzell734adb42017-06-07 16:58:31 -0400622#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700623ast_enum! {
624 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700625 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 pub enum RangeLimits {
627 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800628 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700629 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800630 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700631 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700632}
633
Michael Layzell734adb42017-06-07 16:58:31 -0400634#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700635ast_struct! {
636 /// A single field in a struct pattern
637 ///
638 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
639 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
640 /// except `is_shorthand` is true
641 pub struct FieldPat {
642 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500643 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 /// The pattern the field is destructured to
645 pub pat: Box<Pat>,
646 pub is_shorthand: bool,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800647 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700648 pub attrs: Vec<Attribute>,
649 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700650}
651
Michael Layzell734adb42017-06-07 16:58:31 -0400652#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700653ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700654 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700655 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800656 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700657 ByValue(Mutability),
658 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700659}
660
Michael Layzell734adb42017-06-07 16:58:31 -0400661#[cfg(feature = "full")]
Michael Layzell6a5a1642017-06-04 19:35:15 -0400662ast_enum! {
663 #[cfg_attr(feature = "clone-impls", derive(Copy))]
664 pub enum InPlaceKind {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800665 Arrow(Token![<-]),
666 In(Token![in]),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400667 }
668}
669
Michael Layzell3936ceb2017-07-08 00:28:36 -0400670#[cfg(any(feature = "parsing", feature = "printing"))]
671#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700672fn arm_expr_requires_comma(expr: &Expr) -> bool {
673 // see https://github.com/rust-lang/rust/blob/eb8f2586e
674 // /src/libsyntax/parse/classify.rs#L17-L37
675 match expr.node {
David Tolnay51382052017-12-27 13:46:21 -0500676 ExprKind::Unsafe(..)
677 | ExprKind::Block(..)
678 | ExprKind::If(..)
679 | ExprKind::IfLet(..)
680 | ExprKind::Match(..)
681 | ExprKind::While(..)
682 | ExprKind::WhileLet(..)
683 | ExprKind::Loop(..)
684 | ExprKind::ForLoop(..)
685 | ExprKind::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700686 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400687 }
688}
689
David Tolnayb9c8e322016-09-23 20:48:37 -0700690#[cfg(feature = "parsing")]
691pub mod parsing {
692 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700693 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700694
Michael Layzell734adb42017-06-07 16:58:31 -0400695 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500696 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500697 use synom::Synom;
698 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400699 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500700 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500701 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700702
David Tolnaybcf26022017-12-25 22:10:52 -0500703 // When we're parsing expressions which occur before blocks, like in an if
704 // statement's condition, we cannot parse a struct literal.
705 //
706 // Struct literals are ambiguous in certain positions
707 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700708 macro_rules! ambiguous_expr {
709 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700710 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700711 };
712 }
713
David Tolnaybcf26022017-12-25 22:10:52 -0500714 // When we are parsing an optional suffix expression, we cannot allow blocks
715 // if structs are not allowed.
716 //
717 // Example:
718 //
719 // if break {} {}
720 //
721 // is ambiguous between:
722 //
723 // if (break {}) {}
724 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400725 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400726 macro_rules! opt_ambiguous_expr {
727 ($i:expr, $allow_struct:ident) => {
728 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
729 };
730 }
731
Alex Crichton954046c2017-05-30 21:49:42 -0700732 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400733 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700734
735 fn description() -> Option<&'static str> {
736 Some("expression")
737 }
738 }
739
Michael Layzell734adb42017-06-07 16:58:31 -0400740 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700741 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
742
David Tolnaybcf26022017-12-25 22:10:52 -0500743 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400744 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500745 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
Michael Layzellb78f3b52017-06-04 19:03:03 -0400746 map!(
David Tolnay54e854d2016-10-24 12:03:30 -0700747 i,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400748 call!(assign_expr, allow_struct, allow_block),
749 ExprKind::into
750 )
751 }
752
Michael Layzell734adb42017-06-07 16:58:31 -0400753 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500754 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
Michael Layzell734adb42017-06-07 16:58:31 -0400755 map!(
756 i,
757 // NOTE: We intentionally skip assign_expr, placement_expr, and
758 // range_expr, as they are not parsed in non-full mode.
759 call!(or_expr, allow_struct, allow_block),
760 ExprKind::into
761 )
762 }
763
David Tolnaybcf26022017-12-25 22:10:52 -0500764 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400765 macro_rules! binop {
766 (
767 $name: ident,
768 $next: ident,
769 $submac: ident!( $($args:tt)* )
770 ) => {
771 named!($name(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
772 mut e: call!($next, allow_struct, allow_block) >>
773 many0!(do_parse!(
774 op: $submac!($($args)*) >>
775 rhs: call!($next, allow_struct, true) >>
776 ({
777 e = ExprBinary {
778 left: Box::new(e.into()),
779 op: op,
780 right: Box::new(rhs.into()),
781 }.into();
782 })
783 )) >>
784 (e)
785 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700786 }
David Tolnay54e854d2016-10-24 12:03:30 -0700787 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700788
David Tolnaybcf26022017-12-25 22:10:52 -0500789 // <placement> = <placement> ..
790 // <placement> += <placement> ..
791 // <placement> -= <placement> ..
792 // <placement> *= <placement> ..
793 // <placement> /= <placement> ..
794 // <placement> %= <placement> ..
795 // <placement> ^= <placement> ..
796 // <placement> &= <placement> ..
797 // <placement> |= <placement> ..
798 // <placement> <<= <placement> ..
799 // <placement> >>= <placement> ..
800 //
801 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400802 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400803 named!(assign_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
804 mut e: call!(placement_expr, allow_struct, allow_block) >>
805 alt!(
806 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800807 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400808 // Recurse into self to parse right-associative operator.
809 rhs: call!(assign_expr, allow_struct, true) >>
810 ({
811 e = ExprAssign {
812 left: Box::new(e.into()),
813 eq_token: eq,
814 right: Box::new(rhs.into()),
815 }.into();
816 })
817 )
818 |
819 do_parse!(
820 op: call!(BinOp::parse_assign_op) >>
821 // Recurse into self to parse right-associative operator.
822 rhs: call!(assign_expr, allow_struct, true) >>
823 ({
824 e = ExprAssignOp {
825 left: Box::new(e.into()),
826 op: op,
827 right: Box::new(rhs.into()),
828 }.into();
829 })
830 )
831 |
832 epsilon!()
833 ) >>
834 (e)
835 ));
836
David Tolnaybcf26022017-12-25 22:10:52 -0500837 // <range> <- <range> ..
838 //
839 // NOTE: The `in place { expr }` version of this syntax is parsed in
840 // `atom_expr`, not here.
841 //
842 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400843 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400844 named!(placement_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
845 mut e: call!(range_expr, allow_struct, allow_block) >>
846 alt!(
847 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800848 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400849 // Recurse into self to parse right-associative operator.
850 rhs: call!(placement_expr, allow_struct, true) >>
851 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400852 e = ExprInPlace {
853 // op: BinOp::Place(larrow),
854 place: Box::new(e.into()),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400855 kind: InPlaceKind::Arrow(arrow),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400856 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400857 }.into();
858 })
859 )
860 |
861 epsilon!()
862 ) >>
863 (e)
864 ));
865
David Tolnaybcf26022017-12-25 22:10:52 -0500866 // <or> ... <or> ..
867 // <or> .. <or> ..
868 // <or> ..
869 //
870 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
871 // rules are for parsing these expressions are, but this is not correct.
872 // For example, `a .. b .. c` is not a legal expression. It should not
873 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
874 //
875 // NOTE: The form of ranges which don't include a preceding expression are
876 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400877 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400878 named!(range_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
879 mut e: call!(or_expr, allow_struct, allow_block) >>
880 many0!(do_parse!(
881 limits: syn!(RangeLimits) >>
882 // We don't want to allow blocks here if we don't allow structs. See
883 // the reasoning for `opt_ambiguous_expr!` above.
884 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
885 ({
886 e = ExprRange {
887 from: Some(Box::new(e.into())),
888 limits: limits,
889 to: hi.map(|e| Box::new(e.into())),
890 }.into();
891 })
892 )) >>
893 (e)
894 ));
895
David Tolnaybcf26022017-12-25 22:10:52 -0500896 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800897 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400898
David Tolnaybcf26022017-12-25 22:10:52 -0500899 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800900 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400901
David Tolnaybcf26022017-12-25 22:10:52 -0500902 // <bitor> == <bitor> ...
903 // <bitor> != <bitor> ...
904 // <bitor> >= <bitor> ...
905 // <bitor> <= <bitor> ...
906 // <bitor> > <bitor> ...
907 // <bitor> < <bitor> ...
908 //
909 // NOTE: This operator appears to be parsed as left-associative, but errors
910 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500911 binop!(
912 compare_expr,
913 bitor_expr,
914 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800915 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400916 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800917 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400918 |
919 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800920 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400921 |
922 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800923 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400924 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400925 do_parse!(
926 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800927 not!(punct!(<-)) >>
928 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400929 (BinOp::Lt(t))
930 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400931 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800932 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500933 )
934 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400935
David Tolnaybcf26022017-12-25 22:10:52 -0500936 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500937 binop!(
938 bitor_expr,
939 bitxor_expr,
940 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
941 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400942
David Tolnaybcf26022017-12-25 22:10:52 -0500943 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500944 binop!(
945 bitxor_expr,
946 bitand_expr,
947 do_parse!(
948 // NOTE: Make sure we aren't looking at ^=.
949 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
950 )
951 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952
David Tolnaybcf26022017-12-25 22:10:52 -0500953 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500954 binop!(
955 bitand_expr,
956 shift_expr,
957 do_parse!(
958 // NOTE: Make sure we aren't looking at && or &=.
959 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
960 )
961 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400962
David Tolnaybcf26022017-12-25 22:10:52 -0500963 // <arith> << <arith> ...
964 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -0500965 binop!(
966 shift_expr,
967 arith_expr,
968 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800969 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400970 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800971 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -0500972 )
973 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400974
David Tolnaybcf26022017-12-25 22:10:52 -0500975 // <term> + <term> ...
976 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -0500977 binop!(
978 arith_expr,
979 term_expr,
980 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800981 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400982 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800983 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -0500984 )
985 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400986
David Tolnaybcf26022017-12-25 22:10:52 -0500987 // <cast> * <cast> ...
988 // <cast> / <cast> ...
989 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -0500990 binop!(
991 term_expr,
992 cast_expr,
993 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800994 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400995 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800996 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400997 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800998 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -0500999 )
1000 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001001
David Tolnaybcf26022017-12-25 22:10:52 -05001002 // <unary> as <ty>
1003 // <unary> : <ty>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004 named!(cast_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
1005 mut e: call!(unary_expr, allow_struct, allow_block) >>
1006 many0!(alt!(
1007 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001008 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001009 // We can't accept `A + B` in cast expressions, as it's
1010 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001011 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001012 ({
1013 e = ExprCast {
1014 expr: Box::new(e.into()),
1015 as_token: as_,
1016 ty: Box::new(ty),
1017 }.into();
1018 })
1019 )
1020 |
1021 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001023 // We can't accept `A + B` in cast expressions, as it's
1024 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001025 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001026 ({
1027 e = ExprType {
1028 expr: Box::new(e.into()),
1029 colon_token: colon,
1030 ty: Box::new(ty),
1031 }.into();
1032 })
1033 )
1034 )) >>
1035 (e)
1036 ));
1037
David Tolnaybcf26022017-12-25 22:10:52 -05001038 // <UnOp> <trailer>
1039 // & <trailer>
1040 // &mut <trailer>
1041 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001042 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001043 named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
1044 do_parse!(
1045 op: syn!(UnOp) >>
1046 expr: call!(unary_expr, allow_struct, true) >>
1047 (ExprUnary {
1048 op: op,
1049 expr: Box::new(expr.into()),
1050 }.into())
1051 )
1052 |
1053 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001054 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001055 mutability: syn!(Mutability) >>
1056 expr: call!(unary_expr, allow_struct, true) >>
1057 (ExprAddrOf {
1058 and_token: and,
1059 mutbl: mutability,
1060 expr: Box::new(expr.into()),
1061 }.into())
1062 )
1063 |
1064 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001065 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066 expr: call!(unary_expr, allow_struct, true) >>
1067 (ExprBox {
1068 box_token: box_,
1069 expr: Box::new(expr.into()),
1070 }.into())
1071 )
1072 |
1073 call!(trailer_expr, allow_struct, allow_block)
1074 ));
1075
Michael Layzell734adb42017-06-07 16:58:31 -04001076 // XXX: This duplication is ugly
1077 #[cfg(not(feature = "full"))]
1078 named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
1079 do_parse!(
1080 op: syn!(UnOp) >>
1081 expr: call!(unary_expr, allow_struct, true) >>
1082 (ExprUnary {
1083 op: op,
1084 expr: Box::new(expr.into()),
1085 }.into())
1086 )
1087 |
1088 call!(trailer_expr, allow_struct, allow_block)
1089 ));
1090
David Tolnaybcf26022017-12-25 22:10:52 -05001091 // <atom> (..<args>) ...
1092 // <atom> . <ident> (..<args>) ...
1093 // <atom> . <ident> ...
1094 // <atom> . <lit> ...
1095 // <atom> [ <expr> ] ...
1096 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001097 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001098 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
1099 mut e: call!(atom_expr, allow_struct, allow_block) >>
1100 many0!(alt!(
1101 tap!(args: and_call => {
1102 let (args, paren) = args;
1103 e = ExprCall {
1104 func: Box::new(e.into()),
1105 args: args,
1106 paren_token: paren,
1107 }.into();
1108 })
1109 |
1110 tap!(more: and_method_call => {
1111 let mut call = more;
1112 call.expr = Box::new(e.into());
1113 e = call.into();
1114 })
1115 |
1116 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001117 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001118 e = ExprField {
David Tolnay85b69a42017-12-27 20:43:10 -05001119 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001120 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001121 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001122 }.into();
1123 })
1124 |
1125 tap!(i: and_index => {
1126 let (i, token) = i;
1127 e = ExprIndex {
1128 expr: Box::new(e.into()),
1129 bracket_token: token,
1130 index: Box::new(i),
1131 }.into();
1132 })
1133 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001134 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001135 e = ExprTry {
1136 expr: Box::new(e.into()),
1137 question_token: question,
1138 }.into();
1139 })
1140 )) >>
1141 (e)
1142 ));
1143
Michael Layzell734adb42017-06-07 16:58:31 -04001144 // XXX: Duplication == ugly
1145 #[cfg(not(feature = "full"))]
1146 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!(
1147 mut e: call!(atom_expr, allow_struct, allow_block) >>
1148 many0!(alt!(
1149 tap!(args: and_call => {
1150 let (args, paren) = args;
1151 e = ExprCall {
1152 func: Box::new(e.into()),
1153 args: args,
1154 paren_token: paren,
1155 }.into();
1156 })
1157 |
1158 tap!(i: and_index => {
1159 let (i, token) = i;
1160 e = ExprIndex {
1161 expr: Box::new(e.into()),
1162 bracket_token: token,
1163 index: Box::new(i),
1164 }.into();
1165 })
1166 )) >>
1167 (e)
1168 ));
1169
David Tolnaybcf26022017-12-25 22:10:52 -05001170 // Parse all atomic expressions which don't have to worry about precidence
1171 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001172 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 named!(atom_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!(
Michael Layzell93c36282017-06-04 20:43:14 -04001174 syn!(ExprGroup) => { ExprKind::Group } // must be placed first
1175 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001176 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
1177 |
1178 // must be before expr_path
1179 cond_reduce!(allow_struct, map!(syn!(ExprStruct), ExprKind::Struct))
1180 |
1181 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
1182 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001183 syn!(Macro) => { ExprKind::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 |
1185 call!(expr_break, allow_struct) // must be before expr_path
1186 |
1187 syn!(ExprContinue) => { ExprKind::Continue } // must be before expr_path
1188 |
1189 call!(expr_ret, allow_struct) // must be before expr_path
1190 |
1191 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
1192 syn!(ExprInPlace) => { ExprKind::InPlace }
1193 |
1194 syn!(ExprArray) => { ExprKind::Array }
1195 |
David Tolnay05362582017-12-26 01:33:57 -05001196 syn!(ExprTuple) => { ExprKind::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001197 |
1198 syn!(ExprIf) => { ExprKind::If }
1199 |
1200 syn!(ExprIfLet) => { ExprKind::IfLet }
1201 |
1202 syn!(ExprWhile) => { ExprKind::While }
1203 |
1204 syn!(ExprWhileLet) => { ExprKind::WhileLet }
1205 |
1206 syn!(ExprForLoop) => { ExprKind::ForLoop }
1207 |
1208 syn!(ExprLoop) => { ExprKind::Loop }
1209 |
1210 syn!(ExprMatch) => { ExprKind::Match }
1211 |
1212 syn!(ExprCatch) => { ExprKind::Catch }
1213 |
Alex Crichtonfe110462017-06-01 12:49:27 -07001214 syn!(ExprYield) => { ExprKind::Yield }
1215 |
Nika Layzell640832a2017-12-04 13:37:09 -05001216 syn!(ExprUnsafe) => { ExprKind::Unsafe }
1217 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001218 call!(expr_closure, allow_struct)
1219 |
1220 cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
1221 |
1222 // NOTE: This is the prefix-form of range
1223 call!(expr_range, allow_struct)
1224 |
1225 syn!(ExprPath) => { ExprKind::Path }
1226 |
1227 syn!(ExprRepeat) => { ExprKind::Repeat }
1228 ));
1229
Michael Layzell734adb42017-06-07 16:58:31 -04001230 #[cfg(not(feature = "full"))]
1231 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> ExprKind, alt!(
1232 syn!(ExprGroup) => { ExprKind::Group } // must be placed first
1233 |
1234 syn!(Lit) => { ExprKind::Lit } // must be before expr_struct
1235 |
1236 syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup
1237 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001238 syn!(Macro) => { ExprKind::Macro } // must be before expr_path
Michael Layzell734adb42017-06-07 16:58:31 -04001239 |
1240 syn!(ExprPath) => { ExprKind::Path }
1241 ));
1242
Michael Layzell734adb42017-06-07 16:58:31 -04001243 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001244 named!(expr_nosemi -> Expr, map!(alt!(
1245 syn!(ExprIf) => { ExprKind::If }
1246 |
1247 syn!(ExprIfLet) => { ExprKind::IfLet }
1248 |
1249 syn!(ExprWhile) => { ExprKind::While }
1250 |
1251 syn!(ExprWhileLet) => { ExprKind::WhileLet }
1252 |
1253 syn!(ExprForLoop) => { ExprKind::ForLoop }
1254 |
1255 syn!(ExprLoop) => { ExprKind::Loop }
1256 |
1257 syn!(ExprMatch) => { ExprKind::Match }
1258 |
1259 syn!(ExprCatch) => { ExprKind::Catch }
1260 |
Alex Crichtonfe110462017-06-01 12:49:27 -07001261 syn!(ExprYield) => { ExprKind::Yield }
1262 |
Nika Layzell640832a2017-12-04 13:37:09 -05001263 syn!(ExprUnsafe) => { ExprKind::Unsafe }
1264 |
Michael Layzell35418782017-06-07 09:20:25 -04001265 syn!(ExprBlock) => { ExprKind::Block }
1266 ), Expr::from));
1267
Michael Layzell93c36282017-06-04 20:43:14 -04001268 impl Synom for ExprGroup {
1269 named!(parse -> Self, do_parse!(
1270 e: grouped!(syn!(Expr)) >>
1271 (ExprGroup {
1272 expr: Box::new(e.0),
1273 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001274 })
Michael Layzell93c36282017-06-04 20:43:14 -04001275 ));
1276 }
1277
Alex Crichton954046c2017-05-30 21:49:42 -07001278 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001279 named!(parse -> Self, do_parse!(
1280 e: parens!(syn!(Expr)) >>
1281 (ExprParen {
1282 expr: Box::new(e.0),
1283 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001284 })
Michael Layzell92639a52017-06-01 00:07:44 -04001285 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001286 }
David Tolnay89e05672016-10-02 14:39:42 -07001287
Michael Layzell734adb42017-06-07 16:58:31 -04001288 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001289 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001290 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001291 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001292 place: expr_no_struct >>
1293 value: braces!(call!(Block::parse_within)) >>
1294 (ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001295 place: Box::new(place),
Michael Layzell6a5a1642017-06-04 19:35:15 -04001296 kind: InPlaceKind::In(in_),
Michael Layzell92639a52017-06-01 00:07:44 -04001297 value: Box::new(Expr {
1298 node: ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001299 block: Block {
1300 stmts: value.0,
1301 brace_token: value.1,
1302 },
1303 }.into(),
1304 attrs: Vec::new(),
1305 }),
1306 })
1307 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001308 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001309
Michael Layzell734adb42017-06-07 16:58:31 -04001310 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001311 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001312 named!(parse -> Self, do_parse!(
1313 elems: brackets!(call!(Delimited::parse_terminated)) >>
1314 (ExprArray {
1315 exprs: elems.0,
1316 bracket_token: elems.1,
1317 })
1318 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001319 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001320
David Tolnay32954ef2017-12-26 22:43:16 -05001321 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001322 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001323
Michael Layzell734adb42017-06-07 16:58:31 -04001324 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001325 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001326 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001327 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001328 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001329 colon2: punct!(::) >>
1330 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001331 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001332 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001333 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001334 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001335 args: parens!(call!(Delimited::parse_terminated)) >>
1336 ({
1337 let (colon2, lt, tys, gt) = match typarams {
1338 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1339 None => (None, None, None, None),
1340 };
1341 ExprMethodCall {
1342 // this expr will get overwritten after being returned
1343 expr: Box::new(ExprKind::Lit(Lit {
1344 span: Span::default(),
1345 value: LitKind::Bool(false),
1346 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001347
Alex Crichton954046c2017-05-30 21:49:42 -07001348 method: method,
1349 args: args.0,
1350 paren_token: args.1,
1351 dot_token: dot,
1352 lt_token: lt,
1353 gt_token: gt,
1354 colon2_token: colon2,
1355 typarams: tys.unwrap_or_default(),
1356 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001357 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001358 ));
1359
Michael Layzell734adb42017-06-07 16:58:31 -04001360 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001361 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001362 named!(parse -> Self, do_parse!(
1363 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001364 (ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001365 args: elems.0,
1366 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001367 })
1368 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001369 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001370
Michael Layzell734adb42017-06-07 16:58:31 -04001371 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001372 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001373 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001374 if_: keyword!(if) >>
1375 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001376 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001377 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001378 cond: expr_no_struct >>
1379 then_block: braces!(call!(Block::parse_within)) >>
1380 else_block: option!(else_block) >>
1381 (ExprIfLet {
1382 pat: Box::new(pat),
1383 let_token: let_,
1384 eq_token: eq,
1385 expr: Box::new(cond),
1386 if_true: Block {
1387 stmts: then_block.0,
1388 brace_token: then_block.1,
1389 },
1390 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001391 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001392 if_false: else_block.map(|p| Box::new(p.1.into())),
1393 })
1394 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001395 }
1396
Michael Layzell734adb42017-06-07 16:58:31 -04001397 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001398 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001399 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001400 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001401 cond: expr_no_struct >>
1402 then_block: braces!(call!(Block::parse_within)) >>
1403 else_block: option!(else_block) >>
1404 (ExprIf {
1405 cond: Box::new(cond),
1406 if_true: Block {
1407 stmts: then_block.0,
1408 brace_token: then_block.1,
1409 },
1410 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001411 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001412 if_false: else_block.map(|p| Box::new(p.1.into())),
1413 })
1414 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001415 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001416
Michael Layzell734adb42017-06-07 16:58:31 -04001417 #[cfg(feature = "full")]
David Tolnayf8db7ba2017-11-11 22:52:16 -08001418 named!(else_block -> (Token![else], ExprKind), do_parse!(
1419 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001420 expr: alt!(
1421 syn!(ExprIf) => { ExprKind::If }
1422 |
1423 syn!(ExprIfLet) => { ExprKind::IfLet }
1424 |
1425 do_parse!(
1426 else_block: braces!(call!(Block::parse_within)) >>
1427 (ExprKind::Block(ExprBlock {
Alex Crichton954046c2017-05-30 21:49:42 -07001428 block: Block {
1429 stmts: else_block.0,
1430 brace_token: else_block.1,
1431 },
1432 }))
David Tolnay939766a2016-09-23 23:48:12 -07001433 )
Alex Crichton954046c2017-05-30 21:49:42 -07001434 ) >>
1435 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001436 ));
1437
Michael Layzell734adb42017-06-07 16:58:31 -04001438 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001439 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001440 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001441 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1442 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001443 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001444 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001445 expr: expr_no_struct >>
1446 loop_block: syn!(Block) >>
1447 (ExprForLoop {
1448 for_token: for_,
1449 in_token: in_,
1450 pat: Box::new(pat),
1451 expr: Box::new(expr),
1452 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001453 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001454 label: lbl.map(|p| p.0),
1455 })
1456 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001457 }
Gregory Katze5f35682016-09-27 14:20:55 -04001458
Michael Layzell734adb42017-06-07 16:58:31 -04001459 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001460 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001461 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001462 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1463 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001464 loop_block: syn!(Block) >>
1465 (ExprLoop {
1466 loop_token: loop_,
1467 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001468 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001469 label: lbl.map(|p| p.0),
1470 })
1471 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001472 }
1473
Michael Layzell734adb42017-06-07 16:58:31 -04001474 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001475 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001476 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001477 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001478 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001479 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001480 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001481 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001482 ExprMatch {
1483 expr: Box::new(obj),
1484 match_token: match_,
1485 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001486 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001487 }
1488 })
1489 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001490 }
David Tolnay1978c672016-10-27 22:05:52 -07001491
Michael Layzell734adb42017-06-07 16:58:31 -04001492 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001493 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001494 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001495 do_: keyword!(do) >>
1496 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001497 catch_block: syn!(Block) >>
1498 (ExprCatch {
1499 block: catch_block,
1500 do_token: do_,
1501 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001502 })
Michael Layzell92639a52017-06-01 00:07:44 -04001503 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001504 }
Arnavion02ef13f2017-04-25 00:54:31 -07001505
Michael Layzell734adb42017-06-07 16:58:31 -04001506 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001507 impl Synom for ExprYield {
1508 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001509 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001510 expr: option!(syn!(Expr)) >>
1511 (ExprYield {
1512 yield_token: yield_,
1513 expr: expr.map(Box::new),
1514 })
1515 ));
1516 }
1517
1518 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001519 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001520 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001521 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001522 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001523 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1524 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001525 body: do_parse!(
1526 expr: alt!(expr_nosemi | syn!(Expr)) >>
1527 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1528 map!(input_end!(), |_| None)
1529 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001530 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001531 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001532 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001533 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001534 ) >>
1535 (Arm {
1536 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001537 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001538 attrs: attrs,
1539 pats: pats,
1540 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001541 body: Box::new(body.0),
1542 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001543 })
1544 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001545 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001546
Michael Layzell734adb42017-06-07 16:58:31 -04001547 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001548 named!(expr_closure(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001549 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001550 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001551 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001552 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001553 ret_and_body: alt!(
1554 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001555 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001556 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001557 body: syn!(Block) >>
David Tolnay7f675742017-12-27 22:43:21 -05001558 (ReturnType::Type(Box::new(ty), arrow),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001559 ExprKind::Block(ExprBlock {
Alex Crichton62a0a592017-05-22 13:58:53 -07001560 block: body,
1561 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001562 )
1563 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001564 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001565 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001566 (ExprClosure {
1567 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001568 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001569 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001570 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001571 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001572 body: Box::new(ret_and_body.1),
1573 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001574 ));
1575
Michael Layzell734adb42017-06-07 16:58:31 -04001576 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001577 named!(fn_arg -> FnArg, do_parse!(
1578 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001579 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001580 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001581 if let Some((colon, ty)) = ty {
1582 FnArg::Captured(ArgCaptured {
1583 pat: pat,
1584 colon_token: colon,
1585 ty: ty,
1586 })
1587 } else {
1588 FnArg::Inferred(pat)
1589 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001590 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001591 ));
1592
Michael Layzell734adb42017-06-07 16:58:31 -04001593 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001594 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001595 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001596 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1597 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001598 cond: expr_no_struct >>
1599 while_block: syn!(Block) >>
1600 (ExprWhile {
1601 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001602 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001603 cond: Box::new(cond),
1604 body: while_block,
1605 label: lbl.map(|p| p.0),
1606 })
1607 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001608 }
1609
Michael Layzell734adb42017-06-07 16:58:31 -04001610 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001611 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001612 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001613 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1614 while_: keyword!(while) >>
1615 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001616 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001617 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001618 value: expr_no_struct >>
1619 while_block: syn!(Block) >>
1620 (ExprWhileLet {
1621 eq_token: eq,
1622 let_token: let_,
1623 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001624 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001625 pat: Box::new(pat),
1626 expr: Box::new(value),
1627 body: while_block,
1628 label: lbl.map(|p| p.0),
1629 })
1630 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001631 }
1632
Michael Layzell734adb42017-06-07 16:58:31 -04001633 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001634 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001635 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001636 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001637 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001638 (ExprContinue {
1639 continue_token: cont,
1640 label: lbl,
1641 })
1642 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001643 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001644
Michael Layzell734adb42017-06-07 16:58:31 -04001645 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001646 named!(expr_break(allow_struct: bool) -> ExprKind, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001647 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001648 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001649 // We can't allow blocks after a `break` expression when we wouldn't
1650 // allow structs, as this expression is ambiguous.
1651 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001652 (ExprBreak {
1653 label: lbl,
1654 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001655 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001656 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001657 ));
1658
Michael Layzell734adb42017-06-07 16:58:31 -04001659 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001660 named!(expr_ret(allow_struct: bool) -> ExprKind, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001661 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001662 // NOTE: return is greedy and eats blocks after it even when in a
1663 // position where structs are not allowed, such as in if statement
1664 // conditions. For example:
1665 //
David Tolnaybcf26022017-12-25 22:10:52 -05001666 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001667 ret_value: option!(ambiguous_expr!(allow_struct)) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001668 (ExprRet {
1669 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001670 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001671 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001672 ));
1673
Michael Layzell734adb42017-06-07 16:58:31 -04001674 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001675 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001676 named!(parse -> Self, do_parse!(
1677 path: syn!(Path) >>
1678 data: braces!(do_parse!(
1679 fields: call!(Delimited::parse_terminated) >>
1680 base: option!(
1681 cond!(fields.is_empty() || fields.trailing_delim(),
1682 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001683 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001684 base: syn!(Expr) >>
1685 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001686 )
Michael Layzell92639a52017-06-01 00:07:44 -04001687 )
1688 ) >>
1689 (fields, base)
1690 )) >>
1691 ({
1692 let ((fields, base), brace) = data;
1693 let (dots, rest) = match base.and_then(|b| b) {
1694 Some((dots, base)) => (Some(dots), Some(base)),
1695 None => (None, None),
1696 };
1697 ExprStruct {
1698 brace_token: brace,
1699 path: path,
1700 fields: fields,
1701 dot2_token: dots,
1702 rest: rest.map(Box::new),
1703 }
1704 })
1705 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001706 }
1707
Michael Layzell734adb42017-06-07 16:58:31 -04001708 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001709 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001710 named!(parse -> Self, alt!(
1711 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001712 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001713 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001714 value: syn!(Expr) >>
1715 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001716 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001717 expr: value,
1718 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001719 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001720 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001721 })
Michael Layzell92639a52017-06-01 00:07:44 -04001722 )
1723 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001724 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001725 member: Member::Named(name),
Michael Layzell92639a52017-06-01 00:07:44 -04001726 expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(),
1727 is_shorthand: true,
1728 attrs: Vec::new(),
1729 colon_token: None,
1730 })
1731 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001732 }
David Tolnay055a7042016-10-02 19:23:54 -07001733
Michael Layzell734adb42017-06-07 16:58:31 -04001734 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001735 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001736 named!(parse -> Self, do_parse!(
1737 data: brackets!(do_parse!(
1738 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001739 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001740 times: syn!(Expr) >>
1741 (value, semi, times)
1742 )) >>
1743 (ExprRepeat {
1744 expr: Box::new((data.0).0),
1745 amt: Box::new((data.0).2),
1746 bracket_token: data.1,
1747 semi_token: (data.0).1,
1748 })
1749 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001750 }
David Tolnay055a7042016-10-02 19:23:54 -07001751
Michael Layzell734adb42017-06-07 16:58:31 -04001752 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001753 impl Synom for ExprUnsafe {
1754 named!(parse -> Self, do_parse!(
1755 unsafe_: keyword!(unsafe) >>
1756 b: syn!(Block) >>
1757 (ExprUnsafe {
1758 unsafe_token: unsafe_,
1759 block: b,
1760 })
1761 ));
1762 }
1763
1764 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001765 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001766 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001767 b: syn!(Block) >>
1768 (ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001769 block: b,
1770 })
1771 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001772 }
David Tolnay89e05672016-10-02 14:39:42 -07001773
Michael Layzell734adb42017-06-07 16:58:31 -04001774 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -04001775 named!(expr_range(allow_struct: bool) -> ExprKind, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001776 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001777 hi: opt_ambiguous_expr!(allow_struct) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001778 (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001779 ));
1780
Michael Layzell734adb42017-06-07 16:58:31 -04001781 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001782 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001783 named!(parse -> Self, alt!(
1784 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001785 punct!(..=) => { RangeLimits::Closed }
1786 |
1787 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001788 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001789 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001790 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001791 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001792 }
David Tolnay438c9052016-10-07 23:24:48 -07001793
Alex Crichton954046c2017-05-30 21:49:42 -07001794 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001795 named!(parse -> Self, do_parse!(
1796 pair: qpath >>
1797 (ExprPath {
1798 qself: pair.0,
1799 path: pair.1,
1800 })
1801 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001802 }
David Tolnay42602292016-10-01 22:25:45 -07001803
Michael Layzell734adb42017-06-07 16:58:31 -04001804 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001805 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001806
David Tolnay32954ef2017-12-26 22:43:16 -05001807 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001808
Michael Layzell734adb42017-06-07 16:58:31 -04001809 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001810 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001811 named!(parse -> Self, do_parse!(
1812 stmts: braces!(call!(Block::parse_within)) >>
1813 (Block {
1814 stmts: stmts.0,
1815 brace_token: stmts.1,
1816 })
1817 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001818 }
David Tolnay939766a2016-09-23 23:48:12 -07001819
Michael Layzell734adb42017-06-07 16:58:31 -04001820 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001821 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001822 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001823 many0!(punct!(;)) >>
1824 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001825 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001826 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001827 mut e: syn!(Expr) >>
1828 ({
1829 e.attrs = attrs;
1830 Stmt::Expr(Box::new(e))
1831 })
1832 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001833 (match last {
1834 None => standalone,
1835 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001836 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001837 standalone
1838 }
1839 })
1840 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001841 }
1842
Michael Layzell734adb42017-06-07 16:58:31 -04001843 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001844 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001845 named!(parse -> Self, alt!(
1846 stmt_mac
1847 |
1848 stmt_local
1849 |
1850 stmt_item
1851 |
Michael Layzell35418782017-06-07 09:20:25 -04001852 stmt_blockexpr
1853 |
Michael Layzell92639a52017-06-01 00:07:44 -04001854 stmt_expr
1855 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001856 }
David Tolnay939766a2016-09-23 23:48:12 -07001857
Michael Layzell734adb42017-06-07 16:58:31 -04001858 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001859 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001860 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001861 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001862 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001863 // Only parse braces here; paren and bracket will get parsed as
1864 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001865 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001866 semi: option!(punct!(;)) >>
David Tolnaydecf28d2017-11-11 11:56:45 -08001867 (Stmt::Macro(Box::new((
1868 Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001869 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001870 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001871 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001872 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001873 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001874 },
David Tolnayeea28d62016-10-25 20:44:08 -07001875 },
Alex Crichton954046c2017-05-30 21:49:42 -07001876 match semi {
1877 Some(semi) => MacStmtStyle::Semicolon(semi),
1878 None => MacStmtStyle::Braces,
David Tolnay60d48942016-10-30 14:34:52 -07001879 },
David Tolnayeea28d62016-10-25 20:44:08 -07001880 attrs,
1881 ))))
David Tolnay13b3d352016-10-03 00:31:15 -07001882 ));
1883
Michael Layzell734adb42017-06-07 16:58:31 -04001884 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001885 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001886 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001887 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001888 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001889 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001890 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1891 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001892 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001893 let_token: let_,
1894 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001895 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
1896 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001897 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001898 ty: ty.map(|p| Box::new(p.1)),
1899 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001900 attrs: attrs,
1901 })))
1902 ));
1903
Michael Layzell734adb42017-06-07 16:58:31 -04001904 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001905 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07001906
Michael Layzell734adb42017-06-07 16:58:31 -04001907 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001908 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001909 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04001910 mut e: expr_nosemi >>
1911 // If the next token is a `.` or a `?` it is special-cased to parse as
1912 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08001913 not!(punct!(.)) >>
1914 not!(punct!(?)) >>
1915 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04001916 ({
1917 e.attrs = attrs;
1918 if let Some(semi) = semi {
1919 Stmt::Semi(Box::new(e), semi)
1920 } else {
1921 Stmt::Expr(Box::new(e))
1922 }
1923 })
1924 ));
David Tolnaycfe55022016-10-02 22:02:27 -07001925
Michael Layzell734adb42017-06-07 16:58:31 -04001926 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07001927 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001928 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001929 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001930 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07001931 ({
1932 e.attrs = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04001933 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07001934 })
David Tolnay939766a2016-09-23 23:48:12 -07001935 ));
David Tolnay8b07f372016-09-30 10:28:40 -07001936
Michael Layzell734adb42017-06-07 16:58:31 -04001937 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001938 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04001939 named!(parse -> Self, alt!(
1940 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
1941 |
1942 syn!(PatBox) => { Pat::Box } // must be before pat_ident
1943 |
1944 syn!(PatRange) => { Pat::Range } // must be before pat_lit
1945 |
1946 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
1947 |
1948 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
1949 |
David Tolnaydecf28d2017-11-11 11:56:45 -08001950 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04001951 |
1952 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
1953 |
1954 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
1955 |
1956 syn!(PatPath) => { Pat::Path }
1957 |
1958 syn!(PatTuple) => { Pat::Tuple }
1959 |
1960 syn!(PatRef) => { Pat::Ref }
1961 |
1962 syn!(PatSlice) => { Pat::Slice }
1963 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001964 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001965
Michael Layzell734adb42017-06-07 16:58:31 -04001966 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001967 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04001968 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001969 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04001970 |u| PatWild { underscore_token: u }
1971 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001972 }
David Tolnay84aa0752016-10-02 23:01:13 -07001973
Michael Layzell734adb42017-06-07 16:58:31 -04001974 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001975 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04001976 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001977 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001978 pat: syn!(Pat) >>
1979 (PatBox {
1980 pat: Box::new(pat),
1981 box_token: boxed,
1982 })
1983 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001984 }
1985
Michael Layzell734adb42017-06-07 16:58:31 -04001986 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001987 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04001988 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001989 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001990 mutability: syn!(Mutability) >>
1991 name: alt!(
1992 syn!(Ident)
1993 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001994 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04001995 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001996 not!(punct!(<)) >>
1997 not!(punct!(::)) >>
1998 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001999 (PatIdent {
2000 mode: match mode {
2001 Some(mode) => BindingMode::ByRef(mode, mutability),
2002 None => BindingMode::ByValue(mutability),
2003 },
2004 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002005 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002006 subpat: subpat.map(|p| Box::new(p.1)),
2007 })
2008 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002009 }
2010
Michael Layzell734adb42017-06-07 16:58:31 -04002011 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002012 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002013 named!(parse -> Self, do_parse!(
2014 path: syn!(Path) >>
2015 tuple: syn!(PatTuple) >>
2016 (PatTupleStruct {
2017 path: path,
2018 pat: tuple,
2019 })
2020 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002021 }
2022
Michael Layzell734adb42017-06-07 16:58:31 -04002023 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002024 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002025 named!(parse -> Self, do_parse!(
2026 path: syn!(Path) >>
2027 data: braces!(do_parse!(
2028 fields: call!(Delimited::parse_terminated) >>
2029 base: option!(
2030 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002031 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002032 ) >>
2033 (fields, base)
2034 )) >>
2035 (PatStruct {
2036 path: path,
2037 fields: (data.0).0,
2038 brace_token: data.1,
2039 dot2_token: (data.0).1.and_then(|m| m),
2040 })
2041 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002042 }
2043
Michael Layzell734adb42017-06-07 16:58:31 -04002044 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002045 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002046 named!(parse -> Self, alt!(
2047 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002048 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002049 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002050 pat: syn!(Pat) >>
2051 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002052 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002053 pat: Box::new(pat),
2054 is_shorthand: false,
2055 attrs: Vec::new(),
2056 colon_token: Some(colon),
2057 })
2058 )
2059 |
2060 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002061 boxed: option!(keyword!(box)) >>
2062 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002063 mutability: syn!(Mutability) >>
2064 ident: syn!(Ident) >>
2065 ({
2066 let mut pat: Pat = PatIdent {
2067 mode: if let Some(mode) = mode {
2068 BindingMode::ByRef(mode, mutability)
2069 } else {
2070 BindingMode::ByValue(mutability)
2071 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002072 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002073 subpat: None,
2074 at_token: None,
2075 }.into();
2076 if let Some(boxed) = boxed {
2077 pat = PatBox {
2078 pat: Box::new(pat),
2079 box_token: boxed,
2080 }.into();
2081 }
2082 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002083 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002084 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002085 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002086 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002087 colon_token: None,
2088 }
2089 })
2090 )
2091 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002092 }
2093
Michael Layzell734adb42017-06-07 16:58:31 -04002094 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002095 impl Synom for Member {
2096 named!(parse -> Self, alt!(
2097 syn!(Ident) => { Member::Named }
2098 |
2099 syn!(Index) => { Member::Unnamed }
2100 ));
2101 }
2102
2103 #[cfg(feature = "full")]
2104 impl Synom for Index {
2105 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002106 lit: syn!(Lit) >>
2107 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002108 if let Ok(i) = lit.value.to_string().parse() {
2109 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002110 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002111 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002112 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002113 })
David Tolnay85b69a42017-12-27 20:43:10 -05002114 ));
2115 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002116
Michael Layzell734adb42017-06-07 16:58:31 -04002117 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002118 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002119 named!(parse -> Self, map!(
2120 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002121 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002122 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002123 }
David Tolnay9636c052016-10-02 17:11:17 -07002124
Michael Layzell734adb42017-06-07 16:58:31 -04002125 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002126 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002127 named!(parse -> Self, do_parse!(
2128 data: parens!(do_parse!(
2129 elems: call!(Delimited::parse_terminated) >>
2130 dotdot: map!(cond!(
2131 elems.is_empty() || elems.trailing_delim(),
2132 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002133 dots: punct!(..) >>
2134 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002135 (dots, trailing)
2136 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002137 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002138 rest: cond!(match dotdot {
2139 Some((_, Some(_))) => true,
2140 _ => false,
2141 },
2142 call!(Delimited::parse_terminated)) >>
2143 (elems, dotdot, rest)
2144 )) >>
2145 ({
2146 let ((mut elems, dotdot, rest), parens) = data;
2147 let (dotdot, trailing) = match dotdot {
2148 Some((a, b)) => (Some(a), Some(b)),
2149 None => (None, None),
2150 };
2151 PatTuple {
2152 paren_token: parens,
2153 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2154 dot2_token: dotdot,
2155 comma_token: trailing.and_then(|b| b),
2156 pats: {
2157 if let Some(rest) = rest {
2158 for elem in rest {
2159 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002160 }
Michael Layzell92639a52017-06-01 00:07:44 -04002161 }
2162 elems
2163 },
2164 }
2165 })
2166 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002167 }
David Tolnayfbb73232016-10-03 01:00:06 -07002168
Michael Layzell734adb42017-06-07 16:58:31 -04002169 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002170 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002171 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002172 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002173 mutability: syn!(Mutability) >>
2174 pat: syn!(Pat) >>
2175 (PatRef {
2176 pat: Box::new(pat),
2177 mutbl: mutability,
2178 and_token: and,
2179 })
2180 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002181 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002182
Michael Layzell734adb42017-06-07 16:58:31 -04002183 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002184 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002185 named!(parse -> Self, do_parse!(
2186 lit: pat_lit_expr >>
2187 (if let ExprKind::Path(_) = lit.node {
2188 return parse_error(); // these need to be parsed by pat_path
2189 } else {
2190 PatLit {
2191 expr: Box::new(lit),
2192 }
2193 })
2194 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002195 }
David Tolnaye1310902016-10-29 23:40:00 -07002196
Michael Layzell734adb42017-06-07 16:58:31 -04002197 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002198 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002199 named!(parse -> Self, do_parse!(
2200 lo: pat_lit_expr >>
2201 limits: syn!(RangeLimits) >>
2202 hi: pat_lit_expr >>
2203 (PatRange {
2204 lo: Box::new(lo),
2205 hi: Box::new(hi),
2206 limits: limits,
2207 })
2208 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002209 }
David Tolnaye1310902016-10-29 23:40:00 -07002210
Michael Layzell734adb42017-06-07 16:58:31 -04002211 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002212 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002213 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002214 v: alt!(
Alex Crichton954046c2017-05-30 21:49:42 -07002215 syn!(Lit) => { ExprKind::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002216 |
Alex Crichton954046c2017-05-30 21:49:42 -07002217 syn!(ExprPath) => { ExprKind::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002218 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002219 (if let Some(neg) = neg {
Alex Crichton62a0a592017-05-22 13:58:53 -07002220 ExprKind::Unary(ExprUnary {
David Tolnayc29b9892017-12-27 22:58:14 -05002221 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002222 expr: Box::new(v.into())
2223 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002224 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002225 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002226 })
2227 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002228
Michael Layzell734adb42017-06-07 16:58:31 -04002229 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002230 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002231 named!(parse -> Self, map!(
2232 brackets!(do_parse!(
2233 before: call!(Delimited::parse_terminated) >>
2234 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002235 dots: punct!(..) >>
2236 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002237 (dots, trailing)
2238 )) >>
2239 after: cond!(
2240 match middle {
2241 Some((_, ref trailing)) => trailing.is_some(),
2242 _ => false,
2243 },
2244 call!(Delimited::parse_terminated)
2245 ) >>
2246 (before, middle, after)
2247 )),
2248 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002249 let mut before: Delimited<Pat, Token![,]> = before;
2250 let after: Option<Delimited<Pat, Token![,]>> = after;
2251 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002252 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002253 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002254 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002255 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002256 }),
2257 bracket_token: brackets,
2258 middle: middle.and_then(|_| {
2259 if !before.is_empty() && !before.trailing_delim() {
2260 Some(Box::new(before.pop().unwrap().into_item()))
2261 } else {
2262 None
2263 }
2264 }),
2265 front: before,
2266 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002267 }
Alex Crichton954046c2017-05-30 21:49:42 -07002268 }
Michael Layzell92639a52017-06-01 00:07:44 -04002269 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002270 }
David Tolnay435a9a82016-10-29 13:47:20 -07002271
Michael Layzell734adb42017-06-07 16:58:31 -04002272 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002273 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002274 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002275 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002276 |
2277 epsilon!() => { |_| CaptureBy::Ref }
2278 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002279 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002280}
2281
David Tolnayf4bbbd92016-09-23 14:41:55 -07002282#[cfg(feature = "printing")]
2283mod printing {
2284 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002285 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002286 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002287 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002288 #[cfg(feature = "full")]
2289 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002290
David Tolnaybcf26022017-12-25 22:10:52 -05002291 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2292 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002293 #[cfg(feature = "full")]
2294 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
2295 if let ExprKind::Struct(_) = e.node {
David Tolnay32954ef2017-12-26 22:43:16 -05002296 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002297 e.to_tokens(tokens);
2298 });
2299 } else {
2300 e.to_tokens(tokens);
2301 }
2302 }
2303
David Tolnayf4bbbd92016-09-23 14:41:55 -07002304 impl ToTokens for Expr {
Michael Layzell734adb42017-06-07 16:58:31 -04002305 #[cfg(feature = "full")]
David Tolnayf4bbbd92016-09-23 14:41:55 -07002306 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay7184b132016-10-30 10:06:37 -07002307 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002308 self.node.to_tokens(tokens)
2309 }
Michael Layzell734adb42017-06-07 16:58:31 -04002310
2311 #[cfg(not(feature = "full"))]
2312 fn to_tokens(&self, tokens: &mut Tokens) {
2313 self.node.to_tokens(tokens)
2314 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002315 }
2316
Michael Layzell734adb42017-06-07 16:58:31 -04002317 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002318 impl ToTokens for ExprBox {
2319 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002320 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002321 self.expr.to_tokens(tokens);
2322 }
2323 }
2324
Michael Layzell734adb42017-06-07 16:58:31 -04002325 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002326 impl ToTokens for ExprInPlace {
2327 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell6a5a1642017-06-04 19:35:15 -04002328 match self.kind {
2329 InPlaceKind::Arrow(ref arrow) => {
2330 self.place.to_tokens(tokens);
2331 arrow.to_tokens(tokens);
2332 self.value.to_tokens(tokens);
2333 }
2334 InPlaceKind::In(ref _in) => {
2335 _in.to_tokens(tokens);
2336 self.place.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002337 // NOTE: The second operand must be in a block, add one if
2338 // it is not present.
2339 if let ExprKind::Block(_) = self.value.node {
2340 self.value.to_tokens(tokens);
2341 } else {
David Tolnay32954ef2017-12-26 22:43:16 -05002342 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002343 self.value.to_tokens(tokens);
2344 })
2345 }
Michael Layzell6a5a1642017-06-04 19:35:15 -04002346 }
2347 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002348 }
2349 }
2350
Michael Layzell734adb42017-06-07 16:58:31 -04002351 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002352 impl ToTokens for ExprArray {
2353 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002354 self.bracket_token.surround(tokens, |tokens| {
2355 self.exprs.to_tokens(tokens);
2356 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002357 }
2358 }
2359
2360 impl ToTokens for ExprCall {
2361 fn to_tokens(&self, tokens: &mut Tokens) {
2362 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002363 self.paren_token.surround(tokens, |tokens| {
2364 self.args.to_tokens(tokens);
2365 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002366 }
2367 }
2368
Michael Layzell734adb42017-06-07 16:58:31 -04002369 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002370 impl ToTokens for ExprMethodCall {
2371 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002372 self.expr.to_tokens(tokens);
2373 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002374 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002375 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002376 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2377 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002378 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002379 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002380 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002381 self.paren_token.surround(tokens, |tokens| {
2382 self.args.to_tokens(tokens);
2383 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002384 }
2385 }
2386
Michael Layzell734adb42017-06-07 16:58:31 -04002387 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002388 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002389 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002390 self.paren_token.surround(tokens, |tokens| {
2391 self.args.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002392 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002393 // distinguish ExprTuple from ExprParen.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002394 if self.args.len() == 1 && !self.args.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002395 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002396 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002397 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002398 }
2399 }
2400
2401 impl ToTokens for ExprBinary {
2402 fn to_tokens(&self, tokens: &mut Tokens) {
2403 self.left.to_tokens(tokens);
2404 self.op.to_tokens(tokens);
2405 self.right.to_tokens(tokens);
2406 }
2407 }
2408
2409 impl ToTokens for ExprUnary {
2410 fn to_tokens(&self, tokens: &mut Tokens) {
2411 self.op.to_tokens(tokens);
2412 self.expr.to_tokens(tokens);
2413 }
2414 }
2415
2416 impl ToTokens for ExprCast {
2417 fn to_tokens(&self, tokens: &mut Tokens) {
2418 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002419 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002420 self.ty.to_tokens(tokens);
2421 }
2422 }
2423
2424 impl ToTokens for ExprType {
2425 fn to_tokens(&self, tokens: &mut Tokens) {
2426 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002427 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002428 self.ty.to_tokens(tokens);
2429 }
2430 }
2431
Michael Layzell734adb42017-06-07 16:58:31 -04002432 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002433 fn maybe_wrap_else(
2434 tokens: &mut Tokens,
2435 else_token: &Option<Token![else]>,
2436 if_false: &Option<Box<Expr>>,
2437 ) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002438 if let Some(ref if_false) = *if_false {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002439 TokensOrDefault(else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002440
2441 // If we are not one of the valid expressions to exist in an else
2442 // clause, wrap ourselves in a block.
2443 match if_false.node {
David Tolnay51382052017-12-27 13:46:21 -05002444 ExprKind::If(_) | ExprKind::IfLet(_) | ExprKind::Block(_) => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002445 if_false.to_tokens(tokens);
2446 }
2447 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002448 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002449 if_false.to_tokens(tokens);
2450 });
2451 }
2452 }
2453 }
2454 }
2455
2456 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002457 impl ToTokens for ExprIf {
2458 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002459 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002460 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002461 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002462 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002463 }
2464 }
2465
Michael Layzell734adb42017-06-07 16:58:31 -04002466 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002467 impl ToTokens for ExprIfLet {
2468 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002469 self.if_token.to_tokens(tokens);
2470 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002471 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002472 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002473 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002474 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002475 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002476 }
2477 }
2478
Michael Layzell734adb42017-06-07 16:58:31 -04002479 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002480 impl ToTokens for ExprWhile {
2481 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002482 if self.label.is_some() {
2483 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002484 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002485 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002486 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002487 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002488 self.body.to_tokens(tokens);
2489 }
2490 }
2491
Michael Layzell734adb42017-06-07 16:58:31 -04002492 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002493 impl ToTokens for ExprWhileLet {
2494 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002495 if self.label.is_some() {
2496 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002497 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002498 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002499 self.while_token.to_tokens(tokens);
2500 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002501 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002502 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002503 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002504 self.body.to_tokens(tokens);
2505 }
2506 }
2507
Michael Layzell734adb42017-06-07 16:58:31 -04002508 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002509 impl ToTokens for ExprForLoop {
2510 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002511 if self.label.is_some() {
2512 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002513 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002514 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002515 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002516 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002517 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002518 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002519 self.body.to_tokens(tokens);
2520 }
2521 }
2522
Michael Layzell734adb42017-06-07 16:58:31 -04002523 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002524 impl ToTokens for ExprLoop {
2525 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002526 if self.label.is_some() {
2527 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002528 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002529 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002530 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002531 self.body.to_tokens(tokens);
2532 }
2533 }
2534
Michael Layzell734adb42017-06-07 16:58:31 -04002535 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002536 impl ToTokens for ExprMatch {
2537 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002538 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002539 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002540 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002541 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002542 arm.to_tokens(tokens);
2543 // Ensure that we have a comma after a non-block arm, except
2544 // for the last one.
2545 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002546 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002547 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002548 }
2549 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002550 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002551 }
2552 }
2553
Michael Layzell734adb42017-06-07 16:58:31 -04002554 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002555 impl ToTokens for ExprCatch {
2556 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002557 self.do_token.to_tokens(tokens);
2558 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002559 self.block.to_tokens(tokens);
2560 }
2561 }
2562
Michael Layzell734adb42017-06-07 16:58:31 -04002563 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002564 impl ToTokens for ExprYield {
2565 fn to_tokens(&self, tokens: &mut Tokens) {
2566 self.yield_token.to_tokens(tokens);
2567 self.expr.to_tokens(tokens);
2568 }
2569 }
2570
2571 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002572 impl ToTokens for ExprClosure {
2573 fn to_tokens(&self, tokens: &mut Tokens) {
2574 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002575 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002576 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002577 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002578 FnArg::Captured(ArgCaptured {
2579 ref pat,
2580 ty: Type::Infer(_),
2581 ..
2582 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002583 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002584 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002585 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002586 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002587 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002588 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002589 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002590 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002591 self.body.to_tokens(tokens);
2592 }
2593 }
2594
Michael Layzell734adb42017-06-07 16:58:31 -04002595 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002596 impl ToTokens for ExprUnsafe {
2597 fn to_tokens(&self, tokens: &mut Tokens) {
2598 self.unsafe_token.to_tokens(tokens);
2599 self.block.to_tokens(tokens);
2600 }
2601 }
2602
2603 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002604 impl ToTokens for ExprBlock {
2605 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002606 self.block.to_tokens(tokens);
2607 }
2608 }
2609
Michael Layzell734adb42017-06-07 16:58:31 -04002610 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002611 impl ToTokens for ExprAssign {
2612 fn to_tokens(&self, tokens: &mut Tokens) {
2613 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002614 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002615 self.right.to_tokens(tokens);
2616 }
2617 }
2618
Michael Layzell734adb42017-06-07 16:58:31 -04002619 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002620 impl ToTokens for ExprAssignOp {
2621 fn to_tokens(&self, tokens: &mut Tokens) {
2622 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002623 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002624 self.right.to_tokens(tokens);
2625 }
2626 }
2627
Michael Layzell734adb42017-06-07 16:58:31 -04002628 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002629 impl ToTokens for ExprField {
2630 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002631 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002632 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002633 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002634 }
2635 }
2636
Michael Layzell734adb42017-06-07 16:58:31 -04002637 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002638 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002639 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002640 match *self {
2641 Member::Named(ident) => ident.to_tokens(tokens),
2642 Member::Unnamed(ref index) => index.to_tokens(tokens),
2643 }
2644 }
2645 }
2646
2647 #[cfg(feature = "full")]
2648 impl ToTokens for Index {
2649 fn to_tokens(&self, tokens: &mut Tokens) {
2650 tokens.append(TokenTree {
2651 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002652 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002653 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002654 }
2655 }
2656
2657 impl ToTokens for ExprIndex {
2658 fn to_tokens(&self, tokens: &mut Tokens) {
2659 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002660 self.bracket_token.surround(tokens, |tokens| {
2661 self.index.to_tokens(tokens);
2662 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002663 }
2664 }
2665
Michael Layzell734adb42017-06-07 16:58:31 -04002666 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002667 impl ToTokens for ExprRange {
2668 fn to_tokens(&self, tokens: &mut Tokens) {
2669 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002670 match self.limits {
2671 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2672 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2673 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002674 self.to.to_tokens(tokens);
2675 }
2676 }
2677
2678 impl ToTokens for ExprPath {
2679 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002680 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002681 }
2682 }
2683
Michael Layzell734adb42017-06-07 16:58:31 -04002684 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002685 impl ToTokens for ExprAddrOf {
2686 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002687 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002688 self.mutbl.to_tokens(tokens);
2689 self.expr.to_tokens(tokens);
2690 }
2691 }
2692
Michael Layzell734adb42017-06-07 16:58:31 -04002693 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002694 impl ToTokens for ExprBreak {
2695 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002696 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002697 self.label.to_tokens(tokens);
2698 self.expr.to_tokens(tokens);
2699 }
2700 }
2701
Michael Layzell734adb42017-06-07 16:58:31 -04002702 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002703 impl ToTokens for ExprContinue {
2704 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002705 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002706 self.label.to_tokens(tokens);
2707 }
2708 }
2709
Michael Layzell734adb42017-06-07 16:58:31 -04002710 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002711 impl ToTokens for ExprRet {
2712 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002713 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002714 self.expr.to_tokens(tokens);
2715 }
2716 }
2717
Michael Layzell734adb42017-06-07 16:58:31 -04002718 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002719 impl ToTokens for ExprStruct {
2720 fn to_tokens(&self, tokens: &mut Tokens) {
2721 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002722 self.brace_token.surround(tokens, |tokens| {
2723 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002724 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002725 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002726 self.rest.to_tokens(tokens);
2727 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002728 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002729 }
2730 }
2731
Michael Layzell734adb42017-06-07 16:58:31 -04002732 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002733 impl ToTokens for ExprRepeat {
2734 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002735 self.bracket_token.surround(tokens, |tokens| {
2736 self.expr.to_tokens(tokens);
2737 self.semi_token.to_tokens(tokens);
2738 self.amt.to_tokens(tokens);
2739 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002740 }
2741 }
2742
Michael Layzell93c36282017-06-04 20:43:14 -04002743 impl ToTokens for ExprGroup {
2744 fn to_tokens(&self, tokens: &mut Tokens) {
2745 self.group_token.surround(tokens, |tokens| {
2746 self.expr.to_tokens(tokens);
2747 });
2748 }
2749 }
2750
Alex Crichton62a0a592017-05-22 13:58:53 -07002751 impl ToTokens for ExprParen {
2752 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002753 self.paren_token.surround(tokens, |tokens| {
2754 self.expr.to_tokens(tokens);
2755 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002756 }
2757 }
2758
Michael Layzell734adb42017-06-07 16:58:31 -04002759 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002760 impl ToTokens for ExprTry {
2761 fn to_tokens(&self, tokens: &mut Tokens) {
2762 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002763 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002764 }
2765 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002766
Michael Layzell734adb42017-06-07 16:58:31 -04002767 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002768 impl ToTokens for FieldValue {
2769 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002770 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002771 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2772 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002773 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002774 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002775 self.expr.to_tokens(tokens);
2776 }
David Tolnay055a7042016-10-02 19:23:54 -07002777 }
2778 }
2779
Michael Layzell734adb42017-06-07 16:58:31 -04002780 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002781 impl ToTokens for Arm {
2782 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002783 tokens.append_all(&self.attrs);
2784 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002785 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002786 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002787 self.guard.to_tokens(tokens);
2788 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002789 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002790 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002791 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002792 }
2793 }
2794
Michael Layzell734adb42017-06-07 16:58:31 -04002795 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002796 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002797 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002798 self.underscore_token.to_tokens(tokens);
2799 }
2800 }
2801
Michael Layzell734adb42017-06-07 16:58:31 -04002802 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002803 impl ToTokens for PatIdent {
2804 fn to_tokens(&self, tokens: &mut Tokens) {
2805 self.mode.to_tokens(tokens);
2806 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002807 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002808 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002809 self.subpat.to_tokens(tokens);
2810 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002811 }
2812 }
2813
Michael Layzell734adb42017-06-07 16:58:31 -04002814 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002815 impl ToTokens for PatStruct {
2816 fn to_tokens(&self, tokens: &mut Tokens) {
2817 self.path.to_tokens(tokens);
2818 self.brace_token.surround(tokens, |tokens| {
2819 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002820 // NOTE: We need a comma before the dot2 token if it is present.
2821 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002822 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002823 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002824 self.dot2_token.to_tokens(tokens);
2825 });
2826 }
2827 }
2828
Michael Layzell734adb42017-06-07 16:58:31 -04002829 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002830 impl ToTokens for PatTupleStruct {
2831 fn to_tokens(&self, tokens: &mut Tokens) {
2832 self.path.to_tokens(tokens);
2833 self.pat.to_tokens(tokens);
2834 }
2835 }
2836
Michael Layzell734adb42017-06-07 16:58:31 -04002837 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002838 impl ToTokens for PatPath {
2839 fn to_tokens(&self, tokens: &mut Tokens) {
2840 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2841 }
2842 }
2843
Michael Layzell734adb42017-06-07 16:58:31 -04002844 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002845 impl ToTokens for PatTuple {
2846 fn to_tokens(&self, tokens: &mut Tokens) {
2847 self.paren_token.surround(tokens, |tokens| {
2848 for (i, token) in self.pats.iter().enumerate() {
2849 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07002850 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
2851 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002852 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002853 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002854 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002855
2856 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002857 // Ensure there is a comma before the .. token.
2858 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002859 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002860 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002861 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002862 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002863 });
2864 }
2865 }
2866
Michael Layzell734adb42017-06-07 16:58:31 -04002867 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002868 impl ToTokens for PatBox {
2869 fn to_tokens(&self, tokens: &mut Tokens) {
2870 self.box_token.to_tokens(tokens);
2871 self.pat.to_tokens(tokens);
2872 }
2873 }
2874
Michael Layzell734adb42017-06-07 16:58:31 -04002875 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002876 impl ToTokens for PatRef {
2877 fn to_tokens(&self, tokens: &mut Tokens) {
2878 self.and_token.to_tokens(tokens);
2879 self.mutbl.to_tokens(tokens);
2880 self.pat.to_tokens(tokens);
2881 }
2882 }
2883
Michael Layzell734adb42017-06-07 16:58:31 -04002884 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002885 impl ToTokens for PatLit {
2886 fn to_tokens(&self, tokens: &mut Tokens) {
2887 self.expr.to_tokens(tokens);
2888 }
2889 }
2890
Michael Layzell734adb42017-06-07 16:58:31 -04002891 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002892 impl ToTokens for PatRange {
2893 fn to_tokens(&self, tokens: &mut Tokens) {
2894 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002895 match self.limits {
2896 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2897 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
2898 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002899 self.hi.to_tokens(tokens);
2900 }
2901 }
2902
Michael Layzell734adb42017-06-07 16:58:31 -04002903 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002904 impl ToTokens for PatSlice {
2905 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002906 // XXX: This is a mess, and it will be so easy to screw it up. How
2907 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002908 self.bracket_token.surround(tokens, |tokens| {
2909 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002910
2911 // If we need a comma before the middle or standalone .. token,
2912 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05002913 if !self.front.empty_or_trailing()
2914 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04002915 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002916 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002917 }
2918
2919 // If we have an identifier, we always need a .. token.
2920 if self.middle.is_some() {
2921 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002922 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002923 } else if self.dot2_token.is_some() {
2924 self.dot2_token.to_tokens(tokens);
2925 }
2926
2927 // Make sure we have a comma before the back half.
2928 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002929 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002930 self.back.to_tokens(tokens);
2931 } else {
2932 self.comma_token.to_tokens(tokens);
2933 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002934 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07002935 }
2936 }
2937
Michael Layzell734adb42017-06-07 16:58:31 -04002938 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07002939 impl ToTokens for FieldPat {
2940 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002941 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07002942 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05002943 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002944 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002945 }
2946 self.pat.to_tokens(tokens);
2947 }
2948 }
2949
Michael Layzell734adb42017-06-07 16:58:31 -04002950 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002951 impl ToTokens for BindingMode {
2952 fn to_tokens(&self, tokens: &mut Tokens) {
2953 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 BindingMode::ByRef(ref t, ref m) => {
2955 t.to_tokens(tokens);
2956 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002957 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002958 BindingMode::ByValue(ref m) => {
2959 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002960 }
2961 }
2962 }
2963 }
David Tolnay42602292016-10-01 22:25:45 -07002964
Michael Layzell734adb42017-06-07 16:58:31 -04002965 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07002966 impl ToTokens for CaptureBy {
2967 fn to_tokens(&self, tokens: &mut Tokens) {
2968 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002969 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07002970 CaptureBy::Ref => {
2971 // nothing
2972 }
David Tolnay89e05672016-10-02 14:39:42 -07002973 }
2974 }
2975 }
2976
Michael Layzell734adb42017-06-07 16:58:31 -04002977 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07002978 impl ToTokens for Block {
2979 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002980 self.brace_token.surround(tokens, |tokens| {
2981 tokens.append_all(&self.stmts);
2982 });
David Tolnay42602292016-10-01 22:25:45 -07002983 }
2984 }
2985
Michael Layzell734adb42017-06-07 16:58:31 -04002986 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07002987 impl ToTokens for Stmt {
2988 fn to_tokens(&self, tokens: &mut Tokens) {
2989 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07002990 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07002991 Stmt::Item(ref item) => item.to_tokens(tokens),
2992 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002993 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07002994 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002995 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07002996 }
David Tolnaydecf28d2017-11-11 11:56:45 -08002997 Stmt::Macro(ref mac) => {
Alex Crichton2e0229c2017-05-23 09:34:50 -07002998 let (ref mac, ref style, ref attrs) = **mac;
David Tolnay7184b132016-10-30 10:06:37 -07002999 tokens.append_all(attrs.outer());
David Tolnay13b3d352016-10-03 00:31:15 -07003000 mac.to_tokens(tokens);
Alex Crichton2e0229c2017-05-23 09:34:50 -07003001 match *style {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003002 MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003003 MacStmtStyle::Braces | MacStmtStyle::NoBraces => {
3004 // no semicolon
3005 }
David Tolnay13b3d352016-10-03 00:31:15 -07003006 }
3007 }
David Tolnay42602292016-10-01 22:25:45 -07003008 }
3009 }
3010 }
David Tolnay191e0582016-10-02 18:31:09 -07003011
Michael Layzell734adb42017-06-07 16:58:31 -04003012 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003013 impl ToTokens for Local {
3014 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003015 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003016 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003017 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003018 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003019 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003020 self.ty.to_tokens(tokens);
3021 }
3022 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003023 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003024 self.init.to_tokens(tokens);
3025 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003026 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003027 }
3028 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003029}