blob: 55c2e4f44865e689b6e23653947ab7557752868c [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay2ae520a2017-12-29 11:19:50 -05003use proc_macro2::{Span, TokenStream};
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 Tolnay2ae520a2017-12-29 11:19:50 -05006#[cfg(feature = "extra-traits")]
7use mac::TokenStreamHelper;
8#[cfg(feature = "full")]
9use std::mem;
David Tolnayf4bbbd92016-09-23 14:41:55 -070010
Alex Crichton62a0a592017-05-22 13:58:53 -070011ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -050012 /// An expression.
13 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040015 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050016 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080017 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050018 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070019 }),
Clar Charrd22b5702017-03-10 15:24:56 -050020
David Tolnay8701a5c2017-12-28 23:31:10 -050021 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040022 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050023 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050025 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub value: Box<Expr>,
27 }),
Clar Charrd22b5702017-03-10 15:24:56 -050028
Alex Crichton62a0a592017-05-22 13:58:53 -070029 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040030 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050031 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050032 pub bracket_token: token::Bracket,
David Tolnay2a86fdd2017-12-28 23:34:28 -050033 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
Clar Charrd22b5702017-03-10 15:24:56 -050035
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// A function call.
37 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050038 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050040 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050041 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
Clar Charrd22b5702017-03-10 15:24:56 -050043
Alex Crichton62a0a592017-05-22 13:58:53 -070044 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
45 ///
46 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080047 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070048 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040049 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050050 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -050051 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050053 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -050054 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050055 pub paren_token: token::Paren,
56 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
Clar Charrd22b5702017-03-10 15:24:56 -050058
Alex Crichton62a0a592017-05-22 13:58:53 -070059 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050060 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050061 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050062 pub paren_token: token::Paren,
David Tolnay2a86fdd2017-12-28 23:34:28 -050063 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }),
Clar Charrd22b5702017-03-10 15:24:56 -050065
Alex Crichton62a0a592017-05-22 13:58:53 -070066 /// A binary operation, e.g. `a + b`, `a * b`.
67 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050070 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub right: Box<Expr>,
72 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A unary operation, e.g. `!x`, `*x`.
75 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub op: UnOp,
78 pub expr: Box<Expr>,
79 }),
Clar Charrd22b5702017-03-10 15:24:56 -050080
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050082 pub Lit(ExprLit {
83 pub attrs: Vec<Attribute>,
84 pub lit: Lit,
85 }),
Clar Charrd22b5702017-03-10 15:24:56 -050086
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A cast, e.g. `foo as f64`.
88 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050089 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A type ascription, e.g. `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -050096 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -050097 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800100 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500102
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 /// An `if` block, with an optional else block
104 ///
105 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400106 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500110 pub then_branch: Block,
111 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500113
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 /// An `if let` expression with an optional else block
115 ///
116 /// E.g., `if let pat = expr { block } else { expr }`
117 ///
118 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800121 pub if_token: Token![if],
122 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500123 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800124 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500125 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500126 pub then_branch: Block,
127 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500129
Alex Crichton62a0a592017-05-22 13:58:53 -0700130 /// A while loop, with an optional label
131 ///
132 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400133 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500134 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700135 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub colon_token: Option<Token![:]>,
137 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500138 pub cond: Box<Expr>,
139 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500141
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 /// A while-let loop, with an optional label.
143 ///
144 /// E.g., `'label: while let pat = expr { block }`
145 ///
146 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400147 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500148 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700149 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800150 pub colon_token: Option<Token![:]>,
151 pub while_token: Token![while],
152 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800154 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub expr: Box<Expr>,
156 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500158
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 /// A for loop, with an optional label.
160 ///
161 /// E.g., `'label: for pat in expr { block }`
162 ///
163 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400164 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500165 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500166 pub label: Option<Lifetime>,
167 pub colon_token: Option<Token![:]>,
168 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700169 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500170 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 pub expr: Box<Expr>,
172 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500174
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 /// Conditionless loop with an optional label.
176 ///
177 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400178 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500179 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700180 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800181 pub colon_token: Option<Token![:]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500182 pub loop_token: Token![loop],
183 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700184 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500185
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400187 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500188 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800189 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500191 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 pub arms: Vec<Arm>,
193 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500194
Alex Crichton62a0a592017-05-22 13:58:53 -0700195 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400196 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500197 pub attrs: Vec<Attribute>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500198 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800199 pub or1_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500200 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500202 pub output: ReturnType,
203 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700204 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500205
Nika Layzell640832a2017-12-04 13:37:09 -0500206 /// An unsafe block (`unsafe { ... }`)
207 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500208 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500209 pub unsafe_token: Token![unsafe],
210 pub block: Block,
211 }),
212
213 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400214 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500215 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700216 pub block: Block,
217 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700218
Alex Crichton62a0a592017-05-22 13:58:53 -0700219 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400220 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500221 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700222 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800223 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500224 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700225 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500226
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 /// An assignment with an operator
228 ///
229 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400230 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500231 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500233 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 pub right: Box<Expr>,
235 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500236
David Tolnay85b69a42017-12-27 20:43:10 -0500237 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
238 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400239 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500240 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500241 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800242 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500243 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700244 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500245
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 /// An indexing operation (`foo[2]`)
247 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500248 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700249 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500250 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500251 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700252 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500253
David Tolnaybe55d7b2017-12-17 23:41:20 -0800254 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400255 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500256 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700257 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500259 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700260 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700261
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 /// Variable reference, possibly containing `::` and/or type
263 /// parameters, e.g. foo::bar::<baz>.
264 ///
265 /// Optionally "qualified",
266 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
267 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500268 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 pub qself: Option<QSelf>,
270 pub path: Path,
271 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700272
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400274 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500275 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800276 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500277 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700278 pub expr: Box<Expr>,
279 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500280
Alex Crichton62a0a592017-05-22 13:58:53 -0700281 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400282 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500283 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500284 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700285 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700286 pub expr: Option<Box<Expr>>,
287 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500288
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400290 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500291 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800292 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500293 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700294 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500295
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500297 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500298 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800299 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500300 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700301 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700302
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500304 pub Macro(ExprMacro #full {
305 pub attrs: Vec<Attribute>,
306 pub mac: Macro,
307 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700308
Alex Crichton62a0a592017-05-22 13:58:53 -0700309 /// A struct literal expression.
310 ///
311 /// For example, `Foo {x: 1, y: 2}`, or
312 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400313 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500314 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700315 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500316 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500317 pub fields: Delimited<FieldValue, Token![,]>,
318 pub dot2_token: Option<Token![..]>,
319 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700320 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700321
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 /// An array literal constructed from one repeated element.
323 ///
324 /// For example, `[1; 5]`. The first expression is the element
325 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400326 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500327 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500328 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700329 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500330 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 pub amt: Box<Expr>,
332 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700333
Alex Crichton62a0a592017-05-22 13:58:53 -0700334 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500335 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500336 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500337 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500338 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700339 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700340
Michael Layzell93c36282017-06-04 20:43:14 -0400341 /// No-op: used solely so we can pretty-print faithfully
342 ///
343 /// A `group` represents a `None`-delimited span in the input
344 /// `TokenStream` which affects the precidence of the resulting
345 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500346 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500347 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500348 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500349 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400350 }),
351
Alex Crichton62a0a592017-05-22 13:58:53 -0700352 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400353 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500354 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800356 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700357 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700358
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 /// A catch expression.
360 ///
361 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400362 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500363 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800364 pub do_token: Token![do],
365 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700366 pub block: Block,
367 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700368
369 /// A yield expression.
370 ///
371 /// E.g. `yield expr`
372 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500373 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800374 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700375 pub expr: Option<Box<Expr>>,
376 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500377
378 pub Verbatim(ExprVerbatim #manual_extra_traits {
379 pub tts: TokenStream,
380 }),
381 }
382}
383
384#[cfg(feature = "extra-traits")]
385impl Eq for ExprVerbatim {}
386
387#[cfg(feature = "extra-traits")]
388impl PartialEq for ExprVerbatim {
389 fn eq(&self, other: &Self) -> bool {
390 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
391 }
392}
393
394#[cfg(feature = "extra-traits")]
395impl Hash for ExprVerbatim {
396 fn hash<H>(&self, state: &mut H)
397 where
398 H: Hasher,
399 {
400 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700401 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700402}
403
David Tolnay8c91b882017-12-28 23:04:32 -0500404impl Expr {
405 // Not public API.
406 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500407 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500408 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500409 match *self {
410 Expr::Box(ExprBox { ref mut attrs, .. }) |
411 Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
412 Expr::Array(ExprArray { ref mut attrs, .. }) |
413 Expr::Call(ExprCall { ref mut attrs, .. }) |
414 Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
415 Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
416 Expr::Binary(ExprBinary { ref mut attrs, .. }) |
417 Expr::Unary(ExprUnary { ref mut attrs, .. }) |
418 Expr::Lit(ExprLit { ref mut attrs, .. }) |
419 Expr::Cast(ExprCast { ref mut attrs, .. }) |
420 Expr::Type(ExprType { ref mut attrs, .. }) |
421 Expr::If(ExprIf { ref mut attrs, .. }) |
422 Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
423 Expr::While(ExprWhile { ref mut attrs, .. }) |
424 Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
425 Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
426 Expr::Loop(ExprLoop { ref mut attrs, .. }) |
427 Expr::Match(ExprMatch { ref mut attrs, .. }) |
428 Expr::Closure(ExprClosure { ref mut attrs, .. }) |
429 Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
430 Expr::Block(ExprBlock { ref mut attrs, .. }) |
431 Expr::Assign(ExprAssign { ref mut attrs, .. }) |
432 Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
433 Expr::Field(ExprField { ref mut attrs, .. }) |
434 Expr::Index(ExprIndex { ref mut attrs, .. }) |
435 Expr::Range(ExprRange { ref mut attrs, .. }) |
436 Expr::Path(ExprPath { ref mut attrs, .. }) |
437 Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
438 Expr::Break(ExprBreak { ref mut attrs, .. }) |
439 Expr::Continue(ExprContinue { ref mut attrs, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500440 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500441 Expr::Macro(ExprMacro { ref mut attrs, .. }) |
442 Expr::Struct(ExprStruct { ref mut attrs, .. }) |
443 Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
444 Expr::Paren(ExprParen { ref mut attrs, .. }) |
445 Expr::Group(ExprGroup { ref mut attrs, .. }) |
446 Expr::Try(ExprTry { ref mut attrs, .. }) |
447 Expr::Catch(ExprCatch { ref mut attrs, .. }) |
David Tolnay2ae520a2017-12-29 11:19:50 -0500448 Expr::Yield(ExprYield { ref mut attrs, .. }) => {
449 mem::replace(attrs, new)
450 }
451 Expr::Verbatim(_) => {
452 // TODO
453 Vec::new()
454 }
David Tolnay8c91b882017-12-28 23:04:32 -0500455 }
456 }
457}
458
David Tolnay85b69a42017-12-27 20:43:10 -0500459ast_enum! {
460 /// A struct or tuple struct field accessed in a struct literal or field
461 /// expression.
462 pub enum Member {
463 /// A named field like `self.x`.
464 Named(Ident),
465 /// An unnamed field like `self.0`.
466 Unnamed(Index),
467 }
468}
469
David Tolnay85b69a42017-12-27 20:43:10 -0500470ast_struct! {
471 /// The index of an unnamed tuple struct field.
472 pub struct Index #manual_extra_traits {
473 pub index: u32,
474 pub span: Span,
475 }
476}
477
David Tolnay14982012017-12-29 00:49:51 -0500478impl From<usize> for Index {
479 fn from(index: usize) -> Index {
480 assert!(index < std::u32::MAX as usize);
481 Index {
482 index: index as u32,
483 span: Span::default(),
484 }
485 }
486}
487
488#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500489impl Eq for Index {}
490
David Tolnay14982012017-12-29 00:49:51 -0500491#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500492impl PartialEq for Index {
493 fn eq(&self, other: &Self) -> bool {
494 self.index == other.index
495 }
496}
497
David Tolnay14982012017-12-29 00:49:51 -0500498#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500499impl Hash for Index {
500 fn hash<H: Hasher>(&self, state: &mut H) {
501 self.index.hash(state);
502 }
503}
504
505#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700506ast_struct! {
David Tolnayd60cfec2017-12-29 00:21:38 -0500507 pub struct MethodTurbofish {
508 pub colon2_token: Token![::],
509 pub lt_token: Token![<],
510 pub args: Delimited<GenericMethodArgument, Token![,]>,
511 pub gt_token: Token![>],
512 }
513}
514
515#[cfg(feature = "full")]
516ast_enum! {
517 /// A individual generic argument like `T`.
518 pub enum GenericMethodArgument {
519 /// The type parameters for this path segment, if present.
520 Type(Type),
521 /// Const expression. Must be inside of a block.
522 ///
523 /// NOTE: Identity expressions are represented as Type arguments, as
524 /// they are indistinguishable syntactically.
525 Const(Expr),
526 }
527}
528
529#[cfg(feature = "full")]
530ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700531 /// A field-value pair in a struct literal.
532 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500533 /// Attributes tagged on the field.
534 pub attrs: Vec<Attribute>,
535
536 /// Name or index of the field.
537 pub member: Member,
538
David Tolnay5d7098a2017-12-29 01:35:24 -0500539 /// The colon in `Struct { x: x }`. If written in shorthand like
540 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500541 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500542
Alex Crichton62a0a592017-05-22 13:58:53 -0700543 /// Value of the field.
544 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700545 }
David Tolnay055a7042016-10-02 19:23:54 -0700546}
547
Michael Layzell734adb42017-06-07 16:58:31 -0400548#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700549ast_struct! {
550 /// A Block (`{ .. }`).
551 ///
552 /// E.g. `{ .. }` as in `fn foo() { .. }`
553 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500554 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700555 /// Statements in a block
556 pub stmts: Vec<Stmt>,
557 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700558}
559
Michael Layzell734adb42017-06-07 16:58:31 -0400560#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700561ast_enum! {
562 /// A statement, usually ending in a semicolon.
563 pub enum Stmt {
564 /// A local (let) binding.
565 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700566
Alex Crichton62a0a592017-05-22 13:58:53 -0700567 /// An item definition.
568 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700569
Alex Crichton62a0a592017-05-22 13:58:53 -0700570 /// Expr without trailing semicolon.
571 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700572
Alex Crichton62a0a592017-05-22 13:58:53 -0700573 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800574 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700576}
577
Michael Layzell734adb42017-06-07 16:58:31 -0400578#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700579ast_struct! {
580 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
581 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500582 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800583 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500585 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800586 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500587 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 /// Initializer expression to set the value, if any
589 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500590 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700591 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700592}
593
Michael Layzell734adb42017-06-07 16:58:31 -0400594#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700596 // Clippy false positive
597 // https://github.com/Manishearth/rust-clippy/issues/1241
598 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
599 pub enum Pat {
600 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700601 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800602 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700604
Alex Crichton62a0a592017-05-22 13:58:53 -0700605 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
606 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
607 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
608 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700609 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500610 pub by_ref: Option<Token![ref]>,
611 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700612 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800613 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500614 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700615 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700616
Alex Crichton62a0a592017-05-22 13:58:53 -0700617 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
618 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700619 pub Struct(PatStruct {
620 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500621 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500622 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800623 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700624 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700625
Alex Crichton62a0a592017-05-22 13:58:53 -0700626 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
627 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
628 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700629 pub TupleStruct(PatTupleStruct {
630 pub path: Path,
631 pub pat: PatTuple,
632 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700633
Alex Crichton62a0a592017-05-22 13:58:53 -0700634 /// A possibly qualified path pattern.
635 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
636 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
637 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700638 pub Path(PatPath {
639 pub qself: Option<QSelf>,
640 pub path: Path,
641 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700642
Alex Crichton62a0a592017-05-22 13:58:53 -0700643 /// A tuple pattern `(a, b)`.
644 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
645 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700646 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500647 pub paren_token: token::Paren,
David Tolnay41871922017-12-29 01:53:45 -0500648 pub front: Delimited<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500649 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500650 pub comma_token: Option<Token![,]>,
651 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700652 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700653 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700654 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800655 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500656 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700657 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700658 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700659 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800660 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500661 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500662 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700663 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700664 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700665 pub Lit(PatLit {
666 pub expr: Box<Expr>,
667 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800668 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700669 pub Range(PatRange {
670 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700671 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500672 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700673 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400674 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700675 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500676 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800677 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700678 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500679 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500680 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500681 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700682 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700683 /// A macro pattern; pre-expansion
David Tolnay323279a2017-12-29 11:26:32 -0500684 pub Macro(PatMacro {
685 pub mac: Macro,
686 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500687 pub Verbatim(PatVerbatim #manual_extra_traits {
688 pub tts: TokenStream,
689 }),
690 }
691}
692
693#[cfg(feature = "extra-traits")]
694impl Eq for PatVerbatim {}
695
696#[cfg(feature = "extra-traits")]
697impl PartialEq for PatVerbatim {
698 fn eq(&self, other: &Self) -> bool {
699 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
700 }
701}
702
703#[cfg(feature = "extra-traits")]
704impl Hash for PatVerbatim {
705 fn hash<H>(&self, state: &mut H)
706 where
707 H: Hasher,
708 {
709 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700710 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700711}
712
Michael Layzell734adb42017-06-07 16:58:31 -0400713#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700714ast_struct! {
715 /// An arm of a 'match'.
716 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800717 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500719 /// ```rust
720 /// # #![feature(dotdoteq_in_patterns)]
721 /// #
722 /// # fn main() {
723 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700724 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500725 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700726 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500727 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700728 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500729 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700730 /// ```
731 pub struct Arm {
732 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800733 pub pats: Delimited<Pat, Token![|]>,
734 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800736 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700737 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800738 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700739 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700740}
741
Michael Layzell734adb42017-06-07 16:58:31 -0400742#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700743ast_enum! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700745 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 pub enum RangeLimits {
747 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800748 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800750 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700751 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700752}
753
Michael Layzell734adb42017-06-07 16:58:31 -0400754#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700755ast_struct! {
756 /// A single field in a struct pattern
757 ///
758 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay5d7098a2017-12-29 01:35:24 -0500759 /// are treated the same as `x: x, y: ref y, z: ref mut z` but
760 /// there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700761 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500762 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500764 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500765 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700766 /// The pattern the field is destructured to
767 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700768 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700769}
770
Michael Layzell3936ceb2017-07-08 00:28:36 -0400771#[cfg(any(feature = "parsing", feature = "printing"))]
772#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700773fn arm_expr_requires_comma(expr: &Expr) -> bool {
774 // see https://github.com/rust-lang/rust/blob/eb8f2586e
775 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500776 match *expr {
777 Expr::Unsafe(..)
778 | Expr::Block(..)
779 | Expr::If(..)
780 | Expr::IfLet(..)
781 | Expr::Match(..)
782 | Expr::While(..)
783 | Expr::WhileLet(..)
784 | Expr::Loop(..)
785 | Expr::ForLoop(..)
786 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700787 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400788 }
789}
790
David Tolnayb9c8e322016-09-23 20:48:37 -0700791#[cfg(feature = "parsing")]
792pub mod parsing {
793 use super::*;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500794 use ty::parsing::qpath;
795 #[cfg(feature = "full")]
796 use ty::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700797
Michael Layzell734adb42017-06-07 16:58:31 -0400798 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500799 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500800 use synom::Synom;
801 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400802 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500803 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500804 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805
David Tolnaybcf26022017-12-25 22:10:52 -0500806 // When we're parsing expressions which occur before blocks, like in an if
807 // statement's condition, we cannot parse a struct literal.
808 //
809 // Struct literals are ambiguous in certain positions
810 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700811 macro_rules! ambiguous_expr {
812 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700813 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700814 };
815 }
816
David Tolnaybcf26022017-12-25 22:10:52 -0500817 // When we are parsing an optional suffix expression, we cannot allow blocks
818 // if structs are not allowed.
819 //
820 // Example:
821 //
822 // if break {} {}
823 //
824 // is ambiguous between:
825 //
826 // if (break {}) {}
827 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400828 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400829 macro_rules! opt_ambiguous_expr {
830 ($i:expr, $allow_struct:ident) => {
831 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
832 };
833 }
834
Alex Crichton954046c2017-05-30 21:49:42 -0700835 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400836 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700837
838 fn description() -> Option<&'static str> {
839 Some("expression")
840 }
841 }
842
Michael Layzell734adb42017-06-07 16:58:31 -0400843 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700844 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
845
David Tolnaybcf26022017-12-25 22:10:52 -0500846 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400847 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500848 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500849 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400850 }
851
Michael Layzell734adb42017-06-07 16:58:31 -0400852 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500853 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500854 // NOTE: We intentionally skip assign_expr, placement_expr, and
855 // range_expr, as they are not parsed in non-full mode.
856 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400857 }
858
David Tolnaybcf26022017-12-25 22:10:52 -0500859 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400860 macro_rules! binop {
861 (
862 $name: ident,
863 $next: ident,
864 $submac: ident!( $($args:tt)* )
865 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500866 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400867 mut e: call!($next, allow_struct, allow_block) >>
868 many0!(do_parse!(
869 op: $submac!($($args)*) >>
870 rhs: call!($next, allow_struct, true) >>
871 ({
872 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500873 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400874 left: Box::new(e.into()),
875 op: op,
876 right: Box::new(rhs.into()),
877 }.into();
878 })
879 )) >>
880 (e)
881 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700882 }
David Tolnay54e854d2016-10-24 12:03:30 -0700883 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700884
David Tolnaybcf26022017-12-25 22:10:52 -0500885 // <placement> = <placement> ..
886 // <placement> += <placement> ..
887 // <placement> -= <placement> ..
888 // <placement> *= <placement> ..
889 // <placement> /= <placement> ..
890 // <placement> %= <placement> ..
891 // <placement> ^= <placement> ..
892 // <placement> &= <placement> ..
893 // <placement> |= <placement> ..
894 // <placement> <<= <placement> ..
895 // <placement> >>= <placement> ..
896 //
897 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400898 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500899 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400900 mut e: call!(placement_expr, allow_struct, allow_block) >>
901 alt!(
902 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400904 // Recurse into self to parse right-associative operator.
905 rhs: call!(assign_expr, allow_struct, true) >>
906 ({
907 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500908 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400909 left: Box::new(e.into()),
910 eq_token: eq,
911 right: Box::new(rhs.into()),
912 }.into();
913 })
914 )
915 |
916 do_parse!(
917 op: call!(BinOp::parse_assign_op) >>
918 // Recurse into self to parse right-associative operator.
919 rhs: call!(assign_expr, allow_struct, true) >>
920 ({
921 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500922 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400923 left: Box::new(e.into()),
924 op: op,
925 right: Box::new(rhs.into()),
926 }.into();
927 })
928 )
929 |
930 epsilon!()
931 ) >>
932 (e)
933 ));
934
David Tolnaybcf26022017-12-25 22:10:52 -0500935 // <range> <- <range> ..
936 //
937 // NOTE: The `in place { expr }` version of this syntax is parsed in
938 // `atom_expr`, not here.
939 //
940 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400941 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500942 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400943 mut e: call!(range_expr, allow_struct, allow_block) >>
944 alt!(
945 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800946 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400947 // Recurse into self to parse right-associative operator.
948 rhs: call!(placement_expr, allow_struct, true) >>
949 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400950 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500951 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952 // op: BinOp::Place(larrow),
953 place: Box::new(e.into()),
David Tolnay8701a5c2017-12-28 23:31:10 -0500954 arrow_token: arrow,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400955 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400956 }.into();
957 })
958 )
959 |
960 epsilon!()
961 ) >>
962 (e)
963 ));
964
David Tolnaybcf26022017-12-25 22:10:52 -0500965 // <or> ... <or> ..
966 // <or> .. <or> ..
967 // <or> ..
968 //
969 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
970 // rules are for parsing these expressions are, but this is not correct.
971 // For example, `a .. b .. c` is not a legal expression. It should not
972 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
973 //
974 // NOTE: The form of ranges which don't include a preceding expression are
975 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400976 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500977 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978 mut e: call!(or_expr, allow_struct, allow_block) >>
979 many0!(do_parse!(
980 limits: syn!(RangeLimits) >>
981 // We don't want to allow blocks here if we don't allow structs. See
982 // the reasoning for `opt_ambiguous_expr!` above.
983 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
984 ({
985 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500986 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400987 from: Some(Box::new(e.into())),
988 limits: limits,
989 to: hi.map(|e| Box::new(e.into())),
990 }.into();
991 })
992 )) >>
993 (e)
994 ));
995
David Tolnaybcf26022017-12-25 22:10:52 -0500996 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800997 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400998
David Tolnaybcf26022017-12-25 22:10:52 -0500999 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001000 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001001
David Tolnaybcf26022017-12-25 22:10:52 -05001002 // <bitor> == <bitor> ...
1003 // <bitor> != <bitor> ...
1004 // <bitor> >= <bitor> ...
1005 // <bitor> <= <bitor> ...
1006 // <bitor> > <bitor> ...
1007 // <bitor> < <bitor> ...
1008 //
1009 // NOTE: This operator appears to be parsed as left-associative, but errors
1010 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001011 binop!(
1012 compare_expr,
1013 bitor_expr,
1014 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001018 |
1019 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001020 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001021 |
1022 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001024 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001025 do_parse!(
1026 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001027 not!(punct!(<-)) >>
1028 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001029 (BinOp::Lt(t))
1030 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001031 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001033 )
1034 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001035
David Tolnaybcf26022017-12-25 22:10:52 -05001036 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001037 binop!(
1038 bitor_expr,
1039 bitxor_expr,
1040 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1041 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001044 binop!(
1045 bitxor_expr,
1046 bitand_expr,
1047 do_parse!(
1048 // NOTE: Make sure we aren't looking at ^=.
1049 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1050 )
1051 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001052
David Tolnaybcf26022017-12-25 22:10:52 -05001053 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001054 binop!(
1055 bitand_expr,
1056 shift_expr,
1057 do_parse!(
1058 // NOTE: Make sure we aren't looking at && or &=.
1059 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1060 )
1061 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001062
David Tolnaybcf26022017-12-25 22:10:52 -05001063 // <arith> << <arith> ...
1064 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001065 binop!(
1066 shift_expr,
1067 arith_expr,
1068 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001069 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001070 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001071 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001072 )
1073 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001074
David Tolnaybcf26022017-12-25 22:10:52 -05001075 // <term> + <term> ...
1076 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001077 binop!(
1078 arith_expr,
1079 term_expr,
1080 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001081 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001082 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001083 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001084 )
1085 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001086
David Tolnaybcf26022017-12-25 22:10:52 -05001087 // <cast> * <cast> ...
1088 // <cast> / <cast> ...
1089 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001090 binop!(
1091 term_expr,
1092 cast_expr,
1093 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001094 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001095 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001096 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001097 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001098 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001099 )
1100 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001101
David Tolnaybcf26022017-12-25 22:10:52 -05001102 // <unary> as <ty>
1103 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001104 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001105 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001106 mut e: call!(unary_expr, allow_struct, allow_block) >>
1107 many0!(alt!(
1108 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001109 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001110 // We can't accept `A + B` in cast expressions, as it's
1111 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001112 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001113 ({
1114 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001115 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001116 expr: Box::new(e.into()),
1117 as_token: as_,
1118 ty: Box::new(ty),
1119 }.into();
1120 })
1121 )
1122 |
1123 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001124 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001125 // We can't accept `A + B` in cast expressions, as it's
1126 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001127 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001128 ({
1129 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001130 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001131 expr: Box::new(e.into()),
1132 colon_token: colon,
1133 ty: Box::new(ty),
1134 }.into();
1135 })
1136 )
1137 )) >>
1138 (e)
1139 ));
1140
David Tolnay0cf94f22017-12-28 23:46:26 -05001141 // <unary> as <ty>
1142 #[cfg(not(feature = "full"))]
1143 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1144 mut e: call!(unary_expr, allow_struct, allow_block) >>
1145 many0!(do_parse!(
1146 as_: keyword!(as) >>
1147 // We can't accept `A + B` in cast expressions, as it's
1148 // ambiguous with the + expression.
1149 ty: call!(Type::without_plus) >>
1150 ({
1151 e = ExprCast {
1152 attrs: Vec::new(),
1153 expr: Box::new(e.into()),
1154 as_token: as_,
1155 ty: Box::new(ty),
1156 }.into();
1157 })
1158 )) >>
1159 (e)
1160 ));
1161
David Tolnaybcf26022017-12-25 22:10:52 -05001162 // <UnOp> <trailer>
1163 // & <trailer>
1164 // &mut <trailer>
1165 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001166 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001167 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 do_parse!(
1169 op: syn!(UnOp) >>
1170 expr: call!(unary_expr, allow_struct, true) >>
1171 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001172 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 op: op,
1174 expr: Box::new(expr.into()),
1175 }.into())
1176 )
1177 |
1178 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001180 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 expr: call!(unary_expr, allow_struct, true) >>
1182 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001183 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001185 mutability: mutability,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001186 expr: Box::new(expr.into()),
1187 }.into())
1188 )
1189 |
1190 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001191 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001192 expr: call!(unary_expr, allow_struct, true) >>
1193 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001194 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001195 box_token: box_,
1196 expr: Box::new(expr.into()),
1197 }.into())
1198 )
1199 |
1200 call!(trailer_expr, allow_struct, allow_block)
1201 ));
1202
Michael Layzell734adb42017-06-07 16:58:31 -04001203 // XXX: This duplication is ugly
1204 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001205 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001206 do_parse!(
1207 op: syn!(UnOp) >>
1208 expr: call!(unary_expr, allow_struct, true) >>
1209 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001210 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001211 op: op,
1212 expr: Box::new(expr.into()),
1213 }.into())
1214 )
1215 |
1216 call!(trailer_expr, allow_struct, allow_block)
1217 ));
1218
David Tolnaybcf26022017-12-25 22:10:52 -05001219 // <atom> (..<args>) ...
1220 // <atom> . <ident> (..<args>) ...
1221 // <atom> . <ident> ...
1222 // <atom> . <lit> ...
1223 // <atom> [ <expr> ] ...
1224 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001225 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001226 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001227 mut e: call!(atom_expr, allow_struct, allow_block) >>
1228 many0!(alt!(
1229 tap!(args: and_call => {
1230 let (args, paren) = args;
1231 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001232 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001233 func: Box::new(e.into()),
1234 args: args,
1235 paren_token: paren,
1236 }.into();
1237 })
1238 |
1239 tap!(more: and_method_call => {
1240 let mut call = more;
David Tolnay76418512017-12-28 23:47:47 -05001241 call.receiver = Box::new(e.into());
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 e = call.into();
1243 })
1244 |
1245 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001246 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001248 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001249 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001251 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 }.into();
1253 })
1254 |
1255 tap!(i: and_index => {
1256 let (i, token) = i;
1257 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001258 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 expr: Box::new(e.into()),
1260 bracket_token: token,
1261 index: Box::new(i),
1262 }.into();
1263 })
1264 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001265 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001266 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001267 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001268 expr: Box::new(e.into()),
1269 question_token: question,
1270 }.into();
1271 })
1272 )) >>
1273 (e)
1274 ));
1275
Michael Layzell734adb42017-06-07 16:58:31 -04001276 // XXX: Duplication == ugly
1277 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001278 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001279 mut e: call!(atom_expr, allow_struct, allow_block) >>
1280 many0!(alt!(
1281 tap!(args: and_call => {
1282 let (args, paren) = args;
1283 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001284 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001285 func: Box::new(e.into()),
1286 args: args,
1287 paren_token: paren,
1288 }.into();
1289 })
1290 |
1291 tap!(i: and_index => {
1292 let (i, token) = i;
1293 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001294 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001295 expr: Box::new(e.into()),
1296 bracket_token: token,
1297 index: Box::new(i),
1298 }.into();
1299 })
1300 )) >>
1301 (e)
1302 ));
1303
David Tolnaybcf26022017-12-25 22:10:52 -05001304 // Parse all atomic expressions which don't have to worry about precidence
1305 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001306 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001307 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1308 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001309 |
David Tolnay8c91b882017-12-28 23:04:32 -05001310 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001311 |
1312 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001313 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001314 |
David Tolnay8c91b882017-12-28 23:04:32 -05001315 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001316 |
David Tolnay8c91b882017-12-28 23:04:32 -05001317 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001318 |
1319 call!(expr_break, allow_struct) // must be before expr_path
1320 |
David Tolnay8c91b882017-12-28 23:04:32 -05001321 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001322 |
1323 call!(expr_ret, allow_struct) // must be before expr_path
1324 |
David Tolnay8c91b882017-12-28 23:04:32 -05001325 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326 |
David Tolnay8c91b882017-12-28 23:04:32 -05001327 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001328 |
David Tolnay8c91b882017-12-28 23:04:32 -05001329 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 |
David Tolnay8c91b882017-12-28 23:04:32 -05001331 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 |
David Tolnay8c91b882017-12-28 23:04:32 -05001333 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 |
David Tolnay8c91b882017-12-28 23:04:32 -05001335 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001336 |
David Tolnay8c91b882017-12-28 23:04:32 -05001337 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338 |
David Tolnay8c91b882017-12-28 23:04:32 -05001339 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001340 |
David Tolnay8c91b882017-12-28 23:04:32 -05001341 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001342 |
David Tolnay8c91b882017-12-28 23:04:32 -05001343 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001344 |
David Tolnay8c91b882017-12-28 23:04:32 -05001345 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001346 |
David Tolnay8c91b882017-12-28 23:04:32 -05001347 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001348 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001349 call!(expr_closure, allow_struct)
1350 |
David Tolnay8c91b882017-12-28 23:04:32 -05001351 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001352 |
1353 // NOTE: This is the prefix-form of range
1354 call!(expr_range, allow_struct)
1355 |
David Tolnay8c91b882017-12-28 23:04:32 -05001356 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001357 |
David Tolnay8c91b882017-12-28 23:04:32 -05001358 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001359 ));
1360
Michael Layzell734adb42017-06-07 16:58:31 -04001361 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001362 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001363 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001364 |
David Tolnay8c91b882017-12-28 23:04:32 -05001365 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001366 ));
1367
Michael Layzell734adb42017-06-07 16:58:31 -04001368 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001369 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001370 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001371 |
David Tolnay8c91b882017-12-28 23:04:32 -05001372 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001373 |
David Tolnay8c91b882017-12-28 23:04:32 -05001374 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001375 |
David Tolnay8c91b882017-12-28 23:04:32 -05001376 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001377 |
David Tolnay8c91b882017-12-28 23:04:32 -05001378 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001379 |
David Tolnay8c91b882017-12-28 23:04:32 -05001380 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001381 |
David Tolnay8c91b882017-12-28 23:04:32 -05001382 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001383 |
David Tolnay8c91b882017-12-28 23:04:32 -05001384 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001385 |
David Tolnay8c91b882017-12-28 23:04:32 -05001386 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001387 |
David Tolnay8c91b882017-12-28 23:04:32 -05001388 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001389 |
David Tolnay8c91b882017-12-28 23:04:32 -05001390 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001391 ), Expr::from));
1392
David Tolnay8c91b882017-12-28 23:04:32 -05001393 impl Synom for ExprLit {
1394 named!(parse -> Self, do_parse!(
1395 lit: syn!(Lit) >>
1396 (ExprLit {
1397 attrs: Vec::new(),
1398 lit: lit,
1399 })
1400 ));
1401 }
1402
1403 #[cfg(feature = "full")]
1404 impl Synom for ExprMacro {
1405 named!(parse -> Self, do_parse!(
1406 mac: syn!(Macro) >>
1407 (ExprMacro {
1408 attrs: Vec::new(),
1409 mac: mac,
1410 })
1411 ));
1412 }
1413
David Tolnaye98775f2017-12-28 23:17:00 -05001414 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001415 impl Synom for ExprGroup {
1416 named!(parse -> Self, do_parse!(
1417 e: grouped!(syn!(Expr)) >>
1418 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001419 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001420 expr: Box::new(e.0),
1421 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001422 })
Michael Layzell93c36282017-06-04 20:43:14 -04001423 ));
1424 }
1425
David Tolnaye98775f2017-12-28 23:17:00 -05001426 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001427 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001428 named!(parse -> Self, do_parse!(
1429 e: parens!(syn!(Expr)) >>
1430 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001431 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001432 expr: Box::new(e.0),
1433 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001434 })
Michael Layzell92639a52017-06-01 00:07:44 -04001435 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001436 }
David Tolnay89e05672016-10-02 14:39:42 -07001437
Michael Layzell734adb42017-06-07 16:58:31 -04001438 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001439 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001440 named!(parse -> Self, do_parse!(
1441 elems: brackets!(call!(Delimited::parse_terminated)) >>
1442 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001443 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001444 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001445 bracket_token: elems.1,
1446 })
1447 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001448 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001449
David Tolnay32954ef2017-12-26 22:43:16 -05001450 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001451 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001452
Michael Layzell734adb42017-06-07 16:58:31 -04001453 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001454 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001455 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001456 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001457 turbofish: option!(tuple!(
1458 punct!(::),
1459 punct!(<),
1460 call!(Delimited::parse_terminated),
1461 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001462 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001463 args: parens!(call!(Delimited::parse_terminated)) >>
1464 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001465 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001466 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001467 // this expr will get overwritten after being returned
David Tolnay76418512017-12-28 23:47:47 -05001468 receiver: Box::new(Expr::Lit(ExprLit {
David Tolnay8c91b882017-12-28 23:04:32 -05001469 attrs: Vec::new(),
1470 lit: Lit {
1471 span: Span::default(),
1472 value: LitKind::Bool(false),
1473 },
Alex Crichton954046c2017-05-30 21:49:42 -07001474 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001475
Alex Crichton954046c2017-05-30 21:49:42 -07001476 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001477 turbofish: turbofish.map(|fish| MethodTurbofish {
1478 colon2_token: fish.0,
1479 lt_token: fish.1,
1480 args: fish.2,
1481 gt_token: fish.3,
1482 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001483 args: args.0,
1484 paren_token: args.1,
1485 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001486 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001487 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001488 ));
1489
Michael Layzell734adb42017-06-07 16:58:31 -04001490 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001491 impl Synom for GenericMethodArgument {
1492 // TODO parse const generics as well
1493 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1494 }
1495
1496 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001497 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001498 named!(parse -> Self, do_parse!(
1499 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001500 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001501 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001502 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001503 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001504 })
1505 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001506 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001507
Michael Layzell734adb42017-06-07 16:58:31 -04001508 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001509 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001510 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001511 if_: keyword!(if) >>
1512 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001513 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001514 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001515 cond: expr_no_struct >>
1516 then_block: braces!(call!(Block::parse_within)) >>
1517 else_block: option!(else_block) >>
1518 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001519 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001520 pat: Box::new(pat),
1521 let_token: let_,
1522 eq_token: eq,
1523 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001524 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001525 stmts: then_block.0,
1526 brace_token: then_block.1,
1527 },
1528 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001529 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001530 })
1531 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001532 }
1533
Michael Layzell734adb42017-06-07 16:58:31 -04001534 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001535 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001536 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001537 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001538 cond: expr_no_struct >>
1539 then_block: braces!(call!(Block::parse_within)) >>
1540 else_block: option!(else_block) >>
1541 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001542 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001543 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001544 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001545 stmts: then_block.0,
1546 brace_token: then_block.1,
1547 },
1548 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001549 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001550 })
1551 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001552 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001553
Michael Layzell734adb42017-06-07 16:58:31 -04001554 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001555 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001556 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001557 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001558 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001559 |
David Tolnay8c91b882017-12-28 23:04:32 -05001560 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001561 |
1562 do_parse!(
1563 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001564 (Expr::Block(ExprBlock {
1565 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001566 block: Block {
1567 stmts: else_block.0,
1568 brace_token: else_block.1,
1569 },
1570 }))
David Tolnay939766a2016-09-23 23:48:12 -07001571 )
Alex Crichton954046c2017-05-30 21:49:42 -07001572 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001573 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001574 ));
1575
Michael Layzell734adb42017-06-07 16:58:31 -04001576 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001577 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001578 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001579 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1580 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001581 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001582 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001583 expr: expr_no_struct >>
1584 loop_block: syn!(Block) >>
1585 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001586 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001587 for_token: for_,
1588 in_token: in_,
1589 pat: Box::new(pat),
1590 expr: Box::new(expr),
1591 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001592 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001593 label: lbl.map(|p| p.0),
1594 })
1595 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001596 }
Gregory Katze5f35682016-09-27 14:20:55 -04001597
Michael Layzell734adb42017-06-07 16:58:31 -04001598 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001599 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001600 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001601 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1602 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001603 loop_block: syn!(Block) >>
1604 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001605 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001606 loop_token: loop_,
1607 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001608 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001609 label: lbl.map(|p| p.0),
1610 })
1611 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001612 }
1613
Michael Layzell734adb42017-06-07 16:58:31 -04001614 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001615 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001616 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001617 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001618 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001619 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001620 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001621 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001622 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001623 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001624 expr: Box::new(obj),
1625 match_token: match_,
1626 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001627 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001628 }
1629 })
1630 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001631 }
David Tolnay1978c672016-10-27 22:05:52 -07001632
Michael Layzell734adb42017-06-07 16:58:31 -04001633 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001634 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001635 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001636 do_: keyword!(do) >>
1637 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001638 catch_block: syn!(Block) >>
1639 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001640 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001641 block: catch_block,
1642 do_token: do_,
1643 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001644 })
Michael Layzell92639a52017-06-01 00:07:44 -04001645 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001646 }
Arnavion02ef13f2017-04-25 00:54:31 -07001647
Michael Layzell734adb42017-06-07 16:58:31 -04001648 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001649 impl Synom for ExprYield {
1650 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001651 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001652 expr: option!(syn!(Expr)) >>
1653 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001654 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001655 yield_token: yield_,
1656 expr: expr.map(Box::new),
1657 })
1658 ));
1659 }
1660
1661 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001662 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001663 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001664 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001665 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001666 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1667 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001668 body: do_parse!(
1669 expr: alt!(expr_nosemi | syn!(Expr)) >>
1670 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1671 map!(input_end!(), |_| None)
1672 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001673 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001674 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001675 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001676 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001677 ) >>
1678 (Arm {
1679 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001680 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001681 attrs: attrs,
1682 pats: pats,
1683 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001684 body: Box::new(body.0),
1685 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001686 })
1687 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001688 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001689
Michael Layzell734adb42017-06-07 16:58:31 -04001690 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001691 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001692 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001693 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001694 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001695 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001696 ret_and_body: alt!(
1697 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001698 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001699 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001700 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001701 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001702 Expr::Block(ExprBlock {
1703 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001704 block: body,
1705 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001706 )
1707 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001708 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001709 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001710 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001711 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001712 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001713 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001714 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001715 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001716 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001717 body: Box::new(ret_and_body.1),
1718 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001719 ));
1720
Michael Layzell734adb42017-06-07 16:58:31 -04001721 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001722 named!(fn_arg -> FnArg, do_parse!(
1723 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001724 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001725 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001726 if let Some((colon, ty)) = ty {
1727 FnArg::Captured(ArgCaptured {
1728 pat: pat,
1729 colon_token: colon,
1730 ty: ty,
1731 })
1732 } else {
1733 FnArg::Inferred(pat)
1734 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001735 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001736 ));
1737
Michael Layzell734adb42017-06-07 16:58:31 -04001738 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001739 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001740 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001741 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1742 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001743 cond: expr_no_struct >>
1744 while_block: syn!(Block) >>
1745 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001746 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001747 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001748 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001749 cond: Box::new(cond),
1750 body: while_block,
1751 label: lbl.map(|p| p.0),
1752 })
1753 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001754 }
1755
Michael Layzell734adb42017-06-07 16:58:31 -04001756 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001757 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001758 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001759 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1760 while_: keyword!(while) >>
1761 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001762 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001763 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001764 value: expr_no_struct >>
1765 while_block: syn!(Block) >>
1766 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001767 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001768 eq_token: eq,
1769 let_token: let_,
1770 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001771 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001772 pat: Box::new(pat),
1773 expr: Box::new(value),
1774 body: while_block,
1775 label: lbl.map(|p| p.0),
1776 })
1777 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001778 }
1779
Michael Layzell734adb42017-06-07 16:58:31 -04001780 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001781 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001782 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001783 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001784 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001785 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001786 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001787 continue_token: cont,
1788 label: lbl,
1789 })
1790 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001791 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001792
Michael Layzell734adb42017-06-07 16:58:31 -04001793 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001794 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001795 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001796 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001797 // We can't allow blocks after a `break` expression when we wouldn't
1798 // allow structs, as this expression is ambiguous.
1799 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001800 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001801 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001802 label: lbl,
1803 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001804 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001805 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001806 ));
1807
Michael Layzell734adb42017-06-07 16:58:31 -04001808 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001809 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001810 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001811 // NOTE: return is greedy and eats blocks after it even when in a
1812 // position where structs are not allowed, such as in if statement
1813 // conditions. For example:
1814 //
David Tolnaybcf26022017-12-25 22:10:52 -05001815 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001816 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001817 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001818 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001819 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001820 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001821 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001822 ));
1823
Michael Layzell734adb42017-06-07 16:58:31 -04001824 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001825 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001826 named!(parse -> Self, do_parse!(
1827 path: syn!(Path) >>
1828 data: braces!(do_parse!(
1829 fields: call!(Delimited::parse_terminated) >>
1830 base: option!(
1831 cond!(fields.is_empty() || fields.trailing_delim(),
1832 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001833 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001834 base: syn!(Expr) >>
1835 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001836 )
Michael Layzell92639a52017-06-01 00:07:44 -04001837 )
1838 ) >>
1839 (fields, base)
1840 )) >>
1841 ({
1842 let ((fields, base), brace) = data;
1843 let (dots, rest) = match base.and_then(|b| b) {
1844 Some((dots, base)) => (Some(dots), Some(base)),
1845 None => (None, None),
1846 };
1847 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001848 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001849 brace_token: brace,
1850 path: path,
1851 fields: fields,
1852 dot2_token: dots,
1853 rest: rest.map(Box::new),
1854 }
1855 })
1856 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001857 }
1858
Michael Layzell734adb42017-06-07 16:58:31 -04001859 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001860 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001861 named!(parse -> Self, alt!(
1862 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001863 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001864 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001865 value: syn!(Expr) >>
1866 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001867 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001868 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001869 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001870 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001871 })
Michael Layzell92639a52017-06-01 00:07:44 -04001872 )
1873 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001874 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001875 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001876 expr: Expr::Path(ExprPath {
1877 attrs: Vec::new(),
1878 qself: None,
1879 path: name.into(),
1880 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001881 attrs: Vec::new(),
1882 colon_token: None,
1883 })
1884 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001885 }
David Tolnay055a7042016-10-02 19:23:54 -07001886
Michael Layzell734adb42017-06-07 16:58:31 -04001887 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001888 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001889 named!(parse -> Self, do_parse!(
1890 data: brackets!(do_parse!(
1891 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001892 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001893 times: syn!(Expr) >>
1894 (value, semi, times)
1895 )) >>
1896 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001897 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001898 expr: Box::new((data.0).0),
1899 amt: Box::new((data.0).2),
1900 bracket_token: data.1,
1901 semi_token: (data.0).1,
1902 })
1903 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001904 }
David Tolnay055a7042016-10-02 19:23:54 -07001905
Michael Layzell734adb42017-06-07 16:58:31 -04001906 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001907 impl Synom for ExprUnsafe {
1908 named!(parse -> Self, do_parse!(
1909 unsafe_: keyword!(unsafe) >>
1910 b: syn!(Block) >>
1911 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001912 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001913 unsafe_token: unsafe_,
1914 block: b,
1915 })
1916 ));
1917 }
1918
1919 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001920 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001921 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001922 b: syn!(Block) >>
1923 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001924 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001925 block: b,
1926 })
1927 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001928 }
David Tolnay89e05672016-10-02 14:39:42 -07001929
Michael Layzell734adb42017-06-07 16:58:31 -04001930 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001931 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001932 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001933 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001934 (ExprRange {
1935 attrs: Vec::new(),
1936 from: None,
1937 to: hi.map(Box::new),
1938 limits: limits,
1939 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001940 ));
1941
Michael Layzell734adb42017-06-07 16:58:31 -04001942 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001943 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001944 named!(parse -> Self, alt!(
1945 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001946 punct!(..=) => { RangeLimits::Closed }
1947 |
1948 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001949 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001950 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001951 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001952 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001953 }
David Tolnay438c9052016-10-07 23:24:48 -07001954
Alex Crichton954046c2017-05-30 21:49:42 -07001955 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001956 named!(parse -> Self, do_parse!(
1957 pair: qpath >>
1958 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001959 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001960 qself: pair.0,
1961 path: pair.1,
1962 })
1963 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001964 }
David Tolnay42602292016-10-01 22:25:45 -07001965
Michael Layzell734adb42017-06-07 16:58:31 -04001966 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001967 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001968
David Tolnay32954ef2017-12-26 22:43:16 -05001969 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001970
Michael Layzell734adb42017-06-07 16:58:31 -04001971 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001972 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001973 named!(parse -> Self, do_parse!(
1974 stmts: braces!(call!(Block::parse_within)) >>
1975 (Block {
1976 stmts: stmts.0,
1977 brace_token: stmts.1,
1978 })
1979 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001980 }
David Tolnay939766a2016-09-23 23:48:12 -07001981
Michael Layzell734adb42017-06-07 16:58:31 -04001982 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001983 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001984 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001985 many0!(punct!(;)) >>
1986 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001987 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001988 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001989 mut e: syn!(Expr) >>
1990 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05001991 e.replace_attrs(attrs);
Alex Crichton70bbd592017-08-27 10:40:03 -07001992 Stmt::Expr(Box::new(e))
1993 })
1994 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001995 (match last {
1996 None => standalone,
1997 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001998 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001999 standalone
2000 }
2001 })
2002 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002003 }
2004
Michael Layzell734adb42017-06-07 16:58:31 -04002005 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002006 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002007 named!(parse -> Self, alt!(
2008 stmt_mac
2009 |
2010 stmt_local
2011 |
2012 stmt_item
2013 |
Michael Layzell35418782017-06-07 09:20:25 -04002014 stmt_blockexpr
2015 |
Michael Layzell92639a52017-06-01 00:07:44 -04002016 stmt_expr
2017 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002018 }
David Tolnay939766a2016-09-23 23:48:12 -07002019
Michael Layzell734adb42017-06-07 16:58:31 -04002020 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002021 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002022 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002023 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002024 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002025 // Only parse braces here; paren and bracket will get parsed as
2026 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002027 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002028 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05002029 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
2030 attrs: attrs,
2031 ident: None,
2032 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002033 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002034 bang_token: bang,
David Tolnayfe9d2782017-12-29 11:32:42 -05002035 tt: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05002036 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07002037 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05002038 },
David Tolnayeea28d62016-10-25 20:44:08 -07002039 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002040 semi_token: semi,
2041 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07002042 ));
2043
Michael Layzell734adb42017-06-07 16:58:31 -04002044 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002045 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002046 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002047 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002048 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002049 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002050 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2051 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07002052 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07002053 let_token: let_,
2054 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002055 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
2056 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07002057 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002058 ty: ty.map(|p| Box::new(p.1)),
2059 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002060 attrs: attrs,
2061 })))
2062 ));
2063
Michael Layzell734adb42017-06-07 16:58:31 -04002064 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002065 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002066
Michael Layzell734adb42017-06-07 16:58:31 -04002067 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002068 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002069 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002070 mut e: expr_nosemi >>
2071 // If the next token is a `.` or a `?` it is special-cased to parse as
2072 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002073 not!(punct!(.)) >>
2074 not!(punct!(?)) >>
2075 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002076 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002077 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002078 if let Some(semi) = semi {
2079 Stmt::Semi(Box::new(e), semi)
2080 } else {
2081 Stmt::Expr(Box::new(e))
2082 }
2083 })
2084 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002085
Michael Layzell734adb42017-06-07 16:58:31 -04002086 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002087 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002088 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002089 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002090 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002091 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002092 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002093 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002094 })
David Tolnay939766a2016-09-23 23:48:12 -07002095 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002096
Michael Layzell734adb42017-06-07 16:58:31 -04002097 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002098 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002099 named!(parse -> Self, alt!(
2100 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2101 |
2102 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2103 |
2104 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2105 |
2106 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2107 |
2108 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2109 |
David Tolnay323279a2017-12-29 11:26:32 -05002110 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002111 |
2112 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2113 |
2114 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2115 |
2116 syn!(PatPath) => { Pat::Path }
2117 |
2118 syn!(PatTuple) => { Pat::Tuple }
2119 |
2120 syn!(PatRef) => { Pat::Ref }
2121 |
2122 syn!(PatSlice) => { Pat::Slice }
2123 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002124 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002125
Michael Layzell734adb42017-06-07 16:58:31 -04002126 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002127 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002128 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002129 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002130 |u| PatWild { underscore_token: u }
2131 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002132 }
David Tolnay84aa0752016-10-02 23:01:13 -07002133
Michael Layzell734adb42017-06-07 16:58:31 -04002134 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002135 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002136 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002137 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002138 pat: syn!(Pat) >>
2139 (PatBox {
2140 pat: Box::new(pat),
2141 box_token: boxed,
2142 })
2143 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002144 }
2145
Michael Layzell734adb42017-06-07 16:58:31 -04002146 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002147 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002148 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002149 by_ref: option!(keyword!(ref)) >>
2150 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002151 name: alt!(
2152 syn!(Ident)
2153 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002154 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002155 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002156 not!(punct!(<)) >>
2157 not!(punct!(::)) >>
2158 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002159 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002160 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002161 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002162 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002163 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002164 subpat: subpat.map(|p| Box::new(p.1)),
2165 })
2166 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002167 }
2168
Michael Layzell734adb42017-06-07 16:58:31 -04002169 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002170 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002171 named!(parse -> Self, do_parse!(
2172 path: syn!(Path) >>
2173 tuple: syn!(PatTuple) >>
2174 (PatTupleStruct {
2175 path: path,
2176 pat: tuple,
2177 })
2178 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002179 }
2180
Michael Layzell734adb42017-06-07 16:58:31 -04002181 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002182 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002183 named!(parse -> Self, do_parse!(
2184 path: syn!(Path) >>
2185 data: braces!(do_parse!(
2186 fields: call!(Delimited::parse_terminated) >>
2187 base: option!(
2188 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002189 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002190 ) >>
2191 (fields, base)
2192 )) >>
2193 (PatStruct {
2194 path: path,
2195 fields: (data.0).0,
2196 brace_token: data.1,
2197 dot2_token: (data.0).1.and_then(|m| m),
2198 })
2199 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002200 }
2201
Michael Layzell734adb42017-06-07 16:58:31 -04002202 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002203 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002204 named!(parse -> Self, alt!(
2205 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002206 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002207 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002208 pat: syn!(Pat) >>
2209 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002210 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002211 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002212 attrs: Vec::new(),
2213 colon_token: Some(colon),
2214 })
2215 )
2216 |
2217 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002218 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002219 by_ref: option!(keyword!(ref)) >>
2220 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002221 ident: syn!(Ident) >>
2222 ({
2223 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002224 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002225 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002226 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002227 subpat: None,
2228 at_token: None,
2229 }.into();
2230 if let Some(boxed) = boxed {
2231 pat = PatBox {
2232 pat: Box::new(pat),
2233 box_token: boxed,
2234 }.into();
2235 }
2236 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002237 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002238 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002239 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002240 colon_token: None,
2241 }
2242 })
2243 )
2244 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002245 }
2246
Michael Layzell734adb42017-06-07 16:58:31 -04002247 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002248 impl Synom for Member {
2249 named!(parse -> Self, alt!(
2250 syn!(Ident) => { Member::Named }
2251 |
2252 syn!(Index) => { Member::Unnamed }
2253 ));
2254 }
2255
2256 #[cfg(feature = "full")]
2257 impl Synom for Index {
2258 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002259 lit: syn!(Lit) >>
2260 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002261 if let Ok(i) = lit.value.to_string().parse() {
2262 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002263 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002264 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002265 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002266 })
David Tolnay85b69a42017-12-27 20:43:10 -05002267 ));
2268 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002269
Michael Layzell734adb42017-06-07 16:58:31 -04002270 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002271 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002272 named!(parse -> Self, map!(
2273 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002274 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002275 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002276 }
David Tolnay9636c052016-10-02 17:11:17 -07002277
Michael Layzell734adb42017-06-07 16:58:31 -04002278 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002279 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002280 named!(parse -> Self, do_parse!(
2281 data: parens!(do_parse!(
David Tolnay41871922017-12-29 01:53:45 -05002282 front: call!(Delimited::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002283 dotdot: map!(cond!(
David Tolnay41871922017-12-29 01:53:45 -05002284 front.is_empty() || front.trailing_delim(),
Michael Layzell92639a52017-06-01 00:07:44 -04002285 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002286 dots: punct!(..) >>
2287 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002288 (dots, trailing)
2289 ))
David Tolnay41871922017-12-29 01:53:45 -05002290 ), Option::unwrap_or_default) >>
2291 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002292 Some((_, Some(_))) => true,
2293 _ => false,
2294 },
2295 call!(Delimited::parse_terminated)) >>
David Tolnay41871922017-12-29 01:53:45 -05002296 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002297 )) >>
2298 ({
David Tolnay41871922017-12-29 01:53:45 -05002299 let ((front, dotdot, back), parens) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002300 let (dotdot, trailing) = match dotdot {
2301 Some((a, b)) => (Some(a), Some(b)),
2302 None => (None, None),
2303 };
2304 PatTuple {
2305 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002306 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002307 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002308 comma_token: trailing.unwrap_or_default(),
2309 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002310 }
2311 })
2312 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002313 }
David Tolnayfbb73232016-10-03 01:00:06 -07002314
Michael Layzell734adb42017-06-07 16:58:31 -04002315 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002316 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002317 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002318 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002319 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002320 pat: syn!(Pat) >>
2321 (PatRef {
2322 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002323 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002324 and_token: and,
2325 })
2326 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002327 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002328
Michael Layzell734adb42017-06-07 16:58:31 -04002329 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002330 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002331 named!(parse -> Self, do_parse!(
2332 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002333 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002334 return parse_error(); // these need to be parsed by pat_path
2335 } else {
2336 PatLit {
2337 expr: Box::new(lit),
2338 }
2339 })
2340 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002341 }
David Tolnaye1310902016-10-29 23:40:00 -07002342
Michael Layzell734adb42017-06-07 16:58:31 -04002343 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002344 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002345 named!(parse -> Self, do_parse!(
2346 lo: pat_lit_expr >>
2347 limits: syn!(RangeLimits) >>
2348 hi: pat_lit_expr >>
2349 (PatRange {
2350 lo: Box::new(lo),
2351 hi: Box::new(hi),
2352 limits: limits,
2353 })
2354 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002355 }
David Tolnaye1310902016-10-29 23:40:00 -07002356
Michael Layzell734adb42017-06-07 16:58:31 -04002357 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002358 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002359 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002360 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002361 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002362 |
David Tolnay8c91b882017-12-28 23:04:32 -05002363 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002364 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002365 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002366 Expr::Unary(ExprUnary {
2367 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002368 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002369 expr: Box::new(v.into())
2370 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002371 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002372 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002373 })
2374 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002375
Michael Layzell734adb42017-06-07 16:58:31 -04002376 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002377 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002378 named!(parse -> Self, map!(
2379 brackets!(do_parse!(
2380 before: call!(Delimited::parse_terminated) >>
2381 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002382 dots: punct!(..) >>
2383 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002384 (dots, trailing)
2385 )) >>
2386 after: cond!(
2387 match middle {
2388 Some((_, ref trailing)) => trailing.is_some(),
2389 _ => false,
2390 },
2391 call!(Delimited::parse_terminated)
2392 ) >>
2393 (before, middle, after)
2394 )),
2395 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002396 let mut before: Delimited<Pat, Token![,]> = before;
2397 let after: Option<Delimited<Pat, Token![,]>> = after;
2398 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002399 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002400 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002401 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002402 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002403 }),
2404 bracket_token: brackets,
2405 middle: middle.and_then(|_| {
2406 if !before.is_empty() && !before.trailing_delim() {
2407 Some(Box::new(before.pop().unwrap().into_item()))
2408 } else {
2409 None
2410 }
2411 }),
2412 front: before,
2413 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002414 }
Alex Crichton954046c2017-05-30 21:49:42 -07002415 }
Michael Layzell92639a52017-06-01 00:07:44 -04002416 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002417 }
David Tolnay323279a2017-12-29 11:26:32 -05002418
2419 #[cfg(feature = "full")]
2420 impl Synom for PatMacro {
2421 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
2422 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002423}
2424
David Tolnayf4bbbd92016-09-23 14:41:55 -07002425#[cfg(feature = "printing")]
2426mod printing {
2427 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002428 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002429 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002430 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002431 #[cfg(feature = "full")]
2432 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002433
David Tolnaybcf26022017-12-25 22:10:52 -05002434 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2435 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002436 #[cfg(feature = "full")]
2437 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002438 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002439 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002440 e.to_tokens(tokens);
2441 });
2442 } else {
2443 e.to_tokens(tokens);
2444 }
2445 }
2446
David Tolnay8c91b882017-12-28 23:04:32 -05002447 #[cfg(feature = "full")]
2448 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2449 tokens.append_all(attrs.outer());
2450 }
Michael Layzell734adb42017-06-07 16:58:31 -04002451
David Tolnay8c91b882017-12-28 23:04:32 -05002452 #[cfg(not(feature = "full"))]
2453 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002454 }
2455
Michael Layzell734adb42017-06-07 16:58:31 -04002456 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002457 impl ToTokens for ExprBox {
2458 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002459 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002460 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002461 self.expr.to_tokens(tokens);
2462 }
2463 }
2464
Michael Layzell734adb42017-06-07 16:58:31 -04002465 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002466 impl ToTokens for ExprInPlace {
2467 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002468 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002469 self.place.to_tokens(tokens);
2470 self.arrow_token.to_tokens(tokens);
2471 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002472 }
2473 }
2474
Michael Layzell734adb42017-06-07 16:58:31 -04002475 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002476 impl ToTokens for ExprArray {
2477 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002478 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002479 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002480 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002481 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002482 }
2483 }
2484
2485 impl ToTokens for ExprCall {
2486 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002487 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002488 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002489 self.paren_token.surround(tokens, |tokens| {
2490 self.args.to_tokens(tokens);
2491 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002492 }
2493 }
2494
Michael Layzell734adb42017-06-07 16:58:31 -04002495 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002496 impl ToTokens for ExprMethodCall {
2497 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002498 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002499 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002500 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002501 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002502 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002503 self.paren_token.surround(tokens, |tokens| {
2504 self.args.to_tokens(tokens);
2505 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002506 }
2507 }
2508
Michael Layzell734adb42017-06-07 16:58:31 -04002509 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002510 impl ToTokens for MethodTurbofish {
2511 fn to_tokens(&self, tokens: &mut Tokens) {
2512 self.colon2_token.to_tokens(tokens);
2513 self.lt_token.to_tokens(tokens);
2514 self.args.to_tokens(tokens);
2515 self.gt_token.to_tokens(tokens);
2516 }
2517 }
2518
2519 #[cfg(feature = "full")]
2520 impl ToTokens for GenericMethodArgument {
2521 fn to_tokens(&self, tokens: &mut Tokens) {
2522 match *self {
2523 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2524 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2525 }
2526 }
2527 }
2528
2529 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002530 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002531 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002532 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002533 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002534 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002535 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002536 // distinguish ExprTuple from ExprParen.
David Tolnay2a86fdd2017-12-28 23:34:28 -05002537 if self.elems.len() == 1 && !self.elems.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002538 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002539 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002540 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002541 }
2542 }
2543
2544 impl ToTokens for ExprBinary {
2545 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002546 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002547 self.left.to_tokens(tokens);
2548 self.op.to_tokens(tokens);
2549 self.right.to_tokens(tokens);
2550 }
2551 }
2552
2553 impl ToTokens for ExprUnary {
2554 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002555 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002556 self.op.to_tokens(tokens);
2557 self.expr.to_tokens(tokens);
2558 }
2559 }
2560
David Tolnay8c91b882017-12-28 23:04:32 -05002561 impl ToTokens for ExprLit {
2562 fn to_tokens(&self, tokens: &mut Tokens) {
2563 attrs_to_tokens(&self.attrs, tokens);
2564 self.lit.to_tokens(tokens);
2565 }
2566 }
2567
Alex Crichton62a0a592017-05-22 13:58:53 -07002568 impl ToTokens for ExprCast {
2569 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002570 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002571 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002572 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002573 self.ty.to_tokens(tokens);
2574 }
2575 }
2576
David Tolnay0cf94f22017-12-28 23:46:26 -05002577 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002578 impl ToTokens for ExprType {
2579 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002580 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002581 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002582 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002583 self.ty.to_tokens(tokens);
2584 }
2585 }
2586
Michael Layzell734adb42017-06-07 16:58:31 -04002587 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002588 fn maybe_wrap_else(
2589 tokens: &mut Tokens,
David Tolnay2ccf32a2017-12-29 00:34:26 -05002590 else_: &Option<(Token![else], Box<Expr>)>,
David Tolnay51382052017-12-27 13:46:21 -05002591 ) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002592 if let Some((ref else_token, ref else_)) = *else_ {
2593 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002594
2595 // If we are not one of the valid expressions to exist in an else
2596 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002597 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002598 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002599 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002600 }
2601 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002602 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002603 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002604 });
2605 }
2606 }
2607 }
2608 }
2609
2610 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002611 impl ToTokens for ExprIf {
2612 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002613 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002614 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002615 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002616 self.then_branch.to_tokens(tokens);
2617 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002618 }
2619 }
2620
Michael Layzell734adb42017-06-07 16:58:31 -04002621 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002622 impl ToTokens for ExprIfLet {
2623 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002624 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002625 self.if_token.to_tokens(tokens);
2626 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002627 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002628 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002629 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002630 self.then_branch.to_tokens(tokens);
2631 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002632 }
2633 }
2634
Michael Layzell734adb42017-06-07 16:58:31 -04002635 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002636 impl ToTokens for ExprWhile {
2637 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002638 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002639 if self.label.is_some() {
2640 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002641 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002642 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002643 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002644 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002645 self.body.to_tokens(tokens);
2646 }
2647 }
2648
Michael Layzell734adb42017-06-07 16:58:31 -04002649 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002650 impl ToTokens for ExprWhileLet {
2651 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002652 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002653 if self.label.is_some() {
2654 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002655 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002656 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002657 self.while_token.to_tokens(tokens);
2658 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002659 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002660 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002661 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002662 self.body.to_tokens(tokens);
2663 }
2664 }
2665
Michael Layzell734adb42017-06-07 16:58:31 -04002666 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002667 impl ToTokens for ExprForLoop {
2668 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002669 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002670 if self.label.is_some() {
2671 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002672 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002673 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002674 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002675 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002676 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002677 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002678 self.body.to_tokens(tokens);
2679 }
2680 }
2681
Michael Layzell734adb42017-06-07 16:58:31 -04002682 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002683 impl ToTokens for ExprLoop {
2684 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002685 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002686 if self.label.is_some() {
2687 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002688 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002689 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002690 self.loop_token.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")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002696 impl ToTokens for ExprMatch {
2697 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002698 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002699 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002700 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002701 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002702 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002703 arm.to_tokens(tokens);
2704 // Ensure that we have a comma after a non-block arm, except
2705 // for the last one.
2706 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002707 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002708 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002709 }
2710 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002711 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002712 }
2713 }
2714
Michael Layzell734adb42017-06-07 16:58:31 -04002715 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002716 impl ToTokens for ExprCatch {
2717 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002718 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002719 self.do_token.to_tokens(tokens);
2720 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002721 self.block.to_tokens(tokens);
2722 }
2723 }
2724
Michael Layzell734adb42017-06-07 16:58:31 -04002725 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002726 impl ToTokens for ExprYield {
2727 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002728 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002729 self.yield_token.to_tokens(tokens);
2730 self.expr.to_tokens(tokens);
2731 }
2732 }
2733
2734 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002735 impl ToTokens for ExprClosure {
2736 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002737 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002738 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002739 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002740 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002741 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002742 FnArg::Captured(ArgCaptured {
2743 ref pat,
2744 ty: Type::Infer(_),
2745 ..
2746 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002747 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002748 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002749 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002750 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002751 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002752 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002753 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002754 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002755 self.body.to_tokens(tokens);
2756 }
2757 }
2758
Michael Layzell734adb42017-06-07 16:58:31 -04002759 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002760 impl ToTokens for ExprUnsafe {
2761 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002762 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002763 self.unsafe_token.to_tokens(tokens);
2764 self.block.to_tokens(tokens);
2765 }
2766 }
2767
2768 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 impl ToTokens for ExprBlock {
2770 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002771 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002772 self.block.to_tokens(tokens);
2773 }
2774 }
2775
Michael Layzell734adb42017-06-07 16:58:31 -04002776 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002777 impl ToTokens for ExprAssign {
2778 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002779 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002780 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002781 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002782 self.right.to_tokens(tokens);
2783 }
2784 }
2785
Michael Layzell734adb42017-06-07 16:58:31 -04002786 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002787 impl ToTokens for ExprAssignOp {
2788 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002789 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002790 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002791 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002792 self.right.to_tokens(tokens);
2793 }
2794 }
2795
Michael Layzell734adb42017-06-07 16:58:31 -04002796 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002797 impl ToTokens for ExprField {
2798 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002799 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002800 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002801 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002802 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002803 }
2804 }
2805
Michael Layzell734adb42017-06-07 16:58:31 -04002806 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002807 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002808 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002809 match *self {
2810 Member::Named(ident) => ident.to_tokens(tokens),
2811 Member::Unnamed(ref index) => index.to_tokens(tokens),
2812 }
2813 }
2814 }
2815
2816 #[cfg(feature = "full")]
2817 impl ToTokens for Index {
2818 fn to_tokens(&self, tokens: &mut Tokens) {
2819 tokens.append(TokenTree {
2820 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002821 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002822 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002823 }
2824 }
2825
2826 impl ToTokens for ExprIndex {
2827 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002828 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002829 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002830 self.bracket_token.surround(tokens, |tokens| {
2831 self.index.to_tokens(tokens);
2832 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002833 }
2834 }
2835
Michael Layzell734adb42017-06-07 16:58:31 -04002836 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002837 impl ToTokens for ExprRange {
2838 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002839 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002840 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002841 match self.limits {
2842 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2843 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2844 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002845 self.to.to_tokens(tokens);
2846 }
2847 }
2848
2849 impl ToTokens for ExprPath {
2850 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002851 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002852 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002853 }
2854 }
2855
Michael Layzell734adb42017-06-07 16:58:31 -04002856 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002857 impl ToTokens for ExprAddrOf {
2858 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002859 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002860 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002861 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002862 self.expr.to_tokens(tokens);
2863 }
2864 }
2865
Michael Layzell734adb42017-06-07 16:58:31 -04002866 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002867 impl ToTokens for ExprBreak {
2868 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002869 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002870 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002871 self.label.to_tokens(tokens);
2872 self.expr.to_tokens(tokens);
2873 }
2874 }
2875
Michael Layzell734adb42017-06-07 16:58:31 -04002876 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002877 impl ToTokens for ExprContinue {
2878 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002879 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002880 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002881 self.label.to_tokens(tokens);
2882 }
2883 }
2884
Michael Layzell734adb42017-06-07 16:58:31 -04002885 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002886 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002887 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002888 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002889 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002890 self.expr.to_tokens(tokens);
2891 }
2892 }
2893
Michael Layzell734adb42017-06-07 16:58:31 -04002894 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002895 impl ToTokens for ExprMacro {
2896 fn to_tokens(&self, tokens: &mut Tokens) {
2897 tokens.append_all(self.attrs.outer());
2898 self.mac.to_tokens(tokens);
2899 }
2900 }
2901
2902 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002903 impl ToTokens for ExprStruct {
2904 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002905 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002906 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002907 self.brace_token.surround(tokens, |tokens| {
2908 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002909 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002910 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002911 self.rest.to_tokens(tokens);
2912 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002913 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002914 }
2915 }
2916
Michael Layzell734adb42017-06-07 16:58:31 -04002917 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 impl ToTokens for ExprRepeat {
2919 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002920 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002921 self.bracket_token.surround(tokens, |tokens| {
2922 self.expr.to_tokens(tokens);
2923 self.semi_token.to_tokens(tokens);
2924 self.amt.to_tokens(tokens);
2925 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002926 }
2927 }
2928
David Tolnaye98775f2017-12-28 23:17:00 -05002929 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002930 impl ToTokens for ExprGroup {
2931 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002932 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002933 self.group_token.surround(tokens, |tokens| {
2934 self.expr.to_tokens(tokens);
2935 });
2936 }
2937 }
2938
David Tolnaye98775f2017-12-28 23:17:00 -05002939 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002940 impl ToTokens for ExprParen {
2941 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002942 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002943 self.paren_token.surround(tokens, |tokens| {
2944 self.expr.to_tokens(tokens);
2945 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002946 }
2947 }
2948
Michael Layzell734adb42017-06-07 16:58:31 -04002949 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002950 impl ToTokens for ExprTry {
2951 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002952 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002953 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002955 }
2956 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002957
David Tolnay2ae520a2017-12-29 11:19:50 -05002958 impl ToTokens for ExprVerbatim {
2959 fn to_tokens(&self, tokens: &mut Tokens) {
2960 self.tts.to_tokens(tokens);
2961 }
2962 }
2963
Michael Layzell734adb42017-06-07 16:58:31 -04002964 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002965 impl ToTokens for FieldValue {
2966 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002967 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05002968 if let Some(ref colon_token) = self.colon_token {
2969 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002970 self.expr.to_tokens(tokens);
2971 }
David Tolnay055a7042016-10-02 19:23:54 -07002972 }
2973 }
2974
Michael Layzell734adb42017-06-07 16:58:31 -04002975 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002976 impl ToTokens for Arm {
2977 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002978 tokens.append_all(&self.attrs);
2979 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002980 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002981 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002982 self.guard.to_tokens(tokens);
2983 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002984 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002985 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002986 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002987 }
2988 }
2989
Michael Layzell734adb42017-06-07 16:58:31 -04002990 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002991 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002992 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002993 self.underscore_token.to_tokens(tokens);
2994 }
2995 }
2996
Michael Layzell734adb42017-06-07 16:58:31 -04002997 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002998 impl ToTokens for PatIdent {
2999 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003000 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003001 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003002 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003003 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003004 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003005 self.subpat.to_tokens(tokens);
3006 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003007 }
3008 }
3009
Michael Layzell734adb42017-06-07 16:58:31 -04003010 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011 impl ToTokens for PatStruct {
3012 fn to_tokens(&self, tokens: &mut Tokens) {
3013 self.path.to_tokens(tokens);
3014 self.brace_token.surround(tokens, |tokens| {
3015 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003016 // NOTE: We need a comma before the dot2 token if it is present.
3017 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003018 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003019 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003020 self.dot2_token.to_tokens(tokens);
3021 });
3022 }
3023 }
3024
Michael Layzell734adb42017-06-07 16:58:31 -04003025 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003026 impl ToTokens for PatTupleStruct {
3027 fn to_tokens(&self, tokens: &mut Tokens) {
3028 self.path.to_tokens(tokens);
3029 self.pat.to_tokens(tokens);
3030 }
3031 }
3032
Michael Layzell734adb42017-06-07 16:58:31 -04003033 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 impl ToTokens for PatPath {
3035 fn to_tokens(&self, tokens: &mut Tokens) {
3036 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3037 }
3038 }
3039
Michael Layzell734adb42017-06-07 16:58:31 -04003040 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003041 impl ToTokens for PatTuple {
3042 fn to_tokens(&self, tokens: &mut Tokens) {
3043 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003044 self.front.to_tokens(tokens);
3045 if let Some(ref dot2_token) = self.dot2_token {
3046 if !self.front.empty_or_trailing() {
3047 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003048 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003049 }
David Tolnay41871922017-12-29 01:53:45 -05003050 dot2_token.to_tokens(tokens);
3051 self.comma_token.to_tokens(tokens);
3052 if self.comma_token.is_none() && !self.back.is_empty() {
3053 // Ensure there is a comma after the .. token.
3054 <Token![,]>::default().to_tokens(tokens);
3055 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003056 }
David Tolnay41871922017-12-29 01:53:45 -05003057 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003058 });
3059 }
3060 }
3061
Michael Layzell734adb42017-06-07 16:58:31 -04003062 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003063 impl ToTokens for PatBox {
3064 fn to_tokens(&self, tokens: &mut Tokens) {
3065 self.box_token.to_tokens(tokens);
3066 self.pat.to_tokens(tokens);
3067 }
3068 }
3069
Michael Layzell734adb42017-06-07 16:58:31 -04003070 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003071 impl ToTokens for PatRef {
3072 fn to_tokens(&self, tokens: &mut Tokens) {
3073 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003074 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003075 self.pat.to_tokens(tokens);
3076 }
3077 }
3078
Michael Layzell734adb42017-06-07 16:58:31 -04003079 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003080 impl ToTokens for PatLit {
3081 fn to_tokens(&self, tokens: &mut Tokens) {
3082 self.expr.to_tokens(tokens);
3083 }
3084 }
3085
Michael Layzell734adb42017-06-07 16:58:31 -04003086 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003087 impl ToTokens for PatRange {
3088 fn to_tokens(&self, tokens: &mut Tokens) {
3089 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003090 match self.limits {
3091 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3092 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3093 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003094 self.hi.to_tokens(tokens);
3095 }
3096 }
3097
Michael Layzell734adb42017-06-07 16:58:31 -04003098 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003099 impl ToTokens for PatSlice {
3100 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003101 // XXX: This is a mess, and it will be so easy to screw it up. How
3102 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003103 self.bracket_token.surround(tokens, |tokens| {
3104 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003105
3106 // If we need a comma before the middle or standalone .. token,
3107 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003108 if !self.front.empty_or_trailing()
3109 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003110 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003111 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003112 }
3113
3114 // If we have an identifier, we always need a .. token.
3115 if self.middle.is_some() {
3116 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003117 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003118 } else if self.dot2_token.is_some() {
3119 self.dot2_token.to_tokens(tokens);
3120 }
3121
3122 // Make sure we have a comma before the back half.
3123 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003124 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003125 self.back.to_tokens(tokens);
3126 } else {
3127 self.comma_token.to_tokens(tokens);
3128 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003129 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003130 }
3131 }
3132
Michael Layzell734adb42017-06-07 16:58:31 -04003133 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003134 impl ToTokens for PatMacro {
3135 fn to_tokens(&self, tokens: &mut Tokens) {
3136 self.mac.to_tokens(tokens);
3137 }
3138 }
3139
3140 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003141 impl ToTokens for PatVerbatim {
3142 fn to_tokens(&self, tokens: &mut Tokens) {
3143 self.tts.to_tokens(tokens);
3144 }
3145 }
3146
3147 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003148 impl ToTokens for FieldPat {
3149 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003150 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003151 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003152 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003153 }
3154 self.pat.to_tokens(tokens);
3155 }
3156 }
3157
Michael Layzell734adb42017-06-07 16:58:31 -04003158 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003159 impl ToTokens for Block {
3160 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 self.brace_token.surround(tokens, |tokens| {
3162 tokens.append_all(&self.stmts);
3163 });
David Tolnay42602292016-10-01 22:25:45 -07003164 }
3165 }
3166
Michael Layzell734adb42017-06-07 16:58:31 -04003167 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003168 impl ToTokens for Stmt {
3169 fn to_tokens(&self, tokens: &mut Tokens) {
3170 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003171 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003172 Stmt::Item(ref item) => item.to_tokens(tokens),
3173 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003174 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003175 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003176 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003177 }
David Tolnay42602292016-10-01 22:25:45 -07003178 }
3179 }
3180 }
David Tolnay191e0582016-10-02 18:31:09 -07003181
Michael Layzell734adb42017-06-07 16:58:31 -04003182 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003183 impl ToTokens for Local {
3184 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003185 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003186 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003187 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003188 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003189 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003190 self.ty.to_tokens(tokens);
3191 }
3192 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003193 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003194 self.init.to_tokens(tokens);
3195 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003196 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003197 }
3198 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003199}