blob: 165b69231c6402dda721282acce94ee0297222c3 [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
Michael Layzellb78f3b52017-06-04 19:03:03 -040018 /// E.g. 'place <- val' or `in place { val }`.
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>,
Michael Layzell6a5a1642017-06-04 19:35:15 -040022 pub kind: InPlaceKind,
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 Tolnay4a3f59a2017-12-28 21:21:12 -050030 pub exprs: 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 Tolnay4a3f59a2017-12-28 21:21:12 -050063 pub args: 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
337 pub Paren(ExprParen {
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.
348 pub Group(ExprGroup {
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)]
385 pub fn attrs_mut(&mut self) -> &mut Vec<Attribute> {
386 match *self {
387 Expr::Box(ExprBox { ref mut attrs, .. }) |
388 Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
389 Expr::Array(ExprArray { ref mut attrs, .. }) |
390 Expr::Call(ExprCall { ref mut attrs, .. }) |
391 Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
392 Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
393 Expr::Binary(ExprBinary { ref mut attrs, .. }) |
394 Expr::Unary(ExprUnary { ref mut attrs, .. }) |
395 Expr::Lit(ExprLit { ref mut attrs, .. }) |
396 Expr::Cast(ExprCast { ref mut attrs, .. }) |
397 Expr::Type(ExprType { ref mut attrs, .. }) |
398 Expr::If(ExprIf { ref mut attrs, .. }) |
399 Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
400 Expr::While(ExprWhile { ref mut attrs, .. }) |
401 Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
402 Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
403 Expr::Loop(ExprLoop { ref mut attrs, .. }) |
404 Expr::Match(ExprMatch { ref mut attrs, .. }) |
405 Expr::Closure(ExprClosure { ref mut attrs, .. }) |
406 Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
407 Expr::Block(ExprBlock { ref mut attrs, .. }) |
408 Expr::Assign(ExprAssign { ref mut attrs, .. }) |
409 Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
410 Expr::Field(ExprField { ref mut attrs, .. }) |
411 Expr::Index(ExprIndex { ref mut attrs, .. }) |
412 Expr::Range(ExprRange { ref mut attrs, .. }) |
413 Expr::Path(ExprPath { ref mut attrs, .. }) |
414 Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
415 Expr::Break(ExprBreak { ref mut attrs, .. }) |
416 Expr::Continue(ExprContinue { ref mut attrs, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500417 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500418 Expr::Macro(ExprMacro { ref mut attrs, .. }) |
419 Expr::Struct(ExprStruct { ref mut attrs, .. }) |
420 Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
421 Expr::Paren(ExprParen { ref mut attrs, .. }) |
422 Expr::Group(ExprGroup { ref mut attrs, .. }) |
423 Expr::Try(ExprTry { ref mut attrs, .. }) |
424 Expr::Catch(ExprCatch { ref mut attrs, .. }) |
425 Expr::Yield(ExprYield { ref mut attrs, .. }) => attrs,
426 }
427 }
428}
429
Michael Layzell734adb42017-06-07 16:58:31 -0400430#[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500431ast_enum! {
432 /// A struct or tuple struct field accessed in a struct literal or field
433 /// expression.
434 pub enum Member {
435 /// A named field like `self.x`.
436 Named(Ident),
437 /// An unnamed field like `self.0`.
438 Unnamed(Index),
439 }
440}
441
442#[cfg(feature = "full")]
443ast_struct! {
444 /// The index of an unnamed tuple struct field.
445 pub struct Index #manual_extra_traits {
446 pub index: u32,
447 pub span: Span,
448 }
449}
450
451#[cfg(feature = "full")]
452impl Eq for Index {}
453
454#[cfg(feature = "full")]
455impl PartialEq for Index {
456 fn eq(&self, other: &Self) -> bool {
457 self.index == other.index
458 }
459}
460
461#[cfg(feature = "full")]
462impl Hash for Index {
463 fn hash<H: Hasher>(&self, state: &mut H) {
464 self.index.hash(state);
465 }
466}
467
468#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700469ast_struct! {
470 /// A field-value pair in a struct literal.
471 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500472 /// Attributes tagged on the field.
473 pub attrs: Vec<Attribute>,
474
475 /// Name or index of the field.
476 pub member: Member,
477
478 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500479
Alex Crichton62a0a592017-05-22 13:58:53 -0700480 /// Value of the field.
481 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500482
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 /// Whether this is a shorthand field, e.g. `Struct { x }`
484 /// instead of `Struct { x: x }`.
485 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700486 }
David Tolnay055a7042016-10-02 19:23:54 -0700487}
488
Michael Layzell734adb42017-06-07 16:58:31 -0400489#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700490ast_struct! {
491 /// A Block (`{ .. }`).
492 ///
493 /// E.g. `{ .. }` as in `fn foo() { .. }`
494 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500495 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700496 /// Statements in a block
497 pub stmts: Vec<Stmt>,
498 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700499}
500
Michael Layzell734adb42017-06-07 16:58:31 -0400501#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700502ast_enum! {
503 /// A statement, usually ending in a semicolon.
504 pub enum Stmt {
505 /// A local (let) binding.
506 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700507
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 /// An item definition.
509 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700510
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 /// Expr without trailing semicolon.
512 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700513
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800515 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700516 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700517}
518
Michael Layzell734adb42017-06-07 16:58:31 -0400519#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700520ast_struct! {
521 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
522 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500523 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800524 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500526 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800527 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500528 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 /// Initializer expression to set the value, if any
530 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700533}
534
Michael Layzell734adb42017-06-07 16:58:31 -0400535#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 // Clippy false positive
538 // https://github.com/Manishearth/rust-clippy/issues/1241
539 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
540 pub enum Pat {
541 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800543 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700544 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700545
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
547 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
548 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
549 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700550 pub Ident(PatIdent {
551 pub mode: BindingMode,
552 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800553 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500554 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700556
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
558 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700559 pub Struct(PatStruct {
560 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500561 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500562 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800563 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700564 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700565
Alex Crichton62a0a592017-05-22 13:58:53 -0700566 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
567 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
568 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700569 pub TupleStruct(PatTupleStruct {
570 pub path: Path,
571 pub pat: PatTuple,
572 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700573
Alex Crichton62a0a592017-05-22 13:58:53 -0700574 /// A possibly qualified path pattern.
575 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
576 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
577 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700578 pub Path(PatPath {
579 pub qself: Option<QSelf>,
580 pub path: Path,
581 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700582
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 /// A tuple pattern `(a, b)`.
584 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
585 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700586 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500587 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500588 pub pats: Delimited<Pat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800589 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500590 pub dots_pos: Option<usize>,
591 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700592 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700593 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700594 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800595 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500596 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700597 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700598 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700599 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800600 pub and_token: Token![&],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500601 pub mutbl: Mutability,
602 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700604 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700605 pub Lit(PatLit {
606 pub expr: Box<Expr>,
607 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800608 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700609 pub Range(PatRange {
610 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700611 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500612 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700613 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400614 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700615 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500616 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700618 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800619 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500620 pub dot2_token: Option<Token![..]>,
621 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700622 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800624 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700625 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700626}
627
Michael Layzell734adb42017-06-07 16:58:31 -0400628#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700629ast_struct! {
630 /// An arm of a 'match'.
631 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800632 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700633 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500634 /// ```rust
635 /// # #![feature(dotdoteq_in_patterns)]
636 /// #
637 /// # fn main() {
638 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700639 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500640 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700641 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500642 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700643 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500644 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700645 /// ```
646 pub struct Arm {
647 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800648 pub pats: Delimited<Pat, Token![|]>,
649 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700650 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800651 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800653 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700654 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700655}
656
Michael Layzell734adb42017-06-07 16:58:31 -0400657#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700658ast_enum! {
659 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700660 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700661 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800662 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700663 Ref,
664 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700665}
666
Michael Layzell734adb42017-06-07 16:58:31 -0400667#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700668ast_enum! {
669 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700670 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700671 pub enum RangeLimits {
672 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800673 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800675 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700676 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700677}
678
Michael Layzell734adb42017-06-07 16:58:31 -0400679#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700680ast_struct! {
681 /// A single field in a struct pattern
682 ///
683 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
684 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
685 /// except `is_shorthand` is true
686 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500687 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500689 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500690 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700691 /// The pattern the field is destructured to
692 pub pat: Box<Pat>,
693 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700694 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700695}
696
Michael Layzell734adb42017-06-07 16:58:31 -0400697#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700698ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700699 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800701 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 ByValue(Mutability),
703 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700704}
705
Michael Layzell734adb42017-06-07 16:58:31 -0400706#[cfg(feature = "full")]
Michael Layzell6a5a1642017-06-04 19:35:15 -0400707ast_enum! {
708 #[cfg_attr(feature = "clone-impls", derive(Copy))]
709 pub enum InPlaceKind {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800710 Arrow(Token![<-]),
711 In(Token![in]),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400712 }
713}
714
Michael Layzell3936ceb2017-07-08 00:28:36 -0400715#[cfg(any(feature = "parsing", feature = "printing"))]
716#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700717fn arm_expr_requires_comma(expr: &Expr) -> bool {
718 // see https://github.com/rust-lang/rust/blob/eb8f2586e
719 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500720 match *expr {
721 Expr::Unsafe(..)
722 | Expr::Block(..)
723 | Expr::If(..)
724 | Expr::IfLet(..)
725 | Expr::Match(..)
726 | Expr::While(..)
727 | Expr::WhileLet(..)
728 | Expr::Loop(..)
729 | Expr::ForLoop(..)
730 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700731 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400732 }
733}
734
David Tolnayb9c8e322016-09-23 20:48:37 -0700735#[cfg(feature = "parsing")]
736pub mod parsing {
737 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700738 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700739
Michael Layzell734adb42017-06-07 16:58:31 -0400740 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500741 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500742 use synom::Synom;
743 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400744 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500745 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500746 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700747
David Tolnaybcf26022017-12-25 22:10:52 -0500748 // When we're parsing expressions which occur before blocks, like in an if
749 // statement's condition, we cannot parse a struct literal.
750 //
751 // Struct literals are ambiguous in certain positions
752 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700753 macro_rules! ambiguous_expr {
754 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700755 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700756 };
757 }
758
David Tolnaybcf26022017-12-25 22:10:52 -0500759 // When we are parsing an optional suffix expression, we cannot allow blocks
760 // if structs are not allowed.
761 //
762 // Example:
763 //
764 // if break {} {}
765 //
766 // is ambiguous between:
767 //
768 // if (break {}) {}
769 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400770 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400771 macro_rules! opt_ambiguous_expr {
772 ($i:expr, $allow_struct:ident) => {
773 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
774 };
775 }
776
Alex Crichton954046c2017-05-30 21:49:42 -0700777 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400778 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700779
780 fn description() -> Option<&'static str> {
781 Some("expression")
782 }
783 }
784
Michael Layzell734adb42017-06-07 16:58:31 -0400785 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700786 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
787
David Tolnaybcf26022017-12-25 22:10:52 -0500788 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400789 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500790 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500791 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400792 }
793
Michael Layzell734adb42017-06-07 16:58:31 -0400794 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500795 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500796 // NOTE: We intentionally skip assign_expr, placement_expr, and
797 // range_expr, as they are not parsed in non-full mode.
798 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400799 }
800
David Tolnaybcf26022017-12-25 22:10:52 -0500801 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400802 macro_rules! binop {
803 (
804 $name: ident,
805 $next: ident,
806 $submac: ident!( $($args:tt)* )
807 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500808 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400809 mut e: call!($next, allow_struct, allow_block) >>
810 many0!(do_parse!(
811 op: $submac!($($args)*) >>
812 rhs: call!($next, allow_struct, true) >>
813 ({
814 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500815 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400816 left: Box::new(e.into()),
817 op: op,
818 right: Box::new(rhs.into()),
819 }.into();
820 })
821 )) >>
822 (e)
823 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700824 }
David Tolnay54e854d2016-10-24 12:03:30 -0700825 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700826
David Tolnaybcf26022017-12-25 22:10:52 -0500827 // <placement> = <placement> ..
828 // <placement> += <placement> ..
829 // <placement> -= <placement> ..
830 // <placement> *= <placement> ..
831 // <placement> /= <placement> ..
832 // <placement> %= <placement> ..
833 // <placement> ^= <placement> ..
834 // <placement> &= <placement> ..
835 // <placement> |= <placement> ..
836 // <placement> <<= <placement> ..
837 // <placement> >>= <placement> ..
838 //
839 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400840 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500841 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400842 mut e: call!(placement_expr, allow_struct, allow_block) >>
843 alt!(
844 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800845 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400846 // Recurse into self to parse right-associative operator.
847 rhs: call!(assign_expr, allow_struct, true) >>
848 ({
849 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500850 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400851 left: Box::new(e.into()),
852 eq_token: eq,
853 right: Box::new(rhs.into()),
854 }.into();
855 })
856 )
857 |
858 do_parse!(
859 op: call!(BinOp::parse_assign_op) >>
860 // Recurse into self to parse right-associative operator.
861 rhs: call!(assign_expr, allow_struct, true) >>
862 ({
863 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500864 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400865 left: Box::new(e.into()),
866 op: op,
867 right: Box::new(rhs.into()),
868 }.into();
869 })
870 )
871 |
872 epsilon!()
873 ) >>
874 (e)
875 ));
876
David Tolnaybcf26022017-12-25 22:10:52 -0500877 // <range> <- <range> ..
878 //
879 // NOTE: The `in place { expr }` version of this syntax is parsed in
880 // `atom_expr`, not here.
881 //
882 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400883 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500884 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400885 mut e: call!(range_expr, allow_struct, allow_block) >>
886 alt!(
887 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800888 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400889 // Recurse into self to parse right-associative operator.
890 rhs: call!(placement_expr, allow_struct, true) >>
891 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400892 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500893 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400894 // op: BinOp::Place(larrow),
895 place: Box::new(e.into()),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400896 kind: InPlaceKind::Arrow(arrow),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400897 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400898 }.into();
899 })
900 )
901 |
902 epsilon!()
903 ) >>
904 (e)
905 ));
906
David Tolnaybcf26022017-12-25 22:10:52 -0500907 // <or> ... <or> ..
908 // <or> .. <or> ..
909 // <or> ..
910 //
911 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
912 // rules are for parsing these expressions are, but this is not correct.
913 // For example, `a .. b .. c` is not a legal expression. It should not
914 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
915 //
916 // NOTE: The form of ranges which don't include a preceding expression are
917 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400918 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500919 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400920 mut e: call!(or_expr, allow_struct, allow_block) >>
921 many0!(do_parse!(
922 limits: syn!(RangeLimits) >>
923 // We don't want to allow blocks here if we don't allow structs. See
924 // the reasoning for `opt_ambiguous_expr!` above.
925 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
926 ({
927 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500928 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400929 from: Some(Box::new(e.into())),
930 limits: limits,
931 to: hi.map(|e| Box::new(e.into())),
932 }.into();
933 })
934 )) >>
935 (e)
936 ));
937
David Tolnaybcf26022017-12-25 22:10:52 -0500938 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800939 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400940
David Tolnaybcf26022017-12-25 22:10:52 -0500941 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800942 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400943
David Tolnaybcf26022017-12-25 22:10:52 -0500944 // <bitor> == <bitor> ...
945 // <bitor> != <bitor> ...
946 // <bitor> >= <bitor> ...
947 // <bitor> <= <bitor> ...
948 // <bitor> > <bitor> ...
949 // <bitor> < <bitor> ...
950 //
951 // NOTE: This operator appears to be parsed as left-associative, but errors
952 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500953 binop!(
954 compare_expr,
955 bitor_expr,
956 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400958 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800959 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400960 |
961 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400963 |
964 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800965 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400966 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400967 do_parse!(
968 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800969 not!(punct!(<-)) >>
970 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400971 (BinOp::Lt(t))
972 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400973 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500975 )
976 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400977
David Tolnaybcf26022017-12-25 22:10:52 -0500978 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500979 binop!(
980 bitor_expr,
981 bitxor_expr,
982 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
983 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400984
David Tolnaybcf26022017-12-25 22:10:52 -0500985 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500986 binop!(
987 bitxor_expr,
988 bitand_expr,
989 do_parse!(
990 // NOTE: Make sure we aren't looking at ^=.
991 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
992 )
993 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400994
David Tolnaybcf26022017-12-25 22:10:52 -0500995 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500996 binop!(
997 bitand_expr,
998 shift_expr,
999 do_parse!(
1000 // NOTE: Make sure we aren't looking at && or &=.
1001 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1002 )
1003 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004
David Tolnaybcf26022017-12-25 22:10:52 -05001005 // <arith> << <arith> ...
1006 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001007 binop!(
1008 shift_expr,
1009 arith_expr,
1010 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001011 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001012 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001013 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001014 )
1015 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016
David Tolnaybcf26022017-12-25 22:10:52 -05001017 // <term> + <term> ...
1018 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001019 binop!(
1020 arith_expr,
1021 term_expr,
1022 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001024 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001025 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001026 )
1027 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001028
David Tolnaybcf26022017-12-25 22:10:52 -05001029 // <cast> * <cast> ...
1030 // <cast> / <cast> ...
1031 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001032 binop!(
1033 term_expr,
1034 cast_expr,
1035 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001036 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001037 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001038 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001039 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001040 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001041 )
1042 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001043
David Tolnaybcf26022017-12-25 22:10:52 -05001044 // <unary> as <ty>
1045 // <unary> : <ty>
David Tolnay8c91b882017-12-28 23:04:32 -05001046 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001047 mut e: call!(unary_expr, allow_struct, allow_block) >>
1048 many0!(alt!(
1049 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001051 // We can't accept `A + B` in cast expressions, as it's
1052 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001053 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001054 ({
1055 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001056 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001057 expr: Box::new(e.into()),
1058 as_token: as_,
1059 ty: Box::new(ty),
1060 }.into();
1061 })
1062 )
1063 |
1064 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001065 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066 // We can't accept `A + B` in cast expressions, as it's
1067 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001068 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001069 ({
1070 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001071 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001072 expr: Box::new(e.into()),
1073 colon_token: colon,
1074 ty: Box::new(ty),
1075 }.into();
1076 })
1077 )
1078 )) >>
1079 (e)
1080 ));
1081
David Tolnaybcf26022017-12-25 22:10:52 -05001082 // <UnOp> <trailer>
1083 // & <trailer>
1084 // &mut <trailer>
1085 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001086 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001087 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001088 do_parse!(
1089 op: syn!(UnOp) >>
1090 expr: call!(unary_expr, allow_struct, true) >>
1091 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001092 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001093 op: op,
1094 expr: Box::new(expr.into()),
1095 }.into())
1096 )
1097 |
1098 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001099 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001100 mutability: syn!(Mutability) >>
1101 expr: call!(unary_expr, allow_struct, true) >>
1102 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001103 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001104 and_token: and,
1105 mutbl: mutability,
1106 expr: Box::new(expr.into()),
1107 }.into())
1108 )
1109 |
1110 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001111 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001112 expr: call!(unary_expr, allow_struct, true) >>
1113 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001114 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001115 box_token: box_,
1116 expr: Box::new(expr.into()),
1117 }.into())
1118 )
1119 |
1120 call!(trailer_expr, allow_struct, allow_block)
1121 ));
1122
Michael Layzell734adb42017-06-07 16:58:31 -04001123 // XXX: This duplication is ugly
1124 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001125 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001126 do_parse!(
1127 op: syn!(UnOp) >>
1128 expr: call!(unary_expr, allow_struct, true) >>
1129 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001130 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001131 op: op,
1132 expr: Box::new(expr.into()),
1133 }.into())
1134 )
1135 |
1136 call!(trailer_expr, allow_struct, allow_block)
1137 ));
1138
David Tolnaybcf26022017-12-25 22:10:52 -05001139 // <atom> (..<args>) ...
1140 // <atom> . <ident> (..<args>) ...
1141 // <atom> . <ident> ...
1142 // <atom> . <lit> ...
1143 // <atom> [ <expr> ] ...
1144 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001145 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001146 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001147 mut e: call!(atom_expr, allow_struct, allow_block) >>
1148 many0!(alt!(
1149 tap!(args: and_call => {
1150 let (args, paren) = args;
1151 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001152 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001153 func: Box::new(e.into()),
1154 args: args,
1155 paren_token: paren,
1156 }.into();
1157 })
1158 |
1159 tap!(more: and_method_call => {
1160 let mut call = more;
1161 call.expr = Box::new(e.into());
1162 e = call.into();
1163 })
1164 |
1165 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001166 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001167 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001168 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001169 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001170 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001171 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001172 }.into();
1173 })
1174 |
1175 tap!(i: and_index => {
1176 let (i, token) = i;
1177 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001178 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179 expr: Box::new(e.into()),
1180 bracket_token: token,
1181 index: Box::new(i),
1182 }.into();
1183 })
1184 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001185 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001186 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001187 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 expr: Box::new(e.into()),
1189 question_token: question,
1190 }.into();
1191 })
1192 )) >>
1193 (e)
1194 ));
1195
Michael Layzell734adb42017-06-07 16:58:31 -04001196 // XXX: Duplication == ugly
1197 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001198 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001199 mut e: call!(atom_expr, allow_struct, allow_block) >>
1200 many0!(alt!(
1201 tap!(args: and_call => {
1202 let (args, paren) = args;
1203 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001204 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001205 func: Box::new(e.into()),
1206 args: args,
1207 paren_token: paren,
1208 }.into();
1209 })
1210 |
1211 tap!(i: and_index => {
1212 let (i, token) = i;
1213 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001214 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001215 expr: Box::new(e.into()),
1216 bracket_token: token,
1217 index: Box::new(i),
1218 }.into();
1219 })
1220 )) >>
1221 (e)
1222 ));
1223
David Tolnaybcf26022017-12-25 22:10:52 -05001224 // Parse all atomic expressions which don't have to worry about precidence
1225 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001226 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001227 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1228 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001229 |
David Tolnay8c91b882017-12-28 23:04:32 -05001230 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001231 |
1232 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001233 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001234 |
David Tolnay8c91b882017-12-28 23:04:32 -05001235 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001236 |
David Tolnay8c91b882017-12-28 23:04:32 -05001237 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001238 |
1239 call!(expr_break, allow_struct) // must be before expr_path
1240 |
David Tolnay8c91b882017-12-28 23:04:32 -05001241 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 |
1243 call!(expr_ret, allow_struct) // must be before expr_path
1244 |
1245 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
David Tolnay8c91b882017-12-28 23:04:32 -05001246 syn!(ExprInPlace) => { Expr::InPlace }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 |
David Tolnay8c91b882017-12-28 23:04:32 -05001248 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001249 |
David Tolnay8c91b882017-12-28 23:04:32 -05001250 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001251 |
David Tolnay8c91b882017-12-28 23:04:32 -05001252 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001253 |
David Tolnay8c91b882017-12-28 23:04:32 -05001254 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001255 |
David Tolnay8c91b882017-12-28 23:04:32 -05001256 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001257 |
David Tolnay8c91b882017-12-28 23:04:32 -05001258 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 |
David Tolnay8c91b882017-12-28 23:04:32 -05001260 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001261 |
David Tolnay8c91b882017-12-28 23:04:32 -05001262 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001263 |
David Tolnay8c91b882017-12-28 23:04:32 -05001264 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001265 |
David Tolnay8c91b882017-12-28 23:04:32 -05001266 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267 |
David Tolnay8c91b882017-12-28 23:04:32 -05001268 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001269 |
David Tolnay8c91b882017-12-28 23:04:32 -05001270 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001271 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001272 call!(expr_closure, allow_struct)
1273 |
David Tolnay8c91b882017-12-28 23:04:32 -05001274 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001275 |
1276 // NOTE: This is the prefix-form of range
1277 call!(expr_range, allow_struct)
1278 |
David Tolnay8c91b882017-12-28 23:04:32 -05001279 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001280 |
David Tolnay8c91b882017-12-28 23:04:32 -05001281 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001282 ));
1283
Michael Layzell734adb42017-06-07 16:58:31 -04001284 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001285 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
1286 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell734adb42017-06-07 16:58:31 -04001287 |
David Tolnay8c91b882017-12-28 23:04:32 -05001288 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzell734adb42017-06-07 16:58:31 -04001289 |
David Tolnay8c91b882017-12-28 23:04:32 -05001290 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzell734adb42017-06-07 16:58:31 -04001291 |
David Tolnay8c91b882017-12-28 23:04:32 -05001292 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001293 ));
1294
Michael Layzell734adb42017-06-07 16:58:31 -04001295 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001296 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001297 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001298 |
David Tolnay8c91b882017-12-28 23:04:32 -05001299 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001300 |
David Tolnay8c91b882017-12-28 23:04:32 -05001301 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001302 |
David Tolnay8c91b882017-12-28 23:04:32 -05001303 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001304 |
David Tolnay8c91b882017-12-28 23:04:32 -05001305 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001306 |
David Tolnay8c91b882017-12-28 23:04:32 -05001307 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001308 |
David Tolnay8c91b882017-12-28 23:04:32 -05001309 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001310 |
David Tolnay8c91b882017-12-28 23:04:32 -05001311 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001314 |
David Tolnay8c91b882017-12-28 23:04:32 -05001315 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001316 |
David Tolnay8c91b882017-12-28 23:04:32 -05001317 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001318 ), Expr::from));
1319
David Tolnay8c91b882017-12-28 23:04:32 -05001320 impl Synom for ExprLit {
1321 named!(parse -> Self, do_parse!(
1322 lit: syn!(Lit) >>
1323 (ExprLit {
1324 attrs: Vec::new(),
1325 lit: lit,
1326 })
1327 ));
1328 }
1329
1330 #[cfg(feature = "full")]
1331 impl Synom for ExprMacro {
1332 named!(parse -> Self, do_parse!(
1333 mac: syn!(Macro) >>
1334 (ExprMacro {
1335 attrs: Vec::new(),
1336 mac: mac,
1337 })
1338 ));
1339 }
1340
Michael Layzell93c36282017-06-04 20:43:14 -04001341 impl Synom for ExprGroup {
1342 named!(parse -> Self, do_parse!(
1343 e: grouped!(syn!(Expr)) >>
1344 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001345 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001346 expr: Box::new(e.0),
1347 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001348 })
Michael Layzell93c36282017-06-04 20:43:14 -04001349 ));
1350 }
1351
Alex Crichton954046c2017-05-30 21:49:42 -07001352 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001353 named!(parse -> Self, do_parse!(
1354 e: parens!(syn!(Expr)) >>
1355 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001356 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001357 expr: Box::new(e.0),
1358 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001359 })
Michael Layzell92639a52017-06-01 00:07:44 -04001360 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001361 }
David Tolnay89e05672016-10-02 14:39:42 -07001362
Michael Layzell734adb42017-06-07 16:58:31 -04001363 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001364 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001365 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001366 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001367 place: expr_no_struct >>
1368 value: braces!(call!(Block::parse_within)) >>
1369 (ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001370 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001371 place: Box::new(place),
Michael Layzell6a5a1642017-06-04 19:35:15 -04001372 kind: InPlaceKind::In(in_),
David Tolnay8c91b882017-12-28 23:04:32 -05001373 value: Box::new(ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001374 attrs: Vec::new(),
David Tolnay8c91b882017-12-28 23:04:32 -05001375 block: Block {
1376 stmts: value.0,
1377 brace_token: value.1,
1378 },
1379 }.into()),
Michael Layzell92639a52017-06-01 00:07:44 -04001380 })
1381 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001382 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001383
Michael Layzell734adb42017-06-07 16:58:31 -04001384 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001385 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001386 named!(parse -> Self, do_parse!(
1387 elems: brackets!(call!(Delimited::parse_terminated)) >>
1388 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001389 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001390 exprs: elems.0,
1391 bracket_token: elems.1,
1392 })
1393 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001394 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001395
David Tolnay32954ef2017-12-26 22:43:16 -05001396 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001397 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001398
Michael Layzell734adb42017-06-07 16:58:31 -04001399 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001400 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001401 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001402 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001403 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001404 colon2: punct!(::) >>
1405 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001406 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001407 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001408 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001409 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001410 args: parens!(call!(Delimited::parse_terminated)) >>
1411 ({
1412 let (colon2, lt, tys, gt) = match typarams {
1413 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1414 None => (None, None, None, None),
1415 };
1416 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001417 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001418 // this expr will get overwritten after being returned
David Tolnay8c91b882017-12-28 23:04:32 -05001419 expr: Box::new(Expr::Lit(ExprLit {
1420 attrs: Vec::new(),
1421 lit: Lit {
1422 span: Span::default(),
1423 value: LitKind::Bool(false),
1424 },
Alex Crichton954046c2017-05-30 21:49:42 -07001425 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001426
Alex Crichton954046c2017-05-30 21:49:42 -07001427 method: method,
1428 args: args.0,
1429 paren_token: args.1,
1430 dot_token: dot,
1431 lt_token: lt,
1432 gt_token: gt,
1433 colon2_token: colon2,
1434 typarams: tys.unwrap_or_default(),
1435 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001436 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001437 ));
1438
Michael Layzell734adb42017-06-07 16:58:31 -04001439 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001440 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001441 named!(parse -> Self, do_parse!(
1442 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001443 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001444 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001445 args: elems.0,
1446 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001447 })
1448 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001449 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001450
Michael Layzell734adb42017-06-07 16:58:31 -04001451 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001452 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001453 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001454 if_: keyword!(if) >>
1455 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001456 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001457 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001458 cond: expr_no_struct >>
1459 then_block: braces!(call!(Block::parse_within)) >>
1460 else_block: option!(else_block) >>
1461 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001462 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001463 pat: Box::new(pat),
1464 let_token: let_,
1465 eq_token: eq,
1466 expr: Box::new(cond),
1467 if_true: Block {
1468 stmts: then_block.0,
1469 brace_token: then_block.1,
1470 },
1471 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001472 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001473 if_false: else_block.map(|p| Box::new(p.1.into())),
1474 })
1475 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001476 }
1477
Michael Layzell734adb42017-06-07 16:58:31 -04001478 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001479 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001480 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001481 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001482 cond: expr_no_struct >>
1483 then_block: braces!(call!(Block::parse_within)) >>
1484 else_block: option!(else_block) >>
1485 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001486 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001487 cond: Box::new(cond),
1488 if_true: Block {
1489 stmts: then_block.0,
1490 brace_token: then_block.1,
1491 },
1492 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001493 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001494 if_false: else_block.map(|p| Box::new(p.1.into())),
1495 })
1496 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001497 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001498
Michael Layzell734adb42017-06-07 16:58:31 -04001499 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001500 named!(else_block -> (Token![else], Expr), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001501 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001502 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001503 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001504 |
David Tolnay8c91b882017-12-28 23:04:32 -05001505 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001506 |
1507 do_parse!(
1508 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001509 (Expr::Block(ExprBlock {
1510 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001511 block: Block {
1512 stmts: else_block.0,
1513 brace_token: else_block.1,
1514 },
1515 }))
David Tolnay939766a2016-09-23 23:48:12 -07001516 )
Alex Crichton954046c2017-05-30 21:49:42 -07001517 ) >>
1518 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001519 ));
1520
Michael Layzell734adb42017-06-07 16:58:31 -04001521 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001522 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001523 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001524 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1525 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001526 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001527 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001528 expr: expr_no_struct >>
1529 loop_block: syn!(Block) >>
1530 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001531 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001532 for_token: for_,
1533 in_token: in_,
1534 pat: Box::new(pat),
1535 expr: Box::new(expr),
1536 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001537 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001538 label: lbl.map(|p| p.0),
1539 })
1540 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001541 }
Gregory Katze5f35682016-09-27 14:20:55 -04001542
Michael Layzell734adb42017-06-07 16:58:31 -04001543 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001544 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001545 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001546 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1547 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001548 loop_block: syn!(Block) >>
1549 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001550 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001551 loop_token: loop_,
1552 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001553 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001554 label: lbl.map(|p| p.0),
1555 })
1556 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001557 }
1558
Michael Layzell734adb42017-06-07 16:58:31 -04001559 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001560 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001561 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001562 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001563 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001564 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001565 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001566 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001567 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001568 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001569 expr: Box::new(obj),
1570 match_token: match_,
1571 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001572 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001573 }
1574 })
1575 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001576 }
David Tolnay1978c672016-10-27 22:05:52 -07001577
Michael Layzell734adb42017-06-07 16:58:31 -04001578 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001579 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001580 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001581 do_: keyword!(do) >>
1582 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001583 catch_block: syn!(Block) >>
1584 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001585 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001586 block: catch_block,
1587 do_token: do_,
1588 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001589 })
Michael Layzell92639a52017-06-01 00:07:44 -04001590 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001591 }
Arnavion02ef13f2017-04-25 00:54:31 -07001592
Michael Layzell734adb42017-06-07 16:58:31 -04001593 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001594 impl Synom for ExprYield {
1595 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001596 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001597 expr: option!(syn!(Expr)) >>
1598 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001599 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001600 yield_token: yield_,
1601 expr: expr.map(Box::new),
1602 })
1603 ));
1604 }
1605
1606 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001607 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001608 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001609 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001610 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001611 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1612 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001613 body: do_parse!(
1614 expr: alt!(expr_nosemi | syn!(Expr)) >>
1615 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1616 map!(input_end!(), |_| None)
1617 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001618 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001619 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001620 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001621 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001622 ) >>
1623 (Arm {
1624 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001625 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001626 attrs: attrs,
1627 pats: pats,
1628 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001629 body: Box::new(body.0),
1630 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001631 })
1632 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001633 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001634
Michael Layzell734adb42017-06-07 16:58:31 -04001635 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001636 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001637 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001638 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001639 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001640 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001641 ret_and_body: alt!(
1642 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001643 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001644 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001645 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001646 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001647 Expr::Block(ExprBlock {
1648 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001649 block: body,
1650 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001651 )
1652 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001653 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001654 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001655 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001656 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001657 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001658 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001659 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001660 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001661 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001662 body: Box::new(ret_and_body.1),
1663 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001664 ));
1665
Michael Layzell734adb42017-06-07 16:58:31 -04001666 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001667 named!(fn_arg -> FnArg, do_parse!(
1668 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001669 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001670 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001671 if let Some((colon, ty)) = ty {
1672 FnArg::Captured(ArgCaptured {
1673 pat: pat,
1674 colon_token: colon,
1675 ty: ty,
1676 })
1677 } else {
1678 FnArg::Inferred(pat)
1679 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001680 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001681 ));
1682
Michael Layzell734adb42017-06-07 16:58:31 -04001683 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001684 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001685 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001686 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1687 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001688 cond: expr_no_struct >>
1689 while_block: syn!(Block) >>
1690 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001691 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001692 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001693 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001694 cond: Box::new(cond),
1695 body: while_block,
1696 label: lbl.map(|p| p.0),
1697 })
1698 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001699 }
1700
Michael Layzell734adb42017-06-07 16:58:31 -04001701 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001702 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001703 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001704 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1705 while_: keyword!(while) >>
1706 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001707 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001708 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001709 value: expr_no_struct >>
1710 while_block: syn!(Block) >>
1711 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001712 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001713 eq_token: eq,
1714 let_token: let_,
1715 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001716 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001717 pat: Box::new(pat),
1718 expr: Box::new(value),
1719 body: while_block,
1720 label: lbl.map(|p| p.0),
1721 })
1722 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001723 }
1724
Michael Layzell734adb42017-06-07 16:58:31 -04001725 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001726 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001727 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001728 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001729 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001730 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001731 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001732 continue_token: cont,
1733 label: lbl,
1734 })
1735 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001736 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001737
Michael Layzell734adb42017-06-07 16:58:31 -04001738 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001739 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001740 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001741 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001742 // We can't allow blocks after a `break` expression when we wouldn't
1743 // allow structs, as this expression is ambiguous.
1744 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001745 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001746 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001747 label: lbl,
1748 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001749 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001750 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001751 ));
1752
Michael Layzell734adb42017-06-07 16:58:31 -04001753 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001754 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001755 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001756 // NOTE: return is greedy and eats blocks after it even when in a
1757 // position where structs are not allowed, such as in if statement
1758 // conditions. For example:
1759 //
David Tolnaybcf26022017-12-25 22:10:52 -05001760 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001761 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001762 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001763 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001764 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001765 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001766 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001767 ));
1768
Michael Layzell734adb42017-06-07 16:58:31 -04001769 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001770 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001771 named!(parse -> Self, do_parse!(
1772 path: syn!(Path) >>
1773 data: braces!(do_parse!(
1774 fields: call!(Delimited::parse_terminated) >>
1775 base: option!(
1776 cond!(fields.is_empty() || fields.trailing_delim(),
1777 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001778 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001779 base: syn!(Expr) >>
1780 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001781 )
Michael Layzell92639a52017-06-01 00:07:44 -04001782 )
1783 ) >>
1784 (fields, base)
1785 )) >>
1786 ({
1787 let ((fields, base), brace) = data;
1788 let (dots, rest) = match base.and_then(|b| b) {
1789 Some((dots, base)) => (Some(dots), Some(base)),
1790 None => (None, None),
1791 };
1792 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001793 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001794 brace_token: brace,
1795 path: path,
1796 fields: fields,
1797 dot2_token: dots,
1798 rest: rest.map(Box::new),
1799 }
1800 })
1801 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001802 }
1803
Michael Layzell734adb42017-06-07 16:58:31 -04001804 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001805 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001806 named!(parse -> Self, alt!(
1807 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001808 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001809 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001810 value: syn!(Expr) >>
1811 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001812 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001813 expr: value,
1814 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001815 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001816 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001817 })
Michael Layzell92639a52017-06-01 00:07:44 -04001818 )
1819 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001820 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001821 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001822 expr: Expr::Path(ExprPath {
1823 attrs: Vec::new(),
1824 qself: None,
1825 path: name.into(),
1826 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001827 is_shorthand: true,
1828 attrs: Vec::new(),
1829 colon_token: None,
1830 })
1831 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001832 }
David Tolnay055a7042016-10-02 19:23:54 -07001833
Michael Layzell734adb42017-06-07 16:58:31 -04001834 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001835 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001836 named!(parse -> Self, do_parse!(
1837 data: brackets!(do_parse!(
1838 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001839 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001840 times: syn!(Expr) >>
1841 (value, semi, times)
1842 )) >>
1843 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001844 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001845 expr: Box::new((data.0).0),
1846 amt: Box::new((data.0).2),
1847 bracket_token: data.1,
1848 semi_token: (data.0).1,
1849 })
1850 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001851 }
David Tolnay055a7042016-10-02 19:23:54 -07001852
Michael Layzell734adb42017-06-07 16:58:31 -04001853 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001854 impl Synom for ExprUnsafe {
1855 named!(parse -> Self, do_parse!(
1856 unsafe_: keyword!(unsafe) >>
1857 b: syn!(Block) >>
1858 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001859 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001860 unsafe_token: unsafe_,
1861 block: b,
1862 })
1863 ));
1864 }
1865
1866 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001867 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001868 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001869 b: syn!(Block) >>
1870 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001871 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001872 block: b,
1873 })
1874 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001875 }
David Tolnay89e05672016-10-02 14:39:42 -07001876
Michael Layzell734adb42017-06-07 16:58:31 -04001877 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001878 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001879 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001880 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001881 (ExprRange {
1882 attrs: Vec::new(),
1883 from: None,
1884 to: hi.map(Box::new),
1885 limits: limits,
1886 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001887 ));
1888
Michael Layzell734adb42017-06-07 16:58:31 -04001889 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001890 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001891 named!(parse -> Self, alt!(
1892 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001893 punct!(..=) => { RangeLimits::Closed }
1894 |
1895 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001896 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001897 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001898 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001899 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001900 }
David Tolnay438c9052016-10-07 23:24:48 -07001901
Alex Crichton954046c2017-05-30 21:49:42 -07001902 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001903 named!(parse -> Self, do_parse!(
1904 pair: qpath >>
1905 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001906 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001907 qself: pair.0,
1908 path: pair.1,
1909 })
1910 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001911 }
David Tolnay42602292016-10-01 22:25:45 -07001912
Michael Layzell734adb42017-06-07 16:58:31 -04001913 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001914 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001915
David Tolnay32954ef2017-12-26 22:43:16 -05001916 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001917
Michael Layzell734adb42017-06-07 16:58:31 -04001918 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001919 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001920 named!(parse -> Self, do_parse!(
1921 stmts: braces!(call!(Block::parse_within)) >>
1922 (Block {
1923 stmts: stmts.0,
1924 brace_token: stmts.1,
1925 })
1926 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001927 }
David Tolnay939766a2016-09-23 23:48:12 -07001928
Michael Layzell734adb42017-06-07 16:58:31 -04001929 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001930 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001931 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001932 many0!(punct!(;)) >>
1933 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001934 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001935 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001936 mut e: syn!(Expr) >>
1937 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001938 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001939 Stmt::Expr(Box::new(e))
1940 })
1941 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001942 (match last {
1943 None => standalone,
1944 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001945 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001946 standalone
1947 }
1948 })
1949 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001950 }
1951
Michael Layzell734adb42017-06-07 16:58:31 -04001952 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001953 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001954 named!(parse -> Self, alt!(
1955 stmt_mac
1956 |
1957 stmt_local
1958 |
1959 stmt_item
1960 |
Michael Layzell35418782017-06-07 09:20:25 -04001961 stmt_blockexpr
1962 |
Michael Layzell92639a52017-06-01 00:07:44 -04001963 stmt_expr
1964 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001965 }
David Tolnay939766a2016-09-23 23:48:12 -07001966
Michael Layzell734adb42017-06-07 16:58:31 -04001967 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001968 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001969 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001970 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001971 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001972 // Only parse braces here; paren and bracket will get parsed as
1973 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001974 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001975 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001976 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1977 attrs: attrs,
1978 ident: None,
1979 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001980 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001981 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001982 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001983 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001984 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001985 },
David Tolnayeea28d62016-10-25 20:44:08 -07001986 },
David Tolnay57b52bc2017-12-28 18:06:38 -05001987 semi_token: semi,
1988 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07001989 ));
1990
Michael Layzell734adb42017-06-07 16:58:31 -04001991 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001992 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001993 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001994 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001995 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001996 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001997 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1998 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001999 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07002000 let_token: let_,
2001 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002002 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
2003 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07002004 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002005 ty: ty.map(|p| Box::new(p.1)),
2006 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002007 attrs: attrs,
2008 })))
2009 ));
2010
Michael Layzell734adb42017-06-07 16:58:31 -04002011 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002012 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002013
Michael Layzell734adb42017-06-07 16:58:31 -04002014 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002015 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002016 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002017 mut e: expr_nosemi >>
2018 // If the next token is a `.` or a `?` it is special-cased to parse as
2019 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002020 not!(punct!(.)) >>
2021 not!(punct!(?)) >>
2022 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002023 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002024 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002025 if let Some(semi) = semi {
2026 Stmt::Semi(Box::new(e), semi)
2027 } else {
2028 Stmt::Expr(Box::new(e))
2029 }
2030 })
2031 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002032
Michael Layzell734adb42017-06-07 16:58:31 -04002033 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002034 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002035 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002036 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002037 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002038 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002039 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002040 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002041 })
David Tolnay939766a2016-09-23 23:48:12 -07002042 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002043
Michael Layzell734adb42017-06-07 16:58:31 -04002044 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002045 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002046 named!(parse -> Self, alt!(
2047 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2048 |
2049 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2050 |
2051 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2052 |
2053 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2054 |
2055 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2056 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002057 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002058 |
2059 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2060 |
2061 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2062 |
2063 syn!(PatPath) => { Pat::Path }
2064 |
2065 syn!(PatTuple) => { Pat::Tuple }
2066 |
2067 syn!(PatRef) => { Pat::Ref }
2068 |
2069 syn!(PatSlice) => { Pat::Slice }
2070 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002071 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002072
Michael Layzell734adb42017-06-07 16:58:31 -04002073 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002074 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002075 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002076 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002077 |u| PatWild { underscore_token: u }
2078 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002079 }
David Tolnay84aa0752016-10-02 23:01:13 -07002080
Michael Layzell734adb42017-06-07 16:58:31 -04002081 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002082 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002083 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002084 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002085 pat: syn!(Pat) >>
2086 (PatBox {
2087 pat: Box::new(pat),
2088 box_token: boxed,
2089 })
2090 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002091 }
2092
Michael Layzell734adb42017-06-07 16:58:31 -04002093 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002094 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002095 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002096 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002097 mutability: syn!(Mutability) >>
2098 name: alt!(
2099 syn!(Ident)
2100 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002101 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002102 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002103 not!(punct!(<)) >>
2104 not!(punct!(::)) >>
2105 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002106 (PatIdent {
2107 mode: match mode {
2108 Some(mode) => BindingMode::ByRef(mode, mutability),
2109 None => BindingMode::ByValue(mutability),
2110 },
2111 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002112 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002113 subpat: subpat.map(|p| Box::new(p.1)),
2114 })
2115 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002116 }
2117
Michael Layzell734adb42017-06-07 16:58:31 -04002118 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002119 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002120 named!(parse -> Self, do_parse!(
2121 path: syn!(Path) >>
2122 tuple: syn!(PatTuple) >>
2123 (PatTupleStruct {
2124 path: path,
2125 pat: tuple,
2126 })
2127 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002128 }
2129
Michael Layzell734adb42017-06-07 16:58:31 -04002130 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002131 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002132 named!(parse -> Self, do_parse!(
2133 path: syn!(Path) >>
2134 data: braces!(do_parse!(
2135 fields: call!(Delimited::parse_terminated) >>
2136 base: option!(
2137 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002138 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002139 ) >>
2140 (fields, base)
2141 )) >>
2142 (PatStruct {
2143 path: path,
2144 fields: (data.0).0,
2145 brace_token: data.1,
2146 dot2_token: (data.0).1.and_then(|m| m),
2147 })
2148 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002149 }
2150
Michael Layzell734adb42017-06-07 16:58:31 -04002151 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002152 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002153 named!(parse -> Self, alt!(
2154 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002155 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002156 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002157 pat: syn!(Pat) >>
2158 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002159 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002160 pat: Box::new(pat),
2161 is_shorthand: false,
2162 attrs: Vec::new(),
2163 colon_token: Some(colon),
2164 })
2165 )
2166 |
2167 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002168 boxed: option!(keyword!(box)) >>
2169 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002170 mutability: syn!(Mutability) >>
2171 ident: syn!(Ident) >>
2172 ({
2173 let mut pat: Pat = PatIdent {
2174 mode: if let Some(mode) = mode {
2175 BindingMode::ByRef(mode, mutability)
2176 } else {
2177 BindingMode::ByValue(mutability)
2178 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002179 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002180 subpat: None,
2181 at_token: None,
2182 }.into();
2183 if let Some(boxed) = boxed {
2184 pat = PatBox {
2185 pat: Box::new(pat),
2186 box_token: boxed,
2187 }.into();
2188 }
2189 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002190 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002191 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002192 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002193 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002194 colon_token: None,
2195 }
2196 })
2197 )
2198 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002199 }
2200
Michael Layzell734adb42017-06-07 16:58:31 -04002201 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002202 impl Synom for Member {
2203 named!(parse -> Self, alt!(
2204 syn!(Ident) => { Member::Named }
2205 |
2206 syn!(Index) => { Member::Unnamed }
2207 ));
2208 }
2209
2210 #[cfg(feature = "full")]
2211 impl Synom for Index {
2212 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002213 lit: syn!(Lit) >>
2214 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002215 if let Ok(i) = lit.value.to_string().parse() {
2216 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002217 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002218 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002219 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002220 })
David Tolnay85b69a42017-12-27 20:43:10 -05002221 ));
2222 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002223
Michael Layzell734adb42017-06-07 16:58:31 -04002224 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002225 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002226 named!(parse -> Self, map!(
2227 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002228 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002229 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002230 }
David Tolnay9636c052016-10-02 17:11:17 -07002231
Michael Layzell734adb42017-06-07 16:58:31 -04002232 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002233 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002234 named!(parse -> Self, do_parse!(
2235 data: parens!(do_parse!(
2236 elems: call!(Delimited::parse_terminated) >>
2237 dotdot: map!(cond!(
2238 elems.is_empty() || elems.trailing_delim(),
2239 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002240 dots: punct!(..) >>
2241 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002242 (dots, trailing)
2243 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002244 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002245 rest: cond!(match dotdot {
2246 Some((_, Some(_))) => true,
2247 _ => false,
2248 },
2249 call!(Delimited::parse_terminated)) >>
2250 (elems, dotdot, rest)
2251 )) >>
2252 ({
2253 let ((mut elems, dotdot, rest), parens) = data;
2254 let (dotdot, trailing) = match dotdot {
2255 Some((a, b)) => (Some(a), Some(b)),
2256 None => (None, None),
2257 };
2258 PatTuple {
2259 paren_token: parens,
2260 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2261 dot2_token: dotdot,
2262 comma_token: trailing.and_then(|b| b),
2263 pats: {
2264 if let Some(rest) = rest {
2265 for elem in rest {
2266 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002267 }
Michael Layzell92639a52017-06-01 00:07:44 -04002268 }
2269 elems
2270 },
2271 }
2272 })
2273 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002274 }
David Tolnayfbb73232016-10-03 01:00:06 -07002275
Michael Layzell734adb42017-06-07 16:58:31 -04002276 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002277 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002278 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002279 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002280 mutability: syn!(Mutability) >>
2281 pat: syn!(Pat) >>
2282 (PatRef {
2283 pat: Box::new(pat),
2284 mutbl: mutability,
2285 and_token: and,
2286 })
2287 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002288 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002289
Michael Layzell734adb42017-06-07 16:58:31 -04002290 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002291 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002292 named!(parse -> Self, do_parse!(
2293 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002294 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002295 return parse_error(); // these need to be parsed by pat_path
2296 } else {
2297 PatLit {
2298 expr: Box::new(lit),
2299 }
2300 })
2301 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002302 }
David Tolnaye1310902016-10-29 23:40:00 -07002303
Michael Layzell734adb42017-06-07 16:58:31 -04002304 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002305 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002306 named!(parse -> Self, do_parse!(
2307 lo: pat_lit_expr >>
2308 limits: syn!(RangeLimits) >>
2309 hi: pat_lit_expr >>
2310 (PatRange {
2311 lo: Box::new(lo),
2312 hi: Box::new(hi),
2313 limits: limits,
2314 })
2315 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002316 }
David Tolnaye1310902016-10-29 23:40:00 -07002317
Michael Layzell734adb42017-06-07 16:58:31 -04002318 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002319 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002320 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002321 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002322 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002323 |
David Tolnay8c91b882017-12-28 23:04:32 -05002324 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002325 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002326 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002327 Expr::Unary(ExprUnary {
2328 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002329 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002330 expr: Box::new(v.into())
2331 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002332 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002333 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002334 })
2335 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002336
Michael Layzell734adb42017-06-07 16:58:31 -04002337 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002338 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002339 named!(parse -> Self, map!(
2340 brackets!(do_parse!(
2341 before: call!(Delimited::parse_terminated) >>
2342 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002343 dots: punct!(..) >>
2344 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002345 (dots, trailing)
2346 )) >>
2347 after: cond!(
2348 match middle {
2349 Some((_, ref trailing)) => trailing.is_some(),
2350 _ => false,
2351 },
2352 call!(Delimited::parse_terminated)
2353 ) >>
2354 (before, middle, after)
2355 )),
2356 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002357 let mut before: Delimited<Pat, Token![,]> = before;
2358 let after: Option<Delimited<Pat, Token![,]>> = after;
2359 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002360 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002361 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002362 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002363 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002364 }),
2365 bracket_token: brackets,
2366 middle: middle.and_then(|_| {
2367 if !before.is_empty() && !before.trailing_delim() {
2368 Some(Box::new(before.pop().unwrap().into_item()))
2369 } else {
2370 None
2371 }
2372 }),
2373 front: before,
2374 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002375 }
Alex Crichton954046c2017-05-30 21:49:42 -07002376 }
Michael Layzell92639a52017-06-01 00:07:44 -04002377 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002378 }
David Tolnay435a9a82016-10-29 13:47:20 -07002379
Michael Layzell734adb42017-06-07 16:58:31 -04002380 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002381 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002382 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002383 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002384 |
2385 epsilon!() => { |_| CaptureBy::Ref }
2386 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002387 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002388}
2389
David Tolnayf4bbbd92016-09-23 14:41:55 -07002390#[cfg(feature = "printing")]
2391mod printing {
2392 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002393 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002394 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002395 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002396 #[cfg(feature = "full")]
2397 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002398
David Tolnaybcf26022017-12-25 22:10:52 -05002399 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2400 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002401 #[cfg(feature = "full")]
2402 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002403 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002404 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002405 e.to_tokens(tokens);
2406 });
2407 } else {
2408 e.to_tokens(tokens);
2409 }
2410 }
2411
David Tolnay8c91b882017-12-28 23:04:32 -05002412 #[cfg(feature = "full")]
2413 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2414 tokens.append_all(attrs.outer());
2415 }
Michael Layzell734adb42017-06-07 16:58:31 -04002416
David Tolnay8c91b882017-12-28 23:04:32 -05002417 #[cfg(not(feature = "full"))]
2418 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002419 }
2420
Michael Layzell734adb42017-06-07 16:58:31 -04002421 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002422 impl ToTokens for ExprBox {
2423 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002424 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002425 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002426 self.expr.to_tokens(tokens);
2427 }
2428 }
2429
Michael Layzell734adb42017-06-07 16:58:31 -04002430 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002431 impl ToTokens for ExprInPlace {
2432 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002433 tokens.append_all(self.attrs.outer());
Michael Layzell6a5a1642017-06-04 19:35:15 -04002434 match self.kind {
2435 InPlaceKind::Arrow(ref arrow) => {
2436 self.place.to_tokens(tokens);
2437 arrow.to_tokens(tokens);
2438 self.value.to_tokens(tokens);
2439 }
2440 InPlaceKind::In(ref _in) => {
2441 _in.to_tokens(tokens);
2442 self.place.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002443 // NOTE: The second operand must be in a block, add one if
2444 // it is not present.
David Tolnay8c91b882017-12-28 23:04:32 -05002445 if let Expr::Block(_) = *self.value {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002446 self.value.to_tokens(tokens);
2447 } else {
David Tolnay32954ef2017-12-26 22:43:16 -05002448 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002449 self.value.to_tokens(tokens);
2450 })
2451 }
Michael Layzell6a5a1642017-06-04 19:35:15 -04002452 }
2453 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002454 }
2455 }
2456
Michael Layzell734adb42017-06-07 16:58:31 -04002457 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002458 impl ToTokens for ExprArray {
2459 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002460 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002461 self.bracket_token.surround(tokens, |tokens| {
2462 self.exprs.to_tokens(tokens);
2463 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002464 }
2465 }
2466
2467 impl ToTokens for ExprCall {
2468 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002469 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002470 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002471 self.paren_token.surround(tokens, |tokens| {
2472 self.args.to_tokens(tokens);
2473 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002474 }
2475 }
2476
Michael Layzell734adb42017-06-07 16:58:31 -04002477 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002478 impl ToTokens for ExprMethodCall {
2479 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002480 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002481 self.expr.to_tokens(tokens);
2482 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002483 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002484 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002485 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2486 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002487 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002488 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002489 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002490 self.paren_token.surround(tokens, |tokens| {
2491 self.args.to_tokens(tokens);
2492 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002493 }
2494 }
2495
Michael Layzell734adb42017-06-07 16:58:31 -04002496 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002497 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002498 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002499 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002500 self.paren_token.surround(tokens, |tokens| {
2501 self.args.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002502 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002503 // distinguish ExprTuple from ExprParen.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002504 if self.args.len() == 1 && !self.args.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002505 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002506 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002507 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002508 }
2509 }
2510
2511 impl ToTokens for ExprBinary {
2512 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002513 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002514 self.left.to_tokens(tokens);
2515 self.op.to_tokens(tokens);
2516 self.right.to_tokens(tokens);
2517 }
2518 }
2519
2520 impl ToTokens for ExprUnary {
2521 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002522 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002523 self.op.to_tokens(tokens);
2524 self.expr.to_tokens(tokens);
2525 }
2526 }
2527
David Tolnay8c91b882017-12-28 23:04:32 -05002528 impl ToTokens for ExprLit {
2529 fn to_tokens(&self, tokens: &mut Tokens) {
2530 attrs_to_tokens(&self.attrs, tokens);
2531 self.lit.to_tokens(tokens);
2532 }
2533 }
2534
Alex Crichton62a0a592017-05-22 13:58:53 -07002535 impl ToTokens for ExprCast {
2536 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002537 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002538 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002539 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002540 self.ty.to_tokens(tokens);
2541 }
2542 }
2543
2544 impl ToTokens for ExprType {
2545 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002546 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002547 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002548 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002549 self.ty.to_tokens(tokens);
2550 }
2551 }
2552
Michael Layzell734adb42017-06-07 16:58:31 -04002553 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002554 fn maybe_wrap_else(
2555 tokens: &mut Tokens,
2556 else_token: &Option<Token![else]>,
2557 if_false: &Option<Box<Expr>>,
2558 ) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002559 if let Some(ref if_false) = *if_false {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002560 TokensOrDefault(else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002561
2562 // If we are not one of the valid expressions to exist in an else
2563 // clause, wrap ourselves in a block.
David Tolnay8c91b882017-12-28 23:04:32 -05002564 match **if_false {
2565 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002566 if_false.to_tokens(tokens);
2567 }
2568 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002569 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002570 if_false.to_tokens(tokens);
2571 });
2572 }
2573 }
2574 }
2575 }
2576
2577 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002578 impl ToTokens for ExprIf {
2579 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002580 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002581 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002582 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002583 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002584 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002585 }
2586 }
2587
Michael Layzell734adb42017-06-07 16:58:31 -04002588 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002589 impl ToTokens for ExprIfLet {
2590 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002591 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002592 self.if_token.to_tokens(tokens);
2593 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002594 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002595 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002596 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002597 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002598 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002599 }
2600 }
2601
Michael Layzell734adb42017-06-07 16:58:31 -04002602 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002603 impl ToTokens for ExprWhile {
2604 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002605 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002606 if self.label.is_some() {
2607 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002608 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002609 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002610 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002611 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002612 self.body.to_tokens(tokens);
2613 }
2614 }
2615
Michael Layzell734adb42017-06-07 16:58:31 -04002616 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002617 impl ToTokens for ExprWhileLet {
2618 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002619 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002620 if self.label.is_some() {
2621 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002622 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002623 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002624 self.while_token.to_tokens(tokens);
2625 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002626 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002627 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002628 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002629 self.body.to_tokens(tokens);
2630 }
2631 }
2632
Michael Layzell734adb42017-06-07 16:58:31 -04002633 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002634 impl ToTokens for ExprForLoop {
2635 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002636 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002637 if self.label.is_some() {
2638 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002639 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002640 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002641 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002642 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002643 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002644 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002645 self.body.to_tokens(tokens);
2646 }
2647 }
2648
Michael Layzell734adb42017-06-07 16:58:31 -04002649 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002650 impl ToTokens for ExprLoop {
2651 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002652 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002653 if self.label.is_some() {
2654 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002655 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002656 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002657 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002658 self.body.to_tokens(tokens);
2659 }
2660 }
2661
Michael Layzell734adb42017-06-07 16:58:31 -04002662 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002663 impl ToTokens for ExprMatch {
2664 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002665 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002666 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002667 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002668 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002669 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002670 arm.to_tokens(tokens);
2671 // Ensure that we have a comma after a non-block arm, except
2672 // for the last one.
2673 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002674 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002675 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002676 }
2677 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002678 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002679 }
2680 }
2681
Michael Layzell734adb42017-06-07 16:58:31 -04002682 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002683 impl ToTokens for ExprCatch {
2684 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002685 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002686 self.do_token.to_tokens(tokens);
2687 self.catch_token.to_tokens(tokens);
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 Crichtonfe110462017-06-01 12:49:27 -07002693 impl ToTokens for ExprYield {
2694 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002695 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002696 self.yield_token.to_tokens(tokens);
2697 self.expr.to_tokens(tokens);
2698 }
2699 }
2700
2701 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002702 impl ToTokens for ExprClosure {
2703 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002704 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002705 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002706 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002707 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002708 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002709 FnArg::Captured(ArgCaptured {
2710 ref pat,
2711 ty: Type::Infer(_),
2712 ..
2713 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002714 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002715 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002716 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002717 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002718 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002719 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002720 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002721 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002722 self.body.to_tokens(tokens);
2723 }
2724 }
2725
Michael Layzell734adb42017-06-07 16:58:31 -04002726 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002727 impl ToTokens for ExprUnsafe {
2728 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002729 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002730 self.unsafe_token.to_tokens(tokens);
2731 self.block.to_tokens(tokens);
2732 }
2733 }
2734
2735 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002736 impl ToTokens for ExprBlock {
2737 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002738 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002739 self.block.to_tokens(tokens);
2740 }
2741 }
2742
Michael Layzell734adb42017-06-07 16:58:31 -04002743 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002744 impl ToTokens for ExprAssign {
2745 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002746 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002747 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002748 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002749 self.right.to_tokens(tokens);
2750 }
2751 }
2752
Michael Layzell734adb42017-06-07 16:58:31 -04002753 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002754 impl ToTokens for ExprAssignOp {
2755 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002756 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002757 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002758 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002759 self.right.to_tokens(tokens);
2760 }
2761 }
2762
Michael Layzell734adb42017-06-07 16:58:31 -04002763 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002764 impl ToTokens for ExprField {
2765 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002766 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002767 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002768 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002769 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002770 }
2771 }
2772
Michael Layzell734adb42017-06-07 16:58:31 -04002773 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002774 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002775 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002776 match *self {
2777 Member::Named(ident) => ident.to_tokens(tokens),
2778 Member::Unnamed(ref index) => index.to_tokens(tokens),
2779 }
2780 }
2781 }
2782
2783 #[cfg(feature = "full")]
2784 impl ToTokens for Index {
2785 fn to_tokens(&self, tokens: &mut Tokens) {
2786 tokens.append(TokenTree {
2787 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002788 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002789 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002790 }
2791 }
2792
2793 impl ToTokens for ExprIndex {
2794 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002795 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002796 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002797 self.bracket_token.surround(tokens, |tokens| {
2798 self.index.to_tokens(tokens);
2799 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002800 }
2801 }
2802
Michael Layzell734adb42017-06-07 16:58:31 -04002803 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002804 impl ToTokens for ExprRange {
2805 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002806 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002807 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002808 match self.limits {
2809 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2810 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2811 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002812 self.to.to_tokens(tokens);
2813 }
2814 }
2815
2816 impl ToTokens for ExprPath {
2817 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002818 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002819 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002820 }
2821 }
2822
Michael Layzell734adb42017-06-07 16:58:31 -04002823 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002824 impl ToTokens for ExprAddrOf {
2825 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002826 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002827 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002828 self.mutbl.to_tokens(tokens);
2829 self.expr.to_tokens(tokens);
2830 }
2831 }
2832
Michael Layzell734adb42017-06-07 16:58:31 -04002833 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002834 impl ToTokens for ExprBreak {
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.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002838 self.label.to_tokens(tokens);
2839 self.expr.to_tokens(tokens);
2840 }
2841 }
2842
Michael Layzell734adb42017-06-07 16:58:31 -04002843 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002844 impl ToTokens for ExprContinue {
2845 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002846 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002847 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002848 self.label.to_tokens(tokens);
2849 }
2850 }
2851
Michael Layzell734adb42017-06-07 16:58:31 -04002852 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002853 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002854 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002855 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002856 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002857 self.expr.to_tokens(tokens);
2858 }
2859 }
2860
Michael Layzell734adb42017-06-07 16:58:31 -04002861 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002862 impl ToTokens for ExprMacro {
2863 fn to_tokens(&self, tokens: &mut Tokens) {
2864 tokens.append_all(self.attrs.outer());
2865 self.mac.to_tokens(tokens);
2866 }
2867 }
2868
2869 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002870 impl ToTokens for ExprStruct {
2871 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002872 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002873 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002874 self.brace_token.surround(tokens, |tokens| {
2875 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002876 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002877 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002878 self.rest.to_tokens(tokens);
2879 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002880 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002881 }
2882 }
2883
Michael Layzell734adb42017-06-07 16:58:31 -04002884 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002885 impl ToTokens for ExprRepeat {
2886 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002887 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002888 self.bracket_token.surround(tokens, |tokens| {
2889 self.expr.to_tokens(tokens);
2890 self.semi_token.to_tokens(tokens);
2891 self.amt.to_tokens(tokens);
2892 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002893 }
2894 }
2895
Michael Layzell93c36282017-06-04 20:43:14 -04002896 impl ToTokens for ExprGroup {
2897 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002898 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002899 self.group_token.surround(tokens, |tokens| {
2900 self.expr.to_tokens(tokens);
2901 });
2902 }
2903 }
2904
Alex Crichton62a0a592017-05-22 13:58:53 -07002905 impl ToTokens for ExprParen {
2906 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002907 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002908 self.paren_token.surround(tokens, |tokens| {
2909 self.expr.to_tokens(tokens);
2910 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002911 }
2912 }
2913
Michael Layzell734adb42017-06-07 16:58:31 -04002914 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002915 impl ToTokens for ExprTry {
2916 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002917 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002919 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002920 }
2921 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002922
Michael Layzell734adb42017-06-07 16:58:31 -04002923 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002924 impl ToTokens for FieldValue {
2925 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002926 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002927 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2928 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002929 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002930 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002931 self.expr.to_tokens(tokens);
2932 }
David Tolnay055a7042016-10-02 19:23:54 -07002933 }
2934 }
2935
Michael Layzell734adb42017-06-07 16:58:31 -04002936 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002937 impl ToTokens for Arm {
2938 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002939 tokens.append_all(&self.attrs);
2940 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002941 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002942 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002943 self.guard.to_tokens(tokens);
2944 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002945 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002946 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002947 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002948 }
2949 }
2950
Michael Layzell734adb42017-06-07 16:58:31 -04002951 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002952 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002953 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 self.underscore_token.to_tokens(tokens);
2955 }
2956 }
2957
Michael Layzell734adb42017-06-07 16:58:31 -04002958 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002959 impl ToTokens for PatIdent {
2960 fn to_tokens(&self, tokens: &mut Tokens) {
2961 self.mode.to_tokens(tokens);
2962 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002963 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002964 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002965 self.subpat.to_tokens(tokens);
2966 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002967 }
2968 }
2969
Michael Layzell734adb42017-06-07 16:58:31 -04002970 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002971 impl ToTokens for PatStruct {
2972 fn to_tokens(&self, tokens: &mut Tokens) {
2973 self.path.to_tokens(tokens);
2974 self.brace_token.surround(tokens, |tokens| {
2975 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002976 // NOTE: We need a comma before the dot2 token if it is present.
2977 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002978 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002979 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002980 self.dot2_token.to_tokens(tokens);
2981 });
2982 }
2983 }
2984
Michael Layzell734adb42017-06-07 16:58:31 -04002985 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002986 impl ToTokens for PatTupleStruct {
2987 fn to_tokens(&self, tokens: &mut Tokens) {
2988 self.path.to_tokens(tokens);
2989 self.pat.to_tokens(tokens);
2990 }
2991 }
2992
Michael Layzell734adb42017-06-07 16:58:31 -04002993 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002994 impl ToTokens for PatPath {
2995 fn to_tokens(&self, tokens: &mut Tokens) {
2996 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2997 }
2998 }
2999
Michael Layzell734adb42017-06-07 16:58:31 -04003000 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003001 impl ToTokens for PatTuple {
3002 fn to_tokens(&self, tokens: &mut Tokens) {
3003 self.paren_token.surround(tokens, |tokens| {
3004 for (i, token) in self.pats.iter().enumerate() {
3005 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07003006 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
3007 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003008 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003009 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003010 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011
3012 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003013 // Ensure there is a comma before the .. token.
3014 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003015 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003016 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003017 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003018 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003019 });
3020 }
3021 }
3022
Michael Layzell734adb42017-06-07 16:58:31 -04003023 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003024 impl ToTokens for PatBox {
3025 fn to_tokens(&self, tokens: &mut Tokens) {
3026 self.box_token.to_tokens(tokens);
3027 self.pat.to_tokens(tokens);
3028 }
3029 }
3030
Michael Layzell734adb42017-06-07 16:58:31 -04003031 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003032 impl ToTokens for PatRef {
3033 fn to_tokens(&self, tokens: &mut Tokens) {
3034 self.and_token.to_tokens(tokens);
3035 self.mutbl.to_tokens(tokens);
3036 self.pat.to_tokens(tokens);
3037 }
3038 }
3039
Michael Layzell734adb42017-06-07 16:58:31 -04003040 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003041 impl ToTokens for PatLit {
3042 fn to_tokens(&self, tokens: &mut Tokens) {
3043 self.expr.to_tokens(tokens);
3044 }
3045 }
3046
Michael Layzell734adb42017-06-07 16:58:31 -04003047 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003048 impl ToTokens for PatRange {
3049 fn to_tokens(&self, tokens: &mut Tokens) {
3050 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003051 match self.limits {
3052 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3053 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3054 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003055 self.hi.to_tokens(tokens);
3056 }
3057 }
3058
Michael Layzell734adb42017-06-07 16:58:31 -04003059 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003060 impl ToTokens for PatSlice {
3061 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003062 // XXX: This is a mess, and it will be so easy to screw it up. How
3063 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003064 self.bracket_token.surround(tokens, |tokens| {
3065 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003066
3067 // If we need a comma before the middle or standalone .. token,
3068 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003069 if !self.front.empty_or_trailing()
3070 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003071 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003072 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003073 }
3074
3075 // If we have an identifier, we always need a .. token.
3076 if self.middle.is_some() {
3077 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003078 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003079 } else if self.dot2_token.is_some() {
3080 self.dot2_token.to_tokens(tokens);
3081 }
3082
3083 // Make sure we have a comma before the back half.
3084 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003085 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003086 self.back.to_tokens(tokens);
3087 } else {
3088 self.comma_token.to_tokens(tokens);
3089 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003090 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003091 }
3092 }
3093
Michael Layzell734adb42017-06-07 16:58:31 -04003094 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003095 impl ToTokens for FieldPat {
3096 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003097 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07003098 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05003099 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003100 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003101 }
3102 self.pat.to_tokens(tokens);
3103 }
3104 }
3105
Michael Layzell734adb42017-06-07 16:58:31 -04003106 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003107 impl ToTokens for BindingMode {
3108 fn to_tokens(&self, tokens: &mut Tokens) {
3109 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003110 BindingMode::ByRef(ref t, ref m) => {
3111 t.to_tokens(tokens);
3112 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003113 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003114 BindingMode::ByValue(ref m) => {
3115 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003116 }
3117 }
3118 }
3119 }
David Tolnay42602292016-10-01 22:25:45 -07003120
Michael Layzell734adb42017-06-07 16:58:31 -04003121 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07003122 impl ToTokens for CaptureBy {
3123 fn to_tokens(&self, tokens: &mut Tokens) {
3124 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003125 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003126 CaptureBy::Ref => {
3127 // nothing
3128 }
David Tolnay89e05672016-10-02 14:39:42 -07003129 }
3130 }
3131 }
3132
Michael Layzell734adb42017-06-07 16:58:31 -04003133 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003134 impl ToTokens for Block {
3135 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003136 self.brace_token.surround(tokens, |tokens| {
3137 tokens.append_all(&self.stmts);
3138 });
David Tolnay42602292016-10-01 22:25:45 -07003139 }
3140 }
3141
Michael Layzell734adb42017-06-07 16:58:31 -04003142 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003143 impl ToTokens for Stmt {
3144 fn to_tokens(&self, tokens: &mut Tokens) {
3145 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003146 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003147 Stmt::Item(ref item) => item.to_tokens(tokens),
3148 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003149 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003150 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003151 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003152 }
David Tolnay42602292016-10-01 22:25:45 -07003153 }
3154 }
3155 }
David Tolnay191e0582016-10-02 18:31:09 -07003156
Michael Layzell734adb42017-06-07 16:58:31 -04003157 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003158 impl ToTokens for Local {
3159 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003160 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003162 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003163 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003164 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003165 self.ty.to_tokens(tokens);
3166 }
3167 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003168 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003169 self.init.to_tokens(tokens);
3170 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003171 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003172 }
3173 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003174}