blob: 0cc2d30b0e5bda13029f6fb4f5ba7e759d9b5f86 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -05002use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -05003use proc_macro2::{Span, TokenStream};
David Tolnay14982012017-12-29 00:49:51 -05004#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -05005use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -05006#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -05007use tt::TokenStreamHelper;
David Tolnay2ae520a2017-12-29 11:19:50 -05008#[cfg(feature = "full")]
9use std::mem;
David Tolnayf4bbbd92016-09-23 14:41:55 -070010
Alex Crichton62a0a592017-05-22 13:58:53 -070011ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -050012 /// An expression.
13 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040015 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050016 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080017 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050018 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070019 }),
Clar Charrd22b5702017-03-10 15:24:56 -050020
David Tolnay8701a5c2017-12-28 23:31:10 -050021 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040022 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050023 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050025 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub value: Box<Expr>,
27 }),
Clar Charrd22b5702017-03-10 15:24:56 -050028
Alex Crichton62a0a592017-05-22 13:58:53 -070029 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040030 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050031 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050032 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -050033 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
Clar Charrd22b5702017-03-10 15:24:56 -050035
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// A function call.
37 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050038 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050040 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050041 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
Clar Charrd22b5702017-03-10 15:24:56 -050043
Alex Crichton62a0a592017-05-22 13:58:53 -070044 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
45 ///
46 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080047 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070048 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040049 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050050 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -050051 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050053 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -050054 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050055 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050056 pub args: Punctuated<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 Tolnayf2cfd722017-12-31 18:02:51 -050063 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }),
Clar Charrd22b5702017-03-10 15:24:56 -050065
Alex Crichton62a0a592017-05-22 13:58:53 -070066 /// A binary operation, e.g. `a + b`, `a * b`.
67 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050070 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub right: Box<Expr>,
72 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A unary operation, e.g. `!x`, `*x`.
75 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub op: UnOp,
78 pub expr: Box<Expr>,
79 }),
Clar Charrd22b5702017-03-10 15:24:56 -050080
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050082 pub Lit(ExprLit {
83 pub attrs: Vec<Attribute>,
84 pub lit: Lit,
85 }),
Clar Charrd22b5702017-03-10 15:24:56 -050086
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A cast, e.g. `foo as f64`.
88 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050089 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A type ascription, e.g. `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -050096 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -050097 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800100 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500102
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 /// An `if` block, with an optional else block
104 ///
105 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400106 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500110 pub then_branch: Block,
111 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500113
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 /// An `if let` expression with an optional else block
115 ///
116 /// E.g., `if let pat = expr { block } else { expr }`
117 ///
118 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800121 pub if_token: Token![if],
122 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500123 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800124 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500125 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500126 pub then_branch: Block,
127 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500129
Alex Crichton62a0a592017-05-22 13:58:53 -0700130 /// A while loop, with an optional label
131 ///
132 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400133 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500134 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500135 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800136 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500137 pub cond: Box<Expr>,
138 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700139 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500140
Alex Crichton62a0a592017-05-22 13:58:53 -0700141 /// A while-let loop, with an optional label.
142 ///
143 /// E.g., `'label: while let pat = expr { block }`
144 ///
145 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400146 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500147 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500148 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 pub while_token: Token![while],
150 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500151 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub expr: Box<Expr>,
154 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500156
Alex Crichton62a0a592017-05-22 13:58:53 -0700157 /// A for loop, with an optional label.
158 ///
159 /// E.g., `'label: for pat in expr { block }`
160 ///
161 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400162 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500163 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500164 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500165 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500167 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700168 pub expr: Box<Expr>,
169 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700170 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500171
Alex Crichton62a0a592017-05-22 13:58:53 -0700172 /// Conditionless loop with an optional label.
173 ///
174 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400175 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500176 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500177 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500178 pub loop_token: Token![loop],
179 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500181
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400183 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800185 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700186 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500187 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 pub arms: Vec<Arm>,
189 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500190
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400192 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500193 pub attrs: Vec<Attribute>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500194 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800195 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500196 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800197 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500198 pub output: ReturnType,
199 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500201
Nika Layzell640832a2017-12-04 13:37:09 -0500202 /// An unsafe block (`unsafe { ... }`)
203 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500204 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500205 pub unsafe_token: Token![unsafe],
206 pub block: Block,
207 }),
208
209 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400210 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500211 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700212 pub block: Block,
213 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700214
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400216 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500217 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800219 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500220 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223 /// An assignment with an operator
224 ///
225 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700228 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500229 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700230 pub right: Box<Expr>,
231 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500232
David Tolnay85b69a42017-12-27 20:43:10 -0500233 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
234 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400235 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500236 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500237 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800238 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500239 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500241
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 /// An indexing operation (`foo[2]`)
243 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500244 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500246 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500249
David Tolnaybe55d7b2017-12-17 23:41:20 -0800250 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400251 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500252 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500255 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700257
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 /// Variable reference, possibly containing `::` and/or type
259 /// parameters, e.g. foo::bar::<baz>.
260 ///
261 /// Optionally "qualified",
262 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
263 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500264 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 pub qself: Option<QSelf>,
266 pub path: Path,
267 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700268
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400270 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500271 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500273 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 pub expr: Box<Expr>,
275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400278 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500279 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500280 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700281 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 pub expr: Option<Box<Expr>>,
283 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500284
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400286 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500287 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500289 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500291
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500293 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700298
Alex Crichton62a0a592017-05-22 13:58:53 -0700299 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500300 pub Macro(ExprMacro #full {
301 pub attrs: Vec<Attribute>,
302 pub mac: Macro,
303 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A struct literal expression.
306 ///
307 /// For example, `Foo {x: 1, y: 2}`, or
308 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400309 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500310 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500312 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500313 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500314 pub dot2_token: Option<Token![..]>,
315 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700316 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700317
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 /// An array literal constructed from one repeated element.
319 ///
320 /// For example, `[1; 5]`. The first expression is the element
321 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400322 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500323 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500324 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700325 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500326 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 pub amt: Box<Expr>,
328 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700329
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500331 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500332 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500333 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500334 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700336
Michael Layzell93c36282017-06-04 20:43:14 -0400337 /// No-op: used solely so we can pretty-print faithfully
338 ///
339 /// A `group` represents a `None`-delimited span in the input
340 /// `TokenStream` which affects the precidence of the resulting
341 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500342 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500343 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500344 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500345 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400346 }),
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400349 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500350 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800352 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700354
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 /// A catch expression.
356 ///
357 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400358 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500359 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub do_token: Token![do],
361 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 pub block: Block,
363 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700364
365 /// A yield expression.
366 ///
367 /// E.g. `yield expr`
368 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500369 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700371 pub expr: Option<Box<Expr>>,
372 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500373
374 pub Verbatim(ExprVerbatim #manual_extra_traits {
375 pub tts: TokenStream,
376 }),
377 }
378}
379
380#[cfg(feature = "extra-traits")]
381impl Eq for ExprVerbatim {}
382
383#[cfg(feature = "extra-traits")]
384impl PartialEq for ExprVerbatim {
385 fn eq(&self, other: &Self) -> bool {
386 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
387 }
388}
389
390#[cfg(feature = "extra-traits")]
391impl Hash for ExprVerbatim {
392 fn hash<H>(&self, state: &mut H)
393 where
394 H: Hasher,
395 {
396 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700397 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700398}
399
David Tolnay8c91b882017-12-28 23:04:32 -0500400impl Expr {
401 // Not public API.
402 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500403 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500404 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500405 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800406 Expr::Box(ExprBox { ref mut attrs, .. })
407 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
408 | Expr::Array(ExprArray { ref mut attrs, .. })
409 | Expr::Call(ExprCall { ref mut attrs, .. })
410 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
411 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
412 | Expr::Binary(ExprBinary { ref mut attrs, .. })
413 | Expr::Unary(ExprUnary { ref mut attrs, .. })
414 | Expr::Lit(ExprLit { ref mut attrs, .. })
415 | Expr::Cast(ExprCast { ref mut attrs, .. })
416 | Expr::Type(ExprType { ref mut attrs, .. })
417 | Expr::If(ExprIf { ref mut attrs, .. })
418 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
419 | Expr::While(ExprWhile { ref mut attrs, .. })
420 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
421 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
422 | Expr::Loop(ExprLoop { ref mut attrs, .. })
423 | Expr::Match(ExprMatch { ref mut attrs, .. })
424 | Expr::Closure(ExprClosure { ref mut attrs, .. })
425 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
426 | Expr::Block(ExprBlock { ref mut attrs, .. })
427 | Expr::Assign(ExprAssign { ref mut attrs, .. })
428 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
429 | Expr::Field(ExprField { ref mut attrs, .. })
430 | Expr::Index(ExprIndex { ref mut attrs, .. })
431 | Expr::Range(ExprRange { ref mut attrs, .. })
432 | Expr::Path(ExprPath { ref mut attrs, .. })
433 | Expr::AddrOf(ExprAddrOf { ref mut attrs, .. })
434 | Expr::Break(ExprBreak { ref mut attrs, .. })
435 | Expr::Continue(ExprContinue { ref mut attrs, .. })
436 | Expr::Return(ExprReturn { ref mut attrs, .. })
437 | Expr::Macro(ExprMacro { ref mut attrs, .. })
438 | Expr::Struct(ExprStruct { ref mut attrs, .. })
439 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
440 | Expr::Paren(ExprParen { ref mut attrs, .. })
441 | Expr::Group(ExprGroup { ref mut attrs, .. })
442 | Expr::Try(ExprTry { ref mut attrs, .. })
443 | Expr::Catch(ExprCatch { ref mut attrs, .. })
444 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500445 Expr::Verbatim(_) => {
446 // TODO
447 Vec::new()
448 }
David Tolnay8c91b882017-12-28 23:04:32 -0500449 }
450 }
451}
452
David Tolnay85b69a42017-12-27 20:43:10 -0500453ast_enum! {
454 /// A struct or tuple struct field accessed in a struct literal or field
455 /// expression.
456 pub enum Member {
457 /// A named field like `self.x`.
458 Named(Ident),
459 /// An unnamed field like `self.0`.
460 Unnamed(Index),
461 }
462}
463
David Tolnay85b69a42017-12-27 20:43:10 -0500464ast_struct! {
465 /// The index of an unnamed tuple struct field.
466 pub struct Index #manual_extra_traits {
467 pub index: u32,
468 pub span: Span,
469 }
470}
471
David Tolnay14982012017-12-29 00:49:51 -0500472impl From<usize> for Index {
473 fn from(index: usize) -> Index {
474 assert!(index < std::u32::MAX as usize);
475 Index {
476 index: index as u32,
477 span: Span::default(),
478 }
479 }
480}
481
482#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500483impl Eq for Index {}
484
David Tolnay14982012017-12-29 00:49:51 -0500485#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500486impl PartialEq for Index {
487 fn eq(&self, other: &Self) -> bool {
488 self.index == other.index
489 }
490}
491
David Tolnay14982012017-12-29 00:49:51 -0500492#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500493impl Hash for Index {
494 fn hash<H: Hasher>(&self, state: &mut H) {
495 self.index.hash(state);
496 }
497}
498
499#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700500ast_struct! {
David Tolnayd60cfec2017-12-29 00:21:38 -0500501 pub struct MethodTurbofish {
502 pub colon2_token: Token![::],
503 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500504 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500505 pub gt_token: Token![>],
506 }
507}
508
509#[cfg(feature = "full")]
510ast_enum! {
511 /// A individual generic argument like `T`.
512 pub enum GenericMethodArgument {
513 /// The type parameters for this path segment, if present.
514 Type(Type),
515 /// Const expression. Must be inside of a block.
516 ///
517 /// NOTE: Identity expressions are represented as Type arguments, as
518 /// they are indistinguishable syntactically.
519 Const(Expr),
520 }
521}
522
523#[cfg(feature = "full")]
524ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 /// A field-value pair in a struct literal.
526 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500527 /// Attributes tagged on the field.
528 pub attrs: Vec<Attribute>,
529
530 /// Name or index of the field.
531 pub member: Member,
532
David Tolnay5d7098a2017-12-29 01:35:24 -0500533 /// The colon in `Struct { x: x }`. If written in shorthand like
534 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500535 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500536
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 /// Value of the field.
538 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700539 }
David Tolnay055a7042016-10-02 19:23:54 -0700540}
541
Michael Layzell734adb42017-06-07 16:58:31 -0400542#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700543ast_struct! {
David Tolnaybcd498f2017-12-29 12:02:33 -0500544 pub struct Label {
545 pub name: Lifetime,
546 pub colon_token: Token![:],
547 }
548}
549
550#[cfg(feature = "full")]
551ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700552 /// A Block (`{ .. }`).
553 ///
554 /// E.g. `{ .. }` as in `fn foo() { .. }`
555 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500556 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700557 /// Statements in a block
558 pub stmts: Vec<Stmt>,
559 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700560}
561
Michael Layzell734adb42017-06-07 16:58:31 -0400562#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700563ast_enum! {
564 /// A statement, usually ending in a semicolon.
565 pub enum Stmt {
566 /// A local (let) binding.
567 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700568
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 /// An item definition.
570 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700571
Alex Crichton62a0a592017-05-22 13:58:53 -0700572 /// Expr without trailing semicolon.
573 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700574
Alex Crichton62a0a592017-05-22 13:58:53 -0700575 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800576 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700577 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700578}
579
Michael Layzell734adb42017-06-07 16:58:31 -0400580#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700581ast_struct! {
582 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
583 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500584 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800585 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700586 pub pat: Box<Pat>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500587 pub ty: Option<(Token![:], Box<Type>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 /// Initializer expression to set the value, if any
David Tolnay8b4d3022017-12-29 12:11:10 -0500589 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500590 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700591 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700592}
593
Michael Layzell734adb42017-06-07 16:58:31 -0400594#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700595ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700596 // Clippy false positive
597 // https://github.com/Manishearth/rust-clippy/issues/1241
598 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
599 pub enum Pat {
600 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700601 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800602 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700604
Alex Crichton62a0a592017-05-22 13:58:53 -0700605 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
606 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
607 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
608 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700609 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500610 pub by_ref: Option<Token![ref]>,
611 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700612 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500613 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700614 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700615
Alex Crichton62a0a592017-05-22 13:58:53 -0700616 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
617 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700618 pub Struct(PatStruct {
619 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500620 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500621 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700623 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700624
Alex Crichton62a0a592017-05-22 13:58:53 -0700625 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
626 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
627 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700628 pub TupleStruct(PatTupleStruct {
629 pub path: Path,
630 pub pat: PatTuple,
631 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700632
Alex Crichton62a0a592017-05-22 13:58:53 -0700633 /// A possibly qualified path pattern.
634 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
635 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
636 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700637 pub Path(PatPath {
638 pub qself: Option<QSelf>,
639 pub path: Path,
640 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700641
Alex Crichton62a0a592017-05-22 13:58:53 -0700642 /// A tuple pattern `(a, b)`.
643 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
644 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700645 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500646 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500647 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500648 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500649 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500650 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700651 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700652 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700653 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800654 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500655 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700656 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700657 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700658 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800659 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500660 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500661 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700662 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700663 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700664 pub Lit(PatLit {
665 pub expr: Box<Expr>,
666 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800667 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700668 pub Range(PatRange {
669 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700670 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500671 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700672 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400673 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700674 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500675 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500676 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700677 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500678 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500679 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500680 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700681 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700682 /// A macro pattern; pre-expansion
David Tolnay323279a2017-12-29 11:26:32 -0500683 pub Macro(PatMacro {
684 pub mac: Macro,
685 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500686 pub Verbatim(PatVerbatim #manual_extra_traits {
687 pub tts: TokenStream,
688 }),
689 }
690}
691
David Tolnayc43b44e2017-12-30 23:55:54 -0500692#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500693impl Eq for PatVerbatim {}
694
David Tolnayc43b44e2017-12-30 23:55:54 -0500695#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500696impl PartialEq for PatVerbatim {
697 fn eq(&self, other: &Self) -> bool {
698 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
699 }
700}
701
David Tolnayc43b44e2017-12-30 23:55:54 -0500702#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500703impl Hash for PatVerbatim {
704 fn hash<H>(&self, state: &mut H)
705 where
706 H: Hasher,
707 {
708 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700710}
711
Michael Layzell734adb42017-06-07 16:58:31 -0400712#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700713ast_struct! {
714 /// An arm of a 'match'.
715 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800716 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700717 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500718 /// ```rust
719 /// # #![feature(dotdoteq_in_patterns)]
720 /// #
721 /// # fn main() {
722 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500724 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500726 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700727 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500728 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700729 /// ```
730 pub struct Arm {
731 pub attrs: Vec<Attribute>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500732 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500733 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800734 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800736 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700737 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700738}
739
Michael Layzell734adb42017-06-07 16:58:31 -0400740#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700741ast_enum! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700742 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700743 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 pub enum RangeLimits {
745 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800746 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700747 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800748 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700750}
751
Michael Layzell734adb42017-06-07 16:58:31 -0400752#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700753ast_struct! {
754 /// A single field in a struct pattern
755 ///
756 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay5d7098a2017-12-29 01:35:24 -0500757 /// are treated the same as `x: x, y: ref y, z: ref mut z` but
758 /// there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700759 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500760 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700761 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500762 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500763 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700764 /// The pattern the field is destructured to
765 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700766 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700767}
768
Michael Layzell3936ceb2017-07-08 00:28:36 -0400769#[cfg(any(feature = "parsing", feature = "printing"))]
770#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700771fn arm_expr_requires_comma(expr: &Expr) -> bool {
772 // see https://github.com/rust-lang/rust/blob/eb8f2586e
773 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500774 match *expr {
775 Expr::Unsafe(..)
776 | Expr::Block(..)
777 | Expr::If(..)
778 | Expr::IfLet(..)
779 | Expr::Match(..)
780 | Expr::While(..)
781 | Expr::WhileLet(..)
782 | Expr::Loop(..)
783 | Expr::ForLoop(..)
784 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700785 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400786 }
787}
788
David Tolnayb9c8e322016-09-23 20:48:37 -0700789#[cfg(feature = "parsing")]
790pub mod parsing {
791 use super::*;
David Tolnay056de302018-01-05 14:29:05 -0800792 use path::parsing::qpath;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500793 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -0800794 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700795
Michael Layzell734adb42017-06-07 16:58:31 -0400796 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -0800797 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500798 use synom::Synom;
799 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400800 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500801 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500802 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700803
David Tolnaybcf26022017-12-25 22:10:52 -0500804 // When we're parsing expressions which occur before blocks, like in an if
805 // statement's condition, we cannot parse a struct literal.
806 //
807 // Struct literals are ambiguous in certain positions
808 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700809 macro_rules! ambiguous_expr {
810 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700811 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700812 };
813 }
814
David Tolnaybcf26022017-12-25 22:10:52 -0500815 // When we are parsing an optional suffix expression, we cannot allow blocks
816 // if structs are not allowed.
817 //
818 // Example:
819 //
820 // if break {} {}
821 //
822 // is ambiguous between:
823 //
824 // if (break {}) {}
825 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400826 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400827 macro_rules! opt_ambiguous_expr {
828 ($i:expr, $allow_struct:ident) => {
829 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
830 };
831 }
832
Alex Crichton954046c2017-05-30 21:49:42 -0700833 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400834 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700835
836 fn description() -> Option<&'static str> {
837 Some("expression")
838 }
839 }
840
Michael Layzell734adb42017-06-07 16:58:31 -0400841 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700842 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
843
David Tolnaybcf26022017-12-25 22:10:52 -0500844 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400845 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500846 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500847 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400848 }
849
Michael Layzell734adb42017-06-07 16:58:31 -0400850 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500851 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500852 // NOTE: We intentionally skip assign_expr, placement_expr, and
853 // range_expr, as they are not parsed in non-full mode.
854 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400855 }
856
David Tolnaybcf26022017-12-25 22:10:52 -0500857 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400858 macro_rules! binop {
859 (
860 $name: ident,
861 $next: ident,
862 $submac: ident!( $($args:tt)* )
863 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500864 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400865 mut e: call!($next, allow_struct, allow_block) >>
866 many0!(do_parse!(
867 op: $submac!($($args)*) >>
868 rhs: call!($next, allow_struct, true) >>
869 ({
870 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500871 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400872 left: Box::new(e.into()),
873 op: op,
874 right: Box::new(rhs.into()),
875 }.into();
876 })
877 )) >>
878 (e)
879 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700880 }
David Tolnay54e854d2016-10-24 12:03:30 -0700881 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700882
David Tolnaybcf26022017-12-25 22:10:52 -0500883 // <placement> = <placement> ..
884 // <placement> += <placement> ..
885 // <placement> -= <placement> ..
886 // <placement> *= <placement> ..
887 // <placement> /= <placement> ..
888 // <placement> %= <placement> ..
889 // <placement> ^= <placement> ..
890 // <placement> &= <placement> ..
891 // <placement> |= <placement> ..
892 // <placement> <<= <placement> ..
893 // <placement> >>= <placement> ..
894 //
895 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400896 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500897 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400898 mut e: call!(placement_expr, allow_struct, allow_block) >>
899 alt!(
900 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800901 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400902 // Recurse into self to parse right-associative operator.
903 rhs: call!(assign_expr, allow_struct, true) >>
904 ({
905 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500906 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500907 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400908 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -0500909 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400910 }.into();
911 })
912 )
913 |
914 do_parse!(
915 op: call!(BinOp::parse_assign_op) >>
916 // Recurse into self to parse right-associative operator.
917 rhs: call!(assign_expr, allow_struct, true) >>
918 ({
919 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500920 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500921 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400922 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -0500923 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400924 }.into();
925 })
926 )
927 |
928 epsilon!()
929 ) >>
930 (e)
931 ));
932
David Tolnaybcf26022017-12-25 22:10:52 -0500933 // <range> <- <range> ..
934 //
935 // NOTE: The `in place { expr }` version of this syntax is parsed in
936 // `atom_expr`, not here.
937 //
938 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400939 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500940 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400941 mut e: call!(range_expr, allow_struct, allow_block) >>
942 alt!(
943 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800944 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400945 // Recurse into self to parse right-associative operator.
946 rhs: call!(placement_expr, allow_struct, true) >>
947 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400948 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500949 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400950 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -0500951 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -0500952 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -0500953 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400954 }.into();
955 })
956 )
957 |
958 epsilon!()
959 ) >>
960 (e)
961 ));
962
David Tolnaybcf26022017-12-25 22:10:52 -0500963 // <or> ... <or> ..
964 // <or> .. <or> ..
965 // <or> ..
966 //
967 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
968 // rules are for parsing these expressions are, but this is not correct.
969 // For example, `a .. b .. c` is not a legal expression. It should not
970 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
971 //
972 // NOTE: The form of ranges which don't include a preceding expression are
973 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400974 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500975 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400976 mut e: call!(or_expr, allow_struct, allow_block) >>
977 many0!(do_parse!(
978 limits: syn!(RangeLimits) >>
979 // We don't want to allow blocks here if we don't allow structs. See
980 // the reasoning for `opt_ambiguous_expr!` above.
981 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
982 ({
983 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500984 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500985 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400986 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -0500987 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400988 }.into();
989 })
990 )) >>
991 (e)
992 ));
993
David Tolnaybcf26022017-12-25 22:10:52 -0500994 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800995 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400996
David Tolnaybcf26022017-12-25 22:10:52 -0500997 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800998 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400999
David Tolnaybcf26022017-12-25 22:10:52 -05001000 // <bitor> == <bitor> ...
1001 // <bitor> != <bitor> ...
1002 // <bitor> >= <bitor> ...
1003 // <bitor> <= <bitor> ...
1004 // <bitor> > <bitor> ...
1005 // <bitor> < <bitor> ...
1006 //
1007 // NOTE: This operator appears to be parsed as left-associative, but errors
1008 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001009 binop!(
1010 compare_expr,
1011 bitor_expr,
1012 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001013 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001014 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016 |
1017 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001018 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001019 |
1020 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001021 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001022 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001023 do_parse!(
1024 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001025 not!(punct!(<-)) >>
1026 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001027 (BinOp::Lt(t))
1028 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001029 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001030 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001031 )
1032 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001033
David Tolnaybcf26022017-12-25 22:10:52 -05001034 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001035 binop!(
1036 bitor_expr,
1037 bitxor_expr,
1038 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1039 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001040
David Tolnaybcf26022017-12-25 22:10:52 -05001041 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001042 binop!(
1043 bitxor_expr,
1044 bitand_expr,
1045 do_parse!(
1046 // NOTE: Make sure we aren't looking at ^=.
1047 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1048 )
1049 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001050
David Tolnaybcf26022017-12-25 22:10:52 -05001051 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001052 binop!(
1053 bitand_expr,
1054 shift_expr,
1055 do_parse!(
1056 // NOTE: Make sure we aren't looking at && or &=.
1057 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1058 )
1059 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001060
David Tolnaybcf26022017-12-25 22:10:52 -05001061 // <arith> << <arith> ...
1062 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001063 binop!(
1064 shift_expr,
1065 arith_expr,
1066 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001067 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001068 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001069 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001070 )
1071 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001072
David Tolnaybcf26022017-12-25 22:10:52 -05001073 // <term> + <term> ...
1074 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001075 binop!(
1076 arith_expr,
1077 term_expr,
1078 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001079 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001080 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001081 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001082 )
1083 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001084
David Tolnaybcf26022017-12-25 22:10:52 -05001085 // <cast> * <cast> ...
1086 // <cast> / <cast> ...
1087 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001088 binop!(
1089 term_expr,
1090 cast_expr,
1091 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001092 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001093 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001094 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001095 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001096 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001097 )
1098 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001099
David Tolnaybcf26022017-12-25 22:10:52 -05001100 // <unary> as <ty>
1101 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001102 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001103 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001104 mut e: call!(unary_expr, allow_struct, allow_block) >>
1105 many0!(alt!(
1106 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001107 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001108 // We can't accept `A + B` in cast expressions, as it's
1109 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001110 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001111 ({
1112 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001113 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001114 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001115 as_token: as_,
1116 ty: Box::new(ty),
1117 }.into();
1118 })
1119 )
1120 |
1121 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001122 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001123 // We can't accept `A + B` in cast expressions, as it's
1124 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001125 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001126 ({
1127 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001128 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001129 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001130 colon_token: colon,
1131 ty: Box::new(ty),
1132 }.into();
1133 })
1134 )
1135 )) >>
1136 (e)
1137 ));
1138
David Tolnay0cf94f22017-12-28 23:46:26 -05001139 // <unary> as <ty>
1140 #[cfg(not(feature = "full"))]
1141 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1142 mut e: call!(unary_expr, allow_struct, allow_block) >>
1143 many0!(do_parse!(
1144 as_: keyword!(as) >>
1145 // We can't accept `A + B` in cast expressions, as it's
1146 // ambiguous with the + expression.
1147 ty: call!(Type::without_plus) >>
1148 ({
1149 e = ExprCast {
1150 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001151 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001152 as_token: as_,
1153 ty: Box::new(ty),
1154 }.into();
1155 })
1156 )) >>
1157 (e)
1158 ));
1159
David Tolnaybcf26022017-12-25 22:10:52 -05001160 // <UnOp> <trailer>
1161 // & <trailer>
1162 // &mut <trailer>
1163 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001164 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001165 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001166 do_parse!(
1167 op: syn!(UnOp) >>
1168 expr: call!(unary_expr, allow_struct, true) >>
1169 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001170 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001171 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001172 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 }.into())
1174 )
1175 |
1176 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001177 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001178 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179 expr: call!(unary_expr, allow_struct, true) >>
1180 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001181 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001182 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001183 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001184 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001185 }.into())
1186 )
1187 |
1188 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001189 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001190 expr: call!(unary_expr, allow_struct, true) >>
1191 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001192 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001193 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001194 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001195 }.into())
1196 )
1197 |
1198 call!(trailer_expr, allow_struct, allow_block)
1199 ));
1200
Michael Layzell734adb42017-06-07 16:58:31 -04001201 // XXX: This duplication is ugly
1202 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001203 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001204 do_parse!(
1205 op: syn!(UnOp) >>
1206 expr: call!(unary_expr, allow_struct, true) >>
1207 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001208 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001209 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001210 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001211 }.into())
1212 )
1213 |
1214 call!(trailer_expr, allow_struct, allow_block)
1215 ));
1216
David Tolnaybcf26022017-12-25 22:10:52 -05001217 // <atom> (..<args>) ...
1218 // <atom> . <ident> (..<args>) ...
1219 // <atom> . <ident> ...
1220 // <atom> . <lit> ...
1221 // <atom> [ <expr> ] ...
1222 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001223 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001224 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001225 mut e: call!(atom_expr, allow_struct, allow_block) >>
1226 many0!(alt!(
1227 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001228 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001229 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001230 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001231 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001232 args: args,
1233 paren_token: paren,
1234 }.into();
1235 })
1236 |
1237 tap!(more: and_method_call => {
1238 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001239 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001240 e = call.into();
1241 })
1242 |
1243 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001244 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001245 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001246 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001247 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001249 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 }.into();
1251 })
1252 |
1253 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001254 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001255 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001256 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001257 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001258 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 index: Box::new(i),
1260 }.into();
1261 })
1262 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001263 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001264 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001265 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001266 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267 question_token: question,
1268 }.into();
1269 })
1270 )) >>
1271 (e)
1272 ));
1273
Michael Layzell734adb42017-06-07 16:58:31 -04001274 // XXX: Duplication == ugly
1275 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001276 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001277 mut e: call!(atom_expr, allow_struct, allow_block) >>
1278 many0!(alt!(
1279 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001280 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001281 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001282 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001283 paren_token: args.0,
1284 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001285 }.into();
1286 })
1287 |
1288 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001289 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001290 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001291 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001292 bracket_token: i.0,
1293 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001294 }.into();
1295 })
1296 )) >>
1297 (e)
1298 ));
1299
David Tolnaybcf26022017-12-25 22:10:52 -05001300 // Parse all atomic expressions which don't have to worry about precidence
1301 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001302 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001303 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1304 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001305 |
David Tolnay8c91b882017-12-28 23:04:32 -05001306 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001307 |
1308 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001309 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001310 |
David Tolnay8c91b882017-12-28 23:04:32 -05001311 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001314 |
1315 call!(expr_break, allow_struct) // must be before expr_path
1316 |
David Tolnay8c91b882017-12-28 23:04:32 -05001317 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001318 |
1319 call!(expr_ret, allow_struct) // must be before expr_path
1320 |
David Tolnay8c91b882017-12-28 23:04:32 -05001321 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001322 |
David Tolnay8c91b882017-12-28 23:04:32 -05001323 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001324 |
David Tolnay8c91b882017-12-28 23:04:32 -05001325 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326 |
David Tolnay8c91b882017-12-28 23:04:32 -05001327 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001328 |
David Tolnay8c91b882017-12-28 23:04:32 -05001329 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 |
David Tolnay8c91b882017-12-28 23:04:32 -05001331 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 |
David Tolnay8c91b882017-12-28 23:04:32 -05001333 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 |
David Tolnay8c91b882017-12-28 23:04:32 -05001335 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001336 |
David Tolnay8c91b882017-12-28 23:04:32 -05001337 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338 |
David Tolnay8c91b882017-12-28 23:04:32 -05001339 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001340 |
David Tolnay8c91b882017-12-28 23:04:32 -05001341 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001342 |
David Tolnay8c91b882017-12-28 23:04:32 -05001343 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001344 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001345 call!(expr_closure, allow_struct)
1346 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001347 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001348 |
1349 // NOTE: This is the prefix-form of range
1350 call!(expr_range, allow_struct)
1351 |
David Tolnay8c91b882017-12-28 23:04:32 -05001352 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001353 |
David Tolnay8c91b882017-12-28 23:04:32 -05001354 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001355 ));
1356
Michael Layzell734adb42017-06-07 16:58:31 -04001357 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001358 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001359 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001360 |
David Tolnay8c91b882017-12-28 23:04:32 -05001361 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001362 ));
1363
Michael Layzell734adb42017-06-07 16:58:31 -04001364 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001365 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001366 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001367 |
David Tolnay8c91b882017-12-28 23:04:32 -05001368 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001369 |
David Tolnay8c91b882017-12-28 23:04:32 -05001370 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001371 |
David Tolnay8c91b882017-12-28 23:04:32 -05001372 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001373 |
David Tolnay8c91b882017-12-28 23:04:32 -05001374 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001375 |
David Tolnay8c91b882017-12-28 23:04:32 -05001376 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001377 |
David Tolnay8c91b882017-12-28 23:04:32 -05001378 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001379 |
David Tolnay8c91b882017-12-28 23:04:32 -05001380 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001381 |
David Tolnay8c91b882017-12-28 23:04:32 -05001382 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001383 |
David Tolnay8c91b882017-12-28 23:04:32 -05001384 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001385 |
David Tolnay8c91b882017-12-28 23:04:32 -05001386 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001387 ), Expr::from));
1388
David Tolnay8c91b882017-12-28 23:04:32 -05001389 impl Synom for ExprLit {
1390 named!(parse -> Self, do_parse!(
1391 lit: syn!(Lit) >>
1392 (ExprLit {
1393 attrs: Vec::new(),
1394 lit: lit,
1395 })
1396 ));
1397 }
1398
1399 #[cfg(feature = "full")]
1400 impl Synom for ExprMacro {
1401 named!(parse -> Self, do_parse!(
1402 mac: syn!(Macro) >>
1403 (ExprMacro {
1404 attrs: Vec::new(),
1405 mac: mac,
1406 })
1407 ));
1408 }
1409
David Tolnaye98775f2017-12-28 23:17:00 -05001410 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001411 impl Synom for ExprGroup {
1412 named!(parse -> Self, do_parse!(
1413 e: grouped!(syn!(Expr)) >>
1414 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001415 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001416 expr: Box::new(e.1),
1417 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001418 })
Michael Layzell93c36282017-06-04 20:43:14 -04001419 ));
1420 }
1421
David Tolnaye98775f2017-12-28 23:17:00 -05001422 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001423 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001424 named!(parse -> Self, do_parse!(
1425 e: parens!(syn!(Expr)) >>
1426 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001427 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001428 paren_token: e.0,
1429 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001430 })
Michael Layzell92639a52017-06-01 00:07:44 -04001431 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001432 }
David Tolnay89e05672016-10-02 14:39:42 -07001433
Michael Layzell734adb42017-06-07 16:58:31 -04001434 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001435 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001436 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001437 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001438 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001439 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001440 bracket_token: elems.0,
1441 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001442 })
1443 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001444
1445 fn description() -> Option<&'static str> {
1446 Some("array")
1447 }
Alex Crichton954046c2017-05-30 21:49:42 -07001448 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001449
David Tolnayf2cfd722017-12-31 18:02:51 -05001450 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1451 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001452
Michael Layzell734adb42017-06-07 16:58:31 -04001453 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001454 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001455 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001456 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001457 turbofish: option!(tuple!(
1458 punct!(::),
1459 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001460 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001461 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001462 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001463 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001464 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001465 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001466 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001467 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001468 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1469 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001470 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001471
Alex Crichton954046c2017-05-30 21:49:42 -07001472 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001473 turbofish: turbofish.map(|fish| MethodTurbofish {
1474 colon2_token: fish.0,
1475 lt_token: fish.1,
1476 args: fish.2,
1477 gt_token: fish.3,
1478 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001479 args: args.1,
1480 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001481 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001482 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001483 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001484 ));
1485
Michael Layzell734adb42017-06-07 16:58:31 -04001486 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001487 impl Synom for GenericMethodArgument {
1488 // TODO parse const generics as well
1489 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1490 }
1491
1492 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001493 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001494 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001495 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001496 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001497 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001498 elems: elems.1,
1499 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001500 })
1501 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001502
1503 fn description() -> Option<&'static str> {
1504 Some("tuple")
1505 }
Alex Crichton954046c2017-05-30 21:49:42 -07001506 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001507
Michael Layzell734adb42017-06-07 16:58:31 -04001508 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001509 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001510 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001511 if_: keyword!(if) >>
1512 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001513 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001514 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001515 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001516 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001517 else_block: option!(else_block) >>
1518 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001519 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001520 pat: Box::new(pat),
1521 let_token: let_,
1522 eq_token: eq,
1523 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001524 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001525 brace_token: then_block.0,
1526 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001527 },
1528 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001529 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001530 })
1531 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001532
1533 fn description() -> Option<&'static str> {
1534 Some("`if let` expression")
1535 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001536 }
1537
Michael Layzell734adb42017-06-07 16:58:31 -04001538 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001539 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001540 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001541 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001542 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001543 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001544 else_block: option!(else_block) >>
1545 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001546 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001547 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001548 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001549 brace_token: then_block.0,
1550 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001551 },
1552 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001553 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001554 })
1555 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001556
1557 fn description() -> Option<&'static str> {
1558 Some("`if` expression")
1559 }
Alex Crichton954046c2017-05-30 21:49:42 -07001560 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001561
Michael Layzell734adb42017-06-07 16:58:31 -04001562 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001563 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001564 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001565 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001566 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001567 |
David Tolnay8c91b882017-12-28 23:04:32 -05001568 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001569 |
1570 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001571 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001572 (Expr::Block(ExprBlock {
1573 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001574 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001575 brace_token: else_block.0,
1576 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001577 },
1578 }))
David Tolnay939766a2016-09-23 23:48:12 -07001579 )
Alex Crichton954046c2017-05-30 21:49:42 -07001580 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001581 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001582 ));
1583
Michael Layzell734adb42017-06-07 16:58:31 -04001584 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001585 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001586 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001587 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001588 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001589 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001590 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001591 expr: expr_no_struct >>
1592 loop_block: syn!(Block) >>
1593 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001594 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001595 for_token: for_,
1596 in_token: in_,
1597 pat: Box::new(pat),
1598 expr: Box::new(expr),
1599 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001600 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001601 })
1602 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001603
1604 fn description() -> Option<&'static str> {
1605 Some("`for` loop")
1606 }
Alex Crichton954046c2017-05-30 21:49:42 -07001607 }
Gregory Katze5f35682016-09-27 14:20:55 -04001608
Michael Layzell734adb42017-06-07 16:58:31 -04001609 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001610 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001611 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001612 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001613 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001614 loop_block: syn!(Block) >>
1615 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001616 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001617 loop_token: loop_,
1618 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001619 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001620 })
1621 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001622
1623 fn description() -> Option<&'static str> {
1624 Some("`loop`")
1625 }
Alex Crichton954046c2017-05-30 21:49:42 -07001626 }
1627
Michael Layzell734adb42017-06-07 16:58:31 -04001628 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001629 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001630 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001631 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001632 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001633 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001634 (ExprMatch {
1635 attrs: Vec::new(),
1636 expr: Box::new(obj),
1637 match_token: match_,
1638 brace_token: res.0,
1639 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001640 })
1641 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001642
1643 fn description() -> Option<&'static str> {
1644 Some("`match` expression")
1645 }
Alex Crichton954046c2017-05-30 21:49:42 -07001646 }
David Tolnay1978c672016-10-27 22:05:52 -07001647
Michael Layzell734adb42017-06-07 16:58:31 -04001648 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001649 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001650 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001651 do_: keyword!(do) >>
1652 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001653 catch_block: syn!(Block) >>
1654 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001655 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001656 block: catch_block,
1657 do_token: do_,
1658 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001659 })
Michael Layzell92639a52017-06-01 00:07:44 -04001660 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001661
1662 fn description() -> Option<&'static str> {
1663 Some("`catch` expression")
1664 }
Alex Crichton954046c2017-05-30 21:49:42 -07001665 }
Arnavion02ef13f2017-04-25 00:54:31 -07001666
Michael Layzell734adb42017-06-07 16:58:31 -04001667 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001668 impl Synom for ExprYield {
1669 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001670 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001671 expr: option!(syn!(Expr)) >>
1672 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001673 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001674 yield_token: yield_,
1675 expr: expr.map(Box::new),
1676 })
1677 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001678
1679 fn description() -> Option<&'static str> {
1680 Some("`yield` expression")
1681 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001682 }
1683
1684 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001685 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001686 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001687 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001688 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001689 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1690 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001691 body: do_parse!(
1692 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001693 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1694 true => alt!(
1695 input_end!() => { |_| None }
1696 |
1697 punct!(,) => { Some }
1698 )
Alex Crichton03b30272017-08-28 09:35:24 -07001699 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001700 false => option!(punct!(,))
1701 ) >>
1702 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001703 ) >>
1704 (Arm {
1705 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001706 attrs: attrs,
1707 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001708 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001709 body: Box::new(body.0),
1710 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001711 })
1712 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001713
1714 fn description() -> Option<&'static str> {
1715 Some("`match` arm")
1716 }
Alex Crichton954046c2017-05-30 21:49:42 -07001717 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001718
Michael Layzell734adb42017-06-07 16:58:31 -04001719 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001720 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001721 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001722 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001723 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001724 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001725 ret_and_body: alt!(
1726 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001727 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001728 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001729 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001730 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001731 Expr::Block(ExprBlock {
1732 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001733 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001734 }))
David Tolnay89e05672016-10-02 14:39:42 -07001735 )
1736 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001737 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001738 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001739 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001740 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001741 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001742 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001743 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001744 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001745 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001746 body: Box::new(ret_and_body.1),
1747 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001748 ));
1749
Michael Layzell734adb42017-06-07 16:58:31 -04001750 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001751 named!(fn_arg -> FnArg, do_parse!(
1752 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001753 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001754 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001755 if let Some((colon, ty)) = ty {
1756 FnArg::Captured(ArgCaptured {
1757 pat: pat,
1758 colon_token: colon,
1759 ty: ty,
1760 })
1761 } else {
1762 FnArg::Inferred(pat)
1763 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001764 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001765 ));
1766
Michael Layzell734adb42017-06-07 16:58:31 -04001767 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001768 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001769 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001770 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001771 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001772 cond: expr_no_struct >>
1773 while_block: syn!(Block) >>
1774 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001775 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001776 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001777 cond: Box::new(cond),
1778 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001779 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001780 })
1781 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001782
1783 fn description() -> Option<&'static str> {
1784 Some("`while` expression")
1785 }
Alex Crichton954046c2017-05-30 21:49:42 -07001786 }
1787
Michael Layzell734adb42017-06-07 16:58:31 -04001788 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001789 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001790 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001791 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001792 while_: keyword!(while) >>
1793 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001794 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001795 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001796 value: expr_no_struct >>
1797 while_block: syn!(Block) >>
1798 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001799 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001800 eq_token: eq,
1801 let_token: let_,
1802 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001803 pat: Box::new(pat),
1804 expr: Box::new(value),
1805 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001806 label: label,
1807 })
1808 ));
1809 }
1810
1811 #[cfg(feature = "full")]
1812 impl Synom for Label {
1813 named!(parse -> Self, do_parse!(
1814 name: syn!(Lifetime) >>
1815 colon: punct!(:) >>
1816 (Label {
1817 name: name,
1818 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001819 })
1820 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001821
1822 fn description() -> Option<&'static str> {
1823 Some("`while let` expression")
1824 }
Alex Crichton954046c2017-05-30 21:49:42 -07001825 }
1826
Michael Layzell734adb42017-06-07 16:58:31 -04001827 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001828 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001829 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001830 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001831 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001832 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001833 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001834 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001835 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001836 })
1837 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001838
1839 fn description() -> Option<&'static str> {
1840 Some("`continue`")
1841 }
Alex Crichton954046c2017-05-30 21:49:42 -07001842 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001843
Michael Layzell734adb42017-06-07 16:58:31 -04001844 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001845 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001846 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001847 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001848 // We can't allow blocks after a `break` expression when we wouldn't
1849 // allow structs, as this expression is ambiguous.
1850 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001851 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001852 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001853 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001854 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001855 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001856 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001857 ));
1858
Michael Layzell734adb42017-06-07 16:58:31 -04001859 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001860 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001861 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001862 // NOTE: return is greedy and eats blocks after it even when in a
1863 // position where structs are not allowed, such as in if statement
1864 // conditions. For example:
1865 //
David Tolnaybcf26022017-12-25 22:10:52 -05001866 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001867 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001868 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001869 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001870 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001871 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001872 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001873 ));
1874
Michael Layzell734adb42017-06-07 16:58:31 -04001875 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001876 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001877 named!(parse -> Self, do_parse!(
1878 path: syn!(Path) >>
1879 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001880 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001881 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
1882 dots: punct!(..) >>
1883 base: syn!(Expr) >>
1884 (dots, base)
1885 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001886 (fields, base)
1887 )) >>
1888 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001889 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04001890 let (dots, rest) = match base.and_then(|b| b) {
1891 Some((dots, base)) => (Some(dots), Some(base)),
1892 None => (None, None),
1893 };
1894 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001895 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001896 brace_token: brace,
1897 path: path,
1898 fields: fields,
1899 dot2_token: dots,
1900 rest: rest.map(Box::new),
1901 }
1902 })
1903 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001904
1905 fn description() -> Option<&'static str> {
1906 Some("struct literal expression")
1907 }
Alex Crichton954046c2017-05-30 21:49:42 -07001908 }
1909
Michael Layzell734adb42017-06-07 16:58:31 -04001910 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001911 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001912 named!(parse -> Self, alt!(
1913 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001914 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001915 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001916 value: syn!(Expr) >>
1917 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001918 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001919 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001920 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001921 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001922 })
Michael Layzell92639a52017-06-01 00:07:44 -04001923 )
1924 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001925 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001926 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001927 expr: Expr::Path(ExprPath {
1928 attrs: Vec::new(),
1929 qself: None,
1930 path: name.into(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001931 }),
Michael Layzell92639a52017-06-01 00:07:44 -04001932 attrs: Vec::new(),
1933 colon_token: None,
1934 })
1935 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001936
1937 fn description() -> Option<&'static str> {
1938 Some("field-value pair: `field: value`")
1939 }
Alex Crichton954046c2017-05-30 21:49:42 -07001940 }
David Tolnay055a7042016-10-02 19:23:54 -07001941
Michael Layzell734adb42017-06-07 16:58:31 -04001942 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001943 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001944 named!(parse -> Self, do_parse!(
1945 data: brackets!(do_parse!(
1946 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001947 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001948 times: syn!(Expr) >>
1949 (value, semi, times)
1950 )) >>
1951 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001952 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001953 expr: Box::new((data.1).0),
1954 amt: Box::new((data.1).2),
1955 bracket_token: data.0,
1956 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001957 })
1958 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001959
1960 fn description() -> Option<&'static str> {
1961 Some("repeated array literal: `[val; N]`")
1962 }
Alex Crichton954046c2017-05-30 21:49:42 -07001963 }
David Tolnay055a7042016-10-02 19:23:54 -07001964
Michael Layzell734adb42017-06-07 16:58:31 -04001965 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001966 impl Synom for ExprUnsafe {
1967 named!(parse -> Self, do_parse!(
1968 unsafe_: keyword!(unsafe) >>
1969 b: syn!(Block) >>
1970 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001971 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001972 unsafe_token: unsafe_,
1973 block: b,
1974 })
1975 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001976
1977 fn description() -> Option<&'static str> {
1978 Some("unsafe block: `unsafe { .. }`")
1979 }
Nika Layzell640832a2017-12-04 13:37:09 -05001980 }
1981
1982 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001983 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001984 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001985 b: syn!(Block) >>
1986 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001987 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001988 block: b,
1989 })
1990 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001991
1992 fn description() -> Option<&'static str> {
1993 Some("block: `{ .. }`")
1994 }
Alex Crichton954046c2017-05-30 21:49:42 -07001995 }
David Tolnay89e05672016-10-02 14:39:42 -07001996
Michael Layzell734adb42017-06-07 16:58:31 -04001997 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001998 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001999 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002000 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002001 (ExprRange {
2002 attrs: Vec::new(),
2003 from: None,
2004 to: hi.map(Box::new),
2005 limits: limits,
2006 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002007 ));
2008
Michael Layzell734adb42017-06-07 16:58:31 -04002009 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002010 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002011 named!(parse -> Self, alt!(
2012 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002013 punct!(..=) => { RangeLimits::Closed }
2014 |
2015 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002016 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002017 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002018 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002019 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002020
2021 fn description() -> Option<&'static str> {
2022 Some("range limit: `..`, `...` or `..=`")
2023 }
Alex Crichton954046c2017-05-30 21:49:42 -07002024 }
David Tolnay438c9052016-10-07 23:24:48 -07002025
Alex Crichton954046c2017-05-30 21:49:42 -07002026 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002027 named!(parse -> Self, do_parse!(
2028 pair: qpath >>
2029 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002030 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002031 qself: pair.0,
2032 path: pair.1,
2033 })
2034 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002035
2036 fn description() -> Option<&'static str> {
2037 Some("path: `a::b::c`")
2038 }
Alex Crichton954046c2017-05-30 21:49:42 -07002039 }
David Tolnay42602292016-10-01 22:25:45 -07002040
Michael Layzell734adb42017-06-07 16:58:31 -04002041 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002042 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002043
David Tolnay8875fca2017-12-31 13:52:37 -05002044 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002045
Michael Layzell734adb42017-06-07 16:58:31 -04002046 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002047 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002048 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002049 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002050 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002051 brace_token: stmts.0,
2052 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002053 })
2054 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002055
2056 fn description() -> Option<&'static str> {
2057 Some("block: `{ .. }`")
2058 }
Alex Crichton954046c2017-05-30 21:49:42 -07002059 }
David Tolnay939766a2016-09-23 23:48:12 -07002060
Michael Layzell734adb42017-06-07 16:58:31 -04002061 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002062 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002063 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002064 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002065 mut standalone: many0!(do_parse!(
2066 stmt: syn!(Stmt) >>
2067 many0!(punct!(;)) >>
2068 (stmt)
2069 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002070 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002071 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002072 mut e: syn!(Expr) >>
2073 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002074 e.replace_attrs(attrs);
Alex Crichton70bbd592017-08-27 10:40:03 -07002075 Stmt::Expr(Box::new(e))
2076 })
2077 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002078 (match last {
2079 None => standalone,
2080 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002081 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002082 standalone
2083 }
2084 })
2085 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002086 }
2087
Michael Layzell734adb42017-06-07 16:58:31 -04002088 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002089 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002090 named!(parse -> Self, alt!(
2091 stmt_mac
2092 |
2093 stmt_local
2094 |
2095 stmt_item
2096 |
Michael Layzell35418782017-06-07 09:20:25 -04002097 stmt_blockexpr
2098 |
Michael Layzell92639a52017-06-01 00:07:44 -04002099 stmt_expr
2100 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002101
2102 fn description() -> Option<&'static str> {
2103 Some("statement")
2104 }
Alex Crichton954046c2017-05-30 21:49:42 -07002105 }
David Tolnay939766a2016-09-23 23:48:12 -07002106
Michael Layzell734adb42017-06-07 16:58:31 -04002107 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002108 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002109 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002110 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002111 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002112 // Only parse braces here; paren and bracket will get parsed as
2113 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002114 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002115 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05002116 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
2117 attrs: attrs,
2118 ident: None,
2119 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002120 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002121 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002122 delimiter: MacroDelimiter::Brace(data.0),
2123 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002124 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002125 semi_token: semi,
2126 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07002127 ));
2128
Michael Layzell734adb42017-06-07 16:58:31 -04002129 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002130 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002131 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002132 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002133 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002134 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002135 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2136 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07002137 (Stmt::Local(Box::new(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002138 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002139 let_token: let_,
2140 pat: Box::new(pat),
2141 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2142 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2143 semi_token: semi,
David Tolnay191e0582016-10-02 18:31:09 -07002144 })))
2145 ));
2146
Michael Layzell734adb42017-06-07 16:58:31 -04002147 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002148 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002149
Michael Layzell734adb42017-06-07 16:58:31 -04002150 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002151 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002152 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002153 mut e: expr_nosemi >>
2154 // If the next token is a `.` or a `?` it is special-cased to parse as
2155 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002156 not!(punct!(.)) >>
2157 not!(punct!(?)) >>
2158 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002159 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002160 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002161 if let Some(semi) = semi {
2162 Stmt::Semi(Box::new(e), semi)
2163 } else {
2164 Stmt::Expr(Box::new(e))
2165 }
2166 })
2167 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002168
Michael Layzell734adb42017-06-07 16:58:31 -04002169 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002170 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002171 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002172 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002173 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002174 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002175 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002176 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002177 })
David Tolnay939766a2016-09-23 23:48:12 -07002178 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002179
Michael Layzell734adb42017-06-07 16:58:31 -04002180 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002181 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002182 named!(parse -> Self, alt!(
2183 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2184 |
2185 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2186 |
2187 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2188 |
2189 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2190 |
2191 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2192 |
David Tolnay323279a2017-12-29 11:26:32 -05002193 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002194 |
2195 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2196 |
2197 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2198 |
2199 syn!(PatPath) => { Pat::Path }
2200 |
2201 syn!(PatTuple) => { Pat::Tuple }
2202 |
2203 syn!(PatRef) => { Pat::Ref }
2204 |
2205 syn!(PatSlice) => { Pat::Slice }
2206 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002207
2208 fn description() -> Option<&'static str> {
2209 Some("pattern")
2210 }
Alex Crichton954046c2017-05-30 21:49:42 -07002211 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002212
Michael Layzell734adb42017-06-07 16:58:31 -04002213 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002214 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002215 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002216 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002217 |u| PatWild { underscore_token: u }
2218 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002219
2220 fn description() -> Option<&'static str> {
2221 Some("wild pattern: `_`")
2222 }
Alex Crichton954046c2017-05-30 21:49:42 -07002223 }
David Tolnay84aa0752016-10-02 23:01:13 -07002224
Michael Layzell734adb42017-06-07 16:58:31 -04002225 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002226 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002227 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002228 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002229 pat: syn!(Pat) >>
2230 (PatBox {
2231 pat: Box::new(pat),
2232 box_token: boxed,
2233 })
2234 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002235
2236 fn description() -> Option<&'static str> {
2237 Some("box pattern")
2238 }
Alex Crichton954046c2017-05-30 21:49:42 -07002239 }
2240
Michael Layzell734adb42017-06-07 16:58:31 -04002241 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002242 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002243 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002244 by_ref: option!(keyword!(ref)) >>
2245 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002246 name: alt!(
2247 syn!(Ident)
2248 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002249 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002250 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002251 not!(punct!(<)) >>
2252 not!(punct!(::)) >>
2253 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002254 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002255 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002256 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002257 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002258 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002259 })
2260 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002261
2262 fn description() -> Option<&'static str> {
2263 Some("pattern identifier binding")
2264 }
Alex Crichton954046c2017-05-30 21:49:42 -07002265 }
2266
Michael Layzell734adb42017-06-07 16:58:31 -04002267 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002268 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002269 named!(parse -> Self, do_parse!(
2270 path: syn!(Path) >>
2271 tuple: syn!(PatTuple) >>
2272 (PatTupleStruct {
2273 path: path,
2274 pat: tuple,
2275 })
2276 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002277
2278 fn description() -> Option<&'static str> {
2279 Some("tuple struct pattern")
2280 }
Alex Crichton954046c2017-05-30 21:49:42 -07002281 }
2282
Michael Layzell734adb42017-06-07 16:58:31 -04002283 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002284 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002285 named!(parse -> Self, do_parse!(
2286 path: syn!(Path) >>
2287 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002288 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002289 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002290 (fields, base)
2291 )) >>
2292 (PatStruct {
2293 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002294 fields: (data.1).0,
2295 brace_token: data.0,
2296 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002297 })
2298 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002299
2300 fn description() -> Option<&'static str> {
2301 Some("struct pattern")
2302 }
Alex Crichton954046c2017-05-30 21:49:42 -07002303 }
2304
Michael Layzell734adb42017-06-07 16:58:31 -04002305 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002306 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002307 named!(parse -> Self, alt!(
2308 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002309 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002310 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002311 pat: syn!(Pat) >>
2312 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002313 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002314 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002315 attrs: Vec::new(),
2316 colon_token: Some(colon),
2317 })
2318 )
2319 |
2320 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002321 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002322 by_ref: option!(keyword!(ref)) >>
2323 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002324 ident: syn!(Ident) >>
2325 ({
2326 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002327 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002328 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002329 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002330 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002331 }.into();
2332 if let Some(boxed) = boxed {
2333 pat = PatBox {
2334 pat: Box::new(pat),
2335 box_token: boxed,
2336 }.into();
2337 }
2338 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002339 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002340 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002341 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002342 colon_token: None,
2343 }
2344 })
2345 )
2346 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002347
2348 fn description() -> Option<&'static str> {
2349 Some("field pattern")
2350 }
Alex Crichton954046c2017-05-30 21:49:42 -07002351 }
2352
Michael Layzell734adb42017-06-07 16:58:31 -04002353 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002354 impl Synom for Member {
2355 named!(parse -> Self, alt!(
2356 syn!(Ident) => { Member::Named }
2357 |
2358 syn!(Index) => { Member::Unnamed }
2359 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002360
2361 fn description() -> Option<&'static str> {
2362 Some("field member")
2363 }
David Tolnay85b69a42017-12-27 20:43:10 -05002364 }
2365
2366 #[cfg(feature = "full")]
2367 impl Synom for Index {
2368 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002369 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002370 ({
David Tolnay360efd22018-01-04 23:35:26 -08002371 if let IntSuffix::None = lit.suffix() {
2372 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002373 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002374 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002375 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002376 })
David Tolnay85b69a42017-12-27 20:43:10 -05002377 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002378
2379 fn description() -> Option<&'static str> {
2380 Some("field index")
2381 }
David Tolnay85b69a42017-12-27 20:43:10 -05002382 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002383
Michael Layzell734adb42017-06-07 16:58:31 -04002384 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002385 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002386 named!(parse -> Self, map!(
2387 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002388 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002389 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002390
2391 fn description() -> Option<&'static str> {
2392 Some("path pattern")
2393 }
Alex Crichton954046c2017-05-30 21:49:42 -07002394 }
David Tolnay9636c052016-10-02 17:11:17 -07002395
Michael Layzell734adb42017-06-07 16:58:31 -04002396 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002397 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002398 named!(parse -> Self, do_parse!(
2399 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002400 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002401 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2402 tuple!(punct!(..), option!(punct!(,)))
2403 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002404 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002405 Some((_, Some(_))) => true,
2406 _ => false,
2407 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002408 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002409 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002410 )) >>
2411 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002412 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002413 let (dotdot, trailing) = match dotdot {
2414 Some((a, b)) => (Some(a), Some(b)),
2415 None => (None, None),
2416 };
2417 PatTuple {
2418 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002419 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002420 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002421 comma_token: trailing.unwrap_or_default(),
2422 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002423 }
2424 })
2425 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002426
2427 fn description() -> Option<&'static str> {
2428 Some("tuple pattern")
2429 }
Alex Crichton954046c2017-05-30 21:49:42 -07002430 }
David Tolnayfbb73232016-10-03 01:00:06 -07002431
Michael Layzell734adb42017-06-07 16:58:31 -04002432 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002433 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002434 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002435 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002436 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002437 pat: syn!(Pat) >>
2438 (PatRef {
2439 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002440 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002441 and_token: and,
2442 })
2443 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002444
2445 fn description() -> Option<&'static str> {
2446 Some("reference pattern")
2447 }
Alex Crichton954046c2017-05-30 21:49:42 -07002448 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002449
Michael Layzell734adb42017-06-07 16:58:31 -04002450 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002451 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002452 named!(parse -> Self, do_parse!(
2453 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002454 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002455 return parse_error(); // these need to be parsed by pat_path
2456 } else {
2457 PatLit {
2458 expr: Box::new(lit),
2459 }
2460 })
2461 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002462
2463 fn description() -> Option<&'static str> {
2464 Some("literal pattern")
2465 }
Alex Crichton954046c2017-05-30 21:49:42 -07002466 }
David Tolnaye1310902016-10-29 23:40:00 -07002467
Michael Layzell734adb42017-06-07 16:58:31 -04002468 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002469 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002470 named!(parse -> Self, do_parse!(
2471 lo: pat_lit_expr >>
2472 limits: syn!(RangeLimits) >>
2473 hi: pat_lit_expr >>
2474 (PatRange {
2475 lo: Box::new(lo),
2476 hi: Box::new(hi),
2477 limits: limits,
2478 })
2479 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002480
2481 fn description() -> Option<&'static str> {
2482 Some("range pattern")
2483 }
Alex Crichton954046c2017-05-30 21:49:42 -07002484 }
David Tolnaye1310902016-10-29 23:40:00 -07002485
Michael Layzell734adb42017-06-07 16:58:31 -04002486 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002487 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002488 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002489 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002490 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002491 |
David Tolnay8c91b882017-12-28 23:04:32 -05002492 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002493 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002494 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002495 Expr::Unary(ExprUnary {
2496 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002497 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002498 expr: Box::new(v)
2499 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002500 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002501 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002502 })
2503 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002504
Michael Layzell734adb42017-06-07 16:58:31 -04002505 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002506 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002507 named!(parse -> Self, map!(
2508 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002509 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002510 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002511 dots: punct!(..) >>
2512 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002513 (dots, trailing)
2514 )) >>
2515 after: cond!(
2516 match middle {
2517 Some((_, ref trailing)) => trailing.is_some(),
2518 _ => false,
2519 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002520 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002521 ) >>
2522 (before, middle, after)
2523 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002524 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002525 let mut before: Punctuated<Pat, Token![,]> = before;
2526 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002527 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002528 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002529 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002530 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002531 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002532 }),
2533 bracket_token: brackets,
2534 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002535 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002536 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002537 } else {
2538 Some(Box::new(before.pop().unwrap().into_item()))
Michael Layzell92639a52017-06-01 00:07:44 -04002539 }
2540 }),
2541 front: before,
2542 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002543 }
Alex Crichton954046c2017-05-30 21:49:42 -07002544 }
Michael Layzell92639a52017-06-01 00:07:44 -04002545 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002546
2547 fn description() -> Option<&'static str> {
2548 Some("slice pattern")
2549 }
Alex Crichton954046c2017-05-30 21:49:42 -07002550 }
David Tolnay323279a2017-12-29 11:26:32 -05002551
2552 #[cfg(feature = "full")]
2553 impl Synom for PatMacro {
2554 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002555
2556 fn description() -> Option<&'static str> {
2557 Some("macro pattern")
2558 }
David Tolnay323279a2017-12-29 11:26:32 -05002559 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002560}
2561
David Tolnayf4bbbd92016-09-23 14:41:55 -07002562#[cfg(feature = "printing")]
2563mod printing {
2564 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002565 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002566 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002567 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002568 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002569 use proc_macro2::{Literal, TokenNode, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002570
David Tolnaybcf26022017-12-25 22:10:52 -05002571 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2572 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002573 #[cfg(feature = "full")]
2574 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002575 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002576 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002577 e.to_tokens(tokens);
2578 });
2579 } else {
2580 e.to_tokens(tokens);
2581 }
2582 }
2583
David Tolnay8c91b882017-12-28 23:04:32 -05002584 #[cfg(feature = "full")]
2585 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2586 tokens.append_all(attrs.outer());
2587 }
Michael Layzell734adb42017-06-07 16:58:31 -04002588
David Tolnay8c91b882017-12-28 23:04:32 -05002589 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002590 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002591
Michael Layzell734adb42017-06-07 16:58:31 -04002592 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 impl ToTokens for ExprBox {
2594 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002595 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002596 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002597 self.expr.to_tokens(tokens);
2598 }
2599 }
2600
Michael Layzell734adb42017-06-07 16:58:31 -04002601 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002602 impl ToTokens for ExprInPlace {
2603 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002604 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002605 self.place.to_tokens(tokens);
2606 self.arrow_token.to_tokens(tokens);
2607 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002608 }
2609 }
2610
Michael Layzell734adb42017-06-07 16:58:31 -04002611 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002612 impl ToTokens for ExprArray {
2613 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002614 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002615 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002616 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002617 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002618 }
2619 }
2620
2621 impl ToTokens for ExprCall {
2622 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002623 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002624 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002625 self.paren_token.surround(tokens, |tokens| {
2626 self.args.to_tokens(tokens);
2627 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002628 }
2629 }
2630
Michael Layzell734adb42017-06-07 16:58:31 -04002631 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002632 impl ToTokens for ExprMethodCall {
2633 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002634 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002635 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002636 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002637 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002638 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002639 self.paren_token.surround(tokens, |tokens| {
2640 self.args.to_tokens(tokens);
2641 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002642 }
2643 }
2644
Michael Layzell734adb42017-06-07 16:58:31 -04002645 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002646 impl ToTokens for MethodTurbofish {
2647 fn to_tokens(&self, tokens: &mut Tokens) {
2648 self.colon2_token.to_tokens(tokens);
2649 self.lt_token.to_tokens(tokens);
2650 self.args.to_tokens(tokens);
2651 self.gt_token.to_tokens(tokens);
2652 }
2653 }
2654
2655 #[cfg(feature = "full")]
2656 impl ToTokens for GenericMethodArgument {
2657 fn to_tokens(&self, tokens: &mut Tokens) {
2658 match *self {
2659 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2660 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2661 }
2662 }
2663 }
2664
2665 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002666 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002667 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002668 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002669 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002670 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002671 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002672 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002673 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002674 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002675 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002676 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002677 }
2678 }
2679
2680 impl ToTokens for ExprBinary {
2681 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002682 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002683 self.left.to_tokens(tokens);
2684 self.op.to_tokens(tokens);
2685 self.right.to_tokens(tokens);
2686 }
2687 }
2688
2689 impl ToTokens for ExprUnary {
2690 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002691 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002692 self.op.to_tokens(tokens);
2693 self.expr.to_tokens(tokens);
2694 }
2695 }
2696
David Tolnay8c91b882017-12-28 23:04:32 -05002697 impl ToTokens for ExprLit {
2698 fn to_tokens(&self, tokens: &mut Tokens) {
2699 attrs_to_tokens(&self.attrs, tokens);
2700 self.lit.to_tokens(tokens);
2701 }
2702 }
2703
Alex Crichton62a0a592017-05-22 13:58:53 -07002704 impl ToTokens for ExprCast {
2705 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002706 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002707 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002708 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002709 self.ty.to_tokens(tokens);
2710 }
2711 }
2712
David Tolnay0cf94f22017-12-28 23:46:26 -05002713 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002714 impl ToTokens for ExprType {
2715 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002716 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002717 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002718 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002719 self.ty.to_tokens(tokens);
2720 }
2721 }
2722
Michael Layzell734adb42017-06-07 16:58:31 -04002723 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002724 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002725 if let Some((ref else_token, ref else_)) = *else_ {
2726 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002727
2728 // If we are not one of the valid expressions to exist in an else
2729 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002730 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002731 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002732 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002733 }
2734 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002735 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002736 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002737 });
2738 }
2739 }
2740 }
2741 }
2742
2743 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002744 impl ToTokens for ExprIf {
2745 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002746 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002747 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002748 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002749 self.then_branch.to_tokens(tokens);
2750 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002751 }
2752 }
2753
Michael Layzell734adb42017-06-07 16:58:31 -04002754 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002755 impl ToTokens for ExprIfLet {
2756 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002757 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002758 self.if_token.to_tokens(tokens);
2759 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002760 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002761 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002762 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002763 self.then_branch.to_tokens(tokens);
2764 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002765 }
2766 }
2767
Michael Layzell734adb42017-06-07 16:58:31 -04002768 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 impl ToTokens for ExprWhile {
2770 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002771 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002772 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002773 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002774 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002775 self.body.to_tokens(tokens);
2776 }
2777 }
2778
Michael Layzell734adb42017-06-07 16:58:31 -04002779 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002780 impl ToTokens for ExprWhileLet {
2781 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002782 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002783 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002784 self.while_token.to_tokens(tokens);
2785 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002786 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002787 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002788 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002789 self.body.to_tokens(tokens);
2790 }
2791 }
2792
Michael Layzell734adb42017-06-07 16:58:31 -04002793 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002794 impl ToTokens for ExprForLoop {
2795 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002796 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002797 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002798 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002799 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002800 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002801 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002802 self.body.to_tokens(tokens);
2803 }
2804 }
2805
Michael Layzell734adb42017-06-07 16:58:31 -04002806 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002807 impl ToTokens for ExprLoop {
2808 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002809 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002810 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002811 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002812 self.body.to_tokens(tokens);
2813 }
2814 }
2815
Michael Layzell734adb42017-06-07 16:58:31 -04002816 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002817 impl ToTokens for ExprMatch {
2818 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002819 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002820 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002821 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002822 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002823 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002824 arm.to_tokens(tokens);
2825 // Ensure that we have a comma after a non-block arm, except
2826 // for the last one.
2827 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002828 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002829 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002830 }
2831 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002832 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002833 }
2834 }
2835
Michael Layzell734adb42017-06-07 16:58:31 -04002836 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002837 impl ToTokens for ExprCatch {
2838 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002839 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002840 self.do_token.to_tokens(tokens);
2841 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002842 self.block.to_tokens(tokens);
2843 }
2844 }
2845
Michael Layzell734adb42017-06-07 16:58:31 -04002846 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002847 impl ToTokens for ExprYield {
2848 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002849 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002850 self.yield_token.to_tokens(tokens);
2851 self.expr.to_tokens(tokens);
2852 }
2853 }
2854
2855 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002856 impl ToTokens for ExprClosure {
2857 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002858 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002859 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002860 self.or1_token.to_tokens(tokens);
David Tolnay6eff4da2018-01-01 20:27:45 -08002861 for input in self.inputs.elements() {
David Tolnayf2cfd722017-12-31 18:02:51 -05002862 match **input.item() {
David Tolnay51382052017-12-27 13:46:21 -05002863 FnArg::Captured(ArgCaptured {
2864 ref pat,
2865 ty: Type::Infer(_),
2866 ..
2867 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002868 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002869 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002870 _ => input.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002871 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002872 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002873 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002874 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002875 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002876 self.body.to_tokens(tokens);
2877 }
2878 }
2879
Michael Layzell734adb42017-06-07 16:58:31 -04002880 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002881 impl ToTokens for ExprUnsafe {
2882 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002883 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002884 self.unsafe_token.to_tokens(tokens);
2885 self.block.to_tokens(tokens);
2886 }
2887 }
2888
2889 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002890 impl ToTokens for ExprBlock {
2891 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002892 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002893 self.block.to_tokens(tokens);
2894 }
2895 }
2896
Michael Layzell734adb42017-06-07 16:58:31 -04002897 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002898 impl ToTokens for ExprAssign {
2899 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002900 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002901 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002902 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002903 self.right.to_tokens(tokens);
2904 }
2905 }
2906
Michael Layzell734adb42017-06-07 16:58:31 -04002907 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002908 impl ToTokens for ExprAssignOp {
2909 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002910 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002911 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002912 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002913 self.right.to_tokens(tokens);
2914 }
2915 }
2916
Michael Layzell734adb42017-06-07 16:58:31 -04002917 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 impl ToTokens for ExprField {
2919 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002920 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002921 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002922 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002923 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002924 }
2925 }
2926
Michael Layzell734adb42017-06-07 16:58:31 -04002927 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002928 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002929 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002930 match *self {
2931 Member::Named(ident) => ident.to_tokens(tokens),
2932 Member::Unnamed(ref index) => index.to_tokens(tokens),
2933 }
2934 }
2935 }
2936
2937 #[cfg(feature = "full")]
2938 impl ToTokens for Index {
2939 fn to_tokens(&self, tokens: &mut Tokens) {
2940 tokens.append(TokenTree {
2941 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002942 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002943 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002944 }
2945 }
2946
2947 impl ToTokens for ExprIndex {
2948 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002949 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002950 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002951 self.bracket_token.surround(tokens, |tokens| {
2952 self.index.to_tokens(tokens);
2953 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002954 }
2955 }
2956
Michael Layzell734adb42017-06-07 16:58:31 -04002957 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002958 impl ToTokens for ExprRange {
2959 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002960 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002961 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002962 match self.limits {
2963 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2964 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2965 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002966 self.to.to_tokens(tokens);
2967 }
2968 }
2969
2970 impl ToTokens for ExprPath {
2971 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002972 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002973 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002974 }
2975 }
2976
Michael Layzell734adb42017-06-07 16:58:31 -04002977 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002978 impl ToTokens for ExprAddrOf {
2979 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002980 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002981 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002982 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002983 self.expr.to_tokens(tokens);
2984 }
2985 }
2986
Michael Layzell734adb42017-06-07 16:58:31 -04002987 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002988 impl ToTokens for ExprBreak {
2989 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002990 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002991 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002992 self.label.to_tokens(tokens);
2993 self.expr.to_tokens(tokens);
2994 }
2995 }
2996
Michael Layzell734adb42017-06-07 16:58:31 -04002997 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002998 impl ToTokens for ExprContinue {
2999 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003000 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003001 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003002 self.label.to_tokens(tokens);
3003 }
3004 }
3005
Michael Layzell734adb42017-06-07 16:58:31 -04003006 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003007 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003008 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003009 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003010 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003011 self.expr.to_tokens(tokens);
3012 }
3013 }
3014
Michael Layzell734adb42017-06-07 16:58:31 -04003015 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003016 impl ToTokens for ExprMacro {
3017 fn to_tokens(&self, tokens: &mut Tokens) {
3018 tokens.append_all(self.attrs.outer());
3019 self.mac.to_tokens(tokens);
3020 }
3021 }
3022
3023 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003024 impl ToTokens for ExprStruct {
3025 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003026 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003027 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003028 self.brace_token.surround(tokens, |tokens| {
3029 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003030 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003031 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003032 self.rest.to_tokens(tokens);
3033 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003035 }
3036 }
3037
Michael Layzell734adb42017-06-07 16:58:31 -04003038 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003039 impl ToTokens for ExprRepeat {
3040 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003041 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003042 self.bracket_token.surround(tokens, |tokens| {
3043 self.expr.to_tokens(tokens);
3044 self.semi_token.to_tokens(tokens);
3045 self.amt.to_tokens(tokens);
3046 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003047 }
3048 }
3049
David Tolnaye98775f2017-12-28 23:17:00 -05003050 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003051 impl ToTokens for ExprGroup {
3052 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003053 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003054 self.group_token.surround(tokens, |tokens| {
3055 self.expr.to_tokens(tokens);
3056 });
3057 }
3058 }
3059
David Tolnaye98775f2017-12-28 23:17:00 -05003060 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003061 impl ToTokens for ExprParen {
3062 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003063 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003064 self.paren_token.surround(tokens, |tokens| {
3065 self.expr.to_tokens(tokens);
3066 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003067 }
3068 }
3069
Michael Layzell734adb42017-06-07 16:58:31 -04003070 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003071 impl ToTokens for ExprTry {
3072 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003073 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003074 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003075 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003076 }
3077 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003078
David Tolnay2ae520a2017-12-29 11:19:50 -05003079 impl ToTokens for ExprVerbatim {
3080 fn to_tokens(&self, tokens: &mut Tokens) {
3081 self.tts.to_tokens(tokens);
3082 }
3083 }
3084
Michael Layzell734adb42017-06-07 16:58:31 -04003085 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003086 impl ToTokens for Label {
3087 fn to_tokens(&self, tokens: &mut Tokens) {
3088 self.name.to_tokens(tokens);
3089 self.colon_token.to_tokens(tokens);
3090 }
3091 }
3092
3093 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003094 impl ToTokens for FieldValue {
3095 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003096 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003097 if let Some(ref colon_token) = self.colon_token {
3098 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003099 self.expr.to_tokens(tokens);
3100 }
David Tolnay055a7042016-10-02 19:23:54 -07003101 }
3102 }
3103
Michael Layzell734adb42017-06-07 16:58:31 -04003104 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003105 impl ToTokens for Arm {
3106 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003107 tokens.append_all(&self.attrs);
3108 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003109 if let Some((ref if_token, ref guard)) = self.guard {
3110 if_token.to_tokens(tokens);
3111 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003112 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003113 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003114 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003115 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003116 }
3117 }
3118
Michael Layzell734adb42017-06-07 16:58:31 -04003119 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003120 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003121 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003122 self.underscore_token.to_tokens(tokens);
3123 }
3124 }
3125
Michael Layzell734adb42017-06-07 16:58:31 -04003126 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003127 impl ToTokens for PatIdent {
3128 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003129 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003130 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003131 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003132 if let Some((ref at_token, ref subpat)) = self.subpat {
3133 at_token.to_tokens(tokens);
3134 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003135 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003136 }
3137 }
3138
Michael Layzell734adb42017-06-07 16:58:31 -04003139 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003140 impl ToTokens for PatStruct {
3141 fn to_tokens(&self, tokens: &mut Tokens) {
3142 self.path.to_tokens(tokens);
3143 self.brace_token.surround(tokens, |tokens| {
3144 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003145 // NOTE: We need a comma before the dot2 token if it is present.
3146 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003147 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003148 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003149 self.dot2_token.to_tokens(tokens);
3150 });
3151 }
3152 }
3153
Michael Layzell734adb42017-06-07 16:58:31 -04003154 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003155 impl ToTokens for PatTupleStruct {
3156 fn to_tokens(&self, tokens: &mut Tokens) {
3157 self.path.to_tokens(tokens);
3158 self.pat.to_tokens(tokens);
3159 }
3160 }
3161
Michael Layzell734adb42017-06-07 16:58:31 -04003162 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003163 impl ToTokens for PatPath {
3164 fn to_tokens(&self, tokens: &mut Tokens) {
3165 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3166 }
3167 }
3168
Michael Layzell734adb42017-06-07 16:58:31 -04003169 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003170 impl ToTokens for PatTuple {
3171 fn to_tokens(&self, tokens: &mut Tokens) {
3172 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003173 self.front.to_tokens(tokens);
3174 if let Some(ref dot2_token) = self.dot2_token {
3175 if !self.front.empty_or_trailing() {
3176 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003177 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003178 }
David Tolnay41871922017-12-29 01:53:45 -05003179 dot2_token.to_tokens(tokens);
3180 self.comma_token.to_tokens(tokens);
3181 if self.comma_token.is_none() && !self.back.is_empty() {
3182 // Ensure there is a comma after the .. token.
3183 <Token![,]>::default().to_tokens(tokens);
3184 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003185 }
David Tolnay41871922017-12-29 01:53:45 -05003186 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003187 });
3188 }
3189 }
3190
Michael Layzell734adb42017-06-07 16:58:31 -04003191 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003192 impl ToTokens for PatBox {
3193 fn to_tokens(&self, tokens: &mut Tokens) {
3194 self.box_token.to_tokens(tokens);
3195 self.pat.to_tokens(tokens);
3196 }
3197 }
3198
Michael Layzell734adb42017-06-07 16:58:31 -04003199 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003200 impl ToTokens for PatRef {
3201 fn to_tokens(&self, tokens: &mut Tokens) {
3202 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003203 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003204 self.pat.to_tokens(tokens);
3205 }
3206 }
3207
Michael Layzell734adb42017-06-07 16:58:31 -04003208 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003209 impl ToTokens for PatLit {
3210 fn to_tokens(&self, tokens: &mut Tokens) {
3211 self.expr.to_tokens(tokens);
3212 }
3213 }
3214
Michael Layzell734adb42017-06-07 16:58:31 -04003215 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003216 impl ToTokens for PatRange {
3217 fn to_tokens(&self, tokens: &mut Tokens) {
3218 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003219 match self.limits {
3220 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3221 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3222 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003223 self.hi.to_tokens(tokens);
3224 }
3225 }
3226
Michael Layzell734adb42017-06-07 16:58:31 -04003227 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003228 impl ToTokens for PatSlice {
3229 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003230 // XXX: This is a mess, and it will be so easy to screw it up. How
3231 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003232 self.bracket_token.surround(tokens, |tokens| {
3233 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003234
3235 // If we need a comma before the middle or standalone .. token,
3236 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003237 if !self.front.empty_or_trailing()
3238 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003239 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003240 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003241 }
3242
3243 // If we have an identifier, we always need a .. token.
3244 if self.middle.is_some() {
3245 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003246 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003247 } else if self.dot2_token.is_some() {
3248 self.dot2_token.to_tokens(tokens);
3249 }
3250
3251 // Make sure we have a comma before the back half.
3252 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003253 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003254 self.back.to_tokens(tokens);
3255 } else {
3256 self.comma_token.to_tokens(tokens);
3257 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003258 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003259 }
3260 }
3261
Michael Layzell734adb42017-06-07 16:58:31 -04003262 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003263 impl ToTokens for PatMacro {
3264 fn to_tokens(&self, tokens: &mut Tokens) {
3265 self.mac.to_tokens(tokens);
3266 }
3267 }
3268
3269 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003270 impl ToTokens for PatVerbatim {
3271 fn to_tokens(&self, tokens: &mut Tokens) {
3272 self.tts.to_tokens(tokens);
3273 }
3274 }
3275
3276 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003277 impl ToTokens for FieldPat {
3278 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003279 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003280 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003281 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003282 }
3283 self.pat.to_tokens(tokens);
3284 }
3285 }
3286
Michael Layzell734adb42017-06-07 16:58:31 -04003287 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003288 impl ToTokens for Block {
3289 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003290 self.brace_token.surround(tokens, |tokens| {
3291 tokens.append_all(&self.stmts);
3292 });
David Tolnay42602292016-10-01 22:25:45 -07003293 }
3294 }
3295
Michael Layzell734adb42017-06-07 16:58:31 -04003296 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003297 impl ToTokens for Stmt {
3298 fn to_tokens(&self, tokens: &mut Tokens) {
3299 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003300 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003301 Stmt::Item(ref item) => item.to_tokens(tokens),
3302 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003303 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003304 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003305 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003306 }
David Tolnay42602292016-10-01 22:25:45 -07003307 }
3308 }
3309 }
David Tolnay191e0582016-10-02 18:31:09 -07003310
Michael Layzell734adb42017-06-07 16:58:31 -04003311 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003312 impl ToTokens for Local {
3313 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003314 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003315 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003316 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003317 if let Some((ref colon_token, ref ty)) = self.ty {
3318 colon_token.to_tokens(tokens);
3319 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003320 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003321 if let Some((ref eq_token, ref init)) = self.init {
3322 eq_token.to_tokens(tokens);
3323 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003324 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003325 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003326 }
3327 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003328}