blob: 6ce039086e38b6e6983a81665a93630abb83d157 [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 -05003use proc_macro2::Span;
David Tolnay14982012017-12-29 00:49:51 -05004#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -05005use std::hash::{Hash, Hasher};
David Tolnayf4bbbd92016-09-23 14:41:55 -07006
Alex Crichton62a0a592017-05-22 13:58:53 -07007ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -05008 /// An expression.
9 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070010 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040011 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050012 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080013 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050014 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070015 }),
Clar Charrd22b5702017-03-10 15:24:56 -050016
David Tolnay8701a5c2017-12-28 23:31:10 -050017 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040018 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050019 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070020 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050021 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070022 pub value: Box<Expr>,
23 }),
Clar Charrd22b5702017-03-10 15:24:56 -050024
Alex Crichton62a0a592017-05-22 13:58:53 -070025 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040026 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050027 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050028 pub bracket_token: token::Bracket,
David Tolnay2a86fdd2017-12-28 23:34:28 -050029 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070030 }),
Clar Charrd22b5702017-03-10 15:24:56 -050031
Alex Crichton62a0a592017-05-22 13:58:53 -070032 /// A function call.
33 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050034 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070035 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050036 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050037 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070038 }),
Clar Charrd22b5702017-03-10 15:24:56 -050039
Alex Crichton62a0a592017-05-22 13:58:53 -070040 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
41 ///
42 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080043 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070044 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040045 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050046 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -050047 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080048 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050049 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -050050 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050051 pub paren_token: token::Paren,
52 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070053 }),
Clar Charrd22b5702017-03-10 15:24:56 -050054
Alex Crichton62a0a592017-05-22 13:58:53 -070055 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050056 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050057 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050058 pub paren_token: token::Paren,
David Tolnay2a86fdd2017-12-28 23:34:28 -050059 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070060 }),
Clar Charrd22b5702017-03-10 15:24:56 -050061
Alex Crichton62a0a592017-05-22 13:58:53 -070062 /// A binary operation, e.g. `a + b`, `a * b`.
63 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050064 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070065 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050066 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070067 pub right: Box<Expr>,
68 }),
Clar Charrd22b5702017-03-10 15:24:56 -050069
Alex Crichton62a0a592017-05-22 13:58:53 -070070 /// A unary operation, e.g. `!x`, `*x`.
71 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050072 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070073 pub op: UnOp,
74 pub expr: Box<Expr>,
75 }),
Clar Charrd22b5702017-03-10 15:24:56 -050076
Alex Crichton62a0a592017-05-22 13:58:53 -070077 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050078 pub Lit(ExprLit {
79 pub attrs: Vec<Attribute>,
80 pub lit: Lit,
81 }),
Clar Charrd22b5702017-03-10 15:24:56 -050082
Alex Crichton62a0a592017-05-22 13:58:53 -070083 /// A cast, e.g. `foo as f64`.
84 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050085 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070086 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080087 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080088 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070089 }),
Clar Charrd22b5702017-03-10 15:24:56 -050090
Alex Crichton62a0a592017-05-22 13:58:53 -070091 /// A type ascription, e.g. `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -050092 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -050093 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070094 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080095 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080096 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 }),
Clar Charrd22b5702017-03-10 15:24:56 -050098
Alex Crichton62a0a592017-05-22 13:58:53 -070099 /// An `if` block, with an optional else block
100 ///
101 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400102 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500103 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500104 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500106 pub then_branch: Block,
107 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700108 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500109
Alex Crichton62a0a592017-05-22 13:58:53 -0700110 /// An `if let` expression with an optional else block
111 ///
112 /// E.g., `if let pat = expr { block } else { expr }`
113 ///
114 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400115 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500116 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800117 pub if_token: Token![if],
118 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500119 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800120 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500121 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500122 pub then_branch: Block,
123 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700124 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500125
Alex Crichton62a0a592017-05-22 13:58:53 -0700126 /// A while loop, with an optional label
127 ///
128 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400129 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500130 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700131 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800132 pub colon_token: Option<Token![:]>,
133 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500134 pub cond: Box<Expr>,
135 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500137
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 /// A while-let loop, with an optional label.
139 ///
140 /// E.g., `'label: while let pat = expr { block }`
141 ///
142 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400143 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500144 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700145 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800146 pub colon_token: Option<Token![:]>,
147 pub while_token: Token![while],
148 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500149 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800150 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500151 pub expr: Box<Expr>,
152 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700153 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500154
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 /// A for loop, with an optional label.
156 ///
157 /// E.g., `'label: for pat in expr { block }`
158 ///
159 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400160 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500161 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500162 pub label: Option<Lifetime>,
163 pub colon_token: Option<Token![:]>,
164 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500166 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700167 pub expr: Box<Expr>,
168 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700169 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500170
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 /// Conditionless loop with an optional label.
172 ///
173 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400174 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500175 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700176 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800177 pub colon_token: Option<Token![:]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500178 pub loop_token: Token![loop],
179 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500181
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400183 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800185 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500187 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub arms: Vec<Arm>,
189 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500190
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400192 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500193 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 pub capture: CaptureBy,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800195 pub or1_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500196 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800197 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500198 pub output: ReturnType,
199 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500201
Nika Layzell640832a2017-12-04 13:37:09 -0500202 /// An unsafe block (`unsafe { ... }`)
203 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500204 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500205 pub unsafe_token: Token![unsafe],
206 pub block: Block,
207 }),
208
209 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400210 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500211 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700212 pub block: Block,
213 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700214
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400216 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500217 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800219 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500220 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223 /// An assignment with an operator
224 ///
225 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700228 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500229 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700230 pub right: Box<Expr>,
231 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500232
David Tolnay85b69a42017-12-27 20:43:10 -0500233 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
234 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400235 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500236 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500237 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800238 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500239 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500241
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 /// An indexing operation (`foo[2]`)
243 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500244 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500246 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500249
David Tolnaybe55d7b2017-12-17 23:41:20 -0800250 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400251 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500252 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500255 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700257
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 /// Variable reference, possibly containing `::` and/or type
259 /// parameters, e.g. foo::bar::<baz>.
260 ///
261 /// Optionally "qualified",
262 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
263 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500264 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 pub qself: Option<QSelf>,
266 pub path: Path,
267 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700268
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400270 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500271 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 pub mutbl: Mutability,
274 pub expr: Box<Expr>,
275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400278 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500279 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500280 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700281 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 pub expr: Option<Box<Expr>>,
283 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500284
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400286 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500287 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500289 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500291
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500293 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700298
Alex Crichton62a0a592017-05-22 13:58:53 -0700299 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500300 pub Macro(ExprMacro #full {
301 pub attrs: Vec<Attribute>,
302 pub mac: Macro,
303 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A struct literal expression.
306 ///
307 /// For example, `Foo {x: 1, y: 2}`, or
308 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400309 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500310 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500312 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500313 pub fields: Delimited<FieldValue, Token![,]>,
314 pub dot2_token: Option<Token![..]>,
315 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700316 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700317
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 /// An array literal constructed from one repeated element.
319 ///
320 /// For example, `[1; 5]`. The first expression is the element
321 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400322 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500323 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500324 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700325 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500326 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 pub amt: Box<Expr>,
328 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700329
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500331 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500332 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500333 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500334 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700336
Michael Layzell93c36282017-06-04 20:43:14 -0400337 /// No-op: used solely so we can pretty-print faithfully
338 ///
339 /// A `group` represents a `None`-delimited span in the input
340 /// `TokenStream` which affects the precidence of the resulting
341 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500342 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500343 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500344 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500345 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400346 }),
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400349 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500350 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800352 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700354
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 /// A catch expression.
356 ///
357 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400358 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500359 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub do_token: Token![do],
361 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 pub block: Block,
363 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700364
365 /// A yield expression.
366 ///
367 /// E.g. `yield expr`
368 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500369 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700371 pub expr: Option<Box<Expr>>,
372 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700373 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700374}
375
David Tolnay8c91b882017-12-28 23:04:32 -0500376impl Expr {
377 // Not public API.
378 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500379 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500380 pub fn attrs_mut(&mut self) -> &mut Vec<Attribute> {
381 match *self {
382 Expr::Box(ExprBox { ref mut attrs, .. }) |
383 Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
384 Expr::Array(ExprArray { ref mut attrs, .. }) |
385 Expr::Call(ExprCall { ref mut attrs, .. }) |
386 Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
387 Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
388 Expr::Binary(ExprBinary { ref mut attrs, .. }) |
389 Expr::Unary(ExprUnary { ref mut attrs, .. }) |
390 Expr::Lit(ExprLit { ref mut attrs, .. }) |
391 Expr::Cast(ExprCast { ref mut attrs, .. }) |
392 Expr::Type(ExprType { ref mut attrs, .. }) |
393 Expr::If(ExprIf { ref mut attrs, .. }) |
394 Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
395 Expr::While(ExprWhile { ref mut attrs, .. }) |
396 Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
397 Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
398 Expr::Loop(ExprLoop { ref mut attrs, .. }) |
399 Expr::Match(ExprMatch { ref mut attrs, .. }) |
400 Expr::Closure(ExprClosure { ref mut attrs, .. }) |
401 Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
402 Expr::Block(ExprBlock { ref mut attrs, .. }) |
403 Expr::Assign(ExprAssign { ref mut attrs, .. }) |
404 Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
405 Expr::Field(ExprField { ref mut attrs, .. }) |
406 Expr::Index(ExprIndex { ref mut attrs, .. }) |
407 Expr::Range(ExprRange { ref mut attrs, .. }) |
408 Expr::Path(ExprPath { ref mut attrs, .. }) |
409 Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
410 Expr::Break(ExprBreak { ref mut attrs, .. }) |
411 Expr::Continue(ExprContinue { ref mut attrs, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500412 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500413 Expr::Macro(ExprMacro { ref mut attrs, .. }) |
414 Expr::Struct(ExprStruct { ref mut attrs, .. }) |
415 Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
416 Expr::Paren(ExprParen { ref mut attrs, .. }) |
417 Expr::Group(ExprGroup { ref mut attrs, .. }) |
418 Expr::Try(ExprTry { ref mut attrs, .. }) |
419 Expr::Catch(ExprCatch { ref mut attrs, .. }) |
420 Expr::Yield(ExprYield { ref mut attrs, .. }) => attrs,
421 }
422 }
423}
424
David Tolnay85b69a42017-12-27 20:43:10 -0500425ast_enum! {
426 /// A struct or tuple struct field accessed in a struct literal or field
427 /// expression.
428 pub enum Member {
429 /// A named field like `self.x`.
430 Named(Ident),
431 /// An unnamed field like `self.0`.
432 Unnamed(Index),
433 }
434}
435
David Tolnay85b69a42017-12-27 20:43:10 -0500436ast_struct! {
437 /// The index of an unnamed tuple struct field.
438 pub struct Index #manual_extra_traits {
439 pub index: u32,
440 pub span: Span,
441 }
442}
443
David Tolnay14982012017-12-29 00:49:51 -0500444impl From<usize> for Index {
445 fn from(index: usize) -> Index {
446 assert!(index < std::u32::MAX as usize);
447 Index {
448 index: index as u32,
449 span: Span::default(),
450 }
451 }
452}
453
454#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500455impl Eq for Index {}
456
David Tolnay14982012017-12-29 00:49:51 -0500457#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500458impl PartialEq for Index {
459 fn eq(&self, other: &Self) -> bool {
460 self.index == other.index
461 }
462}
463
David Tolnay14982012017-12-29 00:49:51 -0500464#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500465impl Hash for Index {
466 fn hash<H: Hasher>(&self, state: &mut H) {
467 self.index.hash(state);
468 }
469}
470
471#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700472ast_struct! {
David Tolnayd60cfec2017-12-29 00:21:38 -0500473 pub struct MethodTurbofish {
474 pub colon2_token: Token![::],
475 pub lt_token: Token![<],
476 pub args: Delimited<GenericMethodArgument, Token![,]>,
477 pub gt_token: Token![>],
478 }
479}
480
481#[cfg(feature = "full")]
482ast_enum! {
483 /// A individual generic argument like `T`.
484 pub enum GenericMethodArgument {
485 /// The type parameters for this path segment, if present.
486 Type(Type),
487 /// Const expression. Must be inside of a block.
488 ///
489 /// NOTE: Identity expressions are represented as Type arguments, as
490 /// they are indistinguishable syntactically.
491 Const(Expr),
492 }
493}
494
495#[cfg(feature = "full")]
496ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700497 /// A field-value pair in a struct literal.
498 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500499 /// Attributes tagged on the field.
500 pub attrs: Vec<Attribute>,
501
502 /// Name or index of the field.
503 pub member: Member,
504
505 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500506
Alex Crichton62a0a592017-05-22 13:58:53 -0700507 /// Value of the field.
508 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500509
Alex Crichton62a0a592017-05-22 13:58:53 -0700510 /// Whether this is a shorthand field, e.g. `Struct { x }`
511 /// instead of `Struct { x: x }`.
512 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 }
David Tolnay055a7042016-10-02 19:23:54 -0700514}
515
Michael Layzell734adb42017-06-07 16:58:31 -0400516#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700517ast_struct! {
518 /// A Block (`{ .. }`).
519 ///
520 /// E.g. `{ .. }` as in `fn foo() { .. }`
521 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500522 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700523 /// Statements in a block
524 pub stmts: Vec<Stmt>,
525 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700526}
527
Michael Layzell734adb42017-06-07 16:58:31 -0400528#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700529ast_enum! {
530 /// A statement, usually ending in a semicolon.
531 pub enum Stmt {
532 /// A local (let) binding.
533 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700534
Alex Crichton62a0a592017-05-22 13:58:53 -0700535 /// An item definition.
536 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700537
Alex Crichton62a0a592017-05-22 13:58:53 -0700538 /// Expr without trailing semicolon.
539 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700540
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800542 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700543 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700544}
545
Michael Layzell734adb42017-06-07 16:58:31 -0400546#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700547ast_struct! {
548 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
549 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500550 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800551 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700552 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500553 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800554 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500555 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700556 /// Initializer expression to set the value, if any
557 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500558 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700560}
561
Michael Layzell734adb42017-06-07 16:58:31 -0400562#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700563ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700564 // Clippy false positive
565 // https://github.com/Manishearth/rust-clippy/issues/1241
566 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
567 pub enum Pat {
568 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700569 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800570 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700571 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700572
Alex Crichton62a0a592017-05-22 13:58:53 -0700573 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
574 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
575 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
576 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700577 pub Ident(PatIdent {
578 pub mode: BindingMode,
579 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800580 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500581 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700582 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700583
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
585 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700586 pub Struct(PatStruct {
587 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500588 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500589 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700591 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700592
Alex Crichton62a0a592017-05-22 13:58:53 -0700593 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
594 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
595 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700596 pub TupleStruct(PatTupleStruct {
597 pub path: Path,
598 pub pat: PatTuple,
599 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700600
Alex Crichton62a0a592017-05-22 13:58:53 -0700601 /// A possibly qualified path pattern.
602 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
603 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
604 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700605 pub Path(PatPath {
606 pub qself: Option<QSelf>,
607 pub path: Path,
608 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700609
Alex Crichton62a0a592017-05-22 13:58:53 -0700610 /// A tuple pattern `(a, b)`.
611 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
612 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700613 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500614 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500615 pub pats: Delimited<Pat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800616 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500617 pub dots_pos: Option<usize>,
618 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700619 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700620 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700621 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500623 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700624 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700625 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700626 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 pub and_token: Token![&],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500628 pub mutbl: Mutability,
629 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700630 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700631 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700632 pub Lit(PatLit {
633 pub expr: Box<Expr>,
634 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800635 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700636 pub Range(PatRange {
637 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700638 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500639 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700640 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400641 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700642 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500643 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800644 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700645 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800646 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500647 pub dot2_token: Option<Token![..]>,
648 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700649 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700650 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800651 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700653}
654
Michael Layzell734adb42017-06-07 16:58:31 -0400655#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700656ast_struct! {
657 /// An arm of a 'match'.
658 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800659 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700660 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500661 /// ```rust
662 /// # #![feature(dotdoteq_in_patterns)]
663 /// #
664 /// # fn main() {
665 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700666 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500667 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700668 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500669 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700670 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500671 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700672 /// ```
673 pub struct Arm {
674 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800675 pub pats: Delimited<Pat, Token![|]>,
676 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700677 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800678 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700679 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800680 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700681 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700682}
683
Michael Layzell734adb42017-06-07 16:58:31 -0400684#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700685ast_enum! {
686 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700687 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800689 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700690 Ref,
691 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700692}
693
Michael Layzell734adb42017-06-07 16:58:31 -0400694#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700695ast_enum! {
696 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700697 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700698 pub enum RangeLimits {
699 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800700 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700701 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800702 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700704}
705
Michael Layzell734adb42017-06-07 16:58:31 -0400706#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700707ast_struct! {
708 /// A single field in a struct pattern
709 ///
710 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
711 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
712 /// except `is_shorthand` is true
713 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500714 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700715 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500716 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500717 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 /// The pattern the field is destructured to
719 pub pat: Box<Pat>,
720 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700722}
723
Michael Layzell734adb42017-06-07 16:58:31 -0400724#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700725ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700726 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700727 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800728 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700729 ByValue(Mutability),
730 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700731}
732
Michael Layzell3936ceb2017-07-08 00:28:36 -0400733#[cfg(any(feature = "parsing", feature = "printing"))]
734#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700735fn arm_expr_requires_comma(expr: &Expr) -> bool {
736 // see https://github.com/rust-lang/rust/blob/eb8f2586e
737 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500738 match *expr {
739 Expr::Unsafe(..)
740 | Expr::Block(..)
741 | Expr::If(..)
742 | Expr::IfLet(..)
743 | Expr::Match(..)
744 | Expr::While(..)
745 | Expr::WhileLet(..)
746 | Expr::Loop(..)
747 | Expr::ForLoop(..)
748 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700749 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400750 }
751}
752
David Tolnayb9c8e322016-09-23 20:48:37 -0700753#[cfg(feature = "parsing")]
754pub mod parsing {
755 use super::*;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500756 use ty::parsing::qpath;
757 #[cfg(feature = "full")]
758 use ty::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700759
Michael Layzell734adb42017-06-07 16:58:31 -0400760 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500761 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500762 use synom::Synom;
763 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400764 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500765 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500766 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700767
David Tolnaybcf26022017-12-25 22:10:52 -0500768 // When we're parsing expressions which occur before blocks, like in an if
769 // statement's condition, we cannot parse a struct literal.
770 //
771 // Struct literals are ambiguous in certain positions
772 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700773 macro_rules! ambiguous_expr {
774 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700775 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700776 };
777 }
778
David Tolnaybcf26022017-12-25 22:10:52 -0500779 // When we are parsing an optional suffix expression, we cannot allow blocks
780 // if structs are not allowed.
781 //
782 // Example:
783 //
784 // if break {} {}
785 //
786 // is ambiguous between:
787 //
788 // if (break {}) {}
789 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400790 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400791 macro_rules! opt_ambiguous_expr {
792 ($i:expr, $allow_struct:ident) => {
793 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
794 };
795 }
796
Alex Crichton954046c2017-05-30 21:49:42 -0700797 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400798 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700799
800 fn description() -> Option<&'static str> {
801 Some("expression")
802 }
803 }
804
Michael Layzell734adb42017-06-07 16:58:31 -0400805 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700806 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
807
David Tolnaybcf26022017-12-25 22:10:52 -0500808 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400809 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500810 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500811 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400812 }
813
Michael Layzell734adb42017-06-07 16:58:31 -0400814 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500815 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500816 // NOTE: We intentionally skip assign_expr, placement_expr, and
817 // range_expr, as they are not parsed in non-full mode.
818 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400819 }
820
David Tolnaybcf26022017-12-25 22:10:52 -0500821 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400822 macro_rules! binop {
823 (
824 $name: ident,
825 $next: ident,
826 $submac: ident!( $($args:tt)* )
827 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500828 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400829 mut e: call!($next, allow_struct, allow_block) >>
830 many0!(do_parse!(
831 op: $submac!($($args)*) >>
832 rhs: call!($next, allow_struct, true) >>
833 ({
834 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500835 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400836 left: Box::new(e.into()),
837 op: op,
838 right: Box::new(rhs.into()),
839 }.into();
840 })
841 )) >>
842 (e)
843 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700844 }
David Tolnay54e854d2016-10-24 12:03:30 -0700845 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700846
David Tolnaybcf26022017-12-25 22:10:52 -0500847 // <placement> = <placement> ..
848 // <placement> += <placement> ..
849 // <placement> -= <placement> ..
850 // <placement> *= <placement> ..
851 // <placement> /= <placement> ..
852 // <placement> %= <placement> ..
853 // <placement> ^= <placement> ..
854 // <placement> &= <placement> ..
855 // <placement> |= <placement> ..
856 // <placement> <<= <placement> ..
857 // <placement> >>= <placement> ..
858 //
859 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400860 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500861 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400862 mut e: call!(placement_expr, allow_struct, allow_block) >>
863 alt!(
864 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800865 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400866 // Recurse into self to parse right-associative operator.
867 rhs: call!(assign_expr, allow_struct, true) >>
868 ({
869 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500870 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400871 left: Box::new(e.into()),
872 eq_token: eq,
873 right: Box::new(rhs.into()),
874 }.into();
875 })
876 )
877 |
878 do_parse!(
879 op: call!(BinOp::parse_assign_op) >>
880 // Recurse into self to parse right-associative operator.
881 rhs: call!(assign_expr, allow_struct, true) >>
882 ({
883 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500884 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400885 left: Box::new(e.into()),
886 op: op,
887 right: Box::new(rhs.into()),
888 }.into();
889 })
890 )
891 |
892 epsilon!()
893 ) >>
894 (e)
895 ));
896
David Tolnaybcf26022017-12-25 22:10:52 -0500897 // <range> <- <range> ..
898 //
899 // NOTE: The `in place { expr }` version of this syntax is parsed in
900 // `atom_expr`, not here.
901 //
902 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400903 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500904 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400905 mut e: call!(range_expr, allow_struct, allow_block) >>
906 alt!(
907 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800908 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400909 // Recurse into self to parse right-associative operator.
910 rhs: call!(placement_expr, allow_struct, true) >>
911 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400912 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500913 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400914 // op: BinOp::Place(larrow),
915 place: Box::new(e.into()),
David Tolnay8701a5c2017-12-28 23:31:10 -0500916 arrow_token: arrow,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400917 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400918 }.into();
919 })
920 )
921 |
922 epsilon!()
923 ) >>
924 (e)
925 ));
926
David Tolnaybcf26022017-12-25 22:10:52 -0500927 // <or> ... <or> ..
928 // <or> .. <or> ..
929 // <or> ..
930 //
931 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
932 // rules are for parsing these expressions are, but this is not correct.
933 // For example, `a .. b .. c` is not a legal expression. It should not
934 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
935 //
936 // NOTE: The form of ranges which don't include a preceding expression are
937 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400938 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500939 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400940 mut e: call!(or_expr, allow_struct, allow_block) >>
941 many0!(do_parse!(
942 limits: syn!(RangeLimits) >>
943 // We don't want to allow blocks here if we don't allow structs. See
944 // the reasoning for `opt_ambiguous_expr!` above.
945 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
946 ({
947 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500948 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400949 from: Some(Box::new(e.into())),
950 limits: limits,
951 to: hi.map(|e| Box::new(e.into())),
952 }.into();
953 })
954 )) >>
955 (e)
956 ));
957
David Tolnaybcf26022017-12-25 22:10:52 -0500958 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800959 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400960
David Tolnaybcf26022017-12-25 22:10:52 -0500961 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400963
David Tolnaybcf26022017-12-25 22:10:52 -0500964 // <bitor> == <bitor> ...
965 // <bitor> != <bitor> ...
966 // <bitor> >= <bitor> ...
967 // <bitor> <= <bitor> ...
968 // <bitor> > <bitor> ...
969 // <bitor> < <bitor> ...
970 //
971 // NOTE: This operator appears to be parsed as left-associative, but errors
972 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500973 binop!(
974 compare_expr,
975 bitor_expr,
976 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800977 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800979 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400980 |
981 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800982 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400983 |
984 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800985 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400986 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400987 do_parse!(
988 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800989 not!(punct!(<-)) >>
990 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400991 (BinOp::Lt(t))
992 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400993 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800994 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500995 )
996 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400997
David Tolnaybcf26022017-12-25 22:10:52 -0500998 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500999 binop!(
1000 bitor_expr,
1001 bitxor_expr,
1002 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1003 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004
David Tolnaybcf26022017-12-25 22:10:52 -05001005 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001006 binop!(
1007 bitxor_expr,
1008 bitand_expr,
1009 do_parse!(
1010 // NOTE: Make sure we aren't looking at ^=.
1011 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1012 )
1013 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001014
David Tolnaybcf26022017-12-25 22:10:52 -05001015 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001016 binop!(
1017 bitand_expr,
1018 shift_expr,
1019 do_parse!(
1020 // NOTE: Make sure we aren't looking at && or &=.
1021 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1022 )
1023 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001024
David Tolnaybcf26022017-12-25 22:10:52 -05001025 // <arith> << <arith> ...
1026 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001027 binop!(
1028 shift_expr,
1029 arith_expr,
1030 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001031 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001032 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001034 )
1035 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001036
David Tolnaybcf26022017-12-25 22:10:52 -05001037 // <term> + <term> ...
1038 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001039 binop!(
1040 arith_expr,
1041 term_expr,
1042 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001043 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001044 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001045 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001046 )
1047 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001048
David Tolnaybcf26022017-12-25 22:10:52 -05001049 // <cast> * <cast> ...
1050 // <cast> / <cast> ...
1051 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001052 binop!(
1053 term_expr,
1054 cast_expr,
1055 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001056 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001057 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001058 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001059 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001060 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001061 )
1062 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001063
David Tolnaybcf26022017-12-25 22:10:52 -05001064 // <unary> as <ty>
1065 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001066 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001067 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001068 mut e: call!(unary_expr, allow_struct, allow_block) >>
1069 many0!(alt!(
1070 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001071 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001072 // We can't accept `A + B` in cast expressions, as it's
1073 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001074 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001075 ({
1076 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001077 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001078 expr: Box::new(e.into()),
1079 as_token: as_,
1080 ty: Box::new(ty),
1081 }.into();
1082 })
1083 )
1084 |
1085 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001086 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001087 // We can't accept `A + B` in cast expressions, as it's
1088 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001089 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001090 ({
1091 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001092 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001093 expr: Box::new(e.into()),
1094 colon_token: colon,
1095 ty: Box::new(ty),
1096 }.into();
1097 })
1098 )
1099 )) >>
1100 (e)
1101 ));
1102
David Tolnay0cf94f22017-12-28 23:46:26 -05001103 // <unary> as <ty>
1104 #[cfg(not(feature = "full"))]
1105 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1106 mut e: call!(unary_expr, allow_struct, allow_block) >>
1107 many0!(do_parse!(
1108 as_: keyword!(as) >>
1109 // We can't accept `A + B` in cast expressions, as it's
1110 // ambiguous with the + expression.
1111 ty: call!(Type::without_plus) >>
1112 ({
1113 e = ExprCast {
1114 attrs: Vec::new(),
1115 expr: Box::new(e.into()),
1116 as_token: as_,
1117 ty: Box::new(ty),
1118 }.into();
1119 })
1120 )) >>
1121 (e)
1122 ));
1123
David Tolnaybcf26022017-12-25 22:10:52 -05001124 // <UnOp> <trailer>
1125 // & <trailer>
1126 // &mut <trailer>
1127 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001128 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001129 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001130 do_parse!(
1131 op: syn!(UnOp) >>
1132 expr: call!(unary_expr, allow_struct, true) >>
1133 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001134 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001135 op: op,
1136 expr: Box::new(expr.into()),
1137 }.into())
1138 )
1139 |
1140 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001141 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001142 mutability: syn!(Mutability) >>
1143 expr: call!(unary_expr, allow_struct, true) >>
1144 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001145 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001146 and_token: and,
1147 mutbl: mutability,
1148 expr: Box::new(expr.into()),
1149 }.into())
1150 )
1151 |
1152 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001153 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001154 expr: call!(unary_expr, allow_struct, true) >>
1155 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001156 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001157 box_token: box_,
1158 expr: Box::new(expr.into()),
1159 }.into())
1160 )
1161 |
1162 call!(trailer_expr, allow_struct, allow_block)
1163 ));
1164
Michael Layzell734adb42017-06-07 16:58:31 -04001165 // XXX: This duplication is ugly
1166 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001167 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001168 do_parse!(
1169 op: syn!(UnOp) >>
1170 expr: call!(unary_expr, allow_struct, true) >>
1171 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001172 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001173 op: op,
1174 expr: Box::new(expr.into()),
1175 }.into())
1176 )
1177 |
1178 call!(trailer_expr, allow_struct, allow_block)
1179 ));
1180
David Tolnaybcf26022017-12-25 22:10:52 -05001181 // <atom> (..<args>) ...
1182 // <atom> . <ident> (..<args>) ...
1183 // <atom> . <ident> ...
1184 // <atom> . <lit> ...
1185 // <atom> [ <expr> ] ...
1186 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001187 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001188 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001189 mut e: call!(atom_expr, allow_struct, allow_block) >>
1190 many0!(alt!(
1191 tap!(args: and_call => {
1192 let (args, paren) = args;
1193 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001194 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001195 func: Box::new(e.into()),
1196 args: args,
1197 paren_token: paren,
1198 }.into();
1199 })
1200 |
1201 tap!(more: and_method_call => {
1202 let mut call = more;
David Tolnay76418512017-12-28 23:47:47 -05001203 call.receiver = Box::new(e.into());
Michael Layzellb78f3b52017-06-04 19:03:03 -04001204 e = call.into();
1205 })
1206 |
1207 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001208 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001209 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001210 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001211 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001212 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001213 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001214 }.into();
1215 })
1216 |
1217 tap!(i: and_index => {
1218 let (i, token) = i;
1219 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001220 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001221 expr: Box::new(e.into()),
1222 bracket_token: token,
1223 index: Box::new(i),
1224 }.into();
1225 })
1226 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001227 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001228 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001229 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001230 expr: Box::new(e.into()),
1231 question_token: question,
1232 }.into();
1233 })
1234 )) >>
1235 (e)
1236 ));
1237
Michael Layzell734adb42017-06-07 16:58:31 -04001238 // XXX: Duplication == ugly
1239 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001240 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001241 mut e: call!(atom_expr, allow_struct, allow_block) >>
1242 many0!(alt!(
1243 tap!(args: and_call => {
1244 let (args, paren) = args;
1245 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001246 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001247 func: Box::new(e.into()),
1248 args: args,
1249 paren_token: paren,
1250 }.into();
1251 })
1252 |
1253 tap!(i: and_index => {
1254 let (i, token) = i;
1255 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001256 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001257 expr: Box::new(e.into()),
1258 bracket_token: token,
1259 index: Box::new(i),
1260 }.into();
1261 })
1262 )) >>
1263 (e)
1264 ));
1265
David Tolnaybcf26022017-12-25 22:10:52 -05001266 // Parse all atomic expressions which don't have to worry about precidence
1267 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001268 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001269 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1270 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001271 |
David Tolnay8c91b882017-12-28 23:04:32 -05001272 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001273 |
1274 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001275 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001276 |
David Tolnay8c91b882017-12-28 23:04:32 -05001277 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001278 |
David Tolnay8c91b882017-12-28 23:04:32 -05001279 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001280 |
1281 call!(expr_break, allow_struct) // must be before expr_path
1282 |
David Tolnay8c91b882017-12-28 23:04:32 -05001283 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001284 |
1285 call!(expr_ret, allow_struct) // must be before expr_path
1286 |
David Tolnay8c91b882017-12-28 23:04:32 -05001287 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001288 |
David Tolnay8c91b882017-12-28 23:04:32 -05001289 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001290 |
David Tolnay8c91b882017-12-28 23:04:32 -05001291 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001292 |
David Tolnay8c91b882017-12-28 23:04:32 -05001293 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001294 |
David Tolnay8c91b882017-12-28 23:04:32 -05001295 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001296 |
David Tolnay8c91b882017-12-28 23:04:32 -05001297 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001298 |
David Tolnay8c91b882017-12-28 23:04:32 -05001299 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001300 |
David Tolnay8c91b882017-12-28 23:04:32 -05001301 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001302 |
David Tolnay8c91b882017-12-28 23:04:32 -05001303 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001304 |
David Tolnay8c91b882017-12-28 23:04:32 -05001305 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001306 |
David Tolnay8c91b882017-12-28 23:04:32 -05001307 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001308 |
David Tolnay8c91b882017-12-28 23:04:32 -05001309 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001310 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001311 call!(expr_closure, allow_struct)
1312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001314 |
1315 // NOTE: This is the prefix-form of range
1316 call!(expr_range, allow_struct)
1317 |
David Tolnay8c91b882017-12-28 23:04:32 -05001318 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001319 |
David Tolnay8c91b882017-12-28 23:04:32 -05001320 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001321 ));
1322
Michael Layzell734adb42017-06-07 16:58:31 -04001323 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001324 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001325 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001326 |
David Tolnay8c91b882017-12-28 23:04:32 -05001327 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001328 ));
1329
Michael Layzell734adb42017-06-07 16:58:31 -04001330 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001331 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001332 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001333 |
David Tolnay8c91b882017-12-28 23:04:32 -05001334 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001335 |
David Tolnay8c91b882017-12-28 23:04:32 -05001336 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001337 |
David Tolnay8c91b882017-12-28 23:04:32 -05001338 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001339 |
David Tolnay8c91b882017-12-28 23:04:32 -05001340 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001341 |
David Tolnay8c91b882017-12-28 23:04:32 -05001342 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001343 |
David Tolnay8c91b882017-12-28 23:04:32 -05001344 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001345 |
David Tolnay8c91b882017-12-28 23:04:32 -05001346 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001347 |
David Tolnay8c91b882017-12-28 23:04:32 -05001348 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001349 |
David Tolnay8c91b882017-12-28 23:04:32 -05001350 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001351 |
David Tolnay8c91b882017-12-28 23:04:32 -05001352 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001353 ), Expr::from));
1354
David Tolnay8c91b882017-12-28 23:04:32 -05001355 impl Synom for ExprLit {
1356 named!(parse -> Self, do_parse!(
1357 lit: syn!(Lit) >>
1358 (ExprLit {
1359 attrs: Vec::new(),
1360 lit: lit,
1361 })
1362 ));
1363 }
1364
1365 #[cfg(feature = "full")]
1366 impl Synom for ExprMacro {
1367 named!(parse -> Self, do_parse!(
1368 mac: syn!(Macro) >>
1369 (ExprMacro {
1370 attrs: Vec::new(),
1371 mac: mac,
1372 })
1373 ));
1374 }
1375
David Tolnaye98775f2017-12-28 23:17:00 -05001376 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001377 impl Synom for ExprGroup {
1378 named!(parse -> Self, do_parse!(
1379 e: grouped!(syn!(Expr)) >>
1380 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001381 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001382 expr: Box::new(e.0),
1383 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001384 })
Michael Layzell93c36282017-06-04 20:43:14 -04001385 ));
1386 }
1387
David Tolnaye98775f2017-12-28 23:17:00 -05001388 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001389 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001390 named!(parse -> Self, do_parse!(
1391 e: parens!(syn!(Expr)) >>
1392 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001393 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001394 expr: Box::new(e.0),
1395 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001396 })
Michael Layzell92639a52017-06-01 00:07:44 -04001397 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001398 }
David Tolnay89e05672016-10-02 14:39:42 -07001399
Michael Layzell734adb42017-06-07 16:58:31 -04001400 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001401 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001402 named!(parse -> Self, do_parse!(
1403 elems: brackets!(call!(Delimited::parse_terminated)) >>
1404 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001405 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001406 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001407 bracket_token: elems.1,
1408 })
1409 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001410 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001411
David Tolnay32954ef2017-12-26 22:43:16 -05001412 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001413 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001414
Michael Layzell734adb42017-06-07 16:58:31 -04001415 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001416 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001417 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001418 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001419 turbofish: option!(tuple!(
1420 punct!(::),
1421 punct!(<),
1422 call!(Delimited::parse_terminated),
1423 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001424 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001425 args: parens!(call!(Delimited::parse_terminated)) >>
1426 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001427 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001428 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001429 // this expr will get overwritten after being returned
David Tolnay76418512017-12-28 23:47:47 -05001430 receiver: Box::new(Expr::Lit(ExprLit {
David Tolnay8c91b882017-12-28 23:04:32 -05001431 attrs: Vec::new(),
1432 lit: Lit {
1433 span: Span::default(),
1434 value: LitKind::Bool(false),
1435 },
Alex Crichton954046c2017-05-30 21:49:42 -07001436 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001437
Alex Crichton954046c2017-05-30 21:49:42 -07001438 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001439 turbofish: turbofish.map(|fish| MethodTurbofish {
1440 colon2_token: fish.0,
1441 lt_token: fish.1,
1442 args: fish.2,
1443 gt_token: fish.3,
1444 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001445 args: args.0,
1446 paren_token: args.1,
1447 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001448 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001449 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001450 ));
1451
Michael Layzell734adb42017-06-07 16:58:31 -04001452 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001453 impl Synom for GenericMethodArgument {
1454 // TODO parse const generics as well
1455 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1456 }
1457
1458 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001459 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001460 named!(parse -> Self, do_parse!(
1461 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001462 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001463 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001464 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001465 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001466 })
1467 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001468 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001469
Michael Layzell734adb42017-06-07 16:58:31 -04001470 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001471 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001472 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001473 if_: keyword!(if) >>
1474 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001475 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001476 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001477 cond: expr_no_struct >>
1478 then_block: braces!(call!(Block::parse_within)) >>
1479 else_block: option!(else_block) >>
1480 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001481 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001482 pat: Box::new(pat),
1483 let_token: let_,
1484 eq_token: eq,
1485 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001486 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001487 stmts: then_block.0,
1488 brace_token: then_block.1,
1489 },
1490 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001491 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001492 })
1493 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001494 }
1495
Michael Layzell734adb42017-06-07 16:58:31 -04001496 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001497 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001498 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001499 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001500 cond: expr_no_struct >>
1501 then_block: braces!(call!(Block::parse_within)) >>
1502 else_block: option!(else_block) >>
1503 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001504 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001505 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001506 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001507 stmts: then_block.0,
1508 brace_token: then_block.1,
1509 },
1510 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001511 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001512 })
1513 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001514 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001515
Michael Layzell734adb42017-06-07 16:58:31 -04001516 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001517 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001518 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001519 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001520 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001521 |
David Tolnay8c91b882017-12-28 23:04:32 -05001522 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001523 |
1524 do_parse!(
1525 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001526 (Expr::Block(ExprBlock {
1527 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001528 block: Block {
1529 stmts: else_block.0,
1530 brace_token: else_block.1,
1531 },
1532 }))
David Tolnay939766a2016-09-23 23:48:12 -07001533 )
Alex Crichton954046c2017-05-30 21:49:42 -07001534 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001535 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001536 ));
1537
Michael Layzell734adb42017-06-07 16:58:31 -04001538 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001539 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001540 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001541 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1542 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001543 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001544 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001545 expr: expr_no_struct >>
1546 loop_block: syn!(Block) >>
1547 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001548 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001549 for_token: for_,
1550 in_token: in_,
1551 pat: Box::new(pat),
1552 expr: Box::new(expr),
1553 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001554 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001555 label: lbl.map(|p| p.0),
1556 })
1557 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001558 }
Gregory Katze5f35682016-09-27 14:20:55 -04001559
Michael Layzell734adb42017-06-07 16:58:31 -04001560 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001561 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001562 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001563 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1564 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001565 loop_block: syn!(Block) >>
1566 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001567 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001568 loop_token: loop_,
1569 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001570 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001571 label: lbl.map(|p| p.0),
1572 })
1573 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001574 }
1575
Michael Layzell734adb42017-06-07 16:58:31 -04001576 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001577 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001578 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001579 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001580 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001581 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001582 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001583 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001584 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001585 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001586 expr: Box::new(obj),
1587 match_token: match_,
1588 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001589 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001590 }
1591 })
1592 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001593 }
David Tolnay1978c672016-10-27 22:05:52 -07001594
Michael Layzell734adb42017-06-07 16:58:31 -04001595 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001596 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001597 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001598 do_: keyword!(do) >>
1599 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001600 catch_block: syn!(Block) >>
1601 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001602 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001603 block: catch_block,
1604 do_token: do_,
1605 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001606 })
Michael Layzell92639a52017-06-01 00:07:44 -04001607 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001608 }
Arnavion02ef13f2017-04-25 00:54:31 -07001609
Michael Layzell734adb42017-06-07 16:58:31 -04001610 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001611 impl Synom for ExprYield {
1612 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001613 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001614 expr: option!(syn!(Expr)) >>
1615 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001616 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001617 yield_token: yield_,
1618 expr: expr.map(Box::new),
1619 })
1620 ));
1621 }
1622
1623 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001624 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001625 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001626 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001627 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001628 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1629 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001630 body: do_parse!(
1631 expr: alt!(expr_nosemi | syn!(Expr)) >>
1632 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1633 map!(input_end!(), |_| None)
1634 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001635 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001636 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001637 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001638 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001639 ) >>
1640 (Arm {
1641 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001642 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001643 attrs: attrs,
1644 pats: pats,
1645 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001646 body: Box::new(body.0),
1647 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001648 })
1649 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001650 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001651
Michael Layzell734adb42017-06-07 16:58:31 -04001652 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001653 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001654 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001655 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001656 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001657 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001658 ret_and_body: alt!(
1659 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001660 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001661 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001662 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001663 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001664 Expr::Block(ExprBlock {
1665 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001666 block: body,
1667 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001668 )
1669 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001670 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001671 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001672 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001673 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001674 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001675 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001676 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001677 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001678 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001679 body: Box::new(ret_and_body.1),
1680 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001681 ));
1682
Michael Layzell734adb42017-06-07 16:58:31 -04001683 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001684 named!(fn_arg -> FnArg, do_parse!(
1685 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001686 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001687 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001688 if let Some((colon, ty)) = ty {
1689 FnArg::Captured(ArgCaptured {
1690 pat: pat,
1691 colon_token: colon,
1692 ty: ty,
1693 })
1694 } else {
1695 FnArg::Inferred(pat)
1696 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001697 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001698 ));
1699
Michael Layzell734adb42017-06-07 16:58:31 -04001700 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001701 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001702 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001703 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1704 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001705 cond: expr_no_struct >>
1706 while_block: syn!(Block) >>
1707 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001708 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001709 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001710 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001711 cond: Box::new(cond),
1712 body: while_block,
1713 label: lbl.map(|p| p.0),
1714 })
1715 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001716 }
1717
Michael Layzell734adb42017-06-07 16:58:31 -04001718 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001719 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001720 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001721 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1722 while_: keyword!(while) >>
1723 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001724 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001725 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001726 value: expr_no_struct >>
1727 while_block: syn!(Block) >>
1728 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001729 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001730 eq_token: eq,
1731 let_token: let_,
1732 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001733 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001734 pat: Box::new(pat),
1735 expr: Box::new(value),
1736 body: while_block,
1737 label: lbl.map(|p| p.0),
1738 })
1739 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001740 }
1741
Michael Layzell734adb42017-06-07 16:58:31 -04001742 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001743 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001744 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001745 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001746 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001747 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001748 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001749 continue_token: cont,
1750 label: lbl,
1751 })
1752 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001753 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001754
Michael Layzell734adb42017-06-07 16:58:31 -04001755 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001756 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001757 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001758 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001759 // We can't allow blocks after a `break` expression when we wouldn't
1760 // allow structs, as this expression is ambiguous.
1761 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001762 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001763 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001764 label: lbl,
1765 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001766 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001767 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001768 ));
1769
Michael Layzell734adb42017-06-07 16:58:31 -04001770 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001771 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001772 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001773 // NOTE: return is greedy and eats blocks after it even when in a
1774 // position where structs are not allowed, such as in if statement
1775 // conditions. For example:
1776 //
David Tolnaybcf26022017-12-25 22:10:52 -05001777 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001778 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001779 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001780 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001781 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001782 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001783 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001784 ));
1785
Michael Layzell734adb42017-06-07 16:58:31 -04001786 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001787 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001788 named!(parse -> Self, do_parse!(
1789 path: syn!(Path) >>
1790 data: braces!(do_parse!(
1791 fields: call!(Delimited::parse_terminated) >>
1792 base: option!(
1793 cond!(fields.is_empty() || fields.trailing_delim(),
1794 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001795 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001796 base: syn!(Expr) >>
1797 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001798 )
Michael Layzell92639a52017-06-01 00:07:44 -04001799 )
1800 ) >>
1801 (fields, base)
1802 )) >>
1803 ({
1804 let ((fields, base), brace) = data;
1805 let (dots, rest) = match base.and_then(|b| b) {
1806 Some((dots, base)) => (Some(dots), Some(base)),
1807 None => (None, None),
1808 };
1809 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001810 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001811 brace_token: brace,
1812 path: path,
1813 fields: fields,
1814 dot2_token: dots,
1815 rest: rest.map(Box::new),
1816 }
1817 })
1818 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001819 }
1820
Michael Layzell734adb42017-06-07 16:58:31 -04001821 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001822 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001823 named!(parse -> Self, alt!(
1824 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001825 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001826 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001827 value: syn!(Expr) >>
1828 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001829 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001830 expr: value,
1831 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001832 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001833 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001834 })
Michael Layzell92639a52017-06-01 00:07:44 -04001835 )
1836 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001837 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001838 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001839 expr: Expr::Path(ExprPath {
1840 attrs: Vec::new(),
1841 qself: None,
1842 path: name.into(),
1843 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001844 is_shorthand: true,
1845 attrs: Vec::new(),
1846 colon_token: None,
1847 })
1848 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001849 }
David Tolnay055a7042016-10-02 19:23:54 -07001850
Michael Layzell734adb42017-06-07 16:58:31 -04001851 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001852 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001853 named!(parse -> Self, do_parse!(
1854 data: brackets!(do_parse!(
1855 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001856 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001857 times: syn!(Expr) >>
1858 (value, semi, times)
1859 )) >>
1860 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001861 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001862 expr: Box::new((data.0).0),
1863 amt: Box::new((data.0).2),
1864 bracket_token: data.1,
1865 semi_token: (data.0).1,
1866 })
1867 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001868 }
David Tolnay055a7042016-10-02 19:23:54 -07001869
Michael Layzell734adb42017-06-07 16:58:31 -04001870 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001871 impl Synom for ExprUnsafe {
1872 named!(parse -> Self, do_parse!(
1873 unsafe_: keyword!(unsafe) >>
1874 b: syn!(Block) >>
1875 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001876 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001877 unsafe_token: unsafe_,
1878 block: b,
1879 })
1880 ));
1881 }
1882
1883 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001884 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001885 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001886 b: syn!(Block) >>
1887 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001888 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001889 block: b,
1890 })
1891 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001892 }
David Tolnay89e05672016-10-02 14:39:42 -07001893
Michael Layzell734adb42017-06-07 16:58:31 -04001894 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001895 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001896 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001897 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001898 (ExprRange {
1899 attrs: Vec::new(),
1900 from: None,
1901 to: hi.map(Box::new),
1902 limits: limits,
1903 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001904 ));
1905
Michael Layzell734adb42017-06-07 16:58:31 -04001906 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001907 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001908 named!(parse -> Self, alt!(
1909 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001910 punct!(..=) => { RangeLimits::Closed }
1911 |
1912 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001913 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001914 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001915 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001916 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001917 }
David Tolnay438c9052016-10-07 23:24:48 -07001918
Alex Crichton954046c2017-05-30 21:49:42 -07001919 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001920 named!(parse -> Self, do_parse!(
1921 pair: qpath >>
1922 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001923 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001924 qself: pair.0,
1925 path: pair.1,
1926 })
1927 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001928 }
David Tolnay42602292016-10-01 22:25:45 -07001929
Michael Layzell734adb42017-06-07 16:58:31 -04001930 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001931 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001932
David Tolnay32954ef2017-12-26 22:43:16 -05001933 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001934
Michael Layzell734adb42017-06-07 16:58:31 -04001935 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001936 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001937 named!(parse -> Self, do_parse!(
1938 stmts: braces!(call!(Block::parse_within)) >>
1939 (Block {
1940 stmts: stmts.0,
1941 brace_token: stmts.1,
1942 })
1943 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001944 }
David Tolnay939766a2016-09-23 23:48:12 -07001945
Michael Layzell734adb42017-06-07 16:58:31 -04001946 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001947 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001948 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001949 many0!(punct!(;)) >>
1950 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001951 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001952 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001953 mut e: syn!(Expr) >>
1954 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001955 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001956 Stmt::Expr(Box::new(e))
1957 })
1958 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001959 (match last {
1960 None => standalone,
1961 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001962 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001963 standalone
1964 }
1965 })
1966 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001967 }
1968
Michael Layzell734adb42017-06-07 16:58:31 -04001969 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001970 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001971 named!(parse -> Self, alt!(
1972 stmt_mac
1973 |
1974 stmt_local
1975 |
1976 stmt_item
1977 |
Michael Layzell35418782017-06-07 09:20:25 -04001978 stmt_blockexpr
1979 |
Michael Layzell92639a52017-06-01 00:07:44 -04001980 stmt_expr
1981 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001982 }
David Tolnay939766a2016-09-23 23:48:12 -07001983
Michael Layzell734adb42017-06-07 16:58:31 -04001984 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001985 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001986 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001987 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001988 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001989 // Only parse braces here; paren and bracket will get parsed as
1990 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001991 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001992 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001993 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1994 attrs: attrs,
1995 ident: None,
1996 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001997 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001998 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001999 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05002000 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07002001 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05002002 },
David Tolnayeea28d62016-10-25 20:44:08 -07002003 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002004 semi_token: semi,
2005 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07002006 ));
2007
Michael Layzell734adb42017-06-07 16:58:31 -04002008 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002009 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002010 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002011 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002012 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002013 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002014 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2015 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07002016 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07002017 let_token: let_,
2018 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002019 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
2020 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07002021 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002022 ty: ty.map(|p| Box::new(p.1)),
2023 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002024 attrs: attrs,
2025 })))
2026 ));
2027
Michael Layzell734adb42017-06-07 16:58:31 -04002028 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002029 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002030
Michael Layzell734adb42017-06-07 16:58:31 -04002031 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002032 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002033 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002034 mut e: expr_nosemi >>
2035 // If the next token is a `.` or a `?` it is special-cased to parse as
2036 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002037 not!(punct!(.)) >>
2038 not!(punct!(?)) >>
2039 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002040 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002041 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002042 if let Some(semi) = semi {
2043 Stmt::Semi(Box::new(e), semi)
2044 } else {
2045 Stmt::Expr(Box::new(e))
2046 }
2047 })
2048 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002049
Michael Layzell734adb42017-06-07 16:58:31 -04002050 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002051 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002052 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002053 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002054 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002055 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002056 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002057 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002058 })
David Tolnay939766a2016-09-23 23:48:12 -07002059 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002060
Michael Layzell734adb42017-06-07 16:58:31 -04002061 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002062 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002063 named!(parse -> Self, alt!(
2064 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2065 |
2066 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2067 |
2068 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2069 |
2070 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2071 |
2072 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2073 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002074 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002075 |
2076 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2077 |
2078 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2079 |
2080 syn!(PatPath) => { Pat::Path }
2081 |
2082 syn!(PatTuple) => { Pat::Tuple }
2083 |
2084 syn!(PatRef) => { Pat::Ref }
2085 |
2086 syn!(PatSlice) => { Pat::Slice }
2087 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002088 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002089
Michael Layzell734adb42017-06-07 16:58:31 -04002090 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002091 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002092 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002093 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002094 |u| PatWild { underscore_token: u }
2095 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002096 }
David Tolnay84aa0752016-10-02 23:01:13 -07002097
Michael Layzell734adb42017-06-07 16:58:31 -04002098 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002099 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002100 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002101 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002102 pat: syn!(Pat) >>
2103 (PatBox {
2104 pat: Box::new(pat),
2105 box_token: boxed,
2106 })
2107 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002108 }
2109
Michael Layzell734adb42017-06-07 16:58:31 -04002110 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002111 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002112 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002113 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002114 mutability: syn!(Mutability) >>
2115 name: alt!(
2116 syn!(Ident)
2117 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002118 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002119 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002120 not!(punct!(<)) >>
2121 not!(punct!(::)) >>
2122 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002123 (PatIdent {
2124 mode: match mode {
2125 Some(mode) => BindingMode::ByRef(mode, mutability),
2126 None => BindingMode::ByValue(mutability),
2127 },
2128 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002129 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002130 subpat: subpat.map(|p| Box::new(p.1)),
2131 })
2132 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002133 }
2134
Michael Layzell734adb42017-06-07 16:58:31 -04002135 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002136 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002137 named!(parse -> Self, do_parse!(
2138 path: syn!(Path) >>
2139 tuple: syn!(PatTuple) >>
2140 (PatTupleStruct {
2141 path: path,
2142 pat: tuple,
2143 })
2144 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002145 }
2146
Michael Layzell734adb42017-06-07 16:58:31 -04002147 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002148 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002149 named!(parse -> Self, do_parse!(
2150 path: syn!(Path) >>
2151 data: braces!(do_parse!(
2152 fields: call!(Delimited::parse_terminated) >>
2153 base: option!(
2154 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002155 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002156 ) >>
2157 (fields, base)
2158 )) >>
2159 (PatStruct {
2160 path: path,
2161 fields: (data.0).0,
2162 brace_token: data.1,
2163 dot2_token: (data.0).1.and_then(|m| m),
2164 })
2165 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002166 }
2167
Michael Layzell734adb42017-06-07 16:58:31 -04002168 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002169 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002170 named!(parse -> Self, alt!(
2171 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002172 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002173 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002174 pat: syn!(Pat) >>
2175 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002176 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002177 pat: Box::new(pat),
2178 is_shorthand: false,
2179 attrs: Vec::new(),
2180 colon_token: Some(colon),
2181 })
2182 )
2183 |
2184 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002185 boxed: option!(keyword!(box)) >>
2186 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002187 mutability: syn!(Mutability) >>
2188 ident: syn!(Ident) >>
2189 ({
2190 let mut pat: Pat = PatIdent {
2191 mode: if let Some(mode) = mode {
2192 BindingMode::ByRef(mode, mutability)
2193 } else {
2194 BindingMode::ByValue(mutability)
2195 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002196 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002197 subpat: None,
2198 at_token: None,
2199 }.into();
2200 if let Some(boxed) = boxed {
2201 pat = PatBox {
2202 pat: Box::new(pat),
2203 box_token: boxed,
2204 }.into();
2205 }
2206 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002207 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002208 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002209 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002210 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002211 colon_token: None,
2212 }
2213 })
2214 )
2215 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002216 }
2217
Michael Layzell734adb42017-06-07 16:58:31 -04002218 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002219 impl Synom for Member {
2220 named!(parse -> Self, alt!(
2221 syn!(Ident) => { Member::Named }
2222 |
2223 syn!(Index) => { Member::Unnamed }
2224 ));
2225 }
2226
2227 #[cfg(feature = "full")]
2228 impl Synom for Index {
2229 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002230 lit: syn!(Lit) >>
2231 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002232 if let Ok(i) = lit.value.to_string().parse() {
2233 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002234 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002235 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002236 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002237 })
David Tolnay85b69a42017-12-27 20:43:10 -05002238 ));
2239 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002240
Michael Layzell734adb42017-06-07 16:58:31 -04002241 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002242 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002243 named!(parse -> Self, map!(
2244 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002245 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002246 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002247 }
David Tolnay9636c052016-10-02 17:11:17 -07002248
Michael Layzell734adb42017-06-07 16:58:31 -04002249 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002250 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002251 named!(parse -> Self, do_parse!(
2252 data: parens!(do_parse!(
2253 elems: call!(Delimited::parse_terminated) >>
2254 dotdot: map!(cond!(
2255 elems.is_empty() || elems.trailing_delim(),
2256 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002257 dots: punct!(..) >>
2258 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002259 (dots, trailing)
2260 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002261 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002262 rest: cond!(match dotdot {
2263 Some((_, Some(_))) => true,
2264 _ => false,
2265 },
2266 call!(Delimited::parse_terminated)) >>
2267 (elems, dotdot, rest)
2268 )) >>
2269 ({
2270 let ((mut elems, dotdot, rest), parens) = data;
2271 let (dotdot, trailing) = match dotdot {
2272 Some((a, b)) => (Some(a), Some(b)),
2273 None => (None, None),
2274 };
2275 PatTuple {
2276 paren_token: parens,
2277 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2278 dot2_token: dotdot,
2279 comma_token: trailing.and_then(|b| b),
2280 pats: {
2281 if let Some(rest) = rest {
2282 for elem in rest {
2283 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002284 }
Michael Layzell92639a52017-06-01 00:07:44 -04002285 }
2286 elems
2287 },
2288 }
2289 })
2290 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002291 }
David Tolnayfbb73232016-10-03 01:00:06 -07002292
Michael Layzell734adb42017-06-07 16:58:31 -04002293 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002294 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002295 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002296 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002297 mutability: syn!(Mutability) >>
2298 pat: syn!(Pat) >>
2299 (PatRef {
2300 pat: Box::new(pat),
2301 mutbl: mutability,
2302 and_token: and,
2303 })
2304 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002305 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002306
Michael Layzell734adb42017-06-07 16:58:31 -04002307 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002308 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002309 named!(parse -> Self, do_parse!(
2310 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002311 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002312 return parse_error(); // these need to be parsed by pat_path
2313 } else {
2314 PatLit {
2315 expr: Box::new(lit),
2316 }
2317 })
2318 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002319 }
David Tolnaye1310902016-10-29 23:40:00 -07002320
Michael Layzell734adb42017-06-07 16:58:31 -04002321 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002322 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002323 named!(parse -> Self, do_parse!(
2324 lo: pat_lit_expr >>
2325 limits: syn!(RangeLimits) >>
2326 hi: pat_lit_expr >>
2327 (PatRange {
2328 lo: Box::new(lo),
2329 hi: Box::new(hi),
2330 limits: limits,
2331 })
2332 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002333 }
David Tolnaye1310902016-10-29 23:40:00 -07002334
Michael Layzell734adb42017-06-07 16:58:31 -04002335 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002336 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002337 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002338 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002339 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002340 |
David Tolnay8c91b882017-12-28 23:04:32 -05002341 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002342 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002343 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002344 Expr::Unary(ExprUnary {
2345 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002346 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002347 expr: Box::new(v.into())
2348 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002349 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002350 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002351 })
2352 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002353
Michael Layzell734adb42017-06-07 16:58:31 -04002354 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002355 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002356 named!(parse -> Self, map!(
2357 brackets!(do_parse!(
2358 before: call!(Delimited::parse_terminated) >>
2359 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002360 dots: punct!(..) >>
2361 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002362 (dots, trailing)
2363 )) >>
2364 after: cond!(
2365 match middle {
2366 Some((_, ref trailing)) => trailing.is_some(),
2367 _ => false,
2368 },
2369 call!(Delimited::parse_terminated)
2370 ) >>
2371 (before, middle, after)
2372 )),
2373 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002374 let mut before: Delimited<Pat, Token![,]> = before;
2375 let after: Option<Delimited<Pat, Token![,]>> = after;
2376 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002377 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002378 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002379 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002380 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002381 }),
2382 bracket_token: brackets,
2383 middle: middle.and_then(|_| {
2384 if !before.is_empty() && !before.trailing_delim() {
2385 Some(Box::new(before.pop().unwrap().into_item()))
2386 } else {
2387 None
2388 }
2389 }),
2390 front: before,
2391 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002392 }
Alex Crichton954046c2017-05-30 21:49:42 -07002393 }
Michael Layzell92639a52017-06-01 00:07:44 -04002394 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002395 }
David Tolnay435a9a82016-10-29 13:47:20 -07002396
Michael Layzell734adb42017-06-07 16:58:31 -04002397 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002398 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002399 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002400 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002401 |
2402 epsilon!() => { |_| CaptureBy::Ref }
2403 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002404 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002405}
2406
David Tolnayf4bbbd92016-09-23 14:41:55 -07002407#[cfg(feature = "printing")]
2408mod printing {
2409 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002410 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002411 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002412 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002413 #[cfg(feature = "full")]
2414 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002415
David Tolnaybcf26022017-12-25 22:10:52 -05002416 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2417 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002418 #[cfg(feature = "full")]
2419 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002420 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002421 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002422 e.to_tokens(tokens);
2423 });
2424 } else {
2425 e.to_tokens(tokens);
2426 }
2427 }
2428
David Tolnay8c91b882017-12-28 23:04:32 -05002429 #[cfg(feature = "full")]
2430 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2431 tokens.append_all(attrs.outer());
2432 }
Michael Layzell734adb42017-06-07 16:58:31 -04002433
David Tolnay8c91b882017-12-28 23:04:32 -05002434 #[cfg(not(feature = "full"))]
2435 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002436 }
2437
Michael Layzell734adb42017-06-07 16:58:31 -04002438 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002439 impl ToTokens for ExprBox {
2440 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002441 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002442 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002443 self.expr.to_tokens(tokens);
2444 }
2445 }
2446
Michael Layzell734adb42017-06-07 16:58:31 -04002447 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002448 impl ToTokens for ExprInPlace {
2449 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002450 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002451 self.place.to_tokens(tokens);
2452 self.arrow_token.to_tokens(tokens);
2453 self.value.to_tokens(tokens);
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| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002462 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002463 })
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());
David Tolnay76418512017-12-28 23:47:47 -05002481 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002482 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002483 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002484 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002485 self.paren_token.surround(tokens, |tokens| {
2486 self.args.to_tokens(tokens);
2487 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002488 }
2489 }
2490
Michael Layzell734adb42017-06-07 16:58:31 -04002491 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002492 impl ToTokens for MethodTurbofish {
2493 fn to_tokens(&self, tokens: &mut Tokens) {
2494 self.colon2_token.to_tokens(tokens);
2495 self.lt_token.to_tokens(tokens);
2496 self.args.to_tokens(tokens);
2497 self.gt_token.to_tokens(tokens);
2498 }
2499 }
2500
2501 #[cfg(feature = "full")]
2502 impl ToTokens for GenericMethodArgument {
2503 fn to_tokens(&self, tokens: &mut Tokens) {
2504 match *self {
2505 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2506 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2507 }
2508 }
2509 }
2510
2511 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002512 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002513 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002514 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002515 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002516 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002517 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002518 // distinguish ExprTuple from ExprParen.
David Tolnay2a86fdd2017-12-28 23:34:28 -05002519 if self.elems.len() == 1 && !self.elems.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002520 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002521 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002522 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002523 }
2524 }
2525
2526 impl ToTokens for ExprBinary {
2527 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002528 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002529 self.left.to_tokens(tokens);
2530 self.op.to_tokens(tokens);
2531 self.right.to_tokens(tokens);
2532 }
2533 }
2534
2535 impl ToTokens for ExprUnary {
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.op.to_tokens(tokens);
2539 self.expr.to_tokens(tokens);
2540 }
2541 }
2542
David Tolnay8c91b882017-12-28 23:04:32 -05002543 impl ToTokens for ExprLit {
2544 fn to_tokens(&self, tokens: &mut Tokens) {
2545 attrs_to_tokens(&self.attrs, tokens);
2546 self.lit.to_tokens(tokens);
2547 }
2548 }
2549
Alex Crichton62a0a592017-05-22 13:58:53 -07002550 impl ToTokens for ExprCast {
2551 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002552 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002553 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002554 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002555 self.ty.to_tokens(tokens);
2556 }
2557 }
2558
David Tolnay0cf94f22017-12-28 23:46:26 -05002559 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002560 impl ToTokens for ExprType {
2561 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002562 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002563 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002564 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002565 self.ty.to_tokens(tokens);
2566 }
2567 }
2568
Michael Layzell734adb42017-06-07 16:58:31 -04002569 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002570 fn maybe_wrap_else(
2571 tokens: &mut Tokens,
David Tolnay2ccf32a2017-12-29 00:34:26 -05002572 else_: &Option<(Token![else], Box<Expr>)>,
David Tolnay51382052017-12-27 13:46:21 -05002573 ) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002574 if let Some((ref else_token, ref else_)) = *else_ {
2575 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002576
2577 // If we are not one of the valid expressions to exist in an else
2578 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002579 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002580 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002581 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002582 }
2583 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002584 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002585 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002586 });
2587 }
2588 }
2589 }
2590 }
2591
2592 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 impl ToTokens for ExprIf {
2594 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002595 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002596 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002597 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002598 self.then_branch.to_tokens(tokens);
2599 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002600 }
2601 }
2602
Michael Layzell734adb42017-06-07 16:58:31 -04002603 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002604 impl ToTokens for ExprIfLet {
2605 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002606 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002607 self.if_token.to_tokens(tokens);
2608 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002609 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002610 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002611 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002612 self.then_branch.to_tokens(tokens);
2613 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002614 }
2615 }
2616
Michael Layzell734adb42017-06-07 16:58:31 -04002617 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002618 impl ToTokens for ExprWhile {
2619 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002620 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002621 if self.label.is_some() {
2622 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002623 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002624 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002625 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002626 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002627 self.body.to_tokens(tokens);
2628 }
2629 }
2630
Michael Layzell734adb42017-06-07 16:58:31 -04002631 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002632 impl ToTokens for ExprWhileLet {
2633 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002634 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002635 if self.label.is_some() {
2636 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002637 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002638 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002639 self.while_token.to_tokens(tokens);
2640 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002641 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002642 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002643 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002644 self.body.to_tokens(tokens);
2645 }
2646 }
2647
Michael Layzell734adb42017-06-07 16:58:31 -04002648 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002649 impl ToTokens for ExprForLoop {
2650 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002651 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002652 if self.label.is_some() {
2653 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002654 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002655 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002656 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002657 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002658 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002659 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002660 self.body.to_tokens(tokens);
2661 }
2662 }
2663
Michael Layzell734adb42017-06-07 16:58:31 -04002664 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002665 impl ToTokens for ExprLoop {
2666 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002667 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002668 if self.label.is_some() {
2669 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002670 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002671 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002672 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002673 self.body.to_tokens(tokens);
2674 }
2675 }
2676
Michael Layzell734adb42017-06-07 16:58:31 -04002677 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002678 impl ToTokens for ExprMatch {
2679 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002680 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002681 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002682 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002683 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002684 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002685 arm.to_tokens(tokens);
2686 // Ensure that we have a comma after a non-block arm, except
2687 // for the last one.
2688 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002689 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002690 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002691 }
2692 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002693 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002694 }
2695 }
2696
Michael Layzell734adb42017-06-07 16:58:31 -04002697 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002698 impl ToTokens for ExprCatch {
2699 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002700 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002701 self.do_token.to_tokens(tokens);
2702 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002703 self.block.to_tokens(tokens);
2704 }
2705 }
2706
Michael Layzell734adb42017-06-07 16:58:31 -04002707 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002708 impl ToTokens for ExprYield {
2709 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002710 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002711 self.yield_token.to_tokens(tokens);
2712 self.expr.to_tokens(tokens);
2713 }
2714 }
2715
2716 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002717 impl ToTokens for ExprClosure {
2718 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002719 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002720 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002721 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002722 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002723 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002724 FnArg::Captured(ArgCaptured {
2725 ref pat,
2726 ty: Type::Infer(_),
2727 ..
2728 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002729 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002730 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002731 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002732 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002733 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002734 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002735 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002736 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002737 self.body.to_tokens(tokens);
2738 }
2739 }
2740
Michael Layzell734adb42017-06-07 16:58:31 -04002741 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002742 impl ToTokens for ExprUnsafe {
2743 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002744 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002745 self.unsafe_token.to_tokens(tokens);
2746 self.block.to_tokens(tokens);
2747 }
2748 }
2749
2750 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002751 impl ToTokens for ExprBlock {
2752 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002753 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002754 self.block.to_tokens(tokens);
2755 }
2756 }
2757
Michael Layzell734adb42017-06-07 16:58:31 -04002758 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002759 impl ToTokens for ExprAssign {
2760 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002761 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002762 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002763 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002764 self.right.to_tokens(tokens);
2765 }
2766 }
2767
Michael Layzell734adb42017-06-07 16:58:31 -04002768 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 impl ToTokens for ExprAssignOp {
2770 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002771 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002772 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002773 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002774 self.right.to_tokens(tokens);
2775 }
2776 }
2777
Michael Layzell734adb42017-06-07 16:58:31 -04002778 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002779 impl ToTokens for ExprField {
2780 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002781 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002782 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002783 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002784 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002785 }
2786 }
2787
Michael Layzell734adb42017-06-07 16:58:31 -04002788 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002789 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002790 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002791 match *self {
2792 Member::Named(ident) => ident.to_tokens(tokens),
2793 Member::Unnamed(ref index) => index.to_tokens(tokens),
2794 }
2795 }
2796 }
2797
2798 #[cfg(feature = "full")]
2799 impl ToTokens for Index {
2800 fn to_tokens(&self, tokens: &mut Tokens) {
2801 tokens.append(TokenTree {
2802 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002803 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002804 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002805 }
2806 }
2807
2808 impl ToTokens for ExprIndex {
2809 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002810 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002811 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002812 self.bracket_token.surround(tokens, |tokens| {
2813 self.index.to_tokens(tokens);
2814 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002815 }
2816 }
2817
Michael Layzell734adb42017-06-07 16:58:31 -04002818 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002819 impl ToTokens for ExprRange {
2820 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002821 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002822 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002823 match self.limits {
2824 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2825 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2826 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002827 self.to.to_tokens(tokens);
2828 }
2829 }
2830
2831 impl ToTokens for ExprPath {
2832 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002833 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002834 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002835 }
2836 }
2837
Michael Layzell734adb42017-06-07 16:58:31 -04002838 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002839 impl ToTokens for ExprAddrOf {
2840 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002841 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002842 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002843 self.mutbl.to_tokens(tokens);
2844 self.expr.to_tokens(tokens);
2845 }
2846 }
2847
Michael Layzell734adb42017-06-07 16:58:31 -04002848 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002849 impl ToTokens for ExprBreak {
2850 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002851 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002852 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002853 self.label.to_tokens(tokens);
2854 self.expr.to_tokens(tokens);
2855 }
2856 }
2857
Michael Layzell734adb42017-06-07 16:58:31 -04002858 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002859 impl ToTokens for ExprContinue {
2860 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002861 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002862 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002863 self.label.to_tokens(tokens);
2864 }
2865 }
2866
Michael Layzell734adb42017-06-07 16:58:31 -04002867 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002868 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002869 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002870 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002871 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002872 self.expr.to_tokens(tokens);
2873 }
2874 }
2875
Michael Layzell734adb42017-06-07 16:58:31 -04002876 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002877 impl ToTokens for ExprMacro {
2878 fn to_tokens(&self, tokens: &mut Tokens) {
2879 tokens.append_all(self.attrs.outer());
2880 self.mac.to_tokens(tokens);
2881 }
2882 }
2883
2884 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002885 impl ToTokens for ExprStruct {
2886 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002887 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002888 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002889 self.brace_token.surround(tokens, |tokens| {
2890 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002891 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002892 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002893 self.rest.to_tokens(tokens);
2894 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002895 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002896 }
2897 }
2898
Michael Layzell734adb42017-06-07 16:58:31 -04002899 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002900 impl ToTokens for ExprRepeat {
2901 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002902 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002903 self.bracket_token.surround(tokens, |tokens| {
2904 self.expr.to_tokens(tokens);
2905 self.semi_token.to_tokens(tokens);
2906 self.amt.to_tokens(tokens);
2907 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002908 }
2909 }
2910
David Tolnaye98775f2017-12-28 23:17:00 -05002911 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002912 impl ToTokens for ExprGroup {
2913 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002914 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002915 self.group_token.surround(tokens, |tokens| {
2916 self.expr.to_tokens(tokens);
2917 });
2918 }
2919 }
2920
David Tolnaye98775f2017-12-28 23:17:00 -05002921 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002922 impl ToTokens for ExprParen {
2923 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002924 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002925 self.paren_token.surround(tokens, |tokens| {
2926 self.expr.to_tokens(tokens);
2927 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002928 }
2929 }
2930
Michael Layzell734adb42017-06-07 16:58:31 -04002931 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002932 impl ToTokens for ExprTry {
2933 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002934 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002935 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002936 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002937 }
2938 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002939
Michael Layzell734adb42017-06-07 16:58:31 -04002940 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002941 impl ToTokens for FieldValue {
2942 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002943 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002944 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2945 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002946 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002947 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002948 self.expr.to_tokens(tokens);
2949 }
David Tolnay055a7042016-10-02 19:23:54 -07002950 }
2951 }
2952
Michael Layzell734adb42017-06-07 16:58:31 -04002953 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002954 impl ToTokens for Arm {
2955 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002956 tokens.append_all(&self.attrs);
2957 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002958 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002959 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002960 self.guard.to_tokens(tokens);
2961 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002962 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002963 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002964 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002965 }
2966 }
2967
Michael Layzell734adb42017-06-07 16:58:31 -04002968 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002969 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002970 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002971 self.underscore_token.to_tokens(tokens);
2972 }
2973 }
2974
Michael Layzell734adb42017-06-07 16:58:31 -04002975 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002976 impl ToTokens for PatIdent {
2977 fn to_tokens(&self, tokens: &mut Tokens) {
2978 self.mode.to_tokens(tokens);
2979 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002980 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002981 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002982 self.subpat.to_tokens(tokens);
2983 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002984 }
2985 }
2986
Michael Layzell734adb42017-06-07 16:58:31 -04002987 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002988 impl ToTokens for PatStruct {
2989 fn to_tokens(&self, tokens: &mut Tokens) {
2990 self.path.to_tokens(tokens);
2991 self.brace_token.surround(tokens, |tokens| {
2992 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002993 // NOTE: We need a comma before the dot2 token if it is present.
2994 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002995 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002996 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002997 self.dot2_token.to_tokens(tokens);
2998 });
2999 }
3000 }
3001
Michael Layzell734adb42017-06-07 16:58:31 -04003002 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003003 impl ToTokens for PatTupleStruct {
3004 fn to_tokens(&self, tokens: &mut Tokens) {
3005 self.path.to_tokens(tokens);
3006 self.pat.to_tokens(tokens);
3007 }
3008 }
3009
Michael Layzell734adb42017-06-07 16:58:31 -04003010 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011 impl ToTokens for PatPath {
3012 fn to_tokens(&self, tokens: &mut Tokens) {
3013 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3014 }
3015 }
3016
Michael Layzell734adb42017-06-07 16:58:31 -04003017 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003018 impl ToTokens for PatTuple {
3019 fn to_tokens(&self, tokens: &mut Tokens) {
3020 self.paren_token.surround(tokens, |tokens| {
3021 for (i, token) in self.pats.iter().enumerate() {
3022 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07003023 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
3024 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003025 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003026 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003027 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003028
3029 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003030 // Ensure there is a comma before the .. token.
3031 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003032 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003033 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003035 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003036 });
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 PatBox {
3042 fn to_tokens(&self, tokens: &mut Tokens) {
3043 self.box_token.to_tokens(tokens);
3044 self.pat.to_tokens(tokens);
3045 }
3046 }
3047
Michael Layzell734adb42017-06-07 16:58:31 -04003048 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003049 impl ToTokens for PatRef {
3050 fn to_tokens(&self, tokens: &mut Tokens) {
3051 self.and_token.to_tokens(tokens);
3052 self.mutbl.to_tokens(tokens);
3053 self.pat.to_tokens(tokens);
3054 }
3055 }
3056
Michael Layzell734adb42017-06-07 16:58:31 -04003057 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003058 impl ToTokens for PatLit {
3059 fn to_tokens(&self, tokens: &mut Tokens) {
3060 self.expr.to_tokens(tokens);
3061 }
3062 }
3063
Michael Layzell734adb42017-06-07 16:58:31 -04003064 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003065 impl ToTokens for PatRange {
3066 fn to_tokens(&self, tokens: &mut Tokens) {
3067 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003068 match self.limits {
3069 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3070 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3071 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003072 self.hi.to_tokens(tokens);
3073 }
3074 }
3075
Michael Layzell734adb42017-06-07 16:58:31 -04003076 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003077 impl ToTokens for PatSlice {
3078 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003079 // XXX: This is a mess, and it will be so easy to screw it up. How
3080 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003081 self.bracket_token.surround(tokens, |tokens| {
3082 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003083
3084 // If we need a comma before the middle or standalone .. token,
3085 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003086 if !self.front.empty_or_trailing()
3087 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003088 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003089 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003090 }
3091
3092 // If we have an identifier, we always need a .. token.
3093 if self.middle.is_some() {
3094 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003095 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003096 } else if self.dot2_token.is_some() {
3097 self.dot2_token.to_tokens(tokens);
3098 }
3099
3100 // Make sure we have a comma before the back half.
3101 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003102 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003103 self.back.to_tokens(tokens);
3104 } else {
3105 self.comma_token.to_tokens(tokens);
3106 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003107 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003108 }
3109 }
3110
Michael Layzell734adb42017-06-07 16:58:31 -04003111 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003112 impl ToTokens for FieldPat {
3113 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003114 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07003115 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05003116 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003117 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003118 }
3119 self.pat.to_tokens(tokens);
3120 }
3121 }
3122
Michael Layzell734adb42017-06-07 16:58:31 -04003123 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003124 impl ToTokens for BindingMode {
3125 fn to_tokens(&self, tokens: &mut Tokens) {
3126 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003127 BindingMode::ByRef(ref t, ref m) => {
3128 t.to_tokens(tokens);
3129 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003130 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003131 BindingMode::ByValue(ref m) => {
3132 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003133 }
3134 }
3135 }
3136 }
David Tolnay42602292016-10-01 22:25:45 -07003137
Michael Layzell734adb42017-06-07 16:58:31 -04003138 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07003139 impl ToTokens for CaptureBy {
3140 fn to_tokens(&self, tokens: &mut Tokens) {
3141 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003142 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003143 CaptureBy::Ref => {
3144 // nothing
3145 }
David Tolnay89e05672016-10-02 14:39:42 -07003146 }
3147 }
3148 }
3149
Michael Layzell734adb42017-06-07 16:58:31 -04003150 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003151 impl ToTokens for Block {
3152 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003153 self.brace_token.surround(tokens, |tokens| {
3154 tokens.append_all(&self.stmts);
3155 });
David Tolnay42602292016-10-01 22:25:45 -07003156 }
3157 }
3158
Michael Layzell734adb42017-06-07 16:58:31 -04003159 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003160 impl ToTokens for Stmt {
3161 fn to_tokens(&self, tokens: &mut Tokens) {
3162 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003163 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003164 Stmt::Item(ref item) => item.to_tokens(tokens),
3165 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003166 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003167 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003168 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003169 }
David Tolnay42602292016-10-01 22:25:45 -07003170 }
3171 }
3172 }
David Tolnay191e0582016-10-02 18:31:09 -07003173
Michael Layzell734adb42017-06-07 16:58:31 -04003174 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003175 impl ToTokens for Local {
3176 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003177 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003178 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003179 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003180 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003181 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003182 self.ty.to_tokens(tokens);
3183 }
3184 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003185 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003186 self.init.to_tokens(tokens);
3187 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003188 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003189 }
3190 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003191}