blob: 13c462a2bdf99596983931739038c164f331cc6c [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 {
406 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, .. }) |
David Tolnayc246cd32017-12-28 23:14:32 -0500436 Expr::Return(ExprReturn { ref mut attrs, .. }) |
David Tolnay8c91b882017-12-28 23:04:32 -0500437 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, .. }) |
David Tolnay2ae520a2017-12-29 11:19:50 -0500444 Expr::Yield(ExprYield { ref mut attrs, .. }) => {
445 mem::replace(attrs, new)
446 }
447 Expr::Verbatim(_) => {
448 // TODO
449 Vec::new()
450 }
David Tolnay8c91b882017-12-28 23:04:32 -0500451 }
452 }
453}
454
David Tolnay85b69a42017-12-27 20:43:10 -0500455ast_enum! {
456 /// A struct or tuple struct field accessed in a struct literal or field
457 /// expression.
458 pub enum Member {
459 /// A named field like `self.x`.
460 Named(Ident),
461 /// An unnamed field like `self.0`.
462 Unnamed(Index),
463 }
464}
465
David Tolnay85b69a42017-12-27 20:43:10 -0500466ast_struct! {
467 /// The index of an unnamed tuple struct field.
468 pub struct Index #manual_extra_traits {
469 pub index: u32,
470 pub span: Span,
471 }
472}
473
David Tolnay14982012017-12-29 00:49:51 -0500474impl From<usize> for Index {
475 fn from(index: usize) -> Index {
476 assert!(index < std::u32::MAX as usize);
477 Index {
478 index: index as u32,
479 span: Span::default(),
480 }
481 }
482}
483
484#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500485impl Eq for Index {}
486
David Tolnay14982012017-12-29 00:49:51 -0500487#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500488impl PartialEq for Index {
489 fn eq(&self, other: &Self) -> bool {
490 self.index == other.index
491 }
492}
493
David Tolnay14982012017-12-29 00:49:51 -0500494#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500495impl Hash for Index {
496 fn hash<H: Hasher>(&self, state: &mut H) {
497 self.index.hash(state);
498 }
499}
500
501#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700502ast_struct! {
David Tolnayd60cfec2017-12-29 00:21:38 -0500503 pub struct MethodTurbofish {
504 pub colon2_token: Token![::],
505 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500506 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500507 pub gt_token: Token![>],
508 }
509}
510
511#[cfg(feature = "full")]
512ast_enum! {
513 /// A individual generic argument like `T`.
514 pub enum GenericMethodArgument {
515 /// The type parameters for this path segment, if present.
516 Type(Type),
517 /// Const expression. Must be inside of a block.
518 ///
519 /// NOTE: Identity expressions are represented as Type arguments, as
520 /// they are indistinguishable syntactically.
521 Const(Expr),
522 }
523}
524
525#[cfg(feature = "full")]
526ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700527 /// A field-value pair in a struct literal.
528 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500529 /// Attributes tagged on the field.
530 pub attrs: Vec<Attribute>,
531
532 /// Name or index of the field.
533 pub member: Member,
534
David Tolnay5d7098a2017-12-29 01:35:24 -0500535 /// The colon in `Struct { x: x }`. If written in shorthand like
536 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500537 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500538
Alex Crichton62a0a592017-05-22 13:58:53 -0700539 /// Value of the field.
540 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 }
David Tolnay055a7042016-10-02 19:23:54 -0700542}
543
Michael Layzell734adb42017-06-07 16:58:31 -0400544#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700545ast_struct! {
David Tolnaybcd498f2017-12-29 12:02:33 -0500546 pub struct Label {
547 pub name: Lifetime,
548 pub colon_token: Token![:],
549 }
550}
551
552#[cfg(feature = "full")]
553ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700554 /// A Block (`{ .. }`).
555 ///
556 /// E.g. `{ .. }` as in `fn foo() { .. }`
557 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500558 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 /// Statements in a block
560 pub stmts: Vec<Stmt>,
561 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700562}
563
Michael Layzell734adb42017-06-07 16:58:31 -0400564#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700565ast_enum! {
566 /// A statement, usually ending in a semicolon.
567 pub enum Stmt {
568 /// A local (let) binding.
569 Local(Box<Local>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700570
Alex Crichton62a0a592017-05-22 13:58:53 -0700571 /// An item definition.
572 Item(Box<Item>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700573
Alex Crichton62a0a592017-05-22 13:58:53 -0700574 /// Expr without trailing semicolon.
575 Expr(Box<Expr>),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700576
Alex Crichton62a0a592017-05-22 13:58:53 -0700577 /// Expression with trailing semicolon;
David Tolnayf8db7ba2017-11-11 22:52:16 -0800578 Semi(Box<Expr>, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700579 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700580}
581
Michael Layzell734adb42017-06-07 16:58:31 -0400582#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700583ast_struct! {
584 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
585 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500586 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800587 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700588 pub pat: Box<Pat>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500589 pub ty: Option<(Token![:], Box<Type>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700590 /// Initializer expression to set the value, if any
David Tolnay8b4d3022017-12-29 12:11:10 -0500591 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500592 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700593 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700594}
595
Michael Layzell734adb42017-06-07 16:58:31 -0400596#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700597ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700598 // Clippy false positive
599 // https://github.com/Manishearth/rust-clippy/issues/1241
600 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
601 pub enum Pat {
602 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800604 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700605 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700606
Alex Crichton62a0a592017-05-22 13:58:53 -0700607 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
608 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
609 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
610 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700611 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500612 pub by_ref: Option<Token![ref]>,
613 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700614 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500615 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700616 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700617
Alex Crichton62a0a592017-05-22 13:58:53 -0700618 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
619 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700620 pub Struct(PatStruct {
621 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500622 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500623 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800624 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700625 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700626
Alex Crichton62a0a592017-05-22 13:58:53 -0700627 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
628 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
629 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700630 pub TupleStruct(PatTupleStruct {
631 pub path: Path,
632 pub pat: PatTuple,
633 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700634
Alex Crichton62a0a592017-05-22 13:58:53 -0700635 /// A possibly qualified path pattern.
636 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
637 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
638 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700639 pub Path(PatPath {
640 pub qself: Option<QSelf>,
641 pub path: Path,
642 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700643
Alex Crichton62a0a592017-05-22 13:58:53 -0700644 /// A tuple pattern `(a, b)`.
645 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
646 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700647 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500648 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500649 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500650 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500651 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500652 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700653 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700654 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700655 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800656 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500657 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700658 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700659 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700660 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800661 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500662 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500663 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700664 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700665 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700666 pub Lit(PatLit {
667 pub expr: Box<Expr>,
668 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800669 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700670 pub Range(PatRange {
671 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700672 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500673 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700674 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400675 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700676 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500677 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500678 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700679 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500680 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500681 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500682 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700683 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700684 /// A macro pattern; pre-expansion
David Tolnay323279a2017-12-29 11:26:32 -0500685 pub Macro(PatMacro {
686 pub mac: Macro,
687 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500688 pub Verbatim(PatVerbatim #manual_extra_traits {
689 pub tts: TokenStream,
690 }),
691 }
692}
693
David Tolnayc43b44e2017-12-30 23:55:54 -0500694#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500695impl Eq for PatVerbatim {}
696
David Tolnayc43b44e2017-12-30 23:55:54 -0500697#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500698impl PartialEq for PatVerbatim {
699 fn eq(&self, other: &Self) -> bool {
700 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
701 }
702}
703
David Tolnayc43b44e2017-12-30 23:55:54 -0500704#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500705impl Hash for PatVerbatim {
706 fn hash<H>(&self, state: &mut H)
707 where
708 H: Hasher,
709 {
710 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700711 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700712}
713
Michael Layzell734adb42017-06-07 16:58:31 -0400714#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700715ast_struct! {
716 /// An arm of a 'match'.
717 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800718 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700719 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500720 /// ```rust
721 /// # #![feature(dotdoteq_in_patterns)]
722 /// #
723 /// # fn main() {
724 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500726 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700727 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500728 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700729 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500730 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700731 /// ```
732 pub struct Arm {
733 pub attrs: Vec<Attribute>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500734 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500735 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800736 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700737 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800738 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700739 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700740}
741
Michael Layzell734adb42017-06-07 16:58:31 -0400742#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700743ast_enum! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700745 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 pub enum RangeLimits {
747 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800748 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700749 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800750 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700751 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700752}
753
Michael Layzell734adb42017-06-07 16:58:31 -0400754#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700755ast_struct! {
756 /// A single field in a struct pattern
757 ///
758 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay5d7098a2017-12-29 01:35:24 -0500759 /// are treated the same as `x: x, y: ref y, z: ref mut z` but
760 /// there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700761 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500762 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700763 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500764 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500765 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700766 /// The pattern the field is destructured to
767 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700768 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700769}
770
Michael Layzell3936ceb2017-07-08 00:28:36 -0400771#[cfg(any(feature = "parsing", feature = "printing"))]
772#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700773fn arm_expr_requires_comma(expr: &Expr) -> bool {
774 // see https://github.com/rust-lang/rust/blob/eb8f2586e
775 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500776 match *expr {
777 Expr::Unsafe(..)
778 | Expr::Block(..)
779 | Expr::If(..)
780 | Expr::IfLet(..)
781 | Expr::Match(..)
782 | Expr::While(..)
783 | Expr::WhileLet(..)
784 | Expr::Loop(..)
785 | Expr::ForLoop(..)
786 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700787 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400788 }
789}
790
David Tolnayb9c8e322016-09-23 20:48:37 -0700791#[cfg(feature = "parsing")]
792pub mod parsing {
793 use super::*;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500794 use ty::parsing::qpath;
795 #[cfg(feature = "full")]
796 use ty::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700797
Michael Layzell734adb42017-06-07 16:58:31 -0400798 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -0800799 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500800 use synom::Synom;
801 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400802 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500803 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500804 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700805
David Tolnaybcf26022017-12-25 22:10:52 -0500806 // When we're parsing expressions which occur before blocks, like in an if
807 // statement's condition, we cannot parse a struct literal.
808 //
809 // Struct literals are ambiguous in certain positions
810 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700811 macro_rules! ambiguous_expr {
812 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700813 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700814 };
815 }
816
David Tolnaybcf26022017-12-25 22:10:52 -0500817 // When we are parsing an optional suffix expression, we cannot allow blocks
818 // if structs are not allowed.
819 //
820 // Example:
821 //
822 // if break {} {}
823 //
824 // is ambiguous between:
825 //
826 // if (break {}) {}
827 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400828 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400829 macro_rules! opt_ambiguous_expr {
830 ($i:expr, $allow_struct:ident) => {
831 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
832 };
833 }
834
Alex Crichton954046c2017-05-30 21:49:42 -0700835 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400836 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700837
838 fn description() -> Option<&'static str> {
839 Some("expression")
840 }
841 }
842
Michael Layzell734adb42017-06-07 16:58:31 -0400843 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700844 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
845
David Tolnaybcf26022017-12-25 22:10:52 -0500846 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400847 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500848 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500849 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400850 }
851
Michael Layzell734adb42017-06-07 16:58:31 -0400852 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500853 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500854 // NOTE: We intentionally skip assign_expr, placement_expr, and
855 // range_expr, as they are not parsed in non-full mode.
856 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400857 }
858
David Tolnaybcf26022017-12-25 22:10:52 -0500859 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400860 macro_rules! binop {
861 (
862 $name: ident,
863 $next: ident,
864 $submac: ident!( $($args:tt)* )
865 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500866 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400867 mut e: call!($next, allow_struct, allow_block) >>
868 many0!(do_parse!(
869 op: $submac!($($args)*) >>
870 rhs: call!($next, allow_struct, true) >>
871 ({
872 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500873 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400874 left: Box::new(e.into()),
875 op: op,
876 right: Box::new(rhs.into()),
877 }.into();
878 })
879 )) >>
880 (e)
881 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700882 }
David Tolnay54e854d2016-10-24 12:03:30 -0700883 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700884
David Tolnaybcf26022017-12-25 22:10:52 -0500885 // <placement> = <placement> ..
886 // <placement> += <placement> ..
887 // <placement> -= <placement> ..
888 // <placement> *= <placement> ..
889 // <placement> /= <placement> ..
890 // <placement> %= <placement> ..
891 // <placement> ^= <placement> ..
892 // <placement> &= <placement> ..
893 // <placement> |= <placement> ..
894 // <placement> <<= <placement> ..
895 // <placement> >>= <placement> ..
896 //
897 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400898 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500899 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400900 mut e: call!(placement_expr, allow_struct, allow_block) >>
901 alt!(
902 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800903 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400904 // Recurse into self to parse right-associative operator.
905 rhs: call!(assign_expr, allow_struct, true) >>
906 ({
907 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500908 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500909 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400910 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -0500911 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400912 }.into();
913 })
914 )
915 |
916 do_parse!(
917 op: call!(BinOp::parse_assign_op) >>
918 // Recurse into self to parse right-associative operator.
919 rhs: call!(assign_expr, allow_struct, true) >>
920 ({
921 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500922 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500923 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400924 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -0500925 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400926 }.into();
927 })
928 )
929 |
930 epsilon!()
931 ) >>
932 (e)
933 ));
934
David Tolnaybcf26022017-12-25 22:10:52 -0500935 // <range> <- <range> ..
936 //
937 // NOTE: The `in place { expr }` version of this syntax is parsed in
938 // `atom_expr`, not here.
939 //
940 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400941 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500942 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400943 mut e: call!(range_expr, allow_struct, allow_block) >>
944 alt!(
945 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800946 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400947 // Recurse into self to parse right-associative operator.
948 rhs: call!(placement_expr, allow_struct, true) >>
949 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400950 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500951 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400952 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -0500953 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -0500954 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -0500955 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400956 }.into();
957 })
958 )
959 |
960 epsilon!()
961 ) >>
962 (e)
963 ));
964
David Tolnaybcf26022017-12-25 22:10:52 -0500965 // <or> ... <or> ..
966 // <or> .. <or> ..
967 // <or> ..
968 //
969 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
970 // rules are for parsing these expressions are, but this is not correct.
971 // For example, `a .. b .. c` is not a legal expression. It should not
972 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
973 //
974 // NOTE: The form of ranges which don't include a preceding expression are
975 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400976 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500977 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978 mut e: call!(or_expr, allow_struct, allow_block) >>
979 many0!(do_parse!(
980 limits: syn!(RangeLimits) >>
981 // We don't want to allow blocks here if we don't allow structs. See
982 // the reasoning for `opt_ambiguous_expr!` above.
983 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
984 ({
985 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500986 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500987 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400988 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -0500989 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400990 }.into();
991 })
992 )) >>
993 (e)
994 ));
995
David Tolnaybcf26022017-12-25 22:10:52 -0500996 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800997 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400998
David Tolnaybcf26022017-12-25 22:10:52 -0500999 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001000 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001001
David Tolnaybcf26022017-12-25 22:10:52 -05001002 // <bitor> == <bitor> ...
1003 // <bitor> != <bitor> ...
1004 // <bitor> >= <bitor> ...
1005 // <bitor> <= <bitor> ...
1006 // <bitor> > <bitor> ...
1007 // <bitor> < <bitor> ...
1008 //
1009 // NOTE: This operator appears to be parsed as left-associative, but errors
1010 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001011 binop!(
1012 compare_expr,
1013 bitor_expr,
1014 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001015 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001016 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001018 |
1019 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001020 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001021 |
1022 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001024 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001025 do_parse!(
1026 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001027 not!(punct!(<-)) >>
1028 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001029 (BinOp::Lt(t))
1030 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001031 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001032 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001033 )
1034 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001035
David Tolnaybcf26022017-12-25 22:10:52 -05001036 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001037 binop!(
1038 bitor_expr,
1039 bitxor_expr,
1040 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1041 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001044 binop!(
1045 bitxor_expr,
1046 bitand_expr,
1047 do_parse!(
1048 // NOTE: Make sure we aren't looking at ^=.
1049 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1050 )
1051 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001052
David Tolnaybcf26022017-12-25 22:10:52 -05001053 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001054 binop!(
1055 bitand_expr,
1056 shift_expr,
1057 do_parse!(
1058 // NOTE: Make sure we aren't looking at && or &=.
1059 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1060 )
1061 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001062
David Tolnaybcf26022017-12-25 22:10:52 -05001063 // <arith> << <arith> ...
1064 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001065 binop!(
1066 shift_expr,
1067 arith_expr,
1068 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001069 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001070 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001071 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001072 )
1073 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001074
David Tolnaybcf26022017-12-25 22:10:52 -05001075 // <term> + <term> ...
1076 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001077 binop!(
1078 arith_expr,
1079 term_expr,
1080 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001081 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001082 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001083 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001084 )
1085 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001086
David Tolnaybcf26022017-12-25 22:10:52 -05001087 // <cast> * <cast> ...
1088 // <cast> / <cast> ...
1089 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001090 binop!(
1091 term_expr,
1092 cast_expr,
1093 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001094 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001095 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001096 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001097 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001098 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001099 )
1100 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001101
David Tolnaybcf26022017-12-25 22:10:52 -05001102 // <unary> as <ty>
1103 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001104 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001105 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001106 mut e: call!(unary_expr, allow_struct, allow_block) >>
1107 many0!(alt!(
1108 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001109 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001110 // We can't accept `A + B` in cast expressions, as it's
1111 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001112 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001113 ({
1114 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001115 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001116 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001117 as_token: as_,
1118 ty: Box::new(ty),
1119 }.into();
1120 })
1121 )
1122 |
1123 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001124 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001125 // We can't accept `A + B` in cast expressions, as it's
1126 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001127 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001128 ({
1129 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001130 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001131 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001132 colon_token: colon,
1133 ty: Box::new(ty),
1134 }.into();
1135 })
1136 )
1137 )) >>
1138 (e)
1139 ));
1140
David Tolnay0cf94f22017-12-28 23:46:26 -05001141 // <unary> as <ty>
1142 #[cfg(not(feature = "full"))]
1143 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1144 mut e: call!(unary_expr, allow_struct, allow_block) >>
1145 many0!(do_parse!(
1146 as_: keyword!(as) >>
1147 // We can't accept `A + B` in cast expressions, as it's
1148 // ambiguous with the + expression.
1149 ty: call!(Type::without_plus) >>
1150 ({
1151 e = ExprCast {
1152 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001153 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001154 as_token: as_,
1155 ty: Box::new(ty),
1156 }.into();
1157 })
1158 )) >>
1159 (e)
1160 ));
1161
David Tolnaybcf26022017-12-25 22:10:52 -05001162 // <UnOp> <trailer>
1163 // & <trailer>
1164 // &mut <trailer>
1165 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001166 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001167 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001168 do_parse!(
1169 op: syn!(UnOp) >>
1170 expr: call!(unary_expr, allow_struct, true) >>
1171 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001172 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001173 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001174 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001175 }.into())
1176 )
1177 |
1178 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001179 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001180 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 expr: call!(unary_expr, allow_struct, true) >>
1182 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001183 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001184 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001185 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001186 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001187 }.into())
1188 )
1189 |
1190 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001191 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001192 expr: call!(unary_expr, allow_struct, true) >>
1193 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001194 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001195 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001196 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001197 }.into())
1198 )
1199 |
1200 call!(trailer_expr, allow_struct, allow_block)
1201 ));
1202
Michael Layzell734adb42017-06-07 16:58:31 -04001203 // XXX: This duplication is ugly
1204 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001205 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001206 do_parse!(
1207 op: syn!(UnOp) >>
1208 expr: call!(unary_expr, allow_struct, true) >>
1209 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001210 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001211 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001212 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001213 }.into())
1214 )
1215 |
1216 call!(trailer_expr, allow_struct, allow_block)
1217 ));
1218
David Tolnaybcf26022017-12-25 22:10:52 -05001219 // <atom> (..<args>) ...
1220 // <atom> . <ident> (..<args>) ...
1221 // <atom> . <ident> ...
1222 // <atom> . <lit> ...
1223 // <atom> [ <expr> ] ...
1224 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001225 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001226 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001227 mut e: call!(atom_expr, allow_struct, allow_block) >>
1228 many0!(alt!(
1229 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001230 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001231 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001232 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001233 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001234 args: args,
1235 paren_token: paren,
1236 }.into();
1237 })
1238 |
1239 tap!(more: and_method_call => {
1240 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001241 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 e = call.into();
1243 })
1244 |
1245 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001246 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001248 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001249 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001250 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001251 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001252 }.into();
1253 })
1254 |
1255 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001256 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001257 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001258 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001259 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001260 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001261 index: Box::new(i),
1262 }.into();
1263 })
1264 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001265 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001266 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001267 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001268 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001269 question_token: question,
1270 }.into();
1271 })
1272 )) >>
1273 (e)
1274 ));
1275
Michael Layzell734adb42017-06-07 16:58:31 -04001276 // XXX: Duplication == ugly
1277 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001278 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001279 mut e: call!(atom_expr, allow_struct, allow_block) >>
1280 many0!(alt!(
1281 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001282 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001283 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001284 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001285 paren_token: args.0,
1286 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001287 }.into();
1288 })
1289 |
1290 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001291 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001292 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001293 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001294 bracket_token: i.0,
1295 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001296 }.into();
1297 })
1298 )) >>
1299 (e)
1300 ));
1301
David Tolnaybcf26022017-12-25 22:10:52 -05001302 // Parse all atomic expressions which don't have to worry about precidence
1303 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001304 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001305 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1306 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001307 |
David Tolnay8c91b882017-12-28 23:04:32 -05001308 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001309 |
1310 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001311 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001314 |
David Tolnay8c91b882017-12-28 23:04:32 -05001315 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001316 |
1317 call!(expr_break, allow_struct) // must be before expr_path
1318 |
David Tolnay8c91b882017-12-28 23:04:32 -05001319 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
1321 call!(expr_ret, allow_struct) // must be before expr_path
1322 |
David Tolnay8c91b882017-12-28 23:04:32 -05001323 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001324 |
David Tolnay8c91b882017-12-28 23:04:32 -05001325 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326 |
David Tolnay8c91b882017-12-28 23:04:32 -05001327 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001328 |
David Tolnay8c91b882017-12-28 23:04:32 -05001329 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 |
David Tolnay8c91b882017-12-28 23:04:32 -05001331 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 |
David Tolnay8c91b882017-12-28 23:04:32 -05001333 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 |
David Tolnay8c91b882017-12-28 23:04:32 -05001335 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001336 |
David Tolnay8c91b882017-12-28 23:04:32 -05001337 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338 |
David Tolnay8c91b882017-12-28 23:04:32 -05001339 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001340 |
David Tolnay8c91b882017-12-28 23:04:32 -05001341 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001342 |
David Tolnay8c91b882017-12-28 23:04:32 -05001343 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001344 |
David Tolnay8c91b882017-12-28 23:04:32 -05001345 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001346 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001347 call!(expr_closure, allow_struct)
1348 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001349 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001350 |
1351 // NOTE: This is the prefix-form of range
1352 call!(expr_range, allow_struct)
1353 |
David Tolnay8c91b882017-12-28 23:04:32 -05001354 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001355 |
David Tolnay8c91b882017-12-28 23:04:32 -05001356 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001357 ));
1358
Michael Layzell734adb42017-06-07 16:58:31 -04001359 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001360 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001361 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001362 |
David Tolnay8c91b882017-12-28 23:04:32 -05001363 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001364 ));
1365
Michael Layzell734adb42017-06-07 16:58:31 -04001366 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001367 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001368 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001369 |
David Tolnay8c91b882017-12-28 23:04:32 -05001370 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001371 |
David Tolnay8c91b882017-12-28 23:04:32 -05001372 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001373 |
David Tolnay8c91b882017-12-28 23:04:32 -05001374 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001375 |
David Tolnay8c91b882017-12-28 23:04:32 -05001376 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001377 |
David Tolnay8c91b882017-12-28 23:04:32 -05001378 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001379 |
David Tolnay8c91b882017-12-28 23:04:32 -05001380 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001381 |
David Tolnay8c91b882017-12-28 23:04:32 -05001382 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001383 |
David Tolnay8c91b882017-12-28 23:04:32 -05001384 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001385 |
David Tolnay8c91b882017-12-28 23:04:32 -05001386 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001387 |
David Tolnay8c91b882017-12-28 23:04:32 -05001388 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001389 ), Expr::from));
1390
David Tolnay8c91b882017-12-28 23:04:32 -05001391 impl Synom for ExprLit {
1392 named!(parse -> Self, do_parse!(
1393 lit: syn!(Lit) >>
1394 (ExprLit {
1395 attrs: Vec::new(),
1396 lit: lit,
1397 })
1398 ));
1399 }
1400
1401 #[cfg(feature = "full")]
1402 impl Synom for ExprMacro {
1403 named!(parse -> Self, do_parse!(
1404 mac: syn!(Macro) >>
1405 (ExprMacro {
1406 attrs: Vec::new(),
1407 mac: mac,
1408 })
1409 ));
1410 }
1411
David Tolnaye98775f2017-12-28 23:17:00 -05001412 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001413 impl Synom for ExprGroup {
1414 named!(parse -> Self, do_parse!(
1415 e: grouped!(syn!(Expr)) >>
1416 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001417 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001418 expr: Box::new(e.1),
1419 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001420 })
Michael Layzell93c36282017-06-04 20:43:14 -04001421 ));
1422 }
1423
David Tolnaye98775f2017-12-28 23:17:00 -05001424 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001425 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001426 named!(parse -> Self, do_parse!(
1427 e: parens!(syn!(Expr)) >>
1428 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001429 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001430 paren_token: e.0,
1431 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001432 })
Michael Layzell92639a52017-06-01 00:07:44 -04001433 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001434 }
David Tolnay89e05672016-10-02 14:39:42 -07001435
Michael Layzell734adb42017-06-07 16:58:31 -04001436 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001437 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001438 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001439 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001440 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001441 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001442 bracket_token: elems.0,
1443 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001444 })
1445 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001446
1447 fn description() -> Option<&'static str> {
1448 Some("array")
1449 }
Alex Crichton954046c2017-05-30 21:49:42 -07001450 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001451
David Tolnayf2cfd722017-12-31 18:02:51 -05001452 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1453 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001454
Michael Layzell734adb42017-06-07 16:58:31 -04001455 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001456 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001457 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001458 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001459 turbofish: option!(tuple!(
1460 punct!(::),
1461 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001462 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001463 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001464 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001465 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001466 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001467 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001468 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001469 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001470 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1471 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001472 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001473
Alex Crichton954046c2017-05-30 21:49:42 -07001474 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001475 turbofish: turbofish.map(|fish| MethodTurbofish {
1476 colon2_token: fish.0,
1477 lt_token: fish.1,
1478 args: fish.2,
1479 gt_token: fish.3,
1480 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001481 args: args.1,
1482 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001483 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001484 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001485 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001486 ));
1487
Michael Layzell734adb42017-06-07 16:58:31 -04001488 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001489 impl Synom for GenericMethodArgument {
1490 // TODO parse const generics as well
1491 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1492 }
1493
1494 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001495 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001496 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001497 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001498 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001499 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001500 elems: elems.1,
1501 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001502 })
1503 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001504
1505 fn description() -> Option<&'static str> {
1506 Some("tuple")
1507 }
Alex Crichton954046c2017-05-30 21:49:42 -07001508 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001509
Michael Layzell734adb42017-06-07 16:58:31 -04001510 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001511 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001512 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001513 if_: keyword!(if) >>
1514 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001515 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001516 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001517 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001518 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001519 else_block: option!(else_block) >>
1520 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001521 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001522 pat: Box::new(pat),
1523 let_token: let_,
1524 eq_token: eq,
1525 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001526 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001527 brace_token: then_block.0,
1528 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001529 },
1530 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001531 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001532 })
1533 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001534
1535 fn description() -> Option<&'static str> {
1536 Some("`if let` expression")
1537 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001538 }
1539
Michael Layzell734adb42017-06-07 16:58:31 -04001540 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001541 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001542 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001543 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001544 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001545 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001546 else_block: option!(else_block) >>
1547 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001548 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001549 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001550 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001551 brace_token: then_block.0,
1552 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001553 },
1554 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001555 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001556 })
1557 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001558
1559 fn description() -> Option<&'static str> {
1560 Some("`if` expression")
1561 }
Alex Crichton954046c2017-05-30 21:49:42 -07001562 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001563
Michael Layzell734adb42017-06-07 16:58:31 -04001564 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001565 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001566 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001567 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001568 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001569 |
David Tolnay8c91b882017-12-28 23:04:32 -05001570 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001571 |
1572 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001573 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001574 (Expr::Block(ExprBlock {
1575 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001576 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001577 brace_token: else_block.0,
1578 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001579 },
1580 }))
David Tolnay939766a2016-09-23 23:48:12 -07001581 )
Alex Crichton954046c2017-05-30 21:49:42 -07001582 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001583 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001584 ));
1585
Michael Layzell734adb42017-06-07 16:58:31 -04001586 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001587 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001588 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001589 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001590 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001591 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001592 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001593 expr: expr_no_struct >>
1594 loop_block: syn!(Block) >>
1595 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001596 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001597 for_token: for_,
1598 in_token: in_,
1599 pat: Box::new(pat),
1600 expr: Box::new(expr),
1601 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001602 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001603 })
1604 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001605
1606 fn description() -> Option<&'static str> {
1607 Some("`for` loop")
1608 }
Alex Crichton954046c2017-05-30 21:49:42 -07001609 }
Gregory Katze5f35682016-09-27 14:20:55 -04001610
Michael Layzell734adb42017-06-07 16:58:31 -04001611 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001612 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001613 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001614 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001615 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001616 loop_block: syn!(Block) >>
1617 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001618 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001619 loop_token: loop_,
1620 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001621 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001622 })
1623 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001624
1625 fn description() -> Option<&'static str> {
1626 Some("`loop`")
1627 }
Alex Crichton954046c2017-05-30 21:49:42 -07001628 }
1629
Michael Layzell734adb42017-06-07 16:58:31 -04001630 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001631 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001632 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001633 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001634 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001635 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001636 (ExprMatch {
1637 attrs: Vec::new(),
1638 expr: Box::new(obj),
1639 match_token: match_,
1640 brace_token: res.0,
1641 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001642 })
1643 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001644
1645 fn description() -> Option<&'static str> {
1646 Some("`match` expression")
1647 }
Alex Crichton954046c2017-05-30 21:49:42 -07001648 }
David Tolnay1978c672016-10-27 22:05:52 -07001649
Michael Layzell734adb42017-06-07 16:58:31 -04001650 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001651 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001652 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001653 do_: keyword!(do) >>
1654 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001655 catch_block: syn!(Block) >>
1656 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001657 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001658 block: catch_block,
1659 do_token: do_,
1660 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001661 })
Michael Layzell92639a52017-06-01 00:07:44 -04001662 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001663
1664 fn description() -> Option<&'static str> {
1665 Some("`catch` expression")
1666 }
Alex Crichton954046c2017-05-30 21:49:42 -07001667 }
Arnavion02ef13f2017-04-25 00:54:31 -07001668
Michael Layzell734adb42017-06-07 16:58:31 -04001669 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001670 impl Synom for ExprYield {
1671 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001672 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001673 expr: option!(syn!(Expr)) >>
1674 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001675 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001676 yield_token: yield_,
1677 expr: expr.map(Box::new),
1678 })
1679 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001680
1681 fn description() -> Option<&'static str> {
1682 Some("`yield` expression")
1683 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001684 }
1685
1686 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001687 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001688 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001689 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001690 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001691 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1692 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001693 body: do_parse!(
1694 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001695 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1696 true => alt!(
1697 input_end!() => { |_| None }
1698 |
1699 punct!(,) => { Some }
1700 )
Alex Crichton03b30272017-08-28 09:35:24 -07001701 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001702 false => option!(punct!(,))
1703 ) >>
1704 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001705 ) >>
1706 (Arm {
1707 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001708 attrs: attrs,
1709 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001710 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001711 body: Box::new(body.0),
1712 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001713 })
1714 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001715
1716 fn description() -> Option<&'static str> {
1717 Some("`match` arm")
1718 }
Alex Crichton954046c2017-05-30 21:49:42 -07001719 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001720
Michael Layzell734adb42017-06-07 16:58:31 -04001721 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001722 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001723 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001724 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001725 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001726 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001727 ret_and_body: alt!(
1728 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001729 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001730 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001731 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001732 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001733 Expr::Block(ExprBlock {
1734 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001735 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001736 }))
David Tolnay89e05672016-10-02 14:39:42 -07001737 )
1738 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001739 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001740 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001741 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001742 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001743 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001744 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001745 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001746 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001747 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001748 body: Box::new(ret_and_body.1),
1749 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001750 ));
1751
Michael Layzell734adb42017-06-07 16:58:31 -04001752 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001753 named!(fn_arg -> FnArg, do_parse!(
1754 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001755 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001756 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001757 if let Some((colon, ty)) = ty {
1758 FnArg::Captured(ArgCaptured {
1759 pat: pat,
1760 colon_token: colon,
1761 ty: ty,
1762 })
1763 } else {
1764 FnArg::Inferred(pat)
1765 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001766 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001767 ));
1768
Michael Layzell734adb42017-06-07 16:58:31 -04001769 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001770 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001771 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001772 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001773 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001774 cond: expr_no_struct >>
1775 while_block: syn!(Block) >>
1776 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001777 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001778 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001779 cond: Box::new(cond),
1780 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001781 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001782 })
1783 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001784
1785 fn description() -> Option<&'static str> {
1786 Some("`while` expression")
1787 }
Alex Crichton954046c2017-05-30 21:49:42 -07001788 }
1789
Michael Layzell734adb42017-06-07 16:58:31 -04001790 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001791 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001792 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001793 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001794 while_: keyword!(while) >>
1795 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001796 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001797 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001798 value: expr_no_struct >>
1799 while_block: syn!(Block) >>
1800 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001801 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001802 eq_token: eq,
1803 let_token: let_,
1804 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001805 pat: Box::new(pat),
1806 expr: Box::new(value),
1807 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001808 label: label,
1809 })
1810 ));
1811 }
1812
1813 #[cfg(feature = "full")]
1814 impl Synom for Label {
1815 named!(parse -> Self, do_parse!(
1816 name: syn!(Lifetime) >>
1817 colon: punct!(:) >>
1818 (Label {
1819 name: name,
1820 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001821 })
1822 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001823
1824 fn description() -> Option<&'static str> {
1825 Some("`while let` expression")
1826 }
Alex Crichton954046c2017-05-30 21:49:42 -07001827 }
1828
Michael Layzell734adb42017-06-07 16:58:31 -04001829 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001830 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001831 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001832 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001833 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001834 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001835 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001836 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001837 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001838 })
1839 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001840
1841 fn description() -> Option<&'static str> {
1842 Some("`continue`")
1843 }
Alex Crichton954046c2017-05-30 21:49:42 -07001844 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001845
Michael Layzell734adb42017-06-07 16:58:31 -04001846 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001847 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001848 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001849 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001850 // We can't allow blocks after a `break` expression when we wouldn't
1851 // allow structs, as this expression is ambiguous.
1852 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001853 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001854 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001855 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001856 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001857 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001858 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001859 ));
1860
Michael Layzell734adb42017-06-07 16:58:31 -04001861 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001862 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001863 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001864 // NOTE: return is greedy and eats blocks after it even when in a
1865 // position where structs are not allowed, such as in if statement
1866 // conditions. For example:
1867 //
David Tolnaybcf26022017-12-25 22:10:52 -05001868 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001869 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001870 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001871 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001872 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001873 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001874 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001875 ));
1876
Michael Layzell734adb42017-06-07 16:58:31 -04001877 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001878 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001879 named!(parse -> Self, do_parse!(
1880 path: syn!(Path) >>
1881 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001882 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001883 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
1884 dots: punct!(..) >>
1885 base: syn!(Expr) >>
1886 (dots, base)
1887 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001888 (fields, base)
1889 )) >>
1890 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001891 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04001892 let (dots, rest) = match base.and_then(|b| b) {
1893 Some((dots, base)) => (Some(dots), Some(base)),
1894 None => (None, None),
1895 };
1896 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001897 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001898 brace_token: brace,
1899 path: path,
1900 fields: fields,
1901 dot2_token: dots,
1902 rest: rest.map(Box::new),
1903 }
1904 })
1905 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001906
1907 fn description() -> Option<&'static str> {
1908 Some("struct literal expression")
1909 }
Alex Crichton954046c2017-05-30 21:49:42 -07001910 }
1911
Michael Layzell734adb42017-06-07 16:58:31 -04001912 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001913 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001914 named!(parse -> Self, alt!(
1915 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001916 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001917 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001918 value: syn!(Expr) >>
1919 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001920 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001921 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001922 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001923 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001924 })
Michael Layzell92639a52017-06-01 00:07:44 -04001925 )
1926 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001927 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001928 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001929 expr: Expr::Path(ExprPath {
1930 attrs: Vec::new(),
1931 qself: None,
1932 path: name.into(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001933 }),
Michael Layzell92639a52017-06-01 00:07:44 -04001934 attrs: Vec::new(),
1935 colon_token: None,
1936 })
1937 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001938
1939 fn description() -> Option<&'static str> {
1940 Some("field-value pair: `field: value`")
1941 }
Alex Crichton954046c2017-05-30 21:49:42 -07001942 }
David Tolnay055a7042016-10-02 19:23:54 -07001943
Michael Layzell734adb42017-06-07 16:58:31 -04001944 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001945 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001946 named!(parse -> Self, do_parse!(
1947 data: brackets!(do_parse!(
1948 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001949 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001950 times: syn!(Expr) >>
1951 (value, semi, times)
1952 )) >>
1953 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001954 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001955 expr: Box::new((data.1).0),
1956 amt: Box::new((data.1).2),
1957 bracket_token: data.0,
1958 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001959 })
1960 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001961
1962 fn description() -> Option<&'static str> {
1963 Some("repeated array literal: `[val; N]`")
1964 }
Alex Crichton954046c2017-05-30 21:49:42 -07001965 }
David Tolnay055a7042016-10-02 19:23:54 -07001966
Michael Layzell734adb42017-06-07 16:58:31 -04001967 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001968 impl Synom for ExprUnsafe {
1969 named!(parse -> Self, do_parse!(
1970 unsafe_: keyword!(unsafe) >>
1971 b: syn!(Block) >>
1972 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001973 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001974 unsafe_token: unsafe_,
1975 block: b,
1976 })
1977 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001978
1979 fn description() -> Option<&'static str> {
1980 Some("unsafe block: `unsafe { .. }`")
1981 }
Nika Layzell640832a2017-12-04 13:37:09 -05001982 }
1983
1984 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001985 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001986 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001987 b: syn!(Block) >>
1988 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001989 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001990 block: b,
1991 })
1992 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001993
1994 fn description() -> Option<&'static str> {
1995 Some("block: `{ .. }`")
1996 }
Alex Crichton954046c2017-05-30 21:49:42 -07001997 }
David Tolnay89e05672016-10-02 14:39:42 -07001998
Michael Layzell734adb42017-06-07 16:58:31 -04001999 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002000 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002001 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002002 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002003 (ExprRange {
2004 attrs: Vec::new(),
2005 from: None,
2006 to: hi.map(Box::new),
2007 limits: limits,
2008 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002009 ));
2010
Michael Layzell734adb42017-06-07 16:58:31 -04002011 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002012 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002013 named!(parse -> Self, alt!(
2014 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002015 punct!(..=) => { RangeLimits::Closed }
2016 |
2017 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002018 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002019 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002020 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002021 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002022
2023 fn description() -> Option<&'static str> {
2024 Some("range limit: `..`, `...` or `..=`")
2025 }
Alex Crichton954046c2017-05-30 21:49:42 -07002026 }
David Tolnay438c9052016-10-07 23:24:48 -07002027
Alex Crichton954046c2017-05-30 21:49:42 -07002028 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002029 named!(parse -> Self, do_parse!(
2030 pair: qpath >>
2031 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002032 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002033 qself: pair.0,
2034 path: pair.1,
2035 })
2036 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002037
2038 fn description() -> Option<&'static str> {
2039 Some("path: `a::b::c`")
2040 }
Alex Crichton954046c2017-05-30 21:49:42 -07002041 }
David Tolnay42602292016-10-01 22:25:45 -07002042
Michael Layzell734adb42017-06-07 16:58:31 -04002043 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002044 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002045
David Tolnay8875fca2017-12-31 13:52:37 -05002046 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002047
Michael Layzell734adb42017-06-07 16:58:31 -04002048 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002049 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002050 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002051 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002052 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002053 brace_token: stmts.0,
2054 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002055 })
2056 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002057
2058 fn description() -> Option<&'static str> {
2059 Some("block: `{ .. }`")
2060 }
Alex Crichton954046c2017-05-30 21:49:42 -07002061 }
David Tolnay939766a2016-09-23 23:48:12 -07002062
Michael Layzell734adb42017-06-07 16:58:31 -04002063 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002064 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002065 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002066 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002067 mut standalone: many0!(do_parse!(
2068 stmt: syn!(Stmt) >>
2069 many0!(punct!(;)) >>
2070 (stmt)
2071 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002072 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002073 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002074 mut e: syn!(Expr) >>
2075 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002076 e.replace_attrs(attrs);
Alex Crichton70bbd592017-08-27 10:40:03 -07002077 Stmt::Expr(Box::new(e))
2078 })
2079 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002080 (match last {
2081 None => standalone,
2082 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002083 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002084 standalone
2085 }
2086 })
2087 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002088 }
2089
Michael Layzell734adb42017-06-07 16:58:31 -04002090 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002091 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002092 named!(parse -> Self, alt!(
2093 stmt_mac
2094 |
2095 stmt_local
2096 |
2097 stmt_item
2098 |
Michael Layzell35418782017-06-07 09:20:25 -04002099 stmt_blockexpr
2100 |
Michael Layzell92639a52017-06-01 00:07:44 -04002101 stmt_expr
2102 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002103
2104 fn description() -> Option<&'static str> {
2105 Some("statement")
2106 }
Alex Crichton954046c2017-05-30 21:49:42 -07002107 }
David Tolnay939766a2016-09-23 23:48:12 -07002108
Michael Layzell734adb42017-06-07 16:58:31 -04002109 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002110 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002111 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002112 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002113 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002114 // Only parse braces here; paren and bracket will get parsed as
2115 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002116 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002117 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05002118 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
2119 attrs: attrs,
2120 ident: None,
2121 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002122 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002123 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002124 delimiter: MacroDelimiter::Brace(data.0),
2125 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002126 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002127 semi_token: semi,
2128 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07002129 ));
2130
Michael Layzell734adb42017-06-07 16:58:31 -04002131 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002132 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002133 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002134 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002135 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002136 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002137 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2138 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07002139 (Stmt::Local(Box::new(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002140 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002141 let_token: let_,
2142 pat: Box::new(pat),
2143 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2144 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2145 semi_token: semi,
David Tolnay191e0582016-10-02 18:31:09 -07002146 })))
2147 ));
2148
Michael Layzell734adb42017-06-07 16:58:31 -04002149 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002150 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002151
Michael Layzell734adb42017-06-07 16:58:31 -04002152 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002153 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002154 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002155 mut e: expr_nosemi >>
2156 // If the next token is a `.` or a `?` it is special-cased to parse as
2157 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002158 not!(punct!(.)) >>
2159 not!(punct!(?)) >>
2160 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002161 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002162 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002163 if let Some(semi) = semi {
2164 Stmt::Semi(Box::new(e), semi)
2165 } else {
2166 Stmt::Expr(Box::new(e))
2167 }
2168 })
2169 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002170
Michael Layzell734adb42017-06-07 16:58:31 -04002171 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002172 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002173 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002174 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002175 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002176 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002177 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002178 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002179 })
David Tolnay939766a2016-09-23 23:48:12 -07002180 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002181
Michael Layzell734adb42017-06-07 16:58:31 -04002182 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002183 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002184 named!(parse -> Self, alt!(
2185 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2186 |
2187 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2188 |
2189 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2190 |
2191 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2192 |
2193 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2194 |
David Tolnay323279a2017-12-29 11:26:32 -05002195 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002196 |
2197 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2198 |
2199 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2200 |
2201 syn!(PatPath) => { Pat::Path }
2202 |
2203 syn!(PatTuple) => { Pat::Tuple }
2204 |
2205 syn!(PatRef) => { Pat::Ref }
2206 |
2207 syn!(PatSlice) => { Pat::Slice }
2208 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002209
2210 fn description() -> Option<&'static str> {
2211 Some("pattern")
2212 }
Alex Crichton954046c2017-05-30 21:49:42 -07002213 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002214
Michael Layzell734adb42017-06-07 16:58:31 -04002215 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002216 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002217 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002218 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002219 |u| PatWild { underscore_token: u }
2220 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002221
2222 fn description() -> Option<&'static str> {
2223 Some("wild pattern: `_`")
2224 }
Alex Crichton954046c2017-05-30 21:49:42 -07002225 }
David Tolnay84aa0752016-10-02 23:01:13 -07002226
Michael Layzell734adb42017-06-07 16:58:31 -04002227 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002228 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002229 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002230 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002231 pat: syn!(Pat) >>
2232 (PatBox {
2233 pat: Box::new(pat),
2234 box_token: boxed,
2235 })
2236 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002237
2238 fn description() -> Option<&'static str> {
2239 Some("box pattern")
2240 }
Alex Crichton954046c2017-05-30 21:49:42 -07002241 }
2242
Michael Layzell734adb42017-06-07 16:58:31 -04002243 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002244 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002245 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002246 by_ref: option!(keyword!(ref)) >>
2247 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002248 name: alt!(
2249 syn!(Ident)
2250 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002251 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002252 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002253 not!(punct!(<)) >>
2254 not!(punct!(::)) >>
2255 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002256 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002257 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002258 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002259 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002260 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002261 })
2262 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002263
2264 fn description() -> Option<&'static str> {
2265 Some("pattern identifier binding")
2266 }
Alex Crichton954046c2017-05-30 21:49:42 -07002267 }
2268
Michael Layzell734adb42017-06-07 16:58:31 -04002269 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002270 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002271 named!(parse -> Self, do_parse!(
2272 path: syn!(Path) >>
2273 tuple: syn!(PatTuple) >>
2274 (PatTupleStruct {
2275 path: path,
2276 pat: tuple,
2277 })
2278 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002279
2280 fn description() -> Option<&'static str> {
2281 Some("tuple struct pattern")
2282 }
Alex Crichton954046c2017-05-30 21:49:42 -07002283 }
2284
Michael Layzell734adb42017-06-07 16:58:31 -04002285 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002286 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002287 named!(parse -> Self, do_parse!(
2288 path: syn!(Path) >>
2289 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002290 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002291 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002292 (fields, base)
2293 )) >>
2294 (PatStruct {
2295 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002296 fields: (data.1).0,
2297 brace_token: data.0,
2298 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002299 })
2300 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002301
2302 fn description() -> Option<&'static str> {
2303 Some("struct pattern")
2304 }
Alex Crichton954046c2017-05-30 21:49:42 -07002305 }
2306
Michael Layzell734adb42017-06-07 16:58:31 -04002307 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002308 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002309 named!(parse -> Self, alt!(
2310 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002311 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002312 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002313 pat: syn!(Pat) >>
2314 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002315 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002316 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002317 attrs: Vec::new(),
2318 colon_token: Some(colon),
2319 })
2320 )
2321 |
2322 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002323 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002324 by_ref: option!(keyword!(ref)) >>
2325 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002326 ident: syn!(Ident) >>
2327 ({
2328 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002329 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002330 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002331 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002332 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002333 }.into();
2334 if let Some(boxed) = boxed {
2335 pat = PatBox {
2336 pat: Box::new(pat),
2337 box_token: boxed,
2338 }.into();
2339 }
2340 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002341 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002342 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002343 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002344 colon_token: None,
2345 }
2346 })
2347 )
2348 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002349
2350 fn description() -> Option<&'static str> {
2351 Some("field pattern")
2352 }
Alex Crichton954046c2017-05-30 21:49:42 -07002353 }
2354
Michael Layzell734adb42017-06-07 16:58:31 -04002355 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002356 impl Synom for Member {
2357 named!(parse -> Self, alt!(
2358 syn!(Ident) => { Member::Named }
2359 |
2360 syn!(Index) => { Member::Unnamed }
2361 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002362
2363 fn description() -> Option<&'static str> {
2364 Some("field member")
2365 }
David Tolnay85b69a42017-12-27 20:43:10 -05002366 }
2367
2368 #[cfg(feature = "full")]
2369 impl Synom for Index {
2370 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002371 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002372 ({
David Tolnay360efd22018-01-04 23:35:26 -08002373 if let IntSuffix::None = lit.suffix() {
2374 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002375 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002376 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002377 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002378 })
David Tolnay85b69a42017-12-27 20:43:10 -05002379 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002380
2381 fn description() -> Option<&'static str> {
2382 Some("field index")
2383 }
David Tolnay85b69a42017-12-27 20:43:10 -05002384 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002385
Michael Layzell734adb42017-06-07 16:58:31 -04002386 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002387 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002388 named!(parse -> Self, map!(
2389 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002390 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002391 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002392
2393 fn description() -> Option<&'static str> {
2394 Some("path pattern")
2395 }
Alex Crichton954046c2017-05-30 21:49:42 -07002396 }
David Tolnay9636c052016-10-02 17:11:17 -07002397
Michael Layzell734adb42017-06-07 16:58:31 -04002398 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002399 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002400 named!(parse -> Self, do_parse!(
2401 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002402 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002403 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2404 tuple!(punct!(..), option!(punct!(,)))
2405 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002406 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002407 Some((_, Some(_))) => true,
2408 _ => false,
2409 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002410 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002411 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002412 )) >>
2413 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002414 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002415 let (dotdot, trailing) = match dotdot {
2416 Some((a, b)) => (Some(a), Some(b)),
2417 None => (None, None),
2418 };
2419 PatTuple {
2420 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002421 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002422 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002423 comma_token: trailing.unwrap_or_default(),
2424 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002425 }
2426 })
2427 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002428
2429 fn description() -> Option<&'static str> {
2430 Some("tuple pattern")
2431 }
Alex Crichton954046c2017-05-30 21:49:42 -07002432 }
David Tolnayfbb73232016-10-03 01:00:06 -07002433
Michael Layzell734adb42017-06-07 16:58:31 -04002434 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002435 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002436 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002437 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002438 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002439 pat: syn!(Pat) >>
2440 (PatRef {
2441 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002442 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002443 and_token: and,
2444 })
2445 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002446
2447 fn description() -> Option<&'static str> {
2448 Some("reference pattern")
2449 }
Alex Crichton954046c2017-05-30 21:49:42 -07002450 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002451
Michael Layzell734adb42017-06-07 16:58:31 -04002452 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002453 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002454 named!(parse -> Self, do_parse!(
2455 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002456 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002457 return parse_error(); // these need to be parsed by pat_path
2458 } else {
2459 PatLit {
2460 expr: Box::new(lit),
2461 }
2462 })
2463 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002464
2465 fn description() -> Option<&'static str> {
2466 Some("literal pattern")
2467 }
Alex Crichton954046c2017-05-30 21:49:42 -07002468 }
David Tolnaye1310902016-10-29 23:40:00 -07002469
Michael Layzell734adb42017-06-07 16:58:31 -04002470 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002471 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002472 named!(parse -> Self, do_parse!(
2473 lo: pat_lit_expr >>
2474 limits: syn!(RangeLimits) >>
2475 hi: pat_lit_expr >>
2476 (PatRange {
2477 lo: Box::new(lo),
2478 hi: Box::new(hi),
2479 limits: limits,
2480 })
2481 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002482
2483 fn description() -> Option<&'static str> {
2484 Some("range pattern")
2485 }
Alex Crichton954046c2017-05-30 21:49:42 -07002486 }
David Tolnaye1310902016-10-29 23:40:00 -07002487
Michael Layzell734adb42017-06-07 16:58:31 -04002488 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002489 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002490 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002491 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002492 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002493 |
David Tolnay8c91b882017-12-28 23:04:32 -05002494 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002495 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002496 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002497 Expr::Unary(ExprUnary {
2498 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002499 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002500 expr: Box::new(v)
2501 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002502 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002503 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002504 })
2505 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002506
Michael Layzell734adb42017-06-07 16:58:31 -04002507 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002508 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002509 named!(parse -> Self, map!(
2510 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002511 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002512 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002513 dots: punct!(..) >>
2514 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002515 (dots, trailing)
2516 )) >>
2517 after: cond!(
2518 match middle {
2519 Some((_, ref trailing)) => trailing.is_some(),
2520 _ => false,
2521 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002522 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002523 ) >>
2524 (before, middle, after)
2525 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002526 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002527 let mut before: Punctuated<Pat, Token![,]> = before;
2528 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002529 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002530 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002531 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002532 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002533 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002534 }),
2535 bracket_token: brackets,
2536 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002537 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002538 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002539 } else {
2540 Some(Box::new(before.pop().unwrap().into_item()))
Michael Layzell92639a52017-06-01 00:07:44 -04002541 }
2542 }),
2543 front: before,
2544 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002545 }
Alex Crichton954046c2017-05-30 21:49:42 -07002546 }
Michael Layzell92639a52017-06-01 00:07:44 -04002547 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002548
2549 fn description() -> Option<&'static str> {
2550 Some("slice pattern")
2551 }
Alex Crichton954046c2017-05-30 21:49:42 -07002552 }
David Tolnay323279a2017-12-29 11:26:32 -05002553
2554 #[cfg(feature = "full")]
2555 impl Synom for PatMacro {
2556 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002557
2558 fn description() -> Option<&'static str> {
2559 Some("macro pattern")
2560 }
David Tolnay323279a2017-12-29 11:26:32 -05002561 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002562}
2563
David Tolnayf4bbbd92016-09-23 14:41:55 -07002564#[cfg(feature = "printing")]
2565mod printing {
2566 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002567 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002568 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002569 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002570 #[cfg(feature = "full")]
2571 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002572
David Tolnaybcf26022017-12-25 22:10:52 -05002573 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2574 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002575 #[cfg(feature = "full")]
2576 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002577 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002578 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002579 e.to_tokens(tokens);
2580 });
2581 } else {
2582 e.to_tokens(tokens);
2583 }
2584 }
2585
David Tolnay8c91b882017-12-28 23:04:32 -05002586 #[cfg(feature = "full")]
2587 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2588 tokens.append_all(attrs.outer());
2589 }
Michael Layzell734adb42017-06-07 16:58:31 -04002590
David Tolnay8c91b882017-12-28 23:04:32 -05002591 #[cfg(not(feature = "full"))]
2592 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 }
2594
Michael Layzell734adb42017-06-07 16:58:31 -04002595 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002596 impl ToTokens for ExprBox {
2597 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002598 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002599 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002600 self.expr.to_tokens(tokens);
2601 }
2602 }
2603
Michael Layzell734adb42017-06-07 16:58:31 -04002604 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002605 impl ToTokens for ExprInPlace {
2606 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002607 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002608 self.place.to_tokens(tokens);
2609 self.arrow_token.to_tokens(tokens);
2610 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002611 }
2612 }
2613
Michael Layzell734adb42017-06-07 16:58:31 -04002614 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002615 impl ToTokens for ExprArray {
2616 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002617 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002618 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002619 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002620 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002621 }
2622 }
2623
2624 impl ToTokens for ExprCall {
2625 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002626 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002627 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002628 self.paren_token.surround(tokens, |tokens| {
2629 self.args.to_tokens(tokens);
2630 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002631 }
2632 }
2633
Michael Layzell734adb42017-06-07 16:58:31 -04002634 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002635 impl ToTokens for ExprMethodCall {
2636 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002637 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002638 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002639 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002640 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002641 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002642 self.paren_token.surround(tokens, |tokens| {
2643 self.args.to_tokens(tokens);
2644 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002645 }
2646 }
2647
Michael Layzell734adb42017-06-07 16:58:31 -04002648 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002649 impl ToTokens for MethodTurbofish {
2650 fn to_tokens(&self, tokens: &mut Tokens) {
2651 self.colon2_token.to_tokens(tokens);
2652 self.lt_token.to_tokens(tokens);
2653 self.args.to_tokens(tokens);
2654 self.gt_token.to_tokens(tokens);
2655 }
2656 }
2657
2658 #[cfg(feature = "full")]
2659 impl ToTokens for GenericMethodArgument {
2660 fn to_tokens(&self, tokens: &mut Tokens) {
2661 match *self {
2662 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2663 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2664 }
2665 }
2666 }
2667
2668 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002669 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002670 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002671 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002672 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002673 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002674 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002675 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002676 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002677 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002678 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002679 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002680 }
2681 }
2682
2683 impl ToTokens for ExprBinary {
2684 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002685 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002686 self.left.to_tokens(tokens);
2687 self.op.to_tokens(tokens);
2688 self.right.to_tokens(tokens);
2689 }
2690 }
2691
2692 impl ToTokens for ExprUnary {
2693 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002694 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002695 self.op.to_tokens(tokens);
2696 self.expr.to_tokens(tokens);
2697 }
2698 }
2699
David Tolnay8c91b882017-12-28 23:04:32 -05002700 impl ToTokens for ExprLit {
2701 fn to_tokens(&self, tokens: &mut Tokens) {
2702 attrs_to_tokens(&self.attrs, tokens);
2703 self.lit.to_tokens(tokens);
2704 }
2705 }
2706
Alex Crichton62a0a592017-05-22 13:58:53 -07002707 impl ToTokens for ExprCast {
2708 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002709 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002710 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002711 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002712 self.ty.to_tokens(tokens);
2713 }
2714 }
2715
David Tolnay0cf94f22017-12-28 23:46:26 -05002716 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002717 impl ToTokens for ExprType {
2718 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002719 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002720 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002721 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002722 self.ty.to_tokens(tokens);
2723 }
2724 }
2725
Michael Layzell734adb42017-06-07 16:58:31 -04002726 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002727 fn maybe_wrap_else(
2728 tokens: &mut Tokens,
David Tolnay2ccf32a2017-12-29 00:34:26 -05002729 else_: &Option<(Token![else], Box<Expr>)>,
David Tolnay51382052017-12-27 13:46:21 -05002730 ) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002731 if let Some((ref else_token, ref else_)) = *else_ {
2732 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002733
2734 // If we are not one of the valid expressions to exist in an else
2735 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002736 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002737 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002738 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002739 }
2740 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002741 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002742 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002743 });
2744 }
2745 }
2746 }
2747 }
2748
2749 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002750 impl ToTokens for ExprIf {
2751 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002752 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002753 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002754 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002755 self.then_branch.to_tokens(tokens);
2756 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002757 }
2758 }
2759
Michael Layzell734adb42017-06-07 16:58:31 -04002760 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002761 impl ToTokens for ExprIfLet {
2762 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002763 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002764 self.if_token.to_tokens(tokens);
2765 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002766 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002767 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002768 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002769 self.then_branch.to_tokens(tokens);
2770 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002771 }
2772 }
2773
Michael Layzell734adb42017-06-07 16:58:31 -04002774 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002775 impl ToTokens for ExprWhile {
2776 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002777 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002778 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002779 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002780 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002781 self.body.to_tokens(tokens);
2782 }
2783 }
2784
Michael Layzell734adb42017-06-07 16:58:31 -04002785 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002786 impl ToTokens for ExprWhileLet {
2787 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002788 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002789 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002790 self.while_token.to_tokens(tokens);
2791 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002792 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002793 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002794 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002795 self.body.to_tokens(tokens);
2796 }
2797 }
2798
Michael Layzell734adb42017-06-07 16:58:31 -04002799 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002800 impl ToTokens for ExprForLoop {
2801 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002802 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002803 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002804 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002805 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002806 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002807 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002808 self.body.to_tokens(tokens);
2809 }
2810 }
2811
Michael Layzell734adb42017-06-07 16:58:31 -04002812 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002813 impl ToTokens for ExprLoop {
2814 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002815 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002816 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002817 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002818 self.body.to_tokens(tokens);
2819 }
2820 }
2821
Michael Layzell734adb42017-06-07 16:58:31 -04002822 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002823 impl ToTokens for ExprMatch {
2824 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002825 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002826 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002827 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002828 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002829 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002830 arm.to_tokens(tokens);
2831 // Ensure that we have a comma after a non-block arm, except
2832 // for the last one.
2833 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002834 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002835 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002836 }
2837 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002838 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002839 }
2840 }
2841
Michael Layzell734adb42017-06-07 16:58:31 -04002842 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002843 impl ToTokens for ExprCatch {
2844 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002845 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002846 self.do_token.to_tokens(tokens);
2847 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002848 self.block.to_tokens(tokens);
2849 }
2850 }
2851
Michael Layzell734adb42017-06-07 16:58:31 -04002852 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002853 impl ToTokens for ExprYield {
2854 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002855 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002856 self.yield_token.to_tokens(tokens);
2857 self.expr.to_tokens(tokens);
2858 }
2859 }
2860
2861 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002862 impl ToTokens for ExprClosure {
2863 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002864 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002865 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002866 self.or1_token.to_tokens(tokens);
David Tolnay6eff4da2018-01-01 20:27:45 -08002867 for input in self.inputs.elements() {
David Tolnayf2cfd722017-12-31 18:02:51 -05002868 match **input.item() {
David Tolnay51382052017-12-27 13:46:21 -05002869 FnArg::Captured(ArgCaptured {
2870 ref pat,
2871 ty: Type::Infer(_),
2872 ..
2873 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002874 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002875 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002876 _ => input.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002877 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002878 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002879 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002880 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002881 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002882 self.body.to_tokens(tokens);
2883 }
2884 }
2885
Michael Layzell734adb42017-06-07 16:58:31 -04002886 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002887 impl ToTokens for ExprUnsafe {
2888 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002889 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002890 self.unsafe_token.to_tokens(tokens);
2891 self.block.to_tokens(tokens);
2892 }
2893 }
2894
2895 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002896 impl ToTokens for ExprBlock {
2897 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002898 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002899 self.block.to_tokens(tokens);
2900 }
2901 }
2902
Michael Layzell734adb42017-06-07 16:58:31 -04002903 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002904 impl ToTokens for ExprAssign {
2905 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002906 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002907 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002908 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002909 self.right.to_tokens(tokens);
2910 }
2911 }
2912
Michael Layzell734adb42017-06-07 16:58:31 -04002913 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002914 impl ToTokens for ExprAssignOp {
2915 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002916 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002917 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002918 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002919 self.right.to_tokens(tokens);
2920 }
2921 }
2922
Michael Layzell734adb42017-06-07 16:58:31 -04002923 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002924 impl ToTokens for ExprField {
2925 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002926 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002927 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002928 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002929 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002930 }
2931 }
2932
Michael Layzell734adb42017-06-07 16:58:31 -04002933 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002934 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002935 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002936 match *self {
2937 Member::Named(ident) => ident.to_tokens(tokens),
2938 Member::Unnamed(ref index) => index.to_tokens(tokens),
2939 }
2940 }
2941 }
2942
2943 #[cfg(feature = "full")]
2944 impl ToTokens for Index {
2945 fn to_tokens(&self, tokens: &mut Tokens) {
2946 tokens.append(TokenTree {
2947 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002948 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002949 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002950 }
2951 }
2952
2953 impl ToTokens for ExprIndex {
2954 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002955 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002956 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002957 self.bracket_token.surround(tokens, |tokens| {
2958 self.index.to_tokens(tokens);
2959 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002960 }
2961 }
2962
Michael Layzell734adb42017-06-07 16:58:31 -04002963 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002964 impl ToTokens for ExprRange {
2965 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002966 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002967 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002968 match self.limits {
2969 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2970 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2971 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002972 self.to.to_tokens(tokens);
2973 }
2974 }
2975
2976 impl ToTokens for ExprPath {
2977 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002978 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002979 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002980 }
2981 }
2982
Michael Layzell734adb42017-06-07 16:58:31 -04002983 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002984 impl ToTokens for ExprAddrOf {
2985 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002986 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002987 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002988 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002989 self.expr.to_tokens(tokens);
2990 }
2991 }
2992
Michael Layzell734adb42017-06-07 16:58:31 -04002993 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002994 impl ToTokens for ExprBreak {
2995 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002996 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002997 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002998 self.label.to_tokens(tokens);
2999 self.expr.to_tokens(tokens);
3000 }
3001 }
3002
Michael Layzell734adb42017-06-07 16:58:31 -04003003 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003004 impl ToTokens for ExprContinue {
3005 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003006 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003007 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003008 self.label.to_tokens(tokens);
3009 }
3010 }
3011
Michael Layzell734adb42017-06-07 16:58:31 -04003012 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003013 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003014 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003015 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003016 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003017 self.expr.to_tokens(tokens);
3018 }
3019 }
3020
Michael Layzell734adb42017-06-07 16:58:31 -04003021 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003022 impl ToTokens for ExprMacro {
3023 fn to_tokens(&self, tokens: &mut Tokens) {
3024 tokens.append_all(self.attrs.outer());
3025 self.mac.to_tokens(tokens);
3026 }
3027 }
3028
3029 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003030 impl ToTokens for ExprStruct {
3031 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003032 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003033 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 self.brace_token.surround(tokens, |tokens| {
3035 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003036 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003037 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003038 self.rest.to_tokens(tokens);
3039 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003040 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 }
3042 }
3043
Michael Layzell734adb42017-06-07 16:58:31 -04003044 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003045 impl ToTokens for ExprRepeat {
3046 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003047 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003048 self.bracket_token.surround(tokens, |tokens| {
3049 self.expr.to_tokens(tokens);
3050 self.semi_token.to_tokens(tokens);
3051 self.amt.to_tokens(tokens);
3052 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003053 }
3054 }
3055
David Tolnaye98775f2017-12-28 23:17:00 -05003056 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003057 impl ToTokens for ExprGroup {
3058 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003059 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003060 self.group_token.surround(tokens, |tokens| {
3061 self.expr.to_tokens(tokens);
3062 });
3063 }
3064 }
3065
David Tolnaye98775f2017-12-28 23:17:00 -05003066 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003067 impl ToTokens for ExprParen {
3068 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003069 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003070 self.paren_token.surround(tokens, |tokens| {
3071 self.expr.to_tokens(tokens);
3072 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003073 }
3074 }
3075
Michael Layzell734adb42017-06-07 16:58:31 -04003076 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003077 impl ToTokens for ExprTry {
3078 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003079 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003080 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003081 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003082 }
3083 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003084
David Tolnay2ae520a2017-12-29 11:19:50 -05003085 impl ToTokens for ExprVerbatim {
3086 fn to_tokens(&self, tokens: &mut Tokens) {
3087 self.tts.to_tokens(tokens);
3088 }
3089 }
3090
Michael Layzell734adb42017-06-07 16:58:31 -04003091 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003092 impl ToTokens for Label {
3093 fn to_tokens(&self, tokens: &mut Tokens) {
3094 self.name.to_tokens(tokens);
3095 self.colon_token.to_tokens(tokens);
3096 }
3097 }
3098
3099 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003100 impl ToTokens for FieldValue {
3101 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003102 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003103 if let Some(ref colon_token) = self.colon_token {
3104 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003105 self.expr.to_tokens(tokens);
3106 }
David Tolnay055a7042016-10-02 19:23:54 -07003107 }
3108 }
3109
Michael Layzell734adb42017-06-07 16:58:31 -04003110 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003111 impl ToTokens for Arm {
3112 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003113 tokens.append_all(&self.attrs);
3114 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003115 if let Some((ref if_token, ref guard)) = self.guard {
3116 if_token.to_tokens(tokens);
3117 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003118 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003119 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003120 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003121 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003122 }
3123 }
3124
Michael Layzell734adb42017-06-07 16:58:31 -04003125 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003126 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003127 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003128 self.underscore_token.to_tokens(tokens);
3129 }
3130 }
3131
Michael Layzell734adb42017-06-07 16:58:31 -04003132 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003133 impl ToTokens for PatIdent {
3134 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003135 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003136 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003137 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003138 if let Some((ref at_token, ref subpat)) = self.subpat {
3139 at_token.to_tokens(tokens);
3140 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003141 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003142 }
3143 }
3144
Michael Layzell734adb42017-06-07 16:58:31 -04003145 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003146 impl ToTokens for PatStruct {
3147 fn to_tokens(&self, tokens: &mut Tokens) {
3148 self.path.to_tokens(tokens);
3149 self.brace_token.surround(tokens, |tokens| {
3150 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003151 // NOTE: We need a comma before the dot2 token if it is present.
3152 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003153 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003154 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003155 self.dot2_token.to_tokens(tokens);
3156 });
3157 }
3158 }
3159
Michael Layzell734adb42017-06-07 16:58:31 -04003160 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 impl ToTokens for PatTupleStruct {
3162 fn to_tokens(&self, tokens: &mut Tokens) {
3163 self.path.to_tokens(tokens);
3164 self.pat.to_tokens(tokens);
3165 }
3166 }
3167
Michael Layzell734adb42017-06-07 16:58:31 -04003168 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003169 impl ToTokens for PatPath {
3170 fn to_tokens(&self, tokens: &mut Tokens) {
3171 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3172 }
3173 }
3174
Michael Layzell734adb42017-06-07 16:58:31 -04003175 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003176 impl ToTokens for PatTuple {
3177 fn to_tokens(&self, tokens: &mut Tokens) {
3178 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003179 self.front.to_tokens(tokens);
3180 if let Some(ref dot2_token) = self.dot2_token {
3181 if !self.front.empty_or_trailing() {
3182 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003183 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003184 }
David Tolnay41871922017-12-29 01:53:45 -05003185 dot2_token.to_tokens(tokens);
3186 self.comma_token.to_tokens(tokens);
3187 if self.comma_token.is_none() && !self.back.is_empty() {
3188 // Ensure there is a comma after the .. token.
3189 <Token![,]>::default().to_tokens(tokens);
3190 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003191 }
David Tolnay41871922017-12-29 01:53:45 -05003192 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003193 });
3194 }
3195 }
3196
Michael Layzell734adb42017-06-07 16:58:31 -04003197 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003198 impl ToTokens for PatBox {
3199 fn to_tokens(&self, tokens: &mut Tokens) {
3200 self.box_token.to_tokens(tokens);
3201 self.pat.to_tokens(tokens);
3202 }
3203 }
3204
Michael Layzell734adb42017-06-07 16:58:31 -04003205 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003206 impl ToTokens for PatRef {
3207 fn to_tokens(&self, tokens: &mut Tokens) {
3208 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003209 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003210 self.pat.to_tokens(tokens);
3211 }
3212 }
3213
Michael Layzell734adb42017-06-07 16:58:31 -04003214 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003215 impl ToTokens for PatLit {
3216 fn to_tokens(&self, tokens: &mut Tokens) {
3217 self.expr.to_tokens(tokens);
3218 }
3219 }
3220
Michael Layzell734adb42017-06-07 16:58:31 -04003221 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003222 impl ToTokens for PatRange {
3223 fn to_tokens(&self, tokens: &mut Tokens) {
3224 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003225 match self.limits {
3226 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3227 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3228 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003229 self.hi.to_tokens(tokens);
3230 }
3231 }
3232
Michael Layzell734adb42017-06-07 16:58:31 -04003233 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003234 impl ToTokens for PatSlice {
3235 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003236 // XXX: This is a mess, and it will be so easy to screw it up. How
3237 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003238 self.bracket_token.surround(tokens, |tokens| {
3239 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003240
3241 // If we need a comma before the middle or standalone .. token,
3242 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003243 if !self.front.empty_or_trailing()
3244 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003245 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003246 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003247 }
3248
3249 // If we have an identifier, we always need a .. token.
3250 if self.middle.is_some() {
3251 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003252 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003253 } else if self.dot2_token.is_some() {
3254 self.dot2_token.to_tokens(tokens);
3255 }
3256
3257 // Make sure we have a comma before the back half.
3258 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003259 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003260 self.back.to_tokens(tokens);
3261 } else {
3262 self.comma_token.to_tokens(tokens);
3263 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003264 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003265 }
3266 }
3267
Michael Layzell734adb42017-06-07 16:58:31 -04003268 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003269 impl ToTokens for PatMacro {
3270 fn to_tokens(&self, tokens: &mut Tokens) {
3271 self.mac.to_tokens(tokens);
3272 }
3273 }
3274
3275 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003276 impl ToTokens for PatVerbatim {
3277 fn to_tokens(&self, tokens: &mut Tokens) {
3278 self.tts.to_tokens(tokens);
3279 }
3280 }
3281
3282 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003283 impl ToTokens for FieldPat {
3284 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003285 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003286 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003287 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003288 }
3289 self.pat.to_tokens(tokens);
3290 }
3291 }
3292
Michael Layzell734adb42017-06-07 16:58:31 -04003293 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003294 impl ToTokens for Block {
3295 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003296 self.brace_token.surround(tokens, |tokens| {
3297 tokens.append_all(&self.stmts);
3298 });
David Tolnay42602292016-10-01 22:25:45 -07003299 }
3300 }
3301
Michael Layzell734adb42017-06-07 16:58:31 -04003302 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003303 impl ToTokens for Stmt {
3304 fn to_tokens(&self, tokens: &mut Tokens) {
3305 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003306 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003307 Stmt::Item(ref item) => item.to_tokens(tokens),
3308 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003309 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003310 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003311 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003312 }
David Tolnay42602292016-10-01 22:25:45 -07003313 }
3314 }
3315 }
David Tolnay191e0582016-10-02 18:31:09 -07003316
Michael Layzell734adb42017-06-07 16:58:31 -04003317 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003318 impl ToTokens for Local {
3319 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003320 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003321 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003322 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003323 if let Some((ref colon_token, ref ty)) = self.ty {
3324 colon_token.to_tokens(tokens);
3325 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003326 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003327 if let Some((ref eq_token, ref init)) = self.init {
3328 eq_token.to_tokens(tokens);
3329 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003330 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003331 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003332 }
3333 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003334}