blob: 975c6359cf247c7a88cf961012e3fedfa34914b1 [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 ));
David Tolnay79777332018-01-07 10:04:42 -08001389
1390 fn description() -> Option<&'static str> {
1391 Some("literal")
1392 }
David Tolnay8c91b882017-12-28 23:04:32 -05001393 }
1394
1395 #[cfg(feature = "full")]
1396 impl Synom for ExprMacro {
1397 named!(parse -> Self, do_parse!(
1398 mac: syn!(Macro) >>
1399 (ExprMacro {
1400 attrs: Vec::new(),
1401 mac: mac,
1402 })
1403 ));
David Tolnay79777332018-01-07 10:04:42 -08001404
1405 fn description() -> Option<&'static str> {
1406 Some("macro invocation expression")
1407 }
David Tolnay8c91b882017-12-28 23:04:32 -05001408 }
1409
David Tolnaye98775f2017-12-28 23:17:00 -05001410 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04001411 impl Synom for ExprGroup {
1412 named!(parse -> Self, do_parse!(
1413 e: grouped!(syn!(Expr)) >>
1414 (ExprGroup {
David Tolnay8c91b882017-12-28 23:04:32 -05001415 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001416 expr: Box::new(e.1),
1417 group_token: e.0,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001418 })
Michael Layzell93c36282017-06-04 20:43:14 -04001419 ));
David Tolnay79777332018-01-07 10:04:42 -08001420
1421 fn description() -> Option<&'static str> {
1422 Some("expression surrounded by invisible delimiters")
1423 }
Michael Layzell93c36282017-06-04 20:43:14 -04001424 }
1425
David Tolnaye98775f2017-12-28 23:17:00 -05001426 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001427 impl Synom for ExprParen {
Michael Layzell92639a52017-06-01 00:07:44 -04001428 named!(parse -> Self, do_parse!(
1429 e: parens!(syn!(Expr)) >>
1430 (ExprParen {
David Tolnay8c91b882017-12-28 23:04:32 -05001431 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001432 paren_token: e.0,
1433 expr: Box::new(e.1),
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001434 })
Michael Layzell92639a52017-06-01 00:07:44 -04001435 ));
David Tolnay79777332018-01-07 10:04:42 -08001436
1437 fn description() -> Option<&'static str> {
1438 Some("parenthesized expression")
1439 }
Alex Crichton954046c2017-05-30 21:49:42 -07001440 }
David Tolnay89e05672016-10-02 14:39:42 -07001441
Michael Layzell734adb42017-06-07 16:58:31 -04001442 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001443 impl Synom for ExprArray {
Michael Layzell92639a52017-06-01 00:07:44 -04001444 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001445 elems: brackets!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001446 (ExprArray {
David Tolnay8c91b882017-12-28 23:04:32 -05001447 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001448 bracket_token: elems.0,
1449 elems: elems.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001450 })
1451 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001452
1453 fn description() -> Option<&'static str> {
David Tolnay79777332018-01-07 10:04:42 -08001454 Some("array expression")
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001455 }
Alex Crichton954046c2017-05-30 21:49:42 -07001456 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001457
David Tolnayf2cfd722017-12-31 18:02:51 -05001458 named!(and_call -> (token::Paren, Punctuated<Expr, Token![,]>),
1459 parens!(Punctuated::parse_terminated));
David Tolnayfa0edf22016-09-23 22:58:24 -07001460
Michael Layzell734adb42017-06-07 16:58:31 -04001461 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001462 named!(and_method_call -> ExprMethodCall, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001463 dot: punct!(.) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001464 method: syn!(Ident) >>
David Tolnayd60cfec2017-12-29 00:21:38 -05001465 turbofish: option!(tuple!(
1466 punct!(::),
1467 punct!(<),
David Tolnayf2cfd722017-12-31 18:02:51 -05001468 call!(Punctuated::parse_terminated),
David Tolnayd60cfec2017-12-29 00:21:38 -05001469 punct!(>)
David Tolnayfa0edf22016-09-23 22:58:24 -07001470 )) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001471 args: parens!(Punctuated::parse_terminated) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001472 ({
Alex Crichton954046c2017-05-30 21:49:42 -07001473 ExprMethodCall {
David Tolnay8c91b882017-12-28 23:04:32 -05001474 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001475 // this expr will get overwritten after being returned
David Tolnay360efd22018-01-04 23:35:26 -08001476 receiver: Box::new(Expr::Verbatim(ExprVerbatim {
1477 tts: TokenStream::empty(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001478 })),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001479
Alex Crichton954046c2017-05-30 21:49:42 -07001480 method: method,
David Tolnayd60cfec2017-12-29 00:21:38 -05001481 turbofish: turbofish.map(|fish| MethodTurbofish {
1482 colon2_token: fish.0,
1483 lt_token: fish.1,
1484 args: fish.2,
1485 gt_token: fish.3,
1486 }),
David Tolnay8875fca2017-12-31 13:52:37 -05001487 args: args.1,
1488 paren_token: args.0,
Alex Crichton954046c2017-05-30 21:49:42 -07001489 dot_token: dot,
Alex Crichton954046c2017-05-30 21:49:42 -07001490 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001491 })
David Tolnayfa0edf22016-09-23 22:58:24 -07001492 ));
1493
Michael Layzell734adb42017-06-07 16:58:31 -04001494 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05001495 impl Synom for GenericMethodArgument {
1496 // TODO parse const generics as well
1497 named!(parse -> Self, map!(ty_no_eq_after, GenericMethodArgument::Type));
David Tolnay79777332018-01-07 10:04:42 -08001498
1499 fn description() -> Option<&'static str> {
1500 Some("generic method argument")
1501 }
David Tolnayd60cfec2017-12-29 00:21:38 -05001502 }
1503
1504 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05001505 impl Synom for ExprTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04001506 named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001507 elems: parens!(Punctuated::parse_terminated) >>
David Tolnay05362582017-12-26 01:33:57 -05001508 (ExprTuple {
David Tolnay8c91b882017-12-28 23:04:32 -05001509 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001510 elems: elems.1,
1511 paren_token: elems.0,
Michael Layzell92639a52017-06-01 00:07:44 -04001512 })
1513 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001514
1515 fn description() -> Option<&'static str> {
1516 Some("tuple")
1517 }
Alex Crichton954046c2017-05-30 21:49:42 -07001518 }
David Tolnayfa0edf22016-09-23 22:58:24 -07001519
Michael Layzell734adb42017-06-07 16:58:31 -04001520 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001521 impl Synom for ExprIfLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001522 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001523 if_: keyword!(if) >>
1524 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001525 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001526 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001527 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001528 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001529 else_block: option!(else_block) >>
1530 (ExprIfLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001531 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001532 pat: Box::new(pat),
1533 let_token: let_,
1534 eq_token: eq,
1535 expr: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001536 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001537 brace_token: then_block.0,
1538 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001539 },
1540 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001541 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001542 })
1543 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001544
1545 fn description() -> Option<&'static str> {
1546 Some("`if let` expression")
1547 }
David Tolnay29f9ce12016-10-02 20:58:40 -07001548 }
1549
Michael Layzell734adb42017-06-07 16:58:31 -04001550 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001551 impl Synom for ExprIf {
Michael Layzell92639a52017-06-01 00:07:44 -04001552 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001553 if_: keyword!(if) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001554 cond: expr_no_struct >>
David Tolnaye64213b2017-12-30 00:24:20 -05001555 then_block: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001556 else_block: option!(else_block) >>
1557 (ExprIf {
David Tolnay8c91b882017-12-28 23:04:32 -05001558 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001559 cond: Box::new(cond),
David Tolnay2ccf32a2017-12-29 00:34:26 -05001560 then_branch: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001561 brace_token: then_block.0,
1562 stmts: then_block.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001563 },
1564 if_token: if_,
David Tolnay2ccf32a2017-12-29 00:34:26 -05001565 else_branch: else_block,
Michael Layzell92639a52017-06-01 00:07:44 -04001566 })
1567 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001568
1569 fn description() -> Option<&'static str> {
1570 Some("`if` expression")
1571 }
Alex Crichton954046c2017-05-30 21:49:42 -07001572 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001573
Michael Layzell734adb42017-06-07 16:58:31 -04001574 #[cfg(feature = "full")]
David Tolnay2ccf32a2017-12-29 00:34:26 -05001575 named!(else_block -> (Token![else], Box<Expr>), do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001576 else_: keyword!(else) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001577 expr: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05001578 syn!(ExprIf) => { Expr::If }
Alex Crichton954046c2017-05-30 21:49:42 -07001579 |
David Tolnay8c91b882017-12-28 23:04:32 -05001580 syn!(ExprIfLet) => { Expr::IfLet }
Alex Crichton954046c2017-05-30 21:49:42 -07001581 |
1582 do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05001583 else_block: braces!(Block::parse_within) >>
David Tolnay8c91b882017-12-28 23:04:32 -05001584 (Expr::Block(ExprBlock {
1585 attrs: Vec::new(),
Alex Crichton954046c2017-05-30 21:49:42 -07001586 block: Block {
David Tolnay8875fca2017-12-31 13:52:37 -05001587 brace_token: else_block.0,
1588 stmts: else_block.1,
Alex Crichton954046c2017-05-30 21:49:42 -07001589 },
1590 }))
David Tolnay939766a2016-09-23 23:48:12 -07001591 )
Alex Crichton954046c2017-05-30 21:49:42 -07001592 ) >>
David Tolnay2ccf32a2017-12-29 00:34:26 -05001593 (else_, Box::new(expr))
David Tolnay939766a2016-09-23 23:48:12 -07001594 ));
1595
Michael Layzell734adb42017-06-07 16:58:31 -04001596 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001597 impl Synom for ExprForLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001598 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001599 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001600 for_: keyword!(for) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001601 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001602 in_: keyword!(in) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001603 expr: expr_no_struct >>
1604 loop_block: syn!(Block) >>
1605 (ExprForLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001606 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001607 for_token: for_,
1608 in_token: in_,
1609 pat: Box::new(pat),
1610 expr: Box::new(expr),
1611 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001612 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001613 })
1614 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001615
1616 fn description() -> Option<&'static str> {
1617 Some("`for` loop")
1618 }
Alex Crichton954046c2017-05-30 21:49:42 -07001619 }
Gregory Katze5f35682016-09-27 14:20:55 -04001620
Michael Layzell734adb42017-06-07 16:58:31 -04001621 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001622 impl Synom for ExprLoop {
Michael Layzell92639a52017-06-01 00:07:44 -04001623 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001624 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001625 loop_: keyword!(loop) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001626 loop_block: syn!(Block) >>
1627 (ExprLoop {
David Tolnay8c91b882017-12-28 23:04:32 -05001628 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001629 loop_token: loop_,
1630 body: loop_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001631 label: label,
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("`loop`")
1637 }
Alex Crichton954046c2017-05-30 21:49:42 -07001638 }
1639
Michael Layzell734adb42017-06-07 16:58:31 -04001640 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001641 impl Synom for ExprMatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001642 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001643 match_: keyword!(match) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001644 obj: expr_no_struct >>
David Tolnay2c136452017-12-27 14:13:32 -05001645 res: braces!(many0!(Arm::parse)) >>
David Tolnay8875fca2017-12-31 13:52:37 -05001646 (ExprMatch {
1647 attrs: Vec::new(),
1648 expr: Box::new(obj),
1649 match_token: match_,
1650 brace_token: res.0,
1651 arms: res.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001652 })
1653 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001654
1655 fn description() -> Option<&'static str> {
1656 Some("`match` expression")
1657 }
Alex Crichton954046c2017-05-30 21:49:42 -07001658 }
David Tolnay1978c672016-10-27 22:05:52 -07001659
Michael Layzell734adb42017-06-07 16:58:31 -04001660 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001661 impl Synom for ExprCatch {
Michael Layzell92639a52017-06-01 00:07:44 -04001662 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001663 do_: keyword!(do) >>
1664 catch_: keyword!(catch) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001665 catch_block: syn!(Block) >>
1666 (ExprCatch {
David Tolnay8c91b882017-12-28 23:04:32 -05001667 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001668 block: catch_block,
1669 do_token: do_,
1670 catch_token: catch_,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05001671 })
Michael Layzell92639a52017-06-01 00:07:44 -04001672 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001673
1674 fn description() -> Option<&'static str> {
1675 Some("`catch` expression")
1676 }
Alex Crichton954046c2017-05-30 21:49:42 -07001677 }
Arnavion02ef13f2017-04-25 00:54:31 -07001678
Michael Layzell734adb42017-06-07 16:58:31 -04001679 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07001680 impl Synom for ExprYield {
1681 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001682 yield_: keyword!(yield) >>
Alex Crichtonfe110462017-06-01 12:49:27 -07001683 expr: option!(syn!(Expr)) >>
1684 (ExprYield {
David Tolnay8c91b882017-12-28 23:04:32 -05001685 attrs: Vec::new(),
Alex Crichtonfe110462017-06-01 12:49:27 -07001686 yield_token: yield_,
1687 expr: expr.map(Box::new),
1688 })
1689 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001690
1691 fn description() -> Option<&'static str> {
1692 Some("`yield` expression")
1693 }
Alex Crichtonfe110462017-06-01 12:49:27 -07001694 }
1695
1696 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001697 impl Synom for Arm {
Michael Layzell92639a52017-06-01 00:07:44 -04001698 named!(parse -> Self, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05001699 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001700 pats: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001701 guard: option!(tuple!(keyword!(if), syn!(Expr))) >>
1702 rocket: punct!(=>) >>
Alex Crichton03b30272017-08-28 09:35:24 -07001703 body: do_parse!(
1704 expr: alt!(expr_nosemi | syn!(Expr)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001705 comma: switch!(value!(arm_expr_requires_comma(&expr)),
1706 true => alt!(
1707 input_end!() => { |_| None }
1708 |
1709 punct!(,) => { Some }
1710 )
Alex Crichton03b30272017-08-28 09:35:24 -07001711 |
David Tolnaydc03aec2017-12-30 01:54:18 -05001712 false => option!(punct!(,))
1713 ) >>
1714 (expr, comma)
Michael Layzell92639a52017-06-01 00:07:44 -04001715 ) >>
1716 (Arm {
1717 rocket_token: rocket,
Michael Layzell92639a52017-06-01 00:07:44 -04001718 attrs: attrs,
1719 pats: pats,
David Tolnay8b4d3022017-12-29 12:11:10 -05001720 guard: guard.map(|(if_, guard)| (if_, Box::new(guard))),
Alex Crichton03b30272017-08-28 09:35:24 -07001721 body: Box::new(body.0),
1722 comma: body.1,
Michael Layzell92639a52017-06-01 00:07:44 -04001723 })
1724 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001725
1726 fn description() -> Option<&'static str> {
1727 Some("`match` arm")
1728 }
Alex Crichton954046c2017-05-30 21:49:42 -07001729 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07001730
Michael Layzell734adb42017-06-07 16:58:31 -04001731 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001732 named!(expr_closure(allow_struct: bool) -> Expr, do_parse!(
David Tolnayefc96fb2017-12-29 02:03:15 -05001733 capture: option!(keyword!(move)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001734 or1: punct!(|) >>
David Tolnayf2cfd722017-12-31 18:02:51 -05001735 inputs: call!(Punctuated::parse_terminated_with, fn_arg) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001736 or2: punct!(|) >>
David Tolnay89e05672016-10-02 14:39:42 -07001737 ret_and_body: alt!(
1738 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001739 arrow: punct!(->) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001740 ty: syn!(Type) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001741 body: syn!(Block) >>
David Tolnay4a3f59a2017-12-28 21:21:12 -05001742 (ReturnType::Type(arrow, Box::new(ty)),
David Tolnay8c91b882017-12-28 23:04:32 -05001743 Expr::Block(ExprBlock {
1744 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001745 block: body,
David Tolnay3bc597f2017-12-31 02:31:11 -05001746 }))
David Tolnay89e05672016-10-02 14:39:42 -07001747 )
1748 |
David Tolnayf93b90d2017-11-11 19:21:26 -08001749 map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e))
David Tolnay89e05672016-10-02 14:39:42 -07001750 ) >>
Alex Crichton62a0a592017-05-22 13:58:53 -07001751 (ExprClosure {
David Tolnay8c91b882017-12-28 23:04:32 -05001752 attrs: Vec::new(),
Alex Crichton62a0a592017-05-22 13:58:53 -07001753 capture: capture,
Alex Crichton954046c2017-05-30 21:49:42 -07001754 or1_token: or1,
David Tolnay7f675742017-12-27 22:43:21 -05001755 inputs: inputs,
Alex Crichton954046c2017-05-30 21:49:42 -07001756 or2_token: or2,
David Tolnay7f675742017-12-27 22:43:21 -05001757 output: ret_and_body.0,
Alex Crichton62a0a592017-05-22 13:58:53 -07001758 body: Box::new(ret_and_body.1),
1759 }.into())
David Tolnay89e05672016-10-02 14:39:42 -07001760 ));
1761
Michael Layzell734adb42017-06-07 16:58:31 -04001762 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001763 named!(fn_arg -> FnArg, do_parse!(
1764 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001765 ty: option!(tuple!(punct!(:), syn!(Type))) >>
Alex Crichton954046c2017-05-30 21:49:42 -07001766 ({
David Tolnay80ed55f2017-12-27 22:54:40 -05001767 if let Some((colon, ty)) = ty {
1768 FnArg::Captured(ArgCaptured {
1769 pat: pat,
1770 colon_token: colon,
1771 ty: ty,
1772 })
1773 } else {
1774 FnArg::Inferred(pat)
1775 }
David Tolnaybb6feae2016-10-02 21:25:20 -07001776 })
Gregory Katz3e562cc2016-09-28 18:33:02 -04001777 ));
1778
Michael Layzell734adb42017-06-07 16:58:31 -04001779 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001780 impl Synom for ExprWhile {
Michael Layzell92639a52017-06-01 00:07:44 -04001781 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001782 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001783 while_: keyword!(while) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001784 cond: expr_no_struct >>
1785 while_block: syn!(Block) >>
1786 (ExprWhile {
David Tolnay8c91b882017-12-28 23:04:32 -05001787 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001788 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001789 cond: Box::new(cond),
1790 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001791 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001792 })
1793 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001794
1795 fn description() -> Option<&'static str> {
1796 Some("`while` expression")
1797 }
Alex Crichton954046c2017-05-30 21:49:42 -07001798 }
1799
Michael Layzell734adb42017-06-07 16:58:31 -04001800 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001801 impl Synom for ExprWhileLet {
Michael Layzell92639a52017-06-01 00:07:44 -04001802 named!(parse -> Self, do_parse!(
David Tolnaybcd498f2017-12-29 12:02:33 -05001803 label: option!(syn!(Label)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001804 while_: keyword!(while) >>
1805 let_: keyword!(let) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001806 pat: syn!(Pat) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001807 eq: punct!(=) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001808 value: expr_no_struct >>
1809 while_block: syn!(Block) >>
1810 (ExprWhileLet {
David Tolnay8c91b882017-12-28 23:04:32 -05001811 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001812 eq_token: eq,
1813 let_token: let_,
1814 while_token: while_,
Michael Layzell92639a52017-06-01 00:07:44 -04001815 pat: Box::new(pat),
1816 expr: Box::new(value),
1817 body: while_block,
David Tolnaybcd498f2017-12-29 12:02:33 -05001818 label: label,
1819 })
1820 ));
David Tolnay79777332018-01-07 10:04:42 -08001821
1822 fn description() -> Option<&'static str> {
1823 Some("`while let` expression")
1824 }
David Tolnaybcd498f2017-12-29 12:02:33 -05001825 }
1826
1827 #[cfg(feature = "full")]
1828 impl Synom for Label {
1829 named!(parse -> Self, do_parse!(
1830 name: syn!(Lifetime) >>
1831 colon: punct!(:) >>
1832 (Label {
1833 name: name,
1834 colon_token: colon,
Michael Layzell92639a52017-06-01 00:07:44 -04001835 })
1836 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001837
1838 fn description() -> Option<&'static str> {
1839 Some("`while let` expression")
1840 }
Alex Crichton954046c2017-05-30 21:49:42 -07001841 }
1842
Michael Layzell734adb42017-06-07 16:58:31 -04001843 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001844 impl Synom for ExprContinue {
Michael Layzell92639a52017-06-01 00:07:44 -04001845 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001846 cont: keyword!(continue) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001847 label: option!(syn!(Lifetime)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001848 (ExprContinue {
David Tolnay8c91b882017-12-28 23:04:32 -05001849 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001850 continue_token: cont,
David Tolnaybcd498f2017-12-29 12:02:33 -05001851 label: label,
Michael Layzell92639a52017-06-01 00:07:44 -04001852 })
1853 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001854
1855 fn description() -> Option<&'static str> {
1856 Some("`continue`")
1857 }
Alex Crichton954046c2017-05-30 21:49:42 -07001858 }
Gregory Katzfd6935d2016-09-30 22:51:25 -04001859
Michael Layzell734adb42017-06-07 16:58:31 -04001860 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001861 named!(expr_break(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001862 break_: keyword!(break) >>
David Tolnaybcd498f2017-12-29 12:02:33 -05001863 label: option!(syn!(Lifetime)) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001864 // We can't allow blocks after a `break` expression when we wouldn't
1865 // allow structs, as this expression is ambiguous.
1866 val: opt_ambiguous_expr!(allow_struct) >>
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001867 (ExprBreak {
David Tolnay8c91b882017-12-28 23:04:32 -05001868 attrs: Vec::new(),
David Tolnaybcd498f2017-12-29 12:02:33 -05001869 label: label,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001870 expr: val.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001871 break_token: break_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001872 }.into())
Gregory Katzfd6935d2016-09-30 22:51:25 -04001873 ));
1874
Michael Layzell734adb42017-06-07 16:58:31 -04001875 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05001876 named!(expr_ret(allow_struct: bool) -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08001877 return_: keyword!(return) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04001878 // NOTE: return is greedy and eats blocks after it even when in a
1879 // position where structs are not allowed, such as in if statement
1880 // conditions. For example:
1881 //
David Tolnaybcf26022017-12-25 22:10:52 -05001882 // if return { println!("A") } {} // Prints "A"
David Tolnayaf2557e2016-10-24 11:52:21 -07001883 ret_value: option!(ambiguous_expr!(allow_struct)) >>
David Tolnayc246cd32017-12-28 23:14:32 -05001884 (ExprReturn {
David Tolnay8c91b882017-12-28 23:04:32 -05001885 attrs: Vec::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001886 expr: ret_value.map(Box::new),
Alex Crichton954046c2017-05-30 21:49:42 -07001887 return_token: return_,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001888 }.into())
David Tolnay055a7042016-10-02 19:23:54 -07001889 ));
1890
Michael Layzell734adb42017-06-07 16:58:31 -04001891 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001892 impl Synom for ExprStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04001893 named!(parse -> Self, do_parse!(
1894 path: syn!(Path) >>
1895 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05001896 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001897 base: option!(cond!(fields.empty_or_trailing(), do_parse!(
1898 dots: punct!(..) >>
1899 base: syn!(Expr) >>
1900 (dots, base)
1901 ))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001902 (fields, base)
1903 )) >>
1904 ({
David Tolnay8875fca2017-12-31 13:52:37 -05001905 let (brace, (fields, base)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04001906 let (dots, rest) = match base.and_then(|b| b) {
1907 Some((dots, base)) => (Some(dots), Some(base)),
1908 None => (None, None),
1909 };
1910 ExprStruct {
David Tolnay8c91b882017-12-28 23:04:32 -05001911 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001912 brace_token: brace,
1913 path: path,
1914 fields: fields,
1915 dot2_token: dots,
1916 rest: rest.map(Box::new),
1917 }
1918 })
1919 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001920
1921 fn description() -> Option<&'static str> {
1922 Some("struct literal expression")
1923 }
Alex Crichton954046c2017-05-30 21:49:42 -07001924 }
1925
Michael Layzell734adb42017-06-07 16:58:31 -04001926 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001927 impl Synom for FieldValue {
Michael Layzell92639a52017-06-01 00:07:44 -04001928 named!(parse -> Self, alt!(
1929 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05001930 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001931 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001932 value: syn!(Expr) >>
1933 (FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001934 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04001935 expr: value,
Alex Crichton954046c2017-05-30 21:49:42 -07001936 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04001937 colon_token: Some(colon),
Alex Crichton954046c2017-05-30 21:49:42 -07001938 })
Michael Layzell92639a52017-06-01 00:07:44 -04001939 )
1940 |
David Tolnaybc7d7d92017-06-03 20:54:05 -07001941 map!(syn!(Ident), |name| FieldValue {
David Tolnay85b69a42017-12-27 20:43:10 -05001942 member: Member::Named(name),
David Tolnay8c91b882017-12-28 23:04:32 -05001943 expr: Expr::Path(ExprPath {
1944 attrs: Vec::new(),
1945 qself: None,
1946 path: name.into(),
David Tolnay3bc597f2017-12-31 02:31:11 -05001947 }),
Michael Layzell92639a52017-06-01 00:07:44 -04001948 attrs: Vec::new(),
1949 colon_token: None,
1950 })
1951 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001952
1953 fn description() -> Option<&'static str> {
1954 Some("field-value pair: `field: value`")
1955 }
Alex Crichton954046c2017-05-30 21:49:42 -07001956 }
David Tolnay055a7042016-10-02 19:23:54 -07001957
Michael Layzell734adb42017-06-07 16:58:31 -04001958 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001959 impl Synom for ExprRepeat {
Michael Layzell92639a52017-06-01 00:07:44 -04001960 named!(parse -> Self, do_parse!(
1961 data: brackets!(do_parse!(
1962 value: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08001963 semi: punct!(;) >>
Michael Layzell92639a52017-06-01 00:07:44 -04001964 times: syn!(Expr) >>
1965 (value, semi, times)
1966 )) >>
1967 (ExprRepeat {
David Tolnay8c91b882017-12-28 23:04:32 -05001968 attrs: Vec::new(),
David Tolnay8875fca2017-12-31 13:52:37 -05001969 expr: Box::new((data.1).0),
David Tolnay84d80442018-01-07 01:03:20 -08001970 len: Box::new((data.1).2),
David Tolnay8875fca2017-12-31 13:52:37 -05001971 bracket_token: data.0,
1972 semi_token: (data.1).1,
Michael Layzell92639a52017-06-01 00:07:44 -04001973 })
1974 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001975
1976 fn description() -> Option<&'static str> {
1977 Some("repeated array literal: `[val; N]`")
1978 }
Alex Crichton954046c2017-05-30 21:49:42 -07001979 }
David Tolnay055a7042016-10-02 19:23:54 -07001980
Michael Layzell734adb42017-06-07 16:58:31 -04001981 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05001982 impl Synom for ExprUnsafe {
1983 named!(parse -> Self, do_parse!(
1984 unsafe_: keyword!(unsafe) >>
1985 b: syn!(Block) >>
1986 (ExprUnsafe {
David Tolnay8c91b882017-12-28 23:04:32 -05001987 attrs: Vec::new(),
Nika Layzell640832a2017-12-04 13:37:09 -05001988 unsafe_token: unsafe_,
1989 block: b,
1990 })
1991 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08001992
1993 fn description() -> Option<&'static str> {
1994 Some("unsafe block: `unsafe { .. }`")
1995 }
Nika Layzell640832a2017-12-04 13:37:09 -05001996 }
1997
1998 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07001999 impl Synom for ExprBlock {
Michael Layzell92639a52017-06-01 00:07:44 -04002000 named!(parse -> Self, do_parse!(
Michael Layzell92639a52017-06-01 00:07:44 -04002001 b: syn!(Block) >>
2002 (ExprBlock {
David Tolnay8c91b882017-12-28 23:04:32 -05002003 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002004 block: b,
2005 })
2006 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002007
2008 fn description() -> Option<&'static str> {
2009 Some("block: `{ .. }`")
2010 }
Alex Crichton954046c2017-05-30 21:49:42 -07002011 }
David Tolnay89e05672016-10-02 14:39:42 -07002012
Michael Layzell734adb42017-06-07 16:58:31 -04002013 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05002014 named!(expr_range(allow_struct: bool) -> Expr, do_parse!(
Alex Crichton954046c2017-05-30 21:49:42 -07002015 limits: syn!(RangeLimits) >>
Michael Layzellb78f3b52017-06-04 19:03:03 -04002016 hi: opt_ambiguous_expr!(allow_struct) >>
David Tolnay8c91b882017-12-28 23:04:32 -05002017 (ExprRange {
2018 attrs: Vec::new(),
2019 from: None,
2020 to: hi.map(Box::new),
2021 limits: limits,
2022 }.into())
David Tolnay438c9052016-10-07 23:24:48 -07002023 ));
2024
Michael Layzell734adb42017-06-07 16:58:31 -04002025 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002026 impl Synom for RangeLimits {
Michael Layzell92639a52017-06-01 00:07:44 -04002027 named!(parse -> Self, alt!(
2028 // Must come before Dot2
David Tolnaybe55d7b2017-12-17 23:41:20 -08002029 punct!(..=) => { RangeLimits::Closed }
2030 |
2031 // Must come before Dot2
David Tolnay995bff22017-12-17 23:44:43 -08002032 punct!(...) => { |dot3| RangeLimits::Closed(Token![..=](dot3.0)) }
Michael Layzell92639a52017-06-01 00:07:44 -04002033 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002034 punct!(..) => { RangeLimits::HalfOpen }
Michael Layzell92639a52017-06-01 00:07:44 -04002035 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002036
2037 fn description() -> Option<&'static str> {
2038 Some("range limit: `..`, `...` or `..=`")
2039 }
Alex Crichton954046c2017-05-30 21:49:42 -07002040 }
David Tolnay438c9052016-10-07 23:24:48 -07002041
Alex Crichton954046c2017-05-30 21:49:42 -07002042 impl Synom for ExprPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002043 named!(parse -> Self, do_parse!(
2044 pair: qpath >>
2045 (ExprPath {
David Tolnay8c91b882017-12-28 23:04:32 -05002046 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002047 qself: pair.0,
2048 path: pair.1,
2049 })
2050 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002051
2052 fn description() -> Option<&'static str> {
2053 Some("path: `a::b::c`")
2054 }
Alex Crichton954046c2017-05-30 21:49:42 -07002055 }
David Tolnay42602292016-10-01 22:25:45 -07002056
Michael Layzell734adb42017-06-07 16:58:31 -04002057 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002058 named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member)));
David Tolnay438c9052016-10-07 23:24:48 -07002059
David Tolnay8875fca2017-12-31 13:52:37 -05002060 named!(and_index -> (token::Bracket, Expr), brackets!(syn!(Expr)));
David Tolnay438c9052016-10-07 23:24:48 -07002061
Michael Layzell734adb42017-06-07 16:58:31 -04002062 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002063 impl Synom for Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002064 named!(parse -> Self, do_parse!(
David Tolnaye64213b2017-12-30 00:24:20 -05002065 stmts: braces!(Block::parse_within) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002066 (Block {
David Tolnay8875fca2017-12-31 13:52:37 -05002067 brace_token: stmts.0,
2068 stmts: stmts.1,
Michael Layzell92639a52017-06-01 00:07:44 -04002069 })
2070 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002071
2072 fn description() -> Option<&'static str> {
2073 Some("block: `{ .. }`")
2074 }
Alex Crichton954046c2017-05-30 21:49:42 -07002075 }
David Tolnay939766a2016-09-23 23:48:12 -07002076
Michael Layzell734adb42017-06-07 16:58:31 -04002077 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002078 impl Block {
Michael Layzell92639a52017-06-01 00:07:44 -04002079 named!(pub parse_within -> Vec<Stmt>, do_parse!(
David Tolnay4699a312017-12-27 14:39:22 -05002080 many0!(punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002081 mut standalone: many0!(do_parse!(
2082 stmt: syn!(Stmt) >>
2083 many0!(punct!(;)) >>
2084 (stmt)
2085 )) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002086 last: option!(do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002087 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton70bbd592017-08-27 10:40:03 -07002088 mut e: syn!(Expr) >>
2089 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002090 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002091 Stmt::Expr(e)
Alex Crichton70bbd592017-08-27 10:40:03 -07002092 })
2093 )) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002094 (match last {
2095 None => standalone,
2096 Some(last) => {
Alex Crichton70bbd592017-08-27 10:40:03 -07002097 standalone.push(last);
Michael Layzell92639a52017-06-01 00:07:44 -04002098 standalone
2099 }
2100 })
2101 ));
Alex Crichton954046c2017-05-30 21:49:42 -07002102 }
2103
Michael Layzell734adb42017-06-07 16:58:31 -04002104 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002105 impl Synom for Stmt {
Michael Layzell92639a52017-06-01 00:07:44 -04002106 named!(parse -> Self, alt!(
2107 stmt_mac
2108 |
2109 stmt_local
2110 |
2111 stmt_item
2112 |
Michael Layzell35418782017-06-07 09:20:25 -04002113 stmt_blockexpr
2114 |
Michael Layzell92639a52017-06-01 00:07:44 -04002115 stmt_expr
2116 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002117
2118 fn description() -> Option<&'static str> {
2119 Some("statement")
2120 }
Alex Crichton954046c2017-05-30 21:49:42 -07002121 }
David Tolnay939766a2016-09-23 23:48:12 -07002122
Michael Layzell734adb42017-06-07 16:58:31 -04002123 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002124 named!(stmt_mac -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002125 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002126 what: syn!(Path) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002127 bang: punct!(!) >>
David Tolnayeea28d62016-10-25 20:44:08 -07002128 // Only parse braces here; paren and bracket will get parsed as
2129 // expression statements
Alex Crichton954046c2017-05-30 21:49:42 -07002130 data: braces!(syn!(TokenStream)) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002131 semi: option!(punct!(;)) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002132 (Stmt::Item(Item::Macro(ItemMacro {
David Tolnay57b52bc2017-12-28 18:06:38 -05002133 attrs: attrs,
2134 ident: None,
2135 mac: Macro {
David Tolnay5d55ef72016-12-21 20:20:04 -05002136 path: what,
Alex Crichton954046c2017-05-30 21:49:42 -07002137 bang_token: bang,
David Tolnay8875fca2017-12-31 13:52:37 -05002138 delimiter: MacroDelimiter::Brace(data.0),
2139 tts: data.1,
David Tolnayeea28d62016-10-25 20:44:08 -07002140 },
David Tolnay57b52bc2017-12-28 18:06:38 -05002141 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002142 })))
David Tolnay13b3d352016-10-03 00:31:15 -07002143 ));
2144
Michael Layzell734adb42017-06-07 16:58:31 -04002145 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07002146 named!(stmt_local -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002147 attrs: many0!(Attribute::parse_outer) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002148 let_: keyword!(let) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002149 pat: syn!(Pat) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -08002150 ty: option!(tuple!(punct!(:), syn!(Type))) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002151 init: option!(tuple!(punct!(=), syn!(Expr))) >>
2152 semi: punct!(;) >>
David Tolnay1f0b7b82018-01-06 16:07:14 -08002153 (Stmt::Local(Local {
David Tolnay191e0582016-10-02 18:31:09 -07002154 attrs: attrs,
David Tolnay8b4d3022017-12-29 12:11:10 -05002155 let_token: let_,
2156 pat: Box::new(pat),
2157 ty: ty.map(|(colon, ty)| (colon, Box::new(ty))),
2158 init: init.map(|(eq, expr)| (eq, Box::new(expr))),
2159 semi_token: semi,
David Tolnay1f0b7b82018-01-06 16:07:14 -08002160 }))
David Tolnay191e0582016-10-02 18:31:09 -07002161 ));
2162
Michael Layzell734adb42017-06-07 16:58:31 -04002163 #[cfg(feature = "full")]
David Tolnay1f0b7b82018-01-06 16:07:14 -08002164 named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(i)));
David Tolnay191e0582016-10-02 18:31:09 -07002165
Michael Layzell734adb42017-06-07 16:58:31 -04002166 #[cfg(feature = "full")]
Michael Layzell35418782017-06-07 09:20:25 -04002167 named!(stmt_blockexpr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002168 attrs: many0!(Attribute::parse_outer) >>
Michael Layzell35418782017-06-07 09:20:25 -04002169 mut e: expr_nosemi >>
2170 // If the next token is a `.` or a `?` it is special-cased to parse as
2171 // an expression instead of a blockexpression.
David Tolnayf8db7ba2017-11-11 22:52:16 -08002172 not!(punct!(.)) >>
2173 not!(punct!(?)) >>
2174 semi: option!(punct!(;)) >>
Michael Layzell35418782017-06-07 09:20:25 -04002175 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002176 e.replace_attrs(attrs);
Michael Layzell35418782017-06-07 09:20:25 -04002177 if let Some(semi) = semi {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002178 Stmt::Semi(e, semi)
Michael Layzell35418782017-06-07 09:20:25 -04002179 } else {
David Tolnay1f0b7b82018-01-06 16:07:14 -08002180 Stmt::Expr(e)
Michael Layzell35418782017-06-07 09:20:25 -04002181 }
2182 })
2183 ));
David Tolnaycfe55022016-10-02 22:02:27 -07002184
Michael Layzell734adb42017-06-07 16:58:31 -04002185 #[cfg(feature = "full")]
David Tolnaycfe55022016-10-02 22:02:27 -07002186 named!(stmt_expr -> Stmt, do_parse!(
David Tolnay2c136452017-12-27 14:13:32 -05002187 attrs: many0!(Attribute::parse_outer) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002188 mut e: syn!(Expr) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002189 semi: punct!(;) >>
David Tolnay7184b132016-10-30 10:06:37 -07002190 ({
David Tolnay2ae520a2017-12-29 11:19:50 -05002191 e.replace_attrs(attrs);
David Tolnay1f0b7b82018-01-06 16:07:14 -08002192 Stmt::Semi(e, semi)
David Tolnaycfe55022016-10-02 22:02:27 -07002193 })
David Tolnay939766a2016-09-23 23:48:12 -07002194 ));
David Tolnay8b07f372016-09-30 10:28:40 -07002195
Michael Layzell734adb42017-06-07 16:58:31 -04002196 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002197 impl Synom for Pat {
Michael Layzell92639a52017-06-01 00:07:44 -04002198 named!(parse -> Self, alt!(
2199 syn!(PatWild) => { Pat::Wild } // must be before pat_ident
2200 |
2201 syn!(PatBox) => { Pat::Box } // must be before pat_ident
2202 |
2203 syn!(PatRange) => { Pat::Range } // must be before pat_lit
2204 |
2205 syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident
2206 |
2207 syn!(PatStruct) => { Pat::Struct } // must be before pat_ident
2208 |
David Tolnay323279a2017-12-29 11:26:32 -05002209 syn!(PatMacro) => { Pat::Macro } // must be before pat_ident
Michael Layzell92639a52017-06-01 00:07:44 -04002210 |
2211 syn!(PatLit) => { Pat::Lit } // must be before pat_ident
2212 |
2213 syn!(PatIdent) => { Pat::Ident } // must be before pat_path
2214 |
2215 syn!(PatPath) => { Pat::Path }
2216 |
2217 syn!(PatTuple) => { Pat::Tuple }
2218 |
2219 syn!(PatRef) => { Pat::Ref }
2220 |
2221 syn!(PatSlice) => { Pat::Slice }
2222 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002223
2224 fn description() -> Option<&'static str> {
2225 Some("pattern")
2226 }
Alex Crichton954046c2017-05-30 21:49:42 -07002227 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07002228
Michael Layzell734adb42017-06-07 16:58:31 -04002229 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002230 impl Synom for PatWild {
Michael Layzell92639a52017-06-01 00:07:44 -04002231 named!(parse -> Self, map!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002232 punct!(_),
Michael Layzell92639a52017-06-01 00:07:44 -04002233 |u| PatWild { underscore_token: u }
2234 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002235
2236 fn description() -> Option<&'static str> {
2237 Some("wild pattern: `_`")
2238 }
Alex Crichton954046c2017-05-30 21:49:42 -07002239 }
David Tolnay84aa0752016-10-02 23:01:13 -07002240
Michael Layzell734adb42017-06-07 16:58:31 -04002241 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002242 impl Synom for PatBox {
Michael Layzell92639a52017-06-01 00:07:44 -04002243 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002244 boxed: keyword!(box) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002245 pat: syn!(Pat) >>
2246 (PatBox {
2247 pat: Box::new(pat),
2248 box_token: boxed,
2249 })
2250 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002251
2252 fn description() -> Option<&'static str> {
2253 Some("box pattern")
2254 }
Alex Crichton954046c2017-05-30 21:49:42 -07002255 }
2256
Michael Layzell734adb42017-06-07 16:58:31 -04002257 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002258 impl Synom for PatIdent {
Michael Layzell92639a52017-06-01 00:07:44 -04002259 named!(parse -> Self, do_parse!(
David Tolnay24237fb2017-12-29 02:15:26 -05002260 by_ref: option!(keyword!(ref)) >>
2261 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002262 name: alt!(
2263 syn!(Ident)
2264 |
David Tolnayf8db7ba2017-11-11 22:52:16 -08002265 keyword!(self) => { Into::into }
Michael Layzell92639a52017-06-01 00:07:44 -04002266 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002267 not!(punct!(<)) >>
2268 not!(punct!(::)) >>
2269 subpat: option!(tuple!(punct!(@), syn!(Pat))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002270 (PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002271 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002272 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002273 ident: name,
David Tolnay8b4d3022017-12-29 12:11:10 -05002274 subpat: subpat.map(|(at, pat)| (at, Box::new(pat))),
Michael Layzell92639a52017-06-01 00:07:44 -04002275 })
2276 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002277
2278 fn description() -> Option<&'static str> {
2279 Some("pattern identifier binding")
2280 }
Alex Crichton954046c2017-05-30 21:49:42 -07002281 }
2282
Michael Layzell734adb42017-06-07 16:58:31 -04002283 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002284 impl Synom for PatTupleStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002285 named!(parse -> Self, do_parse!(
2286 path: syn!(Path) >>
2287 tuple: syn!(PatTuple) >>
2288 (PatTupleStruct {
2289 path: path,
2290 pat: tuple,
2291 })
2292 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002293
2294 fn description() -> Option<&'static str> {
2295 Some("tuple struct pattern")
2296 }
Alex Crichton954046c2017-05-30 21:49:42 -07002297 }
2298
Michael Layzell734adb42017-06-07 16:58:31 -04002299 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002300 impl Synom for PatStruct {
Michael Layzell92639a52017-06-01 00:07:44 -04002301 named!(parse -> Self, do_parse!(
2302 path: syn!(Path) >>
2303 data: braces!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002304 fields: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002305 base: option!(cond!(fields.empty_or_trailing(), punct!(..))) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002306 (fields, base)
2307 )) >>
2308 (PatStruct {
2309 path: path,
David Tolnay8875fca2017-12-31 13:52:37 -05002310 fields: (data.1).0,
2311 brace_token: data.0,
2312 dot2_token: (data.1).1.and_then(|m| m),
Michael Layzell92639a52017-06-01 00:07:44 -04002313 })
2314 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002315
2316 fn description() -> Option<&'static str> {
2317 Some("struct pattern")
2318 }
Alex Crichton954046c2017-05-30 21:49:42 -07002319 }
2320
Michael Layzell734adb42017-06-07 16:58:31 -04002321 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002322 impl Synom for FieldPat {
Michael Layzell92639a52017-06-01 00:07:44 -04002323 named!(parse -> Self, alt!(
2324 do_parse!(
David Tolnay85b69a42017-12-27 20:43:10 -05002325 member: syn!(Member) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -08002326 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002327 pat: syn!(Pat) >>
2328 (FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002329 member: member,
Michael Layzell92639a52017-06-01 00:07:44 -04002330 pat: Box::new(pat),
Michael Layzell92639a52017-06-01 00:07:44 -04002331 attrs: Vec::new(),
2332 colon_token: Some(colon),
2333 })
2334 )
2335 |
2336 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002337 boxed: option!(keyword!(box)) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002338 by_ref: option!(keyword!(ref)) >>
2339 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002340 ident: syn!(Ident) >>
2341 ({
2342 let mut pat: Pat = PatIdent {
David Tolnay24237fb2017-12-29 02:15:26 -05002343 by_ref: by_ref,
David Tolnayefc96fb2017-12-29 02:03:15 -05002344 mutability: mutability,
David Tolnaybb4ca9f2017-12-26 12:28:58 -05002345 ident: ident,
Michael Layzell92639a52017-06-01 00:07:44 -04002346 subpat: None,
Michael Layzell92639a52017-06-01 00:07:44 -04002347 }.into();
2348 if let Some(boxed) = boxed {
2349 pat = PatBox {
2350 pat: Box::new(pat),
2351 box_token: boxed,
2352 }.into();
2353 }
2354 FieldPat {
David Tolnay85b69a42017-12-27 20:43:10 -05002355 member: Member::Named(ident),
Alex Crichton954046c2017-05-30 21:49:42 -07002356 pat: Box::new(pat),
Alex Crichton954046c2017-05-30 21:49:42 -07002357 attrs: Vec::new(),
Michael Layzell92639a52017-06-01 00:07:44 -04002358 colon_token: None,
2359 }
2360 })
2361 )
2362 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002363
2364 fn description() -> Option<&'static str> {
2365 Some("field pattern")
2366 }
Alex Crichton954046c2017-05-30 21:49:42 -07002367 }
2368
Michael Layzell734adb42017-06-07 16:58:31 -04002369 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002370 impl Synom for Member {
2371 named!(parse -> Self, alt!(
2372 syn!(Ident) => { Member::Named }
2373 |
2374 syn!(Index) => { Member::Unnamed }
2375 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002376
2377 fn description() -> Option<&'static str> {
2378 Some("field member")
2379 }
David Tolnay85b69a42017-12-27 20:43:10 -05002380 }
2381
2382 #[cfg(feature = "full")]
2383 impl Synom for Index {
2384 named!(parse -> Self, do_parse!(
David Tolnay360efd22018-01-04 23:35:26 -08002385 lit: syn!(LitInt) >>
Alex Crichton954046c2017-05-30 21:49:42 -07002386 ({
David Tolnay360efd22018-01-04 23:35:26 -08002387 if let IntSuffix::None = lit.suffix() {
2388 Index { index: lit.value() as u32, span: lit.span }
Alex Crichton954046c2017-05-30 21:49:42 -07002389 } else {
Michael Layzell92639a52017-06-01 00:07:44 -04002390 return parse_error();
David Tolnayda167382016-10-30 13:34:09 -07002391 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002392 })
David Tolnay85b69a42017-12-27 20:43:10 -05002393 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002394
2395 fn description() -> Option<&'static str> {
2396 Some("field index")
2397 }
David Tolnay85b69a42017-12-27 20:43:10 -05002398 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07002399
Michael Layzell734adb42017-06-07 16:58:31 -04002400 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002401 impl Synom for PatPath {
Michael Layzell92639a52017-06-01 00:07:44 -04002402 named!(parse -> Self, map!(
2403 syn!(ExprPath),
David Tolnaybc7d7d92017-06-03 20:54:05 -07002404 |p| PatPath { qself: p.qself, path: p.path }
Michael Layzell92639a52017-06-01 00:07:44 -04002405 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002406
2407 fn description() -> Option<&'static str> {
2408 Some("path pattern")
2409 }
Alex Crichton954046c2017-05-30 21:49:42 -07002410 }
David Tolnay9636c052016-10-02 17:11:17 -07002411
Michael Layzell734adb42017-06-07 16:58:31 -04002412 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002413 impl Synom for PatTuple {
Michael Layzell92639a52017-06-01 00:07:44 -04002414 named!(parse -> Self, do_parse!(
2415 data: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002416 front: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05002417 dotdot: option!(cond_reduce!(front.empty_or_trailing(),
2418 tuple!(punct!(..), option!(punct!(,)))
2419 )) >>
David Tolnay41871922017-12-29 01:53:45 -05002420 back: cond!(match dotdot {
Michael Layzell92639a52017-06-01 00:07:44 -04002421 Some((_, Some(_))) => true,
2422 _ => false,
2423 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002424 Punctuated::parse_terminated) >>
David Tolnay41871922017-12-29 01:53:45 -05002425 (front, dotdot, back)
Michael Layzell92639a52017-06-01 00:07:44 -04002426 )) >>
2427 ({
David Tolnay8875fca2017-12-31 13:52:37 -05002428 let (parens, (front, dotdot, back)) = data;
Michael Layzell92639a52017-06-01 00:07:44 -04002429 let (dotdot, trailing) = match dotdot {
2430 Some((a, b)) => (Some(a), Some(b)),
2431 None => (None, None),
2432 };
2433 PatTuple {
2434 paren_token: parens,
David Tolnay41871922017-12-29 01:53:45 -05002435 front: front,
Michael Layzell92639a52017-06-01 00:07:44 -04002436 dot2_token: dotdot,
David Tolnay41871922017-12-29 01:53:45 -05002437 comma_token: trailing.unwrap_or_default(),
2438 back: back.unwrap_or_default(),
Michael Layzell92639a52017-06-01 00:07:44 -04002439 }
2440 })
2441 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002442
2443 fn description() -> Option<&'static str> {
2444 Some("tuple pattern")
2445 }
Alex Crichton954046c2017-05-30 21:49:42 -07002446 }
David Tolnayfbb73232016-10-03 01:00:06 -07002447
Michael Layzell734adb42017-06-07 16:58:31 -04002448 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002449 impl Synom for PatRef {
Michael Layzell92639a52017-06-01 00:07:44 -04002450 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002451 and: punct!(&) >>
David Tolnay24237fb2017-12-29 02:15:26 -05002452 mutability: option!(keyword!(mut)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002453 pat: syn!(Pat) >>
2454 (PatRef {
2455 pat: Box::new(pat),
David Tolnay24237fb2017-12-29 02:15:26 -05002456 mutability: mutability,
Michael Layzell92639a52017-06-01 00:07:44 -04002457 and_token: and,
2458 })
2459 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002460
2461 fn description() -> Option<&'static str> {
2462 Some("reference pattern")
2463 }
Alex Crichton954046c2017-05-30 21:49:42 -07002464 }
David Tolnayffdb97f2016-10-03 01:28:33 -07002465
Michael Layzell734adb42017-06-07 16:58:31 -04002466 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002467 impl Synom for PatLit {
Michael Layzell92639a52017-06-01 00:07:44 -04002468 named!(parse -> Self, do_parse!(
2469 lit: pat_lit_expr >>
David Tolnay8c91b882017-12-28 23:04:32 -05002470 (if let Expr::Path(_) = lit {
Michael Layzell92639a52017-06-01 00:07:44 -04002471 return parse_error(); // these need to be parsed by pat_path
2472 } else {
2473 PatLit {
2474 expr: Box::new(lit),
2475 }
2476 })
2477 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002478
2479 fn description() -> Option<&'static str> {
2480 Some("literal pattern")
2481 }
Alex Crichton954046c2017-05-30 21:49:42 -07002482 }
David Tolnaye1310902016-10-29 23:40:00 -07002483
Michael Layzell734adb42017-06-07 16:58:31 -04002484 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002485 impl Synom for PatRange {
Michael Layzell92639a52017-06-01 00:07:44 -04002486 named!(parse -> Self, do_parse!(
2487 lo: pat_lit_expr >>
2488 limits: syn!(RangeLimits) >>
2489 hi: pat_lit_expr >>
2490 (PatRange {
2491 lo: Box::new(lo),
2492 hi: Box::new(hi),
2493 limits: limits,
2494 })
2495 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002496
2497 fn description() -> Option<&'static str> {
2498 Some("range pattern")
2499 }
Alex Crichton954046c2017-05-30 21:49:42 -07002500 }
David Tolnaye1310902016-10-29 23:40:00 -07002501
Michael Layzell734adb42017-06-07 16:58:31 -04002502 #[cfg(feature = "full")]
David Tolnay2cfddc62016-10-30 01:03:27 -07002503 named!(pat_lit_expr -> Expr, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002504 neg: option!(punct!(-)) >>
David Tolnay2cfddc62016-10-30 01:03:27 -07002505 v: alt!(
David Tolnay8c91b882017-12-28 23:04:32 -05002506 syn!(ExprLit) => { Expr::Lit }
David Tolnay2cfddc62016-10-30 01:03:27 -07002507 |
David Tolnay8c91b882017-12-28 23:04:32 -05002508 syn!(ExprPath) => { Expr::Path }
David Tolnay2cfddc62016-10-30 01:03:27 -07002509 ) >>
David Tolnayc29b9892017-12-27 22:58:14 -05002510 (if let Some(neg) = neg {
David Tolnay8c91b882017-12-28 23:04:32 -05002511 Expr::Unary(ExprUnary {
2512 attrs: Vec::new(),
David Tolnayc29b9892017-12-27 22:58:14 -05002513 op: UnOp::Neg(neg),
David Tolnay3bc597f2017-12-31 02:31:11 -05002514 expr: Box::new(v)
2515 })
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002516 } else {
David Tolnay3bc597f2017-12-31 02:31:11 -05002517 v
David Tolnay0ad9e9f2016-10-29 22:20:02 -07002518 })
2519 ));
David Tolnay8b308c22016-10-03 01:24:10 -07002520
Michael Layzell734adb42017-06-07 16:58:31 -04002521 #[cfg(feature = "full")]
Alex Crichton954046c2017-05-30 21:49:42 -07002522 impl Synom for PatSlice {
Michael Layzell92639a52017-06-01 00:07:44 -04002523 named!(parse -> Self, map!(
2524 brackets!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -05002525 before: call!(Punctuated::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002526 middle: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -08002527 dots: punct!(..) >>
2528 trailing: option!(punct!(,)) >>
Michael Layzell92639a52017-06-01 00:07:44 -04002529 (dots, trailing)
2530 )) >>
2531 after: cond!(
2532 match middle {
2533 Some((_, ref trailing)) => trailing.is_some(),
2534 _ => false,
2535 },
David Tolnayf2cfd722017-12-31 18:02:51 -05002536 Punctuated::parse_terminated
Michael Layzell92639a52017-06-01 00:07:44 -04002537 ) >>
2538 (before, middle, after)
2539 )),
David Tolnay8875fca2017-12-31 13:52:37 -05002540 |(brackets, (before, middle, after))| {
David Tolnayf2cfd722017-12-31 18:02:51 -05002541 let mut before: Punctuated<Pat, Token![,]> = before;
2542 let after: Option<Punctuated<Pat, Token![,]>> = after;
David Tolnayf8db7ba2017-11-11 22:52:16 -08002543 let middle: Option<(Token![..], Option<Token![,]>)> = middle;
Michael Layzell92639a52017-06-01 00:07:44 -04002544 PatSlice {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002545 dot2_token: middle.as_ref().map(|m| Token![..]((m.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -04002546 comma_token: middle.as_ref().and_then(|m| {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002547 m.1.as_ref().map(|m| Token![,](m.0))
Michael Layzell92639a52017-06-01 00:07:44 -04002548 }),
2549 bracket_token: brackets,
2550 middle: middle.and_then(|_| {
David Tolnaydc03aec2017-12-30 01:54:18 -05002551 if before.empty_or_trailing() {
Michael Layzell92639a52017-06-01 00:07:44 -04002552 None
David Tolnaydc03aec2017-12-30 01:54:18 -05002553 } else {
David Tolnay56080682018-01-06 14:01:52 -08002554 Some(Box::new(before.pop().unwrap().into_value()))
Michael Layzell92639a52017-06-01 00:07:44 -04002555 }
2556 }),
2557 front: before,
2558 back: after.unwrap_or_default(),
David Tolnaye1f13c32016-10-29 23:34:40 -07002559 }
Alex Crichton954046c2017-05-30 21:49:42 -07002560 }
Michael Layzell92639a52017-06-01 00:07:44 -04002561 ));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002562
2563 fn description() -> Option<&'static str> {
2564 Some("slice pattern")
2565 }
Alex Crichton954046c2017-05-30 21:49:42 -07002566 }
David Tolnay323279a2017-12-29 11:26:32 -05002567
2568 #[cfg(feature = "full")]
2569 impl Synom for PatMacro {
2570 named!(parse -> Self, map!(syn!(Macro), |mac| PatMacro { mac: mac }));
Sergio Benitez5680d6a2017-12-29 11:20:29 -08002571
2572 fn description() -> Option<&'static str> {
2573 Some("macro pattern")
2574 }
David Tolnay323279a2017-12-29 11:26:32 -05002575 }
David Tolnayb9c8e322016-09-23 20:48:37 -07002576}
2577
David Tolnayf4bbbd92016-09-23 14:41:55 -07002578#[cfg(feature = "printing")]
2579mod printing {
2580 use super::*;
Michael Layzell734adb42017-06-07 16:58:31 -04002581 #[cfg(feature = "full")]
David Tolnay13b3d352016-10-03 00:31:15 -07002582 use attr::FilterAttrs;
David Tolnay51382052017-12-27 13:46:21 -05002583 use quote::{ToTokens, Tokens};
David Tolnay61037c62018-01-05 16:21:03 -08002584 use proc_macro2::{Literal, TokenNode, TokenTree};
David Tolnayf4bbbd92016-09-23 14:41:55 -07002585
David Tolnaybcf26022017-12-25 22:10:52 -05002586 // If the given expression is a bare `ExprStruct`, wraps it in parenthesis
2587 // before appending it to `Tokens`.
Michael Layzell3936ceb2017-07-08 00:28:36 -04002588 #[cfg(feature = "full")]
2589 fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) {
David Tolnay8c91b882017-12-28 23:04:32 -05002590 if let Expr::Struct(_) = *e {
David Tolnay32954ef2017-12-26 22:43:16 -05002591 token::Paren::default().surround(tokens, |tokens| {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002592 e.to_tokens(tokens);
2593 });
2594 } else {
2595 e.to_tokens(tokens);
2596 }
2597 }
2598
David Tolnay8c91b882017-12-28 23:04:32 -05002599 #[cfg(feature = "full")]
2600 fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) {
2601 tokens.append_all(attrs.outer());
2602 }
Michael Layzell734adb42017-06-07 16:58:31 -04002603
David Tolnay8c91b882017-12-28 23:04:32 -05002604 #[cfg(not(feature = "full"))]
David Tolnay61037c62018-01-05 16:21:03 -08002605 fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {}
Alex Crichton62a0a592017-05-22 13:58:53 -07002606
Michael Layzell734adb42017-06-07 16:58:31 -04002607 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002608 impl ToTokens for ExprBox {
2609 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002610 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002611 self.box_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002612 self.expr.to_tokens(tokens);
2613 }
2614 }
2615
Michael Layzell734adb42017-06-07 16:58:31 -04002616 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002617 impl ToTokens for ExprInPlace {
2618 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002619 tokens.append_all(self.attrs.outer());
David Tolnay8701a5c2017-12-28 23:31:10 -05002620 self.place.to_tokens(tokens);
2621 self.arrow_token.to_tokens(tokens);
2622 self.value.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002623 }
2624 }
2625
Michael Layzell734adb42017-06-07 16:58:31 -04002626 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002627 impl ToTokens for ExprArray {
2628 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002629 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002630 self.bracket_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002631 self.elems.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002632 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002633 }
2634 }
2635
2636 impl ToTokens for ExprCall {
2637 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002638 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002639 self.func.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002640 self.paren_token.surround(tokens, |tokens| {
2641 self.args.to_tokens(tokens);
2642 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002643 }
2644 }
2645
Michael Layzell734adb42017-06-07 16:58:31 -04002646 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002647 impl ToTokens for ExprMethodCall {
2648 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002649 tokens.append_all(self.attrs.outer());
David Tolnay76418512017-12-28 23:47:47 -05002650 self.receiver.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002651 self.dot_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002652 self.method.to_tokens(tokens);
David Tolnayd60cfec2017-12-29 00:21:38 -05002653 self.turbofish.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002654 self.paren_token.surround(tokens, |tokens| {
2655 self.args.to_tokens(tokens);
2656 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002657 }
2658 }
2659
Michael Layzell734adb42017-06-07 16:58:31 -04002660 #[cfg(feature = "full")]
David Tolnayd60cfec2017-12-29 00:21:38 -05002661 impl ToTokens for MethodTurbofish {
2662 fn to_tokens(&self, tokens: &mut Tokens) {
2663 self.colon2_token.to_tokens(tokens);
2664 self.lt_token.to_tokens(tokens);
2665 self.args.to_tokens(tokens);
2666 self.gt_token.to_tokens(tokens);
2667 }
2668 }
2669
2670 #[cfg(feature = "full")]
2671 impl ToTokens for GenericMethodArgument {
2672 fn to_tokens(&self, tokens: &mut Tokens) {
2673 match *self {
2674 GenericMethodArgument::Type(ref t) => t.to_tokens(tokens),
2675 GenericMethodArgument::Const(ref c) => c.to_tokens(tokens),
2676 }
2677 }
2678 }
2679
2680 #[cfg(feature = "full")]
David Tolnay05362582017-12-26 01:33:57 -05002681 impl ToTokens for ExprTuple {
Alex Crichton62a0a592017-05-22 13:58:53 -07002682 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002683 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002684 self.paren_token.surround(tokens, |tokens| {
David Tolnay2a86fdd2017-12-28 23:34:28 -05002685 self.elems.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002686 // If we only have one argument, we need a trailing comma to
David Tolnay05362582017-12-26 01:33:57 -05002687 // distinguish ExprTuple from ExprParen.
David Tolnaya0834b42018-01-01 21:30:02 -08002688 if self.elems.len() == 1 && !self.elems.trailing_punct() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002689 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002690 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002691 })
Alex Crichton62a0a592017-05-22 13:58:53 -07002692 }
2693 }
2694
2695 impl ToTokens for ExprBinary {
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.left.to_tokens(tokens);
2699 self.op.to_tokens(tokens);
2700 self.right.to_tokens(tokens);
2701 }
2702 }
2703
2704 impl ToTokens for ExprUnary {
2705 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002706 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002707 self.op.to_tokens(tokens);
2708 self.expr.to_tokens(tokens);
2709 }
2710 }
2711
David Tolnay8c91b882017-12-28 23:04:32 -05002712 impl ToTokens for ExprLit {
2713 fn to_tokens(&self, tokens: &mut Tokens) {
2714 attrs_to_tokens(&self.attrs, tokens);
2715 self.lit.to_tokens(tokens);
2716 }
2717 }
2718
Alex Crichton62a0a592017-05-22 13:58:53 -07002719 impl ToTokens for ExprCast {
2720 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002721 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002722 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002723 self.as_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002724 self.ty.to_tokens(tokens);
2725 }
2726 }
2727
David Tolnay0cf94f22017-12-28 23:46:26 -05002728 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002729 impl ToTokens for ExprType {
2730 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002731 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002732 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002733 self.colon_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002734 self.ty.to_tokens(tokens);
2735 }
2736 }
2737
Michael Layzell734adb42017-06-07 16:58:31 -04002738 #[cfg(feature = "full")]
David Tolnay61037c62018-01-05 16:21:03 -08002739 fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002740 if let Some((ref else_token, ref else_)) = *else_ {
2741 else_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002742
2743 // If we are not one of the valid expressions to exist in an else
2744 // clause, wrap ourselves in a block.
David Tolnay2ccf32a2017-12-29 00:34:26 -05002745 match **else_ {
David Tolnay8c91b882017-12-28 23:04:32 -05002746 Expr::If(_) | Expr::IfLet(_) | Expr::Block(_) => {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002747 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002748 }
2749 _ => {
David Tolnay32954ef2017-12-26 22:43:16 -05002750 token::Brace::default().surround(tokens, |tokens| {
David Tolnay2ccf32a2017-12-29 00:34:26 -05002751 else_.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002752 });
2753 }
2754 }
2755 }
2756 }
2757
2758 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002759 impl ToTokens for ExprIf {
2760 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002761 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002762 self.if_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002763 wrap_bare_struct(tokens, &self.cond);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002764 self.then_branch.to_tokens(tokens);
2765 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002766 }
2767 }
2768
Michael Layzell734adb42017-06-07 16:58:31 -04002769 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002770 impl ToTokens for ExprIfLet {
2771 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002772 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002773 self.if_token.to_tokens(tokens);
2774 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002775 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002776 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002777 wrap_bare_struct(tokens, &self.expr);
David Tolnay2ccf32a2017-12-29 00:34:26 -05002778 self.then_branch.to_tokens(tokens);
2779 maybe_wrap_else(tokens, &self.else_branch);
Alex Crichton62a0a592017-05-22 13:58:53 -07002780 }
2781 }
2782
Michael Layzell734adb42017-06-07 16:58:31 -04002783 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002784 impl ToTokens for ExprWhile {
2785 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002786 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002787 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002788 self.while_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002789 wrap_bare_struct(tokens, &self.cond);
Alex Crichton62a0a592017-05-22 13:58:53 -07002790 self.body.to_tokens(tokens);
2791 }
2792 }
2793
Michael Layzell734adb42017-06-07 16:58:31 -04002794 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002795 impl ToTokens for ExprWhileLet {
2796 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002797 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002798 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002799 self.while_token.to_tokens(tokens);
2800 self.let_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002801 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002802 self.eq_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002803 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002804 self.body.to_tokens(tokens);
2805 }
2806 }
2807
Michael Layzell734adb42017-06-07 16:58:31 -04002808 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002809 impl ToTokens for ExprForLoop {
2810 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002811 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002812 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002813 self.for_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002814 self.pat.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002815 self.in_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002816 wrap_bare_struct(tokens, &self.expr);
Alex Crichton62a0a592017-05-22 13:58:53 -07002817 self.body.to_tokens(tokens);
2818 }
2819 }
2820
Michael Layzell734adb42017-06-07 16:58:31 -04002821 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002822 impl ToTokens for ExprLoop {
2823 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002824 tokens.append_all(self.attrs.outer());
David Tolnaybcd498f2017-12-29 12:02:33 -05002825 self.label.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002826 self.loop_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002827 self.body.to_tokens(tokens);
2828 }
2829 }
2830
Michael Layzell734adb42017-06-07 16:58:31 -04002831 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002832 impl ToTokens for ExprMatch {
2833 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002834 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002835 self.match_token.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002836 wrap_bare_struct(tokens, &self.expr);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002837 self.brace_token.surround(tokens, |tokens| {
David Tolnay51382052017-12-27 13:46:21 -05002838 for (i, arm) in self.arms.iter().enumerate() {
Michael Layzell3936ceb2017-07-08 00:28:36 -04002839 arm.to_tokens(tokens);
2840 // Ensure that we have a comma after a non-block arm, except
2841 // for the last one.
2842 let is_last = i == self.arms.len() - 1;
Alex Crichton03b30272017-08-28 09:35:24 -07002843 if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08002844 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04002845 }
2846 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002847 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002848 }
2849 }
2850
Michael Layzell734adb42017-06-07 16:58:31 -04002851 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002852 impl ToTokens for ExprCatch {
2853 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002854 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002855 self.do_token.to_tokens(tokens);
2856 self.catch_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002857 self.block.to_tokens(tokens);
2858 }
2859 }
2860
Michael Layzell734adb42017-06-07 16:58:31 -04002861 #[cfg(feature = "full")]
Alex Crichtonfe110462017-06-01 12:49:27 -07002862 impl ToTokens for ExprYield {
2863 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002864 tokens.append_all(self.attrs.outer());
Alex Crichtonfe110462017-06-01 12:49:27 -07002865 self.yield_token.to_tokens(tokens);
2866 self.expr.to_tokens(tokens);
2867 }
2868 }
2869
2870 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002871 impl ToTokens for ExprClosure {
2872 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002873 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002874 self.capture.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002875 self.or1_token.to_tokens(tokens);
David Tolnay56080682018-01-06 14:01:52 -08002876 for input in self.inputs.pairs() {
2877 match **input.value() {
David Tolnay51382052017-12-27 13:46:21 -05002878 FnArg::Captured(ArgCaptured {
2879 ref pat,
2880 ty: Type::Infer(_),
2881 ..
2882 }) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07002883 pat.to_tokens(tokens);
David Tolnay9636c052016-10-02 17:11:17 -07002884 }
David Tolnay56080682018-01-06 14:01:52 -08002885 _ => input.value().to_tokens(tokens),
David Tolnay3c2467c2016-10-02 17:55:08 -07002886 }
David Tolnayf2cfd722017-12-31 18:02:51 -05002887 input.punct().to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07002888 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002889 self.or2_token.to_tokens(tokens);
David Tolnay7f675742017-12-27 22:43:21 -05002890 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002891 self.body.to_tokens(tokens);
2892 }
2893 }
2894
Michael Layzell734adb42017-06-07 16:58:31 -04002895 #[cfg(feature = "full")]
Nika Layzell640832a2017-12-04 13:37:09 -05002896 impl ToTokens for ExprUnsafe {
2897 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002898 tokens.append_all(self.attrs.outer());
Nika Layzell640832a2017-12-04 13:37:09 -05002899 self.unsafe_token.to_tokens(tokens);
2900 self.block.to_tokens(tokens);
2901 }
2902 }
2903
2904 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002905 impl ToTokens for ExprBlock {
2906 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002907 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002908 self.block.to_tokens(tokens);
2909 }
2910 }
2911
Michael Layzell734adb42017-06-07 16:58:31 -04002912 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002913 impl ToTokens for ExprAssign {
2914 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002915 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002916 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002917 self.eq_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002918 self.right.to_tokens(tokens);
2919 }
2920 }
2921
Michael Layzell734adb42017-06-07 16:58:31 -04002922 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002923 impl ToTokens for ExprAssignOp {
2924 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002925 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002926 self.left.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002927 self.op.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002928 self.right.to_tokens(tokens);
2929 }
2930 }
2931
Michael Layzell734adb42017-06-07 16:58:31 -04002932 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002933 impl ToTokens for ExprField {
2934 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002935 tokens.append_all(self.attrs.outer());
David Tolnay85b69a42017-12-27 20:43:10 -05002936 self.base.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002937 self.dot_token.to_tokens(tokens);
David Tolnay85b69a42017-12-27 20:43:10 -05002938 self.member.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002939 }
2940 }
2941
Michael Layzell734adb42017-06-07 16:58:31 -04002942 #[cfg(feature = "full")]
David Tolnay85b69a42017-12-27 20:43:10 -05002943 impl ToTokens for Member {
Alex Crichton62a0a592017-05-22 13:58:53 -07002944 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05002945 match *self {
2946 Member::Named(ident) => ident.to_tokens(tokens),
2947 Member::Unnamed(ref index) => index.to_tokens(tokens),
2948 }
2949 }
2950 }
2951
David Tolnay85b69a42017-12-27 20:43:10 -05002952 impl ToTokens for Index {
2953 fn to_tokens(&self, tokens: &mut Tokens) {
2954 tokens.append(TokenTree {
2955 span: self.span,
David Tolnay9bce0572017-12-27 22:24:09 -05002956 kind: TokenNode::Literal(Literal::integer(i64::from(self.index))),
David Tolnay85b69a42017-12-27 20:43:10 -05002957 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002958 }
2959 }
2960
2961 impl ToTokens for ExprIndex {
2962 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002963 attrs_to_tokens(&self.attrs, tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002964 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002965 self.bracket_token.surround(tokens, |tokens| {
2966 self.index.to_tokens(tokens);
2967 });
Alex Crichton62a0a592017-05-22 13:58:53 -07002968 }
2969 }
2970
Michael Layzell734adb42017-06-07 16:58:31 -04002971 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002972 impl ToTokens for ExprRange {
2973 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002974 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07002975 self.from.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08002976 match self.limits {
2977 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
2978 RangeLimits::Closed(ref t) => t.to_tokens(tokens),
2979 }
Alex Crichton62a0a592017-05-22 13:58:53 -07002980 self.to.to_tokens(tokens);
2981 }
2982 }
2983
2984 impl ToTokens for ExprPath {
2985 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002986 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002987 ::PathTokens(&self.qself, &self.path).to_tokens(tokens)
Alex Crichton62a0a592017-05-22 13:58:53 -07002988 }
2989 }
2990
Michael Layzell734adb42017-06-07 16:58:31 -04002991 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07002992 impl ToTokens for ExprAddrOf {
2993 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05002994 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002995 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05002996 self.mutability.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07002997 self.expr.to_tokens(tokens);
2998 }
2999 }
3000
Michael Layzell734adb42017-06-07 16:58:31 -04003001 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003002 impl ToTokens for ExprBreak {
3003 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003004 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003005 self.break_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003006 self.label.to_tokens(tokens);
3007 self.expr.to_tokens(tokens);
3008 }
3009 }
3010
Michael Layzell734adb42017-06-07 16:58:31 -04003011 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003012 impl ToTokens for ExprContinue {
3013 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003014 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003015 self.continue_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003016 self.label.to_tokens(tokens);
3017 }
3018 }
3019
Michael Layzell734adb42017-06-07 16:58:31 -04003020 #[cfg(feature = "full")]
David Tolnayc246cd32017-12-28 23:14:32 -05003021 impl ToTokens for ExprReturn {
Alex Crichton62a0a592017-05-22 13:58:53 -07003022 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003023 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003024 self.return_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -07003025 self.expr.to_tokens(tokens);
3026 }
3027 }
3028
Michael Layzell734adb42017-06-07 16:58:31 -04003029 #[cfg(feature = "full")]
David Tolnay8c91b882017-12-28 23:04:32 -05003030 impl ToTokens for ExprMacro {
3031 fn to_tokens(&self, tokens: &mut Tokens) {
3032 tokens.append_all(self.attrs.outer());
3033 self.mac.to_tokens(tokens);
3034 }
3035 }
3036
3037 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003038 impl ToTokens for ExprStruct {
3039 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003040 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003041 self.path.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003042 self.brace_token.surround(tokens, |tokens| {
3043 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003044 if self.rest.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07003045 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003046 self.rest.to_tokens(tokens);
3047 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003048 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003049 }
3050 }
3051
Michael Layzell734adb42017-06-07 16:58:31 -04003052 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003053 impl ToTokens for ExprRepeat {
3054 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003055 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003056 self.bracket_token.surround(tokens, |tokens| {
3057 self.expr.to_tokens(tokens);
3058 self.semi_token.to_tokens(tokens);
David Tolnay84d80442018-01-07 01:03:20 -08003059 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003060 })
Alex Crichton62a0a592017-05-22 13:58:53 -07003061 }
3062 }
3063
David Tolnaye98775f2017-12-28 23:17:00 -05003064 #[cfg(feature = "full")]
Michael Layzell93c36282017-06-04 20:43:14 -04003065 impl ToTokens for ExprGroup {
3066 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003067 attrs_to_tokens(&self.attrs, tokens);
Michael Layzell93c36282017-06-04 20:43:14 -04003068 self.group_token.surround(tokens, |tokens| {
3069 self.expr.to_tokens(tokens);
3070 });
3071 }
3072 }
3073
David Tolnaye98775f2017-12-28 23:17:00 -05003074 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003075 impl ToTokens for ExprParen {
3076 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003077 attrs_to_tokens(&self.attrs, tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003078 self.paren_token.surround(tokens, |tokens| {
3079 self.expr.to_tokens(tokens);
3080 });
Alex Crichton62a0a592017-05-22 13:58:53 -07003081 }
3082 }
3083
Michael Layzell734adb42017-06-07 16:58:31 -04003084 #[cfg(feature = "full")]
Alex Crichton62a0a592017-05-22 13:58:53 -07003085 impl ToTokens for ExprTry {
3086 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay8c91b882017-12-28 23:04:32 -05003087 tokens.append_all(self.attrs.outer());
Alex Crichton62a0a592017-05-22 13:58:53 -07003088 self.expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003089 self.question_token.to_tokens(tokens);
David Tolnayf4bbbd92016-09-23 14:41:55 -07003090 }
3091 }
David Tolnayb4ad3b52016-10-01 21:58:13 -07003092
David Tolnay2ae520a2017-12-29 11:19:50 -05003093 impl ToTokens for ExprVerbatim {
3094 fn to_tokens(&self, tokens: &mut Tokens) {
3095 self.tts.to_tokens(tokens);
3096 }
3097 }
3098
Michael Layzell734adb42017-06-07 16:58:31 -04003099 #[cfg(feature = "full")]
David Tolnaybcd498f2017-12-29 12:02:33 -05003100 impl ToTokens for Label {
3101 fn to_tokens(&self, tokens: &mut Tokens) {
3102 self.name.to_tokens(tokens);
3103 self.colon_token.to_tokens(tokens);
3104 }
3105 }
3106
3107 #[cfg(feature = "full")]
David Tolnay055a7042016-10-02 19:23:54 -07003108 impl ToTokens for FieldValue {
3109 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay85b69a42017-12-27 20:43:10 -05003110 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003111 if let Some(ref colon_token) = self.colon_token {
3112 colon_token.to_tokens(tokens);
David Tolnay276690f2016-10-30 12:06:59 -07003113 self.expr.to_tokens(tokens);
3114 }
David Tolnay055a7042016-10-02 19:23:54 -07003115 }
3116 }
3117
Michael Layzell734adb42017-06-07 16:58:31 -04003118 #[cfg(feature = "full")]
David Tolnayb4ad3b52016-10-01 21:58:13 -07003119 impl ToTokens for Arm {
3120 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003121 tokens.append_all(&self.attrs);
3122 self.pats.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003123 if let Some((ref if_token, ref guard)) = self.guard {
3124 if_token.to_tokens(tokens);
3125 guard.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003126 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003127 self.rocket_token.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003128 self.body.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003129 self.comma.to_tokens(tokens);
David Tolnayb4ad3b52016-10-01 21:58:13 -07003130 }
3131 }
3132
Michael Layzell734adb42017-06-07 16:58:31 -04003133 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003134 impl ToTokens for PatWild {
David Tolnayb4ad3b52016-10-01 21:58:13 -07003135 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003136 self.underscore_token.to_tokens(tokens);
3137 }
3138 }
3139
Michael Layzell734adb42017-06-07 16:58:31 -04003140 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003141 impl ToTokens for PatIdent {
3142 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay24237fb2017-12-29 02:15:26 -05003143 self.by_ref.to_tokens(tokens);
David Tolnayefc96fb2017-12-29 02:03:15 -05003144 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003145 self.ident.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003146 if let Some((ref at_token, ref subpat)) = self.subpat {
3147 at_token.to_tokens(tokens);
3148 subpat.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003149 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003150 }
3151 }
3152
Michael Layzell734adb42017-06-07 16:58:31 -04003153 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003154 impl ToTokens for PatStruct {
3155 fn to_tokens(&self, tokens: &mut Tokens) {
3156 self.path.to_tokens(tokens);
3157 self.brace_token.surround(tokens, |tokens| {
3158 self.fields.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003159 // NOTE: We need a comma before the dot2 token if it is present.
3160 if !self.fields.empty_or_trailing() && self.dot2_token.is_some() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003161 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003162 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003163 self.dot2_token.to_tokens(tokens);
3164 });
3165 }
3166 }
3167
Michael Layzell734adb42017-06-07 16:58:31 -04003168 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003169 impl ToTokens for PatTupleStruct {
3170 fn to_tokens(&self, tokens: &mut Tokens) {
3171 self.path.to_tokens(tokens);
3172 self.pat.to_tokens(tokens);
3173 }
3174 }
3175
Michael Layzell734adb42017-06-07 16:58:31 -04003176 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003177 impl ToTokens for PatPath {
3178 fn to_tokens(&self, tokens: &mut Tokens) {
3179 ::PathTokens(&self.qself, &self.path).to_tokens(tokens);
3180 }
3181 }
3182
Michael Layzell734adb42017-06-07 16:58:31 -04003183 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003184 impl ToTokens for PatTuple {
3185 fn to_tokens(&self, tokens: &mut Tokens) {
3186 self.paren_token.surround(tokens, |tokens| {
David Tolnay41871922017-12-29 01:53:45 -05003187 self.front.to_tokens(tokens);
3188 if let Some(ref dot2_token) = self.dot2_token {
3189 if !self.front.empty_or_trailing() {
3190 // Ensure there is a comma before the .. token.
David Tolnayf8db7ba2017-11-11 22:52:16 -08003191 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003192 }
David Tolnay41871922017-12-29 01:53:45 -05003193 dot2_token.to_tokens(tokens);
3194 self.comma_token.to_tokens(tokens);
3195 if self.comma_token.is_none() && !self.back.is_empty() {
3196 // Ensure there is a comma after the .. token.
3197 <Token![,]>::default().to_tokens(tokens);
3198 }
David Tolnay8d9e81a2016-10-03 22:36:32 -07003199 }
David Tolnay41871922017-12-29 01:53:45 -05003200 self.back.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003201 });
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 PatBox {
3207 fn to_tokens(&self, tokens: &mut Tokens) {
3208 self.box_token.to_tokens(tokens);
3209 self.pat.to_tokens(tokens);
3210 }
3211 }
3212
Michael Layzell734adb42017-06-07 16:58:31 -04003213 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003214 impl ToTokens for PatRef {
3215 fn to_tokens(&self, tokens: &mut Tokens) {
3216 self.and_token.to_tokens(tokens);
David Tolnay24237fb2017-12-29 02:15:26 -05003217 self.mutability.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003218 self.pat.to_tokens(tokens);
3219 }
3220 }
3221
Michael Layzell734adb42017-06-07 16:58:31 -04003222 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003223 impl ToTokens for PatLit {
3224 fn to_tokens(&self, tokens: &mut Tokens) {
3225 self.expr.to_tokens(tokens);
3226 }
3227 }
3228
Michael Layzell734adb42017-06-07 16:58:31 -04003229 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003230 impl ToTokens for PatRange {
3231 fn to_tokens(&self, tokens: &mut Tokens) {
3232 self.lo.to_tokens(tokens);
David Tolnay475288a2017-12-19 22:59:44 -08003233 match self.limits {
3234 RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens),
3235 RangeLimits::Closed(ref t) => Token![...](t.0).to_tokens(tokens),
3236 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003237 self.hi.to_tokens(tokens);
3238 }
3239 }
3240
Michael Layzell734adb42017-06-07 16:58:31 -04003241 #[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003242 impl ToTokens for PatSlice {
3243 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -04003244 // XXX: This is a mess, and it will be so easy to screw it up. How
3245 // do we make this correct itself better?
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003246 self.bracket_token.surround(tokens, |tokens| {
3247 self.front.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003248
3249 // If we need a comma before the middle or standalone .. token,
3250 // then make sure it's present.
David Tolnay51382052017-12-27 13:46:21 -05003251 if !self.front.empty_or_trailing()
3252 && (self.middle.is_some() || self.dot2_token.is_some())
Michael Layzell3936ceb2017-07-08 00:28:36 -04003253 {
David Tolnayf8db7ba2017-11-11 22:52:16 -08003254 <Token![,]>::default().to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003255 }
3256
3257 // If we have an identifier, we always need a .. token.
3258 if self.middle.is_some() {
3259 self.middle.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -07003260 TokensOrDefault(&self.dot2_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003261 } else if self.dot2_token.is_some() {
3262 self.dot2_token.to_tokens(tokens);
3263 }
3264
3265 // Make sure we have a comma before the back half.
3266 if !self.back.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07003267 TokensOrDefault(&self.comma_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003268 self.back.to_tokens(tokens);
3269 } else {
3270 self.comma_token.to_tokens(tokens);
3271 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003272 })
David Tolnayb4ad3b52016-10-01 21:58:13 -07003273 }
3274 }
3275
Michael Layzell734adb42017-06-07 16:58:31 -04003276 #[cfg(feature = "full")]
David Tolnay323279a2017-12-29 11:26:32 -05003277 impl ToTokens for PatMacro {
3278 fn to_tokens(&self, tokens: &mut Tokens) {
3279 self.mac.to_tokens(tokens);
3280 }
3281 }
3282
3283 #[cfg(feature = "full")]
David Tolnay2ae520a2017-12-29 11:19:50 -05003284 impl ToTokens for PatVerbatim {
3285 fn to_tokens(&self, tokens: &mut Tokens) {
3286 self.tts.to_tokens(tokens);
3287 }
3288 }
3289
3290 #[cfg(feature = "full")]
David Tolnay8d9e81a2016-10-03 22:36:32 -07003291 impl ToTokens for FieldPat {
3292 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay5d7098a2017-12-29 01:35:24 -05003293 if let Some(ref colon_token) = self.colon_token {
David Tolnay85b69a42017-12-27 20:43:10 -05003294 self.member.to_tokens(tokens);
David Tolnay5d7098a2017-12-29 01:35:24 -05003295 colon_token.to_tokens(tokens);
David Tolnay8d9e81a2016-10-03 22:36:32 -07003296 }
3297 self.pat.to_tokens(tokens);
3298 }
3299 }
3300
Michael Layzell734adb42017-06-07 16:58:31 -04003301 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003302 impl ToTokens for Block {
3303 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003304 self.brace_token.surround(tokens, |tokens| {
3305 tokens.append_all(&self.stmts);
3306 });
David Tolnay42602292016-10-01 22:25:45 -07003307 }
3308 }
3309
Michael Layzell734adb42017-06-07 16:58:31 -04003310 #[cfg(feature = "full")]
David Tolnay42602292016-10-01 22:25:45 -07003311 impl ToTokens for Stmt {
3312 fn to_tokens(&self, tokens: &mut Tokens) {
3313 match *self {
David Tolnay191e0582016-10-02 18:31:09 -07003314 Stmt::Local(ref local) => local.to_tokens(tokens),
David Tolnay42602292016-10-01 22:25:45 -07003315 Stmt::Item(ref item) => item.to_tokens(tokens),
3316 Stmt::Expr(ref expr) => expr.to_tokens(tokens),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003317 Stmt::Semi(ref expr, ref semi) => {
David Tolnay42602292016-10-01 22:25:45 -07003318 expr.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003319 semi.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -07003320 }
David Tolnay42602292016-10-01 22:25:45 -07003321 }
3322 }
3323 }
David Tolnay191e0582016-10-02 18:31:09 -07003324
Michael Layzell734adb42017-06-07 16:58:31 -04003325 #[cfg(feature = "full")]
David Tolnay191e0582016-10-02 18:31:09 -07003326 impl ToTokens for Local {
3327 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay4e3158d2016-10-30 00:30:01 -07003328 tokens.append_all(self.attrs.outer());
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003329 self.let_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003330 self.pat.to_tokens(tokens);
David Tolnay8b4d3022017-12-29 12:11:10 -05003331 if let Some((ref colon_token, ref ty)) = self.ty {
3332 colon_token.to_tokens(tokens);
3333 ty.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003334 }
David Tolnay8b4d3022017-12-29 12:11:10 -05003335 if let Some((ref eq_token, ref init)) = self.init {
3336 eq_token.to_tokens(tokens);
3337 init.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04003338 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07003339 self.semi_token.to_tokens(tokens);
David Tolnay191e0582016-10-02 18:31:09 -07003340 }
3341 }
David Tolnayf4bbbd92016-09-23 14:41:55 -07003342}