blob: 4cef4424d02c0a69b248da9530fa397a80a8da90 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnay2ae520a2017-12-29 11:19:50 -05003use proc_macro2::{Span, TokenStream};
David Tolnay14982012017-12-29 00:49:51 -05004#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -05005use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -05006#[cfg(feature = "extra-traits")]
7use mac::TokenStreamHelper;
8#[cfg(feature = "full")]
9use std::mem;
David Tolnayf4bbbd92016-09-23 14:41:55 -070010
Alex Crichton62a0a592017-05-22 13:58:53 -070011ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -050012 /// An expression.
13 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070014 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040015 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050016 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080017 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050018 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070019 }),
Clar Charrd22b5702017-03-10 15:24:56 -050020
David Tolnay8701a5c2017-12-28 23:31:10 -050021 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040022 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050023 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050025 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070026 pub value: Box<Expr>,
27 }),
Clar Charrd22b5702017-03-10 15:24:56 -050028
Alex Crichton62a0a592017-05-22 13:58:53 -070029 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040030 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050031 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050032 pub bracket_token: token::Bracket,
David Tolnay2a86fdd2017-12-28 23:34:28 -050033 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070034 }),
Clar Charrd22b5702017-03-10 15:24:56 -050035
Alex Crichton62a0a592017-05-22 13:58:53 -070036 /// A function call.
37 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050038 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070039 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050040 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -050041 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
Clar Charrd22b5702017-03-10 15:24:56 -050043
Alex Crichton62a0a592017-05-22 13:58:53 -070044 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
45 ///
46 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080047 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070048 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040049 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050050 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -050051 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080052 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050053 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -050054 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050055 pub paren_token: token::Paren,
56 pub args: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070057 }),
Clar Charrd22b5702017-03-10 15:24:56 -050058
Alex Crichton62a0a592017-05-22 13:58:53 -070059 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050060 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050061 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050062 pub paren_token: token::Paren,
David Tolnay2a86fdd2017-12-28 23:34:28 -050063 pub elems: Delimited<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070064 }),
Clar Charrd22b5702017-03-10 15:24:56 -050065
Alex Crichton62a0a592017-05-22 13:58:53 -070066 /// A binary operation, e.g. `a + b`, `a * b`.
67 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050068 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050070 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub right: Box<Expr>,
72 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A unary operation, e.g. `!x`, `*x`.
75 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub op: UnOp,
78 pub expr: Box<Expr>,
79 }),
Clar Charrd22b5702017-03-10 15:24:56 -050080
Alex Crichton62a0a592017-05-22 13:58:53 -070081 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050082 pub Lit(ExprLit {
83 pub attrs: Vec<Attribute>,
84 pub lit: Lit,
85 }),
Clar Charrd22b5702017-03-10 15:24:56 -050086
Alex Crichton62a0a592017-05-22 13:58:53 -070087 /// A cast, e.g. `foo as f64`.
88 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050089 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070090 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070093 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A type ascription, e.g. `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -050096 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -050097 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070098 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080099 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800100 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500102
Alex Crichton62a0a592017-05-22 13:58:53 -0700103 /// An `if` block, with an optional else block
104 ///
105 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400106 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500107 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500108 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500110 pub then_branch: Block,
111 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700112 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500113
Alex Crichton62a0a592017-05-22 13:58:53 -0700114 /// An `if let` expression with an optional else block
115 ///
116 /// E.g., `if let pat = expr { block } else { expr }`
117 ///
118 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400119 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500120 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800121 pub if_token: Token![if],
122 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500123 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800124 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500125 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500126 pub then_branch: Block,
127 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700128 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500129
Alex Crichton62a0a592017-05-22 13:58:53 -0700130 /// A while loop, with an optional label
131 ///
132 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400133 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500134 pub attrs: Vec<Attribute>,
David 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 Tolnay7f675742017-12-27 22:43:21 -0500196 pub inputs: Delimited<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800197 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500198 pub output: ReturnType,
199 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700200 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500201
Nika Layzell640832a2017-12-04 13:37:09 -0500202 /// An unsafe block (`unsafe { ... }`)
203 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500204 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500205 pub unsafe_token: Token![unsafe],
206 pub block: Block,
207 }),
208
209 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400210 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500211 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700212 pub block: Block,
213 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700214
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400216 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500217 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800219 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500220 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700221 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223 /// An assignment with an operator
224 ///
225 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400226 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500227 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700228 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500229 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700230 pub right: Box<Expr>,
231 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500232
David Tolnay85b69a42017-12-27 20:43:10 -0500233 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
234 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400235 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500236 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500237 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800238 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500239 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500241
Alex Crichton62a0a592017-05-22 13:58:53 -0700242 /// An indexing operation (`foo[2]`)
243 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500244 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500246 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500249
David Tolnaybe55d7b2017-12-17 23:41:20 -0800250 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400251 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500252 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500255 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700257
Alex Crichton62a0a592017-05-22 13:58:53 -0700258 /// Variable reference, possibly containing `::` and/or type
259 /// parameters, e.g. foo::bar::<baz>.
260 ///
261 /// Optionally "qualified",
262 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
263 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500264 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 pub qself: Option<QSelf>,
266 pub path: Path,
267 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700268
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400270 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500271 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800272 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500273 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 pub expr: Box<Expr>,
275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400278 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500279 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500280 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700281 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 pub expr: Option<Box<Expr>>,
283 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500284
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400286 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500287 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800288 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500289 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500291
Alex Crichton62a0a592017-05-22 13:58:53 -0700292 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500293 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500294 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800295 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500296 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700297 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700298
Alex Crichton62a0a592017-05-22 13:58:53 -0700299 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500300 pub Macro(ExprMacro #full {
301 pub attrs: Vec<Attribute>,
302 pub mac: Macro,
303 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700304
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 /// A struct literal expression.
306 ///
307 /// For example, `Foo {x: 1, y: 2}`, or
308 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400309 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500310 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500312 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500313 pub fields: Delimited<FieldValue, Token![,]>,
314 pub dot2_token: Option<Token![..]>,
315 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700316 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700317
Alex Crichton62a0a592017-05-22 13:58:53 -0700318 /// An array literal constructed from one repeated element.
319 ///
320 /// For example, `[1; 5]`. The first expression is the element
321 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400322 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500323 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500324 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700325 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500326 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700327 pub amt: Box<Expr>,
328 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700329
Alex Crichton62a0a592017-05-22 13:58:53 -0700330 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500331 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500332 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500333 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500334 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700336
Michael Layzell93c36282017-06-04 20:43:14 -0400337 /// No-op: used solely so we can pretty-print faithfully
338 ///
339 /// A `group` represents a `None`-delimited span in the input
340 /// `TokenStream` which affects the precidence of the resulting
341 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500342 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500343 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500344 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500345 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400346 }),
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400349 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500350 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700351 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800352 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700353 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700354
Alex Crichton62a0a592017-05-22 13:58:53 -0700355 /// A catch expression.
356 ///
357 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400358 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500359 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub do_token: Token![do],
361 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700362 pub block: Block,
363 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700364
365 /// A yield expression.
366 ///
367 /// E.g. `yield expr`
368 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500369 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800370 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700371 pub expr: Option<Box<Expr>>,
372 }),
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![<],
506 pub args: Delimited<GenericMethodArgument, Token![,]>,
507 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 Tolnay4a3f59a2017-12-28 21:21:12 -0500589 pub colon_token: Option<Token![:]>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800590 pub ty: Option<Box<Type>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500591 pub eq_token: Option<Token![=]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700592 /// Initializer expression to set the value, if any
593 pub init: Option<Box<Expr>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500594 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700595 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700596}
597
Michael Layzell734adb42017-06-07 16:58:31 -0400598#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700599ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700600 // Clippy false positive
601 // https://github.com/Manishearth/rust-clippy/issues/1241
602 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
603 pub enum Pat {
604 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700605 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700607 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700608
Alex Crichton62a0a592017-05-22 13:58:53 -0700609 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
610 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
611 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
612 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700613 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500614 pub by_ref: Option<Token![ref]>,
615 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700616 pub ident: Ident,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800617 pub at_token: Option<Token![@]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500618 pub subpat: Option<Box<Pat>>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700619 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700620
Alex Crichton62a0a592017-05-22 13:58:53 -0700621 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
622 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700623 pub Struct(PatStruct {
624 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500625 pub brace_token: token::Brace,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500626 pub fields: Delimited<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800627 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700628 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700629
Alex Crichton62a0a592017-05-22 13:58:53 -0700630 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
631 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
632 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700633 pub TupleStruct(PatTupleStruct {
634 pub path: Path,
635 pub pat: PatTuple,
636 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700637
Alex Crichton62a0a592017-05-22 13:58:53 -0700638 /// A possibly qualified path pattern.
639 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
640 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
641 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700642 pub Path(PatPath {
643 pub qself: Option<QSelf>,
644 pub path: Path,
645 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700646
Alex Crichton62a0a592017-05-22 13:58:53 -0700647 /// A tuple pattern `(a, b)`.
648 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
649 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700650 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500651 pub paren_token: token::Paren,
David Tolnay41871922017-12-29 01:53:45 -0500652 pub front: Delimited<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500653 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500654 pub comma_token: Option<Token![,]>,
655 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700656 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700657 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700658 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800659 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500660 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700661 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700662 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700663 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800664 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500665 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500666 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700667 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700668 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700669 pub Lit(PatLit {
670 pub expr: Box<Expr>,
671 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800672 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700673 pub Range(PatRange {
674 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700675 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500676 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700677 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400678 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700679 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500680 pub bracket_token: token::Bracket,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800681 pub front: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700682 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500683 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500684 pub comma_token: Option<Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500685 pub back: Delimited<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700686 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 /// A macro pattern; pre-expansion
David Tolnay323279a2017-12-29 11:26:32 -0500688 pub Macro(PatMacro {
689 pub mac: Macro,
690 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500691 pub Verbatim(PatVerbatim #manual_extra_traits {
692 pub tts: TokenStream,
693 }),
694 }
695}
696
697#[cfg(feature = "extra-traits")]
698impl Eq for PatVerbatim {}
699
700#[cfg(feature = "extra-traits")]
701impl PartialEq for PatVerbatim {
702 fn eq(&self, other: &Self) -> bool {
703 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
704 }
705}
706
707#[cfg(feature = "extra-traits")]
708impl Hash for PatVerbatim {
709 fn hash<H>(&self, state: &mut H)
710 where
711 H: Hasher,
712 {
713 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700714 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700715}
716
Michael Layzell734adb42017-06-07 16:58:31 -0400717#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700718ast_struct! {
719 /// An arm of a 'match'.
720 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800721 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700722 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500723 /// ```rust
724 /// # #![feature(dotdoteq_in_patterns)]
725 /// #
726 /// # fn main() {
727 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700728 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500729 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700730 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500731 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500733 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700734 /// ```
735 pub struct Arm {
736 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800737 pub pats: Delimited<Pat, Token![|]>,
738 pub if_token: Option<Token![if]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700739 pub guard: Option<Box<Expr>>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800740 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700741 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700743 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700744}
745
Michael Layzell734adb42017-06-07 16:58:31 -0400746#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700747ast_enum! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700748 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700749 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 pub enum RangeLimits {
751 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800752 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700753 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800754 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700755 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700756}
757
Michael Layzell734adb42017-06-07 16:58:31 -0400758#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700759ast_struct! {
760 /// A single field in a struct pattern
761 ///
762 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay5d7098a2017-12-29 01:35:24 -0500763 /// are treated the same as `x: x, y: ref y, z: ref mut z` but
764 /// there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700765 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500766 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700767 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500768 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500769 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700770 /// The pattern the field is destructured to
771 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700772 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700773}
774
Michael Layzell3936ceb2017-07-08 00:28:36 -0400775#[cfg(any(feature = "parsing", feature = "printing"))]
776#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700777fn arm_expr_requires_comma(expr: &Expr) -> bool {
778 // see https://github.com/rust-lang/rust/blob/eb8f2586e
779 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500780 match *expr {
781 Expr::Unsafe(..)
782 | Expr::Block(..)
783 | Expr::If(..)
784 | Expr::IfLet(..)
785 | Expr::Match(..)
786 | Expr::While(..)
787 | Expr::WhileLet(..)
788 | Expr::Loop(..)
789 | Expr::ForLoop(..)
790 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700791 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400792 }
793}
794
David Tolnayb9c8e322016-09-23 20:48:37 -0700795#[cfg(feature = "parsing")]
796pub mod parsing {
797 use super::*;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500798 use ty::parsing::qpath;
799 #[cfg(feature = "full")]
800 use ty::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700801
Michael Layzell734adb42017-06-07 16:58:31 -0400802 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -0500803 use proc_macro2::{Delimiter, Span, TokenNode, TokenStream};
David Tolnayc5ab8c62017-12-26 16:43:39 -0500804 use synom::Synom;
805 use cursor::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400806 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500807 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500808 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700809
David Tolnaybcf26022017-12-25 22:10:52 -0500810 // When we're parsing expressions which occur before blocks, like in an if
811 // statement's condition, we cannot parse a struct literal.
812 //
813 // Struct literals are ambiguous in certain positions
814 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700815 macro_rules! ambiguous_expr {
816 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700817 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700818 };
819 }
820
David Tolnaybcf26022017-12-25 22:10:52 -0500821 // When we are parsing an optional suffix expression, we cannot allow blocks
822 // if structs are not allowed.
823 //
824 // Example:
825 //
826 // if break {} {}
827 //
828 // is ambiguous between:
829 //
830 // if (break {}) {}
831 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400832 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400833 macro_rules! opt_ambiguous_expr {
834 ($i:expr, $allow_struct:ident) => {
835 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
836 };
837 }
838
Alex Crichton954046c2017-05-30 21:49:42 -0700839 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400840 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700841
842 fn description() -> Option<&'static str> {
843 Some("expression")
844 }
845 }
846
Michael Layzell734adb42017-06-07 16:58:31 -0400847 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700848 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
849
David Tolnaybcf26022017-12-25 22:10:52 -0500850 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400851 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500852 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500853 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400854 }
855
Michael Layzell734adb42017-06-07 16:58:31 -0400856 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500857 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500858 // NOTE: We intentionally skip assign_expr, placement_expr, and
859 // range_expr, as they are not parsed in non-full mode.
860 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400861 }
862
David Tolnaybcf26022017-12-25 22:10:52 -0500863 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400864 macro_rules! binop {
865 (
866 $name: ident,
867 $next: ident,
868 $submac: ident!( $($args:tt)* )
869 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500870 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400871 mut e: call!($next, allow_struct, allow_block) >>
872 many0!(do_parse!(
873 op: $submac!($($args)*) >>
874 rhs: call!($next, allow_struct, true) >>
875 ({
876 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500877 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400878 left: Box::new(e.into()),
879 op: op,
880 right: Box::new(rhs.into()),
881 }.into();
882 })
883 )) >>
884 (e)
885 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700886 }
David Tolnay54e854d2016-10-24 12:03:30 -0700887 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700888
David Tolnaybcf26022017-12-25 22:10:52 -0500889 // <placement> = <placement> ..
890 // <placement> += <placement> ..
891 // <placement> -= <placement> ..
892 // <placement> *= <placement> ..
893 // <placement> /= <placement> ..
894 // <placement> %= <placement> ..
895 // <placement> ^= <placement> ..
896 // <placement> &= <placement> ..
897 // <placement> |= <placement> ..
898 // <placement> <<= <placement> ..
899 // <placement> >>= <placement> ..
900 //
901 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400902 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500903 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400904 mut e: call!(placement_expr, allow_struct, allow_block) >>
905 alt!(
906 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800907 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400908 // Recurse into self to parse right-associative operator.
909 rhs: call!(assign_expr, allow_struct, true) >>
910 ({
911 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500912 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400913 left: Box::new(e.into()),
914 eq_token: eq,
915 right: Box::new(rhs.into()),
916 }.into();
917 })
918 )
919 |
920 do_parse!(
921 op: call!(BinOp::parse_assign_op) >>
922 // Recurse into self to parse right-associative operator.
923 rhs: call!(assign_expr, allow_struct, true) >>
924 ({
925 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500926 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400927 left: Box::new(e.into()),
928 op: op,
929 right: Box::new(rhs.into()),
930 }.into();
931 })
932 )
933 |
934 epsilon!()
935 ) >>
936 (e)
937 ));
938
David Tolnaybcf26022017-12-25 22:10:52 -0500939 // <range> <- <range> ..
940 //
941 // NOTE: The `in place { expr }` version of this syntax is parsed in
942 // `atom_expr`, not here.
943 //
944 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400945 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500946 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400947 mut e: call!(range_expr, allow_struct, allow_block) >>
948 alt!(
949 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800950 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400951 // Recurse into self to parse right-associative operator.
952 rhs: call!(placement_expr, allow_struct, true) >>
953 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400954 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500955 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400956 // op: BinOp::Place(larrow),
957 place: Box::new(e.into()),
David Tolnay8701a5c2017-12-28 23:31:10 -0500958 arrow_token: arrow,
Michael Layzellb78f3b52017-06-04 19:03:03 -0400959 value: Box::new(rhs.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400960 }.into();
961 })
962 )
963 |
964 epsilon!()
965 ) >>
966 (e)
967 ));
968
David Tolnaybcf26022017-12-25 22:10:52 -0500969 // <or> ... <or> ..
970 // <or> .. <or> ..
971 // <or> ..
972 //
973 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
974 // rules are for parsing these expressions are, but this is not correct.
975 // For example, `a .. b .. c` is not a legal expression. It should not
976 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
977 //
978 // NOTE: The form of ranges which don't include a preceding expression are
979 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400980 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500981 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400982 mut e: call!(or_expr, allow_struct, allow_block) >>
983 many0!(do_parse!(
984 limits: syn!(RangeLimits) >>
985 // We don't want to allow blocks here if we don't allow structs. See
986 // the reasoning for `opt_ambiguous_expr!` above.
987 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
988 ({
989 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500990 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400991 from: Some(Box::new(e.into())),
992 limits: limits,
993 to: hi.map(|e| Box::new(e.into())),
994 }.into();
995 })
996 )) >>
997 (e)
998 ));
999
David Tolnaybcf26022017-12-25 22:10:52 -05001000 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001001 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001002
David Tolnaybcf26022017-12-25 22:10:52 -05001003 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001004 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001005
David Tolnaybcf26022017-12-25 22:10:52 -05001006 // <bitor> == <bitor> ...
1007 // <bitor> != <bitor> ...
1008 // <bitor> >= <bitor> ...
1009 // <bitor> <= <bitor> ...
1010 // <bitor> > <bitor> ...
1011 // <bitor> < <bitor> ...
1012 //
1013 // NOTE: This operator appears to be parsed as left-associative, but errors
1014 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001015 binop!(
1016 compare_expr,
1017 bitor_expr,
1018 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001019 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001020 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001021 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001022 |
1023 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001024 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001025 |
1026 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001027 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001028 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001029 do_parse!(
1030 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001031 not!(punct!(<-)) >>
1032 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001033 (BinOp::Lt(t))
1034 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001035 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001036 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001037 )
1038 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001039
David Tolnaybcf26022017-12-25 22:10:52 -05001040 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001041 binop!(
1042 bitor_expr,
1043 bitxor_expr,
1044 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1045 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001046
David Tolnaybcf26022017-12-25 22:10:52 -05001047 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001048 binop!(
1049 bitxor_expr,
1050 bitand_expr,
1051 do_parse!(
1052 // NOTE: Make sure we aren't looking at ^=.
1053 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1054 )
1055 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001056
David Tolnaybcf26022017-12-25 22:10:52 -05001057 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001058 binop!(
1059 bitand_expr,
1060 shift_expr,
1061 do_parse!(
1062 // NOTE: Make sure we aren't looking at && or &=.
1063 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1064 )
1065 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001066
David Tolnaybcf26022017-12-25 22:10:52 -05001067 // <arith> << <arith> ...
1068 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001069 binop!(
1070 shift_expr,
1071 arith_expr,
1072 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001073 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001074 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001075 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001076 )
1077 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001078
David Tolnaybcf26022017-12-25 22:10:52 -05001079 // <term> + <term> ...
1080 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001081 binop!(
1082 arith_expr,
1083 term_expr,
1084 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001085 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001086 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001087 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001088 )
1089 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001090
David Tolnaybcf26022017-12-25 22:10:52 -05001091 // <cast> * <cast> ...
1092 // <cast> / <cast> ...
1093 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001094 binop!(
1095 term_expr,
1096 cast_expr,
1097 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001098 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001099 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001100 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001101 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001102 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001103 )
1104 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001105
David Tolnaybcf26022017-12-25 22:10:52 -05001106 // <unary> as <ty>
1107 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001108 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001109 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001110 mut e: call!(unary_expr, allow_struct, allow_block) >>
1111 many0!(alt!(
1112 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001113 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001114 // We can't accept `A + B` in cast expressions, as it's
1115 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001116 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001117 ({
1118 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001119 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001120 expr: Box::new(e.into()),
1121 as_token: as_,
1122 ty: Box::new(ty),
1123 }.into();
1124 })
1125 )
1126 |
1127 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001128 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001129 // We can't accept `A + B` in cast expressions, as it's
1130 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001131 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001132 ({
1133 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001134 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001135 expr: Box::new(e.into()),
1136 colon_token: colon,
1137 ty: Box::new(ty),
1138 }.into();
1139 })
1140 )
1141 )) >>
1142 (e)
1143 ));
1144
David Tolnay0cf94f22017-12-28 23:46:26 -05001145 // <unary> as <ty>
1146 #[cfg(not(feature = "full"))]
1147 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1148 mut e: call!(unary_expr, allow_struct, allow_block) >>
1149 many0!(do_parse!(
1150 as_: keyword!(as) >>
1151 // We can't accept `A + B` in cast expressions, as it's
1152 // ambiguous with the + expression.
1153 ty: call!(Type::without_plus) >>
1154 ({
1155 e = ExprCast {
1156 attrs: Vec::new(),
1157 expr: Box::new(e.into()),
1158 as_token: as_,
1159 ty: Box::new(ty),
1160 }.into();
1161 })
1162 )) >>
1163 (e)
1164 ));
1165
David Tolnaybcf26022017-12-25 22:10:52 -05001166 // <UnOp> <trailer>
1167 // & <trailer>
1168 // &mut <trailer>
1169 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001170 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001171 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001172 do_parse!(
1173 op: syn!(UnOp) >>
1174 expr: call!(unary_expr, allow_struct, true) >>
1175 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001176 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001177 op: op,
1178 expr: Box::new(expr.into()),
1179 }.into())
1180 )
1181 |
1182 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001183 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001184 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001185 expr: call!(unary_expr, allow_struct, true) >>
1186 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001187 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001188 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001189 mutability: mutability,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001190 expr: Box::new(expr.into()),
1191 }.into())
1192 )
1193 |
1194 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001195 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001196 expr: call!(unary_expr, allow_struct, true) >>
1197 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001198 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001199 box_token: box_,
1200 expr: Box::new(expr.into()),
1201 }.into())
1202 )
1203 |
1204 call!(trailer_expr, allow_struct, allow_block)
1205 ));
1206
Michael Layzell734adb42017-06-07 16:58:31 -04001207 // XXX: This duplication is ugly
1208 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001209 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001210 do_parse!(
1211 op: syn!(UnOp) >>
1212 expr: call!(unary_expr, allow_struct, true) >>
1213 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001214 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001215 op: op,
1216 expr: Box::new(expr.into()),
1217 }.into())
1218 )
1219 |
1220 call!(trailer_expr, allow_struct, allow_block)
1221 ));
1222
David Tolnaybcf26022017-12-25 22:10:52 -05001223 // <atom> (..<args>) ...
1224 // <atom> . <ident> (..<args>) ...
1225 // <atom> . <ident> ...
1226 // <atom> . <lit> ...
1227 // <atom> [ <expr> ] ...
1228 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001229 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001230 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001231 mut e: call!(atom_expr, allow_struct, allow_block) >>
1232 many0!(alt!(
1233 tap!(args: and_call => {
1234 let (args, paren) = args;
1235 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001236 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001237 func: Box::new(e.into()),
1238 args: args,
1239 paren_token: paren,
1240 }.into();
1241 })
1242 |
1243 tap!(more: and_method_call => {
1244 let mut call = more;
David Tolnay76418512017-12-28 23:47:47 -05001245 call.receiver = Box::new(e.into());
Michael Layzellb78f3b52017-06-04 19:03:03 -04001246 e = call.into();
1247 })
1248 |
1249 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001250 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001251 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001252 attrs: Vec::new(),
David Tolnay85b69a42017-12-27 20:43:10 -05001253 base: Box::new(e.into()),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001254 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001255 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 }.into();
1257 })
1258 |
1259 tap!(i: and_index => {
1260 let (i, token) = i;
1261 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001262 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001263 expr: Box::new(e.into()),
1264 bracket_token: token,
1265 index: Box::new(i),
1266 }.into();
1267 })
1268 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001269 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001270 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001271 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001272 expr: Box::new(e.into()),
1273 question_token: question,
1274 }.into();
1275 })
1276 )) >>
1277 (e)
1278 ));
1279
Michael Layzell734adb42017-06-07 16:58:31 -04001280 // XXX: Duplication == ugly
1281 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001282 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001283 mut e: call!(atom_expr, allow_struct, allow_block) >>
1284 many0!(alt!(
1285 tap!(args: and_call => {
1286 let (args, paren) = args;
1287 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001288 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001289 func: Box::new(e.into()),
1290 args: args,
1291 paren_token: paren,
1292 }.into();
1293 })
1294 |
1295 tap!(i: and_index => {
1296 let (i, token) = i;
1297 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001298 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001299 expr: Box::new(e.into()),
1300 bracket_token: token,
1301 index: Box::new(i),
1302 }.into();
1303 })
1304 )) >>
1305 (e)
1306 ));
1307
David Tolnaybcf26022017-12-25 22:10:52 -05001308 // Parse all atomic expressions which don't have to worry about precidence
1309 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001310 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001311 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1312 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001313 |
David Tolnay8c91b882017-12-28 23:04:32 -05001314 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001315 |
1316 // must be before expr_path
David Tolnay8c91b882017-12-28 23:04:32 -05001317 cond_reduce!(allow_struct, map!(syn!(ExprStruct), Expr::Struct))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001318 |
David Tolnay8c91b882017-12-28 23:04:32 -05001319 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
David Tolnay8c91b882017-12-28 23:04:32 -05001321 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001322 |
1323 call!(expr_break, allow_struct) // must be before expr_path
1324 |
David Tolnay8c91b882017-12-28 23:04:32 -05001325 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326 |
1327 call!(expr_ret, allow_struct) // must be before expr_path
1328 |
David Tolnay8c91b882017-12-28 23:04:32 -05001329 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 |
David Tolnay8c91b882017-12-28 23:04:32 -05001331 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 |
David Tolnay8c91b882017-12-28 23:04:32 -05001333 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001334 |
David Tolnay8c91b882017-12-28 23:04:32 -05001335 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001336 |
David Tolnay8c91b882017-12-28 23:04:32 -05001337 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001338 |
David Tolnay8c91b882017-12-28 23:04:32 -05001339 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001340 |
David Tolnay8c91b882017-12-28 23:04:32 -05001341 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001342 |
David Tolnay8c91b882017-12-28 23:04:32 -05001343 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001344 |
David Tolnay8c91b882017-12-28 23:04:32 -05001345 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001346 |
David Tolnay8c91b882017-12-28 23:04:32 -05001347 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001348 |
David Tolnay8c91b882017-12-28 23:04:32 -05001349 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001350 |
David Tolnay8c91b882017-12-28 23:04:32 -05001351 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001352 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001353 call!(expr_closure, allow_struct)
1354 |
David Tolnay8c91b882017-12-28 23:04:32 -05001355 cond_reduce!(allow_block, map!(syn!(ExprBlock), Expr::Block))
Michael Layzellb78f3b52017-06-04 19:03:03 -04001356 |
1357 // NOTE: This is the prefix-form of range
1358 call!(expr_range, allow_struct)
1359 |
David Tolnay8c91b882017-12-28 23:04:32 -05001360 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001361 |
David Tolnay8c91b882017-12-28 23:04:32 -05001362 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001363 ));
1364
Michael Layzell734adb42017-06-07 16:58:31 -04001365 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001366 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001367 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001368 |
David Tolnay8c91b882017-12-28 23:04:32 -05001369 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001370 ));
1371
Michael Layzell734adb42017-06-07 16:58:31 -04001372 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001373 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001374 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001375 |
David Tolnay8c91b882017-12-28 23:04:32 -05001376 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001377 |
David Tolnay8c91b882017-12-28 23:04:32 -05001378 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001379 |
David Tolnay8c91b882017-12-28 23:04:32 -05001380 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001381 |
David Tolnay8c91b882017-12-28 23:04:32 -05001382 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001383 |
David Tolnay8c91b882017-12-28 23:04:32 -05001384 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001385 |
David Tolnay8c91b882017-12-28 23:04:32 -05001386 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001387 |
David Tolnay8c91b882017-12-28 23:04:32 -05001388 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001389 |
David Tolnay8c91b882017-12-28 23:04:32 -05001390 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001391 |
David Tolnay8c91b882017-12-28 23:04:32 -05001392 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001393 |
David Tolnay8c91b882017-12-28 23:04:32 -05001394 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001395 ), Expr::from));
1396
David Tolnay8c91b882017-12-28 23:04:32 -05001397 impl Synom for ExprLit {
1398 named!(parse -> Self, do_parse!(
1399 lit: syn!(Lit) >>
1400 (ExprLit {
1401 attrs: Vec::new(),
1402 lit: lit,
1403 })
1404 ));
1405 }
1406
1407 #[cfg(feature = "full")]
1408 impl Synom for ExprMacro {
1409 named!(parse -> Self, do_parse!(
1410 mac: syn!(Macro) >>
1411 (ExprMacro {
1412 attrs: Vec::new(),
1413 mac: mac,
1414 })
1415 ));
1416 }
1417
David Tolnaye98775f2017-12-28 23:17:00 -05001418 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001419 impl Synom for ExprGroup {
1420 named!(parse -> Self, do_parse!(
1421 e: grouped!(syn!(Expr)) >>
1422 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001423 attrs: Vec::new(),
Michael Layzell93c36282017-06-04 20:43:14 -04001424 expr: Box::new(e.0),
1425 group_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001426 })
Michael Layzell93c36282017-06-04 20:43:14 -04001427 ));
1428 }
1429
David Tolnaye98775f2017-12-28 23:17:00 -05001430 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001431 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001432 named!(parse -> Self, do_parse!(
1433 e: parens!(syn!(Expr)) >>
1434 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001435 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001436 expr: Box::new(e.0),
1437 paren_token: e.1,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001438 })
Michael Layzell92639a52017-06-01 00:07:44 -04001439 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001440 }
David Tolnay89e05672016-10-02 14:39:42 -07001441
Michael Layzell734adb42017-06-07 16:58:31 -04001442 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001443 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001444 named!(parse -> Self, do_parse!(
1445 elems: brackets!(call!(Delimited::parse_terminated)) >>
1446 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001447 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001448 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001449 bracket_token: elems.1,
1450 })
1451 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001452 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001453
David Tolnay32954ef2017-12-26 22:43:16 -05001454 named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren),
Alex Crichton954046c2017-05-30 21:49:42 -07001455 parens!(call!(Delimited::parse_terminated)));
David Tolnayfa0edf22016-09-23 22:58:24 -07001456
Michael Layzell734adb42017-06-07 16:58:31 -04001457 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001458 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001459 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001460 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001461 turbofish: option!(tuple!(
1462 punct!(::),
1463 punct!(<),
1464 call!(Delimited::parse_terminated),
1465 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001466 )) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001467 args: parens!(call!(Delimited::parse_terminated)) >>
1468 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001469 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001470 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001471 // this expr will get overwritten after being returned
David Tolnay76418512017-12-28 23:47:47 -05001472 receiver: Box::new(Expr::Lit(ExprLit {
David Tolnay8c91b882017-12-28 23:04:32 -05001473 attrs: Vec::new(),
1474 lit: Lit {
1475 span: Span::default(),
1476 value: LitKind::Bool(false),
1477 },
Alex Crichton954046c2017-05-30 21:49:42 -07001478 }).into()),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001479
Alex Crichton954046c2017-05-30 21:49:42 -07001480 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001481 turbofish: turbofish.map(|fish| MethodTurbofish {
1482 colon2_token: fish.0,
1483 lt_token: fish.1,
1484 args: fish.2,
1485 gt_token: fish.3,
1486 }),
Alex Crichton954046c2017-05-30 21:49:42 -07001487 args: args.0,
1488 paren_token: args.1,
1489 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001490 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001491 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001492 ));
1493
Michael Layzell734adb42017-06-07 16:58:31 -04001494 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001495 impl Synom for GenericMethodArgument {
1496 // TODO parse const generics as well
1497 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1498 }
1499
1500 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001501 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001502 named!(parse -> Self, do_parse!(
1503 elems: parens!(call!(Delimited::parse_terminated)) >>
David Tolnay05362582017-12-26 01:33:57 -05001504 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001505 attrs: Vec::new(),
David Tolnay2a86fdd2017-12-28 23:34:28 -05001506 elems: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001507 paren_token: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001508 })
1509 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001510 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001511
Michael Layzell734adb42017-06-07 16:58:31 -04001512 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001513 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001514 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001515 if_: keyword!(if) >>
1516 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001517 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001518 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001519 cond: expr_no_struct >>
1520 then_block: braces!(call!(Block::parse_within)) >>
1521 else_block: option!(else_block) >>
1522 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001523 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001524 pat: Box::new(pat),
1525 let_token: let_,
1526 eq_token: eq,
1527 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001528 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001529 stmts: then_block.0,
1530 brace_token: then_block.1,
1531 },
1532 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001533 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001534 })
1535 ));
David Tolnay29f9ce12016-10-02 20:58:40 -07001536 }
1537
Michael Layzell734adb42017-06-07 16:58:31 -04001538 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001539 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001540 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001541 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001542 cond: expr_no_struct >>
1543 then_block: braces!(call!(Block::parse_within)) >>
1544 else_block: option!(else_block) >>
1545 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001546 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001547 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001548 then_branch: Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001549 stmts: then_block.0,
1550 brace_token: then_block.1,
1551 },
1552 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001553 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001554 })
1555 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001556 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001557
Michael Layzell734adb42017-06-07 16:58:31 -04001558 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001559 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001560 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001561 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001562 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001563 |
David Tolnay8c91b882017-12-28 23:04:32 -05001564 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001565 |
1566 do_parse!(
1567 else_block: braces!(call!(Block::parse_within)) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001568 (Expr::Block(ExprBlock {
1569 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001570 block: Block {
1571 stmts: else_block.0,
1572 brace_token: else_block.1,
1573 },
1574 }))
David Tolnay939766a2016-09-23 23:48:12 -07001575 )
Alex Crichton954046c2017-05-30 21:49:42 -07001576 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001577 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001578 ));
1579
Michael Layzell734adb42017-06-07 16:58:31 -04001580 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001581 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001582 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001583 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001584 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001585 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001586 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001587 expr: expr_no_struct >>
1588 loop_block: syn!(Block) >>
1589 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001590 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001591 for_token: for_,
1592 in_token: in_,
1593 pat: Box::new(pat),
1594 expr: Box::new(expr),
1595 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001596 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001597 })
1598 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001599 }
Gregory Katze5f35682016-09-27 14:20:55 -04001600
Michael Layzell734adb42017-06-07 16:58:31 -04001601 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001602 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001603 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001604 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001605 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001606 loop_block: syn!(Block) >>
1607 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001608 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001609 loop_token: loop_,
1610 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001611 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001612 })
1613 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001614 }
1615
Michael Layzell734adb42017-06-07 16:58:31 -04001616 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001617 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001618 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001619 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001620 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001621 res: braces!(many0!(Arm::parse)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001622 ({
Alex Crichton03b30272017-08-28 09:35:24 -07001623 let (arms, brace) = res;
Michael Layzell92639a52017-06-01 00:07:44 -04001624 ExprMatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001625 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001626 expr: Box::new(obj),
1627 match_token: match_,
1628 brace_token: brace,
Alex Crichton03b30272017-08-28 09:35:24 -07001629 arms: arms,
Michael Layzell92639a52017-06-01 00:07:44 -04001630 }
1631 })
1632 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001633 }
David Tolnay1978c672016-10-27 22:05:52 -07001634
Michael Layzell734adb42017-06-07 16:58:31 -04001635 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001636 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001637 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001638 do_: keyword!(do) >>
1639 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001640 catch_block: syn!(Block) >>
1641 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001642 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001643 block: catch_block,
1644 do_token: do_,
1645 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001646 })
Michael Layzell92639a52017-06-01 00:07:44 -04001647 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001648 }
Arnavion02ef13f2017-04-25 00:54:31 -07001649
Michael Layzell734adb42017-06-07 16:58:31 -04001650 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001651 impl Synom for ExprYield {
1652 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001653 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001654 expr: option!(syn!(Expr)) >>
1655 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001656 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001657 yield_token: yield_,
1658 expr: expr.map(Box::new),
1659 })
1660 ));
1661 }
1662
1663 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001664 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001665 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001666 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001667 pats: call!(Delimited::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001668 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1669 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001670 body: do_parse!(
1671 expr: alt!(expr_nosemi | syn!(Expr)) >>
1672 comma1: cond!(arm_expr_requires_comma(&expr), alt!(
1673 map!(input_end!(), |_| None)
1674 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001675 map!(punct!(,), Some)
Alex Crichton03b30272017-08-28 09:35:24 -07001676 )) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001677 comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >>
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001678 (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x)))
Michael Layzell92639a52017-06-01 00:07:44 -04001679 ) >>
1680 (Arm {
1681 rocket_token: rocket,
David Tolnayf8db7ba2017-11-11 22:52:16 -08001682 if_token: guard.as_ref().map(|p| Token![if]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04001683 attrs: attrs,
1684 pats: pats,
1685 guard: guard.map(|p| Box::new(p.1)),
Alex Crichton03b30272017-08-28 09:35:24 -07001686 body: Box::new(body.0),
1687 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001688 })
1689 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001690 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001691
Michael Layzell734adb42017-06-07 16:58:31 -04001692 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001693 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001694 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001695 or1: punct!(|) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001696 inputs: call!(Delimited::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001697 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001698 ret_and_body: alt!(
1699 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001700 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001701 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001702 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001703 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001704 Expr::Block(ExprBlock {
1705 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001706 block: body,
1707 }).into())
David Tolnay89e05672016-10-02 14:39:42 -07001708 )
1709 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001710 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001711 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001712 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001713 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001714 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001715 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001716 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001717 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001718 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001719 body: Box::new(ret_and_body.1),
1720 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001721 ));
1722
Michael Layzell734adb42017-06-07 16:58:31 -04001723 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001724 named!(fn_arg -> FnArg, do_parse!(
1725 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001726 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001727 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001728 if let Some((colon, ty)) = ty {
1729 FnArg::Captured(ArgCaptured {
1730 pat: pat,
1731 colon_token: colon,
1732 ty: ty,
1733 })
1734 } else {
1735 FnArg::Inferred(pat)
1736 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001737 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001738 ));
1739
Michael Layzell734adb42017-06-07 16:58:31 -04001740 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001741 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001742 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001743 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001744 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001745 cond: expr_no_struct >>
1746 while_block: syn!(Block) >>
1747 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001748 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001749 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001750 cond: Box::new(cond),
1751 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001752 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001753 })
1754 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001755 }
1756
Michael Layzell734adb42017-06-07 16:58:31 -04001757 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001758 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001759 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001760 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001761 while_: keyword!(while) >>
1762 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001763 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001764 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001765 value: expr_no_struct >>
1766 while_block: syn!(Block) >>
1767 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001768 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001769 eq_token: eq,
1770 let_token: let_,
1771 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001772 pat: Box::new(pat),
1773 expr: Box::new(value),
1774 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001775 label: label,
1776 })
1777 ));
1778 }
1779
1780 #[cfg(feature = "full")]
1781 impl Synom for Label {
1782 named!(parse -> Self, do_parse!(
1783 name: syn!(Lifetime) >>
1784 colon: punct!(:) >>
1785 (Label {
1786 name: name,
1787 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001788 })
1789 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001790 }
1791
Michael Layzell734adb42017-06-07 16:58:31 -04001792 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001793 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001794 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001795 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001796 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001797 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001798 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001799 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001800 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001801 })
1802 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001803 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001804
Michael Layzell734adb42017-06-07 16:58:31 -04001805 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001806 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001807 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001808 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001809 // We can't allow blocks after a `break` expression when we wouldn't
1810 // allow structs, as this expression is ambiguous.
1811 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001812 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001813 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001814 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001815 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001816 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001817 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001818 ));
1819
Michael Layzell734adb42017-06-07 16:58:31 -04001820 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001821 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001822 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001823 // NOTE: return is greedy and eats blocks after it even when in a
1824 // position where structs are not allowed, such as in if statement
1825 // conditions. For example:
1826 //
David Tolnaybcf26022017-12-25 22:10:52 -05001827 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001828 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001829 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001830 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001831 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001832 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001833 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001834 ));
1835
Michael Layzell734adb42017-06-07 16:58:31 -04001836 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001837 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001838 named!(parse -> Self, do_parse!(
1839 path: syn!(Path) >>
1840 data: braces!(do_parse!(
1841 fields: call!(Delimited::parse_terminated) >>
1842 base: option!(
1843 cond!(fields.is_empty() || fields.trailing_delim(),
1844 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001845 dots: punct!(..) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001846 base: syn!(Expr) >>
1847 (dots, base)
Alex Crichton954046c2017-05-30 21:49:42 -07001848 )
Michael Layzell92639a52017-06-01 00:07:44 -04001849 )
1850 ) >>
1851 (fields, base)
1852 )) >>
1853 ({
1854 let ((fields, base), brace) = data;
1855 let (dots, rest) = match base.and_then(|b| b) {
1856 Some((dots, base)) => (Some(dots), Some(base)),
1857 None => (None, None),
1858 };
1859 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001860 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001861 brace_token: brace,
1862 path: path,
1863 fields: fields,
1864 dot2_token: dots,
1865 rest: rest.map(Box::new),
1866 }
1867 })
1868 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001869 }
1870
Michael Layzell734adb42017-06-07 16:58:31 -04001871 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001872 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001873 named!(parse -> Self, alt!(
1874 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001875 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001876 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001877 value: syn!(Expr) >>
1878 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001879 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001880 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001881 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001882 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001883 })
Michael Layzell92639a52017-06-01 00:07:44 -04001884 )
1885 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001886 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001887 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001888 expr: Expr::Path(ExprPath {
1889 attrs: Vec::new(),
1890 qself: None,
1891 path: name.into(),
1892 }).into(),
Michael Layzell92639a52017-06-01 00:07:44 -04001893 attrs: Vec::new(),
1894 colon_token: None,
1895 })
1896 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001897 }
David Tolnay055a7042016-10-02 19:23:54 -07001898
Michael Layzell734adb42017-06-07 16:58:31 -04001899 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001900 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001901 named!(parse -> Self, do_parse!(
1902 data: brackets!(do_parse!(
1903 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001904 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001905 times: syn!(Expr) >>
1906 (value, semi, times)
1907 )) >>
1908 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001909 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001910 expr: Box::new((data.0).0),
1911 amt: Box::new((data.0).2),
1912 bracket_token: data.1,
1913 semi_token: (data.0).1,
1914 })
1915 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001916 }
David Tolnay055a7042016-10-02 19:23:54 -07001917
Michael Layzell734adb42017-06-07 16:58:31 -04001918 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001919 impl Synom for ExprUnsafe {
1920 named!(parse -> Self, do_parse!(
1921 unsafe_: keyword!(unsafe) >>
1922 b: syn!(Block) >>
1923 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001924 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001925 unsafe_token: unsafe_,
1926 block: b,
1927 })
1928 ));
1929 }
1930
1931 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001932 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001933 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001934 b: syn!(Block) >>
1935 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001936 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001937 block: b,
1938 })
1939 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001940 }
David Tolnay89e05672016-10-02 14:39:42 -07001941
Michael Layzell734adb42017-06-07 16:58:31 -04001942 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001943 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001944 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001945 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001946 (ExprRange {
1947 attrs: Vec::new(),
1948 from: None,
1949 to: hi.map(Box::new),
1950 limits: limits,
1951 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001952 ));
1953
Michael Layzell734adb42017-06-07 16:58:31 -04001954 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001955 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04001956 named!(parse -> Self, alt!(
1957 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08001958 punct!(..=) => { RangeLimits::Closed }
1959 |
1960 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08001961 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04001962 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001963 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04001964 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001965 }
David Tolnay438c9052016-10-07 23:24:48 -07001966
Alex Crichton954046c2017-05-30 21:49:42 -07001967 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04001968 named!(parse -> Self, do_parse!(
1969 pair: qpath >>
1970 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05001971 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001972 qself: pair.0,
1973 path: pair.1,
1974 })
1975 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001976 }
David Tolnay42602292016-10-01 22:25:45 -07001977
Michael Layzell734adb42017-06-07 16:58:31 -04001978 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05001979 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07001980
David Tolnay32954ef2017-12-26 22:43:16 -05001981 named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07001982
Michael Layzell734adb42017-06-07 16:58:31 -04001983 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001984 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001985 named!(parse -> Self, do_parse!(
1986 stmts: braces!(call!(Block::parse_within)) >>
1987 (Block {
1988 stmts: stmts.0,
1989 brace_token: stmts.1,
1990 })
1991 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001992 }
David Tolnay939766a2016-09-23 23:48:12 -07001993
Michael Layzell734adb42017-06-07 16:58:31 -04001994 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001995 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04001996 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05001997 many0!(punct!(;)) >>
1998 mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07001999 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002000 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002001 mut e: syn!(Expr) >>
2002 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002003 e.replace_attrs(attrs);
Alex Crichton70bbd592017-08-27 10:40:03 -07002004 Stmt::Expr(Box::new(e))
2005 })
2006 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002007 (match last {
2008 None => standalone,
2009 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002010 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002011 standalone
2012 }
2013 })
2014 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002015 }
2016
Michael Layzell734adb42017-06-07 16:58:31 -04002017 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002018 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002019 named!(parse -> Self, alt!(
2020 stmt_mac
2021 |
2022 stmt_local
2023 |
2024 stmt_item
2025 |
Michael Layzell35418782017-06-07 09:20:25 -04002026 stmt_blockexpr
2027 |
Michael Layzell92639a52017-06-01 00:07:44 -04002028 stmt_expr
2029 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002030 }
David Tolnay939766a2016-09-23 23:48:12 -07002031
Michael Layzell734adb42017-06-07 16:58:31 -04002032 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002033 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002034 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002035 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002036 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002037 // Only parse braces here; paren and bracket will get parsed as
2038 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002039 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002040 semi: option!(punct!(;)) >>
David Tolnay57b52bc2017-12-28 18:06:38 -05002041 (Stmt::Item(Box::new(Item::Macro(ItemMacro {
2042 attrs: attrs,
2043 ident: None,
2044 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002045 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002046 bang_token: bang,
David Tolnayfe9d2782017-12-29 11:32:42 -05002047 tt: proc_macro2::TokenTree {
David Tolnay98942562017-12-26 21:24:35 -05002048 span: (data.1).0,
Alex Crichtonf9e8f1a2017-07-05 18:20:44 -07002049 kind: TokenNode::Group(Delimiter::Brace, data.0),
David Tolnay369f0c52017-12-27 01:50:45 -05002050 },
David Tolnayeea28d62016-10-25 20:44:08 -07002051 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002052 semi_token: semi,
2053 }))))
David Tolnay13b3d352016-10-03 00:31:15 -07002054 ));
2055
Michael Layzell734adb42017-06-07 16:58:31 -04002056 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002057 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002058 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002059 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002060 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002061 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002062 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2063 semi: punct!(;) >>
David Tolnay191e0582016-10-02 18:31:09 -07002064 (Stmt::Local(Box::new(Local {
Alex Crichton954046c2017-05-30 21:49:42 -07002065 let_token: let_,
2066 semi_token: semi,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002067 colon_token: ty.as_ref().map(|p| Token![:]((p.0).0)),
2068 eq_token: init.as_ref().map(|p| Token![=]((p.0).0)),
David Tolnay191e0582016-10-02 18:31:09 -07002069 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002070 ty: ty.map(|p| Box::new(p.1)),
2071 init: init.map(|p| Box::new(p.1)),
David Tolnay191e0582016-10-02 18:31:09 -07002072 attrs: attrs,
2073 })))
2074 ));
2075
Michael Layzell734adb42017-06-07 16:58:31 -04002076 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002077 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i))));
David Tolnay191e0582016-10-02 18:31:09 -07002078
Michael Layzell734adb42017-06-07 16:58:31 -04002079 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002080 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002081 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002082 mut e: expr_nosemi >>
2083 // If the next token is a `.` or a `?` it is special-cased to parse as
2084 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002085 not!(punct!(.)) >>
2086 not!(punct!(?)) >>
2087 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002088 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002089 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002090 if let Some(semi) = semi {
2091 Stmt::Semi(Box::new(e), semi)
2092 } else {
2093 Stmt::Expr(Box::new(e))
2094 }
2095 })
2096 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002097
Michael Layzell734adb42017-06-07 16:58:31 -04002098 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002099 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002100 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002101 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002102 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002103 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002104 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002105 Stmt::Semi(Box::new(e), semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002106 })
David Tolnay939766a2016-09-23 23:48:12 -07002107 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002108
Michael Layzell734adb42017-06-07 16:58:31 -04002109 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002110 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002111 named!(parse -> Self, alt!(
2112 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2113 |
2114 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2115 |
2116 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2117 |
2118 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2119 |
2120 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2121 |
David Tolnay323279a2017-12-29 11:26:32 -05002122 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002123 |
2124 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2125 |
2126 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2127 |
2128 syn!(PatPath) => { Pat::Path }
2129 |
2130 syn!(PatTuple) => { Pat::Tuple }
2131 |
2132 syn!(PatRef) => { Pat::Ref }
2133 |
2134 syn!(PatSlice) => { Pat::Slice }
2135 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002136 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002137
Michael Layzell734adb42017-06-07 16:58:31 -04002138 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002139 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002140 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002141 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002142 |u| PatWild { underscore_token: u }
2143 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002144 }
David Tolnay84aa0752016-10-02 23:01:13 -07002145
Michael Layzell734adb42017-06-07 16:58:31 -04002146 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002147 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002148 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002149 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002150 pat: syn!(Pat) >>
2151 (PatBox {
2152 pat: Box::new(pat),
2153 box_token: boxed,
2154 })
2155 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002156 }
2157
Michael Layzell734adb42017-06-07 16:58:31 -04002158 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002159 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002160 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002161 by_ref: option!(keyword!(ref)) >>
2162 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002163 name: alt!(
2164 syn!(Ident)
2165 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002166 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002167 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002168 not!(punct!(<)) >>
2169 not!(punct!(::)) >>
2170 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002171 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002172 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002173 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002174 ident: name,
David Tolnayf8db7ba2017-11-11 22:52:16 -08002175 at_token: subpat.as_ref().map(|p| Token![@]((p.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002176 subpat: subpat.map(|p| Box::new(p.1)),
2177 })
2178 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002179 }
2180
Michael Layzell734adb42017-06-07 16:58:31 -04002181 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002182 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002183 named!(parse -> Self, do_parse!(
2184 path: syn!(Path) >>
2185 tuple: syn!(PatTuple) >>
2186 (PatTupleStruct {
2187 path: path,
2188 pat: tuple,
2189 })
2190 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002191 }
2192
Michael Layzell734adb42017-06-07 16:58:31 -04002193 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002194 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002195 named!(parse -> Self, do_parse!(
2196 path: syn!(Path) >>
2197 data: braces!(do_parse!(
2198 fields: call!(Delimited::parse_terminated) >>
2199 base: option!(
2200 cond!(fields.is_empty() || fields.trailing_delim(),
David Tolnayf8db7ba2017-11-11 22:52:16 -08002201 punct!(..))
Michael Layzell92639a52017-06-01 00:07:44 -04002202 ) >>
2203 (fields, base)
2204 )) >>
2205 (PatStruct {
2206 path: path,
2207 fields: (data.0).0,
2208 brace_token: data.1,
2209 dot2_token: (data.0).1.and_then(|m| m),
2210 })
2211 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002212 }
2213
Michael Layzell734adb42017-06-07 16:58:31 -04002214 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002215 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002216 named!(parse -> Self, alt!(
2217 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002218 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002219 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002220 pat: syn!(Pat) >>
2221 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002222 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002223 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002224 attrs: Vec::new(),
2225 colon_token: Some(colon),
2226 })
2227 )
2228 |
2229 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002230 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002231 by_ref: option!(keyword!(ref)) >>
2232 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002233 ident: syn!(Ident) >>
2234 ({
2235 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002236 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002237 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002238 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002239 subpat: None,
2240 at_token: None,
2241 }.into();
2242 if let Some(boxed) = boxed {
2243 pat = PatBox {
2244 pat: Box::new(pat),
2245 box_token: boxed,
2246 }.into();
2247 }
2248 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002249 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002250 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002251 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002252 colon_token: None,
2253 }
2254 })
2255 )
2256 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002257 }
2258
Michael Layzell734adb42017-06-07 16:58:31 -04002259 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002260 impl Synom for Member {
2261 named!(parse -> Self, alt!(
2262 syn!(Ident) => { Member::Named }
2263 |
2264 syn!(Index) => { Member::Unnamed }
2265 ));
2266 }
2267
2268 #[cfg(feature = "full")]
2269 impl Synom for Index {
2270 named!(parse -> Self, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002271 lit: syn!(Lit) >>
2272 ({
David Tolnay85b69a42017-12-27 20:43:10 -05002273 if let Ok(i) = lit.value.to_string().parse() {
2274 Index { index: i, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002275 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002276 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002277 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002278 })
David Tolnay85b69a42017-12-27 20:43:10 -05002279 ));
2280 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002281
Michael Layzell734adb42017-06-07 16:58:31 -04002282 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002283 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002284 named!(parse -> Self, map!(
2285 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002286 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002287 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002288 }
David Tolnay9636c052016-10-02 17:11:17 -07002289
Michael Layzell734adb42017-06-07 16:58:31 -04002290 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002291 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002292 named!(parse -> Self, do_parse!(
2293 data: parens!(do_parse!(
David Tolnay41871922017-12-29 01:53:45 -05002294 front: call!(Delimited::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002295 dotdot: map!(cond!(
David Tolnay41871922017-12-29 01:53:45 -05002296 front.is_empty() || front.trailing_delim(),
Michael Layzell92639a52017-06-01 00:07:44 -04002297 option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002298 dots: punct!(..) >>
2299 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002300 (dots, trailing)
2301 ))
David Tolnay41871922017-12-29 01:53:45 -05002302 ), Option::unwrap_or_default) >>
2303 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002304 Some((_, Some(_))) => true,
2305 _ => false,
2306 },
2307 call!(Delimited::parse_terminated)) >>
David Tolnay41871922017-12-29 01:53:45 -05002308 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002309 )) >>
2310 ({
David Tolnay41871922017-12-29 01:53:45 -05002311 let ((front, dotdot, back), parens) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002312 let (dotdot, trailing) = match dotdot {
2313 Some((a, b)) => (Some(a), Some(b)),
2314 None => (None, None),
2315 };
2316 PatTuple {
2317 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002318 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002319 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002320 comma_token: trailing.unwrap_or_default(),
2321 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002322 }
2323 })
2324 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002325 }
David Tolnayfbb73232016-10-03 01:00:06 -07002326
Michael Layzell734adb42017-06-07 16:58:31 -04002327 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002328 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002329 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002330 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002331 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002332 pat: syn!(Pat) >>
2333 (PatRef {
2334 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002335 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002336 and_token: and,
2337 })
2338 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002339 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002340
Michael Layzell734adb42017-06-07 16:58:31 -04002341 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002342 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002343 named!(parse -> Self, do_parse!(
2344 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002345 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002346 return parse_error(); // these need to be parsed by pat_path
2347 } else {
2348 PatLit {
2349 expr: Box::new(lit),
2350 }
2351 })
2352 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002353 }
David Tolnaye1310902016-10-29 23:40:00 -07002354
Michael Layzell734adb42017-06-07 16:58:31 -04002355 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002356 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002357 named!(parse -> Self, do_parse!(
2358 lo: pat_lit_expr >>
2359 limits: syn!(RangeLimits) >>
2360 hi: pat_lit_expr >>
2361 (PatRange {
2362 lo: Box::new(lo),
2363 hi: Box::new(hi),
2364 limits: limits,
2365 })
2366 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002367 }
David Tolnaye1310902016-10-29 23:40:00 -07002368
Michael Layzell734adb42017-06-07 16:58:31 -04002369 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002370 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002371 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002372 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002373 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002374 |
David Tolnay8c91b882017-12-28 23:04:32 -05002375 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002376 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002377 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002378 Expr::Unary(ExprUnary {
2379 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002380 op: UnOp::Neg(neg),
Alex Crichton62a0a592017-05-22 13:58:53 -07002381 expr: Box::new(v.into())
2382 }).into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002383 } else {
David Tolnay7184b132016-10-30 10:06:37 -07002384 v.into()
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002385 })
2386 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002387
Michael Layzell734adb42017-06-07 16:58:31 -04002388 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002389 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002390 named!(parse -> Self, map!(
2391 brackets!(do_parse!(
2392 before: call!(Delimited::parse_terminated) >>
2393 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002394 dots: punct!(..) >>
2395 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002396 (dots, trailing)
2397 )) >>
2398 after: cond!(
2399 match middle {
2400 Some((_, ref trailing)) => trailing.is_some(),
2401 _ => false,
2402 },
2403 call!(Delimited::parse_terminated)
2404 ) >>
2405 (before, middle, after)
2406 )),
2407 |((before, middle, after), brackets)| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002408 let mut before: Delimited<Pat, Token![,]> = before;
2409 let after: Option<Delimited<Pat, Token![,]>> = after;
2410 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002411 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002412 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002413 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002414 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002415 }),
2416 bracket_token: brackets,
2417 middle: middle.and_then(|_| {
2418 if !before.is_empty() && !before.trailing_delim() {
2419 Some(Box::new(before.pop().unwrap().into_item()))
2420 } else {
2421 None
2422 }
2423 }),
2424 front: before,
2425 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002426 }
Alex Crichton954046c2017-05-30 21:49:42 -07002427 }
Michael Layzell92639a52017-06-01 00:07:44 -04002428 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002429 }
David Tolnay323279a2017-12-29 11:26:32 -05002430
2431 #[cfg(feature = "full")]
2432 impl Synom for PatMacro {
2433 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
2434 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002435}
2436
David Tolnayf4bbbd92016-09-23 14:41:55 -07002437#[cfg(feature = "printing")]
2438mod printing {
2439 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002440 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002441 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002442 use quote::{ToTokens, Tokens};
David Tolnay85b69a42017-12-27 20:43:10 -05002443 #[cfg(feature = "full")]
2444 use proc_macro2::{TokenTree, TokenNode, Literal};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002445
David Tolnaybcf26022017-12-25 22:10:52 -05002446 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2447 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002448 #[cfg(feature = "full")]
2449 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002450 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002451 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002452 e.to_tokens(tokens);
2453 });
2454 } else {
2455 e.to_tokens(tokens);
2456 }
2457 }
2458
David Tolnay8c91b882017-12-28 23:04:32 -05002459 #[cfg(feature = "full")]
2460 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2461 tokens.append_all(attrs.outer());
2462 }
Michael Layzell734adb42017-06-07 16:58:31 -04002463
David Tolnay8c91b882017-12-28 23:04:32 -05002464 #[cfg(not(feature = "full"))]
2465 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {
Alex Crichton62a0a592017-05-22 13:58:53 -07002466 }
2467
Michael Layzell734adb42017-06-07 16:58:31 -04002468 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002469 impl ToTokens for ExprBox {
2470 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002471 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002472 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002473 self.expr.to_tokens(tokens);
2474 }
2475 }
2476
Michael Layzell734adb42017-06-07 16:58:31 -04002477 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002478 impl ToTokens for ExprInPlace {
2479 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002480 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002481 self.place.to_tokens(tokens);
2482 self.arrow_token.to_tokens(tokens);
2483 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002484 }
2485 }
2486
Michael Layzell734adb42017-06-07 16:58:31 -04002487 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002488 impl ToTokens for ExprArray {
2489 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002490 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002491 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002492 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002493 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002494 }
2495 }
2496
2497 impl ToTokens for ExprCall {
2498 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002499 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002500 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002501 self.paren_token.surround(tokens, |tokens| {
2502 self.args.to_tokens(tokens);
2503 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002504 }
2505 }
2506
Michael Layzell734adb42017-06-07 16:58:31 -04002507 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002508 impl ToTokens for ExprMethodCall {
2509 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002510 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002511 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002512 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002513 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002514 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002515 self.paren_token.surround(tokens, |tokens| {
2516 self.args.to_tokens(tokens);
2517 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002518 }
2519 }
2520
Michael Layzell734adb42017-06-07 16:58:31 -04002521 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002522 impl ToTokens for MethodTurbofish {
2523 fn to_tokens(&self, tokens: &mut Tokens) {
2524 self.colon2_token.to_tokens(tokens);
2525 self.lt_token.to_tokens(tokens);
2526 self.args.to_tokens(tokens);
2527 self.gt_token.to_tokens(tokens);
2528 }
2529 }
2530
2531 #[cfg(feature = "full")]
2532 impl ToTokens for GenericMethodArgument {
2533 fn to_tokens(&self, tokens: &mut Tokens) {
2534 match *self {
2535 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2536 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2537 }
2538 }
2539 }
2540
2541 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002542 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002543 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002544 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002545 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002546 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002547 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002548 // distinguish ExprTuple from ExprParen.
David Tolnay2a86fdd2017-12-28 23:34:28 -05002549 if self.elems.len() == 1 && !self.elems.trailing_delim() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002550 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002551 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002552 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002553 }
2554 }
2555
2556 impl ToTokens for ExprBinary {
2557 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002558 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002559 self.left.to_tokens(tokens);
2560 self.op.to_tokens(tokens);
2561 self.right.to_tokens(tokens);
2562 }
2563 }
2564
2565 impl ToTokens for ExprUnary {
2566 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002567 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002568 self.op.to_tokens(tokens);
2569 self.expr.to_tokens(tokens);
2570 }
2571 }
2572
David Tolnay8c91b882017-12-28 23:04:32 -05002573 impl ToTokens for ExprLit {
2574 fn to_tokens(&self, tokens: &mut Tokens) {
2575 attrs_to_tokens(&self.attrs, tokens);
2576 self.lit.to_tokens(tokens);
2577 }
2578 }
2579
Alex Crichton62a0a592017-05-22 13:58:53 -07002580 impl ToTokens for ExprCast {
2581 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002582 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002583 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002584 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002585 self.ty.to_tokens(tokens);
2586 }
2587 }
2588
David Tolnay0cf94f22017-12-28 23:46:26 -05002589 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002590 impl ToTokens for ExprType {
2591 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002592 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002594 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002595 self.ty.to_tokens(tokens);
2596 }
2597 }
2598
Michael Layzell734adb42017-06-07 16:58:31 -04002599 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -05002600 fn maybe_wrap_else(
2601 tokens: &mut Tokens,
David Tolnay2ccf32a2017-12-29 00:34:26 -05002602 else_: &Option<(Token![else], Box<Expr>)>,
David Tolnay51382052017-12-27 13:46:21 -05002603 ) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002604 if let Some((ref else_token, ref else_)) = *else_ {
2605 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002606
2607 // If we are not one of the valid expressions to exist in an else
2608 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002609 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002610 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002611 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002612 }
2613 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002614 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002615 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002616 });
2617 }
2618 }
2619 }
2620 }
2621
2622 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002623 impl ToTokens for ExprIf {
2624 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002625 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002626 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002627 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002628 self.then_branch.to_tokens(tokens);
2629 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002630 }
2631 }
2632
Michael Layzell734adb42017-06-07 16:58:31 -04002633 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002634 impl ToTokens for ExprIfLet {
2635 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002636 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002637 self.if_token.to_tokens(tokens);
2638 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002639 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002640 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002641 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002642 self.then_branch.to_tokens(tokens);
2643 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002644 }
2645 }
2646
Michael Layzell734adb42017-06-07 16:58:31 -04002647 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002648 impl ToTokens for ExprWhile {
2649 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002650 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002651 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002652 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002653 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002654 self.body.to_tokens(tokens);
2655 }
2656 }
2657
Michael Layzell734adb42017-06-07 16:58:31 -04002658 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002659 impl ToTokens for ExprWhileLet {
2660 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002661 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002662 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002663 self.while_token.to_tokens(tokens);
2664 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002665 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002666 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002667 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002668 self.body.to_tokens(tokens);
2669 }
2670 }
2671
Michael Layzell734adb42017-06-07 16:58:31 -04002672 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002673 impl ToTokens for ExprForLoop {
2674 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002675 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002676 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002677 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002678 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002679 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002680 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002681 self.body.to_tokens(tokens);
2682 }
2683 }
2684
Michael Layzell734adb42017-06-07 16:58:31 -04002685 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002686 impl ToTokens for ExprLoop {
2687 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002688 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002689 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002690 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002691 self.body.to_tokens(tokens);
2692 }
2693 }
2694
Michael Layzell734adb42017-06-07 16:58:31 -04002695 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002696 impl ToTokens for ExprMatch {
2697 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002698 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002699 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002700 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002701 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002702 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002703 arm.to_tokens(tokens);
2704 // Ensure that we have a comma after a non-block arm, except
2705 // for the last one.
2706 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002707 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002708 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002709 }
2710 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002711 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002712 }
2713 }
2714
Michael Layzell734adb42017-06-07 16:58:31 -04002715 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002716 impl ToTokens for ExprCatch {
2717 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002718 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002719 self.do_token.to_tokens(tokens);
2720 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002721 self.block.to_tokens(tokens);
2722 }
2723 }
2724
Michael Layzell734adb42017-06-07 16:58:31 -04002725 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002726 impl ToTokens for ExprYield {
2727 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002728 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002729 self.yield_token.to_tokens(tokens);
2730 self.expr.to_tokens(tokens);
2731 }
2732 }
2733
2734 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002735 impl ToTokens for ExprClosure {
2736 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002737 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002738 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002739 self.or1_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002740 for item in self.inputs.iter() {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002741 match **item.item() {
David Tolnay51382052017-12-27 13:46:21 -05002742 FnArg::Captured(ArgCaptured {
2743 ref pat,
2744 ty: Type::Infer(_),
2745 ..
2746 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002747 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002748 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002749 _ => item.item().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002750 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002751 item.delimiter().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002752 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002753 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002754 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002755 self.body.to_tokens(tokens);
2756 }
2757 }
2758
Michael Layzell734adb42017-06-07 16:58:31 -04002759 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002760 impl ToTokens for ExprUnsafe {
2761 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002762 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002763 self.unsafe_token.to_tokens(tokens);
2764 self.block.to_tokens(tokens);
2765 }
2766 }
2767
2768 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002769 impl ToTokens for ExprBlock {
2770 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002771 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002772 self.block.to_tokens(tokens);
2773 }
2774 }
2775
Michael Layzell734adb42017-06-07 16:58:31 -04002776 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002777 impl ToTokens for ExprAssign {
2778 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002779 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002780 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002781 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002782 self.right.to_tokens(tokens);
2783 }
2784 }
2785
Michael Layzell734adb42017-06-07 16:58:31 -04002786 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002787 impl ToTokens for ExprAssignOp {
2788 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002789 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002790 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002791 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002792 self.right.to_tokens(tokens);
2793 }
2794 }
2795
Michael Layzell734adb42017-06-07 16:58:31 -04002796 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002797 impl ToTokens for ExprField {
2798 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002799 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002800 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002801 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002802 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002803 }
2804 }
2805
Michael Layzell734adb42017-06-07 16:58:31 -04002806 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002807 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002808 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002809 match *self {
2810 Member::Named(ident) => ident.to_tokens(tokens),
2811 Member::Unnamed(ref index) => index.to_tokens(tokens),
2812 }
2813 }
2814 }
2815
2816 #[cfg(feature = "full")]
2817 impl ToTokens for Index {
2818 fn to_tokens(&self, tokens: &mut Tokens) {
2819 tokens.append(TokenTree {
2820 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002821 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002822 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002823 }
2824 }
2825
2826 impl ToTokens for ExprIndex {
2827 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002828 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002829 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002830 self.bracket_token.surround(tokens, |tokens| {
2831 self.index.to_tokens(tokens);
2832 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002833 }
2834 }
2835
Michael Layzell734adb42017-06-07 16:58:31 -04002836 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002837 impl ToTokens for ExprRange {
2838 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002839 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002840 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002841 match self.limits {
2842 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2843 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2844 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002845 self.to.to_tokens(tokens);
2846 }
2847 }
2848
2849 impl ToTokens for ExprPath {
2850 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002851 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002852 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002853 }
2854 }
2855
Michael Layzell734adb42017-06-07 16:58:31 -04002856 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002857 impl ToTokens for ExprAddrOf {
2858 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002859 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002860 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002861 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002862 self.expr.to_tokens(tokens);
2863 }
2864 }
2865
Michael Layzell734adb42017-06-07 16:58:31 -04002866 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002867 impl ToTokens for ExprBreak {
2868 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002869 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002870 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002871 self.label.to_tokens(tokens);
2872 self.expr.to_tokens(tokens);
2873 }
2874 }
2875
Michael Layzell734adb42017-06-07 16:58:31 -04002876 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002877 impl ToTokens for ExprContinue {
2878 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002879 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002880 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002881 self.label.to_tokens(tokens);
2882 }
2883 }
2884
Michael Layzell734adb42017-06-07 16:58:31 -04002885 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002886 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002887 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002888 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002889 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002890 self.expr.to_tokens(tokens);
2891 }
2892 }
2893
Michael Layzell734adb42017-06-07 16:58:31 -04002894 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002895 impl ToTokens for ExprMacro {
2896 fn to_tokens(&self, tokens: &mut Tokens) {
2897 tokens.append_all(self.attrs.outer());
2898 self.mac.to_tokens(tokens);
2899 }
2900 }
2901
2902 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002903 impl ToTokens for ExprStruct {
2904 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002905 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002906 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002907 self.brace_token.surround(tokens, |tokens| {
2908 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002909 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002910 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002911 self.rest.to_tokens(tokens);
2912 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002913 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002914 }
2915 }
2916
Michael Layzell734adb42017-06-07 16:58:31 -04002917 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 impl ToTokens for ExprRepeat {
2919 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002920 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002921 self.bracket_token.surround(tokens, |tokens| {
2922 self.expr.to_tokens(tokens);
2923 self.semi_token.to_tokens(tokens);
2924 self.amt.to_tokens(tokens);
2925 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002926 }
2927 }
2928
David Tolnaye98775f2017-12-28 23:17:00 -05002929 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04002930 impl ToTokens for ExprGroup {
2931 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002932 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04002933 self.group_token.surround(tokens, |tokens| {
2934 self.expr.to_tokens(tokens);
2935 });
2936 }
2937 }
2938
David Tolnaye98775f2017-12-28 23:17:00 -05002939 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002940 impl ToTokens for ExprParen {
2941 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002942 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002943 self.paren_token.surround(tokens, |tokens| {
2944 self.expr.to_tokens(tokens);
2945 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002946 }
2947 }
2948
Michael Layzell734adb42017-06-07 16:58:31 -04002949 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002950 impl ToTokens for ExprTry {
2951 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002952 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002953 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002954 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002955 }
2956 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002957
David Tolnay2ae520a2017-12-29 11:19:50 -05002958 impl ToTokens for ExprVerbatim {
2959 fn to_tokens(&self, tokens: &mut Tokens) {
2960 self.tts.to_tokens(tokens);
2961 }
2962 }
2963
Michael Layzell734adb42017-06-07 16:58:31 -04002964 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05002965 impl ToTokens for Label {
2966 fn to_tokens(&self, tokens: &mut Tokens) {
2967 self.name.to_tokens(tokens);
2968 self.colon_token.to_tokens(tokens);
2969 }
2970 }
2971
2972 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07002973 impl ToTokens for FieldValue {
2974 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002975 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05002976 if let Some(ref colon_token) = self.colon_token {
2977 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07002978 self.expr.to_tokens(tokens);
2979 }
David Tolnay055a7042016-10-02 19:23:54 -07002980 }
2981 }
2982
Michael Layzell734adb42017-06-07 16:58:31 -04002983 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07002984 impl ToTokens for Arm {
2985 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002986 tokens.append_all(&self.attrs);
2987 self.pats.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002988 if self.guard.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07002989 TokensOrDefault(&self.if_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002990 self.guard.to_tokens(tokens);
2991 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002992 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002993 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002994 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07002995 }
2996 }
2997
Michael Layzell734adb42017-06-07 16:58:31 -04002998 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002999 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003000 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003001 self.underscore_token.to_tokens(tokens);
3002 }
3003 }
3004
Michael Layzell734adb42017-06-07 16:58:31 -04003005 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003006 impl ToTokens for PatIdent {
3007 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003008 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003009 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003010 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003011 if self.subpat.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003012 TokensOrDefault(&self.at_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003013 self.subpat.to_tokens(tokens);
3014 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003015 }
3016 }
3017
Michael Layzell734adb42017-06-07 16:58:31 -04003018 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003019 impl ToTokens for PatStruct {
3020 fn to_tokens(&self, tokens: &mut Tokens) {
3021 self.path.to_tokens(tokens);
3022 self.brace_token.surround(tokens, |tokens| {
3023 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003024 // NOTE: We need a comma before the dot2 token if it is present.
3025 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003026 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003027 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003028 self.dot2_token.to_tokens(tokens);
3029 });
3030 }
3031 }
3032
Michael Layzell734adb42017-06-07 16:58:31 -04003033 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 impl ToTokens for PatTupleStruct {
3035 fn to_tokens(&self, tokens: &mut Tokens) {
3036 self.path.to_tokens(tokens);
3037 self.pat.to_tokens(tokens);
3038 }
3039 }
3040
Michael Layzell734adb42017-06-07 16:58:31 -04003041 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003042 impl ToTokens for PatPath {
3043 fn to_tokens(&self, tokens: &mut Tokens) {
3044 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3045 }
3046 }
3047
Michael Layzell734adb42017-06-07 16:58:31 -04003048 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003049 impl ToTokens for PatTuple {
3050 fn to_tokens(&self, tokens: &mut Tokens) {
3051 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003052 self.front.to_tokens(tokens);
3053 if let Some(ref dot2_token) = self.dot2_token {
3054 if !self.front.empty_or_trailing() {
3055 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003056 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003057 }
David Tolnay41871922017-12-29 01:53:45 -05003058 dot2_token.to_tokens(tokens);
3059 self.comma_token.to_tokens(tokens);
3060 if self.comma_token.is_none() && !self.back.is_empty() {
3061 // Ensure there is a comma after the .. token.
3062 <Token![,]>::default().to_tokens(tokens);
3063 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003064 }
David Tolnay41871922017-12-29 01:53:45 -05003065 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003066 });
3067 }
3068 }
3069
Michael Layzell734adb42017-06-07 16:58:31 -04003070 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003071 impl ToTokens for PatBox {
3072 fn to_tokens(&self, tokens: &mut Tokens) {
3073 self.box_token.to_tokens(tokens);
3074 self.pat.to_tokens(tokens);
3075 }
3076 }
3077
Michael Layzell734adb42017-06-07 16:58:31 -04003078 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003079 impl ToTokens for PatRef {
3080 fn to_tokens(&self, tokens: &mut Tokens) {
3081 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003082 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003083 self.pat.to_tokens(tokens);
3084 }
3085 }
3086
Michael Layzell734adb42017-06-07 16:58:31 -04003087 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003088 impl ToTokens for PatLit {
3089 fn to_tokens(&self, tokens: &mut Tokens) {
3090 self.expr.to_tokens(tokens);
3091 }
3092 }
3093
Michael Layzell734adb42017-06-07 16:58:31 -04003094 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003095 impl ToTokens for PatRange {
3096 fn to_tokens(&self, tokens: &mut Tokens) {
3097 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003098 match self.limits {
3099 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3100 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3101 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003102 self.hi.to_tokens(tokens);
3103 }
3104 }
3105
Michael Layzell734adb42017-06-07 16:58:31 -04003106 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003107 impl ToTokens for PatSlice {
3108 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003109 // XXX: This is a mess, and it will be so easy to screw it up. How
3110 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003111 self.bracket_token.surround(tokens, |tokens| {
3112 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003113
3114 // If we need a comma before the middle or standalone .. token,
3115 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003116 if !self.front.empty_or_trailing()
3117 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003118 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003119 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003120 }
3121
3122 // If we have an identifier, we always need a .. token.
3123 if self.middle.is_some() {
3124 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003125 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003126 } else if self.dot2_token.is_some() {
3127 self.dot2_token.to_tokens(tokens);
3128 }
3129
3130 // Make sure we have a comma before the back half.
3131 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003132 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003133 self.back.to_tokens(tokens);
3134 } else {
3135 self.comma_token.to_tokens(tokens);
3136 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003137 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003138 }
3139 }
3140
Michael Layzell734adb42017-06-07 16:58:31 -04003141 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003142 impl ToTokens for PatMacro {
3143 fn to_tokens(&self, tokens: &mut Tokens) {
3144 self.mac.to_tokens(tokens);
3145 }
3146 }
3147
3148 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003149 impl ToTokens for PatVerbatim {
3150 fn to_tokens(&self, tokens: &mut Tokens) {
3151 self.tts.to_tokens(tokens);
3152 }
3153 }
3154
3155 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003156 impl ToTokens for FieldPat {
3157 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003158 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003159 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003160 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003161 }
3162 self.pat.to_tokens(tokens);
3163 }
3164 }
3165
Michael Layzell734adb42017-06-07 16:58:31 -04003166 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003167 impl ToTokens for Block {
3168 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003169 self.brace_token.surround(tokens, |tokens| {
3170 tokens.append_all(&self.stmts);
3171 });
David Tolnay42602292016-10-01 22:25:45 -07003172 }
3173 }
3174
Michael Layzell734adb42017-06-07 16:58:31 -04003175 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003176 impl ToTokens for Stmt {
3177 fn to_tokens(&self, tokens: &mut Tokens) {
3178 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003179 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003180 Stmt::Item(ref item) => item.to_tokens(tokens),
3181 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003182 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003183 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003184 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003185 }
David Tolnay42602292016-10-01 22:25:45 -07003186 }
3187 }
3188 }
David Tolnay191e0582016-10-02 18:31:09 -07003189
Michael Layzell734adb42017-06-07 16:58:31 -04003190 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003191 impl ToTokens for Local {
3192 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003193 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003194 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003195 self.pat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003196 if self.ty.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003197 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003198 self.ty.to_tokens(tokens);
3199 }
3200 if self.init.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003201 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003202 self.init.to_tokens(tokens);
3203 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003204 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003205 }
3206 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003207}