blob: b0192314c98f7ae99d76d23c501a0e22c3c12a5e [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayf4bbbd92016-09-23 14:41:55 -07009use super::*;
David Tolnayf2cfd722017-12-31 18:02:51 -050010use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -050011use proc_macro2::{Span, TokenStream};
David Tolnay14982012017-12-29 00:49:51 -050012#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -050013use std::hash::{Hash, Hasher};
David Tolnay2ae520a2017-12-29 11:19:50 -050014#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050015use tt::TokenStreamHelper;
David Tolnay2ae520a2017-12-29 11:19:50 -050016#[cfg(feature = "full")]
17use std::mem;
David Tolnayf4bbbd92016-09-23 14:41:55 -070018
Alex Crichton62a0a592017-05-22 13:58:53 -070019ast_enum_of_structs! {
David Tolnay8c91b882017-12-28 23:04:32 -050020 /// An expression.
21 pub enum Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -070022 /// A `box x` expression.
Michael Layzell734adb42017-06-07 16:58:31 -040023 pub Box(ExprBox #full {
David Tolnay8c91b882017-12-28 23:04:32 -050024 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080025 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -050026 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -070027 }),
Clar Charrd22b5702017-03-10 15:24:56 -050028
David Tolnay8701a5c2017-12-28 23:31:10 -050029 /// E.g. 'place <- value'.
Michael Layzell734adb42017-06-07 16:58:31 -040030 pub InPlace(ExprInPlace #full {
David Tolnay8c91b882017-12-28 23:04:32 -050031 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070032 pub place: Box<Expr>,
David Tolnay8701a5c2017-12-28 23:31:10 -050033 pub arrow_token: Token![<-],
Alex Crichton62a0a592017-05-22 13:58:53 -070034 pub value: Box<Expr>,
35 }),
Clar Charrd22b5702017-03-10 15:24:56 -050036
Alex Crichton62a0a592017-05-22 13:58:53 -070037 /// An array, e.g. `[a, b, c, d]`.
Michael Layzell734adb42017-06-07 16:58:31 -040038 pub Array(ExprArray #full {
David Tolnay8c91b882017-12-28 23:04:32 -050039 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050040 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -050041 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070042 }),
Clar Charrd22b5702017-03-10 15:24:56 -050043
Alex Crichton62a0a592017-05-22 13:58:53 -070044 /// A function call.
45 pub Call(ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -050046 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070047 pub func: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -050048 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050049 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070050 }),
Clar Charrd22b5702017-03-10 15:24:56 -050051
Alex Crichton62a0a592017-05-22 13:58:53 -070052 /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`)
53 ///
54 /// The `Ident` is the identifier for the method name.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080055 /// The vector of `Type`s are the ascripted type parameters for the method
Alex Crichton62a0a592017-05-22 13:58:53 -070056 /// (within the angle brackets).
Michael Layzell734adb42017-06-07 16:58:31 -040057 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050058 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -050059 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080060 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050061 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -050062 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050063 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050064 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070065 }),
Clar Charrd22b5702017-03-10 15:24:56 -050066
Alex Crichton62a0a592017-05-22 13:58:53 -070067 /// A tuple, e.g. `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050068 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050069 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050070 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050071 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070072 }),
Clar Charrd22b5702017-03-10 15:24:56 -050073
Alex Crichton62a0a592017-05-22 13:58:53 -070074 /// A binary operation, e.g. `a + b`, `a * b`.
75 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050076 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070077 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050078 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070079 pub right: Box<Expr>,
80 }),
Clar Charrd22b5702017-03-10 15:24:56 -050081
Alex Crichton62a0a592017-05-22 13:58:53 -070082 /// A unary operation, e.g. `!x`, `*x`.
83 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050084 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070085 pub op: UnOp,
86 pub expr: Box<Expr>,
87 }),
Clar Charrd22b5702017-03-10 15:24:56 -050088
Alex Crichton62a0a592017-05-22 13:58:53 -070089 /// A literal, e.g. `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050090 pub Lit(ExprLit {
91 pub attrs: Vec<Attribute>,
92 pub lit: Lit,
93 }),
Clar Charrd22b5702017-03-10 15:24:56 -050094
Alex Crichton62a0a592017-05-22 13:58:53 -070095 /// A cast, e.g. `foo as f64`.
96 pub Cast(ExprCast {
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 as_token: Token![as],
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 /// A type ascription, e.g. `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -0500104 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500105 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700106 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800107 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800108 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500110
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 /// An `if` block, with an optional else block
112 ///
113 /// E.g., `if expr { block } else { expr }`
Michael Layzell734adb42017-06-07 16:58:31 -0400114 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500115 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500116 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700117 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500118 pub then_branch: Block,
119 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700120 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500121
Alex Crichton62a0a592017-05-22 13:58:53 -0700122 /// An `if let` expression with an optional else block
123 ///
124 /// E.g., `if let pat = expr { block } else { expr }`
125 ///
126 /// This is desugared to a `match` expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400127 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500128 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800129 pub if_token: Token![if],
130 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500131 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800132 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500133 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500134 pub then_branch: Block,
135 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700136 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500137
Alex Crichton62a0a592017-05-22 13:58:53 -0700138 /// A while loop, with an optional label
139 ///
140 /// E.g., `'label: while expr { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400141 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500142 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500143 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800144 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500145 pub cond: Box<Expr>,
146 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700147 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500148
Alex Crichton62a0a592017-05-22 13:58:53 -0700149 /// A while-let loop, with an optional label.
150 ///
151 /// E.g., `'label: while let pat = expr { block }`
152 ///
153 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400154 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500155 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500156 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800157 pub while_token: Token![while],
158 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500159 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800160 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500161 pub expr: Box<Expr>,
162 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700163 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500164
Alex Crichton62a0a592017-05-22 13:58:53 -0700165 /// A for loop, with an optional label.
166 ///
167 /// E.g., `'label: for pat in expr { block }`
168 ///
169 /// This is desugared to a combination of `loop` and `match` expressions.
Michael Layzell734adb42017-06-07 16:58:31 -0400170 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500171 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500172 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500173 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500175 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700176 pub expr: Box<Expr>,
177 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700178 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500179
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 /// Conditionless loop with an optional label.
181 ///
182 /// E.g. `'label: loop { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400183 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500184 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500185 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500186 pub loop_token: Token![loop],
187 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700188 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500189
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 /// A `match` block.
Michael Layzell734adb42017-06-07 16:58:31 -0400191 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500192 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800193 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500195 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 pub arms: Vec<Arm>,
197 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500198
Alex Crichton62a0a592017-05-22 13:58:53 -0700199 /// A closure (for example, `move |a, b, c| a + b + c`)
Michael Layzell734adb42017-06-07 16:58:31 -0400200 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500201 pub attrs: Vec<Attribute>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500202 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800203 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500204 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500206 pub output: ReturnType,
207 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700208 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500209
Nika Layzell640832a2017-12-04 13:37:09 -0500210 /// An unsafe block (`unsafe { ... }`)
211 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500212 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500213 pub unsafe_token: Token![unsafe],
214 pub block: Block,
215 }),
216
217 /// A block (`{ ... }`)
Michael Layzell734adb42017-06-07 16:58:31 -0400218 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500219 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700220 pub block: Block,
221 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700222
Alex Crichton62a0a592017-05-22 13:58:53 -0700223 /// An assignment (`a = foo()`)
Michael Layzell734adb42017-06-07 16:58:31 -0400224 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500225 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700226 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800227 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500228 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700229 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500230
Alex Crichton62a0a592017-05-22 13:58:53 -0700231 /// An assignment with an operator
232 ///
233 /// For example, `a += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400234 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500235 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500237 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700238 pub right: Box<Expr>,
239 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500240
David Tolnay85b69a42017-12-27 20:43:10 -0500241 /// Access of a named struct field (`obj.foo`) or unnamed tuple struct
242 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400243 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500244 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500245 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800246 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500247 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500249
Alex Crichton62a0a592017-05-22 13:58:53 -0700250 /// An indexing operation (`foo[2]`)
251 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500252 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700253 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500254 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500255 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500257
David Tolnaybe55d7b2017-12-17 23:41:20 -0800258 /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`)
Michael Layzell734adb42017-06-07 16:58:31 -0400259 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500260 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700261 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700262 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500263 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700265
Alex Crichton62a0a592017-05-22 13:58:53 -0700266 /// Variable reference, possibly containing `::` and/or type
267 /// parameters, e.g. foo::bar::<baz>.
268 ///
269 /// Optionally "qualified",
270 /// E.g. `<Vec<T> as SomeTrait>::SomeType`.
271 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500272 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700273 pub qself: Option<QSelf>,
274 pub path: Path,
275 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700276
Alex Crichton62a0a592017-05-22 13:58:53 -0700277 /// A referencing operation (`&a` or `&mut a`)
Michael Layzell734adb42017-06-07 16:58:31 -0400278 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500279 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800280 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500281 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 pub expr: Box<Expr>,
283 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500284
Alex Crichton62a0a592017-05-22 13:58:53 -0700285 /// A `break`, with an optional label to break, and an optional expression
Michael Layzell734adb42017-06-07 16:58:31 -0400286 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500287 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500288 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700289 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700290 pub expr: Option<Box<Expr>>,
291 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500292
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 /// A `continue`, with an optional label
Michael Layzell734adb42017-06-07 16:58:31 -0400294 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500295 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800296 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500297 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500299
Alex Crichton62a0a592017-05-22 13:58:53 -0700300 /// A `return`, with an optional value to be returned
David Tolnayc246cd32017-12-28 23:14:32 -0500301 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500302 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800303 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500304 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700305 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700306
Alex Crichton62a0a592017-05-22 13:58:53 -0700307 /// A macro invocation; pre-expansion
David Tolnay8c91b882017-12-28 23:04:32 -0500308 pub Macro(ExprMacro #full {
309 pub attrs: Vec<Attribute>,
310 pub mac: Macro,
311 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700312
Alex Crichton62a0a592017-05-22 13:58:53 -0700313 /// A struct literal expression.
314 ///
315 /// For example, `Foo {x: 1, y: 2}`, or
316 /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`.
Michael Layzell734adb42017-06-07 16:58:31 -0400317 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500318 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700319 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500320 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500321 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500322 pub dot2_token: Option<Token![..]>,
323 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700325
Alex Crichton62a0a592017-05-22 13:58:53 -0700326 /// An array literal constructed from one repeated element.
327 ///
328 /// For example, `[1; 5]`. The first expression is the element
329 /// to be repeated; the second is the number of times to repeat it.
Michael Layzell734adb42017-06-07 16:58:31 -0400330 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500331 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500332 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700333 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500334 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700335 pub amt: Box<Expr>,
336 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700337
Alex Crichton62a0a592017-05-22 13:58:53 -0700338 /// No-op: used solely so we can pretty-print faithfully
David Tolnaye98775f2017-12-28 23:17:00 -0500339 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500340 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500341 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500342 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700343 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700344
Michael Layzell93c36282017-06-04 20:43:14 -0400345 /// No-op: used solely so we can pretty-print faithfully
346 ///
347 /// A `group` represents a `None`-delimited span in the input
348 /// `TokenStream` which affects the precidence of the resulting
349 /// expression. They are used for macro hygiene.
David Tolnaye98775f2017-12-28 23:17:00 -0500350 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500351 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500352 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500353 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400354 }),
355
Alex Crichton62a0a592017-05-22 13:58:53 -0700356 /// `expr?`
Michael Layzell734adb42017-06-07 16:58:31 -0400357 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500358 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700359 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800360 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700361 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700362
Alex Crichton62a0a592017-05-22 13:58:53 -0700363 /// A catch expression.
364 ///
365 /// E.g. `do catch { block }`
Michael Layzell734adb42017-06-07 16:58:31 -0400366 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500367 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800368 pub do_token: Token![do],
369 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700370 pub block: Block,
371 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700372
373 /// A yield expression.
374 ///
375 /// E.g. `yield expr`
376 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500377 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800378 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700379 pub expr: Option<Box<Expr>>,
380 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500381
382 pub Verbatim(ExprVerbatim #manual_extra_traits {
383 pub tts: TokenStream,
384 }),
385 }
386}
387
388#[cfg(feature = "extra-traits")]
389impl Eq for ExprVerbatim {}
390
391#[cfg(feature = "extra-traits")]
392impl PartialEq for ExprVerbatim {
393 fn eq(&self, other: &Self) -> bool {
394 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
395 }
396}
397
398#[cfg(feature = "extra-traits")]
399impl Hash for ExprVerbatim {
400 fn hash<H>(&self, state: &mut H)
401 where
402 H: Hasher,
403 {
404 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700406}
407
David Tolnay8c91b882017-12-28 23:04:32 -0500408impl Expr {
409 // Not public API.
410 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500411 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500412 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500413 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800414 Expr::Box(ExprBox { ref mut attrs, .. })
415 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
416 | Expr::Array(ExprArray { ref mut attrs, .. })
417 | Expr::Call(ExprCall { ref mut attrs, .. })
418 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
419 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
420 | Expr::Binary(ExprBinary { ref mut attrs, .. })
421 | Expr::Unary(ExprUnary { ref mut attrs, .. })
422 | Expr::Lit(ExprLit { ref mut attrs, .. })
423 | Expr::Cast(ExprCast { ref mut attrs, .. })
424 | Expr::Type(ExprType { ref mut attrs, .. })
425 | Expr::If(ExprIf { ref mut attrs, .. })
426 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
427 | Expr::While(ExprWhile { ref mut attrs, .. })
428 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
429 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
430 | Expr::Loop(ExprLoop { ref mut attrs, .. })
431 | Expr::Match(ExprMatch { ref mut attrs, .. })
432 | Expr::Closure(ExprClosure { ref mut attrs, .. })
433 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
434 | Expr::Block(ExprBlock { ref mut attrs, .. })
435 | Expr::Assign(ExprAssign { ref mut attrs, .. })
436 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
437 | Expr::Field(ExprField { ref mut attrs, .. })
438 | Expr::Index(ExprIndex { ref mut attrs, .. })
439 | Expr::Range(ExprRange { ref mut attrs, .. })
440 | Expr::Path(ExprPath { ref mut attrs, .. })
441 | Expr::AddrOf(ExprAddrOf { ref mut attrs, .. })
442 | Expr::Break(ExprBreak { ref mut attrs, .. })
443 | Expr::Continue(ExprContinue { ref mut attrs, .. })
444 | Expr::Return(ExprReturn { ref mut attrs, .. })
445 | Expr::Macro(ExprMacro { ref mut attrs, .. })
446 | Expr::Struct(ExprStruct { ref mut attrs, .. })
447 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
448 | Expr::Paren(ExprParen { ref mut attrs, .. })
449 | Expr::Group(ExprGroup { ref mut attrs, .. })
450 | Expr::Try(ExprTry { ref mut attrs, .. })
451 | Expr::Catch(ExprCatch { ref mut attrs, .. })
452 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500453 Expr::Verbatim(_) => {
454 // TODO
455 Vec::new()
456 }
David Tolnay8c91b882017-12-28 23:04:32 -0500457 }
458 }
459}
460
David Tolnay85b69a42017-12-27 20:43:10 -0500461ast_enum! {
462 /// A struct or tuple struct field accessed in a struct literal or field
463 /// expression.
464 pub enum Member {
465 /// A named field like `self.x`.
466 Named(Ident),
467 /// An unnamed field like `self.0`.
468 Unnamed(Index),
469 }
470}
471
David Tolnay85b69a42017-12-27 20:43:10 -0500472ast_struct! {
473 /// The index of an unnamed tuple struct field.
474 pub struct Index #manual_extra_traits {
475 pub index: u32,
476 pub span: Span,
477 }
478}
479
David Tolnay14982012017-12-29 00:49:51 -0500480impl From<usize> for Index {
481 fn from(index: usize) -> Index {
482 assert!(index < std::u32::MAX as usize);
483 Index {
484 index: index as u32,
485 span: Span::default(),
486 }
487 }
488}
489
490#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500491impl Eq for Index {}
492
David Tolnay14982012017-12-29 00:49:51 -0500493#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500494impl PartialEq for Index {
495 fn eq(&self, other: &Self) -> bool {
496 self.index == other.index
497 }
498}
499
David Tolnay14982012017-12-29 00:49:51 -0500500#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500501impl Hash for Index {
502 fn hash<H: Hasher>(&self, state: &mut H) {
503 self.index.hash(state);
504 }
505}
506
507#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700508ast_struct! {
David Tolnayd60cfec2017-12-29 00:21:38 -0500509 pub struct MethodTurbofish {
510 pub colon2_token: Token![::],
511 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500512 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500513 pub gt_token: Token![>],
514 }
515}
516
517#[cfg(feature = "full")]
518ast_enum! {
519 /// A individual generic argument like `T`.
520 pub enum GenericMethodArgument {
521 /// The type parameters for this path segment, if present.
522 Type(Type),
523 /// Const expression. Must be inside of a block.
524 ///
525 /// NOTE: Identity expressions are represented as Type arguments, as
526 /// they are indistinguishable syntactically.
527 Const(Expr),
528 }
529}
530
531#[cfg(feature = "full")]
532ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700533 /// A field-value pair in a struct literal.
534 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500535 /// Attributes tagged on the field.
536 pub attrs: Vec<Attribute>,
537
538 /// Name or index of the field.
539 pub member: Member,
540
David Tolnay5d7098a2017-12-29 01:35:24 -0500541 /// The colon in `Struct { x: x }`. If written in shorthand like
542 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500543 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500544
Alex Crichton62a0a592017-05-22 13:58:53 -0700545 /// Value of the field.
546 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 }
David Tolnay055a7042016-10-02 19:23:54 -0700548}
549
Michael Layzell734adb42017-06-07 16:58:31 -0400550#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700551ast_struct! {
David Tolnaybcd498f2017-12-29 12:02:33 -0500552 pub struct Label {
553 pub name: Lifetime,
554 pub colon_token: Token![:],
555 }
556}
557
558#[cfg(feature = "full")]
559ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700560 /// A Block (`{ .. }`).
561 ///
562 /// E.g. `{ .. }` as in `fn foo() { .. }`
563 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500564 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700565 /// Statements in a block
566 pub stmts: Vec<Stmt>,
567 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700568}
569
Michael Layzell734adb42017-06-07 16:58:31 -0400570#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700571ast_enum! {
572 /// A statement, usually ending in a semicolon.
573 pub enum Stmt {
574 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800575 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700576
Alex Crichton62a0a592017-05-22 13:58:53 -0700577 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800578 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700579
Alex Crichton62a0a592017-05-22 13:58:53 -0700580 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800581 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700582
Alex Crichton62a0a592017-05-22 13:58:53 -0700583 /// Expression with trailing semicolon;
David Tolnay1f0b7b82018-01-06 16:07:14 -0800584 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700585 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700586}
587
Michael Layzell734adb42017-06-07 16:58:31 -0400588#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700589ast_struct! {
590 /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
591 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500592 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800593 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700594 pub pat: Box<Pat>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500595 pub ty: Option<(Token![:], Box<Type>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700596 /// Initializer expression to set the value, if any
David Tolnay8b4d3022017-12-29 12:11:10 -0500597 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500598 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700599 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700600}
601
Michael Layzell734adb42017-06-07 16:58:31 -0400602#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700603ast_enum_of_structs! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700604 // Clippy false positive
605 // https://github.com/Manishearth/rust-clippy/issues/1241
606 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
607 pub enum Pat {
608 /// Represents a wildcard pattern (`_`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700609 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800610 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700611 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700612
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
614 /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
615 /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
616 /// during name resolution.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700617 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500618 pub by_ref: Option<Token![ref]>,
619 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700620 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500621 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700622 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700623
Alex Crichton62a0a592017-05-22 13:58:53 -0700624 /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
625 /// The `bool` is `true` in the presence of a `..`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700626 pub Struct(PatStruct {
627 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500628 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500629 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800630 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700631 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700632
Alex Crichton62a0a592017-05-22 13:58:53 -0700633 /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
634 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
635 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700636 pub TupleStruct(PatTupleStruct {
637 pub path: Path,
638 pub pat: PatTuple,
639 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700640
Alex Crichton62a0a592017-05-22 13:58:53 -0700641 /// A possibly qualified path pattern.
642 /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants
643 /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can
644 /// only legally refer to associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700645 pub Path(PatPath {
646 pub qself: Option<QSelf>,
647 pub path: Path,
648 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700649
Alex Crichton62a0a592017-05-22 13:58:53 -0700650 /// A tuple pattern `(a, b)`.
651 /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position.
652 /// 0 <= position <= subpats.len()
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700653 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500654 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500655 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500656 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500657 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500658 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700659 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700660 /// A `box` pattern
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700661 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800662 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500663 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700664 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700665 /// A reference pattern, e.g. `&mut (a, b)`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700666 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800667 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500668 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500669 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700670 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700671 /// A literal
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700672 pub Lit(PatLit {
673 pub expr: Box<Expr>,
674 }),
David Tolnaybe55d7b2017-12-17 23:41:20 -0800675 /// A range pattern, e.g. `1..=2`
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700676 pub Range(PatRange {
677 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700678 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500679 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700680 }),
Michael Layzell3936ceb2017-07-08 00:28:36 -0400681 /// `[a, b, i.., y, z]` is represented as:
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700682 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500683 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500684 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700685 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500686 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500687 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500688 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700689 }),
Alex Crichton62a0a592017-05-22 13:58:53 -0700690 /// A macro pattern; pre-expansion
David Tolnay323279a2017-12-29 11:26:32 -0500691 pub Macro(PatMacro {
692 pub mac: Macro,
693 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500694 pub Verbatim(PatVerbatim #manual_extra_traits {
695 pub tts: TokenStream,
696 }),
697 }
698}
699
David Tolnayc43b44e2017-12-30 23:55:54 -0500700#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500701impl Eq for PatVerbatim {}
702
David Tolnayc43b44e2017-12-30 23:55:54 -0500703#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500704impl PartialEq for PatVerbatim {
705 fn eq(&self, other: &Self) -> bool {
706 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
707 }
708}
709
David Tolnayc43b44e2017-12-30 23:55:54 -0500710#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500711impl Hash for PatVerbatim {
712 fn hash<H>(&self, state: &mut H)
713 where
714 H: Hasher,
715 {
716 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700717 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700718}
719
Michael Layzell734adb42017-06-07 16:58:31 -0400720#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700721ast_struct! {
722 /// An arm of a 'match'.
723 ///
David Tolnaybe55d7b2017-12-17 23:41:20 -0800724 /// E.g. `0..=10 => { println!("match!") }` as in
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500726 /// ```rust
727 /// # #![feature(dotdoteq_in_patterns)]
728 /// #
729 /// # fn main() {
730 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700731 /// match n {
David Tolnaybcf26022017-12-25 22:10:52 -0500732 /// 0..=10 => { println!("match!") }
Alex Crichton62a0a592017-05-22 13:58:53 -0700733 /// // ..
David Tolnaybcf26022017-12-25 22:10:52 -0500734 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700735 /// }
David Tolnaybcf26022017-12-25 22:10:52 -0500736 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700737 /// ```
738 pub struct Arm {
739 pub attrs: Vec<Attribute>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500740 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500741 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800742 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700743 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800744 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700745 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700746}
747
Michael Layzell734adb42017-06-07 16:58:31 -0400748#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700749ast_enum! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 /// Limit types of a range (inclusive or exclusive)
Alex Crichton2e0229c2017-05-23 09:34:50 -0700751 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700752 pub enum RangeLimits {
753 /// Inclusive at the beginning, exclusive at the end
David Tolnayf8db7ba2017-11-11 22:52:16 -0800754 HalfOpen(Token![..]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700755 /// Inclusive at the beginning and end
David Tolnaybe55d7b2017-12-17 23:41:20 -0800756 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700757 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700758}
759
Michael Layzell734adb42017-06-07 16:58:31 -0400760#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700761ast_struct! {
762 /// A single field in a struct pattern
763 ///
764 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
David Tolnay5d7098a2017-12-29 01:35:24 -0500765 /// are treated the same as `x: x, y: ref y, z: ref mut z` but
766 /// there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700767 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500768 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700769 /// The identifier for the field
David Tolnay85b69a42017-12-27 20:43:10 -0500770 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500771 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700772 /// The pattern the field is destructured to
773 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700774 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700775}
776
Michael Layzell3936ceb2017-07-08 00:28:36 -0400777#[cfg(any(feature = "parsing", feature = "printing"))]
778#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700779fn arm_expr_requires_comma(expr: &Expr) -> bool {
780 // see https://github.com/rust-lang/rust/blob/eb8f2586e
781 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500782 match *expr {
783 Expr::Unsafe(..)
784 | Expr::Block(..)
785 | Expr::If(..)
786 | Expr::IfLet(..)
787 | Expr::Match(..)
788 | Expr::While(..)
789 | Expr::WhileLet(..)
790 | Expr::Loop(..)
791 | Expr::ForLoop(..)
792 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700793 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400794 }
795}
796
David Tolnayb9c8e322016-09-23 20:48:37 -0700797#[cfg(feature = "parsing")]
798pub mod parsing {
799 use super::*;
David Tolnay056de302018-01-05 14:29:05 -0800800 use path::parsing::qpath;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500801 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -0800802 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700803
Michael Layzell734adb42017-06-07 16:58:31 -0400804 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -0800805 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500806 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -0800807 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400808 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500809 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500810 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700811
David Tolnaybcf26022017-12-25 22:10:52 -0500812 // When we're parsing expressions which occur before blocks, like in an if
813 // statement's condition, we cannot parse a struct literal.
814 //
815 // Struct literals are ambiguous in certain positions
816 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700817 macro_rules! ambiguous_expr {
818 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700819 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700820 };
821 }
822
David Tolnaybcf26022017-12-25 22:10:52 -0500823 // When we are parsing an optional suffix expression, we cannot allow blocks
824 // if structs are not allowed.
825 //
826 // Example:
827 //
828 // if break {} {}
829 //
830 // is ambiguous between:
831 //
832 // if (break {}) {}
833 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400834 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400835 macro_rules! opt_ambiguous_expr {
836 ($i:expr, $allow_struct:ident) => {
837 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
838 };
839 }
840
Alex Crichton954046c2017-05-30 21:49:42 -0700841 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400842 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700843
844 fn description() -> Option<&'static str> {
845 Some("expression")
846 }
847 }
848
Michael Layzell734adb42017-06-07 16:58:31 -0400849 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700850 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
851
David Tolnaybcf26022017-12-25 22:10:52 -0500852 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400853 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500854 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500855 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400856 }
857
Michael Layzell734adb42017-06-07 16:58:31 -0400858 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500859 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500860 // NOTE: We intentionally skip assign_expr, placement_expr, and
861 // range_expr, as they are not parsed in non-full mode.
862 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400863 }
864
David Tolnaybcf26022017-12-25 22:10:52 -0500865 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400866 macro_rules! binop {
867 (
868 $name: ident,
869 $next: ident,
870 $submac: ident!( $($args:tt)* )
871 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500872 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400873 mut e: call!($next, allow_struct, allow_block) >>
874 many0!(do_parse!(
875 op: $submac!($($args)*) >>
876 rhs: call!($next, allow_struct, true) >>
877 ({
878 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500879 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400880 left: Box::new(e.into()),
881 op: op,
882 right: Box::new(rhs.into()),
883 }.into();
884 })
885 )) >>
886 (e)
887 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700888 }
David Tolnay54e854d2016-10-24 12:03:30 -0700889 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700890
David Tolnaybcf26022017-12-25 22:10:52 -0500891 // <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 // <placement> <<= <placement> ..
901 // <placement> >>= <placement> ..
902 //
903 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400904 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500905 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400906 mut e: call!(placement_expr, allow_struct, allow_block) >>
907 alt!(
908 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800909 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400910 // Recurse into self to parse right-associative operator.
911 rhs: call!(assign_expr, allow_struct, true) >>
912 ({
913 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500914 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500915 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400916 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -0500917 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400918 }.into();
919 })
920 )
921 |
922 do_parse!(
923 op: call!(BinOp::parse_assign_op) >>
924 // Recurse into self to parse right-associative operator.
925 rhs: call!(assign_expr, allow_struct, true) >>
926 ({
927 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500928 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500929 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400930 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -0500931 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400932 }.into();
933 })
934 )
935 |
936 epsilon!()
937 ) >>
938 (e)
939 ));
940
David Tolnaybcf26022017-12-25 22:10:52 -0500941 // <range> <- <range> ..
942 //
943 // NOTE: The `in place { expr }` version of this syntax is parsed in
944 // `atom_expr`, not here.
945 //
946 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400947 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500948 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400949 mut e: call!(range_expr, allow_struct, allow_block) >>
950 alt!(
951 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800952 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400953 // Recurse into self to parse right-associative operator.
954 rhs: call!(placement_expr, allow_struct, true) >>
955 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400956 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500957 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400958 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -0500959 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -0500960 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -0500961 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400962 }.into();
963 })
964 )
965 |
966 epsilon!()
967 ) >>
968 (e)
969 ));
970
David Tolnaybcf26022017-12-25 22:10:52 -0500971 // <or> ... <or> ..
972 // <or> .. <or> ..
973 // <or> ..
974 //
975 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
976 // rules are for parsing these expressions are, but this is not correct.
977 // For example, `a .. b .. c` is not a legal expression. It should not
978 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
979 //
980 // NOTE: The form of ranges which don't include a preceding expression are
981 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400982 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500983 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400984 mut e: call!(or_expr, allow_struct, allow_block) >>
985 many0!(do_parse!(
986 limits: syn!(RangeLimits) >>
987 // We don't want to allow blocks here if we don't allow structs. See
988 // the reasoning for `opt_ambiguous_expr!` above.
989 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
990 ({
991 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500992 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500993 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400994 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -0500995 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400996 }.into();
997 })
998 )) >>
999 (e)
1000 ));
1001
David Tolnaybcf26022017-12-25 22:10:52 -05001002 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001003 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001004
David Tolnaybcf26022017-12-25 22:10:52 -05001005 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -08001006 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -04001007
David Tolnaybcf26022017-12-25 22:10:52 -05001008 // <bitor> == <bitor> ...
1009 // <bitor> != <bitor> ...
1010 // <bitor> >= <bitor> ...
1011 // <bitor> <= <bitor> ...
1012 // <bitor> > <bitor> ...
1013 // <bitor> < <bitor> ...
1014 //
1015 // NOTE: This operator appears to be parsed as left-associative, but errors
1016 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001017 binop!(
1018 compare_expr,
1019 bitor_expr,
1020 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001021 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001022 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001023 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001024 |
1025 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001026 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001027 |
1028 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001029 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001030 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001031 do_parse!(
1032 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001033 not!(punct!(<-)) >>
1034 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001035 (BinOp::Lt(t))
1036 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001037 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001038 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001039 )
1040 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001041
David Tolnaybcf26022017-12-25 22:10:52 -05001042 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001043 binop!(
1044 bitor_expr,
1045 bitxor_expr,
1046 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1047 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001048
David Tolnaybcf26022017-12-25 22:10:52 -05001049 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001050 binop!(
1051 bitxor_expr,
1052 bitand_expr,
1053 do_parse!(
1054 // NOTE: Make sure we aren't looking at ^=.
1055 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1056 )
1057 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001058
David Tolnaybcf26022017-12-25 22:10:52 -05001059 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001060 binop!(
1061 bitand_expr,
1062 shift_expr,
1063 do_parse!(
1064 // NOTE: Make sure we aren't looking at && or &=.
1065 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1066 )
1067 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001068
David Tolnaybcf26022017-12-25 22:10:52 -05001069 // <arith> << <arith> ...
1070 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001071 binop!(
1072 shift_expr,
1073 arith_expr,
1074 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001075 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001076 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001077 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001078 )
1079 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001080
David Tolnaybcf26022017-12-25 22:10:52 -05001081 // <term> + <term> ...
1082 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001083 binop!(
1084 arith_expr,
1085 term_expr,
1086 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001087 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001088 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001089 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001090 )
1091 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001092
David Tolnaybcf26022017-12-25 22:10:52 -05001093 // <cast> * <cast> ...
1094 // <cast> / <cast> ...
1095 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001096 binop!(
1097 term_expr,
1098 cast_expr,
1099 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001100 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001101 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001102 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001103 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001104 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001105 )
1106 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001107
David Tolnaybcf26022017-12-25 22:10:52 -05001108 // <unary> as <ty>
1109 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001110 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001111 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001112 mut e: call!(unary_expr, allow_struct, allow_block) >>
1113 many0!(alt!(
1114 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001115 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001116 // We can't accept `A + B` in cast expressions, as it's
1117 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001118 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001119 ({
1120 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001121 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001122 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001123 as_token: as_,
1124 ty: Box::new(ty),
1125 }.into();
1126 })
1127 )
1128 |
1129 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001130 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001131 // We can't accept `A + B` in cast expressions, as it's
1132 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001133 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001134 ({
1135 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001136 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001137 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001138 colon_token: colon,
1139 ty: Box::new(ty),
1140 }.into();
1141 })
1142 )
1143 )) >>
1144 (e)
1145 ));
1146
David Tolnay0cf94f22017-12-28 23:46:26 -05001147 // <unary> as <ty>
1148 #[cfg(not(feature = "full"))]
1149 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1150 mut e: call!(unary_expr, allow_struct, allow_block) >>
1151 many0!(do_parse!(
1152 as_: keyword!(as) >>
1153 // We can't accept `A + B` in cast expressions, as it's
1154 // ambiguous with the + expression.
1155 ty: call!(Type::without_plus) >>
1156 ({
1157 e = ExprCast {
1158 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001159 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001160 as_token: as_,
1161 ty: Box::new(ty),
1162 }.into();
1163 })
1164 )) >>
1165 (e)
1166 ));
1167
David Tolnaybcf26022017-12-25 22:10:52 -05001168 // <UnOp> <trailer>
1169 // & <trailer>
1170 // &mut <trailer>
1171 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001172 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001173 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001174 do_parse!(
1175 op: syn!(UnOp) >>
1176 expr: call!(unary_expr, allow_struct, true) >>
1177 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001178 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001179 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001180 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001181 }.into())
1182 )
1183 |
1184 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001185 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001186 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001187 expr: call!(unary_expr, allow_struct, true) >>
1188 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001189 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001190 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001191 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001192 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001193 }.into())
1194 )
1195 |
1196 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001197 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001198 expr: call!(unary_expr, allow_struct, true) >>
1199 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001200 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001201 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001202 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001203 }.into())
1204 )
1205 |
1206 call!(trailer_expr, allow_struct, allow_block)
1207 ));
1208
Michael Layzell734adb42017-06-07 16:58:31 -04001209 // XXX: This duplication is ugly
1210 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001211 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001212 do_parse!(
1213 op: syn!(UnOp) >>
1214 expr: call!(unary_expr, allow_struct, true) >>
1215 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001216 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001217 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001218 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001219 }.into())
1220 )
1221 |
1222 call!(trailer_expr, allow_struct, allow_block)
1223 ));
1224
David Tolnaybcf26022017-12-25 22:10:52 -05001225 // <atom> (..<args>) ...
1226 // <atom> . <ident> (..<args>) ...
1227 // <atom> . <ident> ...
1228 // <atom> . <lit> ...
1229 // <atom> [ <expr> ] ...
1230 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001231 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001232 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001233 mut e: call!(atom_expr, allow_struct, allow_block) >>
1234 many0!(alt!(
1235 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001236 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001237 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001238 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001239 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001240 args: args,
1241 paren_token: paren,
1242 }.into();
1243 })
1244 |
1245 tap!(more: and_method_call => {
1246 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001247 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001248 e = call.into();
1249 })
1250 |
1251 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001252 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001253 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001254 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001255 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001257 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001258 }.into();
1259 })
1260 |
1261 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001262 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001263 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001264 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001265 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001266 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001267 index: Box::new(i),
1268 }.into();
1269 })
1270 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001271 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001272 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001273 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001274 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001275 question_token: question,
1276 }.into();
1277 })
1278 )) >>
1279 (e)
1280 ));
1281
Michael Layzell734adb42017-06-07 16:58:31 -04001282 // XXX: Duplication == ugly
1283 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001284 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001285 mut e: call!(atom_expr, allow_struct, allow_block) >>
1286 many0!(alt!(
1287 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001288 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001289 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001290 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001291 paren_token: args.0,
1292 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001293 }.into();
1294 })
1295 |
1296 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001297 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001298 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001299 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001300 bracket_token: i.0,
1301 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001302 }.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 Tolnaydc03aec2017-12-30 01:54:18 -05001317 cond_reduce!(allow_struct, 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 Tolnaydc03aec2017-12-30 01:54:18 -05001355 cond_reduce!(allow_block, 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(),
David Tolnay8875fca2017-12-31 13:52:37 -05001424 expr: Box::new(e.1),
1425 group_token: e.0,
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(),
David Tolnay8875fca2017-12-31 13:52:37 -05001436 paren_token: e.0,
1437 expr: Box::new(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!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001445 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001446 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001447 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001448 bracket_token: elems.0,
1449 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001450 })
1451 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001452
1453 fn description() -> Option<&'static str> {
1454 Some("array")
1455 }
Alex Crichton954046c2017-05-30 21:49:42 -07001456 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001457
David Tolnayf2cfd722017-12-31 18:02:51 -05001458 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1459 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001460
Michael Layzell734adb42017-06-07 16:58:31 -04001461 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001462 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001463 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001464 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001465 turbofish: option!(tuple!(
1466 punct!(::),
1467 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001468 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001469 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001470 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001471 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001472 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001473 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001474 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001475 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001476 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1477 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001478 })),
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 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001487 args: args.1,
1488 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001489 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!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001503 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001504 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001505 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001506 elems: elems.1,
1507 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001508 })
1509 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001510
1511 fn description() -> Option<&'static str> {
1512 Some("tuple")
1513 }
Alex Crichton954046c2017-05-30 21:49:42 -07001514 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001515
Michael Layzell734adb42017-06-07 16:58:31 -04001516 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001517 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001518 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001519 if_: keyword!(if) >>
1520 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001521 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001522 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001523 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001524 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001525 else_block: option!(else_block) >>
1526 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001527 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001528 pat: Box::new(pat),
1529 let_token: let_,
1530 eq_token: eq,
1531 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001532 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001533 brace_token: then_block.0,
1534 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001535 },
1536 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001537 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001538 })
1539 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001540
1541 fn description() -> Option<&'static str> {
1542 Some("`if let` expression")
1543 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001544 }
1545
Michael Layzell734adb42017-06-07 16:58:31 -04001546 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001547 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001548 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001549 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001550 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001551 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001552 else_block: option!(else_block) >>
1553 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001554 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001555 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001556 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001557 brace_token: then_block.0,
1558 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001559 },
1560 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001561 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001562 })
1563 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001564
1565 fn description() -> Option<&'static str> {
1566 Some("`if` expression")
1567 }
Alex Crichton954046c2017-05-30 21:49:42 -07001568 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001569
Michael Layzell734adb42017-06-07 16:58:31 -04001570 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001571 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001572 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001573 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001574 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001575 |
David Tolnay8c91b882017-12-28 23:04:32 -05001576 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001577 |
1578 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001579 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001580 (Expr::Block(ExprBlock {
1581 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001582 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001583 brace_token: else_block.0,
1584 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001585 },
1586 }))
David Tolnay939766a2016-09-23 23:48:12 -07001587 )
Alex Crichton954046c2017-05-30 21:49:42 -07001588 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001589 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001590 ));
1591
Michael Layzell734adb42017-06-07 16:58:31 -04001592 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001593 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001594 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001595 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001596 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001597 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001598 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001599 expr: expr_no_struct >>
1600 loop_block: syn!(Block) >>
1601 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001602 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001603 for_token: for_,
1604 in_token: in_,
1605 pat: Box::new(pat),
1606 expr: Box::new(expr),
1607 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001608 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001609 })
1610 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001611
1612 fn description() -> Option<&'static str> {
1613 Some("`for` loop")
1614 }
Alex Crichton954046c2017-05-30 21:49:42 -07001615 }
Gregory Katze5f35682016-09-27 14:20:55 -04001616
Michael Layzell734adb42017-06-07 16:58:31 -04001617 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001618 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001619 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001620 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001621 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001622 loop_block: syn!(Block) >>
1623 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001624 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001625 loop_token: loop_,
1626 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001627 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001628 })
1629 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001630
1631 fn description() -> Option<&'static str> {
1632 Some("`loop`")
1633 }
Alex Crichton954046c2017-05-30 21:49:42 -07001634 }
1635
Michael Layzell734adb42017-06-07 16:58:31 -04001636 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001637 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001638 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001639 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001640 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001641 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001642 (ExprMatch {
1643 attrs: Vec::new(),
1644 expr: Box::new(obj),
1645 match_token: match_,
1646 brace_token: res.0,
1647 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001648 })
1649 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001650
1651 fn description() -> Option<&'static str> {
1652 Some("`match` expression")
1653 }
Alex Crichton954046c2017-05-30 21:49:42 -07001654 }
David Tolnay1978c672016-10-27 22:05:52 -07001655
Michael Layzell734adb42017-06-07 16:58:31 -04001656 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001657 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001658 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001659 do_: keyword!(do) >>
1660 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001661 catch_block: syn!(Block) >>
1662 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001663 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001664 block: catch_block,
1665 do_token: do_,
1666 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001667 })
Michael Layzell92639a52017-06-01 00:07:44 -04001668 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001669
1670 fn description() -> Option<&'static str> {
1671 Some("`catch` expression")
1672 }
Alex Crichton954046c2017-05-30 21:49:42 -07001673 }
Arnavion02ef13f2017-04-25 00:54:31 -07001674
Michael Layzell734adb42017-06-07 16:58:31 -04001675 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001676 impl Synom for ExprYield {
1677 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001678 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001679 expr: option!(syn!(Expr)) >>
1680 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001681 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001682 yield_token: yield_,
1683 expr: expr.map(Box::new),
1684 })
1685 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001686
1687 fn description() -> Option<&'static str> {
1688 Some("`yield` expression")
1689 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001690 }
1691
1692 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001693 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001694 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001695 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001696 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001697 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1698 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001699 body: do_parse!(
1700 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001701 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1702 true => alt!(
1703 input_end!() => { |_| None }
1704 |
1705 punct!(,) => { Some }
1706 )
Alex Crichton03b30272017-08-28 09:35:24 -07001707 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001708 false => option!(punct!(,))
1709 ) >>
1710 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001711 ) >>
1712 (Arm {
1713 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001714 attrs: attrs,
1715 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001716 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001717 body: Box::new(body.0),
1718 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001719 })
1720 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001721
1722 fn description() -> Option<&'static str> {
1723 Some("`match` arm")
1724 }
Alex Crichton954046c2017-05-30 21:49:42 -07001725 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001726
Michael Layzell734adb42017-06-07 16:58:31 -04001727 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001728 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001729 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001730 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001731 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001732 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001733 ret_and_body: alt!(
1734 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001735 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001736 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001737 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001738 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001739 Expr::Block(ExprBlock {
1740 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001741 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001742 }))
David Tolnay89e05672016-10-02 14:39:42 -07001743 )
1744 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001745 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001746 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001747 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001748 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001749 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001750 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001751 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001752 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001753 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001754 body: Box::new(ret_and_body.1),
1755 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001756 ));
1757
Michael Layzell734adb42017-06-07 16:58:31 -04001758 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001759 named!(fn_arg -> FnArg, do_parse!(
1760 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001761 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001762 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001763 if let Some((colon, ty)) = ty {
1764 FnArg::Captured(ArgCaptured {
1765 pat: pat,
1766 colon_token: colon,
1767 ty: ty,
1768 })
1769 } else {
1770 FnArg::Inferred(pat)
1771 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001772 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001773 ));
1774
Michael Layzell734adb42017-06-07 16:58:31 -04001775 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001776 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001777 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001778 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001779 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001780 cond: expr_no_struct >>
1781 while_block: syn!(Block) >>
1782 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001783 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001784 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001785 cond: Box::new(cond),
1786 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001787 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001788 })
1789 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001790
1791 fn description() -> Option<&'static str> {
1792 Some("`while` expression")
1793 }
Alex Crichton954046c2017-05-30 21:49:42 -07001794 }
1795
Michael Layzell734adb42017-06-07 16:58:31 -04001796 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001797 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001798 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001799 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001800 while_: keyword!(while) >>
1801 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001802 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001803 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001804 value: expr_no_struct >>
1805 while_block: syn!(Block) >>
1806 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001807 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001808 eq_token: eq,
1809 let_token: let_,
1810 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001811 pat: Box::new(pat),
1812 expr: Box::new(value),
1813 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001814 label: label,
1815 })
1816 ));
1817 }
1818
1819 #[cfg(feature = "full")]
1820 impl Synom for Label {
1821 named!(parse -> Self, do_parse!(
1822 name: syn!(Lifetime) >>
1823 colon: punct!(:) >>
1824 (Label {
1825 name: name,
1826 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001827 })
1828 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001829
1830 fn description() -> Option<&'static str> {
1831 Some("`while let` expression")
1832 }
Alex Crichton954046c2017-05-30 21:49:42 -07001833 }
1834
Michael Layzell734adb42017-06-07 16:58:31 -04001835 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001836 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001837 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001838 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001839 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001840 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001841 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001842 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001843 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001844 })
1845 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001846
1847 fn description() -> Option<&'static str> {
1848 Some("`continue`")
1849 }
Alex Crichton954046c2017-05-30 21:49:42 -07001850 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001851
Michael Layzell734adb42017-06-07 16:58:31 -04001852 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001853 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001854 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001855 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001856 // We can't allow blocks after a `break` expression when we wouldn't
1857 // allow structs, as this expression is ambiguous.
1858 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001859 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001860 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001861 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001862 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001863 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001864 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001865 ));
1866
Michael Layzell734adb42017-06-07 16:58:31 -04001867 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001868 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001869 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001870 // NOTE: return is greedy and eats blocks after it even when in a
1871 // position where structs are not allowed, such as in if statement
1872 // conditions. For example:
1873 //
David Tolnaybcf26022017-12-25 22:10:52 -05001874 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001875 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001876 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001877 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001878 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001879 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001880 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001881 ));
1882
Michael Layzell734adb42017-06-07 16:58:31 -04001883 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001884 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001885 named!(parse -> Self, do_parse!(
1886 path: syn!(Path) >>
1887 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001888 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001889 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
1890 dots: punct!(..) >>
1891 base: syn!(Expr) >>
1892 (dots, base)
1893 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001894 (fields, base)
1895 )) >>
1896 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001897 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04001898 let (dots, rest) = match base.and_then(|b| b) {
1899 Some((dots, base)) => (Some(dots), Some(base)),
1900 None => (None, None),
1901 };
1902 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001903 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001904 brace_token: brace,
1905 path: path,
1906 fields: fields,
1907 dot2_token: dots,
1908 rest: rest.map(Box::new),
1909 }
1910 })
1911 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001912
1913 fn description() -> Option<&'static str> {
1914 Some("struct literal expression")
1915 }
Alex Crichton954046c2017-05-30 21:49:42 -07001916 }
1917
Michael Layzell734adb42017-06-07 16:58:31 -04001918 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001919 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001920 named!(parse -> Self, alt!(
1921 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001922 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001923 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001924 value: syn!(Expr) >>
1925 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001926 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001927 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001928 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001929 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001930 })
Michael Layzell92639a52017-06-01 00:07:44 -04001931 )
1932 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001933 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001934 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001935 expr: Expr::Path(ExprPath {
1936 attrs: Vec::new(),
1937 qself: None,
1938 path: name.into(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001939 }),
Michael Layzell92639a52017-06-01 00:07:44 -04001940 attrs: Vec::new(),
1941 colon_token: None,
1942 })
1943 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001944
1945 fn description() -> Option<&'static str> {
1946 Some("field-value pair: `field: value`")
1947 }
Alex Crichton954046c2017-05-30 21:49:42 -07001948 }
David Tolnay055a7042016-10-02 19:23:54 -07001949
Michael Layzell734adb42017-06-07 16:58:31 -04001950 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001951 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001952 named!(parse -> Self, do_parse!(
1953 data: brackets!(do_parse!(
1954 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001955 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001956 times: syn!(Expr) >>
1957 (value, semi, times)
1958 )) >>
1959 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001960 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001961 expr: Box::new((data.1).0),
1962 amt: Box::new((data.1).2),
1963 bracket_token: data.0,
1964 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001965 })
1966 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001967
1968 fn description() -> Option<&'static str> {
1969 Some("repeated array literal: `[val; N]`")
1970 }
Alex Crichton954046c2017-05-30 21:49:42 -07001971 }
David Tolnay055a7042016-10-02 19:23:54 -07001972
Michael Layzell734adb42017-06-07 16:58:31 -04001973 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001974 impl Synom for ExprUnsafe {
1975 named!(parse -> Self, do_parse!(
1976 unsafe_: keyword!(unsafe) >>
1977 b: syn!(Block) >>
1978 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001979 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001980 unsafe_token: unsafe_,
1981 block: b,
1982 })
1983 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001984
1985 fn description() -> Option<&'static str> {
1986 Some("unsafe block: `unsafe { .. }`")
1987 }
Nika Layzell640832a2017-12-04 13:37:09 -05001988 }
1989
1990 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001991 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001992 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001993 b: syn!(Block) >>
1994 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001995 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001996 block: b,
1997 })
1998 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001999
2000 fn description() -> Option<&'static str> {
2001 Some("block: `{ .. }`")
2002 }
Alex Crichton954046c2017-05-30 21:49:42 -07002003 }
David Tolnay89e05672016-10-02 14:39:42 -07002004
Michael Layzell734adb42017-06-07 16:58:31 -04002005 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002006 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002007 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002008 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002009 (ExprRange {
2010 attrs: Vec::new(),
2011 from: None,
2012 to: hi.map(Box::new),
2013 limits: limits,
2014 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002015 ));
2016
Michael Layzell734adb42017-06-07 16:58:31 -04002017 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002018 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002019 named!(parse -> Self, alt!(
2020 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002021 punct!(..=) => { RangeLimits::Closed }
2022 |
2023 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002024 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002025 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002026 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002027 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002028
2029 fn description() -> Option<&'static str> {
2030 Some("range limit: `..`, `...` or `..=`")
2031 }
Alex Crichton954046c2017-05-30 21:49:42 -07002032 }
David Tolnay438c9052016-10-07 23:24:48 -07002033
Alex Crichton954046c2017-05-30 21:49:42 -07002034 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002035 named!(parse -> Self, do_parse!(
2036 pair: qpath >>
2037 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002038 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002039 qself: pair.0,
2040 path: pair.1,
2041 })
2042 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002043
2044 fn description() -> Option<&'static str> {
2045 Some("path: `a::b::c`")
2046 }
Alex Crichton954046c2017-05-30 21:49:42 -07002047 }
David Tolnay42602292016-10-01 22:25:45 -07002048
Michael Layzell734adb42017-06-07 16:58:31 -04002049 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002050 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002051
David Tolnay8875fca2017-12-31 13:52:37 -05002052 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002053
Michael Layzell734adb42017-06-07 16:58:31 -04002054 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002055 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002056 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002057 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002058 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002059 brace_token: stmts.0,
2060 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002061 })
2062 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002063
2064 fn description() -> Option<&'static str> {
2065 Some("block: `{ .. }`")
2066 }
Alex Crichton954046c2017-05-30 21:49:42 -07002067 }
David Tolnay939766a2016-09-23 23:48:12 -07002068
Michael Layzell734adb42017-06-07 16:58:31 -04002069 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002070 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002071 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002072 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002073 mut standalone: many0!(do_parse!(
2074 stmt: syn!(Stmt) >>
2075 many0!(punct!(;)) >>
2076 (stmt)
2077 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002078 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002079 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002080 mut e: syn!(Expr) >>
2081 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002082 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002083 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002084 })
2085 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002086 (match last {
2087 None => standalone,
2088 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002089 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002090 standalone
2091 }
2092 })
2093 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002094 }
2095
Michael Layzell734adb42017-06-07 16:58:31 -04002096 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002097 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002098 named!(parse -> Self, alt!(
2099 stmt_mac
2100 |
2101 stmt_local
2102 |
2103 stmt_item
2104 |
Michael Layzell35418782017-06-07 09:20:25 -04002105 stmt_blockexpr
2106 |
Michael Layzell92639a52017-06-01 00:07:44 -04002107 stmt_expr
2108 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002109
2110 fn description() -> Option<&'static str> {
2111 Some("statement")
2112 }
Alex Crichton954046c2017-05-30 21:49:42 -07002113 }
David Tolnay939766a2016-09-23 23:48:12 -07002114
Michael Layzell734adb42017-06-07 16:58:31 -04002115 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002116 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002117 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002118 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002119 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002120 // Only parse braces here; paren and bracket will get parsed as
2121 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002122 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002123 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002124 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002125 attrs: attrs,
2126 ident: None,
2127 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002128 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002129 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002130 delimiter: MacroDelimiter::Brace(data.0),
2131 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002132 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002133 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002134 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002135 ));
2136
Michael Layzell734adb42017-06-07 16:58:31 -04002137 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002138 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002139 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002140 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002141 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002142 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002143 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2144 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002145 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002146 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002147 let_token: let_,
2148 pat: Box::new(pat),
2149 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2150 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2151 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002152 }))
David Tolnay191e0582016-10-02 18:31:09 -07002153 ));
2154
Michael Layzell734adb42017-06-07 16:58:31 -04002155 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002156 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002157
Michael Layzell734adb42017-06-07 16:58:31 -04002158 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002159 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002160 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002161 mut e: expr_nosemi >>
2162 // If the next token is a `.` or a `?` it is special-cased to parse as
2163 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002164 not!(punct!(.)) >>
2165 not!(punct!(?)) >>
2166 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002167 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002168 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002169 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002170 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002171 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002172 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002173 }
2174 })
2175 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002176
Michael Layzell734adb42017-06-07 16:58:31 -04002177 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002178 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002179 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002180 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002181 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002182 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002183 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002184 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002185 })
David Tolnay939766a2016-09-23 23:48:12 -07002186 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002187
Michael Layzell734adb42017-06-07 16:58:31 -04002188 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002189 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002190 named!(parse -> Self, alt!(
2191 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2192 |
2193 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2194 |
2195 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2196 |
2197 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2198 |
2199 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2200 |
David Tolnay323279a2017-12-29 11:26:32 -05002201 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002202 |
2203 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2204 |
2205 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2206 |
2207 syn!(PatPath) => { Pat::Path }
2208 |
2209 syn!(PatTuple) => { Pat::Tuple }
2210 |
2211 syn!(PatRef) => { Pat::Ref }
2212 |
2213 syn!(PatSlice) => { Pat::Slice }
2214 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002215
2216 fn description() -> Option<&'static str> {
2217 Some("pattern")
2218 }
Alex Crichton954046c2017-05-30 21:49:42 -07002219 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002220
Michael Layzell734adb42017-06-07 16:58:31 -04002221 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002222 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002223 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002224 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002225 |u| PatWild { underscore_token: u }
2226 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002227
2228 fn description() -> Option<&'static str> {
2229 Some("wild pattern: `_`")
2230 }
Alex Crichton954046c2017-05-30 21:49:42 -07002231 }
David Tolnay84aa0752016-10-02 23:01:13 -07002232
Michael Layzell734adb42017-06-07 16:58:31 -04002233 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002234 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002235 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002236 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002237 pat: syn!(Pat) >>
2238 (PatBox {
2239 pat: Box::new(pat),
2240 box_token: boxed,
2241 })
2242 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002243
2244 fn description() -> Option<&'static str> {
2245 Some("box pattern")
2246 }
Alex Crichton954046c2017-05-30 21:49:42 -07002247 }
2248
Michael Layzell734adb42017-06-07 16:58:31 -04002249 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002250 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002251 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002252 by_ref: option!(keyword!(ref)) >>
2253 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002254 name: alt!(
2255 syn!(Ident)
2256 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002257 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002258 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002259 not!(punct!(<)) >>
2260 not!(punct!(::)) >>
2261 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002262 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002263 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002264 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002265 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002266 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002267 })
2268 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002269
2270 fn description() -> Option<&'static str> {
2271 Some("pattern identifier binding")
2272 }
Alex Crichton954046c2017-05-30 21:49:42 -07002273 }
2274
Michael Layzell734adb42017-06-07 16:58:31 -04002275 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002276 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002277 named!(parse -> Self, do_parse!(
2278 path: syn!(Path) >>
2279 tuple: syn!(PatTuple) >>
2280 (PatTupleStruct {
2281 path: path,
2282 pat: tuple,
2283 })
2284 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002285
2286 fn description() -> Option<&'static str> {
2287 Some("tuple struct pattern")
2288 }
Alex Crichton954046c2017-05-30 21:49:42 -07002289 }
2290
Michael Layzell734adb42017-06-07 16:58:31 -04002291 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002292 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002293 named!(parse -> Self, do_parse!(
2294 path: syn!(Path) >>
2295 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002296 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002297 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002298 (fields, base)
2299 )) >>
2300 (PatStruct {
2301 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002302 fields: (data.1).0,
2303 brace_token: data.0,
2304 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002305 })
2306 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002307
2308 fn description() -> Option<&'static str> {
2309 Some("struct pattern")
2310 }
Alex Crichton954046c2017-05-30 21:49:42 -07002311 }
2312
Michael Layzell734adb42017-06-07 16:58:31 -04002313 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002314 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002315 named!(parse -> Self, alt!(
2316 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002317 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002318 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002319 pat: syn!(Pat) >>
2320 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002321 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002322 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002323 attrs: Vec::new(),
2324 colon_token: Some(colon),
2325 })
2326 )
2327 |
2328 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002329 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002330 by_ref: option!(keyword!(ref)) >>
2331 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002332 ident: syn!(Ident) >>
2333 ({
2334 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002335 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002336 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002337 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002338 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002339 }.into();
2340 if let Some(boxed) = boxed {
2341 pat = PatBox {
2342 pat: Box::new(pat),
2343 box_token: boxed,
2344 }.into();
2345 }
2346 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002347 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002348 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002349 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002350 colon_token: None,
2351 }
2352 })
2353 )
2354 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002355
2356 fn description() -> Option<&'static str> {
2357 Some("field pattern")
2358 }
Alex Crichton954046c2017-05-30 21:49:42 -07002359 }
2360
Michael Layzell734adb42017-06-07 16:58:31 -04002361 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002362 impl Synom for Member {
2363 named!(parse -> Self, alt!(
2364 syn!(Ident) => { Member::Named }
2365 |
2366 syn!(Index) => { Member::Unnamed }
2367 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002368
2369 fn description() -> Option<&'static str> {
2370 Some("field member")
2371 }
David Tolnay85b69a42017-12-27 20:43:10 -05002372 }
2373
2374 #[cfg(feature = "full")]
2375 impl Synom for Index {
2376 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002377 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002378 ({
David Tolnay360efd22018-01-04 23:35:26 -08002379 if let IntSuffix::None = lit.suffix() {
2380 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002381 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002382 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002383 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002384 })
David Tolnay85b69a42017-12-27 20:43:10 -05002385 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002386
2387 fn description() -> Option<&'static str> {
2388 Some("field index")
2389 }
David Tolnay85b69a42017-12-27 20:43:10 -05002390 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002391
Michael Layzell734adb42017-06-07 16:58:31 -04002392 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002393 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002394 named!(parse -> Self, map!(
2395 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002396 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002397 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002398
2399 fn description() -> Option<&'static str> {
2400 Some("path pattern")
2401 }
Alex Crichton954046c2017-05-30 21:49:42 -07002402 }
David Tolnay9636c052016-10-02 17:11:17 -07002403
Michael Layzell734adb42017-06-07 16:58:31 -04002404 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002405 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002406 named!(parse -> Self, do_parse!(
2407 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002408 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002409 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2410 tuple!(punct!(..), option!(punct!(,)))
2411 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002412 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002413 Some((_, Some(_))) => true,
2414 _ => false,
2415 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002416 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002417 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002418 )) >>
2419 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002420 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002421 let (dotdot, trailing) = match dotdot {
2422 Some((a, b)) => (Some(a), Some(b)),
2423 None => (None, None),
2424 };
2425 PatTuple {
2426 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002427 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002428 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002429 comma_token: trailing.unwrap_or_default(),
2430 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002431 }
2432 })
2433 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002434
2435 fn description() -> Option<&'static str> {
2436 Some("tuple pattern")
2437 }
Alex Crichton954046c2017-05-30 21:49:42 -07002438 }
David Tolnayfbb73232016-10-03 01:00:06 -07002439
Michael Layzell734adb42017-06-07 16:58:31 -04002440 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002441 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002442 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002443 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002444 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002445 pat: syn!(Pat) >>
2446 (PatRef {
2447 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002448 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002449 and_token: and,
2450 })
2451 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002452
2453 fn description() -> Option<&'static str> {
2454 Some("reference pattern")
2455 }
Alex Crichton954046c2017-05-30 21:49:42 -07002456 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002457
Michael Layzell734adb42017-06-07 16:58:31 -04002458 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002459 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002460 named!(parse -> Self, do_parse!(
2461 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002462 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002463 return parse_error(); // these need to be parsed by pat_path
2464 } else {
2465 PatLit {
2466 expr: Box::new(lit),
2467 }
2468 })
2469 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002470
2471 fn description() -> Option<&'static str> {
2472 Some("literal pattern")
2473 }
Alex Crichton954046c2017-05-30 21:49:42 -07002474 }
David Tolnaye1310902016-10-29 23:40:00 -07002475
Michael Layzell734adb42017-06-07 16:58:31 -04002476 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002477 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002478 named!(parse -> Self, do_parse!(
2479 lo: pat_lit_expr >>
2480 limits: syn!(RangeLimits) >>
2481 hi: pat_lit_expr >>
2482 (PatRange {
2483 lo: Box::new(lo),
2484 hi: Box::new(hi),
2485 limits: limits,
2486 })
2487 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002488
2489 fn description() -> Option<&'static str> {
2490 Some("range pattern")
2491 }
Alex Crichton954046c2017-05-30 21:49:42 -07002492 }
David Tolnaye1310902016-10-29 23:40:00 -07002493
Michael Layzell734adb42017-06-07 16:58:31 -04002494 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002495 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002496 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002497 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002498 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002499 |
David Tolnay8c91b882017-12-28 23:04:32 -05002500 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002501 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002502 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002503 Expr::Unary(ExprUnary {
2504 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002505 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002506 expr: Box::new(v)
2507 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002508 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002509 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002510 })
2511 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002512
Michael Layzell734adb42017-06-07 16:58:31 -04002513 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002514 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002515 named!(parse -> Self, map!(
2516 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002517 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002518 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002519 dots: punct!(..) >>
2520 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002521 (dots, trailing)
2522 )) >>
2523 after: cond!(
2524 match middle {
2525 Some((_, ref trailing)) => trailing.is_some(),
2526 _ => false,
2527 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002528 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002529 ) >>
2530 (before, middle, after)
2531 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002532 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002533 let mut before: Punctuated<Pat, Token![,]> = before;
2534 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002535 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002536 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002537 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002538 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002539 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002540 }),
2541 bracket_token: brackets,
2542 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002543 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002544 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002545 } else {
David Tolnay56080682018-01-06 14:01:52 -08002546 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002547 }
2548 }),
2549 front: before,
2550 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002551 }
Alex Crichton954046c2017-05-30 21:49:42 -07002552 }
Michael Layzell92639a52017-06-01 00:07:44 -04002553 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002554
2555 fn description() -> Option<&'static str> {
2556 Some("slice pattern")
2557 }
Alex Crichton954046c2017-05-30 21:49:42 -07002558 }
David Tolnay323279a2017-12-29 11:26:32 -05002559
2560 #[cfg(feature = "full")]
2561 impl Synom for PatMacro {
2562 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002563
2564 fn description() -> Option<&'static str> {
2565 Some("macro pattern")
2566 }
David Tolnay323279a2017-12-29 11:26:32 -05002567 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002568}
2569
David Tolnayf4bbbd92016-09-23 14:41:55 -07002570#[cfg(feature = "printing")]
2571mod printing {
2572 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002573 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002574 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002575 use quote::{ToTokens, Tokens};
David Tolnay61037c62018-01-05 16:21:03 -08002576 use proc_macro2::{Literal, TokenNode, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002577
David Tolnaybcf26022017-12-25 22:10:52 -05002578 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2579 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002580 #[cfg(feature = "full")]
2581 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002582 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002583 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002584 e.to_tokens(tokens);
2585 });
2586 } else {
2587 e.to_tokens(tokens);
2588 }
2589 }
2590
David Tolnay8c91b882017-12-28 23:04:32 -05002591 #[cfg(feature = "full")]
2592 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2593 tokens.append_all(attrs.outer());
2594 }
Michael Layzell734adb42017-06-07 16:58:31 -04002595
David Tolnay8c91b882017-12-28 23:04:32 -05002596 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002597 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002598
Michael Layzell734adb42017-06-07 16:58:31 -04002599 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002600 impl ToTokens for ExprBox {
2601 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002602 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002603 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002604 self.expr.to_tokens(tokens);
2605 }
2606 }
2607
Michael Layzell734adb42017-06-07 16:58:31 -04002608 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002609 impl ToTokens for ExprInPlace {
2610 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002611 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002612 self.place.to_tokens(tokens);
2613 self.arrow_token.to_tokens(tokens);
2614 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002615 }
2616 }
2617
Michael Layzell734adb42017-06-07 16:58:31 -04002618 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002619 impl ToTokens for ExprArray {
2620 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002621 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002622 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002623 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002624 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002625 }
2626 }
2627
2628 impl ToTokens for ExprCall {
2629 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002630 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002631 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002632 self.paren_token.surround(tokens, |tokens| {
2633 self.args.to_tokens(tokens);
2634 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002635 }
2636 }
2637
Michael Layzell734adb42017-06-07 16:58:31 -04002638 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002639 impl ToTokens for ExprMethodCall {
2640 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002641 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002642 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002643 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002644 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002645 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002646 self.paren_token.surround(tokens, |tokens| {
2647 self.args.to_tokens(tokens);
2648 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002649 }
2650 }
2651
Michael Layzell734adb42017-06-07 16:58:31 -04002652 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002653 impl ToTokens for MethodTurbofish {
2654 fn to_tokens(&self, tokens: &mut Tokens) {
2655 self.colon2_token.to_tokens(tokens);
2656 self.lt_token.to_tokens(tokens);
2657 self.args.to_tokens(tokens);
2658 self.gt_token.to_tokens(tokens);
2659 }
2660 }
2661
2662 #[cfg(feature = "full")]
2663 impl ToTokens for GenericMethodArgument {
2664 fn to_tokens(&self, tokens: &mut Tokens) {
2665 match *self {
2666 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2667 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2668 }
2669 }
2670 }
2671
2672 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002673 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002674 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002675 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002676 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002677 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002678 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002679 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002680 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002681 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002682 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002683 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002684 }
2685 }
2686
2687 impl ToTokens for ExprBinary {
2688 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002689 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002690 self.left.to_tokens(tokens);
2691 self.op.to_tokens(tokens);
2692 self.right.to_tokens(tokens);
2693 }
2694 }
2695
2696 impl ToTokens for ExprUnary {
2697 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002698 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002699 self.op.to_tokens(tokens);
2700 self.expr.to_tokens(tokens);
2701 }
2702 }
2703
David Tolnay8c91b882017-12-28 23:04:32 -05002704 impl ToTokens for ExprLit {
2705 fn to_tokens(&self, tokens: &mut Tokens) {
2706 attrs_to_tokens(&self.attrs, tokens);
2707 self.lit.to_tokens(tokens);
2708 }
2709 }
2710
Alex Crichton62a0a592017-05-22 13:58:53 -07002711 impl ToTokens for ExprCast {
2712 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002713 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002714 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002715 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002716 self.ty.to_tokens(tokens);
2717 }
2718 }
2719
David Tolnay0cf94f22017-12-28 23:46:26 -05002720 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002721 impl ToTokens for ExprType {
2722 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002723 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002724 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002725 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002726 self.ty.to_tokens(tokens);
2727 }
2728 }
2729
Michael Layzell734adb42017-06-07 16:58:31 -04002730 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002731 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002732 if let Some((ref else_token, ref else_)) = *else_ {
2733 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002734
2735 // If we are not one of the valid expressions to exist in an else
2736 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002737 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002738 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002739 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002740 }
2741 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002742 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002743 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002744 });
2745 }
2746 }
2747 }
2748 }
2749
2750 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002751 impl ToTokens for ExprIf {
2752 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002753 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002754 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002755 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002756 self.then_branch.to_tokens(tokens);
2757 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002758 }
2759 }
2760
Michael Layzell734adb42017-06-07 16:58:31 -04002761 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002762 impl ToTokens for ExprIfLet {
2763 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002764 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002765 self.if_token.to_tokens(tokens);
2766 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002767 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002768 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002769 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002770 self.then_branch.to_tokens(tokens);
2771 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002772 }
2773 }
2774
Michael Layzell734adb42017-06-07 16:58:31 -04002775 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002776 impl ToTokens for ExprWhile {
2777 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002778 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002779 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002780 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002781 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002782 self.body.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 ExprWhileLet {
2788 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002789 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002790 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002791 self.while_token.to_tokens(tokens);
2792 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002793 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002794 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002795 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002796 self.body.to_tokens(tokens);
2797 }
2798 }
2799
Michael Layzell734adb42017-06-07 16:58:31 -04002800 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002801 impl ToTokens for ExprForLoop {
2802 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002803 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002804 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002805 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002806 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002807 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002808 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002809 self.body.to_tokens(tokens);
2810 }
2811 }
2812
Michael Layzell734adb42017-06-07 16:58:31 -04002813 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002814 impl ToTokens for ExprLoop {
2815 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002816 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002817 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002818 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002819 self.body.to_tokens(tokens);
2820 }
2821 }
2822
Michael Layzell734adb42017-06-07 16:58:31 -04002823 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002824 impl ToTokens for ExprMatch {
2825 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002826 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002827 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002828 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002829 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002830 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002831 arm.to_tokens(tokens);
2832 // Ensure that we have a comma after a non-block arm, except
2833 // for the last one.
2834 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002835 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002836 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002837 }
2838 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002839 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002840 }
2841 }
2842
Michael Layzell734adb42017-06-07 16:58:31 -04002843 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002844 impl ToTokens for ExprCatch {
2845 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002846 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002847 self.do_token.to_tokens(tokens);
2848 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002849 self.block.to_tokens(tokens);
2850 }
2851 }
2852
Michael Layzell734adb42017-06-07 16:58:31 -04002853 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002854 impl ToTokens for ExprYield {
2855 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002856 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002857 self.yield_token.to_tokens(tokens);
2858 self.expr.to_tokens(tokens);
2859 }
2860 }
2861
2862 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002863 impl ToTokens for ExprClosure {
2864 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002865 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002866 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002867 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08002868 for input in self.inputs.pairs() {
2869 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05002870 FnArg::Captured(ArgCaptured {
2871 ref pat,
2872 ty: Type::Infer(_),
2873 ..
2874 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002875 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002876 }
David Tolnay56080682018-01-06 14:01:52 -08002877 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002878 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002879 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002880 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002881 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002882 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002883 self.body.to_tokens(tokens);
2884 }
2885 }
2886
Michael Layzell734adb42017-06-07 16:58:31 -04002887 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002888 impl ToTokens for ExprUnsafe {
2889 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002890 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002891 self.unsafe_token.to_tokens(tokens);
2892 self.block.to_tokens(tokens);
2893 }
2894 }
2895
2896 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002897 impl ToTokens for ExprBlock {
2898 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002899 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002900 self.block.to_tokens(tokens);
2901 }
2902 }
2903
Michael Layzell734adb42017-06-07 16:58:31 -04002904 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002905 impl ToTokens for ExprAssign {
2906 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002907 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002908 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002909 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002910 self.right.to_tokens(tokens);
2911 }
2912 }
2913
Michael Layzell734adb42017-06-07 16:58:31 -04002914 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002915 impl ToTokens for ExprAssignOp {
2916 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002917 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002919 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002920 self.right.to_tokens(tokens);
2921 }
2922 }
2923
Michael Layzell734adb42017-06-07 16:58:31 -04002924 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002925 impl ToTokens for ExprField {
2926 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002927 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002928 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002929 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002930 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002931 }
2932 }
2933
Michael Layzell734adb42017-06-07 16:58:31 -04002934 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002935 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002936 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002937 match *self {
2938 Member::Named(ident) => ident.to_tokens(tokens),
2939 Member::Unnamed(ref index) => index.to_tokens(tokens),
2940 }
2941 }
2942 }
2943
David Tolnay85b69a42017-12-27 20:43:10 -05002944 impl ToTokens for Index {
2945 fn to_tokens(&self, tokens: &mut Tokens) {
2946 tokens.append(TokenTree {
2947 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002948 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002949 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002950 }
2951 }
2952
2953 impl ToTokens for ExprIndex {
2954 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002955 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002956 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002957 self.bracket_token.surround(tokens, |tokens| {
2958 self.index.to_tokens(tokens);
2959 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002960 }
2961 }
2962
Michael Layzell734adb42017-06-07 16:58:31 -04002963 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002964 impl ToTokens for ExprRange {
2965 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002966 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002967 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002968 match self.limits {
2969 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2970 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2971 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002972 self.to.to_tokens(tokens);
2973 }
2974 }
2975
2976 impl ToTokens for ExprPath {
2977 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002978 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002979 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002980 }
2981 }
2982
Michael Layzell734adb42017-06-07 16:58:31 -04002983 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002984 impl ToTokens for ExprAddrOf {
2985 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002986 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002987 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002988 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002989 self.expr.to_tokens(tokens);
2990 }
2991 }
2992
Michael Layzell734adb42017-06-07 16:58:31 -04002993 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002994 impl ToTokens for ExprBreak {
2995 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002996 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002997 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002998 self.label.to_tokens(tokens);
2999 self.expr.to_tokens(tokens);
3000 }
3001 }
3002
Michael Layzell734adb42017-06-07 16:58:31 -04003003 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003004 impl ToTokens for ExprContinue {
3005 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003006 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003007 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003008 self.label.to_tokens(tokens);
3009 }
3010 }
3011
Michael Layzell734adb42017-06-07 16:58:31 -04003012 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003013 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003014 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003015 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003016 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003017 self.expr.to_tokens(tokens);
3018 }
3019 }
3020
Michael Layzell734adb42017-06-07 16:58:31 -04003021 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003022 impl ToTokens for ExprMacro {
3023 fn to_tokens(&self, tokens: &mut Tokens) {
3024 tokens.append_all(self.attrs.outer());
3025 self.mac.to_tokens(tokens);
3026 }
3027 }
3028
3029 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003030 impl ToTokens for ExprStruct {
3031 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003032 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003033 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003034 self.brace_token.surround(tokens, |tokens| {
3035 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003036 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003037 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003038 self.rest.to_tokens(tokens);
3039 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003040 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 }
3042 }
3043
Michael Layzell734adb42017-06-07 16:58:31 -04003044 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003045 impl ToTokens for ExprRepeat {
3046 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003047 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003048 self.bracket_token.surround(tokens, |tokens| {
3049 self.expr.to_tokens(tokens);
3050 self.semi_token.to_tokens(tokens);
3051 self.amt.to_tokens(tokens);
3052 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003053 }
3054 }
3055
David Tolnaye98775f2017-12-28 23:17:00 -05003056 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003057 impl ToTokens for ExprGroup {
3058 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003059 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003060 self.group_token.surround(tokens, |tokens| {
3061 self.expr.to_tokens(tokens);
3062 });
3063 }
3064 }
3065
David Tolnaye98775f2017-12-28 23:17:00 -05003066 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003067 impl ToTokens for ExprParen {
3068 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003069 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003070 self.paren_token.surround(tokens, |tokens| {
3071 self.expr.to_tokens(tokens);
3072 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003073 }
3074 }
3075
Michael Layzell734adb42017-06-07 16:58:31 -04003076 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003077 impl ToTokens for ExprTry {
3078 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003079 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003080 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003081 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003082 }
3083 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003084
David Tolnay2ae520a2017-12-29 11:19:50 -05003085 impl ToTokens for ExprVerbatim {
3086 fn to_tokens(&self, tokens: &mut Tokens) {
3087 self.tts.to_tokens(tokens);
3088 }
3089 }
3090
Michael Layzell734adb42017-06-07 16:58:31 -04003091 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003092 impl ToTokens for Label {
3093 fn to_tokens(&self, tokens: &mut Tokens) {
3094 self.name.to_tokens(tokens);
3095 self.colon_token.to_tokens(tokens);
3096 }
3097 }
3098
3099 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003100 impl ToTokens for FieldValue {
3101 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003102 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003103 if let Some(ref colon_token) = self.colon_token {
3104 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003105 self.expr.to_tokens(tokens);
3106 }
David Tolnay055a7042016-10-02 19:23:54 -07003107 }
3108 }
3109
Michael Layzell734adb42017-06-07 16:58:31 -04003110 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003111 impl ToTokens for Arm {
3112 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003113 tokens.append_all(&self.attrs);
3114 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003115 if let Some((ref if_token, ref guard)) = self.guard {
3116 if_token.to_tokens(tokens);
3117 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003118 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003119 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003120 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003121 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003122 }
3123 }
3124
Michael Layzell734adb42017-06-07 16:58:31 -04003125 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003126 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003127 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003128 self.underscore_token.to_tokens(tokens);
3129 }
3130 }
3131
Michael Layzell734adb42017-06-07 16:58:31 -04003132 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003133 impl ToTokens for PatIdent {
3134 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003135 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003136 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003137 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003138 if let Some((ref at_token, ref subpat)) = self.subpat {
3139 at_token.to_tokens(tokens);
3140 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003141 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003142 }
3143 }
3144
Michael Layzell734adb42017-06-07 16:58:31 -04003145 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003146 impl ToTokens for PatStruct {
3147 fn to_tokens(&self, tokens: &mut Tokens) {
3148 self.path.to_tokens(tokens);
3149 self.brace_token.surround(tokens, |tokens| {
3150 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003151 // NOTE: We need a comma before the dot2 token if it is present.
3152 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003153 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003154 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003155 self.dot2_token.to_tokens(tokens);
3156 });
3157 }
3158 }
3159
Michael Layzell734adb42017-06-07 16:58:31 -04003160 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003161 impl ToTokens for PatTupleStruct {
3162 fn to_tokens(&self, tokens: &mut Tokens) {
3163 self.path.to_tokens(tokens);
3164 self.pat.to_tokens(tokens);
3165 }
3166 }
3167
Michael Layzell734adb42017-06-07 16:58:31 -04003168 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003169 impl ToTokens for PatPath {
3170 fn to_tokens(&self, tokens: &mut Tokens) {
3171 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3172 }
3173 }
3174
Michael Layzell734adb42017-06-07 16:58:31 -04003175 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003176 impl ToTokens for PatTuple {
3177 fn to_tokens(&self, tokens: &mut Tokens) {
3178 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003179 self.front.to_tokens(tokens);
3180 if let Some(ref dot2_token) = self.dot2_token {
3181 if !self.front.empty_or_trailing() {
3182 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003183 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003184 }
David Tolnay41871922017-12-29 01:53:45 -05003185 dot2_token.to_tokens(tokens);
3186 self.comma_token.to_tokens(tokens);
3187 if self.comma_token.is_none() && !self.back.is_empty() {
3188 // Ensure there is a comma after the .. token.
3189 <Token![,]>::default().to_tokens(tokens);
3190 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003191 }
David Tolnay41871922017-12-29 01:53:45 -05003192 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003193 });
3194 }
3195 }
3196
Michael Layzell734adb42017-06-07 16:58:31 -04003197 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003198 impl ToTokens for PatBox {
3199 fn to_tokens(&self, tokens: &mut Tokens) {
3200 self.box_token.to_tokens(tokens);
3201 self.pat.to_tokens(tokens);
3202 }
3203 }
3204
Michael Layzell734adb42017-06-07 16:58:31 -04003205 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003206 impl ToTokens for PatRef {
3207 fn to_tokens(&self, tokens: &mut Tokens) {
3208 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003209 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003210 self.pat.to_tokens(tokens);
3211 }
3212 }
3213
Michael Layzell734adb42017-06-07 16:58:31 -04003214 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003215 impl ToTokens for PatLit {
3216 fn to_tokens(&self, tokens: &mut Tokens) {
3217 self.expr.to_tokens(tokens);
3218 }
3219 }
3220
Michael Layzell734adb42017-06-07 16:58:31 -04003221 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003222 impl ToTokens for PatRange {
3223 fn to_tokens(&self, tokens: &mut Tokens) {
3224 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003225 match self.limits {
3226 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3227 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3228 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003229 self.hi.to_tokens(tokens);
3230 }
3231 }
3232
Michael Layzell734adb42017-06-07 16:58:31 -04003233 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003234 impl ToTokens for PatSlice {
3235 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003236 // XXX: This is a mess, and it will be so easy to screw it up. How
3237 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003238 self.bracket_token.surround(tokens, |tokens| {
3239 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003240
3241 // If we need a comma before the middle or standalone .. token,
3242 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003243 if !self.front.empty_or_trailing()
3244 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003245 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003246 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003247 }
3248
3249 // If we have an identifier, we always need a .. token.
3250 if self.middle.is_some() {
3251 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003252 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003253 } else if self.dot2_token.is_some() {
3254 self.dot2_token.to_tokens(tokens);
3255 }
3256
3257 // Make sure we have a comma before the back half.
3258 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003259 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003260 self.back.to_tokens(tokens);
3261 } else {
3262 self.comma_token.to_tokens(tokens);
3263 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003264 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003265 }
3266 }
3267
Michael Layzell734adb42017-06-07 16:58:31 -04003268 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003269 impl ToTokens for PatMacro {
3270 fn to_tokens(&self, tokens: &mut Tokens) {
3271 self.mac.to_tokens(tokens);
3272 }
3273 }
3274
3275 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003276 impl ToTokens for PatVerbatim {
3277 fn to_tokens(&self, tokens: &mut Tokens) {
3278 self.tts.to_tokens(tokens);
3279 }
3280 }
3281
3282 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003283 impl ToTokens for FieldPat {
3284 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003285 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003286 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003287 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003288 }
3289 self.pat.to_tokens(tokens);
3290 }
3291 }
3292
Michael Layzell734adb42017-06-07 16:58:31 -04003293 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003294 impl ToTokens for Block {
3295 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003296 self.brace_token.surround(tokens, |tokens| {
3297 tokens.append_all(&self.stmts);
3298 });
David Tolnay42602292016-10-01 22:25:45 -07003299 }
3300 }
3301
Michael Layzell734adb42017-06-07 16:58:31 -04003302 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003303 impl ToTokens for Stmt {
3304 fn to_tokens(&self, tokens: &mut Tokens) {
3305 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003306 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003307 Stmt::Item(ref item) => item.to_tokens(tokens),
3308 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003309 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003310 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003311 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003312 }
David Tolnay42602292016-10-01 22:25:45 -07003313 }
3314 }
3315 }
David Tolnay191e0582016-10-02 18:31:09 -07003316
Michael Layzell734adb42017-06-07 16:58:31 -04003317 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003318 impl ToTokens for Local {
3319 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003320 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003321 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003322 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003323 if let Some((ref colon_token, ref ty)) = self.ty {
3324 colon_token.to_tokens(tokens);
3325 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003326 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003327 if let Some((ref eq_token, ref init)) = self.init {
3328 eq_token.to_tokens(tokens);
3329 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003330 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003331 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003332 }
3333 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003334}