blob: 9424deeab41c4a22a092adb3519461b5d058a43b [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay85b69a42017-12-27 20:43:10 -05003#[cfg(feature = "full")]
4use proc_macro2::Span;
5#[cfg(feature = "full")]
6use std::hash::{Hash, Hasher};
David Tolnayf4bbbd92016-09-23 14:41:55 -07007
Alex Crichton62a0a592017-05-22 13:58:53 -07008ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -05009 /// An expression.
10 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070011 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040012 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050013 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080014 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050015 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070016 }),
Clar Charrd22b5702017-03-10 15:24:56 -050017
Michael Layzellb78f3b52017-06-04 19:03:03 -040018 /// E.g. 'place <- val' or `in place { val }`.
Michael Layzell734adb42017-06-07 16:58:31 -040019 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050020 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070021 pub place: Box<Expr>,
Michael Layzell6a5a1642017-06-04 19:35:15 -040022 pub kind: InPlaceKind,
Alex Crichton62a0a592017-05-22 13:58:53 -070023 pub value: Box<Expr>,
24 }),
Clar Charrd22b5702017-03-10 15:24:56 -050025
Alex Crichton62a0a592017-05-22 13:58:53 -070026 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040027 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050028 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050029 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -050030 pub exprs: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070031 }),
Clar Charrd22b5702017-03-10 15:24:56 -050032
Alex Crichton62a0a592017-05-22 13:58:53 -070033 /// A function call.
34 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050035 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050037 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050038 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 }),
Clar Charrd22b5702017-03-10 15:24:56 -050040
Alex Crichton62a0a592017-05-22 13:58:53 -070041 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
42 ///
43 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080044 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070045 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040046 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050047 pub attrs: Vec<Attribute>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -070048 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080049 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050050 pub method: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -080051 pub colon2_token: Option<Token![::]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050052 pub lt_token: Option<Token![<]>,
53 pub typarams: Delimited<Type, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub gt_token: Option<Token![>]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050055 pub paren_token: token::Paren,
56 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
Clar Charrd22b5702017-03-10 15:24:56 -050058
Alex Crichton62a0a592017-05-22 13:58:53 -070059 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050060 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050061 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050062 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050063 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }),
Clar Charrd22b5702017-03-10 15:24:56 -050065
Alex Crichton62a0a592017-05-22 13:58:53 -070066 /// A binary operation, e.g. `a + b`, `a * b`.
67 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050070 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub right: Box<Expr>,
72 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A unary operation, e.g. `!x`, `*x`.
75 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub op: UnOp,
78 pub expr: Box<Expr>,
79 }),
Clar Charrd22b5702017-03-10 15:24:56 -050080
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050082 pub Lit(ExprLit {
83 pub attrs: Vec<Attribute>,
84 pub lit: Lit,
85 }),
Clar Charrd22b5702017-03-10 15:24:56 -050086
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A cast, e.g. `foo as f64`.
88 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050089 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A type ascription, e.g. `foo: f64`.
96 pub Type(ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -050097 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800100 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500102
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 /// An `if` block, with an optional else block
104 ///
105 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400106 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub cond: Box<Expr>,
110 pub if_true: Block,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800111 pub else_token: Option<Token![else]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500112 pub if_false: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700113 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500114
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 /// An `if let` expression with an optional else block
116 ///
117 /// E.g., `if let pat = expr { block } else { expr }`
118 ///
119 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400120 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500121 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800122 pub if_token: Token![if],
123 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500124 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800125 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500126 pub expr: Box<Expr>,
127 pub if_true: Block,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800128 pub else_token: Option<Token![else]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500129 pub if_false: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700130 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500131
Alex Crichton62a0a592017-05-22 13:58:53 -0700132 /// A while loop, with an optional label
133 ///
134 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400135 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500136 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700137 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800138 pub colon_token: Option<Token![:]>,
139 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500140 pub cond: Box<Expr>,
141 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500143
Alex Crichton62a0a592017-05-22 13:58:53 -0700144 /// A while-let loop, with an optional label.
145 ///
146 /// E.g., `'label: while let pat = expr { block }`
147 ///
148 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400149 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500150 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700151 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub colon_token: Option<Token![:]>,
153 pub while_token: Token![while],
154 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500155 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800156 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500157 pub expr: Box<Expr>,
158 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500160
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// A for loop, with an optional label.
162 ///
163 /// E.g., `'label: for pat in expr { block }`
164 ///
165 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400166 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500167 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500168 pub label: Option<Lifetime>,
169 pub colon_token: Option<Token![:]>,
170 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500172 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700173 pub expr: Box<Expr>,
174 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700175 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500176
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 /// Conditionless loop with an optional label.
178 ///
179 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400180 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500181 pub attrs: Vec<Attribute>,
David Tolnay63e3dee2017-06-03 20:13:17 -0700182 pub label: Option<Lifetime>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800183 pub colon_token: Option<Token![:]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500184 pub loop_token: Token![loop],
185 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500187
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400189 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500190 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800191 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500193 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 pub arms: Vec<Arm>,
195 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500196
Alex Crichton62a0a592017-05-22 13:58:53 -0700197 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400198 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500199 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 pub capture: CaptureBy,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800201 pub or1_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500202 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500204 pub output: ReturnType,
205 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500207
Nika Layzell640832a2017-12-04 13:37:09 -0500208 /// An unsafe block (`unsafe { ... }`)
209 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500210 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500211 pub unsafe_token: Token![unsafe],
212 pub block: Block,
213 }),
214
215 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400216 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500217 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub block: Block,
219 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700220
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400222 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500223 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700224 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800225 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500226 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500228
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 /// An assignment with an operator
230 ///
231 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400232 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500233 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700234 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500235 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 pub right: Box<Expr>,
237 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500238
David Tolnay85b69a42017-12-27 20:43:10 -0500239 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
240 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400241 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500242 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500243 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800244 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500245 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500247
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 /// An indexing operation (`foo[2]`)
249 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500250 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700251 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500252 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500253 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500255
David Tolnaybe55d7b2017-12-17 23:41:20 -0800256 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400257 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500258 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700260 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500261 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700263
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 /// Variable reference, possibly containing `::` and/or type
265 /// parameters, e.g. foo::bar::<baz>.
266 ///
267 /// Optionally "qualified",
268 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
269 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500270 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700271 pub qself: Option<QSelf>,
272 pub path: Path,
273 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700274
Alex Crichton62a0a592017-05-22 13:58:53 -0700275 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400276 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500277 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800278 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -0700279 pub mutbl: Mutability,
280 pub expr: Box<Expr>,
281 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500282
Alex Crichton62a0a592017-05-22 13:58:53 -0700283 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400284 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500285 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500286 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700287 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700288 pub expr: Option<Box<Expr>>,
289 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500290
Alex Crichton62a0a592017-05-22 13:58:53 -0700291 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400292 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500293 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800294 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500295 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500297
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500299 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500300 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500302 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500306 pub Macro(ExprMacro #full {
307 pub attrs: Vec<Attribute>,
308 pub mac: Macro,
309 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700310
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 /// A struct literal expression.
312 ///
313 /// For example, `Foo {x: 1, y: 2}`, or
314 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400315 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500316 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500318 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500319 pub fields: Delimited<FieldValue, Token![,]>,
320 pub dot2_token: Option<Token![..]>,
321 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700322 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700323
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 /// An array literal constructed from one repeated element.
325 ///
326 /// For example, `[1; 5]`. The first expression is the element
327 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400328 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500329 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500330 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700331 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500332 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700333 pub amt: Box<Expr>,
334 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700335
Alex Crichton62a0a592017-05-22 13:58:53 -0700336 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500337 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500338 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500339 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500340 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700341 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700342
Michael Layzell93c36282017-06-04 20:43:14 -0400343 /// No-op: used solely so we can pretty-print faithfully
344 ///
345 /// A `group` represents a `None`-delimited span in the input
346 /// `TokenStream` which affects the precidence of the resulting
347 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500348 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500349 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500350 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500351 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400352 }),
353
Alex Crichton62a0a592017-05-22 13:58:53 -0700354 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400355 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500356 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700357 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700360
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 /// A catch expression.
362 ///
363 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400364 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500365 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800366 pub do_token: Token![do],
367 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700368 pub block: Block,
369 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700370
371 /// A yield expression.
372 ///
373 /// E.g. `yield expr`
374 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500375 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800376 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700377 pub expr: Option<Box<Expr>>,
378 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700379 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700380}
381
David Tolnay8c91b882017-12-28 23:04:32 -0500382impl Expr {
383 // Not public API.
384 #[doc(hidden)]
385 pub fn attrs_mut(&mut self) -> &mut Vec<Attribute> {
386 match *self {
387 Expr::Box(ExprBox { ref mut attrs, .. }) |
388 Expr::InPlace(ExprInPlace { ref mut attrs, .. }) |
389 Expr::Array(ExprArray { ref mut attrs, .. }) |
390 Expr::Call(ExprCall { ref mut attrs, .. }) |
391 Expr::MethodCall(ExprMethodCall { ref mut attrs, .. }) |
392 Expr::Tuple(ExprTuple { ref mut attrs, .. }) |
393 Expr::Binary(ExprBinary { ref mut attrs, .. }) |
394 Expr::Unary(ExprUnary { ref mut attrs, .. }) |
395 Expr::Lit(ExprLit { ref mut attrs, .. }) |
396 Expr::Cast(ExprCast { ref mut attrs, .. }) |
397 Expr::Type(ExprType { ref mut attrs, .. }) |
398 Expr::If(ExprIf { ref mut attrs, .. }) |
399 Expr::IfLet(ExprIfLet { ref mut attrs, .. }) |
400 Expr::While(ExprWhile { ref mut attrs, .. }) |
401 Expr::WhileLet(ExprWhileLet { ref mut attrs, .. }) |
402 Expr::ForLoop(ExprForLoop { ref mut attrs, .. }) |
403 Expr::Loop(ExprLoop { ref mut attrs, .. }) |
404 Expr::Match(ExprMatch { ref mut attrs, .. }) |
405 Expr::Closure(ExprClosure { ref mut attrs, .. }) |
406 Expr::Unsafe(ExprUnsafe { ref mut attrs, .. }) |
407 Expr::Block(ExprBlock { ref mut attrs, .. }) |
408 Expr::Assign(ExprAssign { ref mut attrs, .. }) |
409 Expr::AssignOp(ExprAssignOp { ref mut attrs, .. }) |
410 Expr::Field(ExprField { ref mut attrs, .. }) |
411 Expr::Index(ExprIndex { ref mut attrs, .. }) |
412 Expr::Range(ExprRange { ref mut attrs, .. }) |
413 Expr::Path(ExprPath { ref mut attrs, .. }) |
414 Expr::AddrOf(ExprAddrOf { ref mut attrs, .. }) |
415 Expr::Break(ExprBreak { ref mut attrs, .. }) |
416 Expr::Continue(ExprContinue { ref mut attrs, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500417 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500418 Expr::Macro(ExprMacro { ref mut attrs, .. }) |
419 Expr::Struct(ExprStruct { ref mut attrs, .. }) |
420 Expr::Repeat(ExprRepeat { ref mut attrs, .. }) |
421 Expr::Paren(ExprParen { ref mut attrs, .. }) |
422 Expr::Group(ExprGroup { ref mut attrs, .. }) |
423 Expr::Try(ExprTry { ref mut attrs, .. }) |
424 Expr::Catch(ExprCatch { ref mut attrs, .. }) |
425 Expr::Yield(ExprYield { ref mut attrs, .. }) => attrs,
426 }
427 }
428}
429
Michael Layzell734adb42017-06-07 16:58:31 -0400430#[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500431ast_enum! {
432 /// A struct or tuple struct field accessed in a struct literal or field
433 /// expression.
434 pub enum Member {
435 /// A named field like `self.x`.
436 Named(Ident),
437 /// An unnamed field like `self.0`.
438 Unnamed(Index),
439 }
440}
441
442#[cfg(feature = "full")]
443ast_struct! {
444 /// The index of an unnamed tuple struct field.
445 pub struct Index #manual_extra_traits {
446 pub index: u32,
447 pub span: Span,
448 }
449}
450
451#[cfg(feature = "full")]
452impl Eq for Index {}
453
454#[cfg(feature = "full")]
455impl PartialEq for Index {
456 fn eq(&self, other: &Self) -> bool {
457 self.index == other.index
458 }
459}
460
461#[cfg(feature = "full")]
462impl Hash for Index {
463 fn hash<H: Hasher>(&self, state: &mut H) {
464 self.index.hash(state);
465 }
466}
467
468#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700469ast_struct! {
470 /// A field-value pair in a struct literal.
471 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500472 /// Attributes tagged on the field.
473 pub attrs: Vec<Attribute>,
474
475 /// Name or index of the field.
476 pub member: Member,
477
478 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500479
Alex Crichton62a0a592017-05-22 13:58:53 -0700480 /// Value of the field.
481 pub expr: Expr,
Clar Charrd22b5702017-03-10 15:24:56 -0500482
Alex Crichton62a0a592017-05-22 13:58:53 -0700483 /// Whether this is a shorthand field, e.g. `Struct { x }`
484 /// instead of `Struct { x: x }`.
485 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700486 }
David Tolnay055a7042016-10-02 19:23:54 -0700487}
488
Michael Layzell734adb42017-06-07 16:58:31 -0400489#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700490ast_struct! {
491 /// A Block (`{ .. }`).
492 ///
493 /// E.g. `{ .. }` as in `fn foo() { .. }`
494 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500495 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700496 /// Statements in a block
497 pub stmts: Vec<Stmt>,
498 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700499}
500
Michael Layzell734adb42017-06-07 16:58:31 -0400501#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700502ast_enum! {
503 /// A statement, usually ending in a semicolon.
504 pub enum Stmt {
505 /// A local (let) binding.
506 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700507
Alex Crichton62a0a592017-05-22 13:58:53 -0700508 /// An item definition.
509 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700510
Alex Crichton62a0a592017-05-22 13:58:53 -0700511 /// Expr without trailing semicolon.
512 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700513
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800515 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700516 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700517}
518
Michael Layzell734adb42017-06-07 16:58:31 -0400519#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700520ast_struct! {
521 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
522 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500523 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800524 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500526 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800527 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500528 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700529 /// Initializer expression to set the value, if any
530 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500531 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700532 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700533}
534
Michael Layzell734adb42017-06-07 16:58:31 -0400535#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 // Clippy false positive
538 // https://github.com/Manishearth/rust-clippy/issues/1241
539 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
540 pub enum Pat {
541 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800543 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700544 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700545
Alex Crichton62a0a592017-05-22 13:58:53 -0700546 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
547 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
548 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
549 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700550 pub Ident(PatIdent {
551 pub mode: BindingMode,
552 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800553 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500554 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700555 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700556
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
558 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700559 pub Struct(PatStruct {
560 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500561 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500562 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800563 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700564 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700565
Alex Crichton62a0a592017-05-22 13:58:53 -0700566 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
567 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
568 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700569 pub TupleStruct(PatTupleStruct {
570 pub path: Path,
571 pub pat: PatTuple,
572 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700573
Alex Crichton62a0a592017-05-22 13:58:53 -0700574 /// A possibly qualified path pattern.
575 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
576 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
577 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700578 pub Path(PatPath {
579 pub qself: Option<QSelf>,
580 pub path: Path,
581 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700582
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 /// A tuple pattern `(a, b)`.
584 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
585 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700586 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500587 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500588 pub pats: Delimited<Pat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800589 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500590 pub dots_pos: Option<usize>,
591 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700592 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700593 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700594 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800595 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500596 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700597 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700598 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700599 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800600 pub and_token: Token![&],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500601 pub mutbl: Mutability,
602 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700604 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700605 pub Lit(PatLit {
606 pub expr: Box<Expr>,
607 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800608 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700609 pub Range(PatRange {
610 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700611 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500612 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700613 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400614 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700615 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500616 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700618 pub middle: Option<Box<Pat>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800619 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500620 pub dot2_token: Option<Token![..]>,
621 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700622 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700623 /// A macro pattern; pre-expansion
David Tolnaydecf28d2017-11-11 11:56:45 -0800624 pub Macro(Macro),
Alex Crichton62a0a592017-05-22 13:58:53 -0700625 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700626}
627
Michael Layzell734adb42017-06-07 16:58:31 -0400628#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700629ast_struct! {
630 /// An arm of a 'match'.
631 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800632 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700633 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500634 /// ```rust
635 /// # #![feature(dotdoteq_in_patterns)]
636 /// #
637 /// # fn main() {
638 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700639 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500640 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700641 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500642 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700643 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500644 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700645 /// ```
646 pub struct Arm {
647 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800648 pub pats: Delimited<Pat, Token![|]>,
649 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700650 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800651 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800653 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700654 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700655}
656
Michael Layzell734adb42017-06-07 16:58:31 -0400657#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700658ast_enum! {
659 /// A capture clause
Alex Crichton2e0229c2017-05-23 09:34:50 -0700660 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700661 pub enum CaptureBy {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800662 Value(Token![move]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700663 Ref,
664 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700665}
666
Michael Layzell734adb42017-06-07 16:58:31 -0400667#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700668ast_enum! {
669 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700670 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700671 pub enum RangeLimits {
672 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800673 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700674 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800675 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700676 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700677}
678
Michael Layzell734adb42017-06-07 16:58:31 -0400679#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700680ast_struct! {
681 /// A single field in a struct pattern
682 ///
683 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
684 /// are treated the same as `x: x, y: ref y, z: ref mut z`,
685 /// except `is_shorthand` is true
686 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500687 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700688 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500689 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500690 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700691 /// The pattern the field is destructured to
692 pub pat: Box<Pat>,
693 pub is_shorthand: bool,
Alex Crichton62a0a592017-05-22 13:58:53 -0700694 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700695}
696
Michael Layzell734adb42017-06-07 16:58:31 -0400697#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700698ast_enum! {
Alex Crichton2e0229c2017-05-23 09:34:50 -0700699 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700700 pub enum BindingMode {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800701 ByRef(Token![ref], Mutability),
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 ByValue(Mutability),
703 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700704}
705
Michael Layzell734adb42017-06-07 16:58:31 -0400706#[cfg(feature = "full")]
Michael Layzell6a5a1642017-06-04 19:35:15 -0400707ast_enum! {
708 #[cfg_attr(feature = "clone-impls", derive(Copy))]
709 pub enum InPlaceKind {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800710 Arrow(Token![<-]),
711 In(Token![in]),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400712 }
713}
714
Michael Layzell3936ceb2017-07-08 00:28:36 -0400715#[cfg(any(feature = "parsing", feature = "printing"))]
716#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700717fn arm_expr_requires_comma(expr: &Expr) -> bool {
718 // see https://github.com/rust-lang/rust/blob/eb8f2586e
719 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500720 match *expr {
721 Expr::Unsafe(..)
722 | Expr::Block(..)
723 | Expr::If(..)
724 | Expr::IfLet(..)
725 | Expr::Match(..)
726 | Expr::While(..)
727 | Expr::WhileLet(..)
728 | Expr::Loop(..)
729 | Expr::ForLoop(..)
730 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700731 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400732 }
733}
734
David Tolnayb9c8e322016-09-23 20:48:37 -0700735#[cfg(feature = "parsing")]
736pub mod parsing {
737 use super::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700738 use ty::parsing::qpath;
David Tolnayb9c8e322016-09-23 20:48:37 -0700739
Michael Layzell734adb42017-06-07 16:58:31 -0400740 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500741 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500742 use synom::Synom;
743 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400744 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500745 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500746 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700747
David Tolnaybcf26022017-12-25 22:10:52 -0500748 // When we're parsing expressions which occur before blocks, like in an if
749 // statement's condition, we cannot parse a struct literal.
750 //
751 // Struct literals are ambiguous in certain positions
752 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700753 macro_rules! ambiguous_expr {
754 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700755 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700756 };
757 }
758
David Tolnaybcf26022017-12-25 22:10:52 -0500759 // When we are parsing an optional suffix expression, we cannot allow blocks
760 // if structs are not allowed.
761 //
762 // Example:
763 //
764 // if break {} {}
765 //
766 // is ambiguous between:
767 //
768 // if (break {}) {}
769 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400770 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400771 macro_rules! opt_ambiguous_expr {
772 ($i:expr, $allow_struct:ident) => {
773 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
774 };
775 }
776
Alex Crichton954046c2017-05-30 21:49:42 -0700777 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400778 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700779
780 fn description() -> Option<&'static str> {
781 Some("expression")
782 }
783 }
784
Michael Layzell734adb42017-06-07 16:58:31 -0400785 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700786 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
787
David Tolnaybcf26022017-12-25 22:10:52 -0500788 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400789 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500790 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500791 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400792 }
793
Michael Layzell734adb42017-06-07 16:58:31 -0400794 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500795 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500796 // NOTE: We intentionally skip assign_expr, placement_expr, and
797 // range_expr, as they are not parsed in non-full mode.
798 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400799 }
800
David Tolnaybcf26022017-12-25 22:10:52 -0500801 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400802 macro_rules! binop {
803 (
804 $name: ident,
805 $next: ident,
806 $submac: ident!( $($args:tt)* )
807 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500808 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400809 mut e: call!($next, allow_struct, allow_block) >>
810 many0!(do_parse!(
811 op: $submac!($($args)*) >>
812 rhs: call!($next, allow_struct, true) >>
813 ({
814 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500815 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400816 left: Box::new(e.into()),
817 op: op,
818 right: Box::new(rhs.into()),
819 }.into();
820 })
821 )) >>
822 (e)
823 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700824 }
David Tolnay54e854d2016-10-24 12:03:30 -0700825 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700826
David Tolnaybcf26022017-12-25 22:10:52 -0500827 // <placement> = <placement> ..
828 // <placement> += <placement> ..
829 // <placement> -= <placement> ..
830 // <placement> *= <placement> ..
831 // <placement> /= <placement> ..
832 // <placement> %= <placement> ..
833 // <placement> ^= <placement> ..
834 // <placement> &= <placement> ..
835 // <placement> |= <placement> ..
836 // <placement> <<= <placement> ..
837 // <placement> >>= <placement> ..
838 //
839 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400840 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500841 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400842 mut e: call!(placement_expr, allow_struct, allow_block) >>
843 alt!(
844 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800845 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400846 // Recurse into self to parse right-associative operator.
847 rhs: call!(assign_expr, allow_struct, true) >>
848 ({
849 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500850 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400851 left: Box::new(e.into()),
852 eq_token: eq,
853 right: Box::new(rhs.into()),
854 }.into();
855 })
856 )
857 |
858 do_parse!(
859 op: call!(BinOp::parse_assign_op) >>
860 // Recurse into self to parse right-associative operator.
861 rhs: call!(assign_expr, allow_struct, true) >>
862 ({
863 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500864 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400865 left: Box::new(e.into()),
866 op: op,
867 right: Box::new(rhs.into()),
868 }.into();
869 })
870 )
871 |
872 epsilon!()
873 ) >>
874 (e)
875 ));
876
David Tolnaybcf26022017-12-25 22:10:52 -0500877 // <range> <- <range> ..
878 //
879 // NOTE: The `in place { expr }` version of this syntax is parsed in
880 // `atom_expr`, not here.
881 //
882 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400883 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500884 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400885 mut e: call!(range_expr, allow_struct, allow_block) >>
886 alt!(
887 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800888 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400889 // Recurse into self to parse right-associative operator.
890 rhs: call!(placement_expr, allow_struct, true) >>
891 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400892 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500893 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400894 // op: BinOp::Place(larrow),
895 place: Box::new(e.into()),
Michael Layzell6a5a1642017-06-04 19:35:15 -0400896 kind: InPlaceKind::Arrow(arrow),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400897 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400898 }.into();
899 })
900 )
901 |
902 epsilon!()
903 ) >>
904 (e)
905 ));
906
David Tolnaybcf26022017-12-25 22:10:52 -0500907 // <or> ... <or> ..
908 // <or> .. <or> ..
909 // <or> ..
910 //
911 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
912 // rules are for parsing these expressions are, but this is not correct.
913 // For example, `a .. b .. c` is not a legal expression. It should not
914 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
915 //
916 // NOTE: The form of ranges which don't include a preceding expression are
917 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400918 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500919 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400920 mut e: call!(or_expr, allow_struct, allow_block) >>
921 many0!(do_parse!(
922 limits: syn!(RangeLimits) >>
923 // We don't want to allow blocks here if we don't allow structs. See
924 // the reasoning for `opt_ambiguous_expr!` above.
925 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
926 ({
927 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500928 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400929 from: Some(Box::new(e.into())),
930 limits: limits,
931 to: hi.map(|e| Box::new(e.into())),
932 }.into();
933 })
934 )) >>
935 (e)
936 ));
937
David Tolnaybcf26022017-12-25 22:10:52 -0500938 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800939 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400940
David Tolnaybcf26022017-12-25 22:10:52 -0500941 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800942 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400943
David Tolnaybcf26022017-12-25 22:10:52 -0500944 // <bitor> == <bitor> ...
945 // <bitor> != <bitor> ...
946 // <bitor> >= <bitor> ...
947 // <bitor> <= <bitor> ...
948 // <bitor> > <bitor> ...
949 // <bitor> < <bitor> ...
950 //
951 // NOTE: This operator appears to be parsed as left-associative, but errors
952 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -0500953 binop!(
954 compare_expr,
955 bitor_expr,
956 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800957 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400958 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800959 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400960 |
961 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800962 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400963 |
964 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -0800965 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -0400966 |
Michael Layzell6a5a1642017-06-04 19:35:15 -0400967 do_parse!(
968 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -0800969 not!(punct!(<-)) >>
970 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -0400971 (BinOp::Lt(t))
972 )
Michael Layzellb78f3b52017-06-04 19:03:03 -0400973 |
David Tolnayf8db7ba2017-11-11 22:52:16 -0800974 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -0500975 )
976 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400977
David Tolnaybcf26022017-12-25 22:10:52 -0500978 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -0500979 binop!(
980 bitor_expr,
981 bitxor_expr,
982 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
983 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400984
David Tolnaybcf26022017-12-25 22:10:52 -0500985 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -0500986 binop!(
987 bitxor_expr,
988 bitand_expr,
989 do_parse!(
990 // NOTE: Make sure we aren't looking at ^=.
991 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
992 )
993 );
Michael Layzellb78f3b52017-06-04 19:03:03 -0400994
David Tolnaybcf26022017-12-25 22:10:52 -0500995 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -0500996 binop!(
997 bitand_expr,
998 shift_expr,
999 do_parse!(
1000 // NOTE: Make sure we aren't looking at && or &=.
1001 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1002 )
1003 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004
David Tolnaybcf26022017-12-25 22:10:52 -05001005 // <arith> << <arith> ...
1006 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001007 binop!(
1008 shift_expr,
1009 arith_expr,
1010 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001011 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001012 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001013 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001014 )
1015 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016
David Tolnaybcf26022017-12-25 22:10:52 -05001017 // <term> + <term> ...
1018 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001019 binop!(
1020 arith_expr,
1021 term_expr,
1022 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001024 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001025 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001026 )
1027 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001028
David Tolnaybcf26022017-12-25 22:10:52 -05001029 // <cast> * <cast> ...
1030 // <cast> / <cast> ...
1031 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001032 binop!(
1033 term_expr,
1034 cast_expr,
1035 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001036 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001037 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001038 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001039 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001040 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001041 )
1042 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001043
David Tolnaybcf26022017-12-25 22:10:52 -05001044 // <unary> as <ty>
1045 // <unary> : <ty>
David Tolnay8c91b882017-12-28 23:04:32 -05001046 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001047 mut e: call!(unary_expr, allow_struct, allow_block) >>
1048 many0!(alt!(
1049 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001050 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001051 // We can't accept `A + B` in cast expressions, as it's
1052 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001053 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001054 ({
1055 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001056 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001057 expr: Box::new(e.into()),
1058 as_token: as_,
1059 ty: Box::new(ty),
1060 }.into();
1061 })
1062 )
1063 |
1064 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001065 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066 // We can't accept `A + B` in cast expressions, as it's
1067 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001068 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001069 ({
1070 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001071 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001072 expr: Box::new(e.into()),
1073 colon_token: colon,
1074 ty: Box::new(ty),
1075 }.into();
1076 })
1077 )
1078 )) >>
1079 (e)
1080 ));
1081
David Tolnaybcf26022017-12-25 22:10:52 -05001082 // <UnOp> <trailer>
1083 // & <trailer>
1084 // &mut <trailer>
1085 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001086 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001087 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001088 do_parse!(
1089 op: syn!(UnOp) >>
1090 expr: call!(unary_expr, allow_struct, true) >>
1091 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001092 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001093 op: op,
1094 expr: Box::new(expr.into()),
1095 }.into())
1096 )
1097 |
1098 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001099 and: punct!(&) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001100 mutability: syn!(Mutability) >>
1101 expr: call!(unary_expr, allow_struct, true) >>
1102 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001103 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001104 and_token: and,
1105 mutbl: mutability,
1106 expr: Box::new(expr.into()),
1107 }.into())
1108 )
1109 |
1110 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001111 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001112 expr: call!(unary_expr, allow_struct, true) >>
1113 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001114 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001115 box_token: box_,
1116 expr: Box::new(expr.into()),
1117 }.into())
1118 )
1119 |
1120 call!(trailer_expr, allow_struct, allow_block)
1121 ));
1122
Michael Layzell734adb42017-06-07 16:58:31 -04001123 // XXX: This duplication is ugly
1124 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001125 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001126 do_parse!(
1127 op: syn!(UnOp) >>
1128 expr: call!(unary_expr, allow_struct, true) >>
1129 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001130 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001131 op: op,
1132 expr: Box::new(expr.into()),
1133 }.into())
1134 )
1135 |
1136 call!(trailer_expr, allow_struct, allow_block)
1137 ));
1138
David Tolnaybcf26022017-12-25 22:10:52 -05001139 // <atom> (..<args>) ...
1140 // <atom> . <ident> (..<args>) ...
1141 // <atom> . <ident> ...
1142 // <atom> . <lit> ...
1143 // <atom> [ <expr> ] ...
1144 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001145 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001146 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001147 mut e: call!(atom_expr, allow_struct, allow_block) >>
1148 many0!(alt!(
1149 tap!(args: and_call => {
1150 let (args, paren) = args;
1151 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001152 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001153 func: Box::new(e.into()),
1154 args: args,
1155 paren_token: paren,
1156 }.into();
1157 })
1158 |
1159 tap!(more: and_method_call => {
1160 let mut call = more;
1161 call.expr = Box::new(e.into());
1162 e = call.into();
1163 })
1164 |
1165 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001166 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001167 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001168 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001169 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001170 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001171 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001172 }.into();
1173 })
1174 |
1175 tap!(i: and_index => {
1176 let (i, token) = i;
1177 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001178 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179 expr: Box::new(e.into()),
1180 bracket_token: token,
1181 index: Box::new(i),
1182 }.into();
1183 })
1184 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001185 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001186 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001187 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 expr: Box::new(e.into()),
1189 question_token: question,
1190 }.into();
1191 })
1192 )) >>
1193 (e)
1194 ));
1195
Michael Layzell734adb42017-06-07 16:58:31 -04001196 // XXX: Duplication == ugly
1197 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001198 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001199 mut e: call!(atom_expr, allow_struct, allow_block) >>
1200 many0!(alt!(
1201 tap!(args: and_call => {
1202 let (args, paren) = args;
1203 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001204 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001205 func: Box::new(e.into()),
1206 args: args,
1207 paren_token: paren,
1208 }.into();
1209 })
1210 |
1211 tap!(i: and_index => {
1212 let (i, token) = i;
1213 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001214 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001215 expr: Box::new(e.into()),
1216 bracket_token: token,
1217 index: Box::new(i),
1218 }.into();
1219 })
1220 )) >>
1221 (e)
1222 ));
1223
David Tolnaybcf26022017-12-25 22:10:52 -05001224 // Parse all atomic expressions which don't have to worry about precidence
1225 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001226 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001227 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1228 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001229 |
David Tolnay8c91b882017-12-28 23:04:32 -05001230 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001231 |
1232 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001233 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001234 |
David Tolnay8c91b882017-12-28 23:04:32 -05001235 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001236 |
David Tolnay8c91b882017-12-28 23:04:32 -05001237 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001238 |
1239 call!(expr_break, allow_struct) // must be before expr_path
1240 |
David Tolnay8c91b882017-12-28 23:04:32 -05001241 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 |
1243 call!(expr_ret, allow_struct) // must be before expr_path
1244 |
1245 // NOTE: The `in place { expr }` form. `place <- expr` is parsed above.
David Tolnay8c91b882017-12-28 23:04:32 -05001246 syn!(ExprInPlace) => { Expr::InPlace }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 |
David Tolnay8c91b882017-12-28 23:04:32 -05001248 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001249 |
David Tolnay8c91b882017-12-28 23:04:32 -05001250 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001251 |
David Tolnay8c91b882017-12-28 23:04:32 -05001252 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001253 |
David Tolnay8c91b882017-12-28 23:04:32 -05001254 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001255 |
David Tolnay8c91b882017-12-28 23:04:32 -05001256 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001257 |
David Tolnay8c91b882017-12-28 23:04:32 -05001258 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 |
David Tolnay8c91b882017-12-28 23:04:32 -05001260 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001261 |
David Tolnay8c91b882017-12-28 23:04:32 -05001262 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001263 |
David Tolnay8c91b882017-12-28 23:04:32 -05001264 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001265 |
David Tolnay8c91b882017-12-28 23:04:32 -05001266 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267 |
David Tolnay8c91b882017-12-28 23:04:32 -05001268 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001269 |
David Tolnay8c91b882017-12-28 23:04:32 -05001270 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001271 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001272 call!(expr_closure, allow_struct)
1273 |
David Tolnay8c91b882017-12-28 23:04:32 -05001274 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001275 |
1276 // NOTE: This is the prefix-form of range
1277 call!(expr_range, allow_struct)
1278 |
David Tolnay8c91b882017-12-28 23:04:32 -05001279 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001280 |
David Tolnay8c91b882017-12-28 23:04:32 -05001281 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001282 ));
1283
Michael Layzell734adb42017-06-07 16:58:31 -04001284 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001285 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001286 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001287 |
David Tolnay8c91b882017-12-28 23:04:32 -05001288 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001289 ));
1290
Michael Layzell734adb42017-06-07 16:58:31 -04001291 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001292 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001293 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001294 |
David Tolnay8c91b882017-12-28 23:04:32 -05001295 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001296 |
David Tolnay8c91b882017-12-28 23:04:32 -05001297 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001298 |
David Tolnay8c91b882017-12-28 23:04:32 -05001299 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001300 |
David Tolnay8c91b882017-12-28 23:04:32 -05001301 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001302 |
David Tolnay8c91b882017-12-28 23:04:32 -05001303 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001304 |
David Tolnay8c91b882017-12-28 23:04:32 -05001305 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001306 |
David Tolnay8c91b882017-12-28 23:04:32 -05001307 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001308 |
David Tolnay8c91b882017-12-28 23:04:32 -05001309 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001310 |
David Tolnay8c91b882017-12-28 23:04:32 -05001311 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001314 ), Expr::from));
1315
David Tolnay8c91b882017-12-28 23:04:32 -05001316 impl Synom for ExprLit {
1317 named!(parse -> Self, do_parse!(
1318 lit: syn!(Lit) >>
1319 (ExprLit {
1320 attrs: Vec::new(),
1321 lit: lit,
1322 })
1323 ));
1324 }
1325
1326 #[cfg(feature = "full")]
1327 impl Synom for ExprMacro {
1328 named!(parse -> Self, do_parse!(
1329 mac: syn!(Macro) >>
1330 (ExprMacro {
1331 attrs: Vec::new(),
1332 mac: mac,
1333 })
1334 ));
1335 }
1336
David Tolnaye98775f2017-12-28 23:17:00 -05001337 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001338 impl Synom for ExprGroup {
1339 named!(parse -> Self, do_parse!(
1340 e: grouped!(syn!(Expr)) >>
1341 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001342 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001343 expr: Box::new(e.0),
1344 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001345 })
Michael Layzell93c36282017-06-04 20:43:14 -04001346 ));
1347 }
1348
David Tolnaye98775f2017-12-28 23:17:00 -05001349 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001350 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001351 named!(parse -> Self, do_parse!(
1352 e: parens!(syn!(Expr)) >>
1353 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001354 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001355 expr: Box::new(e.0),
1356 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001357 })
Michael Layzell92639a52017-06-01 00:07:44 -04001358 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001359 }
David Tolnay89e05672016-10-02 14:39:42 -07001360
Michael Layzell734adb42017-06-07 16:58:31 -04001361 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001362 impl Synom for ExprInPlace {
Michael Layzell92639a52017-06-01 00:07:44 -04001363 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001364 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001365 place: expr_no_struct >>
1366 value: braces!(call!(Block::parse_within)) >>
1367 (ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -05001368 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001369 place: Box::new(place),
Michael Layzell6a5a1642017-06-04 19:35:15 -04001370 kind: InPlaceKind::In(in_),
David Tolnay8c91b882017-12-28 23:04:32 -05001371 value: Box::new(ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001372 attrs: Vec::new(),
David Tolnay8c91b882017-12-28 23:04:32 -05001373 block: Block {
1374 stmts: value.0,
1375 brace_token: value.1,
1376 },
1377 }.into()),
Michael Layzell92639a52017-06-01 00:07:44 -04001378 })
1379 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001380 }
David Tolnay6696c3e2016-10-30 11:45:10 -07001381
Michael Layzell734adb42017-06-07 16:58:31 -04001382 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001383 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001384 named!(parse -> Self, do_parse!(
1385 elems: brackets!(call!(Delimited::parse_terminated)) >>
1386 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001387 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001388 exprs: elems.0,
1389 bracket_token: elems.1,
1390 })
1391 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001392 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001393
David Tolnay32954ef2017-12-26 22:43:16 -05001394 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001395 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001396
Michael Layzell734adb42017-06-07 16:58:31 -04001397 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001398 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001399 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001400 method: syn!(Ident) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001401 typarams: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001402 colon2: punct!(::) >>
1403 lt: punct!(<) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001404 tys: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001405 gt: punct!(>) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001406 (colon2, lt, tys, gt)
David Tolnayfa0edf22016-09-23 22:58:24 -07001407 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001408 args: parens!(call!(Delimited::parse_terminated)) >>
1409 ({
1410 let (colon2, lt, tys, gt) = match typarams {
1411 Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)),
1412 None => (None, None, None, None),
1413 };
1414 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001415 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001416 // this expr will get overwritten after being returned
David Tolnay8c91b882017-12-28 23:04:32 -05001417 expr: Box::new(Expr::Lit(ExprLit {
1418 attrs: Vec::new(),
1419 lit: Lit {
1420 span: Span::default(),
1421 value: LitKind::Bool(false),
1422 },
Alex Crichton954046c2017-05-30 21:49:42 -07001423 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001424
Alex Crichton954046c2017-05-30 21:49:42 -07001425 method: method,
1426 args: args.0,
1427 paren_token: args.1,
1428 dot_token: dot,
1429 lt_token: lt,
1430 gt_token: gt,
1431 colon2_token: colon2,
1432 typarams: tys.unwrap_or_default(),
1433 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001434 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001435 ));
1436
Michael Layzell734adb42017-06-07 16:58:31 -04001437 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001438 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001439 named!(parse -> Self, do_parse!(
1440 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001441 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001442 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001443 args: elems.0,
1444 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001445 })
1446 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001447 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001448
Michael Layzell734adb42017-06-07 16:58:31 -04001449 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001450 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001451 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001452 if_: keyword!(if) >>
1453 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001454 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001455 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001456 cond: expr_no_struct >>
1457 then_block: braces!(call!(Block::parse_within)) >>
1458 else_block: option!(else_block) >>
1459 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001460 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001461 pat: Box::new(pat),
1462 let_token: let_,
1463 eq_token: eq,
1464 expr: Box::new(cond),
1465 if_true: Block {
1466 stmts: then_block.0,
1467 brace_token: then_block.1,
1468 },
1469 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001470 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001471 if_false: else_block.map(|p| Box::new(p.1.into())),
1472 })
1473 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001474 }
1475
Michael Layzell734adb42017-06-07 16:58:31 -04001476 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001477 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001478 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001479 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001480 cond: expr_no_struct >>
1481 then_block: braces!(call!(Block::parse_within)) >>
1482 else_block: option!(else_block) >>
1483 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001484 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001485 cond: Box::new(cond),
1486 if_true: Block {
1487 stmts: then_block.0,
1488 brace_token: then_block.1,
1489 },
1490 if_token: if_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001491 else_token: else_block.as_ref().map(|p| Token![else]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001492 if_false: else_block.map(|p| Box::new(p.1.into())),
1493 })
1494 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001495 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001496
Michael Layzell734adb42017-06-07 16:58:31 -04001497 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001498 named!(else_block -> (Token![else], Expr), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001499 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001500 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001501 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001502 |
David Tolnay8c91b882017-12-28 23:04:32 -05001503 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001504 |
1505 do_parse!(
1506 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001507 (Expr::Block(ExprBlock {
1508 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001509 block: Block {
1510 stmts: else_block.0,
1511 brace_token: else_block.1,
1512 },
1513 }))
David Tolnay939766a2016-09-23 23:48:12 -07001514 )
Alex Crichton954046c2017-05-30 21:49:42 -07001515 ) >>
1516 (else_, expr)
David Tolnay939766a2016-09-23 23:48:12 -07001517 ));
1518
Michael Layzell734adb42017-06-07 16:58:31 -04001519 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001520 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001521 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001522 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1523 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001524 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001525 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001526 expr: expr_no_struct >>
1527 loop_block: syn!(Block) >>
1528 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001529 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001530 for_token: for_,
1531 in_token: in_,
1532 pat: Box::new(pat),
1533 expr: Box::new(expr),
1534 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001535 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001536 label: lbl.map(|p| p.0),
1537 })
1538 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001539 }
Gregory Katze5f35682016-09-27 14:20:55 -04001540
Michael Layzell734adb42017-06-07 16:58:31 -04001541 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001542 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001543 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001544 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1545 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001546 loop_block: syn!(Block) >>
1547 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001548 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001549 loop_token: loop_,
1550 body: loop_block,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001551 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001552 label: lbl.map(|p| p.0),
1553 })
1554 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001555 }
1556
Michael Layzell734adb42017-06-07 16:58:31 -04001557 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001558 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001559 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001560 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001561 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001562 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001563 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001564 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001565 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001566 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001567 expr: Box::new(obj),
1568 match_token: match_,
1569 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001570 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001571 }
1572 })
1573 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001574 }
David Tolnay1978c672016-10-27 22:05:52 -07001575
Michael Layzell734adb42017-06-07 16:58:31 -04001576 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001577 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001578 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001579 do_: keyword!(do) >>
1580 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001581 catch_block: syn!(Block) >>
1582 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001583 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001584 block: catch_block,
1585 do_token: do_,
1586 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001587 })
Michael Layzell92639a52017-06-01 00:07:44 -04001588 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001589 }
Arnavion02ef13f2017-04-25 00:54:31 -07001590
Michael Layzell734adb42017-06-07 16:58:31 -04001591 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001592 impl Synom for ExprYield {
1593 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001594 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001595 expr: option!(syn!(Expr)) >>
1596 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001597 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001598 yield_token: yield_,
1599 expr: expr.map(Box::new),
1600 })
1601 ));
1602 }
1603
1604 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001605 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001606 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001607 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001608 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001609 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1610 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001611 body: do_parse!(
1612 expr: alt!(expr_nosemi | syn!(Expr)) >>
1613 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1614 map!(input_end!(), |_| None)
1615 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001616 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001617 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001618 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001619 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001620 ) >>
1621 (Arm {
1622 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001623 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001624 attrs: attrs,
1625 pats: pats,
1626 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001627 body: Box::new(body.0),
1628 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001629 })
1630 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001631 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001632
Michael Layzell734adb42017-06-07 16:58:31 -04001633 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001634 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001635 capture: syn!(CaptureBy) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001636 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001637 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001638 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001639 ret_and_body: alt!(
1640 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001641 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001642 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001643 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001644 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001645 Expr::Block(ExprBlock {
1646 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001647 block: body,
1648 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001649 )
1650 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001651 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001652 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001653 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001654 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001655 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001656 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001657 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001658 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001659 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001660 body: Box::new(ret_and_body.1),
1661 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001662 ));
1663
Michael Layzell734adb42017-06-07 16:58:31 -04001664 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001665 named!(fn_arg -> FnArg, do_parse!(
1666 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001667 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001668 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001669 if let Some((colon, ty)) = ty {
1670 FnArg::Captured(ArgCaptured {
1671 pat: pat,
1672 colon_token: colon,
1673 ty: ty,
1674 })
1675 } else {
1676 FnArg::Inferred(pat)
1677 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001678 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001679 ));
1680
Michael Layzell734adb42017-06-07 16:58:31 -04001681 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001682 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001683 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001684 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1685 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001686 cond: expr_no_struct >>
1687 while_block: syn!(Block) >>
1688 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001689 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001690 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001691 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001692 cond: Box::new(cond),
1693 body: while_block,
1694 label: lbl.map(|p| p.0),
1695 })
1696 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001697 }
1698
Michael Layzell734adb42017-06-07 16:58:31 -04001699 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001700 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001701 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001702 lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >>
1703 while_: keyword!(while) >>
1704 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001705 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001706 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001707 value: expr_no_struct >>
1708 while_block: syn!(Block) >>
1709 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001710 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001711 eq_token: eq,
1712 let_token: let_,
1713 while_token: while_,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001714 colon_token: lbl.as_ref().map(|p| Token![:]((p.1).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001715 pat: Box::new(pat),
1716 expr: Box::new(value),
1717 body: while_block,
1718 label: lbl.map(|p| p.0),
1719 })
1720 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001721 }
1722
Michael Layzell734adb42017-06-07 16:58:31 -04001723 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001724 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001725 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001726 cont: keyword!(continue) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001727 lbl: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001728 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001729 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001730 continue_token: cont,
1731 label: lbl,
1732 })
1733 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001734 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001735
Michael Layzell734adb42017-06-07 16:58:31 -04001736 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001737 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001738 break_: keyword!(break) >>
David Tolnay63e3dee2017-06-03 20:13:17 -07001739 lbl: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001740 // We can't allow blocks after a `break` expression when we wouldn't
1741 // allow structs, as this expression is ambiguous.
1742 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001743 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001744 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001745 label: lbl,
1746 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001747 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001748 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001749 ));
1750
Michael Layzell734adb42017-06-07 16:58:31 -04001751 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001752 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001753 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001754 // NOTE: return is greedy and eats blocks after it even when in a
1755 // position where structs are not allowed, such as in if statement
1756 // conditions. For example:
1757 //
David Tolnaybcf26022017-12-25 22:10:52 -05001758 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001759 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001760 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001761 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001762 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001763 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001764 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001765 ));
1766
Michael Layzell734adb42017-06-07 16:58:31 -04001767 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001768 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001769 named!(parse -> Self, do_parse!(
1770 path: syn!(Path) >>
1771 data: braces!(do_parse!(
1772 fields: call!(Delimited::parse_terminated) >>
1773 base: option!(
1774 cond!(fields.is_empty() || fields.trailing_delim(),
1775 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001776 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001777 base: syn!(Expr) >>
1778 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001779 )
Michael Layzell92639a52017-06-01 00:07:44 -04001780 )
1781 ) >>
1782 (fields, base)
1783 )) >>
1784 ({
1785 let ((fields, base), brace) = data;
1786 let (dots, rest) = match base.and_then(|b| b) {
1787 Some((dots, base)) => (Some(dots), Some(base)),
1788 None => (None, None),
1789 };
1790 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001791 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001792 brace_token: brace,
1793 path: path,
1794 fields: fields,
1795 dot2_token: dots,
1796 rest: rest.map(Box::new),
1797 }
1798 })
1799 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001800 }
1801
Michael Layzell734adb42017-06-07 16:58:31 -04001802 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001803 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001804 named!(parse -> Self, alt!(
1805 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001806 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001807 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001808 value: syn!(Expr) >>
1809 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001810 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001811 expr: value,
1812 is_shorthand: false,
Alex Crichton954046c2017-05-30 21:49:42 -07001813 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001814 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001815 })
Michael Layzell92639a52017-06-01 00:07:44 -04001816 )
1817 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001818 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001819 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001820 expr: Expr::Path(ExprPath {
1821 attrs: Vec::new(),
1822 qself: None,
1823 path: name.into(),
1824 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001825 is_shorthand: true,
1826 attrs: Vec::new(),
1827 colon_token: None,
1828 })
1829 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001830 }
David Tolnay055a7042016-10-02 19:23:54 -07001831
Michael Layzell734adb42017-06-07 16:58:31 -04001832 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001833 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001834 named!(parse -> Self, do_parse!(
1835 data: brackets!(do_parse!(
1836 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001837 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001838 times: syn!(Expr) >>
1839 (value, semi, times)
1840 )) >>
1841 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001842 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001843 expr: Box::new((data.0).0),
1844 amt: Box::new((data.0).2),
1845 bracket_token: data.1,
1846 semi_token: (data.0).1,
1847 })
1848 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001849 }
David Tolnay055a7042016-10-02 19:23:54 -07001850
Michael Layzell734adb42017-06-07 16:58:31 -04001851 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001852 impl Synom for ExprUnsafe {
1853 named!(parse -> Self, do_parse!(
1854 unsafe_: keyword!(unsafe) >>
1855 b: syn!(Block) >>
1856 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001857 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001858 unsafe_token: unsafe_,
1859 block: b,
1860 })
1861 ));
1862 }
1863
1864 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001865 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001866 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001867 b: syn!(Block) >>
1868 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001869 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001870 block: b,
1871 })
1872 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001873 }
David Tolnay89e05672016-10-02 14:39:42 -07001874
Michael Layzell734adb42017-06-07 16:58:31 -04001875 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001876 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001877 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001878 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001879 (ExprRange {
1880 attrs: Vec::new(),
1881 from: None,
1882 to: hi.map(Box::new),
1883 limits: limits,
1884 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001885 ));
1886
Michael Layzell734adb42017-06-07 16:58:31 -04001887 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001888 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001889 named!(parse -> Self, alt!(
1890 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001891 punct!(..=) => { RangeLimits::Closed }
1892 |
1893 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001894 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001895 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001896 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001897 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001898 }
David Tolnay438c9052016-10-07 23:24:48 -07001899
Alex Crichton954046c2017-05-30 21:49:42 -07001900 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001901 named!(parse -> Self, do_parse!(
1902 pair: qpath >>
1903 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001904 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001905 qself: pair.0,
1906 path: pair.1,
1907 })
1908 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001909 }
David Tolnay42602292016-10-01 22:25:45 -07001910
Michael Layzell734adb42017-06-07 16:58:31 -04001911 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001912 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001913
David Tolnay32954ef2017-12-26 22:43:16 -05001914 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001915
Michael Layzell734adb42017-06-07 16:58:31 -04001916 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001917 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001918 named!(parse -> Self, do_parse!(
1919 stmts: braces!(call!(Block::parse_within)) >>
1920 (Block {
1921 stmts: stmts.0,
1922 brace_token: stmts.1,
1923 })
1924 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001925 }
David Tolnay939766a2016-09-23 23:48:12 -07001926
Michael Layzell734adb42017-06-07 16:58:31 -04001927 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001928 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001929 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001930 many0!(punct!(;)) >>
1931 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001932 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001933 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001934 mut e: syn!(Expr) >>
1935 ({
David Tolnay8c91b882017-12-28 23:04:32 -05001936 *e.attrs_mut() = attrs;
Alex Crichton70bbd592017-08-27 10:40:03 -07001937 Stmt::Expr(Box::new(e))
1938 })
1939 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001940 (match last {
1941 None => standalone,
1942 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07001943 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04001944 standalone
1945 }
1946 })
1947 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001948 }
1949
Michael Layzell734adb42017-06-07 16:58:31 -04001950 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001951 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04001952 named!(parse -> Self, alt!(
1953 stmt_mac
1954 |
1955 stmt_local
1956 |
1957 stmt_item
1958 |
Michael Layzell35418782017-06-07 09:20:25 -04001959 stmt_blockexpr
1960 |
Michael Layzell92639a52017-06-01 00:07:44 -04001961 stmt_expr
1962 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001963 }
David Tolnay939766a2016-09-23 23:48:12 -07001964
Michael Layzell734adb42017-06-07 16:58:31 -04001965 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07001966 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001967 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001968 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001969 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07001970 // Only parse braces here; paren and bracket will get parsed as
1971 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07001972 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001973 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05001974 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
1975 attrs: attrs,
1976 ident: None,
1977 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05001978 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07001979 bang_token: bang,
David Tolnay369f0c52017-12-27 01:50:45 -05001980 tokens: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05001981 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07001982 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05001983 },
David Tolnayeea28d62016-10-25 20:44:08 -07001984 },
David Tolnay57b52bc2017-12-28 18:06:38 -05001985 semi_token: semi,
1986 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07001987 ));
1988
Michael Layzell734adb42017-06-07 16:58:31 -04001989 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07001990 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001991 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001992 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001993 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001994 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001995 init: option!(tuple!(punct!(=), syn!(Expr))) >>
1996 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07001997 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07001998 let_token: let_,
1999 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002000 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
2001 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07002002 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002003 ty: ty.map(|p| Box::new(p.1)),
2004 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002005 attrs: attrs,
2006 })))
2007 ));
2008
Michael Layzell734adb42017-06-07 16:58:31 -04002009 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002010 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002011
Michael Layzell734adb42017-06-07 16:58:31 -04002012 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002013 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002014 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002015 mut e: expr_nosemi >>
2016 // If the next token is a `.` or a `?` it is special-cased to parse as
2017 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002018 not!(punct!(.)) >>
2019 not!(punct!(?)) >>
2020 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002021 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002022 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002023 if let Some(semi) = semi {
2024 Stmt::Semi(Box::new(e), semi)
2025 } else {
2026 Stmt::Expr(Box::new(e))
2027 }
2028 })
2029 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002030
Michael Layzell734adb42017-06-07 16:58:31 -04002031 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002032 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002033 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002034 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002035 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002036 ({
David Tolnay8c91b882017-12-28 23:04:32 -05002037 *e.attrs_mut() = attrs;
Michael Layzell35418782017-06-07 09:20:25 -04002038 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002039 })
David Tolnay939766a2016-09-23 23:48:12 -07002040 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002041
Michael Layzell734adb42017-06-07 16:58:31 -04002042 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002043 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002044 named!(parse -> Self, alt!(
2045 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2046 |
2047 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2048 |
2049 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2050 |
2051 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2052 |
2053 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2054 |
David Tolnaydecf28d2017-11-11 11:56:45 -08002055 syn!(Macro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002056 |
2057 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2058 |
2059 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2060 |
2061 syn!(PatPath) => { Pat::Path }
2062 |
2063 syn!(PatTuple) => { Pat::Tuple }
2064 |
2065 syn!(PatRef) => { Pat::Ref }
2066 |
2067 syn!(PatSlice) => { Pat::Slice }
2068 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002069 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002070
Michael Layzell734adb42017-06-07 16:58:31 -04002071 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002072 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002073 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002074 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002075 |u| PatWild { underscore_token: u }
2076 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002077 }
David Tolnay84aa0752016-10-02 23:01:13 -07002078
Michael Layzell734adb42017-06-07 16:58:31 -04002079 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002080 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002081 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002082 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002083 pat: syn!(Pat) >>
2084 (PatBox {
2085 pat: Box::new(pat),
2086 box_token: boxed,
2087 })
2088 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002089 }
2090
Michael Layzell734adb42017-06-07 16:58:31 -04002091 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002092 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002093 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002094 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002095 mutability: syn!(Mutability) >>
2096 name: alt!(
2097 syn!(Ident)
2098 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002099 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002100 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002101 not!(punct!(<)) >>
2102 not!(punct!(::)) >>
2103 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002104 (PatIdent {
2105 mode: match mode {
2106 Some(mode) => BindingMode::ByRef(mode, mutability),
2107 None => BindingMode::ByValue(mutability),
2108 },
2109 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002110 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002111 subpat: subpat.map(|p| Box::new(p.1)),
2112 })
2113 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002114 }
2115
Michael Layzell734adb42017-06-07 16:58:31 -04002116 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002117 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002118 named!(parse -> Self, do_parse!(
2119 path: syn!(Path) >>
2120 tuple: syn!(PatTuple) >>
2121 (PatTupleStruct {
2122 path: path,
2123 pat: tuple,
2124 })
2125 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002126 }
2127
Michael Layzell734adb42017-06-07 16:58:31 -04002128 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002129 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002130 named!(parse -> Self, do_parse!(
2131 path: syn!(Path) >>
2132 data: braces!(do_parse!(
2133 fields: call!(Delimited::parse_terminated) >>
2134 base: option!(
2135 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002136 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002137 ) >>
2138 (fields, base)
2139 )) >>
2140 (PatStruct {
2141 path: path,
2142 fields: (data.0).0,
2143 brace_token: data.1,
2144 dot2_token: (data.0).1.and_then(|m| m),
2145 })
2146 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002147 }
2148
Michael Layzell734adb42017-06-07 16:58:31 -04002149 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002150 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002151 named!(parse -> Self, alt!(
2152 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002153 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002154 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002155 pat: syn!(Pat) >>
2156 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002157 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002158 pat: Box::new(pat),
2159 is_shorthand: false,
2160 attrs: Vec::new(),
2161 colon_token: Some(colon),
2162 })
2163 )
2164 |
2165 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002166 boxed: option!(keyword!(box)) >>
2167 mode: option!(keyword!(ref)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002168 mutability: syn!(Mutability) >>
2169 ident: syn!(Ident) >>
2170 ({
2171 let mut pat: Pat = PatIdent {
2172 mode: if let Some(mode) = mode {
2173 BindingMode::ByRef(mode, mutability)
2174 } else {
2175 BindingMode::ByValue(mutability)
2176 },
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002177 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002178 subpat: None,
2179 at_token: None,
2180 }.into();
2181 if let Some(boxed) = boxed {
2182 pat = PatBox {
2183 pat: Box::new(pat),
2184 box_token: boxed,
2185 }.into();
2186 }
2187 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002188 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002189 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002190 is_shorthand: true,
Alex Crichton954046c2017-05-30 21:49:42 -07002191 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002192 colon_token: None,
2193 }
2194 })
2195 )
2196 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002197 }
2198
Michael Layzell734adb42017-06-07 16:58:31 -04002199 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002200 impl Synom for Member {
2201 named!(parse -> Self, alt!(
2202 syn!(Ident) => { Member::Named }
2203 |
2204 syn!(Index) => { Member::Unnamed }
2205 ));
2206 }
2207
2208 #[cfg(feature = "full")]
2209 impl Synom for Index {
2210 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002211 lit: syn!(Lit) >>
2212 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002213 if let Ok(i) = lit.value.to_string().parse() {
2214 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002215 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002216 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002217 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002218 })
David Tolnay85b69a42017-12-27 20:43:10 -05002219 ));
2220 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002221
Michael Layzell734adb42017-06-07 16:58:31 -04002222 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002223 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002224 named!(parse -> Self, map!(
2225 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002226 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002227 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002228 }
David Tolnay9636c052016-10-02 17:11:17 -07002229
Michael Layzell734adb42017-06-07 16:58:31 -04002230 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002231 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002232 named!(parse -> Self, do_parse!(
2233 data: parens!(do_parse!(
2234 elems: call!(Delimited::parse_terminated) >>
2235 dotdot: map!(cond!(
2236 elems.is_empty() || elems.trailing_delim(),
2237 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002238 dots: punct!(..) >>
2239 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002240 (dots, trailing)
2241 ))
David Tolnaybc7d7d92017-06-03 20:54:05 -07002242 ), |x| x.and_then(|x| x)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002243 rest: cond!(match dotdot {
2244 Some((_, Some(_))) => true,
2245 _ => false,
2246 },
2247 call!(Delimited::parse_terminated)) >>
2248 (elems, dotdot, rest)
2249 )) >>
2250 ({
2251 let ((mut elems, dotdot, rest), parens) = data;
2252 let (dotdot, trailing) = match dotdot {
2253 Some((a, b)) => (Some(a), Some(b)),
2254 None => (None, None),
2255 };
2256 PatTuple {
2257 paren_token: parens,
2258 dots_pos: dotdot.as_ref().map(|_| elems.len()),
2259 dot2_token: dotdot,
2260 comma_token: trailing.and_then(|b| b),
2261 pats: {
2262 if let Some(rest) = rest {
2263 for elem in rest {
2264 elems.push(elem);
Alex Crichton954046c2017-05-30 21:49:42 -07002265 }
Michael Layzell92639a52017-06-01 00:07:44 -04002266 }
2267 elems
2268 },
2269 }
2270 })
2271 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002272 }
David Tolnayfbb73232016-10-03 01:00:06 -07002273
Michael Layzell734adb42017-06-07 16:58:31 -04002274 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002275 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002276 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002277 and: punct!(&) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002278 mutability: syn!(Mutability) >>
2279 pat: syn!(Pat) >>
2280 (PatRef {
2281 pat: Box::new(pat),
2282 mutbl: mutability,
2283 and_token: and,
2284 })
2285 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002286 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002287
Michael Layzell734adb42017-06-07 16:58:31 -04002288 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002289 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002290 named!(parse -> Self, do_parse!(
2291 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002292 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002293 return parse_error(); // these need to be parsed by pat_path
2294 } else {
2295 PatLit {
2296 expr: Box::new(lit),
2297 }
2298 })
2299 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002300 }
David Tolnaye1310902016-10-29 23:40:00 -07002301
Michael Layzell734adb42017-06-07 16:58:31 -04002302 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002303 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002304 named!(parse -> Self, do_parse!(
2305 lo: pat_lit_expr >>
2306 limits: syn!(RangeLimits) >>
2307 hi: pat_lit_expr >>
2308 (PatRange {
2309 lo: Box::new(lo),
2310 hi: Box::new(hi),
2311 limits: limits,
2312 })
2313 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002314 }
David Tolnaye1310902016-10-29 23:40:00 -07002315
Michael Layzell734adb42017-06-07 16:58:31 -04002316 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002317 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002318 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002319 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002320 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002321 |
David Tolnay8c91b882017-12-28 23:04:32 -05002322 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002323 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002324 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002325 Expr::Unary(ExprUnary {
2326 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002327 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002328 expr: Box::new(v.into())
2329 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002330 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002331 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002332 })
2333 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002334
Michael Layzell734adb42017-06-07 16:58:31 -04002335 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002336 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002337 named!(parse -> Self, map!(
2338 brackets!(do_parse!(
2339 before: call!(Delimited::parse_terminated) >>
2340 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002341 dots: punct!(..) >>
2342 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002343 (dots, trailing)
2344 )) >>
2345 after: cond!(
2346 match middle {
2347 Some((_, ref trailing)) => trailing.is_some(),
2348 _ => false,
2349 },
2350 call!(Delimited::parse_terminated)
2351 ) >>
2352 (before, middle, after)
2353 )),
2354 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002355 let mut before: Delimited<Pat, Token![,]> = before;
2356 let after: Option<Delimited<Pat, Token![,]>> = after;
2357 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002358 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002359 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002360 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002361 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002362 }),
2363 bracket_token: brackets,
2364 middle: middle.and_then(|_| {
2365 if !before.is_empty() && !before.trailing_delim() {
2366 Some(Box::new(before.pop().unwrap().into_item()))
2367 } else {
2368 None
2369 }
2370 }),
2371 front: before,
2372 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002373 }
Alex Crichton954046c2017-05-30 21:49:42 -07002374 }
Michael Layzell92639a52017-06-01 00:07:44 -04002375 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002376 }
David Tolnay435a9a82016-10-29 13:47:20 -07002377
Michael Layzell734adb42017-06-07 16:58:31 -04002378 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002379 impl Synom for CaptureBy {
Michael Layzell92639a52017-06-01 00:07:44 -04002380 named!(parse -> Self, alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002381 keyword!(move) => { CaptureBy::Value }
Michael Layzell92639a52017-06-01 00:07:44 -04002382 |
2383 epsilon!() => { |_| CaptureBy::Ref }
2384 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002385 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002386}
2387
David Tolnayf4bbbd92016-09-23 14:41:55 -07002388#[cfg(feature = "printing")]
2389mod printing {
2390 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002391 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002392 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002393 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002394 #[cfg(feature = "full")]
2395 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002396
David Tolnaybcf26022017-12-25 22:10:52 -05002397 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2398 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002399 #[cfg(feature = "full")]
2400 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002401 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002402 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002403 e.to_tokens(tokens);
2404 });
2405 } else {
2406 e.to_tokens(tokens);
2407 }
2408 }
2409
David Tolnay8c91b882017-12-28 23:04:32 -05002410 #[cfg(feature = "full")]
2411 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2412 tokens.append_all(attrs.outer());
2413 }
Michael Layzell734adb42017-06-07 16:58:31 -04002414
David Tolnay8c91b882017-12-28 23:04:32 -05002415 #[cfg(not(feature = "full"))]
2416 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002417 }
2418
Michael Layzell734adb42017-06-07 16:58:31 -04002419 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002420 impl ToTokens for ExprBox {
2421 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002422 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002423 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002424 self.expr.to_tokens(tokens);
2425 }
2426 }
2427
Michael Layzell734adb42017-06-07 16:58:31 -04002428 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002429 impl ToTokens for ExprInPlace {
2430 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002431 tokens.append_all(self.attrs.outer());
Michael Layzell6a5a1642017-06-04 19:35:15 -04002432 match self.kind {
2433 InPlaceKind::Arrow(ref arrow) => {
2434 self.place.to_tokens(tokens);
2435 arrow.to_tokens(tokens);
2436 self.value.to_tokens(tokens);
2437 }
2438 InPlaceKind::In(ref _in) => {
2439 _in.to_tokens(tokens);
2440 self.place.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002441 // NOTE: The second operand must be in a block, add one if
2442 // it is not present.
David Tolnay8c91b882017-12-28 23:04:32 -05002443 if let Expr::Block(_) = *self.value {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002444 self.value.to_tokens(tokens);
2445 } else {
David Tolnay32954ef2017-12-26 22:43:16 -05002446 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002447 self.value.to_tokens(tokens);
2448 })
2449 }
Michael Layzell6a5a1642017-06-04 19:35:15 -04002450 }
2451 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002452 }
2453 }
2454
Michael Layzell734adb42017-06-07 16:58:31 -04002455 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002456 impl ToTokens for ExprArray {
2457 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002458 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002459 self.bracket_token.surround(tokens, |tokens| {
2460 self.exprs.to_tokens(tokens);
2461 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002462 }
2463 }
2464
2465 impl ToTokens for ExprCall {
2466 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002467 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002468 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002469 self.paren_token.surround(tokens, |tokens| {
2470 self.args.to_tokens(tokens);
2471 })
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 ExprMethodCall {
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.expr.to_tokens(tokens);
2480 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002481 self.method.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002482 if !self.typarams.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07002483 TokensOrDefault(&self.colon2_token).to_tokens(tokens);
2484 TokensOrDefault(&self.lt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002485 self.typarams.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002486 TokensOrDefault(&self.gt_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002487 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002488 self.paren_token.surround(tokens, |tokens| {
2489 self.args.to_tokens(tokens);
2490 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002491 }
2492 }
2493
Michael Layzell734adb42017-06-07 16:58:31 -04002494 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002495 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002496 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002497 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002498 self.paren_token.surround(tokens, |tokens| {
2499 self.args.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002500 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002501 // distinguish ExprTuple from ExprParen.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002502 if self.args.len() == 1 && !self.args.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002503 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002504 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002505 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002506 }
2507 }
2508
2509 impl ToTokens for ExprBinary {
2510 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002511 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002512 self.left.to_tokens(tokens);
2513 self.op.to_tokens(tokens);
2514 self.right.to_tokens(tokens);
2515 }
2516 }
2517
2518 impl ToTokens for ExprUnary {
2519 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002520 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002521 self.op.to_tokens(tokens);
2522 self.expr.to_tokens(tokens);
2523 }
2524 }
2525
David Tolnay8c91b882017-12-28 23:04:32 -05002526 impl ToTokens for ExprLit {
2527 fn to_tokens(&self, tokens: &mut Tokens) {
2528 attrs_to_tokens(&self.attrs, tokens);
2529 self.lit.to_tokens(tokens);
2530 }
2531 }
2532
Alex Crichton62a0a592017-05-22 13:58:53 -07002533 impl ToTokens for ExprCast {
2534 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002535 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002536 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002537 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002538 self.ty.to_tokens(tokens);
2539 }
2540 }
2541
2542 impl ToTokens for ExprType {
2543 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002544 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002545 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002546 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002547 self.ty.to_tokens(tokens);
2548 }
2549 }
2550
Michael Layzell734adb42017-06-07 16:58:31 -04002551 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002552 fn maybe_wrap_else(
2553 tokens: &mut Tokens,
2554 else_token: &Option<Token![else]>,
2555 if_false: &Option<Box<Expr>>,
2556 ) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002557 if let Some(ref if_false) = *if_false {
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002558 TokensOrDefault(else_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002559
2560 // If we are not one of the valid expressions to exist in an else
2561 // clause, wrap ourselves in a block.
David Tolnay8c91b882017-12-28 23:04:32 -05002562 match **if_false {
2563 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002564 if_false.to_tokens(tokens);
2565 }
2566 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002567 token::Brace::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002568 if_false.to_tokens(tokens);
2569 });
2570 }
2571 }
2572 }
2573 }
2574
2575 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002576 impl ToTokens for ExprIf {
2577 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002578 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002579 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002580 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002581 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002582 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002583 }
2584 }
2585
Michael Layzell734adb42017-06-07 16:58:31 -04002586 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002587 impl ToTokens for ExprIfLet {
2588 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002589 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002590 self.if_token.to_tokens(tokens);
2591 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002592 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002593 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002594 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002595 self.if_true.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002596 maybe_wrap_else(tokens, &self.else_token, &self.if_false);
Alex Crichton62a0a592017-05-22 13:58:53 -07002597 }
2598 }
2599
Michael Layzell734adb42017-06-07 16:58:31 -04002600 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002601 impl ToTokens for ExprWhile {
2602 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002603 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002604 if self.label.is_some() {
2605 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002606 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002607 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002608 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002609 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002610 self.body.to_tokens(tokens);
2611 }
2612 }
2613
Michael Layzell734adb42017-06-07 16:58:31 -04002614 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002615 impl ToTokens for ExprWhileLet {
2616 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002617 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002618 if self.label.is_some() {
2619 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002620 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002621 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002622 self.while_token.to_tokens(tokens);
2623 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002624 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002625 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002626 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002627 self.body.to_tokens(tokens);
2628 }
2629 }
2630
Michael Layzell734adb42017-06-07 16:58:31 -04002631 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002632 impl ToTokens for ExprForLoop {
2633 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002634 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002635 if self.label.is_some() {
2636 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002637 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002638 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002639 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002640 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002641 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002642 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002643 self.body.to_tokens(tokens);
2644 }
2645 }
2646
Michael Layzell734adb42017-06-07 16:58:31 -04002647 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002648 impl ToTokens for ExprLoop {
2649 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002650 tokens.append_all(self.attrs.outer());
Michael Layzell3936ceb2017-07-08 00:28:36 -04002651 if self.label.is_some() {
2652 self.label.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07002653 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002654 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002655 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002656 self.body.to_tokens(tokens);
2657 }
2658 }
2659
Michael Layzell734adb42017-06-07 16:58:31 -04002660 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002661 impl ToTokens for ExprMatch {
2662 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002663 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002664 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002665 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002666 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002667 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002668 arm.to_tokens(tokens);
2669 // Ensure that we have a comma after a non-block arm, except
2670 // for the last one.
2671 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002672 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002673 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002674 }
2675 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002676 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002677 }
2678 }
2679
Michael Layzell734adb42017-06-07 16:58:31 -04002680 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002681 impl ToTokens for ExprCatch {
2682 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002683 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002684 self.do_token.to_tokens(tokens);
2685 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002686 self.block.to_tokens(tokens);
2687 }
2688 }
2689
Michael Layzell734adb42017-06-07 16:58:31 -04002690 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002691 impl ToTokens for ExprYield {
2692 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002693 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002694 self.yield_token.to_tokens(tokens);
2695 self.expr.to_tokens(tokens);
2696 }
2697 }
2698
2699 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002700 impl ToTokens for ExprClosure {
2701 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002702 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002703 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002704 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002705 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002706 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002707 FnArg::Captured(ArgCaptured {
2708 ref pat,
2709 ty: Type::Infer(_),
2710 ..
2711 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002712 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002713 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002714 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002715 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002716 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002717 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002718 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002719 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002720 self.body.to_tokens(tokens);
2721 }
2722 }
2723
Michael Layzell734adb42017-06-07 16:58:31 -04002724 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002725 impl ToTokens for ExprUnsafe {
2726 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002727 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002728 self.unsafe_token.to_tokens(tokens);
2729 self.block.to_tokens(tokens);
2730 }
2731 }
2732
2733 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002734 impl ToTokens for ExprBlock {
2735 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002736 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002737 self.block.to_tokens(tokens);
2738 }
2739 }
2740
Michael Layzell734adb42017-06-07 16:58:31 -04002741 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002742 impl ToTokens for ExprAssign {
2743 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002744 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002745 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002746 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002747 self.right.to_tokens(tokens);
2748 }
2749 }
2750
Michael Layzell734adb42017-06-07 16:58:31 -04002751 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002752 impl ToTokens for ExprAssignOp {
2753 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002754 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002755 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002756 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002757 self.right.to_tokens(tokens);
2758 }
2759 }
2760
Michael Layzell734adb42017-06-07 16:58:31 -04002761 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002762 impl ToTokens for ExprField {
2763 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002764 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002765 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002766 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002767 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002768 }
2769 }
2770
Michael Layzell734adb42017-06-07 16:58:31 -04002771 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002772 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002773 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002774 match *self {
2775 Member::Named(ident) => ident.to_tokens(tokens),
2776 Member::Unnamed(ref index) => index.to_tokens(tokens),
2777 }
2778 }
2779 }
2780
2781 #[cfg(feature = "full")]
2782 impl ToTokens for Index {
2783 fn to_tokens(&self, tokens: &mut Tokens) {
2784 tokens.append(TokenTree {
2785 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002786 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002787 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002788 }
2789 }
2790
2791 impl ToTokens for ExprIndex {
2792 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002793 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002794 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002795 self.bracket_token.surround(tokens, |tokens| {
2796 self.index.to_tokens(tokens);
2797 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002798 }
2799 }
2800
Michael Layzell734adb42017-06-07 16:58:31 -04002801 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002802 impl ToTokens for ExprRange {
2803 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002804 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002805 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002806 match self.limits {
2807 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2808 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2809 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002810 self.to.to_tokens(tokens);
2811 }
2812 }
2813
2814 impl ToTokens for ExprPath {
2815 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002816 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002817 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002818 }
2819 }
2820
Michael Layzell734adb42017-06-07 16:58:31 -04002821 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002822 impl ToTokens for ExprAddrOf {
2823 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002824 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002825 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002826 self.mutbl.to_tokens(tokens);
2827 self.expr.to_tokens(tokens);
2828 }
2829 }
2830
Michael Layzell734adb42017-06-07 16:58:31 -04002831 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002832 impl ToTokens for ExprBreak {
2833 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002834 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002835 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002836 self.label.to_tokens(tokens);
2837 self.expr.to_tokens(tokens);
2838 }
2839 }
2840
Michael Layzell734adb42017-06-07 16:58:31 -04002841 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002842 impl ToTokens for ExprContinue {
2843 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002844 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002845 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002846 self.label.to_tokens(tokens);
2847 }
2848 }
2849
Michael Layzell734adb42017-06-07 16:58:31 -04002850 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002851 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002852 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002853 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002854 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002855 self.expr.to_tokens(tokens);
2856 }
2857 }
2858
Michael Layzell734adb42017-06-07 16:58:31 -04002859 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002860 impl ToTokens for ExprMacro {
2861 fn to_tokens(&self, tokens: &mut Tokens) {
2862 tokens.append_all(self.attrs.outer());
2863 self.mac.to_tokens(tokens);
2864 }
2865 }
2866
2867 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002868 impl ToTokens for ExprStruct {
2869 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002870 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002871 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002872 self.brace_token.surround(tokens, |tokens| {
2873 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002874 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002875 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002876 self.rest.to_tokens(tokens);
2877 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002878 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002879 }
2880 }
2881
Michael Layzell734adb42017-06-07 16:58:31 -04002882 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002883 impl ToTokens for ExprRepeat {
2884 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002885 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002886 self.bracket_token.surround(tokens, |tokens| {
2887 self.expr.to_tokens(tokens);
2888 self.semi_token.to_tokens(tokens);
2889 self.amt.to_tokens(tokens);
2890 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002891 }
2892 }
2893
David Tolnaye98775f2017-12-28 23:17:00 -05002894 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002895 impl ToTokens for ExprGroup {
2896 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002897 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002898 self.group_token.surround(tokens, |tokens| {
2899 self.expr.to_tokens(tokens);
2900 });
2901 }
2902 }
2903
David Tolnaye98775f2017-12-28 23:17:00 -05002904 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002905 impl ToTokens for ExprParen {
2906 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002907 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002908 self.paren_token.surround(tokens, |tokens| {
2909 self.expr.to_tokens(tokens);
2910 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002911 }
2912 }
2913
Michael Layzell734adb42017-06-07 16:58:31 -04002914 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002915 impl ToTokens for ExprTry {
2916 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002917 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002919 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002920 }
2921 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002922
Michael Layzell734adb42017-06-07 16:58:31 -04002923 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002924 impl ToTokens for FieldValue {
2925 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002926 self.member.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002927 // XXX: Override self.is_shorthand if expr is not an IdentExpr with
2928 // the ident self.ident?
David Tolnay276690f2016-10-30 12:06:59 -07002929 if !self.is_shorthand {
Alex Crichton259ee532017-07-14 06:51:02 -07002930 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002931 self.expr.to_tokens(tokens);
2932 }
David Tolnay055a7042016-10-02 19:23:54 -07002933 }
2934 }
2935
Michael Layzell734adb42017-06-07 16:58:31 -04002936 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002937 impl ToTokens for Arm {
2938 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002939 tokens.append_all(&self.attrs);
2940 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002941 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002942 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002943 self.guard.to_tokens(tokens);
2944 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002945 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002946 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002947 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002948 }
2949 }
2950
Michael Layzell734adb42017-06-07 16:58:31 -04002951 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002952 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07002953 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 self.underscore_token.to_tokens(tokens);
2955 }
2956 }
2957
Michael Layzell734adb42017-06-07 16:58:31 -04002958 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002959 impl ToTokens for PatIdent {
2960 fn to_tokens(&self, tokens: &mut Tokens) {
2961 self.mode.to_tokens(tokens);
2962 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002963 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002964 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002965 self.subpat.to_tokens(tokens);
2966 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002967 }
2968 }
2969
Michael Layzell734adb42017-06-07 16:58:31 -04002970 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002971 impl ToTokens for PatStruct {
2972 fn to_tokens(&self, tokens: &mut Tokens) {
2973 self.path.to_tokens(tokens);
2974 self.brace_token.surround(tokens, |tokens| {
2975 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002976 // NOTE: We need a comma before the dot2 token if it is present.
2977 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002978 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002979 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002980 self.dot2_token.to_tokens(tokens);
2981 });
2982 }
2983 }
2984
Michael Layzell734adb42017-06-07 16:58:31 -04002985 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002986 impl ToTokens for PatTupleStruct {
2987 fn to_tokens(&self, tokens: &mut Tokens) {
2988 self.path.to_tokens(tokens);
2989 self.pat.to_tokens(tokens);
2990 }
2991 }
2992
Michael Layzell734adb42017-06-07 16:58:31 -04002993 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002994 impl ToTokens for PatPath {
2995 fn to_tokens(&self, tokens: &mut Tokens) {
2996 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
2997 }
2998 }
2999
Michael Layzell734adb42017-06-07 16:58:31 -04003000 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003001 impl ToTokens for PatTuple {
3002 fn to_tokens(&self, tokens: &mut Tokens) {
3003 self.paren_token.surround(tokens, |tokens| {
3004 for (i, token) in self.pats.iter().enumerate() {
3005 if Some(i) == self.dots_pos {
Alex Crichton259ee532017-07-14 06:51:02 -07003006 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
3007 TokensOrDefault(&self.comma_token).to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003008 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003009 token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003010 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003011
3012 if Some(self.pats.len()) == self.dots_pos {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003013 // Ensure there is a comma before the .. token.
3014 if !self.pats.empty_or_trailing() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003015 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003016 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003017 self.dot2_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003018 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003019 });
3020 }
3021 }
3022
Michael Layzell734adb42017-06-07 16:58:31 -04003023 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003024 impl ToTokens for PatBox {
3025 fn to_tokens(&self, tokens: &mut Tokens) {
3026 self.box_token.to_tokens(tokens);
3027 self.pat.to_tokens(tokens);
3028 }
3029 }
3030
Michael Layzell734adb42017-06-07 16:58:31 -04003031 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003032 impl ToTokens for PatRef {
3033 fn to_tokens(&self, tokens: &mut Tokens) {
3034 self.and_token.to_tokens(tokens);
3035 self.mutbl.to_tokens(tokens);
3036 self.pat.to_tokens(tokens);
3037 }
3038 }
3039
Michael Layzell734adb42017-06-07 16:58:31 -04003040 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003041 impl ToTokens for PatLit {
3042 fn to_tokens(&self, tokens: &mut Tokens) {
3043 self.expr.to_tokens(tokens);
3044 }
3045 }
3046
Michael Layzell734adb42017-06-07 16:58:31 -04003047 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003048 impl ToTokens for PatRange {
3049 fn to_tokens(&self, tokens: &mut Tokens) {
3050 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003051 match self.limits {
3052 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3053 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3054 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003055 self.hi.to_tokens(tokens);
3056 }
3057 }
3058
Michael Layzell734adb42017-06-07 16:58:31 -04003059 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003060 impl ToTokens for PatSlice {
3061 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003062 // XXX: This is a mess, and it will be so easy to screw it up. How
3063 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003064 self.bracket_token.surround(tokens, |tokens| {
3065 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003066
3067 // If we need a comma before the middle or standalone .. token,
3068 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003069 if !self.front.empty_or_trailing()
3070 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003071 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003072 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003073 }
3074
3075 // If we have an identifier, we always need a .. token.
3076 if self.middle.is_some() {
3077 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003078 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003079 } else if self.dot2_token.is_some() {
3080 self.dot2_token.to_tokens(tokens);
3081 }
3082
3083 // Make sure we have a comma before the back half.
3084 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003085 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003086 self.back.to_tokens(tokens);
3087 } else {
3088 self.comma_token.to_tokens(tokens);
3089 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003090 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003091 }
3092 }
3093
Michael Layzell734adb42017-06-07 16:58:31 -04003094 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003095 impl ToTokens for FieldPat {
3096 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003097 // XXX: Override is_shorthand if it was wrong?
David Tolnay8d9e81a2016-10-03 22:36:32 -07003098 if !self.is_shorthand {
David Tolnay85b69a42017-12-27 20:43:10 -05003099 self.member.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003100 TokensOrDefault(&self.colon_token).to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003101 }
3102 self.pat.to_tokens(tokens);
3103 }
3104 }
3105
Michael Layzell734adb42017-06-07 16:58:31 -04003106 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003107 impl ToTokens for BindingMode {
3108 fn to_tokens(&self, tokens: &mut Tokens) {
3109 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003110 BindingMode::ByRef(ref t, ref m) => {
3111 t.to_tokens(tokens);
3112 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003113 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003114 BindingMode::ByValue(ref m) => {
3115 m.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003116 }
3117 }
3118 }
3119 }
David Tolnay42602292016-10-01 22:25:45 -07003120
Michael Layzell734adb42017-06-07 16:58:31 -04003121 #[cfg(feature = "full")]
David Tolnay89e05672016-10-02 14:39:42 -07003122 impl ToTokens for CaptureBy {
3123 fn to_tokens(&self, tokens: &mut Tokens) {
3124 match *self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003125 CaptureBy::Value(ref t) => t.to_tokens(tokens),
David Tolnaydaaf7742016-10-03 11:11:43 -07003126 CaptureBy::Ref => {
3127 // nothing
3128 }
David Tolnay89e05672016-10-02 14:39:42 -07003129 }
3130 }
3131 }
3132
Michael Layzell734adb42017-06-07 16:58:31 -04003133 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003134 impl ToTokens for Block {
3135 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003136 self.brace_token.surround(tokens, |tokens| {
3137 tokens.append_all(&self.stmts);
3138 });
David Tolnay42602292016-10-01 22:25:45 -07003139 }
3140 }
3141
Michael Layzell734adb42017-06-07 16:58:31 -04003142 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003143 impl ToTokens for Stmt {
3144 fn to_tokens(&self, tokens: &mut Tokens) {
3145 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003146 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003147 Stmt::Item(ref item) => item.to_tokens(tokens),
3148 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003149 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003150 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003151 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003152 }
David Tolnay42602292016-10-01 22:25:45 -07003153 }
3154 }
3155 }
David Tolnay191e0582016-10-02 18:31:09 -07003156
Michael Layzell734adb42017-06-07 16:58:31 -04003157 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003158 impl ToTokens for Local {
3159 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003160 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003162 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003163 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003164 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003165 self.ty.to_tokens(tokens);
3166 }
3167 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003168 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003169 self.init.to_tokens(tokens);
3170 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003171 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003172 }
3173 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003174}