blob: 8b781b585077518386f60b133276abe2e8415ce2 [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_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -05009 /// An expression.
10 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070011 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040012 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050013 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080014 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050015 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070016 }),
Clar Charrd22b5702017-03-10 15:24:56 -050017
David Tolnay8701a5c2017-12-28 23:31:10 -050018 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040019 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050020 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070021 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050022 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070023 pub value: Box<Expr>,
24 }),
Clar Charrd22b5702017-03-10 15:24:56 -050025
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040027 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050028 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050029 pub bracket_token: token::Bracket,
David Tolnay2a86fdd2017-12-28 23:34:28 -050030 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070031 }),
Clar Charrd22b5702017-03-10 15:24:56 -050032
Alex Crichton62a0a592017-05-22 13:58:53 -070033 /// A function call.
34 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050035 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050037 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050038 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 }),
Clar Charrd22b5702017-03-10 15:24:56 -050040
Alex Crichton62a0a592017-05-22 13:58:53 -070041 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
42 ///
43 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080044 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070045 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040046 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050047 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070048 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050050 pub method: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 pub colon2_token: Option<Token![::]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050052 pub lt_token: Option<Token![<]>,
53 pub typarams: Delimited<Type, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub gt_token: Option<Token![>]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050055 pub paren_token: token::Paren,
56 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
Clar Charrd22b5702017-03-10 15:24:56 -050058
Alex Crichton62a0a592017-05-22 13:58:53 -070059 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050060 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050061 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050062 pub paren_token: token::Paren,
David Tolnay2a86fdd2017-12-28 23:34:28 -050063 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }),
Clar Charrd22b5702017-03-10 15:24:56 -050065
Alex Crichton62a0a592017-05-22 13:58:53 -070066 /// A binary operation, e.g. `a + b`, `a * b`.
67 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050070 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub right: Box<Expr>,
72 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A unary operation, e.g. `!x`, `*x`.
75 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub op: UnOp,
78 pub expr: Box<Expr>,
79 }),
Clar Charrd22b5702017-03-10 15:24:56 -050080
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050082 pub Lit(ExprLit {
83 pub attrs: Vec<Attribute>,
84 pub lit: Lit,
85 }),
Clar Charrd22b5702017-03-10 15:24:56 -050086
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A cast, e.g. `foo as f64`.
88 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050089 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A type ascription, e.g. `foo: f64`.
96 pub Type(ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -050097 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800100 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500102
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 /// An `if` block, with an optional else block
104 ///
105 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400106 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub cond: Box<Expr>,
110 pub if_true: Block,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub else_token: Option<Token![else]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500112 pub if_false: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500114
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 /// An `if let` expression with an optional else block
116 ///
117 /// E.g., `if let pat = expr { block } else { expr }`
118 ///
119 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400120 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500121 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub if_token: Token![if],
123 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500124 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800125 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500126 pub expr: Box<Expr>,
127 pub if_true: Block,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800128 pub else_token: Option<Token![else]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500129 pub if_false: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700130 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500131
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 /// A while loop, with an optional label
133 ///
134 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400135 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500136 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700137 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800138 pub colon_token: Option<Token![:]>,
139 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500140 pub cond: Box<Expr>,
141 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500143
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 /// A while-let loop, with an optional label.
145 ///
146 /// E.g., `'label: while let pat = expr { block }`
147 ///
148 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700151 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub colon_token: Option<Token![:]>,
153 pub while_token: Token![while],
154 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800156 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500157 pub expr: Box<Expr>,
158 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500160
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// A for loop, with an optional label.
162 ///
163 /// E.g., `'label: for pat in expr { block }`
164 ///
165 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400166 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500167 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500168 pub label: Option<Lifetime>,
169 pub colon_token: Option<Token![:]>,
170 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500172 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub expr: Box<Expr>,
174 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500176
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 /// Conditionless loop with an optional label.
178 ///
179 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400180 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500181 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700182 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800183 pub colon_token: Option<Token![:]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500184 pub loop_token: Token![loop],
185 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500187
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400189 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500190 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800191 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500193 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 pub arms: Vec<Arm>,
195 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500196
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400198 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500199 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 pub capture: CaptureBy,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub or1_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500202 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500204 pub output: ReturnType,
205 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500207
Nika Layzell640832a2017-12-04 13:37:09 -0500208 /// An unsafe block (`unsafe { ... }`)
209 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500210 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500211 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 {
David Tolnay8c91b882017-12-28 23:04:32 -0500217 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub block: Block,
219 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700220
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400222 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500223 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800225 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500226 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500228
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 /// An assignment with an operator
230 ///
231 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400232 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500233 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500235 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 pub right: Box<Expr>,
237 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500238
David Tolnay85b69a42017-12-27 20:43:10 -0500239 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
240 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500243 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800244 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500245 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500247
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 /// An indexing operation (`foo[2]`)
249 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500250 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500252 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500253 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500255
David Tolnaybe55d7b2017-12-17 23:41:20 -0800256 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400257 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500258 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700260 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500261 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700263
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 /// Variable reference, possibly containing `::` and/or type
265 /// parameters, e.g. foo::bar::<baz>.
266 ///
267 /// Optionally "qualified",
268 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
269 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500270 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700271 pub qself: Option<QSelf>,
272 pub path: Path,
273 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700274
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400276 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500277 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800278 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub mutbl: Mutability,
280 pub expr: Box<Expr>,
281 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500282
Alex Crichton62a0a592017-05-22 13:58:53 -0700283 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400284 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500285 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500286 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700287 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 pub expr: Option<Box<Expr>>,
289 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500290
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400292 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500293 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800294 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500295 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500297
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500299 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500300 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500302 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500306 pub Macro(ExprMacro #full {
307 pub attrs: Vec<Attribute>,
308 pub mac: Macro,
309 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700310
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 /// A struct literal expression.
312 ///
313 /// For example, `Foo {x: 1, y: 2}`, or
314 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400315 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500316 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500318 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500319 pub fields: Delimited<FieldValue, Token![,]>,
320 pub dot2_token: Option<Token![..]>,
321 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700323
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 /// An array literal constructed from one repeated element.
325 ///
326 /// For example, `[1; 5]`. The first expression is the element
327 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400328 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500329 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500330 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500332 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700333 pub amt: Box<Expr>,
334 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700335
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500337 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500338 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500339 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500340 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700342
Michael Layzell93c36282017-06-04 20:43:14 -0400343 /// No-op: used solely so we can pretty-print faithfully
344 ///
345 /// A `group` represents a `None`-delimited span in the input
346 /// `TokenStream` which affects the precidence of the resulting
347 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500348 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500349 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500350 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500351 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400352 }),
353
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400355 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500356 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700357 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700360
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 /// A catch expression.
362 ///
363 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400364 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500365 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub do_token: Token![do],
367 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700368 pub block: Block,
369 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700370
371 /// A yield expression.
372 ///
373 /// E.g. `yield expr`
374 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500375 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800376 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700377 pub expr: Option<Box<Expr>>,
378 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700380}
381
David Tolnay8c91b882017-12-28 23:04:32 -0500382impl Expr {
383 // Not public API.
384 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500385 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500386 pub fn attrs_mut(&mut self) -> &mut Vec<Attribute> {
387 match *self {
388 Expr::Box(ExprBox { ref mut attrs, .. }) |
389 Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
390 Expr::Array(ExprArray { ref mut attrs, .. }) |
391 Expr::Call(ExprCall { ref mut attrs, .. }) |
392 Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
393 Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
394 Expr::Binary(ExprBinary { ref mut attrs, .. }) |
395 Expr::Unary(ExprUnary { ref mut attrs, .. }) |
396 Expr::Lit(ExprLit { ref mut attrs, .. }) |
397 Expr::Cast(ExprCast { ref mut attrs, .. }) |
398 Expr::Type(ExprType { ref mut attrs, .. }) |
399 Expr::If(ExprIf { ref mut attrs, .. }) |
400 Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
401 Expr::While(ExprWhile { ref mut attrs, .. }) |
402 Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
403 Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
404 Expr::Loop(ExprLoop { ref mut attrs, .. }) |
405 Expr::Match(ExprMatch { ref mut attrs, .. }) |
406 Expr::Closure(ExprClosure { ref mut attrs, .. }) |
407 Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
408 Expr::Block(ExprBlock { ref mut attrs, .. }) |
409 Expr::Assign(ExprAssign { ref mut attrs, .. }) |
410 Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
411 Expr::Field(ExprField { ref mut attrs, .. }) |
412 Expr::Index(ExprIndex { ref mut attrs, .. }) |
413 Expr::Range(ExprRange { ref mut attrs, .. }) |
414 Expr::Path(ExprPath { ref mut attrs, .. }) |
415 Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
416 Expr::Break(ExprBreak { ref mut attrs, .. }) |
417 Expr::Continue(ExprContinue { ref mut attrs, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500418 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500419 Expr::Macro(ExprMacro { ref mut attrs, .. }) |
420 Expr::Struct(ExprStruct { ref mut attrs, .. }) |
421 Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
422 Expr::Paren(ExprParen { ref mut attrs, .. }) |
423 Expr::Group(ExprGroup { ref mut attrs, .. }) |
424 Expr::Try(ExprTry { ref mut attrs, .. }) |
425 Expr::Catch(ExprCatch { ref mut attrs, .. }) |
426 Expr::Yield(ExprYield { ref mut attrs, .. }) => attrs,
427 }
428 }
429}
430
Michael Layzell734adb42017-06-07 16:58:31 -0400431#[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500432ast_enum! {
433 /// A struct or tuple struct field accessed in a struct literal or field
434 /// expression.
435 pub enum Member {
436 /// A named field like `self.x`.
437 Named(Ident),
438 /// An unnamed field like `self.0`.
439 Unnamed(Index),
440 }
441}
442
443#[cfg(feature = "full")]
444ast_struct! {
445 /// The index of an unnamed tuple struct field.
446 pub struct Index #manual_extra_traits {
447 pub index: u32,
448 pub span: Span,
449 }
450}
451
452#[cfg(feature = "full")]
453impl Eq for Index {}
454
455#[cfg(feature = "full")]
456impl PartialEq for Index {
457 fn eq(&self, other: &Self) -> bool {
458 self.index == other.index
459 }
460}
461
462#[cfg(feature = "full")]
463impl Hash for Index {
464 fn hash<H: Hasher>(&self, state: &mut H) {
465 self.index.hash(state);
466 }
467}
468
469#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700470ast_struct! {
471 /// A field-value pair in a struct literal.
472 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500473 /// Attributes tagged on the field.
474 pub attrs: Vec<Attribute>,
475
476 /// Name or index of the field.
477 pub member: Member,
478
479 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500480
Alex Crichton62a0a592017-05-22 13:58:53 -0700481 /// Value of the field.
482 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500483
Alex Crichton62a0a592017-05-22 13:58:53 -0700484 /// Whether this is a shorthand field, e.g. `Struct { x }`
485 /// instead of `Struct { x: x }`.
486 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700487 }
David Tolnay055a7042016-10-02 19:23:54 -0700488}
489
Michael Layzell734adb42017-06-07 16:58:31 -0400490#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700491ast_struct! {
492 /// A Block (`{ .. }`).
493 ///
494 /// E.g. `{ .. }` as in `fn foo() { .. }`
495 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500496 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 /// Statements in a block
498 pub stmts: Vec<Stmt>,
499 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700500}
501
Michael Layzell734adb42017-06-07 16:58:31 -0400502#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700503ast_enum! {
504 /// A statement, usually ending in a semicolon.
505 pub enum Stmt {
506 /// A local (let) binding.
507 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700508
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 /// An item definition.
510 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700511
Alex Crichton62a0a592017-05-22 13:58:53 -0700512 /// Expr without trailing semicolon.
513 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700514
Alex Crichton62a0a592017-05-22 13:58:53 -0700515 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700517 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700518}
519
Michael Layzell734adb42017-06-07 16:58:31 -0400520#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700521ast_struct! {
522 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
523 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500524 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800525 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700526 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500527 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800528 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500529 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700530 /// Initializer expression to set the value, if any
531 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500532 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700533 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700534}
535
Michael Layzell734adb42017-06-07 16:58:31 -0400536#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700537ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 // Clippy false positive
539 // https://github.com/Manishearth/rust-clippy/issues/1241
540 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
541 pub enum Pat {
542 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700543 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800544 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700545 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700546
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
548 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
549 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
550 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700551 pub Ident(PatIdent {
552 pub mode: BindingMode,
553 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800554 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500555 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700556 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700557
Alex Crichton62a0a592017-05-22 13:58:53 -0700558 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
559 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700560 pub Struct(PatStruct {
561 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500562 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500563 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800564 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700565 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700566
Alex Crichton62a0a592017-05-22 13:58:53 -0700567 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
568 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
569 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700570 pub TupleStruct(PatTupleStruct {
571 pub path: Path,
572 pub pat: PatTuple,
573 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700574
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 /// A possibly qualified path pattern.
576 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
577 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
578 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700579 pub Path(PatPath {
580 pub qself: Option<QSelf>,
581 pub path: Path,
582 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700583
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 /// A tuple pattern `(a, b)`.
585 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
586 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700587 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500588 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500589 pub pats: Delimited<Pat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500591 pub dots_pos: Option<usize>,
592 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700593 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700594 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800596 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500597 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700598 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700599 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700600 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800601 pub and_token: Token![&],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500602 pub mutbl: Mutability,
603 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700604 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700605 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700606 pub Lit(PatLit {
607 pub expr: Box<Expr>,
608 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800609 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700610 pub Range(PatRange {
611 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700612 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500613 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700614 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400615 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700616 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500617 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800618 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700619 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500621 pub dot2_token: Option<Token![..]>,
622 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700623 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700624 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800625 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700627}
628
Michael Layzell734adb42017-06-07 16:58:31 -0400629#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700630ast_struct! {
631 /// An arm of a 'match'.
632 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800633 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700634 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500635 /// ```rust
636 /// # #![feature(dotdoteq_in_patterns)]
637 /// #
638 /// # fn main() {
639 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700640 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500641 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700642 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500643 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500645 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700646 /// ```
647 pub struct Arm {
648 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800649 pub pats: Delimited<Pat, Token![|]>,
650 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700651 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800652 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800654 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700655 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700656}
657
Michael Layzell734adb42017-06-07 16:58:31 -0400658#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700659ast_enum! {
660 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700661 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700662 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800663 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700664 Ref,
665 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700666}
667
Michael Layzell734adb42017-06-07 16:58:31 -0400668#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700669ast_enum! {
670 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700671 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700672 pub enum RangeLimits {
673 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800674 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700675 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800676 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700677 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700678}
679
Michael Layzell734adb42017-06-07 16:58:31 -0400680#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700681ast_struct! {
682 /// A single field in a struct pattern
683 ///
684 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
685 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
686 /// except `is_shorthand` is true
687 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500688 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700689 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500690 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500691 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 /// The pattern the field is destructured to
693 pub pat: Box<Pat>,
694 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700696}
697
Michael Layzell734adb42017-06-07 16:58:31 -0400698#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700699ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700700 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700701 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800702 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 ByValue(Mutability),
704 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700705}
706
Michael Layzell3936ceb2017-07-08 00:28:36 -0400707#[cfg(any(feature = "parsing", feature = "printing"))]
708#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700709fn arm_expr_requires_comma(expr: &Expr) -> bool {
710 // see https://github.com/rust-lang/rust/blob/eb8f2586e
711 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500712 match *expr {
713 Expr::Unsafe(..)
714 | Expr::Block(..)
715 | Expr::If(..)
716 | Expr::IfLet(..)
717 | Expr::Match(..)
718 | Expr::While(..)
719 | Expr::WhileLet(..)
720 | Expr::Loop(..)
721 | Expr::ForLoop(..)
722 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700723 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400724 }
725}
726
David Tolnayb9c8e322016-09-23 20:48:37 -0700727#[cfg(feature = "parsing")]
728pub mod parsing {
729 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700730 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700731
Michael Layzell734adb42017-06-07 16:58:31 -0400732 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500733 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500734 use synom::Synom;
735 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400736 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500737 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500738 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700739
David Tolnaybcf26022017-12-25 22:10:52 -0500740 // When we're parsing expressions which occur before blocks, like in an if
741 // statement's condition, we cannot parse a struct literal.
742 //
743 // Struct literals are ambiguous in certain positions
744 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700745 macro_rules! ambiguous_expr {
746 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700747 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700748 };
749 }
750
David Tolnaybcf26022017-12-25 22:10:52 -0500751 // When we are parsing an optional suffix expression, we cannot allow blocks
752 // if structs are not allowed.
753 //
754 // Example:
755 //
756 // if break {} {}
757 //
758 // is ambiguous between:
759 //
760 // if (break {}) {}
761 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400762 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400763 macro_rules! opt_ambiguous_expr {
764 ($i:expr, $allow_struct:ident) => {
765 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
766 };
767 }
768
Alex Crichton954046c2017-05-30 21:49:42 -0700769 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400770 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700771
772 fn description() -> Option<&'static str> {
773 Some("expression")
774 }
775 }
776
Michael Layzell734adb42017-06-07 16:58:31 -0400777 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700778 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
779
David Tolnaybcf26022017-12-25 22:10:52 -0500780 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400781 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500782 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500783 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400784 }
785
Michael Layzell734adb42017-06-07 16:58:31 -0400786 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500787 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500788 // NOTE: We intentionally skip assign_expr, placement_expr, and
789 // range_expr, as they are not parsed in non-full mode.
790 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400791 }
792
David Tolnaybcf26022017-12-25 22:10:52 -0500793 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400794 macro_rules! binop {
795 (
796 $name: ident,
797 $next: ident,
798 $submac: ident!( $($args:tt)* )
799 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500800 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400801 mut e: call!($next, allow_struct, allow_block) >>
802 many0!(do_parse!(
803 op: $submac!($($args)*) >>
804 rhs: call!($next, allow_struct, true) >>
805 ({
806 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500807 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400808 left: Box::new(e.into()),
809 op: op,
810 right: Box::new(rhs.into()),
811 }.into();
812 })
813 )) >>
814 (e)
815 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700816 }
David Tolnay54e854d2016-10-24 12:03:30 -0700817 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700818
David Tolnaybcf26022017-12-25 22:10:52 -0500819 // <placement> = <placement> ..
820 // <placement> += <placement> ..
821 // <placement> -= <placement> ..
822 // <placement> *= <placement> ..
823 // <placement> /= <placement> ..
824 // <placement> %= <placement> ..
825 // <placement> ^= <placement> ..
826 // <placement> &= <placement> ..
827 // <placement> |= <placement> ..
828 // <placement> <<= <placement> ..
829 // <placement> >>= <placement> ..
830 //
831 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400832 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500833 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400834 mut e: call!(placement_expr, allow_struct, allow_block) >>
835 alt!(
836 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800837 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400838 // Recurse into self to parse right-associative operator.
839 rhs: call!(assign_expr, allow_struct, true) >>
840 ({
841 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500842 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400843 left: Box::new(e.into()),
844 eq_token: eq,
845 right: Box::new(rhs.into()),
846 }.into();
847 })
848 )
849 |
850 do_parse!(
851 op: call!(BinOp::parse_assign_op) >>
852 // Recurse into self to parse right-associative operator.
853 rhs: call!(assign_expr, allow_struct, true) >>
854 ({
855 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500856 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400857 left: Box::new(e.into()),
858 op: op,
859 right: Box::new(rhs.into()),
860 }.into();
861 })
862 )
863 |
864 epsilon!()
865 ) >>
866 (e)
867 ));
868
David Tolnaybcf26022017-12-25 22:10:52 -0500869 // <range> <- <range> ..
870 //
871 // NOTE: The `in place { expr }` version of this syntax is parsed in
872 // `atom_expr`, not here.
873 //
874 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400875 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500876 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400877 mut e: call!(range_expr, allow_struct, allow_block) >>
878 alt!(
879 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800880 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400881 // Recurse into self to parse right-associative operator.
882 rhs: call!(placement_expr, allow_struct, true) >>
883 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400884 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500885 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400886 // op: BinOp::Place(larrow),
887 place: Box::new(e.into()),
David Tolnay8701a5c2017-12-28 23:31:10 -0500888 arrow_token: arrow,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400889 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400890 }.into();
891 })
892 )
893 |
894 epsilon!()
895 ) >>
896 (e)
897 ));
898
David Tolnaybcf26022017-12-25 22:10:52 -0500899 // <or> ... <or> ..
900 // <or> .. <or> ..
901 // <or> ..
902 //
903 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
904 // rules are for parsing these expressions are, but this is not correct.
905 // For example, `a .. b .. c` is not a legal expression. It should not
906 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
907 //
908 // NOTE: The form of ranges which don't include a preceding expression are
909 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400910 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500911 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400912 mut e: call!(or_expr, allow_struct, allow_block) >>
913 many0!(do_parse!(
914 limits: syn!(RangeLimits) >>
915 // We don't want to allow blocks here if we don't allow structs. See
916 // the reasoning for `opt_ambiguous_expr!` above.
917 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
918 ({
919 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500920 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400921 from: Some(Box::new(e.into())),
922 limits: limits,
923 to: hi.map(|e| Box::new(e.into())),
924 }.into();
925 })
926 )) >>
927 (e)
928 ));
929
David Tolnaybcf26022017-12-25 22:10:52 -0500930 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800931 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400932
David Tolnaybcf26022017-12-25 22:10:52 -0500933 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800934 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400935
David Tolnaybcf26022017-12-25 22:10:52 -0500936 // <bitor> == <bitor> ...
937 // <bitor> != <bitor> ...
938 // <bitor> >= <bitor> ...
939 // <bitor> <= <bitor> ...
940 // <bitor> > <bitor> ...
941 // <bitor> < <bitor> ...
942 //
943 // NOTE: This operator appears to be parsed as left-associative, but errors
944 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500945 binop!(
946 compare_expr,
947 bitor_expr,
948 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800949 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400950 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800951 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952 |
953 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800954 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400955 |
956 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400958 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400959 do_parse!(
960 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800961 not!(punct!(<-)) >>
962 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400963 (BinOp::Lt(t))
964 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400965 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800966 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500967 )
968 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400969
David Tolnaybcf26022017-12-25 22:10:52 -0500970 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500971 binop!(
972 bitor_expr,
973 bitxor_expr,
974 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
975 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400976
David Tolnaybcf26022017-12-25 22:10:52 -0500977 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500978 binop!(
979 bitxor_expr,
980 bitand_expr,
981 do_parse!(
982 // NOTE: Make sure we aren't looking at ^=.
983 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
984 )
985 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400986
David Tolnaybcf26022017-12-25 22:10:52 -0500987 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500988 binop!(
989 bitand_expr,
990 shift_expr,
991 do_parse!(
992 // NOTE: Make sure we aren't looking at && or &=.
993 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
994 )
995 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400996
David Tolnaybcf26022017-12-25 22:10:52 -0500997 // <arith> << <arith> ...
998 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -0500999 binop!(
1000 shift_expr,
1001 arith_expr,
1002 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001003 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001005 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001006 )
1007 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001008
David Tolnaybcf26022017-12-25 22:10:52 -05001009 // <term> + <term> ...
1010 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001011 binop!(
1012 arith_expr,
1013 term_expr,
1014 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001018 )
1019 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001020
David Tolnaybcf26022017-12-25 22:10:52 -05001021 // <cast> * <cast> ...
1022 // <cast> / <cast> ...
1023 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001024 binop!(
1025 term_expr,
1026 cast_expr,
1027 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001028 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001029 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001030 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001031 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001033 )
1034 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001035
David Tolnaybcf26022017-12-25 22:10:52 -05001036 // <unary> as <ty>
1037 // <unary> : <ty>
David Tolnay8c91b882017-12-28 23:04:32 -05001038 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001039 mut e: call!(unary_expr, allow_struct, allow_block) >>
1040 many0!(alt!(
1041 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001042 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001043 // We can't accept `A + B` in cast expressions, as it's
1044 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001045 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001046 ({
1047 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001048 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001049 expr: Box::new(e.into()),
1050 as_token: as_,
1051 ty: Box::new(ty),
1052 }.into();
1053 })
1054 )
1055 |
1056 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001057 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001058 // We can't accept `A + B` in cast expressions, as it's
1059 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001060 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001061 ({
1062 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001063 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001064 expr: Box::new(e.into()),
1065 colon_token: colon,
1066 ty: Box::new(ty),
1067 }.into();
1068 })
1069 )
1070 )) >>
1071 (e)
1072 ));
1073
David Tolnaybcf26022017-12-25 22:10:52 -05001074 // <UnOp> <trailer>
1075 // & <trailer>
1076 // &mut <trailer>
1077 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001078 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001079 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001080 do_parse!(
1081 op: syn!(UnOp) >>
1082 expr: call!(unary_expr, allow_struct, true) >>
1083 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001084 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001085 op: op,
1086 expr: Box::new(expr.into()),
1087 }.into())
1088 )
1089 |
1090 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001091 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001092 mutability: syn!(Mutability) >>
1093 expr: call!(unary_expr, allow_struct, true) >>
1094 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001095 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001096 and_token: and,
1097 mutbl: mutability,
1098 expr: Box::new(expr.into()),
1099 }.into())
1100 )
1101 |
1102 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001103 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001104 expr: call!(unary_expr, allow_struct, true) >>
1105 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001106 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001107 box_token: box_,
1108 expr: Box::new(expr.into()),
1109 }.into())
1110 )
1111 |
1112 call!(trailer_expr, allow_struct, allow_block)
1113 ));
1114
Michael Layzell734adb42017-06-07 16:58:31 -04001115 // XXX: This duplication is ugly
1116 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001117 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001118 do_parse!(
1119 op: syn!(UnOp) >>
1120 expr: call!(unary_expr, allow_struct, true) >>
1121 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001122 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001123 op: op,
1124 expr: Box::new(expr.into()),
1125 }.into())
1126 )
1127 |
1128 call!(trailer_expr, allow_struct, allow_block)
1129 ));
1130
David Tolnaybcf26022017-12-25 22:10:52 -05001131 // <atom> (..<args>) ...
1132 // <atom> . <ident> (..<args>) ...
1133 // <atom> . <ident> ...
1134 // <atom> . <lit> ...
1135 // <atom> [ <expr> ] ...
1136 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001137 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001138 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001139 mut e: call!(atom_expr, allow_struct, allow_block) >>
1140 many0!(alt!(
1141 tap!(args: and_call => {
1142 let (args, paren) = args;
1143 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001144 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001145 func: Box::new(e.into()),
1146 args: args,
1147 paren_token: paren,
1148 }.into();
1149 })
1150 |
1151 tap!(more: and_method_call => {
1152 let mut call = more;
1153 call.expr = Box::new(e.into());
1154 e = call.into();
1155 })
1156 |
1157 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001158 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001159 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001160 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001161 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001162 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001163 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001164 }.into();
1165 })
1166 |
1167 tap!(i: and_index => {
1168 let (i, token) = i;
1169 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001170 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001171 expr: Box::new(e.into()),
1172 bracket_token: token,
1173 index: Box::new(i),
1174 }.into();
1175 })
1176 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001177 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001178 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001179 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001180 expr: Box::new(e.into()),
1181 question_token: question,
1182 }.into();
1183 })
1184 )) >>
1185 (e)
1186 ));
1187
Michael Layzell734adb42017-06-07 16:58:31 -04001188 // XXX: Duplication == ugly
1189 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001190 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001191 mut e: call!(atom_expr, allow_struct, allow_block) >>
1192 many0!(alt!(
1193 tap!(args: and_call => {
1194 let (args, paren) = args;
1195 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001196 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001197 func: Box::new(e.into()),
1198 args: args,
1199 paren_token: paren,
1200 }.into();
1201 })
1202 |
1203 tap!(i: and_index => {
1204 let (i, token) = i;
1205 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001206 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001207 expr: Box::new(e.into()),
1208 bracket_token: token,
1209 index: Box::new(i),
1210 }.into();
1211 })
1212 )) >>
1213 (e)
1214 ));
1215
David Tolnaybcf26022017-12-25 22:10:52 -05001216 // Parse all atomic expressions which don't have to worry about precidence
1217 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001218 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001219 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1220 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001221 |
David Tolnay8c91b882017-12-28 23:04:32 -05001222 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001223 |
1224 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001225 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001226 |
David Tolnay8c91b882017-12-28 23:04:32 -05001227 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001228 |
David Tolnay8c91b882017-12-28 23:04:32 -05001229 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001230 |
1231 call!(expr_break, allow_struct) // must be before expr_path
1232 |
David Tolnay8c91b882017-12-28 23:04:32 -05001233 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001234 |
1235 call!(expr_ret, allow_struct) // must be before expr_path
1236 |
David Tolnay8c91b882017-12-28 23:04:32 -05001237 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001238 |
David Tolnay8c91b882017-12-28 23:04:32 -05001239 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001240 |
David Tolnay8c91b882017-12-28 23:04:32 -05001241 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 |
David Tolnay8c91b882017-12-28 23:04:32 -05001243 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001244 |
David Tolnay8c91b882017-12-28 23:04:32 -05001245 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001246 |
David Tolnay8c91b882017-12-28 23:04:32 -05001247 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 |
David Tolnay8c91b882017-12-28 23:04:32 -05001249 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 |
David Tolnay8c91b882017-12-28 23:04:32 -05001251 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 |
David Tolnay8c91b882017-12-28 23:04:32 -05001253 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001254 |
David Tolnay8c91b882017-12-28 23:04:32 -05001255 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 |
David Tolnay8c91b882017-12-28 23:04:32 -05001257 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001258 |
David Tolnay8c91b882017-12-28 23:04:32 -05001259 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001260 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001261 call!(expr_closure, allow_struct)
1262 |
David Tolnay8c91b882017-12-28 23:04:32 -05001263 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001264 |
1265 // NOTE: This is the prefix-form of range
1266 call!(expr_range, allow_struct)
1267 |
David Tolnay8c91b882017-12-28 23:04:32 -05001268 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001269 |
David Tolnay8c91b882017-12-28 23:04:32 -05001270 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001271 ));
1272
Michael Layzell734adb42017-06-07 16:58:31 -04001273 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001274 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001275 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001276 |
David Tolnay8c91b882017-12-28 23:04:32 -05001277 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001278 ));
1279
Michael Layzell734adb42017-06-07 16:58:31 -04001280 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001281 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001282 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001283 |
David Tolnay8c91b882017-12-28 23:04:32 -05001284 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001285 |
David Tolnay8c91b882017-12-28 23:04:32 -05001286 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001287 |
David Tolnay8c91b882017-12-28 23:04:32 -05001288 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001289 |
David Tolnay8c91b882017-12-28 23:04:32 -05001290 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001291 |
David Tolnay8c91b882017-12-28 23:04:32 -05001292 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001293 |
David Tolnay8c91b882017-12-28 23:04:32 -05001294 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001295 |
David Tolnay8c91b882017-12-28 23:04:32 -05001296 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001297 |
David Tolnay8c91b882017-12-28 23:04:32 -05001298 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001299 |
David Tolnay8c91b882017-12-28 23:04:32 -05001300 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001301 |
David Tolnay8c91b882017-12-28 23:04:32 -05001302 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001303 ), Expr::from));
1304
David Tolnay8c91b882017-12-28 23:04:32 -05001305 impl Synom for ExprLit {
1306 named!(parse -> Self, do_parse!(
1307 lit: syn!(Lit) >>
1308 (ExprLit {
1309 attrs: Vec::new(),
1310 lit: lit,
1311 })
1312 ));
1313 }
1314
1315 #[cfg(feature = "full")]
1316 impl Synom for ExprMacro {
1317 named!(parse -> Self, do_parse!(
1318 mac: syn!(Macro) >>
1319 (ExprMacro {
1320 attrs: Vec::new(),
1321 mac: mac,
1322 })
1323 ));
1324 }
1325
David Tolnaye98775f2017-12-28 23:17:00 -05001326 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001327 impl Synom for ExprGroup {
1328 named!(parse -> Self, do_parse!(
1329 e: grouped!(syn!(Expr)) >>
1330 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001331 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001332 expr: Box::new(e.0),
1333 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001334 })
Michael Layzell93c36282017-06-04 20:43:14 -04001335 ));
1336 }
1337
David Tolnaye98775f2017-12-28 23:17:00 -05001338 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001339 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001340 named!(parse -> Self, do_parse!(
1341 e: parens!(syn!(Expr)) >>
1342 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001343 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001344 expr: Box::new(e.0),
1345 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001346 })
Michael Layzell92639a52017-06-01 00:07:44 -04001347 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001348 }
David Tolnay89e05672016-10-02 14:39:42 -07001349
Michael Layzell734adb42017-06-07 16:58:31 -04001350 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001351 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001352 named!(parse -> Self, do_parse!(
1353 elems: brackets!(call!(Delimited::parse_terminated)) >>
1354 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001355 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001356 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001357 bracket_token: elems.1,
1358 })
1359 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001360 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001361
David Tolnay32954ef2017-12-26 22:43:16 -05001362 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001363 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001364
Michael Layzell734adb42017-06-07 16:58:31 -04001365 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001366 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001367 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001368 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001369 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001370 colon2: punct!(::) >>
1371 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001372 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001373 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001374 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001375 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001376 args: parens!(call!(Delimited::parse_terminated)) >>
1377 ({
1378 let (colon2, lt, tys, gt) = match typarams {
1379 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1380 None => (None, None, None, None),
1381 };
1382 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001383 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001384 // this expr will get overwritten after being returned
David Tolnay8c91b882017-12-28 23:04:32 -05001385 expr: Box::new(Expr::Lit(ExprLit {
1386 attrs: Vec::new(),
1387 lit: Lit {
1388 span: Span::default(),
1389 value: LitKind::Bool(false),
1390 },
Alex Crichton954046c2017-05-30 21:49:42 -07001391 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001392
Alex Crichton954046c2017-05-30 21:49:42 -07001393 method: method,
1394 args: args.0,
1395 paren_token: args.1,
1396 dot_token: dot,
1397 lt_token: lt,
1398 gt_token: gt,
1399 colon2_token: colon2,
1400 typarams: tys.unwrap_or_default(),
1401 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001402 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001403 ));
1404
Michael Layzell734adb42017-06-07 16:58:31 -04001405 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001406 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001407 named!(parse -> Self, do_parse!(
1408 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001409 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001410 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001411 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001412 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001413 })
1414 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001415 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001416
Michael Layzell734adb42017-06-07 16:58:31 -04001417 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001418 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001419 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001420 if_: keyword!(if) >>
1421 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001422 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001423 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001424 cond: expr_no_struct >>
1425 then_block: braces!(call!(Block::parse_within)) >>
1426 else_block: option!(else_block) >>
1427 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001428 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001429 pat: Box::new(pat),
1430 let_token: let_,
1431 eq_token: eq,
1432 expr: Box::new(cond),
1433 if_true: Block {
1434 stmts: then_block.0,
1435 brace_token: then_block.1,
1436 },
1437 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001438 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001439 if_false: else_block.map(|p| Box::new(p.1.into())),
1440 })
1441 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001442 }
1443
Michael Layzell734adb42017-06-07 16:58:31 -04001444 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001445 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001446 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001447 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001448 cond: expr_no_struct >>
1449 then_block: braces!(call!(Block::parse_within)) >>
1450 else_block: option!(else_block) >>
1451 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001452 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001453 cond: Box::new(cond),
1454 if_true: Block {
1455 stmts: then_block.0,
1456 brace_token: then_block.1,
1457 },
1458 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001459 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001460 if_false: else_block.map(|p| Box::new(p.1.into())),
1461 })
1462 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001463 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001464
Michael Layzell734adb42017-06-07 16:58:31 -04001465 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001466 named!(else_block -> (Token![else], Expr), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001467 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001468 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001469 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001470 |
David Tolnay8c91b882017-12-28 23:04:32 -05001471 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001472 |
1473 do_parse!(
1474 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001475 (Expr::Block(ExprBlock {
1476 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001477 block: Block {
1478 stmts: else_block.0,
1479 brace_token: else_block.1,
1480 },
1481 }))
David Tolnay939766a2016-09-23 23:48:12 -07001482 )
Alex Crichton954046c2017-05-30 21:49:42 -07001483 ) >>
1484 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001485 ));
1486
Michael Layzell734adb42017-06-07 16:58:31 -04001487 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001488 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001489 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001490 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1491 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001492 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001493 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001494 expr: expr_no_struct >>
1495 loop_block: syn!(Block) >>
1496 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001497 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001498 for_token: for_,
1499 in_token: in_,
1500 pat: Box::new(pat),
1501 expr: Box::new(expr),
1502 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001503 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001504 label: lbl.map(|p| p.0),
1505 })
1506 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001507 }
Gregory Katze5f35682016-09-27 14:20:55 -04001508
Michael Layzell734adb42017-06-07 16:58:31 -04001509 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001510 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001511 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001512 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1513 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001514 loop_block: syn!(Block) >>
1515 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001516 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001517 loop_token: loop_,
1518 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001519 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001520 label: lbl.map(|p| p.0),
1521 })
1522 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001523 }
1524
Michael Layzell734adb42017-06-07 16:58:31 -04001525 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001526 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001527 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001528 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001529 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001530 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001531 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001532 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001533 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001534 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001535 expr: Box::new(obj),
1536 match_token: match_,
1537 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001538 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001539 }
1540 })
1541 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001542 }
David Tolnay1978c672016-10-27 22:05:52 -07001543
Michael Layzell734adb42017-06-07 16:58:31 -04001544 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001545 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001546 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001547 do_: keyword!(do) >>
1548 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001549 catch_block: syn!(Block) >>
1550 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001551 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001552 block: catch_block,
1553 do_token: do_,
1554 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001555 })
Michael Layzell92639a52017-06-01 00:07:44 -04001556 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001557 }
Arnavion02ef13f2017-04-25 00:54:31 -07001558
Michael Layzell734adb42017-06-07 16:58:31 -04001559 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001560 impl Synom for ExprYield {
1561 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001562 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001563 expr: option!(syn!(Expr)) >>
1564 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001565 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001566 yield_token: yield_,
1567 expr: expr.map(Box::new),
1568 })
1569 ));
1570 }
1571
1572 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001573 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001574 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001575 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001576 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001577 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1578 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001579 body: do_parse!(
1580 expr: alt!(expr_nosemi | syn!(Expr)) >>
1581 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1582 map!(input_end!(), |_| None)
1583 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001584 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001585 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001586 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001587 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001588 ) >>
1589 (Arm {
1590 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001591 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001592 attrs: attrs,
1593 pats: pats,
1594 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001595 body: Box::new(body.0),
1596 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001597 })
1598 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001599 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001600
Michael Layzell734adb42017-06-07 16:58:31 -04001601 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001602 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001603 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001604 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001605 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001606 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001607 ret_and_body: alt!(
1608 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001609 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001610 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001611 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001612 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001613 Expr::Block(ExprBlock {
1614 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001615 block: body,
1616 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001617 )
1618 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001619 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001620 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001621 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001622 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001623 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001624 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001625 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001626 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001627 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001628 body: Box::new(ret_and_body.1),
1629 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001630 ));
1631
Michael Layzell734adb42017-06-07 16:58:31 -04001632 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001633 named!(fn_arg -> FnArg, do_parse!(
1634 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001635 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001636 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001637 if let Some((colon, ty)) = ty {
1638 FnArg::Captured(ArgCaptured {
1639 pat: pat,
1640 colon_token: colon,
1641 ty: ty,
1642 })
1643 } else {
1644 FnArg::Inferred(pat)
1645 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001646 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001647 ));
1648
Michael Layzell734adb42017-06-07 16:58:31 -04001649 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001650 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001651 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001652 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1653 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001654 cond: expr_no_struct >>
1655 while_block: syn!(Block) >>
1656 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001657 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001658 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001659 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001660 cond: Box::new(cond),
1661 body: while_block,
1662 label: lbl.map(|p| p.0),
1663 })
1664 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001665 }
1666
Michael Layzell734adb42017-06-07 16:58:31 -04001667 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001668 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001669 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001670 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1671 while_: keyword!(while) >>
1672 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001673 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001674 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001675 value: expr_no_struct >>
1676 while_block: syn!(Block) >>
1677 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001678 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001679 eq_token: eq,
1680 let_token: let_,
1681 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001682 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001683 pat: Box::new(pat),
1684 expr: Box::new(value),
1685 body: while_block,
1686 label: lbl.map(|p| p.0),
1687 })
1688 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001689 }
1690
Michael Layzell734adb42017-06-07 16:58:31 -04001691 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001692 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001693 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001694 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001695 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001696 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001697 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001698 continue_token: cont,
1699 label: lbl,
1700 })
1701 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001702 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001703
Michael Layzell734adb42017-06-07 16:58:31 -04001704 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001705 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001706 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001707 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001708 // We can't allow blocks after a `break` expression when we wouldn't
1709 // allow structs, as this expression is ambiguous.
1710 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001711 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001712 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001713 label: lbl,
1714 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001715 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001716 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001717 ));
1718
Michael Layzell734adb42017-06-07 16:58:31 -04001719 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001720 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001721 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001722 // NOTE: return is greedy and eats blocks after it even when in a
1723 // position where structs are not allowed, such as in if statement
1724 // conditions. For example:
1725 //
David Tolnaybcf26022017-12-25 22:10:52 -05001726 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001727 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001728 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001729 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001730 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001731 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001732 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001733 ));
1734
Michael Layzell734adb42017-06-07 16:58:31 -04001735 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001736 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001737 named!(parse -> Self, do_parse!(
1738 path: syn!(Path) >>
1739 data: braces!(do_parse!(
1740 fields: call!(Delimited::parse_terminated) >>
1741 base: option!(
1742 cond!(fields.is_empty() || fields.trailing_delim(),
1743 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001744 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001745 base: syn!(Expr) >>
1746 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001747 )
Michael Layzell92639a52017-06-01 00:07:44 -04001748 )
1749 ) >>
1750 (fields, base)
1751 )) >>
1752 ({
1753 let ((fields, base), brace) = data;
1754 let (dots, rest) = match base.and_then(|b| b) {
1755 Some((dots, base)) => (Some(dots), Some(base)),
1756 None => (None, None),
1757 };
1758 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001759 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001760 brace_token: brace,
1761 path: path,
1762 fields: fields,
1763 dot2_token: dots,
1764 rest: rest.map(Box::new),
1765 }
1766 })
1767 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001768 }
1769
Michael Layzell734adb42017-06-07 16:58:31 -04001770 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001771 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001772 named!(parse -> Self, alt!(
1773 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001774 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001775 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001776 value: syn!(Expr) >>
1777 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001778 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001779 expr: value,
1780 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001781 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001782 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001783 })
Michael Layzell92639a52017-06-01 00:07:44 -04001784 )
1785 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001786 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001787 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001788 expr: Expr::Path(ExprPath {
1789 attrs: Vec::new(),
1790 qself: None,
1791 path: name.into(),
1792 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001793 is_shorthand: true,
1794 attrs: Vec::new(),
1795 colon_token: None,
1796 })
1797 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001798 }
David Tolnay055a7042016-10-02 19:23:54 -07001799
Michael Layzell734adb42017-06-07 16:58:31 -04001800 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001801 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001802 named!(parse -> Self, do_parse!(
1803 data: brackets!(do_parse!(
1804 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001805 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001806 times: syn!(Expr) >>
1807 (value, semi, times)
1808 )) >>
1809 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001810 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001811 expr: Box::new((data.0).0),
1812 amt: Box::new((data.0).2),
1813 bracket_token: data.1,
1814 semi_token: (data.0).1,
1815 })
1816 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001817 }
David Tolnay055a7042016-10-02 19:23:54 -07001818
Michael Layzell734adb42017-06-07 16:58:31 -04001819 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001820 impl Synom for ExprUnsafe {
1821 named!(parse -> Self, do_parse!(
1822 unsafe_: keyword!(unsafe) >>
1823 b: syn!(Block) >>
1824 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001825 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001826 unsafe_token: unsafe_,
1827 block: b,
1828 })
1829 ));
1830 }
1831
1832 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001833 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001834 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001835 b: syn!(Block) >>
1836 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001837 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001838 block: b,
1839 })
1840 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001841 }
David Tolnay89e05672016-10-02 14:39:42 -07001842
Michael Layzell734adb42017-06-07 16:58:31 -04001843 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001844 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001845 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001846 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001847 (ExprRange {
1848 attrs: Vec::new(),
1849 from: None,
1850 to: hi.map(Box::new),
1851 limits: limits,
1852 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001853 ));
1854
Michael Layzell734adb42017-06-07 16:58:31 -04001855 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001856 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001857 named!(parse -> Self, alt!(
1858 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001859 punct!(..=) => { RangeLimits::Closed }
1860 |
1861 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001862 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001863 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001864 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001865 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001866 }
David Tolnay438c9052016-10-07 23:24:48 -07001867
Alex Crichton954046c2017-05-30 21:49:42 -07001868 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001869 named!(parse -> Self, do_parse!(
1870 pair: qpath >>
1871 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001872 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001873 qself: pair.0,
1874 path: pair.1,
1875 })
1876 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001877 }
David Tolnay42602292016-10-01 22:25:45 -07001878
Michael Layzell734adb42017-06-07 16:58:31 -04001879 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001880 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001881
David Tolnay32954ef2017-12-26 22:43:16 -05001882 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001883
Michael Layzell734adb42017-06-07 16:58:31 -04001884 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001885 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001886 named!(parse -> Self, do_parse!(
1887 stmts: braces!(call!(Block::parse_within)) >>
1888 (Block {
1889 stmts: stmts.0,
1890 brace_token: stmts.1,
1891 })
1892 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001893 }
David Tolnay939766a2016-09-23 23:48:12 -07001894
Michael Layzell734adb42017-06-07 16:58:31 -04001895 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001896 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001897 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001898 many0!(punct!(;)) >>
1899 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001900 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001901 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001902 mut e: syn!(Expr) >>
1903 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001904 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001905 Stmt::Expr(Box::new(e))
1906 })
1907 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001908 (match last {
1909 None => standalone,
1910 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001911 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001912 standalone
1913 }
1914 })
1915 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001916 }
1917
Michael Layzell734adb42017-06-07 16:58:31 -04001918 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001919 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001920 named!(parse -> Self, alt!(
1921 stmt_mac
1922 |
1923 stmt_local
1924 |
1925 stmt_item
1926 |
Michael Layzell35418782017-06-07 09:20:25 -04001927 stmt_blockexpr
1928 |
Michael Layzell92639a52017-06-01 00:07:44 -04001929 stmt_expr
1930 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001931 }
David Tolnay939766a2016-09-23 23:48:12 -07001932
Michael Layzell734adb42017-06-07 16:58:31 -04001933 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001934 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001935 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001936 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001937 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001938 // Only parse braces here; paren and bracket will get parsed as
1939 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001940 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001941 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001942 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1943 attrs: attrs,
1944 ident: None,
1945 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001946 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001947 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001948 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001949 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001950 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001951 },
David Tolnayeea28d62016-10-25 20:44:08 -07001952 },
David Tolnay57b52bc2017-12-28 18:06:38 -05001953 semi_token: semi,
1954 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07001955 ));
1956
Michael Layzell734adb42017-06-07 16:58:31 -04001957 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001958 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001959 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001960 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001961 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001962 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001963 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1964 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001965 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001966 let_token: let_,
1967 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001968 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
1969 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001970 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001971 ty: ty.map(|p| Box::new(p.1)),
1972 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07001973 attrs: attrs,
1974 })))
1975 ));
1976
Michael Layzell734adb42017-06-07 16:58:31 -04001977 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001978 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07001979
Michael Layzell734adb42017-06-07 16:58:31 -04001980 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001981 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001982 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04001983 mut e: expr_nosemi >>
1984 // If the next token is a `.` or a `?` it is special-cased to parse as
1985 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08001986 not!(punct!(.)) >>
1987 not!(punct!(?)) >>
1988 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04001989 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001990 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04001991 if let Some(semi) = semi {
1992 Stmt::Semi(Box::new(e), semi)
1993 } else {
1994 Stmt::Expr(Box::new(e))
1995 }
1996 })
1997 ));
David Tolnaycfe55022016-10-02 22:02:27 -07001998
Michael Layzell734adb42017-06-07 16:58:31 -04001999 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002000 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002001 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002002 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002003 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002004 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002005 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002006 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002007 })
David Tolnay939766a2016-09-23 23:48:12 -07002008 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002009
Michael Layzell734adb42017-06-07 16:58:31 -04002010 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002011 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002012 named!(parse -> Self, alt!(
2013 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2014 |
2015 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2016 |
2017 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2018 |
2019 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2020 |
2021 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2022 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002023 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002024 |
2025 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2026 |
2027 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2028 |
2029 syn!(PatPath) => { Pat::Path }
2030 |
2031 syn!(PatTuple) => { Pat::Tuple }
2032 |
2033 syn!(PatRef) => { Pat::Ref }
2034 |
2035 syn!(PatSlice) => { Pat::Slice }
2036 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002037 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002038
Michael Layzell734adb42017-06-07 16:58:31 -04002039 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002040 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002041 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002042 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002043 |u| PatWild { underscore_token: u }
2044 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002045 }
David Tolnay84aa0752016-10-02 23:01:13 -07002046
Michael Layzell734adb42017-06-07 16:58:31 -04002047 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002048 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002049 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002050 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002051 pat: syn!(Pat) >>
2052 (PatBox {
2053 pat: Box::new(pat),
2054 box_token: boxed,
2055 })
2056 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002057 }
2058
Michael Layzell734adb42017-06-07 16:58:31 -04002059 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002060 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002061 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002062 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002063 mutability: syn!(Mutability) >>
2064 name: alt!(
2065 syn!(Ident)
2066 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002067 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002068 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002069 not!(punct!(<)) >>
2070 not!(punct!(::)) >>
2071 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002072 (PatIdent {
2073 mode: match mode {
2074 Some(mode) => BindingMode::ByRef(mode, mutability),
2075 None => BindingMode::ByValue(mutability),
2076 },
2077 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002078 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002079 subpat: subpat.map(|p| Box::new(p.1)),
2080 })
2081 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002082 }
2083
Michael Layzell734adb42017-06-07 16:58:31 -04002084 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002085 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002086 named!(parse -> Self, do_parse!(
2087 path: syn!(Path) >>
2088 tuple: syn!(PatTuple) >>
2089 (PatTupleStruct {
2090 path: path,
2091 pat: tuple,
2092 })
2093 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002094 }
2095
Michael Layzell734adb42017-06-07 16:58:31 -04002096 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002097 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002098 named!(parse -> Self, do_parse!(
2099 path: syn!(Path) >>
2100 data: braces!(do_parse!(
2101 fields: call!(Delimited::parse_terminated) >>
2102 base: option!(
2103 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002104 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002105 ) >>
2106 (fields, base)
2107 )) >>
2108 (PatStruct {
2109 path: path,
2110 fields: (data.0).0,
2111 brace_token: data.1,
2112 dot2_token: (data.0).1.and_then(|m| m),
2113 })
2114 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002115 }
2116
Michael Layzell734adb42017-06-07 16:58:31 -04002117 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002118 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002119 named!(parse -> Self, alt!(
2120 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002121 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002122 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002123 pat: syn!(Pat) >>
2124 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002125 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002126 pat: Box::new(pat),
2127 is_shorthand: false,
2128 attrs: Vec::new(),
2129 colon_token: Some(colon),
2130 })
2131 )
2132 |
2133 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002134 boxed: option!(keyword!(box)) >>
2135 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002136 mutability: syn!(Mutability) >>
2137 ident: syn!(Ident) >>
2138 ({
2139 let mut pat: Pat = PatIdent {
2140 mode: if let Some(mode) = mode {
2141 BindingMode::ByRef(mode, mutability)
2142 } else {
2143 BindingMode::ByValue(mutability)
2144 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002145 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002146 subpat: None,
2147 at_token: None,
2148 }.into();
2149 if let Some(boxed) = boxed {
2150 pat = PatBox {
2151 pat: Box::new(pat),
2152 box_token: boxed,
2153 }.into();
2154 }
2155 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002156 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002157 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002158 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002159 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002160 colon_token: None,
2161 }
2162 })
2163 )
2164 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002165 }
2166
Michael Layzell734adb42017-06-07 16:58:31 -04002167 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002168 impl Synom for Member {
2169 named!(parse -> Self, alt!(
2170 syn!(Ident) => { Member::Named }
2171 |
2172 syn!(Index) => { Member::Unnamed }
2173 ));
2174 }
2175
2176 #[cfg(feature = "full")]
2177 impl Synom for Index {
2178 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002179 lit: syn!(Lit) >>
2180 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002181 if let Ok(i) = lit.value.to_string().parse() {
2182 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002183 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002184 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002185 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002186 })
David Tolnay85b69a42017-12-27 20:43:10 -05002187 ));
2188 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002189
Michael Layzell734adb42017-06-07 16:58:31 -04002190 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002191 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002192 named!(parse -> Self, map!(
2193 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002194 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002195 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002196 }
David Tolnay9636c052016-10-02 17:11:17 -07002197
Michael Layzell734adb42017-06-07 16:58:31 -04002198 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002199 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002200 named!(parse -> Self, do_parse!(
2201 data: parens!(do_parse!(
2202 elems: call!(Delimited::parse_terminated) >>
2203 dotdot: map!(cond!(
2204 elems.is_empty() || elems.trailing_delim(),
2205 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002206 dots: punct!(..) >>
2207 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002208 (dots, trailing)
2209 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002210 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002211 rest: cond!(match dotdot {
2212 Some((_, Some(_))) => true,
2213 _ => false,
2214 },
2215 call!(Delimited::parse_terminated)) >>
2216 (elems, dotdot, rest)
2217 )) >>
2218 ({
2219 let ((mut elems, dotdot, rest), parens) = data;
2220 let (dotdot, trailing) = match dotdot {
2221 Some((a, b)) => (Some(a), Some(b)),
2222 None => (None, None),
2223 };
2224 PatTuple {
2225 paren_token: parens,
2226 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2227 dot2_token: dotdot,
2228 comma_token: trailing.and_then(|b| b),
2229 pats: {
2230 if let Some(rest) = rest {
2231 for elem in rest {
2232 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002233 }
Michael Layzell92639a52017-06-01 00:07:44 -04002234 }
2235 elems
2236 },
2237 }
2238 })
2239 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002240 }
David Tolnayfbb73232016-10-03 01:00:06 -07002241
Michael Layzell734adb42017-06-07 16:58:31 -04002242 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002243 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002244 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002245 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002246 mutability: syn!(Mutability) >>
2247 pat: syn!(Pat) >>
2248 (PatRef {
2249 pat: Box::new(pat),
2250 mutbl: mutability,
2251 and_token: and,
2252 })
2253 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002254 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002255
Michael Layzell734adb42017-06-07 16:58:31 -04002256 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002257 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002258 named!(parse -> Self, do_parse!(
2259 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002260 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002261 return parse_error(); // these need to be parsed by pat_path
2262 } else {
2263 PatLit {
2264 expr: Box::new(lit),
2265 }
2266 })
2267 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002268 }
David Tolnaye1310902016-10-29 23:40:00 -07002269
Michael Layzell734adb42017-06-07 16:58:31 -04002270 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002271 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002272 named!(parse -> Self, do_parse!(
2273 lo: pat_lit_expr >>
2274 limits: syn!(RangeLimits) >>
2275 hi: pat_lit_expr >>
2276 (PatRange {
2277 lo: Box::new(lo),
2278 hi: Box::new(hi),
2279 limits: limits,
2280 })
2281 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002282 }
David Tolnaye1310902016-10-29 23:40:00 -07002283
Michael Layzell734adb42017-06-07 16:58:31 -04002284 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002285 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002286 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002287 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002288 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002289 |
David Tolnay8c91b882017-12-28 23:04:32 -05002290 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002291 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002292 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002293 Expr::Unary(ExprUnary {
2294 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002295 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002296 expr: Box::new(v.into())
2297 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002298 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002299 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002300 })
2301 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002302
Michael Layzell734adb42017-06-07 16:58:31 -04002303 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002304 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002305 named!(parse -> Self, map!(
2306 brackets!(do_parse!(
2307 before: call!(Delimited::parse_terminated) >>
2308 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002309 dots: punct!(..) >>
2310 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002311 (dots, trailing)
2312 )) >>
2313 after: cond!(
2314 match middle {
2315 Some((_, ref trailing)) => trailing.is_some(),
2316 _ => false,
2317 },
2318 call!(Delimited::parse_terminated)
2319 ) >>
2320 (before, middle, after)
2321 )),
2322 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002323 let mut before: Delimited<Pat, Token![,]> = before;
2324 let after: Option<Delimited<Pat, Token![,]>> = after;
2325 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002326 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002327 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002328 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002329 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002330 }),
2331 bracket_token: brackets,
2332 middle: middle.and_then(|_| {
2333 if !before.is_empty() && !before.trailing_delim() {
2334 Some(Box::new(before.pop().unwrap().into_item()))
2335 } else {
2336 None
2337 }
2338 }),
2339 front: before,
2340 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002341 }
Alex Crichton954046c2017-05-30 21:49:42 -07002342 }
Michael Layzell92639a52017-06-01 00:07:44 -04002343 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002344 }
David Tolnay435a9a82016-10-29 13:47:20 -07002345
Michael Layzell734adb42017-06-07 16:58:31 -04002346 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002347 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002348 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002349 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002350 |
2351 epsilon!() => { |_| CaptureBy::Ref }
2352 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002353 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002354}
2355
David Tolnayf4bbbd92016-09-23 14:41:55 -07002356#[cfg(feature = "printing")]
2357mod printing {
2358 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002359 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002360 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002361 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002362 #[cfg(feature = "full")]
2363 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002364
David Tolnaybcf26022017-12-25 22:10:52 -05002365 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2366 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002367 #[cfg(feature = "full")]
2368 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002369 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002370 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002371 e.to_tokens(tokens);
2372 });
2373 } else {
2374 e.to_tokens(tokens);
2375 }
2376 }
2377
David Tolnay8c91b882017-12-28 23:04:32 -05002378 #[cfg(feature = "full")]
2379 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2380 tokens.append_all(attrs.outer());
2381 }
Michael Layzell734adb42017-06-07 16:58:31 -04002382
David Tolnay8c91b882017-12-28 23:04:32 -05002383 #[cfg(not(feature = "full"))]
2384 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002385 }
2386
Michael Layzell734adb42017-06-07 16:58:31 -04002387 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002388 impl ToTokens for ExprBox {
2389 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002390 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002391 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002392 self.expr.to_tokens(tokens);
2393 }
2394 }
2395
Michael Layzell734adb42017-06-07 16:58:31 -04002396 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002397 impl ToTokens for ExprInPlace {
2398 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002399 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002400 self.place.to_tokens(tokens);
2401 self.arrow_token.to_tokens(tokens);
2402 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002403 }
2404 }
2405
Michael Layzell734adb42017-06-07 16:58:31 -04002406 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002407 impl ToTokens for ExprArray {
2408 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002409 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002410 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002411 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002412 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002413 }
2414 }
2415
2416 impl ToTokens for ExprCall {
2417 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002418 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002419 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002420 self.paren_token.surround(tokens, |tokens| {
2421 self.args.to_tokens(tokens);
2422 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002423 }
2424 }
2425
Michael Layzell734adb42017-06-07 16:58:31 -04002426 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002427 impl ToTokens for ExprMethodCall {
2428 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002429 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002430 self.expr.to_tokens(tokens);
2431 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002432 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002433 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002434 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2435 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002436 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002437 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002438 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002439 self.paren_token.surround(tokens, |tokens| {
2440 self.args.to_tokens(tokens);
2441 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002442 }
2443 }
2444
Michael Layzell734adb42017-06-07 16:58:31 -04002445 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002446 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002447 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002448 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002449 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002450 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002451 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002452 // distinguish ExprTuple from ExprParen.
David Tolnay2a86fdd2017-12-28 23:34:28 -05002453 if self.elems.len() == 1 && !self.elems.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002454 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002455 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002456 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002457 }
2458 }
2459
2460 impl ToTokens for ExprBinary {
2461 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002462 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002463 self.left.to_tokens(tokens);
2464 self.op.to_tokens(tokens);
2465 self.right.to_tokens(tokens);
2466 }
2467 }
2468
2469 impl ToTokens for ExprUnary {
2470 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002471 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002472 self.op.to_tokens(tokens);
2473 self.expr.to_tokens(tokens);
2474 }
2475 }
2476
David Tolnay8c91b882017-12-28 23:04:32 -05002477 impl ToTokens for ExprLit {
2478 fn to_tokens(&self, tokens: &mut Tokens) {
2479 attrs_to_tokens(&self.attrs, tokens);
2480 self.lit.to_tokens(tokens);
2481 }
2482 }
2483
Alex Crichton62a0a592017-05-22 13:58:53 -07002484 impl ToTokens for ExprCast {
2485 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002486 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002487 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002488 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002489 self.ty.to_tokens(tokens);
2490 }
2491 }
2492
2493 impl ToTokens for ExprType {
2494 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002495 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002496 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002497 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002498 self.ty.to_tokens(tokens);
2499 }
2500 }
2501
Michael Layzell734adb42017-06-07 16:58:31 -04002502 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002503 fn maybe_wrap_else(
2504 tokens: &mut Tokens,
2505 else_token: &Option<Token![else]>,
2506 if_false: &Option<Box<Expr>>,
2507 ) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002508 if let Some(ref if_false) = *if_false {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002509 TokensOrDefault(else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002510
2511 // If we are not one of the valid expressions to exist in an else
2512 // clause, wrap ourselves in a block.
David Tolnay8c91b882017-12-28 23:04:32 -05002513 match **if_false {
2514 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002515 if_false.to_tokens(tokens);
2516 }
2517 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002518 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002519 if_false.to_tokens(tokens);
2520 });
2521 }
2522 }
2523 }
2524 }
2525
2526 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002527 impl ToTokens for ExprIf {
2528 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002529 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002530 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002531 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002532 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002533 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002534 }
2535 }
2536
Michael Layzell734adb42017-06-07 16:58:31 -04002537 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002538 impl ToTokens for ExprIfLet {
2539 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002540 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002541 self.if_token.to_tokens(tokens);
2542 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002543 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002544 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002545 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002546 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002547 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002548 }
2549 }
2550
Michael Layzell734adb42017-06-07 16:58:31 -04002551 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002552 impl ToTokens for ExprWhile {
2553 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002554 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002555 if self.label.is_some() {
2556 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002557 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002558 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002559 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002560 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002561 self.body.to_tokens(tokens);
2562 }
2563 }
2564
Michael Layzell734adb42017-06-07 16:58:31 -04002565 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002566 impl ToTokens for ExprWhileLet {
2567 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002568 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002569 if self.label.is_some() {
2570 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002571 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002572 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002573 self.while_token.to_tokens(tokens);
2574 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002575 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002576 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002577 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002578 self.body.to_tokens(tokens);
2579 }
2580 }
2581
Michael Layzell734adb42017-06-07 16:58:31 -04002582 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002583 impl ToTokens for ExprForLoop {
2584 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002585 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002586 if self.label.is_some() {
2587 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002588 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002589 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002590 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002591 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002592 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002593 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002594 self.body.to_tokens(tokens);
2595 }
2596 }
2597
Michael Layzell734adb42017-06-07 16:58:31 -04002598 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002599 impl ToTokens for ExprLoop {
2600 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002601 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002602 if self.label.is_some() {
2603 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002604 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002605 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002606 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002607 self.body.to_tokens(tokens);
2608 }
2609 }
2610
Michael Layzell734adb42017-06-07 16:58:31 -04002611 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002612 impl ToTokens for ExprMatch {
2613 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002614 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002615 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002616 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002617 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002618 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002619 arm.to_tokens(tokens);
2620 // Ensure that we have a comma after a non-block arm, except
2621 // for the last one.
2622 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002623 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002624 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002625 }
2626 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002627 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002628 }
2629 }
2630
Michael Layzell734adb42017-06-07 16:58:31 -04002631 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002632 impl ToTokens for ExprCatch {
2633 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002634 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002635 self.do_token.to_tokens(tokens);
2636 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002637 self.block.to_tokens(tokens);
2638 }
2639 }
2640
Michael Layzell734adb42017-06-07 16:58:31 -04002641 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002642 impl ToTokens for ExprYield {
2643 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002644 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002645 self.yield_token.to_tokens(tokens);
2646 self.expr.to_tokens(tokens);
2647 }
2648 }
2649
2650 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002651 impl ToTokens for ExprClosure {
2652 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002653 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002654 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002655 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002656 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002657 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002658 FnArg::Captured(ArgCaptured {
2659 ref pat,
2660 ty: Type::Infer(_),
2661 ..
2662 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002663 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002664 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002665 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002666 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002667 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002668 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002669 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002670 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002671 self.body.to_tokens(tokens);
2672 }
2673 }
2674
Michael Layzell734adb42017-06-07 16:58:31 -04002675 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002676 impl ToTokens for ExprUnsafe {
2677 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002678 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002679 self.unsafe_token.to_tokens(tokens);
2680 self.block.to_tokens(tokens);
2681 }
2682 }
2683
2684 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002685 impl ToTokens for ExprBlock {
2686 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002687 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002688 self.block.to_tokens(tokens);
2689 }
2690 }
2691
Michael Layzell734adb42017-06-07 16:58:31 -04002692 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002693 impl ToTokens for ExprAssign {
2694 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002695 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002696 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002697 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002698 self.right.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 ExprAssignOp {
2704 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002705 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002706 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002707 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002708 self.right.to_tokens(tokens);
2709 }
2710 }
2711
Michael Layzell734adb42017-06-07 16:58:31 -04002712 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002713 impl ToTokens for ExprField {
2714 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002715 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002716 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002717 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002718 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002719 }
2720 }
2721
Michael Layzell734adb42017-06-07 16:58:31 -04002722 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002723 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002724 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002725 match *self {
2726 Member::Named(ident) => ident.to_tokens(tokens),
2727 Member::Unnamed(ref index) => index.to_tokens(tokens),
2728 }
2729 }
2730 }
2731
2732 #[cfg(feature = "full")]
2733 impl ToTokens for Index {
2734 fn to_tokens(&self, tokens: &mut Tokens) {
2735 tokens.append(TokenTree {
2736 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002737 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002738 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002739 }
2740 }
2741
2742 impl ToTokens for ExprIndex {
2743 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002744 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002745 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002746 self.bracket_token.surround(tokens, |tokens| {
2747 self.index.to_tokens(tokens);
2748 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002749 }
2750 }
2751
Michael Layzell734adb42017-06-07 16:58:31 -04002752 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002753 impl ToTokens for ExprRange {
2754 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002755 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002756 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002757 match self.limits {
2758 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2759 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2760 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002761 self.to.to_tokens(tokens);
2762 }
2763 }
2764
2765 impl ToTokens for ExprPath {
2766 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002767 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002768 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 }
2770 }
2771
Michael Layzell734adb42017-06-07 16:58:31 -04002772 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002773 impl ToTokens for ExprAddrOf {
2774 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002775 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002776 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002777 self.mutbl.to_tokens(tokens);
2778 self.expr.to_tokens(tokens);
2779 }
2780 }
2781
Michael Layzell734adb42017-06-07 16:58:31 -04002782 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002783 impl ToTokens for ExprBreak {
2784 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002785 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002786 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002787 self.label.to_tokens(tokens);
2788 self.expr.to_tokens(tokens);
2789 }
2790 }
2791
Michael Layzell734adb42017-06-07 16:58:31 -04002792 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002793 impl ToTokens for ExprContinue {
2794 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002795 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002796 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002797 self.label.to_tokens(tokens);
2798 }
2799 }
2800
Michael Layzell734adb42017-06-07 16:58:31 -04002801 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002802 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002803 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002804 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002805 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002806 self.expr.to_tokens(tokens);
2807 }
2808 }
2809
Michael Layzell734adb42017-06-07 16:58:31 -04002810 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002811 impl ToTokens for ExprMacro {
2812 fn to_tokens(&self, tokens: &mut Tokens) {
2813 tokens.append_all(self.attrs.outer());
2814 self.mac.to_tokens(tokens);
2815 }
2816 }
2817
2818 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002819 impl ToTokens for ExprStruct {
2820 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002821 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002822 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002823 self.brace_token.surround(tokens, |tokens| {
2824 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002825 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002826 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002827 self.rest.to_tokens(tokens);
2828 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002829 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002830 }
2831 }
2832
Michael Layzell734adb42017-06-07 16:58:31 -04002833 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002834 impl ToTokens for ExprRepeat {
2835 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002836 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002837 self.bracket_token.surround(tokens, |tokens| {
2838 self.expr.to_tokens(tokens);
2839 self.semi_token.to_tokens(tokens);
2840 self.amt.to_tokens(tokens);
2841 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002842 }
2843 }
2844
David Tolnaye98775f2017-12-28 23:17:00 -05002845 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002846 impl ToTokens for ExprGroup {
2847 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002848 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002849 self.group_token.surround(tokens, |tokens| {
2850 self.expr.to_tokens(tokens);
2851 });
2852 }
2853 }
2854
David Tolnaye98775f2017-12-28 23:17:00 -05002855 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002856 impl ToTokens for ExprParen {
2857 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002858 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002859 self.paren_token.surround(tokens, |tokens| {
2860 self.expr.to_tokens(tokens);
2861 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002862 }
2863 }
2864
Michael Layzell734adb42017-06-07 16:58:31 -04002865 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002866 impl ToTokens for ExprTry {
2867 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002868 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002869 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002870 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002871 }
2872 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002873
Michael Layzell734adb42017-06-07 16:58:31 -04002874 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002875 impl ToTokens for FieldValue {
2876 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002877 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002878 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2879 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002880 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002881 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002882 self.expr.to_tokens(tokens);
2883 }
David Tolnay055a7042016-10-02 19:23:54 -07002884 }
2885 }
2886
Michael Layzell734adb42017-06-07 16:58:31 -04002887 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002888 impl ToTokens for Arm {
2889 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002890 tokens.append_all(&self.attrs);
2891 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002892 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002893 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002894 self.guard.to_tokens(tokens);
2895 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002896 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002897 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002898 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002899 }
2900 }
2901
Michael Layzell734adb42017-06-07 16:58:31 -04002902 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002903 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002904 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002905 self.underscore_token.to_tokens(tokens);
2906 }
2907 }
2908
Michael Layzell734adb42017-06-07 16:58:31 -04002909 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002910 impl ToTokens for PatIdent {
2911 fn to_tokens(&self, tokens: &mut Tokens) {
2912 self.mode.to_tokens(tokens);
2913 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002914 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002915 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002916 self.subpat.to_tokens(tokens);
2917 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002918 }
2919 }
2920
Michael Layzell734adb42017-06-07 16:58:31 -04002921 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002922 impl ToTokens for PatStruct {
2923 fn to_tokens(&self, tokens: &mut Tokens) {
2924 self.path.to_tokens(tokens);
2925 self.brace_token.surround(tokens, |tokens| {
2926 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002927 // NOTE: We need a comma before the dot2 token if it is present.
2928 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002929 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002930 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002931 self.dot2_token.to_tokens(tokens);
2932 });
2933 }
2934 }
2935
Michael Layzell734adb42017-06-07 16:58:31 -04002936 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002937 impl ToTokens for PatTupleStruct {
2938 fn to_tokens(&self, tokens: &mut Tokens) {
2939 self.path.to_tokens(tokens);
2940 self.pat.to_tokens(tokens);
2941 }
2942 }
2943
Michael Layzell734adb42017-06-07 16:58:31 -04002944 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002945 impl ToTokens for PatPath {
2946 fn to_tokens(&self, tokens: &mut Tokens) {
2947 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2948 }
2949 }
2950
Michael Layzell734adb42017-06-07 16:58:31 -04002951 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002952 impl ToTokens for PatTuple {
2953 fn to_tokens(&self, tokens: &mut Tokens) {
2954 self.paren_token.surround(tokens, |tokens| {
2955 for (i, token) in self.pats.iter().enumerate() {
2956 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07002957 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
2958 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002959 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002960 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002961 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002962
2963 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002964 // Ensure there is a comma before the .. token.
2965 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002966 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002967 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002968 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07002969 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002970 });
2971 }
2972 }
2973
Michael Layzell734adb42017-06-07 16:58:31 -04002974 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002975 impl ToTokens for PatBox {
2976 fn to_tokens(&self, tokens: &mut Tokens) {
2977 self.box_token.to_tokens(tokens);
2978 self.pat.to_tokens(tokens);
2979 }
2980 }
2981
Michael Layzell734adb42017-06-07 16:58:31 -04002982 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002983 impl ToTokens for PatRef {
2984 fn to_tokens(&self, tokens: &mut Tokens) {
2985 self.and_token.to_tokens(tokens);
2986 self.mutbl.to_tokens(tokens);
2987 self.pat.to_tokens(tokens);
2988 }
2989 }
2990
Michael Layzell734adb42017-06-07 16:58:31 -04002991 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002992 impl ToTokens for PatLit {
2993 fn to_tokens(&self, tokens: &mut Tokens) {
2994 self.expr.to_tokens(tokens);
2995 }
2996 }
2997
Michael Layzell734adb42017-06-07 16:58:31 -04002998 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002999 impl ToTokens for PatRange {
3000 fn to_tokens(&self, tokens: &mut Tokens) {
3001 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003002 match self.limits {
3003 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3004 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3005 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003006 self.hi.to_tokens(tokens);
3007 }
3008 }
3009
Michael Layzell734adb42017-06-07 16:58:31 -04003010 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011 impl ToTokens for PatSlice {
3012 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003013 // XXX: This is a mess, and it will be so easy to screw it up. How
3014 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003015 self.bracket_token.surround(tokens, |tokens| {
3016 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003017
3018 // If we need a comma before the middle or standalone .. token,
3019 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003020 if !self.front.empty_or_trailing()
3021 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003022 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003023 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003024 }
3025
3026 // If we have an identifier, we always need a .. token.
3027 if self.middle.is_some() {
3028 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003029 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003030 } else if self.dot2_token.is_some() {
3031 self.dot2_token.to_tokens(tokens);
3032 }
3033
3034 // Make sure we have a comma before the back half.
3035 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003036 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003037 self.back.to_tokens(tokens);
3038 } else {
3039 self.comma_token.to_tokens(tokens);
3040 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003041 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003042 }
3043 }
3044
Michael Layzell734adb42017-06-07 16:58:31 -04003045 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003046 impl ToTokens for FieldPat {
3047 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003048 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07003049 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05003050 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003051 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003052 }
3053 self.pat.to_tokens(tokens);
3054 }
3055 }
3056
Michael Layzell734adb42017-06-07 16:58:31 -04003057 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003058 impl ToTokens for BindingMode {
3059 fn to_tokens(&self, tokens: &mut Tokens) {
3060 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003061 BindingMode::ByRef(ref t, ref m) => {
3062 t.to_tokens(tokens);
3063 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003064 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003065 BindingMode::ByValue(ref m) => {
3066 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003067 }
3068 }
3069 }
3070 }
David Tolnay42602292016-10-01 22:25:45 -07003071
Michael Layzell734adb42017-06-07 16:58:31 -04003072 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07003073 impl ToTokens for CaptureBy {
3074 fn to_tokens(&self, tokens: &mut Tokens) {
3075 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003076 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003077 CaptureBy::Ref => {
3078 // nothing
3079 }
David Tolnay89e05672016-10-02 14:39:42 -07003080 }
3081 }
3082 }
3083
Michael Layzell734adb42017-06-07 16:58:31 -04003084 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003085 impl ToTokens for Block {
3086 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003087 self.brace_token.surround(tokens, |tokens| {
3088 tokens.append_all(&self.stmts);
3089 });
David Tolnay42602292016-10-01 22:25:45 -07003090 }
3091 }
3092
Michael Layzell734adb42017-06-07 16:58:31 -04003093 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003094 impl ToTokens for Stmt {
3095 fn to_tokens(&self, tokens: &mut Tokens) {
3096 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003097 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003098 Stmt::Item(ref item) => item.to_tokens(tokens),
3099 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003100 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003101 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003102 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003103 }
David Tolnay42602292016-10-01 22:25:45 -07003104 }
3105 }
3106 }
David Tolnay191e0582016-10-02 18:31:09 -07003107
Michael Layzell734adb42017-06-07 16:58:31 -04003108 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003109 impl ToTokens for Local {
3110 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003111 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003112 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003113 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003114 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003115 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003116 self.ty.to_tokens(tokens);
3117 }
3118 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003119 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003120 self.init.to_tokens(tokens);
3121 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003122 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003123 }
3124 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003125}