blob: 7c462242cae085535793a48fb0c6978d180792e8 [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>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500194 pub capture: Option<Token![move]>,
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![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500273 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 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
David Tolnay5d7098a2017-12-29 01:35:24 -0500505 /// The colon in `Struct { x: x }`. If written in shorthand like
506 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500507 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500508
Alex Crichton62a0a592017-05-22 13:58:53 -0700509 /// Value of the field.
510 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 }
David Tolnay055a7042016-10-02 19:23:54 -0700512}
513
Michael Layzell734adb42017-06-07 16:58:31 -0400514#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700515ast_struct! {
516 /// A Block (`{ .. }`).
517 ///
518 /// E.g. `{ .. }` as in `fn foo() { .. }`
519 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500520 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700521 /// Statements in a block
522 pub stmts: Vec<Stmt>,
523 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700524}
525
Michael Layzell734adb42017-06-07 16:58:31 -0400526#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700527ast_enum! {
528 /// A statement, usually ending in a semicolon.
529 pub enum Stmt {
530 /// A local (let) binding.
531 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700532
Alex Crichton62a0a592017-05-22 13:58:53 -0700533 /// An item definition.
534 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700535
Alex Crichton62a0a592017-05-22 13:58:53 -0700536 /// Expr without trailing semicolon.
537 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700538
Alex Crichton62a0a592017-05-22 13:58:53 -0700539 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800540 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700542}
543
Michael Layzell734adb42017-06-07 16:58:31 -0400544#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700545ast_struct! {
546 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
547 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500548 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800549 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700550 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500551 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800552 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500553 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700554 /// Initializer expression to set the value, if any
555 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500556 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700558}
559
Michael Layzell734adb42017-06-07 16:58:31 -0400560#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700561ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700562 // Clippy false positive
563 // https://github.com/Manishearth/rust-clippy/issues/1241
564 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
565 pub enum Pat {
566 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700567 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800568 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700569 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700570
Alex Crichton62a0a592017-05-22 13:58:53 -0700571 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
572 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
573 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
574 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700575 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500576 pub by_ref: Option<Token![ref]>,
577 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700578 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800579 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500580 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700581 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700582
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
584 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700585 pub Struct(PatStruct {
586 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500587 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500588 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800589 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700590 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700591
Alex Crichton62a0a592017-05-22 13:58:53 -0700592 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
593 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
594 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595 pub TupleStruct(PatTupleStruct {
596 pub path: Path,
597 pub pat: PatTuple,
598 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700599
Alex Crichton62a0a592017-05-22 13:58:53 -0700600 /// A possibly qualified path pattern.
601 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
602 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
603 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700604 pub Path(PatPath {
605 pub qself: Option<QSelf>,
606 pub path: Path,
607 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700608
Alex Crichton62a0a592017-05-22 13:58:53 -0700609 /// A tuple pattern `(a, b)`.
610 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
611 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700612 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500613 pub paren_token: token::Paren,
David Tolnay41871922017-12-29 01:53:45 -0500614 pub front: Delimited<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500615 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500616 pub comma_token: Option<Token![,]>,
617 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700618 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700619 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700620 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800621 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500622 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700623 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700624 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700625 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800626 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500627 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500628 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700629 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700630 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700631 pub Lit(PatLit {
632 pub expr: Box<Expr>,
633 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800634 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700635 pub Range(PatRange {
636 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700637 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500638 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700639 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400640 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700641 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500642 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800643 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700644 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500645 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500646 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500647 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700648 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700649 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800650 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700651 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700652}
653
Michael Layzell734adb42017-06-07 16:58:31 -0400654#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700655ast_struct! {
656 /// An arm of a 'match'.
657 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800658 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700659 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500660 /// ```rust
661 /// # #![feature(dotdoteq_in_patterns)]
662 /// #
663 /// # fn main() {
664 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700665 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500666 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700667 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500668 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700669 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500670 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700671 /// ```
672 pub struct Arm {
673 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800674 pub pats: Delimited<Pat, Token![|]>,
675 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700676 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800677 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700678 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800679 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700680 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700681}
682
Michael Layzell734adb42017-06-07 16:58:31 -0400683#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700684ast_enum! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700685 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700686 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 pub enum RangeLimits {
688 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800689 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700690 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800691 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700693}
694
Michael Layzell734adb42017-06-07 16:58:31 -0400695#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700696ast_struct! {
697 /// A single field in a struct pattern
698 ///
699 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay5d7098a2017-12-29 01:35:24 -0500700 /// are treated the same as `x: x, y: ref y, z: ref mut z` but
701 /// there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500703 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500705 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500706 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700707 /// The pattern the field is destructured to
708 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700710}
711
Michael Layzell3936ceb2017-07-08 00:28:36 -0400712#[cfg(any(feature = "parsing", feature = "printing"))]
713#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700714fn arm_expr_requires_comma(expr: &Expr) -> bool {
715 // see https://github.com/rust-lang/rust/blob/eb8f2586e
716 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500717 match *expr {
718 Expr::Unsafe(..)
719 | Expr::Block(..)
720 | Expr::If(..)
721 | Expr::IfLet(..)
722 | Expr::Match(..)
723 | Expr::While(..)
724 | Expr::WhileLet(..)
725 | Expr::Loop(..)
726 | Expr::ForLoop(..)
727 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700728 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400729 }
730}
731
David Tolnayb9c8e322016-09-23 20:48:37 -0700732#[cfg(feature = "parsing")]
733pub mod parsing {
734 use super::*;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500735 use ty::parsing::qpath;
736 #[cfg(feature = "full")]
737 use ty::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700738
Michael Layzell734adb42017-06-07 16:58:31 -0400739 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500740 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500741 use synom::Synom;
742 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400743 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500744 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500745 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700746
David Tolnaybcf26022017-12-25 22:10:52 -0500747 // When we're parsing expressions which occur before blocks, like in an if
748 // statement's condition, we cannot parse a struct literal.
749 //
750 // Struct literals are ambiguous in certain positions
751 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700752 macro_rules! ambiguous_expr {
753 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700754 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700755 };
756 }
757
David Tolnaybcf26022017-12-25 22:10:52 -0500758 // When we are parsing an optional suffix expression, we cannot allow blocks
759 // if structs are not allowed.
760 //
761 // Example:
762 //
763 // if break {} {}
764 //
765 // is ambiguous between:
766 //
767 // if (break {}) {}
768 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400769 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400770 macro_rules! opt_ambiguous_expr {
771 ($i:expr, $allow_struct:ident) => {
772 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
773 };
774 }
775
Alex Crichton954046c2017-05-30 21:49:42 -0700776 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400777 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700778
779 fn description() -> Option<&'static str> {
780 Some("expression")
781 }
782 }
783
Michael Layzell734adb42017-06-07 16:58:31 -0400784 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700785 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
786
David Tolnaybcf26022017-12-25 22:10:52 -0500787 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400788 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500789 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500790 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400791 }
792
Michael Layzell734adb42017-06-07 16:58:31 -0400793 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500794 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500795 // NOTE: We intentionally skip assign_expr, placement_expr, and
796 // range_expr, as they are not parsed in non-full mode.
797 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400798 }
799
David Tolnaybcf26022017-12-25 22:10:52 -0500800 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400801 macro_rules! binop {
802 (
803 $name: ident,
804 $next: ident,
805 $submac: ident!( $($args:tt)* )
806 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500807 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400808 mut e: call!($next, allow_struct, allow_block) >>
809 many0!(do_parse!(
810 op: $submac!($($args)*) >>
811 rhs: call!($next, allow_struct, true) >>
812 ({
813 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500814 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400815 left: Box::new(e.into()),
816 op: op,
817 right: Box::new(rhs.into()),
818 }.into();
819 })
820 )) >>
821 (e)
822 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700823 }
David Tolnay54e854d2016-10-24 12:03:30 -0700824 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700825
David Tolnaybcf26022017-12-25 22:10:52 -0500826 // <placement> = <placement> ..
827 // <placement> += <placement> ..
828 // <placement> -= <placement> ..
829 // <placement> *= <placement> ..
830 // <placement> /= <placement> ..
831 // <placement> %= <placement> ..
832 // <placement> ^= <placement> ..
833 // <placement> &= <placement> ..
834 // <placement> |= <placement> ..
835 // <placement> <<= <placement> ..
836 // <placement> >>= <placement> ..
837 //
838 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400839 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500840 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400841 mut e: call!(placement_expr, allow_struct, allow_block) >>
842 alt!(
843 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800844 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400845 // Recurse into self to parse right-associative operator.
846 rhs: call!(assign_expr, allow_struct, true) >>
847 ({
848 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500849 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400850 left: Box::new(e.into()),
851 eq_token: eq,
852 right: Box::new(rhs.into()),
853 }.into();
854 })
855 )
856 |
857 do_parse!(
858 op: call!(BinOp::parse_assign_op) >>
859 // Recurse into self to parse right-associative operator.
860 rhs: call!(assign_expr, allow_struct, true) >>
861 ({
862 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500863 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400864 left: Box::new(e.into()),
865 op: op,
866 right: Box::new(rhs.into()),
867 }.into();
868 })
869 )
870 |
871 epsilon!()
872 ) >>
873 (e)
874 ));
875
David Tolnaybcf26022017-12-25 22:10:52 -0500876 // <range> <- <range> ..
877 //
878 // NOTE: The `in place { expr }` version of this syntax is parsed in
879 // `atom_expr`, not here.
880 //
881 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400882 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500883 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400884 mut e: call!(range_expr, allow_struct, allow_block) >>
885 alt!(
886 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800887 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400888 // Recurse into self to parse right-associative operator.
889 rhs: call!(placement_expr, allow_struct, true) >>
890 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400891 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500892 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400893 // op: BinOp::Place(larrow),
894 place: Box::new(e.into()),
David Tolnay8701a5c2017-12-28 23:31:10 -0500895 arrow_token: arrow,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400896 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400897 }.into();
898 })
899 )
900 |
901 epsilon!()
902 ) >>
903 (e)
904 ));
905
David Tolnaybcf26022017-12-25 22:10:52 -0500906 // <or> ... <or> ..
907 // <or> .. <or> ..
908 // <or> ..
909 //
910 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
911 // rules are for parsing these expressions are, but this is not correct.
912 // For example, `a .. b .. c` is not a legal expression. It should not
913 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
914 //
915 // NOTE: The form of ranges which don't include a preceding expression are
916 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400917 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500918 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400919 mut e: call!(or_expr, allow_struct, allow_block) >>
920 many0!(do_parse!(
921 limits: syn!(RangeLimits) >>
922 // We don't want to allow blocks here if we don't allow structs. See
923 // the reasoning for `opt_ambiguous_expr!` above.
924 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
925 ({
926 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500927 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400928 from: Some(Box::new(e.into())),
929 limits: limits,
930 to: hi.map(|e| Box::new(e.into())),
931 }.into();
932 })
933 )) >>
934 (e)
935 ));
936
David Tolnaybcf26022017-12-25 22:10:52 -0500937 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800938 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400939
David Tolnaybcf26022017-12-25 22:10:52 -0500940 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800941 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400942
David Tolnaybcf26022017-12-25 22:10:52 -0500943 // <bitor> == <bitor> ...
944 // <bitor> != <bitor> ...
945 // <bitor> >= <bitor> ...
946 // <bitor> <= <bitor> ...
947 // <bitor> > <bitor> ...
948 // <bitor> < <bitor> ...
949 //
950 // NOTE: This operator appears to be parsed as left-associative, but errors
951 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500952 binop!(
953 compare_expr,
954 bitor_expr,
955 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800956 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400957 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800958 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400959 |
960 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800961 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400962 |
963 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800964 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400965 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400966 do_parse!(
967 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800968 not!(punct!(<-)) >>
969 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400970 (BinOp::Lt(t))
971 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400972 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800973 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500974 )
975 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400976
David Tolnaybcf26022017-12-25 22:10:52 -0500977 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500978 binop!(
979 bitor_expr,
980 bitxor_expr,
981 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
982 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400983
David Tolnaybcf26022017-12-25 22:10:52 -0500984 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500985 binop!(
986 bitxor_expr,
987 bitand_expr,
988 do_parse!(
989 // NOTE: Make sure we aren't looking at ^=.
990 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
991 )
992 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400993
David Tolnaybcf26022017-12-25 22:10:52 -0500994 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500995 binop!(
996 bitand_expr,
997 shift_expr,
998 do_parse!(
999 // NOTE: Make sure we aren't looking at && or &=.
1000 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1001 )
1002 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001003
David Tolnaybcf26022017-12-25 22:10:52 -05001004 // <arith> << <arith> ...
1005 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001006 binop!(
1007 shift_expr,
1008 arith_expr,
1009 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001010 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001011 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001012 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001013 )
1014 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001015
David Tolnaybcf26022017-12-25 22:10:52 -05001016 // <term> + <term> ...
1017 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001018 binop!(
1019 arith_expr,
1020 term_expr,
1021 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001023 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001024 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001025 )
1026 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001027
David Tolnaybcf26022017-12-25 22:10:52 -05001028 // <cast> * <cast> ...
1029 // <cast> / <cast> ...
1030 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001031 binop!(
1032 term_expr,
1033 cast_expr,
1034 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001035 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001036 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001037 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001038 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001039 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001040 )
1041 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // <unary> as <ty>
1044 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001045 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001046 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001047 mut e: call!(unary_expr, allow_struct, allow_block) >>
1048 many0!(alt!(
1049 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001051 // We can't accept `A + B` in cast expressions, as it's
1052 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001053 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001054 ({
1055 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001056 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001057 expr: Box::new(e.into()),
1058 as_token: as_,
1059 ty: Box::new(ty),
1060 }.into();
1061 })
1062 )
1063 |
1064 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001065 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066 // We can't accept `A + B` in cast expressions, as it's
1067 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001068 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001069 ({
1070 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001071 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001072 expr: Box::new(e.into()),
1073 colon_token: colon,
1074 ty: Box::new(ty),
1075 }.into();
1076 })
1077 )
1078 )) >>
1079 (e)
1080 ));
1081
David Tolnay0cf94f22017-12-28 23:46:26 -05001082 // <unary> as <ty>
1083 #[cfg(not(feature = "full"))]
1084 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1085 mut e: call!(unary_expr, allow_struct, allow_block) >>
1086 many0!(do_parse!(
1087 as_: keyword!(as) >>
1088 // We can't accept `A + B` in cast expressions, as it's
1089 // ambiguous with the + expression.
1090 ty: call!(Type::without_plus) >>
1091 ({
1092 e = ExprCast {
1093 attrs: Vec::new(),
1094 expr: Box::new(e.into()),
1095 as_token: as_,
1096 ty: Box::new(ty),
1097 }.into();
1098 })
1099 )) >>
1100 (e)
1101 ));
1102
David Tolnaybcf26022017-12-25 22:10:52 -05001103 // <UnOp> <trailer>
1104 // & <trailer>
1105 // &mut <trailer>
1106 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001107 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001108 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001109 do_parse!(
1110 op: syn!(UnOp) >>
1111 expr: call!(unary_expr, allow_struct, true) >>
1112 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001113 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001114 op: op,
1115 expr: Box::new(expr.into()),
1116 }.into())
1117 )
1118 |
1119 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001120 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001121 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001122 expr: call!(unary_expr, allow_struct, true) >>
1123 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001124 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001125 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001126 mutability: mutability,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001127 expr: Box::new(expr.into()),
1128 }.into())
1129 )
1130 |
1131 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001132 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001133 expr: call!(unary_expr, allow_struct, true) >>
1134 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001135 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001136 box_token: box_,
1137 expr: Box::new(expr.into()),
1138 }.into())
1139 )
1140 |
1141 call!(trailer_expr, allow_struct, allow_block)
1142 ));
1143
Michael Layzell734adb42017-06-07 16:58:31 -04001144 // XXX: This duplication is ugly
1145 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001146 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001147 do_parse!(
1148 op: syn!(UnOp) >>
1149 expr: call!(unary_expr, allow_struct, true) >>
1150 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001151 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001152 op: op,
1153 expr: Box::new(expr.into()),
1154 }.into())
1155 )
1156 |
1157 call!(trailer_expr, allow_struct, allow_block)
1158 ));
1159
David Tolnaybcf26022017-12-25 22:10:52 -05001160 // <atom> (..<args>) ...
1161 // <atom> . <ident> (..<args>) ...
1162 // <atom> . <ident> ...
1163 // <atom> . <lit> ...
1164 // <atom> [ <expr> ] ...
1165 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001166 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001167 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 mut e: call!(atom_expr, allow_struct, allow_block) >>
1169 many0!(alt!(
1170 tap!(args: and_call => {
1171 let (args, paren) = args;
1172 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001173 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001174 func: Box::new(e.into()),
1175 args: args,
1176 paren_token: paren,
1177 }.into();
1178 })
1179 |
1180 tap!(more: and_method_call => {
1181 let mut call = more;
David Tolnay76418512017-12-28 23:47:47 -05001182 call.receiver = Box::new(e.into());
Michael Layzellb78f3b52017-06-04 19:03:03 -04001183 e = call.into();
1184 })
1185 |
1186 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001187 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001189 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001190 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001191 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001192 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001193 }.into();
1194 })
1195 |
1196 tap!(i: and_index => {
1197 let (i, token) = i;
1198 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001199 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001200 expr: Box::new(e.into()),
1201 bracket_token: token,
1202 index: Box::new(i),
1203 }.into();
1204 })
1205 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001206 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001207 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001208 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001209 expr: Box::new(e.into()),
1210 question_token: question,
1211 }.into();
1212 })
1213 )) >>
1214 (e)
1215 ));
1216
Michael Layzell734adb42017-06-07 16:58:31 -04001217 // XXX: Duplication == ugly
1218 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001219 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001220 mut e: call!(atom_expr, allow_struct, allow_block) >>
1221 many0!(alt!(
1222 tap!(args: and_call => {
1223 let (args, paren) = args;
1224 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001225 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001226 func: Box::new(e.into()),
1227 args: args,
1228 paren_token: paren,
1229 }.into();
1230 })
1231 |
1232 tap!(i: and_index => {
1233 let (i, token) = i;
1234 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001235 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001236 expr: Box::new(e.into()),
1237 bracket_token: token,
1238 index: Box::new(i),
1239 }.into();
1240 })
1241 )) >>
1242 (e)
1243 ));
1244
David Tolnaybcf26022017-12-25 22:10:52 -05001245 // Parse all atomic expressions which don't have to worry about precidence
1246 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001247 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001248 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1249 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001250 |
David Tolnay8c91b882017-12-28 23:04:32 -05001251 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 |
1253 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001254 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001255 |
David Tolnay8c91b882017-12-28 23:04:32 -05001256 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001257 |
David Tolnay8c91b882017-12-28 23:04:32 -05001258 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 |
1260 call!(expr_break, allow_struct) // must be before expr_path
1261 |
David Tolnay8c91b882017-12-28 23:04:32 -05001262 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001263 |
1264 call!(expr_ret, allow_struct) // must be before expr_path
1265 |
David Tolnay8c91b882017-12-28 23:04:32 -05001266 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267 |
David Tolnay8c91b882017-12-28 23:04:32 -05001268 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001269 |
David Tolnay8c91b882017-12-28 23:04:32 -05001270 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001271 |
David Tolnay8c91b882017-12-28 23:04:32 -05001272 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001273 |
David Tolnay8c91b882017-12-28 23:04:32 -05001274 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001275 |
David Tolnay8c91b882017-12-28 23:04:32 -05001276 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001277 |
David Tolnay8c91b882017-12-28 23:04:32 -05001278 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001279 |
David Tolnay8c91b882017-12-28 23:04:32 -05001280 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001281 |
David Tolnay8c91b882017-12-28 23:04:32 -05001282 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001283 |
David Tolnay8c91b882017-12-28 23:04:32 -05001284 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001285 |
David Tolnay8c91b882017-12-28 23:04:32 -05001286 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001287 |
David Tolnay8c91b882017-12-28 23:04:32 -05001288 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001289 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001290 call!(expr_closure, allow_struct)
1291 |
David Tolnay8c91b882017-12-28 23:04:32 -05001292 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001293 |
1294 // NOTE: This is the prefix-form of range
1295 call!(expr_range, allow_struct)
1296 |
David Tolnay8c91b882017-12-28 23:04:32 -05001297 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001298 |
David Tolnay8c91b882017-12-28 23:04:32 -05001299 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001300 ));
1301
Michael Layzell734adb42017-06-07 16:58:31 -04001302 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001303 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001304 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001305 |
David Tolnay8c91b882017-12-28 23:04:32 -05001306 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001307 ));
1308
Michael Layzell734adb42017-06-07 16:58:31 -04001309 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001310 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001311 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001314 |
David Tolnay8c91b882017-12-28 23:04:32 -05001315 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001316 |
David Tolnay8c91b882017-12-28 23:04:32 -05001317 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001318 |
David Tolnay8c91b882017-12-28 23:04:32 -05001319 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001320 |
David Tolnay8c91b882017-12-28 23:04:32 -05001321 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001322 |
David Tolnay8c91b882017-12-28 23:04:32 -05001323 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001324 |
David Tolnay8c91b882017-12-28 23:04:32 -05001325 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001326 |
David Tolnay8c91b882017-12-28 23:04:32 -05001327 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001328 |
David Tolnay8c91b882017-12-28 23:04:32 -05001329 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001330 |
David Tolnay8c91b882017-12-28 23:04:32 -05001331 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001332 ), Expr::from));
1333
David Tolnay8c91b882017-12-28 23:04:32 -05001334 impl Synom for ExprLit {
1335 named!(parse -> Self, do_parse!(
1336 lit: syn!(Lit) >>
1337 (ExprLit {
1338 attrs: Vec::new(),
1339 lit: lit,
1340 })
1341 ));
1342 }
1343
1344 #[cfg(feature = "full")]
1345 impl Synom for ExprMacro {
1346 named!(parse -> Self, do_parse!(
1347 mac: syn!(Macro) >>
1348 (ExprMacro {
1349 attrs: Vec::new(),
1350 mac: mac,
1351 })
1352 ));
1353 }
1354
David Tolnaye98775f2017-12-28 23:17:00 -05001355 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001356 impl Synom for ExprGroup {
1357 named!(parse -> Self, do_parse!(
1358 e: grouped!(syn!(Expr)) >>
1359 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001360 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001361 expr: Box::new(e.0),
1362 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001363 })
Michael Layzell93c36282017-06-04 20:43:14 -04001364 ));
1365 }
1366
David Tolnaye98775f2017-12-28 23:17:00 -05001367 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001368 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001369 named!(parse -> Self, do_parse!(
1370 e: parens!(syn!(Expr)) >>
1371 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001372 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001373 expr: Box::new(e.0),
1374 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001375 })
Michael Layzell92639a52017-06-01 00:07:44 -04001376 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001377 }
David Tolnay89e05672016-10-02 14:39:42 -07001378
Michael Layzell734adb42017-06-07 16:58:31 -04001379 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001380 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001381 named!(parse -> Self, do_parse!(
1382 elems: brackets!(call!(Delimited::parse_terminated)) >>
1383 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001384 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001385 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001386 bracket_token: elems.1,
1387 })
1388 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001389 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001390
David Tolnay32954ef2017-12-26 22:43:16 -05001391 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001392 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001393
Michael Layzell734adb42017-06-07 16:58:31 -04001394 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001395 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001396 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001397 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001398 turbofish: option!(tuple!(
1399 punct!(::),
1400 punct!(<),
1401 call!(Delimited::parse_terminated),
1402 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001403 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001404 args: parens!(call!(Delimited::parse_terminated)) >>
1405 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001406 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001407 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001408 // this expr will get overwritten after being returned
David Tolnay76418512017-12-28 23:47:47 -05001409 receiver: Box::new(Expr::Lit(ExprLit {
David Tolnay8c91b882017-12-28 23:04:32 -05001410 attrs: Vec::new(),
1411 lit: Lit {
1412 span: Span::default(),
1413 value: LitKind::Bool(false),
1414 },
Alex Crichton954046c2017-05-30 21:49:42 -07001415 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001416
Alex Crichton954046c2017-05-30 21:49:42 -07001417 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001418 turbofish: turbofish.map(|fish| MethodTurbofish {
1419 colon2_token: fish.0,
1420 lt_token: fish.1,
1421 args: fish.2,
1422 gt_token: fish.3,
1423 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001424 args: args.0,
1425 paren_token: args.1,
1426 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001427 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001428 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001429 ));
1430
Michael Layzell734adb42017-06-07 16:58:31 -04001431 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001432 impl Synom for GenericMethodArgument {
1433 // TODO parse const generics as well
1434 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1435 }
1436
1437 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001438 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001439 named!(parse -> Self, do_parse!(
1440 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001441 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001442 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001443 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001444 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001445 })
1446 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001447 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001448
Michael Layzell734adb42017-06-07 16:58:31 -04001449 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001450 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001451 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001452 if_: keyword!(if) >>
1453 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001454 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001455 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001456 cond: expr_no_struct >>
1457 then_block: braces!(call!(Block::parse_within)) >>
1458 else_block: option!(else_block) >>
1459 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001460 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001461 pat: Box::new(pat),
1462 let_token: let_,
1463 eq_token: eq,
1464 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001465 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001466 stmts: then_block.0,
1467 brace_token: then_block.1,
1468 },
1469 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001470 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001471 })
1472 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001473 }
1474
Michael Layzell734adb42017-06-07 16:58:31 -04001475 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001476 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001477 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001478 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001479 cond: expr_no_struct >>
1480 then_block: braces!(call!(Block::parse_within)) >>
1481 else_block: option!(else_block) >>
1482 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001483 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001484 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001485 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001486 stmts: then_block.0,
1487 brace_token: then_block.1,
1488 },
1489 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001490 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001491 })
1492 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001493 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001494
Michael Layzell734adb42017-06-07 16:58:31 -04001495 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001496 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001497 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001498 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001499 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001500 |
David Tolnay8c91b882017-12-28 23:04:32 -05001501 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001502 |
1503 do_parse!(
1504 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001505 (Expr::Block(ExprBlock {
1506 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001507 block: Block {
1508 stmts: else_block.0,
1509 brace_token: else_block.1,
1510 },
1511 }))
David Tolnay939766a2016-09-23 23:48:12 -07001512 )
Alex Crichton954046c2017-05-30 21:49:42 -07001513 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001514 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001515 ));
1516
Michael Layzell734adb42017-06-07 16:58:31 -04001517 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001518 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001519 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001520 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1521 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001522 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001523 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001524 expr: expr_no_struct >>
1525 loop_block: syn!(Block) >>
1526 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001527 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001528 for_token: for_,
1529 in_token: in_,
1530 pat: Box::new(pat),
1531 expr: Box::new(expr),
1532 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001533 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001534 label: lbl.map(|p| p.0),
1535 })
1536 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001537 }
Gregory Katze5f35682016-09-27 14:20:55 -04001538
Michael Layzell734adb42017-06-07 16:58:31 -04001539 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001540 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001541 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001542 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1543 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001544 loop_block: syn!(Block) >>
1545 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001546 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001547 loop_token: loop_,
1548 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001549 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001550 label: lbl.map(|p| p.0),
1551 })
1552 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001553 }
1554
Michael Layzell734adb42017-06-07 16:58:31 -04001555 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001556 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001557 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001558 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001559 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001560 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001561 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001562 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001563 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001564 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001565 expr: Box::new(obj),
1566 match_token: match_,
1567 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001568 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001569 }
1570 })
1571 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001572 }
David Tolnay1978c672016-10-27 22:05:52 -07001573
Michael Layzell734adb42017-06-07 16:58:31 -04001574 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001575 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001576 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001577 do_: keyword!(do) >>
1578 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001579 catch_block: syn!(Block) >>
1580 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001581 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001582 block: catch_block,
1583 do_token: do_,
1584 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001585 })
Michael Layzell92639a52017-06-01 00:07:44 -04001586 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001587 }
Arnavion02ef13f2017-04-25 00:54:31 -07001588
Michael Layzell734adb42017-06-07 16:58:31 -04001589 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001590 impl Synom for ExprYield {
1591 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001592 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001593 expr: option!(syn!(Expr)) >>
1594 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001595 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001596 yield_token: yield_,
1597 expr: expr.map(Box::new),
1598 })
1599 ));
1600 }
1601
1602 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001603 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001604 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001605 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001606 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001607 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1608 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001609 body: do_parse!(
1610 expr: alt!(expr_nosemi | syn!(Expr)) >>
1611 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1612 map!(input_end!(), |_| None)
1613 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001614 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001615 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001616 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001617 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001618 ) >>
1619 (Arm {
1620 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001621 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001622 attrs: attrs,
1623 pats: pats,
1624 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001625 body: Box::new(body.0),
1626 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001627 })
1628 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001629 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001630
Michael Layzell734adb42017-06-07 16:58:31 -04001631 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001632 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001633 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001634 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001635 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001636 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001637 ret_and_body: alt!(
1638 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001639 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001640 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001641 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001642 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001643 Expr::Block(ExprBlock {
1644 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001645 block: body,
1646 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001647 )
1648 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001649 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001650 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001651 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001652 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001653 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001654 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001655 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001656 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001657 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001658 body: Box::new(ret_and_body.1),
1659 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001660 ));
1661
Michael Layzell734adb42017-06-07 16:58:31 -04001662 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001663 named!(fn_arg -> FnArg, do_parse!(
1664 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001665 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001666 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001667 if let Some((colon, ty)) = ty {
1668 FnArg::Captured(ArgCaptured {
1669 pat: pat,
1670 colon_token: colon,
1671 ty: ty,
1672 })
1673 } else {
1674 FnArg::Inferred(pat)
1675 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001676 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001677 ));
1678
Michael Layzell734adb42017-06-07 16:58:31 -04001679 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001680 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001681 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001682 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1683 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001684 cond: expr_no_struct >>
1685 while_block: syn!(Block) >>
1686 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001687 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001688 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001689 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001690 cond: Box::new(cond),
1691 body: while_block,
1692 label: lbl.map(|p| p.0),
1693 })
1694 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001695 }
1696
Michael Layzell734adb42017-06-07 16:58:31 -04001697 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001698 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001699 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001700 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1701 while_: keyword!(while) >>
1702 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001703 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001704 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001705 value: expr_no_struct >>
1706 while_block: syn!(Block) >>
1707 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001708 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001709 eq_token: eq,
1710 let_token: let_,
1711 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001712 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001713 pat: Box::new(pat),
1714 expr: Box::new(value),
1715 body: while_block,
1716 label: lbl.map(|p| p.0),
1717 })
1718 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001719 }
1720
Michael Layzell734adb42017-06-07 16:58:31 -04001721 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001722 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001723 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001724 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001725 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001726 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001727 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001728 continue_token: cont,
1729 label: lbl,
1730 })
1731 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001732 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001733
Michael Layzell734adb42017-06-07 16:58:31 -04001734 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001735 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001736 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001737 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001738 // We can't allow blocks after a `break` expression when we wouldn't
1739 // allow structs, as this expression is ambiguous.
1740 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001741 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001742 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001743 label: lbl,
1744 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001745 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001746 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001747 ));
1748
Michael Layzell734adb42017-06-07 16:58:31 -04001749 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001750 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001751 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001752 // NOTE: return is greedy and eats blocks after it even when in a
1753 // position where structs are not allowed, such as in if statement
1754 // conditions. For example:
1755 //
David Tolnaybcf26022017-12-25 22:10:52 -05001756 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001757 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001758 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001759 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001760 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001761 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001762 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001763 ));
1764
Michael Layzell734adb42017-06-07 16:58:31 -04001765 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001766 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001767 named!(parse -> Self, do_parse!(
1768 path: syn!(Path) >>
1769 data: braces!(do_parse!(
1770 fields: call!(Delimited::parse_terminated) >>
1771 base: option!(
1772 cond!(fields.is_empty() || fields.trailing_delim(),
1773 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001774 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001775 base: syn!(Expr) >>
1776 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001777 )
Michael Layzell92639a52017-06-01 00:07:44 -04001778 )
1779 ) >>
1780 (fields, base)
1781 )) >>
1782 ({
1783 let ((fields, base), brace) = data;
1784 let (dots, rest) = match base.and_then(|b| b) {
1785 Some((dots, base)) => (Some(dots), Some(base)),
1786 None => (None, None),
1787 };
1788 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001789 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001790 brace_token: brace,
1791 path: path,
1792 fields: fields,
1793 dot2_token: dots,
1794 rest: rest.map(Box::new),
1795 }
1796 })
1797 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001798 }
1799
Michael Layzell734adb42017-06-07 16:58:31 -04001800 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001801 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001802 named!(parse -> Self, alt!(
1803 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001804 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001805 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001806 value: syn!(Expr) >>
1807 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001808 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001809 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001810 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001811 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001812 })
Michael Layzell92639a52017-06-01 00:07:44 -04001813 )
1814 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001815 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001816 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001817 expr: Expr::Path(ExprPath {
1818 attrs: Vec::new(),
1819 qself: None,
1820 path: name.into(),
1821 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001822 attrs: Vec::new(),
1823 colon_token: None,
1824 })
1825 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001826 }
David Tolnay055a7042016-10-02 19:23:54 -07001827
Michael Layzell734adb42017-06-07 16:58:31 -04001828 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001829 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001830 named!(parse -> Self, do_parse!(
1831 data: brackets!(do_parse!(
1832 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001833 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001834 times: syn!(Expr) >>
1835 (value, semi, times)
1836 )) >>
1837 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001838 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001839 expr: Box::new((data.0).0),
1840 amt: Box::new((data.0).2),
1841 bracket_token: data.1,
1842 semi_token: (data.0).1,
1843 })
1844 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001845 }
David Tolnay055a7042016-10-02 19:23:54 -07001846
Michael Layzell734adb42017-06-07 16:58:31 -04001847 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001848 impl Synom for ExprUnsafe {
1849 named!(parse -> Self, do_parse!(
1850 unsafe_: keyword!(unsafe) >>
1851 b: syn!(Block) >>
1852 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001853 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001854 unsafe_token: unsafe_,
1855 block: b,
1856 })
1857 ));
1858 }
1859
1860 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001861 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001862 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001863 b: syn!(Block) >>
1864 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001865 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001866 block: b,
1867 })
1868 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001869 }
David Tolnay89e05672016-10-02 14:39:42 -07001870
Michael Layzell734adb42017-06-07 16:58:31 -04001871 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001872 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001873 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001874 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001875 (ExprRange {
1876 attrs: Vec::new(),
1877 from: None,
1878 to: hi.map(Box::new),
1879 limits: limits,
1880 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001881 ));
1882
Michael Layzell734adb42017-06-07 16:58:31 -04001883 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001884 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001885 named!(parse -> Self, alt!(
1886 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001887 punct!(..=) => { RangeLimits::Closed }
1888 |
1889 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001890 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001891 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001892 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001893 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001894 }
David Tolnay438c9052016-10-07 23:24:48 -07001895
Alex Crichton954046c2017-05-30 21:49:42 -07001896 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001897 named!(parse -> Self, do_parse!(
1898 pair: qpath >>
1899 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001900 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001901 qself: pair.0,
1902 path: pair.1,
1903 })
1904 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001905 }
David Tolnay42602292016-10-01 22:25:45 -07001906
Michael Layzell734adb42017-06-07 16:58:31 -04001907 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001908 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001909
David Tolnay32954ef2017-12-26 22:43:16 -05001910 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001911
Michael Layzell734adb42017-06-07 16:58:31 -04001912 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001913 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001914 named!(parse -> Self, do_parse!(
1915 stmts: braces!(call!(Block::parse_within)) >>
1916 (Block {
1917 stmts: stmts.0,
1918 brace_token: stmts.1,
1919 })
1920 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001921 }
David Tolnay939766a2016-09-23 23:48:12 -07001922
Michael Layzell734adb42017-06-07 16:58:31 -04001923 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001924 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001925 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001926 many0!(punct!(;)) >>
1927 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001928 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001929 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001930 mut e: syn!(Expr) >>
1931 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001932 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001933 Stmt::Expr(Box::new(e))
1934 })
1935 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001936 (match last {
1937 None => standalone,
1938 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001939 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001940 standalone
1941 }
1942 })
1943 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001944 }
1945
Michael Layzell734adb42017-06-07 16:58:31 -04001946 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001947 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001948 named!(parse -> Self, alt!(
1949 stmt_mac
1950 |
1951 stmt_local
1952 |
1953 stmt_item
1954 |
Michael Layzell35418782017-06-07 09:20:25 -04001955 stmt_blockexpr
1956 |
Michael Layzell92639a52017-06-01 00:07:44 -04001957 stmt_expr
1958 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001959 }
David Tolnay939766a2016-09-23 23:48:12 -07001960
Michael Layzell734adb42017-06-07 16:58:31 -04001961 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001962 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001963 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001964 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001965 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001966 // Only parse braces here; paren and bracket will get parsed as
1967 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001968 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001969 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001970 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1971 attrs: attrs,
1972 ident: None,
1973 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001974 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001975 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001976 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001977 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001978 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001979 },
David Tolnayeea28d62016-10-25 20:44:08 -07001980 },
David Tolnay57b52bc2017-12-28 18:06:38 -05001981 semi_token: semi,
1982 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07001983 ));
1984
Michael Layzell734adb42017-06-07 16:58:31 -04001985 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001986 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001987 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001988 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001989 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001990 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001991 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1992 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001993 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001994 let_token: let_,
1995 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001996 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
1997 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07001998 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07001999 ty: ty.map(|p| Box::new(p.1)),
2000 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002001 attrs: attrs,
2002 })))
2003 ));
2004
Michael Layzell734adb42017-06-07 16:58:31 -04002005 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002006 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002007
Michael Layzell734adb42017-06-07 16:58:31 -04002008 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002009 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002010 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002011 mut e: expr_nosemi >>
2012 // If the next token is a `.` or a `?` it is special-cased to parse as
2013 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002014 not!(punct!(.)) >>
2015 not!(punct!(?)) >>
2016 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002017 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002018 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002019 if let Some(semi) = semi {
2020 Stmt::Semi(Box::new(e), semi)
2021 } else {
2022 Stmt::Expr(Box::new(e))
2023 }
2024 })
2025 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002026
Michael Layzell734adb42017-06-07 16:58:31 -04002027 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002028 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002029 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002030 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002031 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002032 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002033 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002034 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002035 })
David Tolnay939766a2016-09-23 23:48:12 -07002036 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002037
Michael Layzell734adb42017-06-07 16:58:31 -04002038 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002039 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002040 named!(parse -> Self, alt!(
2041 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2042 |
2043 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2044 |
2045 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2046 |
2047 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2048 |
2049 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2050 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002051 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002052 |
2053 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2054 |
2055 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2056 |
2057 syn!(PatPath) => { Pat::Path }
2058 |
2059 syn!(PatTuple) => { Pat::Tuple }
2060 |
2061 syn!(PatRef) => { Pat::Ref }
2062 |
2063 syn!(PatSlice) => { Pat::Slice }
2064 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002065 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002066
Michael Layzell734adb42017-06-07 16:58:31 -04002067 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002068 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002069 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002070 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002071 |u| PatWild { underscore_token: u }
2072 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002073 }
David Tolnay84aa0752016-10-02 23:01:13 -07002074
Michael Layzell734adb42017-06-07 16:58:31 -04002075 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002076 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002077 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002078 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002079 pat: syn!(Pat) >>
2080 (PatBox {
2081 pat: Box::new(pat),
2082 box_token: boxed,
2083 })
2084 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002085 }
2086
Michael Layzell734adb42017-06-07 16:58:31 -04002087 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002088 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002089 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002090 by_ref: option!(keyword!(ref)) >>
2091 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002092 name: alt!(
2093 syn!(Ident)
2094 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002095 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002096 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002097 not!(punct!(<)) >>
2098 not!(punct!(::)) >>
2099 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002100 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002101 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002102 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002103 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002104 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002105 subpat: subpat.map(|p| Box::new(p.1)),
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 PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002112 named!(parse -> Self, do_parse!(
2113 path: syn!(Path) >>
2114 tuple: syn!(PatTuple) >>
2115 (PatTupleStruct {
2116 path: path,
2117 pat: tuple,
2118 })
2119 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002120 }
2121
Michael Layzell734adb42017-06-07 16:58:31 -04002122 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002123 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002124 named!(parse -> Self, do_parse!(
2125 path: syn!(Path) >>
2126 data: braces!(do_parse!(
2127 fields: call!(Delimited::parse_terminated) >>
2128 base: option!(
2129 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002130 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002131 ) >>
2132 (fields, base)
2133 )) >>
2134 (PatStruct {
2135 path: path,
2136 fields: (data.0).0,
2137 brace_token: data.1,
2138 dot2_token: (data.0).1.and_then(|m| m),
2139 })
2140 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002141 }
2142
Michael Layzell734adb42017-06-07 16:58:31 -04002143 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002144 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002145 named!(parse -> Self, alt!(
2146 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002147 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002148 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002149 pat: syn!(Pat) >>
2150 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002151 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002152 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002153 attrs: Vec::new(),
2154 colon_token: Some(colon),
2155 })
2156 )
2157 |
2158 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002159 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002160 by_ref: option!(keyword!(ref)) >>
2161 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002162 ident: syn!(Ident) >>
2163 ({
2164 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002165 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002166 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002167 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002168 subpat: None,
2169 at_token: None,
2170 }.into();
2171 if let Some(boxed) = boxed {
2172 pat = PatBox {
2173 pat: Box::new(pat),
2174 box_token: boxed,
2175 }.into();
2176 }
2177 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002178 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002179 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002180 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002181 colon_token: None,
2182 }
2183 })
2184 )
2185 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002186 }
2187
Michael Layzell734adb42017-06-07 16:58:31 -04002188 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002189 impl Synom for Member {
2190 named!(parse -> Self, alt!(
2191 syn!(Ident) => { Member::Named }
2192 |
2193 syn!(Index) => { Member::Unnamed }
2194 ));
2195 }
2196
2197 #[cfg(feature = "full")]
2198 impl Synom for Index {
2199 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002200 lit: syn!(Lit) >>
2201 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002202 if let Ok(i) = lit.value.to_string().parse() {
2203 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002204 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002205 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002206 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002207 })
David Tolnay85b69a42017-12-27 20:43:10 -05002208 ));
2209 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002210
Michael Layzell734adb42017-06-07 16:58:31 -04002211 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002212 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002213 named!(parse -> Self, map!(
2214 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002215 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002216 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002217 }
David Tolnay9636c052016-10-02 17:11:17 -07002218
Michael Layzell734adb42017-06-07 16:58:31 -04002219 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002220 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002221 named!(parse -> Self, do_parse!(
2222 data: parens!(do_parse!(
David Tolnay41871922017-12-29 01:53:45 -05002223 front: call!(Delimited::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002224 dotdot: map!(cond!(
David Tolnay41871922017-12-29 01:53:45 -05002225 front.is_empty() || front.trailing_delim(),
Michael Layzell92639a52017-06-01 00:07:44 -04002226 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002227 dots: punct!(..) >>
2228 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002229 (dots, trailing)
2230 ))
David Tolnay41871922017-12-29 01:53:45 -05002231 ), Option::unwrap_or_default) >>
2232 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002233 Some((_, Some(_))) => true,
2234 _ => false,
2235 },
2236 call!(Delimited::parse_terminated)) >>
David Tolnay41871922017-12-29 01:53:45 -05002237 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002238 )) >>
2239 ({
David Tolnay41871922017-12-29 01:53:45 -05002240 let ((front, dotdot, back), parens) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002241 let (dotdot, trailing) = match dotdot {
2242 Some((a, b)) => (Some(a), Some(b)),
2243 None => (None, None),
2244 };
2245 PatTuple {
2246 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002247 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002248 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002249 comma_token: trailing.unwrap_or_default(),
2250 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002251 }
2252 })
2253 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002254 }
David Tolnayfbb73232016-10-03 01:00:06 -07002255
Michael Layzell734adb42017-06-07 16:58:31 -04002256 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002257 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002258 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002259 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002260 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002261 pat: syn!(Pat) >>
2262 (PatRef {
2263 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002264 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002265 and_token: and,
2266 })
2267 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002268 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002269
Michael Layzell734adb42017-06-07 16:58:31 -04002270 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002271 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002272 named!(parse -> Self, do_parse!(
2273 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002274 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002275 return parse_error(); // these need to be parsed by pat_path
2276 } else {
2277 PatLit {
2278 expr: Box::new(lit),
2279 }
2280 })
2281 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002282 }
David Tolnaye1310902016-10-29 23:40:00 -07002283
Michael Layzell734adb42017-06-07 16:58:31 -04002284 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002285 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002286 named!(parse -> Self, do_parse!(
2287 lo: pat_lit_expr >>
2288 limits: syn!(RangeLimits) >>
2289 hi: pat_lit_expr >>
2290 (PatRange {
2291 lo: Box::new(lo),
2292 hi: Box::new(hi),
2293 limits: limits,
2294 })
2295 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002296 }
David Tolnaye1310902016-10-29 23:40:00 -07002297
Michael Layzell734adb42017-06-07 16:58:31 -04002298 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002299 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002300 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002301 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002302 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002303 |
David Tolnay8c91b882017-12-28 23:04:32 -05002304 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002305 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002306 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002307 Expr::Unary(ExprUnary {
2308 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002309 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002310 expr: Box::new(v.into())
2311 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002312 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002313 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002314 })
2315 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002316
Michael Layzell734adb42017-06-07 16:58:31 -04002317 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002318 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002319 named!(parse -> Self, map!(
2320 brackets!(do_parse!(
2321 before: call!(Delimited::parse_terminated) >>
2322 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002323 dots: punct!(..) >>
2324 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002325 (dots, trailing)
2326 )) >>
2327 after: cond!(
2328 match middle {
2329 Some((_, ref trailing)) => trailing.is_some(),
2330 _ => false,
2331 },
2332 call!(Delimited::parse_terminated)
2333 ) >>
2334 (before, middle, after)
2335 )),
2336 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002337 let mut before: Delimited<Pat, Token![,]> = before;
2338 let after: Option<Delimited<Pat, Token![,]>> = after;
2339 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002340 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002341 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002342 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002343 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002344 }),
2345 bracket_token: brackets,
2346 middle: middle.and_then(|_| {
2347 if !before.is_empty() && !before.trailing_delim() {
2348 Some(Box::new(before.pop().unwrap().into_item()))
2349 } else {
2350 None
2351 }
2352 }),
2353 front: before,
2354 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002355 }
Alex Crichton954046c2017-05-30 21:49:42 -07002356 }
Michael Layzell92639a52017-06-01 00:07:44 -04002357 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002358 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002359}
2360
David Tolnayf4bbbd92016-09-23 14:41:55 -07002361#[cfg(feature = "printing")]
2362mod printing {
2363 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002364 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002365 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002366 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002367 #[cfg(feature = "full")]
2368 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002369
David Tolnaybcf26022017-12-25 22:10:52 -05002370 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2371 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002372 #[cfg(feature = "full")]
2373 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002374 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002375 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002376 e.to_tokens(tokens);
2377 });
2378 } else {
2379 e.to_tokens(tokens);
2380 }
2381 }
2382
David Tolnay8c91b882017-12-28 23:04:32 -05002383 #[cfg(feature = "full")]
2384 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2385 tokens.append_all(attrs.outer());
2386 }
Michael Layzell734adb42017-06-07 16:58:31 -04002387
David Tolnay8c91b882017-12-28 23:04:32 -05002388 #[cfg(not(feature = "full"))]
2389 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002390 }
2391
Michael Layzell734adb42017-06-07 16:58:31 -04002392 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002393 impl ToTokens for ExprBox {
2394 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002395 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002396 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002397 self.expr.to_tokens(tokens);
2398 }
2399 }
2400
Michael Layzell734adb42017-06-07 16:58:31 -04002401 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002402 impl ToTokens for ExprInPlace {
2403 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002404 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002405 self.place.to_tokens(tokens);
2406 self.arrow_token.to_tokens(tokens);
2407 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002408 }
2409 }
2410
Michael Layzell734adb42017-06-07 16:58:31 -04002411 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002412 impl ToTokens for ExprArray {
2413 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002414 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002415 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002416 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002417 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002418 }
2419 }
2420
2421 impl ToTokens for ExprCall {
2422 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002423 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002424 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002425 self.paren_token.surround(tokens, |tokens| {
2426 self.args.to_tokens(tokens);
2427 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002428 }
2429 }
2430
Michael Layzell734adb42017-06-07 16:58:31 -04002431 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002432 impl ToTokens for ExprMethodCall {
2433 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002434 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002435 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002436 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002437 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002438 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002439 self.paren_token.surround(tokens, |tokens| {
2440 self.args.to_tokens(tokens);
2441 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002442 }
2443 }
2444
Michael Layzell734adb42017-06-07 16:58:31 -04002445 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002446 impl ToTokens for MethodTurbofish {
2447 fn to_tokens(&self, tokens: &mut Tokens) {
2448 self.colon2_token.to_tokens(tokens);
2449 self.lt_token.to_tokens(tokens);
2450 self.args.to_tokens(tokens);
2451 self.gt_token.to_tokens(tokens);
2452 }
2453 }
2454
2455 #[cfg(feature = "full")]
2456 impl ToTokens for GenericMethodArgument {
2457 fn to_tokens(&self, tokens: &mut Tokens) {
2458 match *self {
2459 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2460 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2461 }
2462 }
2463 }
2464
2465 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002466 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002467 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002468 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002469 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002470 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002471 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002472 // distinguish ExprTuple from ExprParen.
David Tolnay2a86fdd2017-12-28 23:34:28 -05002473 if self.elems.len() == 1 && !self.elems.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002474 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002475 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002476 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002477 }
2478 }
2479
2480 impl ToTokens for ExprBinary {
2481 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002482 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002483 self.left.to_tokens(tokens);
2484 self.op.to_tokens(tokens);
2485 self.right.to_tokens(tokens);
2486 }
2487 }
2488
2489 impl ToTokens for ExprUnary {
2490 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002491 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002492 self.op.to_tokens(tokens);
2493 self.expr.to_tokens(tokens);
2494 }
2495 }
2496
David Tolnay8c91b882017-12-28 23:04:32 -05002497 impl ToTokens for ExprLit {
2498 fn to_tokens(&self, tokens: &mut Tokens) {
2499 attrs_to_tokens(&self.attrs, tokens);
2500 self.lit.to_tokens(tokens);
2501 }
2502 }
2503
Alex Crichton62a0a592017-05-22 13:58:53 -07002504 impl ToTokens for ExprCast {
2505 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002506 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002507 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002508 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002509 self.ty.to_tokens(tokens);
2510 }
2511 }
2512
David Tolnay0cf94f22017-12-28 23:46:26 -05002513 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002514 impl ToTokens for ExprType {
2515 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002516 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002517 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002518 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002519 self.ty.to_tokens(tokens);
2520 }
2521 }
2522
Michael Layzell734adb42017-06-07 16:58:31 -04002523 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002524 fn maybe_wrap_else(
2525 tokens: &mut Tokens,
David Tolnay2ccf32a2017-12-29 00:34:26 -05002526 else_: &Option<(Token![else], Box<Expr>)>,
David Tolnay51382052017-12-27 13:46:21 -05002527 ) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002528 if let Some((ref else_token, ref else_)) = *else_ {
2529 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002530
2531 // If we are not one of the valid expressions to exist in an else
2532 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002533 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002534 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002535 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002536 }
2537 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002538 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002539 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002540 });
2541 }
2542 }
2543 }
2544 }
2545
2546 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002547 impl ToTokens for ExprIf {
2548 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002549 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002550 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002551 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002552 self.then_branch.to_tokens(tokens);
2553 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002554 }
2555 }
2556
Michael Layzell734adb42017-06-07 16:58:31 -04002557 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002558 impl ToTokens for ExprIfLet {
2559 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002560 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002561 self.if_token.to_tokens(tokens);
2562 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002563 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002564 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002565 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002566 self.then_branch.to_tokens(tokens);
2567 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002568 }
2569 }
2570
Michael Layzell734adb42017-06-07 16:58:31 -04002571 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002572 impl ToTokens for ExprWhile {
2573 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002574 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002575 if self.label.is_some() {
2576 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002577 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002578 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002579 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002580 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002581 self.body.to_tokens(tokens);
2582 }
2583 }
2584
Michael Layzell734adb42017-06-07 16:58:31 -04002585 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002586 impl ToTokens for ExprWhileLet {
2587 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002588 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002589 if self.label.is_some() {
2590 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002591 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002592 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002593 self.while_token.to_tokens(tokens);
2594 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002595 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002596 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002597 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002598 self.body.to_tokens(tokens);
2599 }
2600 }
2601
Michael Layzell734adb42017-06-07 16:58:31 -04002602 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002603 impl ToTokens for ExprForLoop {
2604 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002605 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002606 if self.label.is_some() {
2607 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002608 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002609 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002610 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002611 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002612 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002613 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002614 self.body.to_tokens(tokens);
2615 }
2616 }
2617
Michael Layzell734adb42017-06-07 16:58:31 -04002618 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002619 impl ToTokens for ExprLoop {
2620 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002621 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002622 if self.label.is_some() {
2623 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002624 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002625 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002626 self.loop_token.to_tokens(tokens);
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 ExprMatch {
2633 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002634 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002635 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002636 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002637 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002638 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002639 arm.to_tokens(tokens);
2640 // Ensure that we have a comma after a non-block arm, except
2641 // for the last one.
2642 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002643 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002644 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002645 }
2646 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002647 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002648 }
2649 }
2650
Michael Layzell734adb42017-06-07 16:58:31 -04002651 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002652 impl ToTokens for ExprCatch {
2653 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002654 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002655 self.do_token.to_tokens(tokens);
2656 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002657 self.block.to_tokens(tokens);
2658 }
2659 }
2660
Michael Layzell734adb42017-06-07 16:58:31 -04002661 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002662 impl ToTokens for ExprYield {
2663 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002664 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002665 self.yield_token.to_tokens(tokens);
2666 self.expr.to_tokens(tokens);
2667 }
2668 }
2669
2670 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002671 impl ToTokens for ExprClosure {
2672 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002673 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002674 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002675 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002676 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002677 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002678 FnArg::Captured(ArgCaptured {
2679 ref pat,
2680 ty: Type::Infer(_),
2681 ..
2682 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002683 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002684 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002685 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002686 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002687 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002688 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002689 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002690 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002691 self.body.to_tokens(tokens);
2692 }
2693 }
2694
Michael Layzell734adb42017-06-07 16:58:31 -04002695 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002696 impl ToTokens for ExprUnsafe {
2697 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002698 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002699 self.unsafe_token.to_tokens(tokens);
2700 self.block.to_tokens(tokens);
2701 }
2702 }
2703
2704 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002705 impl ToTokens for ExprBlock {
2706 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002707 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002708 self.block.to_tokens(tokens);
2709 }
2710 }
2711
Michael Layzell734adb42017-06-07 16:58:31 -04002712 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002713 impl ToTokens for ExprAssign {
2714 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002715 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002716 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002717 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002718 self.right.to_tokens(tokens);
2719 }
2720 }
2721
Michael Layzell734adb42017-06-07 16:58:31 -04002722 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002723 impl ToTokens for ExprAssignOp {
2724 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002725 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002726 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002727 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002728 self.right.to_tokens(tokens);
2729 }
2730 }
2731
Michael Layzell734adb42017-06-07 16:58:31 -04002732 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002733 impl ToTokens for ExprField {
2734 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002735 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002736 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002737 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002738 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002739 }
2740 }
2741
Michael Layzell734adb42017-06-07 16:58:31 -04002742 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002743 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002744 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002745 match *self {
2746 Member::Named(ident) => ident.to_tokens(tokens),
2747 Member::Unnamed(ref index) => index.to_tokens(tokens),
2748 }
2749 }
2750 }
2751
2752 #[cfg(feature = "full")]
2753 impl ToTokens for Index {
2754 fn to_tokens(&self, tokens: &mut Tokens) {
2755 tokens.append(TokenTree {
2756 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002757 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002758 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002759 }
2760 }
2761
2762 impl ToTokens for ExprIndex {
2763 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002764 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002765 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002766 self.bracket_token.surround(tokens, |tokens| {
2767 self.index.to_tokens(tokens);
2768 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 }
2770 }
2771
Michael Layzell734adb42017-06-07 16:58:31 -04002772 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002773 impl ToTokens for ExprRange {
2774 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002775 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002776 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002777 match self.limits {
2778 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2779 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2780 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002781 self.to.to_tokens(tokens);
2782 }
2783 }
2784
2785 impl ToTokens for ExprPath {
2786 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002787 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002788 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002789 }
2790 }
2791
Michael Layzell734adb42017-06-07 16:58:31 -04002792 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002793 impl ToTokens for ExprAddrOf {
2794 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002795 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002796 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002797 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002798 self.expr.to_tokens(tokens);
2799 }
2800 }
2801
Michael Layzell734adb42017-06-07 16:58:31 -04002802 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002803 impl ToTokens for ExprBreak {
2804 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002805 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002806 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002807 self.label.to_tokens(tokens);
2808 self.expr.to_tokens(tokens);
2809 }
2810 }
2811
Michael Layzell734adb42017-06-07 16:58:31 -04002812 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002813 impl ToTokens for ExprContinue {
2814 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002815 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002816 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002817 self.label.to_tokens(tokens);
2818 }
2819 }
2820
Michael Layzell734adb42017-06-07 16:58:31 -04002821 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002822 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002823 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002824 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002825 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002826 self.expr.to_tokens(tokens);
2827 }
2828 }
2829
Michael Layzell734adb42017-06-07 16:58:31 -04002830 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002831 impl ToTokens for ExprMacro {
2832 fn to_tokens(&self, tokens: &mut Tokens) {
2833 tokens.append_all(self.attrs.outer());
2834 self.mac.to_tokens(tokens);
2835 }
2836 }
2837
2838 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002839 impl ToTokens for ExprStruct {
2840 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002841 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002842 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002843 self.brace_token.surround(tokens, |tokens| {
2844 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002845 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002846 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002847 self.rest.to_tokens(tokens);
2848 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002849 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002850 }
2851 }
2852
Michael Layzell734adb42017-06-07 16:58:31 -04002853 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002854 impl ToTokens for ExprRepeat {
2855 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002856 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002857 self.bracket_token.surround(tokens, |tokens| {
2858 self.expr.to_tokens(tokens);
2859 self.semi_token.to_tokens(tokens);
2860 self.amt.to_tokens(tokens);
2861 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002862 }
2863 }
2864
David Tolnaye98775f2017-12-28 23:17:00 -05002865 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002866 impl ToTokens for ExprGroup {
2867 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002868 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002869 self.group_token.surround(tokens, |tokens| {
2870 self.expr.to_tokens(tokens);
2871 });
2872 }
2873 }
2874
David Tolnaye98775f2017-12-28 23:17:00 -05002875 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002876 impl ToTokens for ExprParen {
2877 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002878 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002879 self.paren_token.surround(tokens, |tokens| {
2880 self.expr.to_tokens(tokens);
2881 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002882 }
2883 }
2884
Michael Layzell734adb42017-06-07 16:58:31 -04002885 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002886 impl ToTokens for ExprTry {
2887 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002888 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002889 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002890 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002891 }
2892 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002893
Michael Layzell734adb42017-06-07 16:58:31 -04002894 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002895 impl ToTokens for FieldValue {
2896 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002897 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05002898 if let Some(ref colon_token) = self.colon_token {
2899 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002900 self.expr.to_tokens(tokens);
2901 }
David Tolnay055a7042016-10-02 19:23:54 -07002902 }
2903 }
2904
Michael Layzell734adb42017-06-07 16:58:31 -04002905 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002906 impl ToTokens for Arm {
2907 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002908 tokens.append_all(&self.attrs);
2909 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002910 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002911 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002912 self.guard.to_tokens(tokens);
2913 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002914 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002915 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002916 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002917 }
2918 }
2919
Michael Layzell734adb42017-06-07 16:58:31 -04002920 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002921 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002922 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002923 self.underscore_token.to_tokens(tokens);
2924 }
2925 }
2926
Michael Layzell734adb42017-06-07 16:58:31 -04002927 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002928 impl ToTokens for PatIdent {
2929 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05002930 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05002931 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002932 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002933 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002934 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002935 self.subpat.to_tokens(tokens);
2936 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002937 }
2938 }
2939
Michael Layzell734adb42017-06-07 16:58:31 -04002940 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002941 impl ToTokens for PatStruct {
2942 fn to_tokens(&self, tokens: &mut Tokens) {
2943 self.path.to_tokens(tokens);
2944 self.brace_token.surround(tokens, |tokens| {
2945 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002946 // NOTE: We need a comma before the dot2 token if it is present.
2947 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002948 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002949 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002950 self.dot2_token.to_tokens(tokens);
2951 });
2952 }
2953 }
2954
Michael Layzell734adb42017-06-07 16:58:31 -04002955 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002956 impl ToTokens for PatTupleStruct {
2957 fn to_tokens(&self, tokens: &mut Tokens) {
2958 self.path.to_tokens(tokens);
2959 self.pat.to_tokens(tokens);
2960 }
2961 }
2962
Michael Layzell734adb42017-06-07 16:58:31 -04002963 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002964 impl ToTokens for PatPath {
2965 fn to_tokens(&self, tokens: &mut Tokens) {
2966 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2967 }
2968 }
2969
Michael Layzell734adb42017-06-07 16:58:31 -04002970 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002971 impl ToTokens for PatTuple {
2972 fn to_tokens(&self, tokens: &mut Tokens) {
2973 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05002974 self.front.to_tokens(tokens);
2975 if let Some(ref dot2_token) = self.dot2_token {
2976 if !self.front.empty_or_trailing() {
2977 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002978 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002979 }
David Tolnay41871922017-12-29 01:53:45 -05002980 dot2_token.to_tokens(tokens);
2981 self.comma_token.to_tokens(tokens);
2982 if self.comma_token.is_none() && !self.back.is_empty() {
2983 // Ensure there is a comma after the .. token.
2984 <Token![,]>::default().to_tokens(tokens);
2985 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002986 }
David Tolnay41871922017-12-29 01:53:45 -05002987 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002988 });
2989 }
2990 }
2991
Michael Layzell734adb42017-06-07 16:58:31 -04002992 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002993 impl ToTokens for PatBox {
2994 fn to_tokens(&self, tokens: &mut Tokens) {
2995 self.box_token.to_tokens(tokens);
2996 self.pat.to_tokens(tokens);
2997 }
2998 }
2999
Michael Layzell734adb42017-06-07 16:58:31 -04003000 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003001 impl ToTokens for PatRef {
3002 fn to_tokens(&self, tokens: &mut Tokens) {
3003 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003004 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003005 self.pat.to_tokens(tokens);
3006 }
3007 }
3008
Michael Layzell734adb42017-06-07 16:58:31 -04003009 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003010 impl ToTokens for PatLit {
3011 fn to_tokens(&self, tokens: &mut Tokens) {
3012 self.expr.to_tokens(tokens);
3013 }
3014 }
3015
Michael Layzell734adb42017-06-07 16:58:31 -04003016 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003017 impl ToTokens for PatRange {
3018 fn to_tokens(&self, tokens: &mut Tokens) {
3019 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003020 match self.limits {
3021 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3022 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3023 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003024 self.hi.to_tokens(tokens);
3025 }
3026 }
3027
Michael Layzell734adb42017-06-07 16:58:31 -04003028 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003029 impl ToTokens for PatSlice {
3030 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003031 // XXX: This is a mess, and it will be so easy to screw it up. How
3032 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003033 self.bracket_token.surround(tokens, |tokens| {
3034 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003035
3036 // If we need a comma before the middle or standalone .. token,
3037 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003038 if !self.front.empty_or_trailing()
3039 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003040 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003041 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003042 }
3043
3044 // If we have an identifier, we always need a .. token.
3045 if self.middle.is_some() {
3046 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003047 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003048 } else if self.dot2_token.is_some() {
3049 self.dot2_token.to_tokens(tokens);
3050 }
3051
3052 // Make sure we have a comma before the back half.
3053 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003054 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003055 self.back.to_tokens(tokens);
3056 } else {
3057 self.comma_token.to_tokens(tokens);
3058 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003059 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003060 }
3061 }
3062
Michael Layzell734adb42017-06-07 16:58:31 -04003063 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003064 impl ToTokens for FieldPat {
3065 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003066 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003067 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003068 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003069 }
3070 self.pat.to_tokens(tokens);
3071 }
3072 }
3073
Michael Layzell734adb42017-06-07 16:58:31 -04003074 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003075 impl ToTokens for Block {
3076 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003077 self.brace_token.surround(tokens, |tokens| {
3078 tokens.append_all(&self.stmts);
3079 });
David Tolnay42602292016-10-01 22:25:45 -07003080 }
3081 }
3082
Michael Layzell734adb42017-06-07 16:58:31 -04003083 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003084 impl ToTokens for Stmt {
3085 fn to_tokens(&self, tokens: &mut Tokens) {
3086 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003087 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003088 Stmt::Item(ref item) => item.to_tokens(tokens),
3089 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003090 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003091 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003092 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003093 }
David Tolnay42602292016-10-01 22:25:45 -07003094 }
3095 }
3096 }
David Tolnay191e0582016-10-02 18:31:09 -07003097
Michael Layzell734adb42017-06-07 16:58:31 -04003098 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003099 impl ToTokens for Local {
3100 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003101 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003102 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003103 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003104 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003105 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003106 self.ty.to_tokens(tokens);
3107 }
3108 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003109 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003110 self.init.to_tokens(tokens);
3111 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003112 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003113 }
3114 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003115}