blob: 2dbcde330f713bcd20fd649659f854a118bdbbef [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 Tolnaya454c8f2018-01-07 01:01:10 -080020 /// A Rust expression.
David Tolnay8c91b882017-12-28 23:04:32 -050021 pub enum Expr {
David Tolnaya454c8f2018-01-07 01:01:10 -080022 /// A box expression: `box f`.
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 Tolnaya454c8f2018-01-07 01:01:10 -080029 /// A placement expression: `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
David Tolnaya454c8f2018-01-07 01:01:10 -080037 /// A slice literal expression: `[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
David Tolnaya454c8f2018-01-07 01:01:10 -080044 /// A function call expression: `invoke(a, b)`.
Alex Crichton62a0a592017-05-22 13:58:53 -070045 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
David Tolnaya454c8f2018-01-07 01:01:10 -080052 /// A method call expression: `x.foo::<T>(a, b)`.
Michael Layzell734adb42017-06-07 16:58:31 -040053 pub MethodCall(ExprMethodCall #full {
David Tolnay8c91b882017-12-28 23:04:32 -050054 pub attrs: Vec<Attribute>,
David Tolnay76418512017-12-28 23:47:47 -050055 pub receiver: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080056 pub dot_token: Token![.],
David Tolnay4a3f59a2017-12-28 21:21:12 -050057 pub method: Ident,
David Tolnayd60cfec2017-12-29 00:21:38 -050058 pub turbofish: Option<MethodTurbofish>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050059 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050060 pub args: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070061 }),
Clar Charrd22b5702017-03-10 15:24:56 -050062
David Tolnaya454c8f2018-01-07 01:01:10 -080063 /// A tuple expression: `(a, b, c, d)`.
David Tolnay05362582017-12-26 01:33:57 -050064 pub Tuple(ExprTuple #full {
David Tolnay8c91b882017-12-28 23:04:32 -050065 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -050066 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050067 pub elems: Punctuated<Expr, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070068 }),
Clar Charrd22b5702017-03-10 15:24:56 -050069
David Tolnaya454c8f2018-01-07 01:01:10 -080070 /// A binary operation: `a + b`, `a * b`.
Alex Crichton62a0a592017-05-22 13:58:53 -070071 pub Binary(ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -050072 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070073 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -050074 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -070075 pub right: Box<Expr>,
76 }),
Clar Charrd22b5702017-03-10 15:24:56 -050077
David Tolnaya454c8f2018-01-07 01:01:10 -080078 /// A unary operation: `!x`, `*x`.
Alex Crichton62a0a592017-05-22 13:58:53 -070079 pub Unary(ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -050080 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070081 pub op: UnOp,
82 pub expr: Box<Expr>,
83 }),
Clar Charrd22b5702017-03-10 15:24:56 -050084
David Tolnaya454c8f2018-01-07 01:01:10 -080085 /// A literal in place of an expression: `1`, `"foo"`.
David Tolnay8c91b882017-12-28 23:04:32 -050086 pub Lit(ExprLit {
87 pub attrs: Vec<Attribute>,
88 pub lit: Lit,
89 }),
Clar Charrd22b5702017-03-10 15:24:56 -050090
David Tolnaya454c8f2018-01-07 01:01:10 -080091 /// A cast expression: `foo as f64`.
Alex Crichton62a0a592017-05-22 13:58:53 -070092 pub Cast(ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -050093 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -070094 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080095 pub as_token: Token![as],
David Tolnayfd6bf5c2017-11-12 09:41:14 -080096 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070097 }),
Clar Charrd22b5702017-03-10 15:24:56 -050098
David Tolnaya454c8f2018-01-07 01:01:10 -080099 /// A type ascription expression: `foo: f64`.
David Tolnay0cf94f22017-12-28 23:46:26 -0500100 pub Type(ExprType #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500101 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700102 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800103 pub colon_token: Token![:],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800104 pub ty: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500106
David Tolnaya454c8f2018-01-07 01:01:10 -0800107 /// An `if` expression with an optional `else` block: `if expr { ... }
108 /// else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700109 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800110 /// The `else` branch expression may only be an `If`, `IfLet`, or
111 /// `Block` expression, not any of the other types of expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400112 pub If(ExprIf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500113 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500114 pub if_token: Token![if],
Alex Crichton62a0a592017-05-22 13:58:53 -0700115 pub cond: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500116 pub then_branch: Block,
117 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700118 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500119
David Tolnaya454c8f2018-01-07 01:01:10 -0800120 /// An `if let` expression with an optional `else` block: `if let pat =
121 /// expr { ... } else { ... }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700122 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800123 /// The `else` branch expression may only be an `If`, `IfLet`, or
124 /// `Block` expression, not any of the other types of expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400125 pub IfLet(ExprIfLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500126 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800127 pub if_token: Token![if],
128 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500129 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800130 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500131 pub expr: Box<Expr>,
David Tolnay2ccf32a2017-12-29 00:34:26 -0500132 pub then_branch: Block,
133 pub else_branch: Option<(Token![else], Box<Expr>)>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700134 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500135
David Tolnaya454c8f2018-01-07 01:01:10 -0800136 /// A while loop: `while expr { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400137 pub While(ExprWhile #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500138 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500139 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800140 pub while_token: Token![while],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500141 pub cond: Box<Expr>,
142 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700143 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500144
David Tolnaya454c8f2018-01-07 01:01:10 -0800145 /// A while-let loop: `while let pat = expr { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400146 pub WhileLet(ExprWhileLet #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500147 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500148 pub label: Option<Label>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800149 pub while_token: Token![while],
150 pub let_token: Token![let],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500151 pub pat: Box<Pat>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800152 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500153 pub expr: Box<Expr>,
154 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700155 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500156
David Tolnaya454c8f2018-01-07 01:01:10 -0800157 /// A for loop: `for pat in expr { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400158 pub ForLoop(ExprForLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500159 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500160 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500161 pub for_token: Token![for],
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 pub pat: Box<Pat>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500163 pub in_token: Token![in],
Alex Crichton62a0a592017-05-22 13:58:53 -0700164 pub expr: Box<Expr>,
165 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700166 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500167
David Tolnaya454c8f2018-01-07 01:01:10 -0800168 /// Conditionless loop: `loop { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400169 pub Loop(ExprLoop #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500170 pub attrs: Vec<Attribute>,
David Tolnaybcd498f2017-12-29 12:02:33 -0500171 pub label: Option<Label>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500172 pub loop_token: Token![loop],
173 pub body: Block,
Alex Crichton62a0a592017-05-22 13:58:53 -0700174 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500175
David Tolnaya454c8f2018-01-07 01:01:10 -0800176 /// A `match` expression: `match n { Some(n) => {}, None => {} }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400177 pub Match(ExprMatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500178 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800179 pub match_token: Token![match],
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500181 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 pub arms: Vec<Arm>,
183 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500184
David Tolnaya454c8f2018-01-07 01:01:10 -0800185 /// A closure expression: `|a, b| a + b`.
Michael Layzell734adb42017-06-07 16:58:31 -0400186 pub Closure(ExprClosure #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500187 pub attrs: Vec<Attribute>,
David Tolnayefc96fb2017-12-29 02:03:15 -0500188 pub capture: Option<Token![move]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800189 pub or1_token: Token![|],
David Tolnayf2cfd722017-12-31 18:02:51 -0500190 pub inputs: Punctuated<FnArg, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800191 pub or2_token: Token![|],
David Tolnay7f675742017-12-27 22:43:21 -0500192 pub output: ReturnType,
193 pub body: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500195
David Tolnaya454c8f2018-01-07 01:01:10 -0800196 /// An unsafe block: `unsafe { ... }`.
Nika Layzell640832a2017-12-04 13:37:09 -0500197 pub Unsafe(ExprUnsafe #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500198 pub attrs: Vec<Attribute>,
Nika Layzell640832a2017-12-04 13:37:09 -0500199 pub unsafe_token: Token![unsafe],
200 pub block: Block,
201 }),
202
David Tolnaya454c8f2018-01-07 01:01:10 -0800203 /// A blocked scope: `{ ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400204 pub Block(ExprBlock #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500205 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700206 pub block: Block,
207 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700208
David Tolnaya454c8f2018-01-07 01:01:10 -0800209 /// An assignment expression: `a = compute()`.
Michael Layzell734adb42017-06-07 16:58:31 -0400210 pub Assign(ExprAssign #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500211 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700212 pub left: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800213 pub eq_token: Token![=],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500214 pub right: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500216
David Tolnaya454c8f2018-01-07 01:01:10 -0800217 /// A compound assignment expression: `counter += 1`.
Michael Layzell734adb42017-06-07 16:58:31 -0400218 pub AssignOp(ExprAssignOp #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500219 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700220 pub left: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500221 pub op: BinOp,
Alex Crichton62a0a592017-05-22 13:58:53 -0700222 pub right: Box<Expr>,
223 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500224
David Tolnaya454c8f2018-01-07 01:01:10 -0800225 /// Access of a named struct field (`obj.k`) or unnamed tuple struct
David Tolnay85b69a42017-12-27 20:43:10 -0500226 /// field (`obj.0`).
Michael Layzell734adb42017-06-07 16:58:31 -0400227 pub Field(ExprField #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500228 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500229 pub base: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800230 pub dot_token: Token![.],
David Tolnay85b69a42017-12-27 20:43:10 -0500231 pub member: Member,
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500233
David Tolnay05658502018-01-07 09:56:37 -0800234 /// A square bracketed indexing expression: `vector[2]`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700235 pub Index(ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -0500236 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700237 pub expr: Box<Expr>,
David Tolnay32954ef2017-12-26 22:43:16 -0500238 pub bracket_token: token::Bracket,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500239 pub index: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500241
David Tolnaya454c8f2018-01-07 01:01:10 -0800242 /// A range expression: `1..2`, `1..`, `..2`, `1..=2`, `..=2`.
Michael Layzell734adb42017-06-07 16:58:31 -0400243 pub Range(ExprRange #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500244 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 pub from: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500247 pub to: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700248 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700249
David Tolnaya454c8f2018-01-07 01:01:10 -0800250 /// A path like `std::mem::replace` possibly containing generic
251 /// parameters and a qualified self-type.
Alex Crichton62a0a592017-05-22 13:58:53 -0700252 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800253 /// A plain identifier like `x` is a path of length 1.
Alex Crichton62a0a592017-05-22 13:58:53 -0700254 pub Path(ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -0500255 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 pub qself: Option<QSelf>,
257 pub path: Path,
258 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700259
David Tolnaya454c8f2018-01-07 01:01:10 -0800260 /// A referencing operation: `&a` or `&mut a`.
Michael Layzell734adb42017-06-07 16:58:31 -0400261 pub AddrOf(ExprAddrOf #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500262 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800263 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500264 pub mutability: Option<Token![mut]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700265 pub expr: Box<Expr>,
266 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500267
David Tolnaya454c8f2018-01-07 01:01:10 -0800268 /// A `break`, with an optional label to break and an optional
269 /// expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400270 pub Break(ExprBreak #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500271 pub attrs: Vec<Attribute>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500272 pub break_token: Token![break],
David Tolnay63e3dee2017-06-03 20:13:17 -0700273 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700274 pub expr: Option<Box<Expr>>,
275 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500276
David Tolnaya454c8f2018-01-07 01:01:10 -0800277 /// A `continue`, with an optional label.
Michael Layzell734adb42017-06-07 16:58:31 -0400278 pub Continue(ExprContinue #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500279 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800280 pub continue_token: Token![continue],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500281 pub label: Option<Lifetime>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 }),
Clar Charrd22b5702017-03-10 15:24:56 -0500283
David Tolnaya454c8f2018-01-07 01:01:10 -0800284 /// A `return`, with an optional value to be returned.
David Tolnayc246cd32017-12-28 23:14:32 -0500285 pub Return(ExprReturn #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500286 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800287 pub return_token: Token![return],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500288 pub expr: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700290
David Tolnaya454c8f2018-01-07 01:01:10 -0800291 /// A macro invocation expression: `format!("{}", q)`.
David Tolnay8c91b882017-12-28 23:04:32 -0500292 pub Macro(ExprMacro #full {
293 pub attrs: Vec<Attribute>,
294 pub mac: Macro,
295 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700296
David Tolnaya454c8f2018-01-07 01:01:10 -0800297 /// A struct literal expression: `Point { x: 1, y: 1 }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700298 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800299 /// The `rest` provides the value of the remaining fields as in `S { a:
300 /// 1, b: 1, ..rest }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400301 pub Struct(ExprStruct #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500302 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500304 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500305 pub fields: Punctuated<FieldValue, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500306 pub dot2_token: Option<Token![..]>,
307 pub rest: Option<Box<Expr>>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700309
David Tolnaya454c8f2018-01-07 01:01:10 -0800310 /// An array literal constructed from one repeated element: `[0u8; N]`.
Michael Layzell734adb42017-06-07 16:58:31 -0400311 pub Repeat(ExprRepeat #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500312 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500313 pub bracket_token: token::Bracket,
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 pub expr: Box<Expr>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500315 pub semi_token: Token![;],
David Tolnay84d80442018-01-07 01:03:20 -0800316 pub len: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700317 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700318
David Tolnaya454c8f2018-01-07 01:01:10 -0800319 /// A parenthesized expression: `(a + b)`.
David Tolnaye98775f2017-12-28 23:17:00 -0500320 pub Paren(ExprParen #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500321 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500322 pub paren_token: token::Paren,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500323 pub expr: Box<Expr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700324 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700325
David Tolnaya454c8f2018-01-07 01:01:10 -0800326 /// An expression contained within invisible delimiters.
Michael Layzell93c36282017-06-04 20:43:14 -0400327 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800328 /// This variant is important for faithfully representing the precedence
329 /// of expressions and is related to `None`-delimited spans in a
330 /// `TokenStream`.
David Tolnaye98775f2017-12-28 23:17:00 -0500331 pub Group(ExprGroup #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500332 pub attrs: Vec<Attribute>,
David Tolnay32954ef2017-12-26 22:43:16 -0500333 pub group_token: token::Group,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500334 pub expr: Box<Expr>,
Michael Layzell93c36282017-06-04 20:43:14 -0400335 }),
336
David Tolnaya454c8f2018-01-07 01:01:10 -0800337 /// A try-expression: `expr?`.
Michael Layzell734adb42017-06-07 16:58:31 -0400338 pub Try(ExprTry #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500339 pub attrs: Vec<Attribute>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700340 pub expr: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800341 pub question_token: Token![?],
Alex Crichton62a0a592017-05-22 13:58:53 -0700342 }),
Arnavion02ef13f2017-04-25 00:54:31 -0700343
David Tolnaya454c8f2018-01-07 01:01:10 -0800344 /// A catch expression: `do catch { ... }`.
Michael Layzell734adb42017-06-07 16:58:31 -0400345 pub Catch(ExprCatch #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500346 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800347 pub do_token: Token![do],
348 pub catch_token: Token![catch],
Alex Crichton62a0a592017-05-22 13:58:53 -0700349 pub block: Block,
350 }),
Alex Crichtonfe110462017-06-01 12:49:27 -0700351
David Tolnaya454c8f2018-01-07 01:01:10 -0800352 /// A yield expression: `yield expr`.
Alex Crichtonfe110462017-06-01 12:49:27 -0700353 pub Yield(ExprYield #full {
David Tolnay8c91b882017-12-28 23:04:32 -0500354 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800355 pub yield_token: Token![yield],
Alex Crichtonfe110462017-06-01 12:49:27 -0700356 pub expr: Option<Box<Expr>>,
357 }),
David Tolnay2ae520a2017-12-29 11:19:50 -0500358
David Tolnaya454c8f2018-01-07 01:01:10 -0800359 /// Tokens in expression position not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500360 pub Verbatim(ExprVerbatim #manual_extra_traits {
361 pub tts: TokenStream,
362 }),
363 }
364}
365
366#[cfg(feature = "extra-traits")]
367impl Eq for ExprVerbatim {}
368
369#[cfg(feature = "extra-traits")]
370impl PartialEq for ExprVerbatim {
371 fn eq(&self, other: &Self) -> bool {
372 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
373 }
374}
375
376#[cfg(feature = "extra-traits")]
377impl Hash for ExprVerbatim {
378 fn hash<H>(&self, state: &mut H)
379 where
380 H: Hasher,
381 {
382 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700383 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700384}
385
David Tolnay8c91b882017-12-28 23:04:32 -0500386impl Expr {
387 // Not public API.
388 #[doc(hidden)]
David Tolnay096d4982017-12-28 23:18:18 -0500389 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -0500390 pub fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
David Tolnay8c91b882017-12-28 23:04:32 -0500391 match *self {
David Tolnay61037c62018-01-05 16:21:03 -0800392 Expr::Box(ExprBox { ref mut attrs, .. })
393 | Expr::InPlace(ExprInPlace { ref mut attrs, .. })
394 | Expr::Array(ExprArray { ref mut attrs, .. })
395 | Expr::Call(ExprCall { ref mut attrs, .. })
396 | Expr::MethodCall(ExprMethodCall { ref mut attrs, .. })
397 | Expr::Tuple(ExprTuple { ref mut attrs, .. })
398 | Expr::Binary(ExprBinary { ref mut attrs, .. })
399 | Expr::Unary(ExprUnary { ref mut attrs, .. })
400 | Expr::Lit(ExprLit { ref mut attrs, .. })
401 | Expr::Cast(ExprCast { ref mut attrs, .. })
402 | Expr::Type(ExprType { ref mut attrs, .. })
403 | Expr::If(ExprIf { ref mut attrs, .. })
404 | Expr::IfLet(ExprIfLet { ref mut attrs, .. })
405 | Expr::While(ExprWhile { ref mut attrs, .. })
406 | Expr::WhileLet(ExprWhileLet { ref mut attrs, .. })
407 | Expr::ForLoop(ExprForLoop { ref mut attrs, .. })
408 | Expr::Loop(ExprLoop { ref mut attrs, .. })
409 | Expr::Match(ExprMatch { ref mut attrs, .. })
410 | Expr::Closure(ExprClosure { ref mut attrs, .. })
411 | Expr::Unsafe(ExprUnsafe { ref mut attrs, .. })
412 | Expr::Block(ExprBlock { ref mut attrs, .. })
413 | Expr::Assign(ExprAssign { ref mut attrs, .. })
414 | Expr::AssignOp(ExprAssignOp { ref mut attrs, .. })
415 | Expr::Field(ExprField { ref mut attrs, .. })
416 | Expr::Index(ExprIndex { ref mut attrs, .. })
417 | Expr::Range(ExprRange { ref mut attrs, .. })
418 | Expr::Path(ExprPath { ref mut attrs, .. })
419 | Expr::AddrOf(ExprAddrOf { ref mut attrs, .. })
420 | Expr::Break(ExprBreak { ref mut attrs, .. })
421 | Expr::Continue(ExprContinue { ref mut attrs, .. })
422 | Expr::Return(ExprReturn { ref mut attrs, .. })
423 | Expr::Macro(ExprMacro { ref mut attrs, .. })
424 | Expr::Struct(ExprStruct { ref mut attrs, .. })
425 | Expr::Repeat(ExprRepeat { ref mut attrs, .. })
426 | Expr::Paren(ExprParen { ref mut attrs, .. })
427 | Expr::Group(ExprGroup { ref mut attrs, .. })
428 | Expr::Try(ExprTry { ref mut attrs, .. })
429 | Expr::Catch(ExprCatch { ref mut attrs, .. })
430 | Expr::Yield(ExprYield { ref mut attrs, .. }) => mem::replace(attrs, new),
David Tolnay2ae520a2017-12-29 11:19:50 -0500431 Expr::Verbatim(_) => {
432 // TODO
433 Vec::new()
434 }
David Tolnay8c91b882017-12-28 23:04:32 -0500435 }
436 }
437}
438
David Tolnay85b69a42017-12-27 20:43:10 -0500439ast_enum! {
440 /// A struct or tuple struct field accessed in a struct literal or field
441 /// expression.
442 pub enum Member {
443 /// A named field like `self.x`.
444 Named(Ident),
445 /// An unnamed field like `self.0`.
446 Unnamed(Index),
447 }
448}
449
David Tolnay85b69a42017-12-27 20:43:10 -0500450ast_struct! {
451 /// The index of an unnamed tuple struct field.
452 pub struct Index #manual_extra_traits {
453 pub index: u32,
454 pub span: Span,
455 }
456}
457
David Tolnay14982012017-12-29 00:49:51 -0500458impl From<usize> for Index {
459 fn from(index: usize) -> Index {
460 assert!(index < std::u32::MAX as usize);
461 Index {
462 index: index as u32,
463 span: Span::default(),
464 }
465 }
466}
467
468#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500469impl Eq for Index {}
470
David Tolnay14982012017-12-29 00:49:51 -0500471#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500472impl PartialEq for Index {
473 fn eq(&self, other: &Self) -> bool {
474 self.index == other.index
475 }
476}
477
David Tolnay14982012017-12-29 00:49:51 -0500478#[cfg(feature = "extra-traits")]
David Tolnay85b69a42017-12-27 20:43:10 -0500479impl Hash for Index {
480 fn hash<H: Hasher>(&self, state: &mut H) {
481 self.index.hash(state);
482 }
483}
484
485#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700486ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800487 /// The `::<>` explicit type parameters passed to a method call:
488 /// `parse::<u64>()`.
David Tolnayd60cfec2017-12-29 00:21:38 -0500489 pub struct MethodTurbofish {
490 pub colon2_token: Token![::],
491 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500492 pub args: Punctuated<GenericMethodArgument, Token![,]>,
David Tolnayd60cfec2017-12-29 00:21:38 -0500493 pub gt_token: Token![>],
494 }
495}
496
497#[cfg(feature = "full")]
498ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800499 /// An individual generic argument to a method, like `T`.
David Tolnayd60cfec2017-12-29 00:21:38 -0500500 pub enum GenericMethodArgument {
David Tolnaya454c8f2018-01-07 01:01:10 -0800501 /// A type argument.
David Tolnayd60cfec2017-12-29 00:21:38 -0500502 Type(Type),
David Tolnaya454c8f2018-01-07 01:01:10 -0800503 /// A const expression. Must be inside of a block.
David Tolnayd60cfec2017-12-29 00:21:38 -0500504 ///
505 /// NOTE: Identity expressions are represented as Type arguments, as
506 /// they are indistinguishable syntactically.
507 Const(Expr),
508 }
509}
510
511#[cfg(feature = "full")]
512ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -0700513 /// A field-value pair in a struct literal.
514 pub struct FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -0500515 /// Attributes tagged on the field.
516 pub attrs: Vec<Attribute>,
517
518 /// Name or index of the field.
519 pub member: Member,
520
David Tolnay5d7098a2017-12-29 01:35:24 -0500521 /// The colon in `Struct { x: x }`. If written in shorthand like
522 /// `Struct { x }`, there is no colon.
David Tolnay85b69a42017-12-27 20:43:10 -0500523 pub colon_token: Option<Token![:]>,
Clar Charrd22b5702017-03-10 15:24:56 -0500524
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 /// Value of the field.
526 pub expr: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -0700527 }
David Tolnay055a7042016-10-02 19:23:54 -0700528}
529
Michael Layzell734adb42017-06-07 16:58:31 -0400530#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700531ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800532 /// A lifetime labeling a `for`, `while`, or `loop`.
David Tolnaybcd498f2017-12-29 12:02:33 -0500533 pub struct Label {
534 pub name: Lifetime,
535 pub colon_token: Token![:],
536 }
537}
538
539#[cfg(feature = "full")]
540ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800541 /// A braced block containing Rust statements.
Alex Crichton62a0a592017-05-22 13:58:53 -0700542 pub struct Block {
David Tolnay32954ef2017-12-26 22:43:16 -0500543 pub brace_token: token::Brace,
Alex Crichton62a0a592017-05-22 13:58:53 -0700544 /// Statements in a block
545 pub stmts: Vec<Stmt>,
546 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700547}
548
Michael Layzell734adb42017-06-07 16:58:31 -0400549#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700550ast_enum! {
551 /// A statement, usually ending in a semicolon.
552 pub enum Stmt {
553 /// A local (let) binding.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800554 Local(Local),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700555
Alex Crichton62a0a592017-05-22 13:58:53 -0700556 /// An item definition.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800557 Item(Item),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700558
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 /// Expr without trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800560 Expr(Expr),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700561
David Tolnaya454c8f2018-01-07 01:01:10 -0800562 /// Expression with trailing semicolon.
David Tolnay1f0b7b82018-01-06 16:07:14 -0800563 Semi(Expr, Token![;]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700564 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700565}
566
Michael Layzell734adb42017-06-07 16:58:31 -0400567#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700568ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800569 /// A local `let` binding: `let x: u64 = s.parse()?`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700570 pub struct Local {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500571 pub attrs: Vec<Attribute>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800572 pub let_token: Token![let],
Alex Crichton62a0a592017-05-22 13:58:53 -0700573 pub pat: Box<Pat>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500574 pub ty: Option<(Token![:], Box<Type>)>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500575 pub init: Option<(Token![=], Box<Expr>)>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500576 pub semi_token: Token![;],
Alex Crichton62a0a592017-05-22 13:58:53 -0700577 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700578}
579
Michael Layzell734adb42017-06-07 16:58:31 -0400580#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700581ast_enum_of_structs! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800582 /// A pattern in a local binding, function signature, match expression, or
583 /// various other places.
Alex Crichton62a0a592017-05-22 13:58:53 -0700584 // Clippy false positive
585 // https://github.com/Manishearth/rust-clippy/issues/1241
586 #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))]
587 pub enum Pat {
David Tolnaya454c8f2018-01-07 01:01:10 -0800588 /// A pattern that matches any value: `_`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700589 pub Wild(PatWild {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800590 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700591 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700592
David Tolnaya454c8f2018-01-07 01:01:10 -0800593 /// A pattern that binds a new variable: `ref mut binding @ SUBPATTERN`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700594 pub Ident(PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -0500595 pub by_ref: Option<Token![ref]>,
596 pub mutability: Option<Token![mut]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700597 pub ident: Ident,
David Tolnay8b4d3022017-12-29 12:11:10 -0500598 pub subpat: Option<(Token![@], Box<Pat>)>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700599 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700600
David Tolnaya454c8f2018-01-07 01:01:10 -0800601 /// A struct or struct variant pattern: `Variant { x, y, .. }`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700602 pub Struct(PatStruct {
603 pub path: Path,
David Tolnay32954ef2017-12-26 22:43:16 -0500604 pub brace_token: token::Brace,
David Tolnayf2cfd722017-12-31 18:02:51 -0500605 pub fields: Punctuated<FieldPat, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800606 pub dot2_token: Option<Token![..]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700607 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700608
David Tolnaya454c8f2018-01-07 01:01:10 -0800609 /// A tuple struct or tuple variant pattern: `Variant(x, y, .., z)`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700610 pub TupleStruct(PatTupleStruct {
611 pub path: Path,
612 pub pat: PatTuple,
613 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700614
David Tolnaya454c8f2018-01-07 01:01:10 -0800615 /// A path pattern like `Color::Red`, optionally qualified with a
616 /// self-type.
617 ///
618 /// Unquailfied path patterns can legally refer to variants, structs,
619 /// constants or associated constants. Quailfied path patterns like
620 /// `<A>::B::C` and `<A as Trait>::B::C` can only legally refer to
621 /// associated constants.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700622 pub Path(PatPath {
623 pub qself: Option<QSelf>,
624 pub path: Path,
625 }),
David Tolnayf4bbbd92016-09-23 14:41:55 -0700626
David Tolnaya454c8f2018-01-07 01:01:10 -0800627 /// A tuple pattern: `(a, b)`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700628 pub Tuple(PatTuple {
David Tolnay32954ef2017-12-26 22:43:16 -0500629 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500630 pub front: Punctuated<Pat, Token![,]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500631 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500632 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500633 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700634 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800635
636 /// A box pattern: `box v`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700637 pub Box(PatBox {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800638 pub box_token: Token![box],
David Tolnay4a3f59a2017-12-28 21:21:12 -0500639 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700640 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800641
642 /// A reference pattern: `&mut (first, second)`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700643 pub Ref(PatRef {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800644 pub and_token: Token![&],
David Tolnay24237fb2017-12-29 02:15:26 -0500645 pub mutability: Option<Token![mut]>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500646 pub pat: Box<Pat>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700647 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800648
649 /// A literal pattern: `0`.
650 ///
651 /// This holds an `Expr` rather than a `Lit` because negative numbers
652 /// are represented as an `Expr::Unary`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700653 pub Lit(PatLit {
654 pub expr: Box<Expr>,
655 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800656
657 /// A range pattern: `1..=2`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700658 pub Range(PatRange {
659 pub lo: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700660 pub limits: RangeLimits,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500661 pub hi: Box<Expr>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700662 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800663
664 /// A dynamically sized slice pattern: `[a, b, i.., y, z]`.
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700665 pub Slice(PatSlice {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500666 pub bracket_token: token::Bracket,
David Tolnayf2cfd722017-12-31 18:02:51 -0500667 pub front: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700668 pub middle: Option<Box<Pat>>,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500669 pub dot2_token: Option<Token![..]>,
David Tolnay41871922017-12-29 01:53:45 -0500670 pub comma_token: Option<Token![,]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500671 pub back: Punctuated<Pat, Token![,]>,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700672 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800673
674 /// A macro in expression position.
David Tolnay323279a2017-12-29 11:26:32 -0500675 pub Macro(PatMacro {
676 pub mac: Macro,
677 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800678
679 /// Tokens in pattern position not interpreted by Syn.
David Tolnay2ae520a2017-12-29 11:19:50 -0500680 pub Verbatim(PatVerbatim #manual_extra_traits {
681 pub tts: TokenStream,
682 }),
683 }
684}
685
David Tolnayc43b44e2017-12-30 23:55:54 -0500686#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500687impl Eq for PatVerbatim {}
688
David Tolnayc43b44e2017-12-30 23:55:54 -0500689#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500690impl PartialEq for PatVerbatim {
691 fn eq(&self, other: &Self) -> bool {
692 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
693 }
694}
695
David Tolnayc43b44e2017-12-30 23:55:54 -0500696#[cfg(all(feature = "full", feature = "extra-traits"))]
David Tolnay2ae520a2017-12-29 11:19:50 -0500697impl Hash for PatVerbatim {
698 fn hash<H>(&self, state: &mut H)
699 where
700 H: Hasher,
701 {
702 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700703 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700704}
705
Michael Layzell734adb42017-06-07 16:58:31 -0400706#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700707ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800708 /// One arm of a `match` expression: `0...10 => { return true; }`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800710 /// As in:
Alex Crichton62a0a592017-05-22 13:58:53 -0700711 ///
David Tolnaybcf26022017-12-25 22:10:52 -0500712 /// ```rust
David Tolnaya454c8f2018-01-07 01:01:10 -0800713 /// # fn f() -> bool {
David Tolnaybcf26022017-12-25 22:10:52 -0500714 /// # let n = 0;
Alex Crichton62a0a592017-05-22 13:58:53 -0700715 /// match n {
David Tolnaya454c8f2018-01-07 01:01:10 -0800716 /// 0...10 => {
717 /// return true;
718 /// }
719 /// // ...
David Tolnaybcf26022017-12-25 22:10:52 -0500720 /// # _ => {}
Alex Crichton62a0a592017-05-22 13:58:53 -0700721 /// }
David Tolnaya454c8f2018-01-07 01:01:10 -0800722 /// # false
David Tolnaybcf26022017-12-25 22:10:52 -0500723 /// # }
Alex Crichton62a0a592017-05-22 13:58:53 -0700724 /// ```
725 pub struct Arm {
726 pub attrs: Vec<Attribute>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500727 pub pats: Punctuated<Pat, Token![|]>,
David Tolnay8b4d3022017-12-29 12:11:10 -0500728 pub guard: Option<(Token![if], Box<Expr>)>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800729 pub rocket_token: Token![=>],
Alex Crichton62a0a592017-05-22 13:58:53 -0700730 pub body: Box<Expr>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800731 pub comma: Option<Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700733}
734
Michael Layzell734adb42017-06-07 16:58:31 -0400735#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700736ast_enum! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800737 /// Limit types of a range, inclusive or exclusive.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700738 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700739 pub enum RangeLimits {
David Tolnaya454c8f2018-01-07 01:01:10 -0800740 /// Inclusive at the beginning, exclusive at the end.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800741 HalfOpen(Token![..]),
David Tolnaya454c8f2018-01-07 01:01:10 -0800742 /// Inclusive at the beginning and end.
David Tolnaybe55d7b2017-12-17 23:41:20 -0800743 Closed(Token![..=]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700744 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700745}
746
Michael Layzell734adb42017-06-07 16:58:31 -0400747#[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -0700748ast_struct! {
David Tolnaya454c8f2018-01-07 01:01:10 -0800749 /// A single field in a struct pattern.
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 ///
David Tolnaya454c8f2018-01-07 01:01:10 -0800751 /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` are treated
752 /// the same as `x: x, y: ref y, z: ref mut z` but there is no colon token.
Alex Crichton62a0a592017-05-22 13:58:53 -0700753 pub struct FieldPat {
David Tolnay4a3f59a2017-12-28 21:21:12 -0500754 pub attrs: Vec<Attribute>,
David Tolnay85b69a42017-12-27 20:43:10 -0500755 pub member: Member,
David Tolnay4a3f59a2017-12-28 21:21:12 -0500756 pub colon_token: Option<Token![:]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700757 pub pat: Box<Pat>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700758 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700759}
760
Michael Layzell3936ceb2017-07-08 00:28:36 -0400761#[cfg(any(feature = "parsing", feature = "printing"))]
762#[cfg(feature = "full")]
Alex Crichton03b30272017-08-28 09:35:24 -0700763fn arm_expr_requires_comma(expr: &Expr) -> bool {
764 // see https://github.com/rust-lang/rust/blob/eb8f2586e
765 // /src/libsyntax/parse/classify.rs#L17-L37
David Tolnay8c91b882017-12-28 23:04:32 -0500766 match *expr {
767 Expr::Unsafe(..)
768 | Expr::Block(..)
769 | Expr::If(..)
770 | Expr::IfLet(..)
771 | Expr::Match(..)
772 | Expr::While(..)
773 | Expr::WhileLet(..)
774 | Expr::Loop(..)
775 | Expr::ForLoop(..)
776 | Expr::Catch(..) => false,
Alex Crichton03b30272017-08-28 09:35:24 -0700777 _ => true,
Michael Layzell3936ceb2017-07-08 00:28:36 -0400778 }
779}
780
David Tolnayb9c8e322016-09-23 20:48:37 -0700781#[cfg(feature = "parsing")]
782pub mod parsing {
783 use super::*;
David Tolnay056de302018-01-05 14:29:05 -0800784 use path::parsing::qpath;
David Tolnay2ccf32a2017-12-29 00:34:26 -0500785 #[cfg(feature = "full")]
David Tolnay056de302018-01-05 14:29:05 -0800786 use path::parsing::ty_no_eq_after;
David Tolnayb9c8e322016-09-23 20:48:37 -0700787
Michael Layzell734adb42017-06-07 16:58:31 -0400788 #[cfg(feature = "full")]
David Tolnay360efd22018-01-04 23:35:26 -0800789 use proc_macro2::TokenStream;
David Tolnayc5ab8c62017-12-26 16:43:39 -0500790 use synom::Synom;
David Tolnaydfc886b2018-01-06 08:03:09 -0800791 use buffer::Cursor;
Michael Layzell734adb42017-06-07 16:58:31 -0400792 #[cfg(feature = "full")]
David Tolnayc5ab8c62017-12-26 16:43:39 -0500793 use parse_error;
David Tolnay203557a2017-12-27 23:59:33 -0500794 use synom::PResult;
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795
David Tolnaybcf26022017-12-25 22:10:52 -0500796 // When we're parsing expressions which occur before blocks, like in an if
797 // statement's condition, we cannot parse a struct literal.
798 //
799 // Struct literals are ambiguous in certain positions
800 // https://github.com/rust-lang/rfcs/pull/92
David Tolnayaf2557e2016-10-24 11:52:21 -0700801 macro_rules! ambiguous_expr {
802 ($i:expr, $allow_struct:ident) => {
David Tolnay54e854d2016-10-24 12:03:30 -0700803 ambiguous_expr($i, $allow_struct, true)
David Tolnayaf2557e2016-10-24 11:52:21 -0700804 };
805 }
806
David Tolnaybcf26022017-12-25 22:10:52 -0500807 // When we are parsing an optional suffix expression, we cannot allow blocks
808 // if structs are not allowed.
809 //
810 // Example:
811 //
812 // if break {} {}
813 //
814 // is ambiguous between:
815 //
816 // if (break {}) {}
817 // if (break) {} {}
Michael Layzell734adb42017-06-07 16:58:31 -0400818 #[cfg(feature = "full")]
Michael Layzellb78f3b52017-06-04 19:03:03 -0400819 macro_rules! opt_ambiguous_expr {
820 ($i:expr, $allow_struct:ident) => {
821 option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct))
822 };
823 }
824
Alex Crichton954046c2017-05-30 21:49:42 -0700825 impl Synom for Expr {
Michael Layzell92639a52017-06-01 00:07:44 -0400826 named!(parse -> Self, ambiguous_expr!(true));
Alex Crichton954046c2017-05-30 21:49:42 -0700827
828 fn description() -> Option<&'static str> {
829 Some("expression")
830 }
831 }
832
Michael Layzell734adb42017-06-07 16:58:31 -0400833 #[cfg(feature = "full")]
David Tolnayaf2557e2016-10-24 11:52:21 -0700834 named!(expr_no_struct -> Expr, ambiguous_expr!(false));
835
David Tolnaybcf26022017-12-25 22:10:52 -0500836 // Parse an arbitrary expression.
Michael Layzell734adb42017-06-07 16:58:31 -0400837 #[cfg(feature = "full")]
David Tolnay51382052017-12-27 13:46:21 -0500838 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500839 call!(i, assign_expr, allow_struct, allow_block)
Michael Layzellb78f3b52017-06-04 19:03:03 -0400840 }
841
Michael Layzell734adb42017-06-07 16:58:31 -0400842 #[cfg(not(feature = "full"))]
David Tolnay51382052017-12-27 13:46:21 -0500843 fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> {
David Tolnay8c91b882017-12-28 23:04:32 -0500844 // NOTE: We intentionally skip assign_expr, placement_expr, and
845 // range_expr, as they are not parsed in non-full mode.
846 call!(i, or_expr, allow_struct, allow_block)
Michael Layzell734adb42017-06-07 16:58:31 -0400847 }
848
David Tolnaybcf26022017-12-25 22:10:52 -0500849 // Parse a left-associative binary operator.
Michael Layzellb78f3b52017-06-04 19:03:03 -0400850 macro_rules! binop {
851 (
852 $name: ident,
853 $next: ident,
854 $submac: ident!( $($args:tt)* )
855 ) => {
David Tolnay8c91b882017-12-28 23:04:32 -0500856 named!($name(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400857 mut e: call!($next, allow_struct, allow_block) >>
858 many0!(do_parse!(
859 op: $submac!($($args)*) >>
860 rhs: call!($next, allow_struct, true) >>
861 ({
862 e = ExprBinary {
David Tolnay8c91b882017-12-28 23:04:32 -0500863 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400864 left: Box::new(e.into()),
865 op: op,
866 right: Box::new(rhs.into()),
867 }.into();
868 })
869 )) >>
870 (e)
871 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700872 }
David Tolnay54e854d2016-10-24 12:03:30 -0700873 }
David Tolnayb9c8e322016-09-23 20:48:37 -0700874
David Tolnaybcf26022017-12-25 22:10:52 -0500875 // <placement> = <placement> ..
876 // <placement> += <placement> ..
877 // <placement> -= <placement> ..
878 // <placement> *= <placement> ..
879 // <placement> /= <placement> ..
880 // <placement> %= <placement> ..
881 // <placement> ^= <placement> ..
882 // <placement> &= <placement> ..
883 // <placement> |= <placement> ..
884 // <placement> <<= <placement> ..
885 // <placement> >>= <placement> ..
886 //
887 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400888 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500889 named!(assign_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400890 mut e: call!(placement_expr, allow_struct, allow_block) >>
891 alt!(
892 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800893 eq: punct!(=) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400894 // Recurse into self to parse right-associative operator.
895 rhs: call!(assign_expr, allow_struct, true) >>
896 ({
897 e = ExprAssign {
David Tolnay8c91b882017-12-28 23:04:32 -0500898 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500899 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400900 eq_token: eq,
David Tolnay3bc597f2017-12-31 02:31:11 -0500901 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400902 }.into();
903 })
904 )
905 |
906 do_parse!(
907 op: call!(BinOp::parse_assign_op) >>
908 // Recurse into self to parse right-associative operator.
909 rhs: call!(assign_expr, allow_struct, true) >>
910 ({
911 e = ExprAssignOp {
David Tolnay8c91b882017-12-28 23:04:32 -0500912 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500913 left: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400914 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -0500915 right: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400916 }.into();
917 })
918 )
919 |
920 epsilon!()
921 ) >>
922 (e)
923 ));
924
David Tolnaybcf26022017-12-25 22:10:52 -0500925 // <range> <- <range> ..
926 //
927 // NOTE: The `in place { expr }` version of this syntax is parsed in
928 // `atom_expr`, not here.
929 //
930 // NOTE: This operator is right-associative.
Michael Layzell734adb42017-06-07 16:58:31 -0400931 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500932 named!(placement_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400933 mut e: call!(range_expr, allow_struct, allow_block) >>
934 alt!(
935 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800936 arrow: punct!(<-) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -0400937 // Recurse into self to parse right-associative operator.
938 rhs: call!(placement_expr, allow_struct, true) >>
939 ({
Michael Layzellb78f3b52017-06-04 19:03:03 -0400940 e = ExprInPlace {
David Tolnay8c91b882017-12-28 23:04:32 -0500941 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400942 // op: BinOp::Place(larrow),
David Tolnay3bc597f2017-12-31 02:31:11 -0500943 place: Box::new(e),
David Tolnay8701a5c2017-12-28 23:31:10 -0500944 arrow_token: arrow,
David Tolnay3bc597f2017-12-31 02:31:11 -0500945 value: Box::new(rhs),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400946 }.into();
947 })
948 )
949 |
950 epsilon!()
951 ) >>
952 (e)
953 ));
954
David Tolnaybcf26022017-12-25 22:10:52 -0500955 // <or> ... <or> ..
956 // <or> .. <or> ..
957 // <or> ..
958 //
959 // NOTE: This is currently parsed oddly - I'm not sure of what the exact
960 // rules are for parsing these expressions are, but this is not correct.
961 // For example, `a .. b .. c` is not a legal expression. It should not
962 // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently.
963 //
964 // NOTE: The form of ranges which don't include a preceding expression are
965 // parsed by `atom_expr`, rather than by this function.
Michael Layzell734adb42017-06-07 16:58:31 -0400966 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -0500967 named!(range_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -0400968 mut e: call!(or_expr, allow_struct, allow_block) >>
969 many0!(do_parse!(
970 limits: syn!(RangeLimits) >>
971 // We don't want to allow blocks here if we don't allow structs. See
972 // the reasoning for `opt_ambiguous_expr!` above.
973 hi: option!(call!(or_expr, allow_struct, allow_struct)) >>
974 ({
975 e = ExprRange {
David Tolnay8c91b882017-12-28 23:04:32 -0500976 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -0500977 from: Some(Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400978 limits: limits,
David Tolnay3bc597f2017-12-31 02:31:11 -0500979 to: hi.map(|e| Box::new(e)),
Michael Layzellb78f3b52017-06-04 19:03:03 -0400980 }.into();
981 })
982 )) >>
983 (e)
984 ));
985
David Tolnaybcf26022017-12-25 22:10:52 -0500986 // <and> || <and> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800987 binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400988
David Tolnaybcf26022017-12-25 22:10:52 -0500989 // <compare> && <compare> ...
David Tolnayf8db7ba2017-11-11 22:52:16 -0800990 binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And));
Michael Layzellb78f3b52017-06-04 19:03:03 -0400991
David Tolnaybcf26022017-12-25 22:10:52 -0500992 // <bitor> == <bitor> ...
993 // <bitor> != <bitor> ...
994 // <bitor> >= <bitor> ...
995 // <bitor> <= <bitor> ...
996 // <bitor> > <bitor> ...
997 // <bitor> < <bitor> ...
998 //
999 // NOTE: This operator appears to be parsed as left-associative, but errors
1000 // if it is used in a non-associative manner.
David Tolnay51382052017-12-27 13:46:21 -05001001 binop!(
1002 compare_expr,
1003 bitor_expr,
1004 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001005 punct!(==) => { BinOp::Eq }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001006 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001007 punct!(!=) => { BinOp::Ne }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001008 |
1009 // must be above Lt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001010 punct!(<=) => { BinOp::Le }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001011 |
1012 // must be above Gt
David Tolnayf8db7ba2017-11-11 22:52:16 -08001013 punct!(>=) => { BinOp::Ge }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001014 |
Michael Layzell6a5a1642017-06-04 19:35:15 -04001015 do_parse!(
1016 // Make sure that we don't eat the < part of a <- operator
David Tolnayf8db7ba2017-11-11 22:52:16 -08001017 not!(punct!(<-)) >>
1018 t: punct!(<) >>
Michael Layzell6a5a1642017-06-04 19:35:15 -04001019 (BinOp::Lt(t))
1020 )
Michael Layzellb78f3b52017-06-04 19:03:03 -04001021 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001022 punct!(>) => { BinOp::Gt }
David Tolnay51382052017-12-27 13:46:21 -05001023 )
1024 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001025
David Tolnaybcf26022017-12-25 22:10:52 -05001026 // <bitxor> | <bitxor> ...
David Tolnay51382052017-12-27 13:46:21 -05001027 binop!(
1028 bitor_expr,
1029 bitxor_expr,
1030 do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t)))
1031 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001032
David Tolnaybcf26022017-12-25 22:10:52 -05001033 // <bitand> ^ <bitand> ...
David Tolnay51382052017-12-27 13:46:21 -05001034 binop!(
1035 bitxor_expr,
1036 bitand_expr,
1037 do_parse!(
1038 // NOTE: Make sure we aren't looking at ^=.
1039 not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t))
1040 )
1041 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001042
David Tolnaybcf26022017-12-25 22:10:52 -05001043 // <shift> & <shift> ...
David Tolnay51382052017-12-27 13:46:21 -05001044 binop!(
1045 bitand_expr,
1046 shift_expr,
1047 do_parse!(
1048 // NOTE: Make sure we aren't looking at && or &=.
1049 not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t))
1050 )
1051 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001052
David Tolnaybcf26022017-12-25 22:10:52 -05001053 // <arith> << <arith> ...
1054 // <arith> >> <arith> ...
David Tolnay51382052017-12-27 13:46:21 -05001055 binop!(
1056 shift_expr,
1057 arith_expr,
1058 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001059 punct!(<<) => { BinOp::Shl }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001060 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001061 punct!(>>) => { BinOp::Shr }
David Tolnay51382052017-12-27 13:46:21 -05001062 )
1063 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001064
David Tolnaybcf26022017-12-25 22:10:52 -05001065 // <term> + <term> ...
1066 // <term> - <term> ...
David Tolnay51382052017-12-27 13:46:21 -05001067 binop!(
1068 arith_expr,
1069 term_expr,
1070 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001071 punct!(+) => { BinOp::Add }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001072 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001073 punct!(-) => { BinOp::Sub }
David Tolnay51382052017-12-27 13:46:21 -05001074 )
1075 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001076
David Tolnaybcf26022017-12-25 22:10:52 -05001077 // <cast> * <cast> ...
1078 // <cast> / <cast> ...
1079 // <cast> % <cast> ...
David Tolnay51382052017-12-27 13:46:21 -05001080 binop!(
1081 term_expr,
1082 cast_expr,
1083 alt!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001084 punct!(*) => { BinOp::Mul }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001085 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001086 punct!(/) => { BinOp::Div }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001087 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001088 punct!(%) => { BinOp::Rem }
David Tolnay51382052017-12-27 13:46:21 -05001089 )
1090 );
Michael Layzellb78f3b52017-06-04 19:03:03 -04001091
David Tolnaybcf26022017-12-25 22:10:52 -05001092 // <unary> as <ty>
1093 // <unary> : <ty>
David Tolnay0cf94f22017-12-28 23:46:26 -05001094 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001095 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001096 mut e: call!(unary_expr, allow_struct, allow_block) >>
1097 many0!(alt!(
1098 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001099 as_: keyword!(as) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001100 // We can't accept `A + B` in cast expressions, as it's
1101 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001102 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001103 ({
1104 e = ExprCast {
David Tolnay8c91b882017-12-28 23:04:32 -05001105 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001106 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001107 as_token: as_,
1108 ty: Box::new(ty),
1109 }.into();
1110 })
1111 )
1112 |
1113 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001114 colon: punct!(:) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001115 // We can't accept `A + B` in cast expressions, as it's
1116 // ambiguous with the + expression.
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001117 ty: call!(Type::without_plus) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001118 ({
1119 e = ExprType {
David Tolnay8c91b882017-12-28 23:04:32 -05001120 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001121 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001122 colon_token: colon,
1123 ty: Box::new(ty),
1124 }.into();
1125 })
1126 )
1127 )) >>
1128 (e)
1129 ));
1130
David Tolnay0cf94f22017-12-28 23:46:26 -05001131 // <unary> as <ty>
1132 #[cfg(not(feature = "full"))]
1133 named!(cast_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
1134 mut e: call!(unary_expr, allow_struct, allow_block) >>
1135 many0!(do_parse!(
1136 as_: keyword!(as) >>
1137 // We can't accept `A + B` in cast expressions, as it's
1138 // ambiguous with the + expression.
1139 ty: call!(Type::without_plus) >>
1140 ({
1141 e = ExprCast {
1142 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001143 expr: Box::new(e),
David Tolnay0cf94f22017-12-28 23:46:26 -05001144 as_token: as_,
1145 ty: Box::new(ty),
1146 }.into();
1147 })
1148 )) >>
1149 (e)
1150 ));
1151
David Tolnaybcf26022017-12-25 22:10:52 -05001152 // <UnOp> <trailer>
1153 // & <trailer>
1154 // &mut <trailer>
1155 // box <trailer>
Michael Layzell734adb42017-06-07 16:58:31 -04001156 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001157 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001158 do_parse!(
1159 op: syn!(UnOp) >>
1160 expr: call!(unary_expr, allow_struct, true) >>
1161 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001162 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001163 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001164 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001165 }.into())
1166 )
1167 |
1168 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001169 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05001170 mutability: option!(keyword!(mut)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001171 expr: call!(unary_expr, allow_struct, true) >>
1172 (ExprAddrOf {
David Tolnay8c91b882017-12-28 23:04:32 -05001173 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001174 and_token: and,
David Tolnay24237fb2017-12-29 02:15:26 -05001175 mutability: mutability,
David Tolnay3bc597f2017-12-31 02:31:11 -05001176 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001177 }.into())
1178 )
1179 |
1180 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001181 box_: keyword!(box) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001182 expr: call!(unary_expr, allow_struct, true) >>
1183 (ExprBox {
David Tolnay8c91b882017-12-28 23:04:32 -05001184 attrs: Vec::new(),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001185 box_token: box_,
David Tolnay3bc597f2017-12-31 02:31:11 -05001186 expr: Box::new(expr),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001187 }.into())
1188 )
1189 |
1190 call!(trailer_expr, allow_struct, allow_block)
1191 ));
1192
Michael Layzell734adb42017-06-07 16:58:31 -04001193 // XXX: This duplication is ugly
1194 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001195 named!(unary_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
Michael Layzell734adb42017-06-07 16:58:31 -04001196 do_parse!(
1197 op: syn!(UnOp) >>
1198 expr: call!(unary_expr, allow_struct, true) >>
1199 (ExprUnary {
David Tolnay8c91b882017-12-28 23:04:32 -05001200 attrs: Vec::new(),
Michael Layzell734adb42017-06-07 16:58:31 -04001201 op: op,
David Tolnay3bc597f2017-12-31 02:31:11 -05001202 expr: Box::new(expr),
Michael Layzell734adb42017-06-07 16:58:31 -04001203 }.into())
1204 )
1205 |
1206 call!(trailer_expr, allow_struct, allow_block)
1207 ));
1208
David Tolnaybcf26022017-12-25 22:10:52 -05001209 // <atom> (..<args>) ...
1210 // <atom> . <ident> (..<args>) ...
1211 // <atom> . <ident> ...
1212 // <atom> . <lit> ...
1213 // <atom> [ <expr> ] ...
1214 // <atom> ? ...
Michael Layzell734adb42017-06-07 16:58:31 -04001215 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001216 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzellb78f3b52017-06-04 19:03:03 -04001217 mut e: call!(atom_expr, allow_struct, allow_block) >>
1218 many0!(alt!(
1219 tap!(args: and_call => {
David Tolnay8875fca2017-12-31 13:52:37 -05001220 let (paren, args) = args;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001221 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001222 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001223 func: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001224 args: args,
1225 paren_token: paren,
1226 }.into();
1227 })
1228 |
1229 tap!(more: and_method_call => {
1230 let mut call = more;
David Tolnay3bc597f2017-12-31 02:31:11 -05001231 call.receiver = Box::new(e);
Michael Layzellb78f3b52017-06-04 19:03:03 -04001232 e = call.into();
1233 })
1234 |
1235 tap!(field: and_field => {
David Tolnay85b69a42017-12-27 20:43:10 -05001236 let (token, member) = field;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001237 e = ExprField {
David Tolnay8c91b882017-12-28 23:04:32 -05001238 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001239 base: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001240 dot_token: token,
David Tolnay85b69a42017-12-27 20:43:10 -05001241 member: member,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001242 }.into();
1243 })
1244 |
1245 tap!(i: and_index => {
David Tolnay8875fca2017-12-31 13:52:37 -05001246 let (bracket, i) = i;
Michael Layzellb78f3b52017-06-04 19:03:03 -04001247 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001248 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001249 expr: Box::new(e),
David Tolnay8875fca2017-12-31 13:52:37 -05001250 bracket_token: bracket,
Michael Layzellb78f3b52017-06-04 19:03:03 -04001251 index: Box::new(i),
1252 }.into();
1253 })
1254 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08001255 tap!(question: punct!(?) => {
Michael Layzellb78f3b52017-06-04 19:03:03 -04001256 e = ExprTry {
David Tolnay8c91b882017-12-28 23:04:32 -05001257 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001258 expr: Box::new(e),
Michael Layzellb78f3b52017-06-04 19:03:03 -04001259 question_token: question,
1260 }.into();
1261 })
1262 )) >>
1263 (e)
1264 ));
1265
Michael Layzell734adb42017-06-07 16:58:31 -04001266 // XXX: Duplication == ugly
1267 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001268 named!(trailer_expr(allow_struct: bool, allow_block: bool) -> Expr, do_parse!(
Michael Layzell734adb42017-06-07 16:58:31 -04001269 mut e: call!(atom_expr, allow_struct, allow_block) >>
1270 many0!(alt!(
1271 tap!(args: and_call => {
Michael Layzell734adb42017-06-07 16:58:31 -04001272 e = ExprCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001273 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001274 func: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001275 paren_token: args.0,
1276 args: args.1,
Michael Layzell734adb42017-06-07 16:58:31 -04001277 }.into();
1278 })
1279 |
1280 tap!(i: and_index => {
Michael Layzell734adb42017-06-07 16:58:31 -04001281 e = ExprIndex {
David Tolnay8c91b882017-12-28 23:04:32 -05001282 attrs: Vec::new(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001283 expr: Box::new(e),
David Tolnaye3d41b72017-12-31 15:24:00 -05001284 bracket_token: i.0,
1285 index: Box::new(i.1),
Michael Layzell734adb42017-06-07 16:58:31 -04001286 }.into();
1287 })
1288 )) >>
1289 (e)
1290 ));
1291
David Tolnaya454c8f2018-01-07 01:01:10 -08001292 // Parse all atomic expressions which don't have to worry about precedence
David Tolnaybcf26022017-12-25 22:10:52 -05001293 // interactions, as they are fully contained.
Michael Layzell734adb42017-06-07 16:58:31 -04001294 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001295 named!(atom_expr(allow_struct: bool, allow_block: bool) -> Expr, alt!(
1296 syn!(ExprGroup) => { Expr::Group } // must be placed first
Michael Layzell93c36282017-06-04 20:43:14 -04001297 |
David Tolnay8c91b882017-12-28 23:04:32 -05001298 syn!(ExprLit) => { Expr::Lit } // must be before expr_struct
Michael Layzellb78f3b52017-06-04 19:03:03 -04001299 |
1300 // must be before expr_path
David Tolnaydc03aec2017-12-30 01:54:18 -05001301 cond_reduce!(allow_struct, syn!(ExprStruct)) => { Expr::Struct }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001302 |
David Tolnay8c91b882017-12-28 23:04:32 -05001303 syn!(ExprParen) => { Expr::Paren } // must be before expr_tup
Michael Layzellb78f3b52017-06-04 19:03:03 -04001304 |
David Tolnay8c91b882017-12-28 23:04:32 -05001305 syn!(ExprMacro) => { Expr::Macro } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001306 |
1307 call!(expr_break, allow_struct) // must be before expr_path
1308 |
David Tolnay8c91b882017-12-28 23:04:32 -05001309 syn!(ExprContinue) => { Expr::Continue } // must be before expr_path
Michael Layzellb78f3b52017-06-04 19:03:03 -04001310 |
1311 call!(expr_ret, allow_struct) // must be before expr_path
1312 |
David Tolnay8c91b882017-12-28 23:04:32 -05001313 syn!(ExprArray) => { Expr::Array }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001314 |
David Tolnay8c91b882017-12-28 23:04:32 -05001315 syn!(ExprTuple) => { Expr::Tuple }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001316 |
David Tolnay8c91b882017-12-28 23:04:32 -05001317 syn!(ExprIf) => { Expr::If }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001318 |
David Tolnay8c91b882017-12-28 23:04:32 -05001319 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001320 |
David Tolnay8c91b882017-12-28 23:04:32 -05001321 syn!(ExprWhile) => { Expr::While }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001322 |
David Tolnay8c91b882017-12-28 23:04:32 -05001323 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001324 |
David Tolnay8c91b882017-12-28 23:04:32 -05001325 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001326 |
David Tolnay8c91b882017-12-28 23:04:32 -05001327 syn!(ExprLoop) => { Expr::Loop }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001328 |
David Tolnay8c91b882017-12-28 23:04:32 -05001329 syn!(ExprMatch) => { Expr::Match }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001330 |
David Tolnay8c91b882017-12-28 23:04:32 -05001331 syn!(ExprCatch) => { Expr::Catch }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001332 |
David Tolnay8c91b882017-12-28 23:04:32 -05001333 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001334 |
David Tolnay8c91b882017-12-28 23:04:32 -05001335 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001336 |
Michael Layzellb78f3b52017-06-04 19:03:03 -04001337 call!(expr_closure, allow_struct)
1338 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001339 cond_reduce!(allow_block, syn!(ExprBlock)) => { Expr::Block }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001340 |
1341 // NOTE: This is the prefix-form of range
1342 call!(expr_range, allow_struct)
1343 |
David Tolnay8c91b882017-12-28 23:04:32 -05001344 syn!(ExprPath) => { Expr::Path }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001345 |
David Tolnay8c91b882017-12-28 23:04:32 -05001346 syn!(ExprRepeat) => { Expr::Repeat }
Michael Layzellb78f3b52017-06-04 19:03:03 -04001347 ));
1348
Michael Layzell734adb42017-06-07 16:58:31 -04001349 #[cfg(not(feature = "full"))]
David Tolnay8c91b882017-12-28 23:04:32 -05001350 named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> Expr, alt!(
David Tolnaye98775f2017-12-28 23:17:00 -05001351 syn!(ExprLit) => { Expr::Lit }
Michael Layzell734adb42017-06-07 16:58:31 -04001352 |
David Tolnay8c91b882017-12-28 23:04:32 -05001353 syn!(ExprPath) => { Expr::Path }
Michael Layzell734adb42017-06-07 16:58:31 -04001354 ));
1355
Michael Layzell734adb42017-06-07 16:58:31 -04001356 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04001357 named!(expr_nosemi -> Expr, map!(alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001358 syn!(ExprIf) => { Expr::If }
Michael Layzell35418782017-06-07 09:20:25 -04001359 |
David Tolnay8c91b882017-12-28 23:04:32 -05001360 syn!(ExprIfLet) => { Expr::IfLet }
Michael Layzell35418782017-06-07 09:20:25 -04001361 |
David Tolnay8c91b882017-12-28 23:04:32 -05001362 syn!(ExprWhile) => { Expr::While }
Michael Layzell35418782017-06-07 09:20:25 -04001363 |
David Tolnay8c91b882017-12-28 23:04:32 -05001364 syn!(ExprWhileLet) => { Expr::WhileLet }
Michael Layzell35418782017-06-07 09:20:25 -04001365 |
David Tolnay8c91b882017-12-28 23:04:32 -05001366 syn!(ExprForLoop) => { Expr::ForLoop }
Michael Layzell35418782017-06-07 09:20:25 -04001367 |
David Tolnay8c91b882017-12-28 23:04:32 -05001368 syn!(ExprLoop) => { Expr::Loop }
Michael Layzell35418782017-06-07 09:20:25 -04001369 |
David Tolnay8c91b882017-12-28 23:04:32 -05001370 syn!(ExprMatch) => { Expr::Match }
Michael Layzell35418782017-06-07 09:20:25 -04001371 |
David Tolnay8c91b882017-12-28 23:04:32 -05001372 syn!(ExprCatch) => { Expr::Catch }
Michael Layzell35418782017-06-07 09:20:25 -04001373 |
David Tolnay8c91b882017-12-28 23:04:32 -05001374 syn!(ExprYield) => { Expr::Yield }
Alex Crichtonfe110462017-06-01 12:49:27 -07001375 |
David Tolnay8c91b882017-12-28 23:04:32 -05001376 syn!(ExprUnsafe) => { Expr::Unsafe }
Nika Layzell640832a2017-12-04 13:37:09 -05001377 |
David Tolnay8c91b882017-12-28 23:04:32 -05001378 syn!(ExprBlock) => { Expr::Block }
Michael Layzell35418782017-06-07 09:20:25 -04001379 ), Expr::from));
1380
David Tolnay8c91b882017-12-28 23:04:32 -05001381 impl Synom for ExprLit {
1382 named!(parse -> Self, do_parse!(
1383 lit: syn!(Lit) >>
1384 (ExprLit {
1385 attrs: Vec::new(),
1386 lit: lit,
1387 })
1388 ));
1389 }
1390
1391 #[cfg(feature = "full")]
1392 impl Synom for ExprMacro {
1393 named!(parse -> Self, do_parse!(
1394 mac: syn!(Macro) >>
1395 (ExprMacro {
1396 attrs: Vec::new(),
1397 mac: mac,
1398 })
1399 ));
1400 }
1401
David Tolnaye98775f2017-12-28 23:17:00 -05001402 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001403 impl Synom for ExprGroup {
1404 named!(parse -> Self, do_parse!(
1405 e: grouped!(syn!(Expr)) >>
1406 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001407 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001408 expr: Box::new(e.1),
1409 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001410 })
Michael Layzell93c36282017-06-04 20:43:14 -04001411 ));
1412 }
1413
David Tolnaye98775f2017-12-28 23:17:00 -05001414 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001415 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001416 named!(parse -> Self, do_parse!(
1417 e: parens!(syn!(Expr)) >>
1418 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001419 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001420 paren_token: e.0,
1421 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001422 })
Michael Layzell92639a52017-06-01 00:07:44 -04001423 ));
Alex Crichton954046c2017-05-30 21:49:42 -07001424 }
David Tolnay89e05672016-10-02 14:39:42 -07001425
Michael Layzell734adb42017-06-07 16:58:31 -04001426 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001427 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001428 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001429 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001430 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001431 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001432 bracket_token: elems.0,
1433 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001434 })
1435 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001436
1437 fn description() -> Option<&'static str> {
1438 Some("array")
1439 }
Alex Crichton954046c2017-05-30 21:49:42 -07001440 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001441
David Tolnayf2cfd722017-12-31 18:02:51 -05001442 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1443 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001444
Michael Layzell734adb42017-06-07 16:58:31 -04001445 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001446 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001447 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001448 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001449 turbofish: option!(tuple!(
1450 punct!(::),
1451 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001452 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001453 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001454 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001455 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001456 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001457 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001458 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001459 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001460 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1461 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001462 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001463
Alex Crichton954046c2017-05-30 21:49:42 -07001464 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001465 turbofish: turbofish.map(|fish| MethodTurbofish {
1466 colon2_token: fish.0,
1467 lt_token: fish.1,
1468 args: fish.2,
1469 gt_token: fish.3,
1470 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001471 args: args.1,
1472 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001473 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001474 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001475 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001476 ));
1477
Michael Layzell734adb42017-06-07 16:58:31 -04001478 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001479 impl Synom for GenericMethodArgument {
1480 // TODO parse const generics as well
1481 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
1482 }
1483
1484 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001485 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001486 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001487 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001488 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001489 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001490 elems: elems.1,
1491 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001492 })
1493 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001494
1495 fn description() -> Option<&'static str> {
1496 Some("tuple")
1497 }
Alex Crichton954046c2017-05-30 21:49:42 -07001498 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001499
Michael Layzell734adb42017-06-07 16:58:31 -04001500 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001501 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001502 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001503 if_: keyword!(if) >>
1504 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001505 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001506 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001507 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001508 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001509 else_block: option!(else_block) >>
1510 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001511 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001512 pat: Box::new(pat),
1513 let_token: let_,
1514 eq_token: eq,
1515 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001516 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001517 brace_token: then_block.0,
1518 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001519 },
1520 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001521 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001522 })
1523 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001524
1525 fn description() -> Option<&'static str> {
1526 Some("`if let` expression")
1527 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001528 }
1529
Michael Layzell734adb42017-06-07 16:58:31 -04001530 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001531 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001532 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001533 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001534 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001535 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001536 else_block: option!(else_block) >>
1537 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001538 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001539 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001540 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001541 brace_token: then_block.0,
1542 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001543 },
1544 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001545 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001546 })
1547 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001548
1549 fn description() -> Option<&'static str> {
1550 Some("`if` expression")
1551 }
Alex Crichton954046c2017-05-30 21:49:42 -07001552 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001553
Michael Layzell734adb42017-06-07 16:58:31 -04001554 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001555 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001556 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001557 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001558 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001559 |
David Tolnay8c91b882017-12-28 23:04:32 -05001560 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001561 |
1562 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001563 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001564 (Expr::Block(ExprBlock {
1565 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001566 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001567 brace_token: else_block.0,
1568 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001569 },
1570 }))
David Tolnay939766a2016-09-23 23:48:12 -07001571 )
Alex Crichton954046c2017-05-30 21:49:42 -07001572 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001573 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001574 ));
1575
Michael Layzell734adb42017-06-07 16:58:31 -04001576 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001577 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001578 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001579 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001580 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001581 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001582 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001583 expr: expr_no_struct >>
1584 loop_block: syn!(Block) >>
1585 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001586 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001587 for_token: for_,
1588 in_token: in_,
1589 pat: Box::new(pat),
1590 expr: Box::new(expr),
1591 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001592 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001593 })
1594 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001595
1596 fn description() -> Option<&'static str> {
1597 Some("`for` loop")
1598 }
Alex Crichton954046c2017-05-30 21:49:42 -07001599 }
Gregory Katze5f35682016-09-27 14:20:55 -04001600
Michael Layzell734adb42017-06-07 16:58:31 -04001601 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001602 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001603 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001604 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001605 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001606 loop_block: syn!(Block) >>
1607 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001608 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001609 loop_token: loop_,
1610 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001611 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001612 })
1613 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001614
1615 fn description() -> Option<&'static str> {
1616 Some("`loop`")
1617 }
Alex Crichton954046c2017-05-30 21:49:42 -07001618 }
1619
Michael Layzell734adb42017-06-07 16:58:31 -04001620 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001621 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001622 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001623 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001624 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001625 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001626 (ExprMatch {
1627 attrs: Vec::new(),
1628 expr: Box::new(obj),
1629 match_token: match_,
1630 brace_token: res.0,
1631 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001632 })
1633 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001634
1635 fn description() -> Option<&'static str> {
1636 Some("`match` expression")
1637 }
Alex Crichton954046c2017-05-30 21:49:42 -07001638 }
David Tolnay1978c672016-10-27 22:05:52 -07001639
Michael Layzell734adb42017-06-07 16:58:31 -04001640 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001641 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001642 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001643 do_: keyword!(do) >>
1644 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001645 catch_block: syn!(Block) >>
1646 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001647 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001648 block: catch_block,
1649 do_token: do_,
1650 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001651 })
Michael Layzell92639a52017-06-01 00:07:44 -04001652 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001653
1654 fn description() -> Option<&'static str> {
1655 Some("`catch` expression")
1656 }
Alex Crichton954046c2017-05-30 21:49:42 -07001657 }
Arnavion02ef13f2017-04-25 00:54:31 -07001658
Michael Layzell734adb42017-06-07 16:58:31 -04001659 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001660 impl Synom for ExprYield {
1661 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001662 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001663 expr: option!(syn!(Expr)) >>
1664 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001665 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001666 yield_token: yield_,
1667 expr: expr.map(Box::new),
1668 })
1669 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001670
1671 fn description() -> Option<&'static str> {
1672 Some("`yield` expression")
1673 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001674 }
1675
1676 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001677 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001678 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001679 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001680 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001681 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1682 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001683 body: do_parse!(
1684 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001685 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1686 true => alt!(
1687 input_end!() => { |_| None }
1688 |
1689 punct!(,) => { Some }
1690 )
Alex Crichton03b30272017-08-28 09:35:24 -07001691 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001692 false => option!(punct!(,))
1693 ) >>
1694 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001695 ) >>
1696 (Arm {
1697 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001698 attrs: attrs,
1699 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001700 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001701 body: Box::new(body.0),
1702 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001703 })
1704 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001705
1706 fn description() -> Option<&'static str> {
1707 Some("`match` arm")
1708 }
Alex Crichton954046c2017-05-30 21:49:42 -07001709 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001710
Michael Layzell734adb42017-06-07 16:58:31 -04001711 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001712 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001713 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001714 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001715 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001716 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001717 ret_and_body: alt!(
1718 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001719 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001720 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001721 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001722 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001723 Expr::Block(ExprBlock {
1724 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001725 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001726 }))
David Tolnay89e05672016-10-02 14:39:42 -07001727 )
1728 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001729 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001730 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001731 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001732 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001733 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001734 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001735 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001736 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001737 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001738 body: Box::new(ret_and_body.1),
1739 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001740 ));
1741
Michael Layzell734adb42017-06-07 16:58:31 -04001742 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001743 named!(fn_arg -> FnArg, do_parse!(
1744 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001745 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001746 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001747 if let Some((colon, ty)) = ty {
1748 FnArg::Captured(ArgCaptured {
1749 pat: pat,
1750 colon_token: colon,
1751 ty: ty,
1752 })
1753 } else {
1754 FnArg::Inferred(pat)
1755 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001756 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001757 ));
1758
Michael Layzell734adb42017-06-07 16:58:31 -04001759 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001760 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001761 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001762 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001763 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001764 cond: expr_no_struct >>
1765 while_block: syn!(Block) >>
1766 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001767 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001768 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001769 cond: Box::new(cond),
1770 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001771 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001772 })
1773 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001774
1775 fn description() -> Option<&'static str> {
1776 Some("`while` expression")
1777 }
Alex Crichton954046c2017-05-30 21:49:42 -07001778 }
1779
Michael Layzell734adb42017-06-07 16:58:31 -04001780 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001781 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001782 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001783 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001784 while_: keyword!(while) >>
1785 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001786 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001787 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001788 value: expr_no_struct >>
1789 while_block: syn!(Block) >>
1790 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001791 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001792 eq_token: eq,
1793 let_token: let_,
1794 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001795 pat: Box::new(pat),
1796 expr: Box::new(value),
1797 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001798 label: label,
1799 })
1800 ));
1801 }
1802
1803 #[cfg(feature = "full")]
1804 impl Synom for Label {
1805 named!(parse -> Self, do_parse!(
1806 name: syn!(Lifetime) >>
1807 colon: punct!(:) >>
1808 (Label {
1809 name: name,
1810 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001811 })
1812 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001813
1814 fn description() -> Option<&'static str> {
1815 Some("`while let` expression")
1816 }
Alex Crichton954046c2017-05-30 21:49:42 -07001817 }
1818
Michael Layzell734adb42017-06-07 16:58:31 -04001819 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001820 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001821 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001822 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001823 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001824 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001825 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001826 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001827 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001828 })
1829 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001830
1831 fn description() -> Option<&'static str> {
1832 Some("`continue`")
1833 }
Alex Crichton954046c2017-05-30 21:49:42 -07001834 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001835
Michael Layzell734adb42017-06-07 16:58:31 -04001836 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001837 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001838 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001839 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001840 // We can't allow blocks after a `break` expression when we wouldn't
1841 // allow structs, as this expression is ambiguous.
1842 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001843 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001844 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001845 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001846 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001847 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001848 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001849 ));
1850
Michael Layzell734adb42017-06-07 16:58:31 -04001851 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001852 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001853 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001854 // NOTE: return is greedy and eats blocks after it even when in a
1855 // position where structs are not allowed, such as in if statement
1856 // conditions. For example:
1857 //
David Tolnaybcf26022017-12-25 22:10:52 -05001858 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001859 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001860 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001861 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001862 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001863 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001864 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001865 ));
1866
Michael Layzell734adb42017-06-07 16:58:31 -04001867 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001868 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001869 named!(parse -> Self, do_parse!(
1870 path: syn!(Path) >>
1871 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001872 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001873 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
1874 dots: punct!(..) >>
1875 base: syn!(Expr) >>
1876 (dots, base)
1877 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001878 (fields, base)
1879 )) >>
1880 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001881 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04001882 let (dots, rest) = match base.and_then(|b| b) {
1883 Some((dots, base)) => (Some(dots), Some(base)),
1884 None => (None, None),
1885 };
1886 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001887 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001888 brace_token: brace,
1889 path: path,
1890 fields: fields,
1891 dot2_token: dots,
1892 rest: rest.map(Box::new),
1893 }
1894 })
1895 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001896
1897 fn description() -> Option<&'static str> {
1898 Some("struct literal expression")
1899 }
Alex Crichton954046c2017-05-30 21:49:42 -07001900 }
1901
Michael Layzell734adb42017-06-07 16:58:31 -04001902 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001903 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001904 named!(parse -> Self, alt!(
1905 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001906 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001907 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001908 value: syn!(Expr) >>
1909 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001910 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001911 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001912 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001913 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001914 })
Michael Layzell92639a52017-06-01 00:07:44 -04001915 )
1916 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001917 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001918 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001919 expr: Expr::Path(ExprPath {
1920 attrs: Vec::new(),
1921 qself: None,
1922 path: name.into(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001923 }),
Michael Layzell92639a52017-06-01 00:07:44 -04001924 attrs: Vec::new(),
1925 colon_token: None,
1926 })
1927 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001928
1929 fn description() -> Option<&'static str> {
1930 Some("field-value pair: `field: value`")
1931 }
Alex Crichton954046c2017-05-30 21:49:42 -07001932 }
David Tolnay055a7042016-10-02 19:23:54 -07001933
Michael Layzell734adb42017-06-07 16:58:31 -04001934 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001935 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001936 named!(parse -> Self, do_parse!(
1937 data: brackets!(do_parse!(
1938 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001939 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001940 times: syn!(Expr) >>
1941 (value, semi, times)
1942 )) >>
1943 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001944 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001945 expr: Box::new((data.1).0),
David Tolnay84d80442018-01-07 01:03:20 -08001946 len: Box::new((data.1).2),
David Tolnay8875fca2017-12-31 13:52:37 -05001947 bracket_token: data.0,
1948 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001949 })
1950 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001951
1952 fn description() -> Option<&'static str> {
1953 Some("repeated array literal: `[val; N]`")
1954 }
Alex Crichton954046c2017-05-30 21:49:42 -07001955 }
David Tolnay055a7042016-10-02 19:23:54 -07001956
Michael Layzell734adb42017-06-07 16:58:31 -04001957 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001958 impl Synom for ExprUnsafe {
1959 named!(parse -> Self, do_parse!(
1960 unsafe_: keyword!(unsafe) >>
1961 b: syn!(Block) >>
1962 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001963 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001964 unsafe_token: unsafe_,
1965 block: b,
1966 })
1967 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001968
1969 fn description() -> Option<&'static str> {
1970 Some("unsafe block: `unsafe { .. }`")
1971 }
Nika Layzell640832a2017-12-04 13:37:09 -05001972 }
1973
1974 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001975 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04001976 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04001977 b: syn!(Block) >>
1978 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05001979 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001980 block: b,
1981 })
1982 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001983
1984 fn description() -> Option<&'static str> {
1985 Some("block: `{ .. }`")
1986 }
Alex Crichton954046c2017-05-30 21:49:42 -07001987 }
David Tolnay89e05672016-10-02 14:39:42 -07001988
Michael Layzell734adb42017-06-07 16:58:31 -04001989 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001990 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07001991 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001992 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001993 (ExprRange {
1994 attrs: Vec::new(),
1995 from: None,
1996 to: hi.map(Box::new),
1997 limits: limits,
1998 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07001999 ));
2000
Michael Layzell734adb42017-06-07 16:58:31 -04002001 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002002 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002003 named!(parse -> Self, alt!(
2004 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002005 punct!(..=) => { RangeLimits::Closed }
2006 |
2007 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002008 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002009 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002010 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002011 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002012
2013 fn description() -> Option<&'static str> {
2014 Some("range limit: `..`, `...` or `..=`")
2015 }
Alex Crichton954046c2017-05-30 21:49:42 -07002016 }
David Tolnay438c9052016-10-07 23:24:48 -07002017
Alex Crichton954046c2017-05-30 21:49:42 -07002018 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002019 named!(parse -> Self, do_parse!(
2020 pair: qpath >>
2021 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002022 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002023 qself: pair.0,
2024 path: pair.1,
2025 })
2026 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002027
2028 fn description() -> Option<&'static str> {
2029 Some("path: `a::b::c`")
2030 }
Alex Crichton954046c2017-05-30 21:49:42 -07002031 }
David Tolnay42602292016-10-01 22:25:45 -07002032
Michael Layzell734adb42017-06-07 16:58:31 -04002033 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002034 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002035
David Tolnay8875fca2017-12-31 13:52:37 -05002036 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002037
Michael Layzell734adb42017-06-07 16:58:31 -04002038 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002039 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002040 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002041 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002042 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002043 brace_token: stmts.0,
2044 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002045 })
2046 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002047
2048 fn description() -> Option<&'static str> {
2049 Some("block: `{ .. }`")
2050 }
Alex Crichton954046c2017-05-30 21:49:42 -07002051 }
David Tolnay939766a2016-09-23 23:48:12 -07002052
Michael Layzell734adb42017-06-07 16:58:31 -04002053 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002054 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002055 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002056 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002057 mut standalone: many0!(do_parse!(
2058 stmt: syn!(Stmt) >>
2059 many0!(punct!(;)) >>
2060 (stmt)
2061 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002062 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002063 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002064 mut e: syn!(Expr) >>
2065 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002066 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002067 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002068 })
2069 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002070 (match last {
2071 None => standalone,
2072 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002073 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002074 standalone
2075 }
2076 })
2077 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002078 }
2079
Michael Layzell734adb42017-06-07 16:58:31 -04002080 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002081 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002082 named!(parse -> Self, alt!(
2083 stmt_mac
2084 |
2085 stmt_local
2086 |
2087 stmt_item
2088 |
Michael Layzell35418782017-06-07 09:20:25 -04002089 stmt_blockexpr
2090 |
Michael Layzell92639a52017-06-01 00:07:44 -04002091 stmt_expr
2092 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002093
2094 fn description() -> Option<&'static str> {
2095 Some("statement")
2096 }
Alex Crichton954046c2017-05-30 21:49:42 -07002097 }
David Tolnay939766a2016-09-23 23:48:12 -07002098
Michael Layzell734adb42017-06-07 16:58:31 -04002099 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002100 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002101 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002102 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002103 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002104 // Only parse braces here; paren and bracket will get parsed as
2105 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002106 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002107 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002108 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002109 attrs: attrs,
2110 ident: None,
2111 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002112 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002113 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002114 delimiter: MacroDelimiter::Brace(data.0),
2115 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002116 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002117 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002118 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002119 ));
2120
Michael Layzell734adb42017-06-07 16:58:31 -04002121 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002122 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002123 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002124 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002125 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002126 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002127 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2128 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002129 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002130 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002131 let_token: let_,
2132 pat: Box::new(pat),
2133 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2134 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2135 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002136 }))
David Tolnay191e0582016-10-02 18:31:09 -07002137 ));
2138
Michael Layzell734adb42017-06-07 16:58:31 -04002139 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002140 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002141
Michael Layzell734adb42017-06-07 16:58:31 -04002142 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002143 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002144 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002145 mut e: expr_nosemi >>
2146 // If the next token is a `.` or a `?` it is special-cased to parse as
2147 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002148 not!(punct!(.)) >>
2149 not!(punct!(?)) >>
2150 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002151 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002152 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002153 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002154 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002155 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002156 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002157 }
2158 })
2159 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002160
Michael Layzell734adb42017-06-07 16:58:31 -04002161 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002162 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002163 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002164 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002165 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002166 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002167 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002168 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002169 })
David Tolnay939766a2016-09-23 23:48:12 -07002170 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002171
Michael Layzell734adb42017-06-07 16:58:31 -04002172 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002173 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002174 named!(parse -> Self, alt!(
2175 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2176 |
2177 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2178 |
2179 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2180 |
2181 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2182 |
2183 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2184 |
David Tolnay323279a2017-12-29 11:26:32 -05002185 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002186 |
2187 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2188 |
2189 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2190 |
2191 syn!(PatPath) => { Pat::Path }
2192 |
2193 syn!(PatTuple) => { Pat::Tuple }
2194 |
2195 syn!(PatRef) => { Pat::Ref }
2196 |
2197 syn!(PatSlice) => { Pat::Slice }
2198 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002199
2200 fn description() -> Option<&'static str> {
2201 Some("pattern")
2202 }
Alex Crichton954046c2017-05-30 21:49:42 -07002203 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002204
Michael Layzell734adb42017-06-07 16:58:31 -04002205 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002206 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002207 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002208 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002209 |u| PatWild { underscore_token: u }
2210 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002211
2212 fn description() -> Option<&'static str> {
2213 Some("wild pattern: `_`")
2214 }
Alex Crichton954046c2017-05-30 21:49:42 -07002215 }
David Tolnay84aa0752016-10-02 23:01:13 -07002216
Michael Layzell734adb42017-06-07 16:58:31 -04002217 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002218 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002219 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002220 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002221 pat: syn!(Pat) >>
2222 (PatBox {
2223 pat: Box::new(pat),
2224 box_token: boxed,
2225 })
2226 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002227
2228 fn description() -> Option<&'static str> {
2229 Some("box pattern")
2230 }
Alex Crichton954046c2017-05-30 21:49:42 -07002231 }
2232
Michael Layzell734adb42017-06-07 16:58:31 -04002233 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002234 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002235 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002236 by_ref: option!(keyword!(ref)) >>
2237 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002238 name: alt!(
2239 syn!(Ident)
2240 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002241 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002242 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002243 not!(punct!(<)) >>
2244 not!(punct!(::)) >>
2245 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002246 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002247 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002248 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002249 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002250 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002251 })
2252 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002253
2254 fn description() -> Option<&'static str> {
2255 Some("pattern identifier binding")
2256 }
Alex Crichton954046c2017-05-30 21:49:42 -07002257 }
2258
Michael Layzell734adb42017-06-07 16:58:31 -04002259 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002260 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002261 named!(parse -> Self, do_parse!(
2262 path: syn!(Path) >>
2263 tuple: syn!(PatTuple) >>
2264 (PatTupleStruct {
2265 path: path,
2266 pat: tuple,
2267 })
2268 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002269
2270 fn description() -> Option<&'static str> {
2271 Some("tuple struct pattern")
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 PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002277 named!(parse -> Self, do_parse!(
2278 path: syn!(Path) >>
2279 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002280 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002281 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002282 (fields, base)
2283 )) >>
2284 (PatStruct {
2285 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002286 fields: (data.1).0,
2287 brace_token: data.0,
2288 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002289 })
2290 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002291
2292 fn description() -> Option<&'static str> {
2293 Some("struct pattern")
2294 }
Alex Crichton954046c2017-05-30 21:49:42 -07002295 }
2296
Michael Layzell734adb42017-06-07 16:58:31 -04002297 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002298 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002299 named!(parse -> Self, alt!(
2300 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002301 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002302 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002303 pat: syn!(Pat) >>
2304 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002305 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002306 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002307 attrs: Vec::new(),
2308 colon_token: Some(colon),
2309 })
2310 )
2311 |
2312 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002313 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002314 by_ref: option!(keyword!(ref)) >>
2315 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002316 ident: syn!(Ident) >>
2317 ({
2318 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002319 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002320 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002321 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002322 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002323 }.into();
2324 if let Some(boxed) = boxed {
2325 pat = PatBox {
2326 pat: Box::new(pat),
2327 box_token: boxed,
2328 }.into();
2329 }
2330 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002331 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002332 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002333 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002334 colon_token: None,
2335 }
2336 })
2337 )
2338 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002339
2340 fn description() -> Option<&'static str> {
2341 Some("field pattern")
2342 }
Alex Crichton954046c2017-05-30 21:49:42 -07002343 }
2344
Michael Layzell734adb42017-06-07 16:58:31 -04002345 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002346 impl Synom for Member {
2347 named!(parse -> Self, alt!(
2348 syn!(Ident) => { Member::Named }
2349 |
2350 syn!(Index) => { Member::Unnamed }
2351 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002352
2353 fn description() -> Option<&'static str> {
2354 Some("field member")
2355 }
David Tolnay85b69a42017-12-27 20:43:10 -05002356 }
2357
2358 #[cfg(feature = "full")]
2359 impl Synom for Index {
2360 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002361 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002362 ({
David Tolnay360efd22018-01-04 23:35:26 -08002363 if let IntSuffix::None = lit.suffix() {
2364 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002365 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002366 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002367 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002368 })
David Tolnay85b69a42017-12-27 20:43:10 -05002369 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002370
2371 fn description() -> Option<&'static str> {
2372 Some("field index")
2373 }
David Tolnay85b69a42017-12-27 20:43:10 -05002374 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002375
Michael Layzell734adb42017-06-07 16:58:31 -04002376 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002377 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002378 named!(parse -> Self, map!(
2379 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002380 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002381 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002382
2383 fn description() -> Option<&'static str> {
2384 Some("path pattern")
2385 }
Alex Crichton954046c2017-05-30 21:49:42 -07002386 }
David Tolnay9636c052016-10-02 17:11:17 -07002387
Michael Layzell734adb42017-06-07 16:58:31 -04002388 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002389 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002390 named!(parse -> Self, do_parse!(
2391 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002392 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002393 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2394 tuple!(punct!(..), option!(punct!(,)))
2395 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002396 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002397 Some((_, Some(_))) => true,
2398 _ => false,
2399 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002400 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002401 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002402 )) >>
2403 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002404 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002405 let (dotdot, trailing) = match dotdot {
2406 Some((a, b)) => (Some(a), Some(b)),
2407 None => (None, None),
2408 };
2409 PatTuple {
2410 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002411 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002412 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002413 comma_token: trailing.unwrap_or_default(),
2414 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002415 }
2416 })
2417 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002418
2419 fn description() -> Option<&'static str> {
2420 Some("tuple pattern")
2421 }
Alex Crichton954046c2017-05-30 21:49:42 -07002422 }
David Tolnayfbb73232016-10-03 01:00:06 -07002423
Michael Layzell734adb42017-06-07 16:58:31 -04002424 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002425 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002426 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002427 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002428 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002429 pat: syn!(Pat) >>
2430 (PatRef {
2431 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002432 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002433 and_token: and,
2434 })
2435 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002436
2437 fn description() -> Option<&'static str> {
2438 Some("reference pattern")
2439 }
Alex Crichton954046c2017-05-30 21:49:42 -07002440 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002441
Michael Layzell734adb42017-06-07 16:58:31 -04002442 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002443 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002444 named!(parse -> Self, do_parse!(
2445 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002446 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002447 return parse_error(); // these need to be parsed by pat_path
2448 } else {
2449 PatLit {
2450 expr: Box::new(lit),
2451 }
2452 })
2453 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002454
2455 fn description() -> Option<&'static str> {
2456 Some("literal pattern")
2457 }
Alex Crichton954046c2017-05-30 21:49:42 -07002458 }
David Tolnaye1310902016-10-29 23:40:00 -07002459
Michael Layzell734adb42017-06-07 16:58:31 -04002460 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002461 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002462 named!(parse -> Self, do_parse!(
2463 lo: pat_lit_expr >>
2464 limits: syn!(RangeLimits) >>
2465 hi: pat_lit_expr >>
2466 (PatRange {
2467 lo: Box::new(lo),
2468 hi: Box::new(hi),
2469 limits: limits,
2470 })
2471 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002472
2473 fn description() -> Option<&'static str> {
2474 Some("range pattern")
2475 }
Alex Crichton954046c2017-05-30 21:49:42 -07002476 }
David Tolnaye1310902016-10-29 23:40:00 -07002477
Michael Layzell734adb42017-06-07 16:58:31 -04002478 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002479 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002480 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002481 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002482 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002483 |
David Tolnay8c91b882017-12-28 23:04:32 -05002484 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002485 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002486 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002487 Expr::Unary(ExprUnary {
2488 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002489 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002490 expr: Box::new(v)
2491 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002492 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002493 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002494 })
2495 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002496
Michael Layzell734adb42017-06-07 16:58:31 -04002497 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002498 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002499 named!(parse -> Self, map!(
2500 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002501 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002502 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002503 dots: punct!(..) >>
2504 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002505 (dots, trailing)
2506 )) >>
2507 after: cond!(
2508 match middle {
2509 Some((_, ref trailing)) => trailing.is_some(),
2510 _ => false,
2511 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002512 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002513 ) >>
2514 (before, middle, after)
2515 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002516 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002517 let mut before: Punctuated<Pat, Token![,]> = before;
2518 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002519 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002520 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002521 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002522 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002523 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002524 }),
2525 bracket_token: brackets,
2526 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002527 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002528 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002529 } else {
David Tolnay56080682018-01-06 14:01:52 -08002530 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002531 }
2532 }),
2533 front: before,
2534 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002535 }
Alex Crichton954046c2017-05-30 21:49:42 -07002536 }
Michael Layzell92639a52017-06-01 00:07:44 -04002537 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002538
2539 fn description() -> Option<&'static str> {
2540 Some("slice pattern")
2541 }
Alex Crichton954046c2017-05-30 21:49:42 -07002542 }
David Tolnay323279a2017-12-29 11:26:32 -05002543
2544 #[cfg(feature = "full")]
2545 impl Synom for PatMacro {
2546 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002547
2548 fn description() -> Option<&'static str> {
2549 Some("macro pattern")
2550 }
David Tolnay323279a2017-12-29 11:26:32 -05002551 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002552}
2553
David Tolnayf4bbbd92016-09-23 14:41:55 -07002554#[cfg(feature = "printing")]
2555mod printing {
2556 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002557 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002558 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002559 use quote::{ToTokens, Tokens};
David Tolnay61037c62018-01-05 16:21:03 -08002560 use proc_macro2::{Literal, TokenNode, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002561
David Tolnaybcf26022017-12-25 22:10:52 -05002562 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2563 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002564 #[cfg(feature = "full")]
2565 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002566 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002567 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002568 e.to_tokens(tokens);
2569 });
2570 } else {
2571 e.to_tokens(tokens);
2572 }
2573 }
2574
David Tolnay8c91b882017-12-28 23:04:32 -05002575 #[cfg(feature = "full")]
2576 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2577 tokens.append_all(attrs.outer());
2578 }
Michael Layzell734adb42017-06-07 16:58:31 -04002579
David Tolnay8c91b882017-12-28 23:04:32 -05002580 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002581 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002582
Michael Layzell734adb42017-06-07 16:58:31 -04002583 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002584 impl ToTokens for ExprBox {
2585 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002586 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002587 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002588 self.expr.to_tokens(tokens);
2589 }
2590 }
2591
Michael Layzell734adb42017-06-07 16:58:31 -04002592 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002593 impl ToTokens for ExprInPlace {
2594 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002595 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002596 self.place.to_tokens(tokens);
2597 self.arrow_token.to_tokens(tokens);
2598 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002599 }
2600 }
2601
Michael Layzell734adb42017-06-07 16:58:31 -04002602 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002603 impl ToTokens for ExprArray {
2604 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002605 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002606 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002607 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002608 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002609 }
2610 }
2611
2612 impl ToTokens for ExprCall {
2613 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002614 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002615 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002616 self.paren_token.surround(tokens, |tokens| {
2617 self.args.to_tokens(tokens);
2618 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002619 }
2620 }
2621
Michael Layzell734adb42017-06-07 16:58:31 -04002622 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002623 impl ToTokens for ExprMethodCall {
2624 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002625 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002626 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002627 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002628 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002629 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002630 self.paren_token.surround(tokens, |tokens| {
2631 self.args.to_tokens(tokens);
2632 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002633 }
2634 }
2635
Michael Layzell734adb42017-06-07 16:58:31 -04002636 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002637 impl ToTokens for MethodTurbofish {
2638 fn to_tokens(&self, tokens: &mut Tokens) {
2639 self.colon2_token.to_tokens(tokens);
2640 self.lt_token.to_tokens(tokens);
2641 self.args.to_tokens(tokens);
2642 self.gt_token.to_tokens(tokens);
2643 }
2644 }
2645
2646 #[cfg(feature = "full")]
2647 impl ToTokens for GenericMethodArgument {
2648 fn to_tokens(&self, tokens: &mut Tokens) {
2649 match *self {
2650 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2651 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2652 }
2653 }
2654 }
2655
2656 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002657 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002658 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002659 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002660 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002661 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002662 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002663 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002664 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002665 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002666 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002667 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002668 }
2669 }
2670
2671 impl ToTokens for ExprBinary {
2672 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002673 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002674 self.left.to_tokens(tokens);
2675 self.op.to_tokens(tokens);
2676 self.right.to_tokens(tokens);
2677 }
2678 }
2679
2680 impl ToTokens for ExprUnary {
2681 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002682 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002683 self.op.to_tokens(tokens);
2684 self.expr.to_tokens(tokens);
2685 }
2686 }
2687
David Tolnay8c91b882017-12-28 23:04:32 -05002688 impl ToTokens for ExprLit {
2689 fn to_tokens(&self, tokens: &mut Tokens) {
2690 attrs_to_tokens(&self.attrs, tokens);
2691 self.lit.to_tokens(tokens);
2692 }
2693 }
2694
Alex Crichton62a0a592017-05-22 13:58:53 -07002695 impl ToTokens for ExprCast {
2696 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002697 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002698 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002699 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002700 self.ty.to_tokens(tokens);
2701 }
2702 }
2703
David Tolnay0cf94f22017-12-28 23:46:26 -05002704 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002705 impl ToTokens for ExprType {
2706 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002707 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002708 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002709 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002710 self.ty.to_tokens(tokens);
2711 }
2712 }
2713
Michael Layzell734adb42017-06-07 16:58:31 -04002714 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002715 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002716 if let Some((ref else_token, ref else_)) = *else_ {
2717 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002718
2719 // If we are not one of the valid expressions to exist in an else
2720 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002721 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002722 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002723 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002724 }
2725 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002726 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002727 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002728 });
2729 }
2730 }
2731 }
2732 }
2733
2734 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002735 impl ToTokens for ExprIf {
2736 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002737 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002738 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002739 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002740 self.then_branch.to_tokens(tokens);
2741 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002742 }
2743 }
2744
Michael Layzell734adb42017-06-07 16:58:31 -04002745 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002746 impl ToTokens for ExprIfLet {
2747 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002748 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002749 self.if_token.to_tokens(tokens);
2750 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002751 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002752 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002753 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002754 self.then_branch.to_tokens(tokens);
2755 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002756 }
2757 }
2758
Michael Layzell734adb42017-06-07 16:58:31 -04002759 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002760 impl ToTokens for ExprWhile {
2761 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002762 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002763 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002764 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002765 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002766 self.body.to_tokens(tokens);
2767 }
2768 }
2769
Michael Layzell734adb42017-06-07 16:58:31 -04002770 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002771 impl ToTokens for ExprWhileLet {
2772 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002773 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002774 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002775 self.while_token.to_tokens(tokens);
2776 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002777 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002778 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002779 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002780 self.body.to_tokens(tokens);
2781 }
2782 }
2783
Michael Layzell734adb42017-06-07 16:58:31 -04002784 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002785 impl ToTokens for ExprForLoop {
2786 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002787 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002788 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002789 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002790 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002791 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002792 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002793 self.body.to_tokens(tokens);
2794 }
2795 }
2796
Michael Layzell734adb42017-06-07 16:58:31 -04002797 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002798 impl ToTokens for ExprLoop {
2799 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002800 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002801 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002802 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002803 self.body.to_tokens(tokens);
2804 }
2805 }
2806
Michael Layzell734adb42017-06-07 16:58:31 -04002807 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002808 impl ToTokens for ExprMatch {
2809 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002810 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002811 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002812 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002813 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002814 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002815 arm.to_tokens(tokens);
2816 // Ensure that we have a comma after a non-block arm, except
2817 // for the last one.
2818 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002819 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002820 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002821 }
2822 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002823 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002824 }
2825 }
2826
Michael Layzell734adb42017-06-07 16:58:31 -04002827 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002828 impl ToTokens for ExprCatch {
2829 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002830 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002831 self.do_token.to_tokens(tokens);
2832 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002833 self.block.to_tokens(tokens);
2834 }
2835 }
2836
Michael Layzell734adb42017-06-07 16:58:31 -04002837 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002838 impl ToTokens for ExprYield {
2839 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002840 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002841 self.yield_token.to_tokens(tokens);
2842 self.expr.to_tokens(tokens);
2843 }
2844 }
2845
2846 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002847 impl ToTokens for ExprClosure {
2848 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002849 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002850 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002851 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08002852 for input in self.inputs.pairs() {
2853 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05002854 FnArg::Captured(ArgCaptured {
2855 ref pat,
2856 ty: Type::Infer(_),
2857 ..
2858 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002859 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002860 }
David Tolnay56080682018-01-06 14:01:52 -08002861 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002862 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002863 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002864 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002865 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002866 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002867 self.body.to_tokens(tokens);
2868 }
2869 }
2870
Michael Layzell734adb42017-06-07 16:58:31 -04002871 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002872 impl ToTokens for ExprUnsafe {
2873 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002874 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002875 self.unsafe_token.to_tokens(tokens);
2876 self.block.to_tokens(tokens);
2877 }
2878 }
2879
2880 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002881 impl ToTokens for ExprBlock {
2882 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002883 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002884 self.block.to_tokens(tokens);
2885 }
2886 }
2887
Michael Layzell734adb42017-06-07 16:58:31 -04002888 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002889 impl ToTokens for ExprAssign {
2890 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002891 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002892 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002893 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002894 self.right.to_tokens(tokens);
2895 }
2896 }
2897
Michael Layzell734adb42017-06-07 16:58:31 -04002898 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002899 impl ToTokens for ExprAssignOp {
2900 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002901 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002902 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002903 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002904 self.right.to_tokens(tokens);
2905 }
2906 }
2907
Michael Layzell734adb42017-06-07 16:58:31 -04002908 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002909 impl ToTokens for ExprField {
2910 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002911 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002912 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002913 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002914 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002915 }
2916 }
2917
Michael Layzell734adb42017-06-07 16:58:31 -04002918 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002919 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002920 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002921 match *self {
2922 Member::Named(ident) => ident.to_tokens(tokens),
2923 Member::Unnamed(ref index) => index.to_tokens(tokens),
2924 }
2925 }
2926 }
2927
David Tolnay85b69a42017-12-27 20:43:10 -05002928 impl ToTokens for Index {
2929 fn to_tokens(&self, tokens: &mut Tokens) {
2930 tokens.append(TokenTree {
2931 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002932 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002933 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002934 }
2935 }
2936
2937 impl ToTokens for ExprIndex {
2938 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002939 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002940 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002941 self.bracket_token.surround(tokens, |tokens| {
2942 self.index.to_tokens(tokens);
2943 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002944 }
2945 }
2946
Michael Layzell734adb42017-06-07 16:58:31 -04002947 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002948 impl ToTokens for ExprRange {
2949 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002950 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002951 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002952 match self.limits {
2953 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2954 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2955 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002956 self.to.to_tokens(tokens);
2957 }
2958 }
2959
2960 impl ToTokens for ExprPath {
2961 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002962 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002963 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002964 }
2965 }
2966
Michael Layzell734adb42017-06-07 16:58:31 -04002967 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002968 impl ToTokens for ExprAddrOf {
2969 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002970 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002971 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002972 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002973 self.expr.to_tokens(tokens);
2974 }
2975 }
2976
Michael Layzell734adb42017-06-07 16:58:31 -04002977 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002978 impl ToTokens for ExprBreak {
2979 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002980 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002981 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002982 self.label.to_tokens(tokens);
2983 self.expr.to_tokens(tokens);
2984 }
2985 }
2986
Michael Layzell734adb42017-06-07 16:58:31 -04002987 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002988 impl ToTokens for ExprContinue {
2989 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002990 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002991 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002992 self.label.to_tokens(tokens);
2993 }
2994 }
2995
Michael Layzell734adb42017-06-07 16:58:31 -04002996 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05002997 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07002998 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002999 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003000 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003001 self.expr.to_tokens(tokens);
3002 }
3003 }
3004
Michael Layzell734adb42017-06-07 16:58:31 -04003005 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003006 impl ToTokens for ExprMacro {
3007 fn to_tokens(&self, tokens: &mut Tokens) {
3008 tokens.append_all(self.attrs.outer());
3009 self.mac.to_tokens(tokens);
3010 }
3011 }
3012
3013 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003014 impl ToTokens for ExprStruct {
3015 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003016 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003017 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003018 self.brace_token.surround(tokens, |tokens| {
3019 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003020 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003021 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003022 self.rest.to_tokens(tokens);
3023 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003024 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003025 }
3026 }
3027
Michael Layzell734adb42017-06-07 16:58:31 -04003028 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003029 impl ToTokens for ExprRepeat {
3030 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003031 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003032 self.bracket_token.surround(tokens, |tokens| {
3033 self.expr.to_tokens(tokens);
3034 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003035 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003036 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003037 }
3038 }
3039
David Tolnaye98775f2017-12-28 23:17:00 -05003040 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003041 impl ToTokens for ExprGroup {
3042 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003043 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003044 self.group_token.surround(tokens, |tokens| {
3045 self.expr.to_tokens(tokens);
3046 });
3047 }
3048 }
3049
David Tolnaye98775f2017-12-28 23:17:00 -05003050 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003051 impl ToTokens for ExprParen {
3052 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003053 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003054 self.paren_token.surround(tokens, |tokens| {
3055 self.expr.to_tokens(tokens);
3056 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003057 }
3058 }
3059
Michael Layzell734adb42017-06-07 16:58:31 -04003060 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003061 impl ToTokens for ExprTry {
3062 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003063 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003064 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003065 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003066 }
3067 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003068
David Tolnay2ae520a2017-12-29 11:19:50 -05003069 impl ToTokens for ExprVerbatim {
3070 fn to_tokens(&self, tokens: &mut Tokens) {
3071 self.tts.to_tokens(tokens);
3072 }
3073 }
3074
Michael Layzell734adb42017-06-07 16:58:31 -04003075 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003076 impl ToTokens for Label {
3077 fn to_tokens(&self, tokens: &mut Tokens) {
3078 self.name.to_tokens(tokens);
3079 self.colon_token.to_tokens(tokens);
3080 }
3081 }
3082
3083 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003084 impl ToTokens for FieldValue {
3085 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003086 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003087 if let Some(ref colon_token) = self.colon_token {
3088 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003089 self.expr.to_tokens(tokens);
3090 }
David Tolnay055a7042016-10-02 19:23:54 -07003091 }
3092 }
3093
Michael Layzell734adb42017-06-07 16:58:31 -04003094 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003095 impl ToTokens for Arm {
3096 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003097 tokens.append_all(&self.attrs);
3098 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003099 if let Some((ref if_token, ref guard)) = self.guard {
3100 if_token.to_tokens(tokens);
3101 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003102 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003103 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003104 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003105 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003106 }
3107 }
3108
Michael Layzell734adb42017-06-07 16:58:31 -04003109 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003110 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003111 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003112 self.underscore_token.to_tokens(tokens);
3113 }
3114 }
3115
Michael Layzell734adb42017-06-07 16:58:31 -04003116 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003117 impl ToTokens for PatIdent {
3118 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003119 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003120 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003121 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003122 if let Some((ref at_token, ref subpat)) = self.subpat {
3123 at_token.to_tokens(tokens);
3124 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003125 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003126 }
3127 }
3128
Michael Layzell734adb42017-06-07 16:58:31 -04003129 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003130 impl ToTokens for PatStruct {
3131 fn to_tokens(&self, tokens: &mut Tokens) {
3132 self.path.to_tokens(tokens);
3133 self.brace_token.surround(tokens, |tokens| {
3134 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003135 // NOTE: We need a comma before the dot2 token if it is present.
3136 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003137 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003138 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003139 self.dot2_token.to_tokens(tokens);
3140 });
3141 }
3142 }
3143
Michael Layzell734adb42017-06-07 16:58:31 -04003144 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003145 impl ToTokens for PatTupleStruct {
3146 fn to_tokens(&self, tokens: &mut Tokens) {
3147 self.path.to_tokens(tokens);
3148 self.pat.to_tokens(tokens);
3149 }
3150 }
3151
Michael Layzell734adb42017-06-07 16:58:31 -04003152 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003153 impl ToTokens for PatPath {
3154 fn to_tokens(&self, tokens: &mut Tokens) {
3155 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3156 }
3157 }
3158
Michael Layzell734adb42017-06-07 16:58:31 -04003159 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003160 impl ToTokens for PatTuple {
3161 fn to_tokens(&self, tokens: &mut Tokens) {
3162 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003163 self.front.to_tokens(tokens);
3164 if let Some(ref dot2_token) = self.dot2_token {
3165 if !self.front.empty_or_trailing() {
3166 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003167 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003168 }
David Tolnay41871922017-12-29 01:53:45 -05003169 dot2_token.to_tokens(tokens);
3170 self.comma_token.to_tokens(tokens);
3171 if self.comma_token.is_none() && !self.back.is_empty() {
3172 // Ensure there is a comma after the .. token.
3173 <Token![,]>::default().to_tokens(tokens);
3174 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003175 }
David Tolnay41871922017-12-29 01:53:45 -05003176 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003177 });
3178 }
3179 }
3180
Michael Layzell734adb42017-06-07 16:58:31 -04003181 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003182 impl ToTokens for PatBox {
3183 fn to_tokens(&self, tokens: &mut Tokens) {
3184 self.box_token.to_tokens(tokens);
3185 self.pat.to_tokens(tokens);
3186 }
3187 }
3188
Michael Layzell734adb42017-06-07 16:58:31 -04003189 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003190 impl ToTokens for PatRef {
3191 fn to_tokens(&self, tokens: &mut Tokens) {
3192 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003193 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003194 self.pat.to_tokens(tokens);
3195 }
3196 }
3197
Michael Layzell734adb42017-06-07 16:58:31 -04003198 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003199 impl ToTokens for PatLit {
3200 fn to_tokens(&self, tokens: &mut Tokens) {
3201 self.expr.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 PatRange {
3207 fn to_tokens(&self, tokens: &mut Tokens) {
3208 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003209 match self.limits {
3210 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3211 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3212 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003213 self.hi.to_tokens(tokens);
3214 }
3215 }
3216
Michael Layzell734adb42017-06-07 16:58:31 -04003217 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003218 impl ToTokens for PatSlice {
3219 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003220 // XXX: This is a mess, and it will be so easy to screw it up. How
3221 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003222 self.bracket_token.surround(tokens, |tokens| {
3223 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003224
3225 // If we need a comma before the middle or standalone .. token,
3226 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003227 if !self.front.empty_or_trailing()
3228 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003229 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003230 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003231 }
3232
3233 // If we have an identifier, we always need a .. token.
3234 if self.middle.is_some() {
3235 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003236 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003237 } else if self.dot2_token.is_some() {
3238 self.dot2_token.to_tokens(tokens);
3239 }
3240
3241 // Make sure we have a comma before the back half.
3242 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003243 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003244 self.back.to_tokens(tokens);
3245 } else {
3246 self.comma_token.to_tokens(tokens);
3247 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003248 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003249 }
3250 }
3251
Michael Layzell734adb42017-06-07 16:58:31 -04003252 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003253 impl ToTokens for PatMacro {
3254 fn to_tokens(&self, tokens: &mut Tokens) {
3255 self.mac.to_tokens(tokens);
3256 }
3257 }
3258
3259 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003260 impl ToTokens for PatVerbatim {
3261 fn to_tokens(&self, tokens: &mut Tokens) {
3262 self.tts.to_tokens(tokens);
3263 }
3264 }
3265
3266 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003267 impl ToTokens for FieldPat {
3268 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003269 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003270 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003271 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003272 }
3273 self.pat.to_tokens(tokens);
3274 }
3275 }
3276
Michael Layzell734adb42017-06-07 16:58:31 -04003277 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003278 impl ToTokens for Block {
3279 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003280 self.brace_token.surround(tokens, |tokens| {
3281 tokens.append_all(&self.stmts);
3282 });
David Tolnay42602292016-10-01 22:25:45 -07003283 }
3284 }
3285
Michael Layzell734adb42017-06-07 16:58:31 -04003286 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003287 impl ToTokens for Stmt {
3288 fn to_tokens(&self, tokens: &mut Tokens) {
3289 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003290 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003291 Stmt::Item(ref item) => item.to_tokens(tokens),
3292 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003293 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003294 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003295 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003296 }
David Tolnay42602292016-10-01 22:25:45 -07003297 }
3298 }
3299 }
David Tolnay191e0582016-10-02 18:31:09 -07003300
Michael Layzell734adb42017-06-07 16:58:31 -04003301 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003302 impl ToTokens for Local {
3303 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003304 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003305 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003306 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003307 if let Some((ref colon_token, ref ty)) = self.ty {
3308 colon_token.to_tokens(tokens);
3309 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003310 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003311 if let Some((ref eq_token, ref init)) = self.init {
3312 eq_token.to_tokens(tokens);
3313 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003314 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003315 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003316 }
3317 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003318}