David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | use super::*; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2 | use delimited::Delimited; |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 3 | #[cfg(feature = "full")] |
| 4 | use proc_macro2::Span; |
| 5 | #[cfg(feature = "full")] |
| 6 | use std::hash::{Hash, Hasher}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 7 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 8 | ast_struct! { |
| 9 | /// An expression. |
| 10 | pub struct Expr { |
| 11 | /// Type of the expression. |
| 12 | pub node: ExprKind, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 13 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 14 | /// Attributes tagged on the expression. |
| 15 | pub attrs: Vec<Attribute>, |
| 16 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | impl From<ExprKind> for Expr { |
| 20 | fn from(node: ExprKind) -> Expr { |
| 21 | Expr { |
| 22 | node: node, |
| 23 | attrs: Vec::new(), |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 28 | ast_enum_of_structs! { |
| 29 | pub enum ExprKind { |
| 30 | /// A `box x` expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 31 | pub Box(ExprBox #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 33 | pub box_token: Token![box], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 34 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 35 | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 36 | /// E.g. 'place <- val' or `in place { val }`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 37 | pub InPlace(ExprInPlace #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 38 | pub place: Box<Expr>, |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 39 | pub kind: InPlaceKind, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 40 | pub value: Box<Expr>, |
| 41 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 42 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 43 | /// An array, e.g. `[a, b, c, d]`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 44 | pub Array(ExprArray #full { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 45 | pub exprs: Delimited<Expr, Token![,]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 46 | pub bracket_token: token::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 47 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 48 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 49 | /// A function call. |
| 50 | pub Call(ExprCall { |
| 51 | pub func: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 52 | pub args: Delimited<Expr, Token![,]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 53 | pub paren_token: token::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 54 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 55 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 56 | /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`) |
| 57 | /// |
| 58 | /// The `Ident` is the identifier for the method name. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 59 | /// The vector of `Type`s are the ascripted type parameters for the method |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 60 | /// (within the angle brackets). |
| 61 | /// |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 62 | /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as |
| 63 | /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 64 | pub MethodCall(ExprMethodCall #full { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 65 | pub expr: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 66 | pub method: Ident, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 67 | pub typarams: Delimited<Type, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 68 | pub args: Delimited<Expr, Token![,]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 69 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 70 | pub dot_token: Token![.], |
| 71 | pub lt_token: Option<Token![<]>, |
| 72 | pub colon2_token: Option<Token![::]>, |
| 73 | pub gt_token: Option<Token![>]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 74 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 75 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 76 | /// A tuple, e.g. `(a, b, c, d)`. |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 77 | pub Tuple(ExprTuple #full { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 78 | pub args: Delimited<Expr, Token![,]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 79 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 80 | pub lone_comma: Option<Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 81 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 82 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 83 | /// A binary operation, e.g. `a + b`, `a * b`. |
| 84 | pub Binary(ExprBinary { |
| 85 | pub op: BinOp, |
| 86 | pub left: Box<Expr>, |
| 87 | pub right: Box<Expr>, |
| 88 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 89 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 90 | /// A unary operation, e.g. `!x`, `*x`. |
| 91 | pub Unary(ExprUnary { |
| 92 | pub op: UnOp, |
| 93 | pub expr: Box<Expr>, |
| 94 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 95 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 96 | /// A literal, e.g. `1`, `"foo"`. |
| 97 | pub Lit(Lit), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 98 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 99 | /// A cast, e.g. `foo as f64`. |
| 100 | pub Cast(ExprCast { |
| 101 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 102 | pub as_token: Token![as], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 103 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 104 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 105 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 106 | /// A type ascription, e.g. `foo: f64`. |
| 107 | pub Type(ExprType { |
| 108 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 109 | pub colon_token: Token![:], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 110 | pub ty: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 111 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 112 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 113 | /// An `if` block, with an optional else block |
| 114 | /// |
| 115 | /// E.g., `if expr { block } else { expr }` |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 116 | pub If(ExprIf #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 117 | pub cond: Box<Expr>, |
| 118 | pub if_true: Block, |
| 119 | pub if_false: Option<Box<Expr>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 120 | pub if_token: Token![if], |
| 121 | pub else_token: Option<Token![else]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 122 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 123 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 124 | /// An `if let` expression with an optional else block |
| 125 | /// |
| 126 | /// E.g., `if let pat = expr { block } else { expr }` |
| 127 | /// |
| 128 | /// This is desugared to a `match` expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 129 | pub IfLet(ExprIfLet #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 130 | pub pat: Box<Pat>, |
| 131 | pub expr: Box<Expr>, |
| 132 | pub if_true: Block, |
| 133 | pub if_false: Option<Box<Expr>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 134 | pub if_token: Token![if], |
| 135 | pub let_token: Token![let], |
| 136 | pub eq_token: Token![=], |
| 137 | pub else_token: Option<Token![else]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 138 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 139 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 140 | /// A while loop, with an optional label |
| 141 | /// |
| 142 | /// E.g., `'label: while expr { block }` |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 143 | pub While(ExprWhile #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 144 | pub cond: Box<Expr>, |
| 145 | pub body: Block, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 146 | pub label: Option<Lifetime>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 147 | pub colon_token: Option<Token![:]>, |
| 148 | pub while_token: Token![while], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 149 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 150 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 151 | /// A while-let loop, with an optional label. |
| 152 | /// |
| 153 | /// E.g., `'label: while let pat = expr { block }` |
| 154 | /// |
| 155 | /// This is desugared to a combination of `loop` and `match` expressions. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 156 | pub WhileLet(ExprWhileLet #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 157 | pub pat: Box<Pat>, |
| 158 | pub expr: Box<Expr>, |
| 159 | pub body: Block, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 160 | pub label: Option<Lifetime>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 161 | pub colon_token: Option<Token![:]>, |
| 162 | pub while_token: Token![while], |
| 163 | pub let_token: Token![let], |
| 164 | pub eq_token: Token![=], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 165 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 166 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 167 | /// A for loop, with an optional label. |
| 168 | /// |
| 169 | /// E.g., `'label: for pat in expr { block }` |
| 170 | /// |
| 171 | /// This is desugared to a combination of `loop` and `match` expressions. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 172 | pub ForLoop(ExprForLoop #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 173 | pub pat: Box<Pat>, |
| 174 | pub expr: Box<Expr>, |
| 175 | pub body: Block, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 176 | pub label: Option<Lifetime>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 177 | pub for_token: Token![for], |
| 178 | pub colon_token: Option<Token![:]>, |
| 179 | pub in_token: Token![in], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 180 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 181 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 182 | /// Conditionless loop with an optional label. |
| 183 | /// |
| 184 | /// E.g. `'label: loop { block }` |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 185 | pub Loop(ExprLoop #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 186 | pub body: Block, |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 187 | pub label: Option<Lifetime>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 188 | pub loop_token: Token![loop], |
| 189 | pub colon_token: Option<Token![:]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 190 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 191 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 192 | /// A `match` block. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 193 | pub Match(ExprMatch #full { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 194 | pub match_token: Token![match], |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 195 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 196 | pub expr: Box<Expr>, |
| 197 | pub arms: Vec<Arm>, |
| 198 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 199 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 200 | /// A closure (for example, `move |a, b, c| a + b + c`) |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 201 | pub Closure(ExprClosure #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 202 | pub capture: CaptureBy, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 203 | pub or1_token: Token![|], |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 204 | pub inputs: Delimited<FnArg, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 205 | pub or2_token: Token![|], |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 206 | pub output: ReturnType, |
| 207 | pub body: Box<Expr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 208 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 209 | |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 210 | /// An unsafe block (`unsafe { ... }`) |
| 211 | pub Unsafe(ExprUnsafe #full { |
| 212 | pub unsafe_token: Token![unsafe], |
| 213 | pub block: Block, |
| 214 | }), |
| 215 | |
| 216 | /// A block (`{ ... }`) |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 217 | pub Block(ExprBlock #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 218 | pub block: Block, |
| 219 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 220 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 221 | /// An assignment (`a = foo()`) |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 222 | pub Assign(ExprAssign #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 223 | pub left: Box<Expr>, |
| 224 | pub right: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 225 | pub eq_token: Token![=], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 226 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 227 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 228 | /// An assignment with an operator |
| 229 | /// |
| 230 | /// For example, `a += 1`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 231 | pub AssignOp(ExprAssignOp #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 232 | pub op: BinOp, |
| 233 | pub left: Box<Expr>, |
| 234 | pub right: Box<Expr>, |
| 235 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 236 | |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 237 | /// Access of a named struct field (`obj.foo`) or unnamed tuple struct |
| 238 | /// field (`obj.0`). |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 239 | pub Field(ExprField #full { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 240 | pub base: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 241 | pub dot_token: Token![.], |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 242 | pub member: Member, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 243 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 244 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 245 | /// An indexing operation (`foo[2]`) |
| 246 | pub Index(ExprIndex { |
| 247 | pub expr: Box<Expr>, |
| 248 | pub index: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 249 | pub bracket_token: token::Bracket, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 250 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 251 | |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 252 | /// A range (`1..2`, `1..`, `..2`, `1..=2`, `..=2`) |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 253 | pub Range(ExprRange #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 254 | pub from: Option<Box<Expr>>, |
| 255 | pub to: Option<Box<Expr>>, |
| 256 | pub limits: RangeLimits, |
| 257 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 258 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 259 | /// Variable reference, possibly containing `::` and/or type |
| 260 | /// parameters, e.g. foo::bar::<baz>. |
| 261 | /// |
| 262 | /// Optionally "qualified", |
| 263 | /// E.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 264 | pub Path(ExprPath { |
| 265 | pub qself: Option<QSelf>, |
| 266 | pub path: Path, |
| 267 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 268 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 269 | /// A referencing operation (`&a` or `&mut a`) |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 270 | pub AddrOf(ExprAddrOf #full { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 271 | pub and_token: Token![&], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 272 | pub mutbl: Mutability, |
| 273 | pub expr: Box<Expr>, |
| 274 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 275 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 276 | /// A `break`, with an optional label to break, and an optional expression |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 277 | pub Break(ExprBreak #full { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 278 | pub label: Option<Lifetime>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 279 | pub expr: Option<Box<Expr>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 280 | pub break_token: Token![break], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 281 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 282 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 283 | /// A `continue`, with an optional label |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 284 | pub Continue(ExprContinue #full { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 285 | pub label: Option<Lifetime>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 286 | pub continue_token: Token![continue], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 287 | }), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 288 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 289 | /// A `return`, with an optional value to be returned |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 290 | pub Ret(ExprRet #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 291 | pub expr: Option<Box<Expr>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 292 | pub return_token: Token![return], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 293 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 294 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 295 | /// A macro invocation; pre-expansion |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 296 | pub Macro(Macro), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 297 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 298 | /// A struct literal expression. |
| 299 | /// |
| 300 | /// For example, `Foo {x: 1, y: 2}`, or |
| 301 | /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 302 | pub Struct(ExprStruct #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 303 | pub path: Path, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 304 | pub fields: Delimited<FieldValue, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 305 | pub rest: Option<Box<Expr>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 306 | pub dot2_token: Option<Token![..]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 307 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 308 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 309 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 310 | /// An array literal constructed from one repeated element. |
| 311 | /// |
| 312 | /// For example, `[1; 5]`. The first expression is the element |
| 313 | /// to be repeated; the second is the number of times to repeat it. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 314 | pub Repeat(ExprRepeat #full { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 315 | pub bracket_token: token::Bracket, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 316 | pub semi_token: Token![;], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 317 | pub expr: Box<Expr>, |
| 318 | pub amt: Box<Expr>, |
| 319 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 320 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 321 | /// No-op: used solely so we can pretty-print faithfully |
| 322 | pub Paren(ExprParen { |
| 323 | pub expr: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 324 | pub paren_token: token::Paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 325 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 326 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 327 | /// No-op: used solely so we can pretty-print faithfully |
| 328 | /// |
| 329 | /// A `group` represents a `None`-delimited span in the input |
| 330 | /// `TokenStream` which affects the precidence of the resulting |
| 331 | /// expression. They are used for macro hygiene. |
| 332 | pub Group(ExprGroup { |
| 333 | pub expr: Box<Expr>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 334 | pub group_token: token::Group, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 335 | }), |
| 336 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 337 | /// `expr?` |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 338 | pub Try(ExprTry #full { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 339 | pub expr: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 340 | pub question_token: Token![?], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 341 | }), |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 342 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 343 | /// A catch expression. |
| 344 | /// |
| 345 | /// E.g. `do catch { block }` |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 346 | pub Catch(ExprCatch #full { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 347 | pub do_token: Token![do], |
| 348 | pub catch_token: Token![catch], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 349 | pub block: Block, |
| 350 | }), |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 351 | |
| 352 | /// A yield expression. |
| 353 | /// |
| 354 | /// E.g. `yield expr` |
| 355 | pub Yield(ExprYield #full { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 356 | pub yield_token: Token![yield], |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 357 | pub expr: Option<Box<Expr>>, |
| 358 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 359 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 362 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 363 | ast_enum! { |
| 364 | /// A struct or tuple struct field accessed in a struct literal or field |
| 365 | /// expression. |
| 366 | pub enum Member { |
| 367 | /// A named field like `self.x`. |
| 368 | Named(Ident), |
| 369 | /// An unnamed field like `self.0`. |
| 370 | Unnamed(Index), |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | #[cfg(feature = "full")] |
| 375 | ast_struct! { |
| 376 | /// The index of an unnamed tuple struct field. |
| 377 | pub struct Index #manual_extra_traits { |
| 378 | pub index: u32, |
| 379 | pub span: Span, |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | #[cfg(feature = "full")] |
| 384 | impl Eq for Index {} |
| 385 | |
| 386 | #[cfg(feature = "full")] |
| 387 | impl PartialEq for Index { |
| 388 | fn eq(&self, other: &Self) -> bool { |
| 389 | self.index == other.index |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | #[cfg(feature = "full")] |
| 394 | impl Hash for Index { |
| 395 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 396 | self.index.hash(state); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 401 | ast_struct! { |
| 402 | /// A field-value pair in a struct literal. |
| 403 | pub struct FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 404 | /// Attributes tagged on the field. |
| 405 | pub attrs: Vec<Attribute>, |
| 406 | |
| 407 | /// Name or index of the field. |
| 408 | pub member: Member, |
| 409 | |
| 410 | pub colon_token: Option<Token![:]>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 411 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 412 | /// Value of the field. |
| 413 | pub expr: Expr, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 414 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 415 | /// Whether this is a shorthand field, e.g. `Struct { x }` |
| 416 | /// instead of `Struct { x: x }`. |
| 417 | pub is_shorthand: bool, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 418 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 421 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 422 | ast_struct! { |
| 423 | /// A Block (`{ .. }`). |
| 424 | /// |
| 425 | /// E.g. `{ .. }` as in `fn foo() { .. }` |
| 426 | pub struct Block { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 427 | pub brace_token: token::Brace, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 428 | /// Statements in a block |
| 429 | pub stmts: Vec<Stmt>, |
| 430 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 433 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 434 | ast_enum! { |
| 435 | /// A statement, usually ending in a semicolon. |
| 436 | pub enum Stmt { |
| 437 | /// A local (let) binding. |
| 438 | Local(Box<Local>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 439 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 440 | /// An item definition. |
| 441 | Item(Box<Item>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 442 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 443 | /// Expr without trailing semicolon. |
| 444 | Expr(Box<Expr>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 445 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 446 | /// Expression with trailing semicolon; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 447 | Semi(Box<Expr>, Token![;]), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 448 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 449 | /// Macro invocation. |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 450 | Macro(Box<(Macro, MacStmtStyle, Vec<Attribute>)>), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 451 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 454 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 455 | ast_enum! { |
| 456 | /// How a macro was invoked. |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 457 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 458 | pub enum MacStmtStyle { |
| 459 | /// The macro statement had a trailing semicolon, e.g. `foo! { ... };` |
| 460 | /// `foo!(...);`, `foo![...];` |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 461 | Semicolon(Token![;]), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 462 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 463 | /// The macro statement had braces; e.g. foo! { ... } |
| 464 | Braces, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 465 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 466 | /// The macro statement had parentheses or brackets and no semicolon; e.g. |
| 467 | /// `foo!(...)`. All of these will end up being converted into macro |
| 468 | /// expressions. |
| 469 | NoBraces, |
| 470 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 473 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 474 | ast_struct! { |
| 475 | /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;` |
| 476 | pub struct Local { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 477 | pub let_token: Token![let], |
| 478 | pub colon_token: Option<Token![:]>, |
| 479 | pub eq_token: Option<Token![=]>, |
| 480 | pub semi_token: Token![;], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 481 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 482 | pub pat: Box<Pat>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 483 | pub ty: Option<Box<Type>>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame] | 484 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 485 | /// Initializer expression to set the value, if any |
| 486 | pub init: Option<Box<Expr>>, |
| 487 | pub attrs: Vec<Attribute>, |
| 488 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 491 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 492 | ast_enum_of_structs! { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 493 | // Clippy false positive |
| 494 | // https://github.com/Manishearth/rust-clippy/issues/1241 |
| 495 | #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))] |
| 496 | pub enum Pat { |
| 497 | /// Represents a wildcard pattern (`_`) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 498 | pub Wild(PatWild { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 499 | pub underscore_token: Token![_], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 500 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 501 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 502 | /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`), |
| 503 | /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third |
| 504 | /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens |
| 505 | /// during name resolution. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 506 | pub Ident(PatIdent { |
| 507 | pub mode: BindingMode, |
| 508 | pub ident: Ident, |
| 509 | pub subpat: Option<Box<Pat>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 510 | pub at_token: Option<Token![@]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 511 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 512 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 513 | /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`. |
| 514 | /// The `bool` is `true` in the presence of a `..`. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 515 | pub Struct(PatStruct { |
| 516 | pub path: Path, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 517 | pub fields: Delimited<FieldPat, Token![,]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 518 | pub brace_token: token::Brace, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 519 | pub dot2_token: Option<Token![..]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 520 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 521 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 522 | /// A tuple struct/variant pattern `Variant(x, y, .., z)`. |
| 523 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 524 | /// 0 <= position <= subpats.len() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 525 | pub TupleStruct(PatTupleStruct { |
| 526 | pub path: Path, |
| 527 | pub pat: PatTuple, |
| 528 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 529 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 530 | /// A possibly qualified path pattern. |
| 531 | /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants |
| 532 | /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can |
| 533 | /// only legally refer to associated constants. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 534 | pub Path(PatPath { |
| 535 | pub qself: Option<QSelf>, |
| 536 | pub path: Path, |
| 537 | }), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 538 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 539 | /// A tuple pattern `(a, b)`. |
| 540 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 541 | /// 0 <= position <= subpats.len() |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 542 | pub Tuple(PatTuple { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 543 | pub pats: Delimited<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 544 | pub dots_pos: Option<usize>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 545 | pub paren_token: token::Paren, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 546 | pub dot2_token: Option<Token![..]>, |
| 547 | pub comma_token: Option<Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 548 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 549 | /// A `box` pattern |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 550 | pub Box(PatBox { |
| 551 | pub pat: Box<Pat>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 552 | pub box_token: Token![box], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 553 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 554 | /// A reference pattern, e.g. `&mut (a, b)` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 555 | pub Ref(PatRef { |
| 556 | pub pat: Box<Pat>, |
| 557 | pub mutbl: Mutability, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 558 | pub and_token: Token![&], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 559 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 560 | /// A literal |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 561 | pub Lit(PatLit { |
| 562 | pub expr: Box<Expr>, |
| 563 | }), |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 564 | /// A range pattern, e.g. `1..=2` |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 565 | pub Range(PatRange { |
| 566 | pub lo: Box<Expr>, |
| 567 | pub hi: Box<Expr>, |
| 568 | pub limits: RangeLimits, |
| 569 | }), |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 570 | /// `[a, b, i.., y, z]` is represented as: |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 571 | pub Slice(PatSlice { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 572 | pub front: Delimited<Pat, Token![,]>, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 573 | pub middle: Option<Box<Pat>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 574 | pub back: Delimited<Pat, Token![,]>, |
| 575 | pub dot2_token: Option<Token![..]>, |
| 576 | pub comma_token: Option<Token![,]>, |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 577 | pub bracket_token: token::Bracket, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 578 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 579 | /// A macro pattern; pre-expansion |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 580 | pub Macro(Macro), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 581 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 584 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 585 | ast_struct! { |
| 586 | /// An arm of a 'match'. |
| 587 | /// |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 588 | /// E.g. `0..=10 => { println!("match!") }` as in |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 589 | /// |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 590 | /// ```rust |
| 591 | /// # #![feature(dotdoteq_in_patterns)] |
| 592 | /// # |
| 593 | /// # fn main() { |
| 594 | /// # let n = 0; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 595 | /// match n { |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 596 | /// 0..=10 => { println!("match!") } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 597 | /// // .. |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 598 | /// # _ => {} |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 599 | /// } |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 600 | /// # } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 601 | /// ``` |
| 602 | pub struct Arm { |
| 603 | pub attrs: Vec<Attribute>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 604 | pub pats: Delimited<Pat, Token![|]>, |
| 605 | pub if_token: Option<Token![if]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 606 | pub guard: Option<Box<Expr>>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 607 | pub rocket_token: Token![=>], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 608 | pub body: Box<Expr>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 609 | pub comma: Option<Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 610 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 613 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 614 | ast_enum! { |
| 615 | /// A capture clause |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 616 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 617 | pub enum CaptureBy { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 618 | Value(Token![move]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 619 | Ref, |
| 620 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 623 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 624 | ast_enum! { |
| 625 | /// Limit types of a range (inclusive or exclusive) |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 626 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 627 | pub enum RangeLimits { |
| 628 | /// Inclusive at the beginning, exclusive at the end |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 629 | HalfOpen(Token![..]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 630 | /// Inclusive at the beginning and end |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 631 | Closed(Token![..=]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 632 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 635 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 636 | ast_struct! { |
| 637 | /// A single field in a struct pattern |
| 638 | /// |
| 639 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` |
| 640 | /// are treated the same as `x: x, y: ref y, z: ref mut z`, |
| 641 | /// except `is_shorthand` is true |
| 642 | pub struct FieldPat { |
| 643 | /// The identifier for the field |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 644 | pub member: Member, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 645 | /// The pattern the field is destructured to |
| 646 | pub pat: Box<Pat>, |
| 647 | pub is_shorthand: bool, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 648 | pub colon_token: Option<Token![:]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 649 | pub attrs: Vec<Attribute>, |
| 650 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 653 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 654 | ast_enum! { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 655 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 656 | pub enum BindingMode { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 657 | ByRef(Token![ref], Mutability), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 658 | ByValue(Mutability), |
| 659 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 662 | #[cfg(feature = "full")] |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 663 | ast_enum! { |
| 664 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
| 665 | pub enum InPlaceKind { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 666 | Arrow(Token![<-]), |
| 667 | In(Token![in]), |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 671 | #[cfg(any(feature = "parsing", feature = "printing"))] |
| 672 | #[cfg(feature = "full")] |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 673 | fn arm_expr_requires_comma(expr: &Expr) -> bool { |
| 674 | // see https://github.com/rust-lang/rust/blob/eb8f2586e |
| 675 | // /src/libsyntax/parse/classify.rs#L17-L37 |
| 676 | match expr.node { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 677 | ExprKind::Unsafe(..) |
| 678 | | ExprKind::Block(..) |
| 679 | | ExprKind::If(..) |
| 680 | | ExprKind::IfLet(..) |
| 681 | | ExprKind::Match(..) |
| 682 | | ExprKind::While(..) |
| 683 | | ExprKind::WhileLet(..) |
| 684 | | ExprKind::Loop(..) |
| 685 | | ExprKind::ForLoop(..) |
| 686 | | ExprKind::Catch(..) => false, |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 687 | _ => true, |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 691 | #[cfg(feature = "parsing")] |
| 692 | pub mod parsing { |
| 693 | use super::*; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 694 | use ty::parsing::qpath; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 695 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 696 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 697 | use proc_macro2::{Delimiter, Span, TokenNode, TokenStream}; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 698 | use synom::Synom; |
| 699 | use cursor::Cursor; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 700 | #[cfg(feature = "full")] |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 701 | use parse_error; |
| 702 | use PResult; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 703 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 704 | // When we're parsing expressions which occur before blocks, like in an if |
| 705 | // statement's condition, we cannot parse a struct literal. |
| 706 | // |
| 707 | // Struct literals are ambiguous in certain positions |
| 708 | // https://github.com/rust-lang/rfcs/pull/92 |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 709 | macro_rules! ambiguous_expr { |
| 710 | ($i:expr, $allow_struct:ident) => { |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 711 | ambiguous_expr($i, $allow_struct, true) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 712 | }; |
| 713 | } |
| 714 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 715 | // When we are parsing an optional suffix expression, we cannot allow blocks |
| 716 | // if structs are not allowed. |
| 717 | // |
| 718 | // Example: |
| 719 | // |
| 720 | // if break {} {} |
| 721 | // |
| 722 | // is ambiguous between: |
| 723 | // |
| 724 | // if (break {}) {} |
| 725 | // if (break) {} {} |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 726 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 727 | macro_rules! opt_ambiguous_expr { |
| 728 | ($i:expr, $allow_struct:ident) => { |
| 729 | option!($i, call!(ambiguous_expr, $allow_struct, $allow_struct)) |
| 730 | }; |
| 731 | } |
| 732 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 733 | impl Synom for Expr { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 734 | named!(parse -> Self, ambiguous_expr!(true)); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 735 | |
| 736 | fn description() -> Option<&'static str> { |
| 737 | Some("expression") |
| 738 | } |
| 739 | } |
| 740 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 741 | #[cfg(feature = "full")] |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 742 | named!(expr_no_struct -> Expr, ambiguous_expr!(false)); |
| 743 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 744 | // Parse an arbitrary expression. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 745 | #[cfg(feature = "full")] |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 746 | fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> { |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 747 | map!( |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 748 | i, |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 749 | call!(assign_expr, allow_struct, allow_block), |
| 750 | ExprKind::into |
| 751 | ) |
| 752 | } |
| 753 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 754 | #[cfg(not(feature = "full"))] |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 755 | fn ambiguous_expr(i: Cursor, allow_struct: bool, allow_block: bool) -> PResult<Expr> { |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 756 | map!( |
| 757 | i, |
| 758 | // NOTE: We intentionally skip assign_expr, placement_expr, and |
| 759 | // range_expr, as they are not parsed in non-full mode. |
| 760 | call!(or_expr, allow_struct, allow_block), |
| 761 | ExprKind::into |
| 762 | ) |
| 763 | } |
| 764 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 765 | // Parse a left-associative binary operator. |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 766 | macro_rules! binop { |
| 767 | ( |
| 768 | $name: ident, |
| 769 | $next: ident, |
| 770 | $submac: ident!( $($args:tt)* ) |
| 771 | ) => { |
| 772 | named!($name(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 773 | mut e: call!($next, allow_struct, allow_block) >> |
| 774 | many0!(do_parse!( |
| 775 | op: $submac!($($args)*) >> |
| 776 | rhs: call!($next, allow_struct, true) >> |
| 777 | ({ |
| 778 | e = ExprBinary { |
| 779 | left: Box::new(e.into()), |
| 780 | op: op, |
| 781 | right: Box::new(rhs.into()), |
| 782 | }.into(); |
| 783 | }) |
| 784 | )) >> |
| 785 | (e) |
| 786 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 787 | } |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 788 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 789 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 790 | // <placement> = <placement> .. |
| 791 | // <placement> += <placement> .. |
| 792 | // <placement> -= <placement> .. |
| 793 | // <placement> *= <placement> .. |
| 794 | // <placement> /= <placement> .. |
| 795 | // <placement> %= <placement> .. |
| 796 | // <placement> ^= <placement> .. |
| 797 | // <placement> &= <placement> .. |
| 798 | // <placement> |= <placement> .. |
| 799 | // <placement> <<= <placement> .. |
| 800 | // <placement> >>= <placement> .. |
| 801 | // |
| 802 | // NOTE: This operator is right-associative. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 803 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 804 | named!(assign_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 805 | mut e: call!(placement_expr, allow_struct, allow_block) >> |
| 806 | alt!( |
| 807 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 808 | eq: punct!(=) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 809 | // Recurse into self to parse right-associative operator. |
| 810 | rhs: call!(assign_expr, allow_struct, true) >> |
| 811 | ({ |
| 812 | e = ExprAssign { |
| 813 | left: Box::new(e.into()), |
| 814 | eq_token: eq, |
| 815 | right: Box::new(rhs.into()), |
| 816 | }.into(); |
| 817 | }) |
| 818 | ) |
| 819 | | |
| 820 | do_parse!( |
| 821 | op: call!(BinOp::parse_assign_op) >> |
| 822 | // Recurse into self to parse right-associative operator. |
| 823 | rhs: call!(assign_expr, allow_struct, true) >> |
| 824 | ({ |
| 825 | e = ExprAssignOp { |
| 826 | left: Box::new(e.into()), |
| 827 | op: op, |
| 828 | right: Box::new(rhs.into()), |
| 829 | }.into(); |
| 830 | }) |
| 831 | ) |
| 832 | | |
| 833 | epsilon!() |
| 834 | ) >> |
| 835 | (e) |
| 836 | )); |
| 837 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 838 | // <range> <- <range> .. |
| 839 | // |
| 840 | // NOTE: The `in place { expr }` version of this syntax is parsed in |
| 841 | // `atom_expr`, not here. |
| 842 | // |
| 843 | // NOTE: This operator is right-associative. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 844 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 845 | named!(placement_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 846 | mut e: call!(range_expr, allow_struct, allow_block) >> |
| 847 | alt!( |
| 848 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 849 | arrow: punct!(<-) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 850 | // Recurse into self to parse right-associative operator. |
| 851 | rhs: call!(placement_expr, allow_struct, true) >> |
| 852 | ({ |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 853 | e = ExprInPlace { |
| 854 | // op: BinOp::Place(larrow), |
| 855 | place: Box::new(e.into()), |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 856 | kind: InPlaceKind::Arrow(arrow), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 857 | value: Box::new(rhs.into()), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 858 | }.into(); |
| 859 | }) |
| 860 | ) |
| 861 | | |
| 862 | epsilon!() |
| 863 | ) >> |
| 864 | (e) |
| 865 | )); |
| 866 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 867 | // <or> ... <or> .. |
| 868 | // <or> .. <or> .. |
| 869 | // <or> .. |
| 870 | // |
| 871 | // NOTE: This is currently parsed oddly - I'm not sure of what the exact |
| 872 | // rules are for parsing these expressions are, but this is not correct. |
| 873 | // For example, `a .. b .. c` is not a legal expression. It should not |
| 874 | // be parsed as either `(a .. b) .. c` or `a .. (b .. c)` apparently. |
| 875 | // |
| 876 | // NOTE: The form of ranges which don't include a preceding expression are |
| 877 | // parsed by `atom_expr`, rather than by this function. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 878 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 879 | named!(range_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 880 | mut e: call!(or_expr, allow_struct, allow_block) >> |
| 881 | many0!(do_parse!( |
| 882 | limits: syn!(RangeLimits) >> |
| 883 | // We don't want to allow blocks here if we don't allow structs. See |
| 884 | // the reasoning for `opt_ambiguous_expr!` above. |
| 885 | hi: option!(call!(or_expr, allow_struct, allow_struct)) >> |
| 886 | ({ |
| 887 | e = ExprRange { |
| 888 | from: Some(Box::new(e.into())), |
| 889 | limits: limits, |
| 890 | to: hi.map(|e| Box::new(e.into())), |
| 891 | }.into(); |
| 892 | }) |
| 893 | )) >> |
| 894 | (e) |
| 895 | )); |
| 896 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 897 | // <and> || <and> ... |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 898 | binop!(or_expr, and_expr, map!(punct!(||), BinOp::Or)); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 899 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 900 | // <compare> && <compare> ... |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 901 | binop!(and_expr, compare_expr, map!(punct!(&&), BinOp::And)); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 902 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 903 | // <bitor> == <bitor> ... |
| 904 | // <bitor> != <bitor> ... |
| 905 | // <bitor> >= <bitor> ... |
| 906 | // <bitor> <= <bitor> ... |
| 907 | // <bitor> > <bitor> ... |
| 908 | // <bitor> < <bitor> ... |
| 909 | // |
| 910 | // NOTE: This operator appears to be parsed as left-associative, but errors |
| 911 | // if it is used in a non-associative manner. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 912 | binop!( |
| 913 | compare_expr, |
| 914 | bitor_expr, |
| 915 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 916 | punct!(==) => { BinOp::Eq } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 917 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 918 | punct!(!=) => { BinOp::Ne } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 919 | | |
| 920 | // must be above Lt |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 921 | punct!(<=) => { BinOp::Le } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 922 | | |
| 923 | // must be above Gt |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 924 | punct!(>=) => { BinOp::Ge } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 925 | | |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 926 | do_parse!( |
| 927 | // Make sure that we don't eat the < part of a <- operator |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 928 | not!(punct!(<-)) >> |
| 929 | t: punct!(<) >> |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 930 | (BinOp::Lt(t)) |
| 931 | ) |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 932 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 933 | punct!(>) => { BinOp::Gt } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 934 | ) |
| 935 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 936 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 937 | // <bitxor> | <bitxor> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 938 | binop!( |
| 939 | bitor_expr, |
| 940 | bitxor_expr, |
| 941 | do_parse!(not!(punct!(||)) >> not!(punct!(|=)) >> t: punct!(|) >> (BinOp::BitOr(t))) |
| 942 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 943 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 944 | // <bitand> ^ <bitand> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 945 | binop!( |
| 946 | bitxor_expr, |
| 947 | bitand_expr, |
| 948 | do_parse!( |
| 949 | // NOTE: Make sure we aren't looking at ^=. |
| 950 | not!(punct!(^=)) >> t: punct!(^) >> (BinOp::BitXor(t)) |
| 951 | ) |
| 952 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 953 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 954 | // <shift> & <shift> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 955 | binop!( |
| 956 | bitand_expr, |
| 957 | shift_expr, |
| 958 | do_parse!( |
| 959 | // NOTE: Make sure we aren't looking at && or &=. |
| 960 | not!(punct!(&&)) >> not!(punct!(&=)) >> t: punct!(&) >> (BinOp::BitAnd(t)) |
| 961 | ) |
| 962 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 963 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 964 | // <arith> << <arith> ... |
| 965 | // <arith> >> <arith> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 966 | binop!( |
| 967 | shift_expr, |
| 968 | arith_expr, |
| 969 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 970 | punct!(<<) => { BinOp::Shl } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 971 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 972 | punct!(>>) => { BinOp::Shr } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 973 | ) |
| 974 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 975 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 976 | // <term> + <term> ... |
| 977 | // <term> - <term> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 978 | binop!( |
| 979 | arith_expr, |
| 980 | term_expr, |
| 981 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 982 | punct!(+) => { BinOp::Add } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 983 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 984 | punct!(-) => { BinOp::Sub } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 985 | ) |
| 986 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 987 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 988 | // <cast> * <cast> ... |
| 989 | // <cast> / <cast> ... |
| 990 | // <cast> % <cast> ... |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 991 | binop!( |
| 992 | term_expr, |
| 993 | cast_expr, |
| 994 | alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 995 | punct!(*) => { BinOp::Mul } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 996 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 997 | punct!(/) => { BinOp::Div } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 998 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 999 | punct!(%) => { BinOp::Rem } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 1000 | ) |
| 1001 | ); |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1002 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1003 | // <unary> as <ty> |
| 1004 | // <unary> : <ty> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1005 | named!(cast_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 1006 | mut e: call!(unary_expr, allow_struct, allow_block) >> |
| 1007 | many0!(alt!( |
| 1008 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1009 | as_: keyword!(as) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1010 | // We can't accept `A + B` in cast expressions, as it's |
| 1011 | // ambiguous with the + expression. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1012 | ty: call!(Type::without_plus) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1013 | ({ |
| 1014 | e = ExprCast { |
| 1015 | expr: Box::new(e.into()), |
| 1016 | as_token: as_, |
| 1017 | ty: Box::new(ty), |
| 1018 | }.into(); |
| 1019 | }) |
| 1020 | ) |
| 1021 | | |
| 1022 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1023 | colon: punct!(:) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1024 | // We can't accept `A + B` in cast expressions, as it's |
| 1025 | // ambiguous with the + expression. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1026 | ty: call!(Type::without_plus) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1027 | ({ |
| 1028 | e = ExprType { |
| 1029 | expr: Box::new(e.into()), |
| 1030 | colon_token: colon, |
| 1031 | ty: Box::new(ty), |
| 1032 | }.into(); |
| 1033 | }) |
| 1034 | ) |
| 1035 | )) >> |
| 1036 | (e) |
| 1037 | )); |
| 1038 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1039 | // <UnOp> <trailer> |
| 1040 | // & <trailer> |
| 1041 | // &mut <trailer> |
| 1042 | // box <trailer> |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1043 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1044 | named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!( |
| 1045 | do_parse!( |
| 1046 | op: syn!(UnOp) >> |
| 1047 | expr: call!(unary_expr, allow_struct, true) >> |
| 1048 | (ExprUnary { |
| 1049 | op: op, |
| 1050 | expr: Box::new(expr.into()), |
| 1051 | }.into()) |
| 1052 | ) |
| 1053 | | |
| 1054 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1055 | and: punct!(&) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1056 | mutability: syn!(Mutability) >> |
| 1057 | expr: call!(unary_expr, allow_struct, true) >> |
| 1058 | (ExprAddrOf { |
| 1059 | and_token: and, |
| 1060 | mutbl: mutability, |
| 1061 | expr: Box::new(expr.into()), |
| 1062 | }.into()) |
| 1063 | ) |
| 1064 | | |
| 1065 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1066 | box_: keyword!(box) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1067 | expr: call!(unary_expr, allow_struct, true) >> |
| 1068 | (ExprBox { |
| 1069 | box_token: box_, |
| 1070 | expr: Box::new(expr.into()), |
| 1071 | }.into()) |
| 1072 | ) |
| 1073 | | |
| 1074 | call!(trailer_expr, allow_struct, allow_block) |
| 1075 | )); |
| 1076 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1077 | // XXX: This duplication is ugly |
| 1078 | #[cfg(not(feature = "full"))] |
| 1079 | named!(unary_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!( |
| 1080 | do_parse!( |
| 1081 | op: syn!(UnOp) >> |
| 1082 | expr: call!(unary_expr, allow_struct, true) >> |
| 1083 | (ExprUnary { |
| 1084 | op: op, |
| 1085 | expr: Box::new(expr.into()), |
| 1086 | }.into()) |
| 1087 | ) |
| 1088 | | |
| 1089 | call!(trailer_expr, allow_struct, allow_block) |
| 1090 | )); |
| 1091 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1092 | // <atom> (..<args>) ... |
| 1093 | // <atom> . <ident> (..<args>) ... |
| 1094 | // <atom> . <ident> ... |
| 1095 | // <atom> . <lit> ... |
| 1096 | // <atom> [ <expr> ] ... |
| 1097 | // <atom> ? ... |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1098 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1099 | named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 1100 | mut e: call!(atom_expr, allow_struct, allow_block) >> |
| 1101 | many0!(alt!( |
| 1102 | tap!(args: and_call => { |
| 1103 | let (args, paren) = args; |
| 1104 | e = ExprCall { |
| 1105 | func: Box::new(e.into()), |
| 1106 | args: args, |
| 1107 | paren_token: paren, |
| 1108 | }.into(); |
| 1109 | }) |
| 1110 | | |
| 1111 | tap!(more: and_method_call => { |
| 1112 | let mut call = more; |
| 1113 | call.expr = Box::new(e.into()); |
| 1114 | e = call.into(); |
| 1115 | }) |
| 1116 | | |
| 1117 | tap!(field: and_field => { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1118 | let (token, member) = field; |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1119 | e = ExprField { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1120 | base: Box::new(e.into()), |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1121 | dot_token: token, |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1122 | member: member, |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1123 | }.into(); |
| 1124 | }) |
| 1125 | | |
| 1126 | tap!(i: and_index => { |
| 1127 | let (i, token) = i; |
| 1128 | e = ExprIndex { |
| 1129 | expr: Box::new(e.into()), |
| 1130 | bracket_token: token, |
| 1131 | index: Box::new(i), |
| 1132 | }.into(); |
| 1133 | }) |
| 1134 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1135 | tap!(question: punct!(?) => { |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1136 | e = ExprTry { |
| 1137 | expr: Box::new(e.into()), |
| 1138 | question_token: question, |
| 1139 | }.into(); |
| 1140 | }) |
| 1141 | )) >> |
| 1142 | (e) |
| 1143 | )); |
| 1144 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1145 | // XXX: Duplication == ugly |
| 1146 | #[cfg(not(feature = "full"))] |
| 1147 | named!(trailer_expr(allow_struct: bool, allow_block: bool) -> ExprKind, do_parse!( |
| 1148 | mut e: call!(atom_expr, allow_struct, allow_block) >> |
| 1149 | many0!(alt!( |
| 1150 | tap!(args: and_call => { |
| 1151 | let (args, paren) = args; |
| 1152 | e = ExprCall { |
| 1153 | func: Box::new(e.into()), |
| 1154 | args: args, |
| 1155 | paren_token: paren, |
| 1156 | }.into(); |
| 1157 | }) |
| 1158 | | |
| 1159 | tap!(i: and_index => { |
| 1160 | let (i, token) = i; |
| 1161 | e = ExprIndex { |
| 1162 | expr: Box::new(e.into()), |
| 1163 | bracket_token: token, |
| 1164 | index: Box::new(i), |
| 1165 | }.into(); |
| 1166 | }) |
| 1167 | )) >> |
| 1168 | (e) |
| 1169 | )); |
| 1170 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1171 | // Parse all atomic expressions which don't have to worry about precidence |
| 1172 | // interactions, as they are fully contained. |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1173 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1174 | named!(atom_expr(allow_struct: bool, allow_block: bool) -> ExprKind, alt!( |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1175 | syn!(ExprGroup) => { ExprKind::Group } // must be placed first |
| 1176 | | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1177 | syn!(Lit) => { ExprKind::Lit } // must be before expr_struct |
| 1178 | | |
| 1179 | // must be before expr_path |
| 1180 | cond_reduce!(allow_struct, map!(syn!(ExprStruct), ExprKind::Struct)) |
| 1181 | | |
| 1182 | syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup |
| 1183 | | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 1184 | syn!(Macro) => { ExprKind::Macro } // must be before expr_path |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1185 | | |
| 1186 | call!(expr_break, allow_struct) // must be before expr_path |
| 1187 | | |
| 1188 | syn!(ExprContinue) => { ExprKind::Continue } // must be before expr_path |
| 1189 | | |
| 1190 | call!(expr_ret, allow_struct) // must be before expr_path |
| 1191 | | |
| 1192 | // NOTE: The `in place { expr }` form. `place <- expr` is parsed above. |
| 1193 | syn!(ExprInPlace) => { ExprKind::InPlace } |
| 1194 | | |
| 1195 | syn!(ExprArray) => { ExprKind::Array } |
| 1196 | | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 1197 | syn!(ExprTuple) => { ExprKind::Tuple } |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1198 | | |
| 1199 | syn!(ExprIf) => { ExprKind::If } |
| 1200 | | |
| 1201 | syn!(ExprIfLet) => { ExprKind::IfLet } |
| 1202 | | |
| 1203 | syn!(ExprWhile) => { ExprKind::While } |
| 1204 | | |
| 1205 | syn!(ExprWhileLet) => { ExprKind::WhileLet } |
| 1206 | | |
| 1207 | syn!(ExprForLoop) => { ExprKind::ForLoop } |
| 1208 | | |
| 1209 | syn!(ExprLoop) => { ExprKind::Loop } |
| 1210 | | |
| 1211 | syn!(ExprMatch) => { ExprKind::Match } |
| 1212 | | |
| 1213 | syn!(ExprCatch) => { ExprKind::Catch } |
| 1214 | | |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1215 | syn!(ExprYield) => { ExprKind::Yield } |
| 1216 | | |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1217 | syn!(ExprUnsafe) => { ExprKind::Unsafe } |
| 1218 | | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1219 | call!(expr_closure, allow_struct) |
| 1220 | | |
| 1221 | cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block)) |
| 1222 | | |
| 1223 | // NOTE: This is the prefix-form of range |
| 1224 | call!(expr_range, allow_struct) |
| 1225 | | |
| 1226 | syn!(ExprPath) => { ExprKind::Path } |
| 1227 | | |
| 1228 | syn!(ExprRepeat) => { ExprKind::Repeat } |
| 1229 | )); |
| 1230 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1231 | #[cfg(not(feature = "full"))] |
| 1232 | named!(atom_expr(_allow_struct: bool, _allow_block: bool) -> ExprKind, alt!( |
| 1233 | syn!(ExprGroup) => { ExprKind::Group } // must be placed first |
| 1234 | | |
| 1235 | syn!(Lit) => { ExprKind::Lit } // must be before expr_struct |
| 1236 | | |
| 1237 | syn!(ExprParen) => { ExprKind::Paren } // must be before expr_tup |
| 1238 | | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 1239 | syn!(Macro) => { ExprKind::Macro } // must be before expr_path |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1240 | | |
| 1241 | syn!(ExprPath) => { ExprKind::Path } |
| 1242 | )); |
| 1243 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1244 | #[cfg(feature = "full")] |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1245 | named!(expr_nosemi -> Expr, map!(alt!( |
| 1246 | syn!(ExprIf) => { ExprKind::If } |
| 1247 | | |
| 1248 | syn!(ExprIfLet) => { ExprKind::IfLet } |
| 1249 | | |
| 1250 | syn!(ExprWhile) => { ExprKind::While } |
| 1251 | | |
| 1252 | syn!(ExprWhileLet) => { ExprKind::WhileLet } |
| 1253 | | |
| 1254 | syn!(ExprForLoop) => { ExprKind::ForLoop } |
| 1255 | | |
| 1256 | syn!(ExprLoop) => { ExprKind::Loop } |
| 1257 | | |
| 1258 | syn!(ExprMatch) => { ExprKind::Match } |
| 1259 | | |
| 1260 | syn!(ExprCatch) => { ExprKind::Catch } |
| 1261 | | |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1262 | syn!(ExprYield) => { ExprKind::Yield } |
| 1263 | | |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1264 | syn!(ExprUnsafe) => { ExprKind::Unsafe } |
| 1265 | | |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1266 | syn!(ExprBlock) => { ExprKind::Block } |
| 1267 | ), Expr::from)); |
| 1268 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1269 | impl Synom for ExprGroup { |
| 1270 | named!(parse -> Self, do_parse!( |
| 1271 | e: grouped!(syn!(Expr)) >> |
| 1272 | (ExprGroup { |
| 1273 | expr: Box::new(e.0), |
| 1274 | group_token: e.1, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1275 | }) |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1276 | )); |
| 1277 | } |
| 1278 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1279 | impl Synom for ExprParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1280 | named!(parse -> Self, do_parse!( |
| 1281 | e: parens!(syn!(Expr)) >> |
| 1282 | (ExprParen { |
| 1283 | expr: Box::new(e.0), |
| 1284 | paren_token: e.1, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1285 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1286 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1287 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1288 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1289 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1290 | impl Synom for ExprInPlace { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1291 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1292 | in_: keyword!(in) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1293 | place: expr_no_struct >> |
| 1294 | value: braces!(call!(Block::parse_within)) >> |
| 1295 | (ExprInPlace { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1296 | place: Box::new(place), |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 1297 | kind: InPlaceKind::In(in_), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1298 | value: Box::new(Expr { |
| 1299 | node: ExprBlock { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1300 | block: Block { |
| 1301 | stmts: value.0, |
| 1302 | brace_token: value.1, |
| 1303 | }, |
| 1304 | }.into(), |
| 1305 | attrs: Vec::new(), |
| 1306 | }), |
| 1307 | }) |
| 1308 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1309 | } |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 1310 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1311 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1312 | impl Synom for ExprArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1313 | named!(parse -> Self, do_parse!( |
| 1314 | elems: brackets!(call!(Delimited::parse_terminated)) >> |
| 1315 | (ExprArray { |
| 1316 | exprs: elems.0, |
| 1317 | bracket_token: elems.1, |
| 1318 | }) |
| 1319 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1320 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1321 | |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 1322 | named!(and_call -> (Delimited<Expr, Token![,]>, token::Paren), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1323 | parens!(call!(Delimited::parse_terminated))); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1324 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1325 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1326 | named!(and_method_call -> ExprMethodCall, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1327 | dot: punct!(.) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1328 | method: syn!(Ident) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1329 | typarams: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1330 | colon2: punct!(::) >> |
| 1331 | lt: punct!(<) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1332 | tys: call!(Delimited::parse_terminated) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1333 | gt: punct!(>) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1334 | (colon2, lt, tys, gt) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1335 | )) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1336 | args: parens!(call!(Delimited::parse_terminated)) >> |
| 1337 | ({ |
| 1338 | let (colon2, lt, tys, gt) = match typarams { |
| 1339 | Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)), |
| 1340 | None => (None, None, None, None), |
| 1341 | }; |
| 1342 | ExprMethodCall { |
| 1343 | // this expr will get overwritten after being returned |
| 1344 | expr: Box::new(ExprKind::Lit(Lit { |
| 1345 | span: Span::default(), |
| 1346 | value: LitKind::Bool(false), |
| 1347 | }).into()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1348 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1349 | method: method, |
| 1350 | args: args.0, |
| 1351 | paren_token: args.1, |
| 1352 | dot_token: dot, |
| 1353 | lt_token: lt, |
| 1354 | gt_token: gt, |
| 1355 | colon2_token: colon2, |
| 1356 | typarams: tys.unwrap_or_default(), |
| 1357 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1358 | }) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1359 | )); |
| 1360 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1361 | #[cfg(feature = "full")] |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 1362 | impl Synom for ExprTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1363 | named!(parse -> Self, do_parse!( |
| 1364 | elems: parens!(call!(Delimited::parse_terminated)) >> |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 1365 | (ExprTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1366 | args: elems.0, |
| 1367 | paren_token: elems.1, |
| 1368 | lone_comma: None, // TODO: parse this |
| 1369 | }) |
| 1370 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1371 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1372 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1373 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1374 | impl Synom for ExprIfLet { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1375 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1376 | if_: keyword!(if) >> |
| 1377 | let_: keyword!(let) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1378 | pat: syn!(Pat) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1379 | eq: punct!(=) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1380 | cond: expr_no_struct >> |
| 1381 | then_block: braces!(call!(Block::parse_within)) >> |
| 1382 | else_block: option!(else_block) >> |
| 1383 | (ExprIfLet { |
| 1384 | pat: Box::new(pat), |
| 1385 | let_token: let_, |
| 1386 | eq_token: eq, |
| 1387 | expr: Box::new(cond), |
| 1388 | if_true: Block { |
| 1389 | stmts: then_block.0, |
| 1390 | brace_token: then_block.1, |
| 1391 | }, |
| 1392 | if_token: if_, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1393 | else_token: else_block.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1394 | if_false: else_block.map(|p| Box::new(p.1.into())), |
| 1395 | }) |
| 1396 | )); |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1399 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1400 | impl Synom for ExprIf { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1401 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1402 | if_: keyword!(if) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1403 | cond: expr_no_struct >> |
| 1404 | then_block: braces!(call!(Block::parse_within)) >> |
| 1405 | else_block: option!(else_block) >> |
| 1406 | (ExprIf { |
| 1407 | cond: Box::new(cond), |
| 1408 | if_true: Block { |
| 1409 | stmts: then_block.0, |
| 1410 | brace_token: then_block.1, |
| 1411 | }, |
| 1412 | if_token: if_, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1413 | else_token: else_block.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1414 | if_false: else_block.map(|p| Box::new(p.1.into())), |
| 1415 | }) |
| 1416 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1417 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1418 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1419 | #[cfg(feature = "full")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1420 | named!(else_block -> (Token![else], ExprKind), do_parse!( |
| 1421 | else_: keyword!(else) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1422 | expr: alt!( |
| 1423 | syn!(ExprIf) => { ExprKind::If } |
| 1424 | | |
| 1425 | syn!(ExprIfLet) => { ExprKind::IfLet } |
| 1426 | | |
| 1427 | do_parse!( |
| 1428 | else_block: braces!(call!(Block::parse_within)) >> |
| 1429 | (ExprKind::Block(ExprBlock { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1430 | block: Block { |
| 1431 | stmts: else_block.0, |
| 1432 | brace_token: else_block.1, |
| 1433 | }, |
| 1434 | })) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1435 | ) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1436 | ) >> |
| 1437 | (else_, expr) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1438 | )); |
| 1439 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1440 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1441 | impl Synom for ExprForLoop { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1442 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1443 | lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >> |
| 1444 | for_: keyword!(for) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1445 | pat: syn!(Pat) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1446 | in_: keyword!(in) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1447 | expr: expr_no_struct >> |
| 1448 | loop_block: syn!(Block) >> |
| 1449 | (ExprForLoop { |
| 1450 | for_token: for_, |
| 1451 | in_token: in_, |
| 1452 | pat: Box::new(pat), |
| 1453 | expr: Box::new(expr), |
| 1454 | body: loop_block, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1455 | colon_token: lbl.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1456 | label: lbl.map(|p| p.0), |
| 1457 | }) |
| 1458 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1459 | } |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 1460 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1461 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1462 | impl Synom for ExprLoop { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1463 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1464 | lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >> |
| 1465 | loop_: keyword!(loop) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1466 | loop_block: syn!(Block) >> |
| 1467 | (ExprLoop { |
| 1468 | loop_token: loop_, |
| 1469 | body: loop_block, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1470 | colon_token: lbl.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1471 | label: lbl.map(|p| p.0), |
| 1472 | }) |
| 1473 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1476 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1477 | impl Synom for ExprMatch { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1478 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1479 | match_: keyword!(match) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1480 | obj: expr_no_struct >> |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1481 | res: braces!(many0!(Arm::parse)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1482 | ({ |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1483 | let (arms, brace) = res; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1484 | ExprMatch { |
| 1485 | expr: Box::new(obj), |
| 1486 | match_token: match_, |
| 1487 | brace_token: brace, |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1488 | arms: arms, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1489 | } |
| 1490 | }) |
| 1491 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1492 | } |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1493 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1494 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1495 | impl Synom for ExprCatch { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1496 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1497 | do_: keyword!(do) >> |
| 1498 | catch_: keyword!(catch) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1499 | catch_block: syn!(Block) >> |
| 1500 | (ExprCatch { |
| 1501 | block: catch_block, |
| 1502 | do_token: do_, |
| 1503 | catch_token: catch_, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1504 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1505 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1506 | } |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1507 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1508 | #[cfg(feature = "full")] |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1509 | impl Synom for ExprYield { |
| 1510 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1511 | yield_: keyword!(yield) >> |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 1512 | expr: option!(syn!(Expr)) >> |
| 1513 | (ExprYield { |
| 1514 | yield_token: yield_, |
| 1515 | expr: expr.map(Box::new), |
| 1516 | }) |
| 1517 | )); |
| 1518 | } |
| 1519 | |
| 1520 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1521 | impl Synom for Arm { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1522 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1523 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1524 | pats: call!(Delimited::parse_separated_nonempty) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1525 | guard: option!(tuple!(keyword!(if), syn!(Expr))) >> |
| 1526 | rocket: punct!(=>) >> |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1527 | body: do_parse!( |
| 1528 | expr: alt!(expr_nosemi | syn!(Expr)) >> |
| 1529 | comma1: cond!(arm_expr_requires_comma(&expr), alt!( |
| 1530 | map!(input_end!(), |_| None) |
| 1531 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1532 | map!(punct!(,), Some) |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1533 | )) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1534 | comma2: cond!(!arm_expr_requires_comma(&expr), option!(punct!(,))) >> |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 1535 | (expr, comma1.and_then(|x| x).or_else(|| comma2.and_then(|x| x))) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1536 | ) >> |
| 1537 | (Arm { |
| 1538 | rocket_token: rocket, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1539 | if_token: guard.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1540 | attrs: attrs, |
| 1541 | pats: pats, |
| 1542 | guard: guard.map(|p| Box::new(p.1)), |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 1543 | body: Box::new(body.0), |
| 1544 | comma: body.1, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1545 | }) |
| 1546 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1547 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1548 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1549 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1550 | named!(expr_closure(allow_struct: bool) -> ExprKind, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1551 | capture: syn!(CaptureBy) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1552 | or1: punct!(|) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1553 | inputs: call!(Delimited::parse_terminated_with, fn_arg) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1554 | or2: punct!(|) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1555 | ret_and_body: alt!( |
| 1556 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1557 | arrow: punct!(->) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1558 | ty: syn!(Type) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1559 | body: syn!(Block) >> |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 1560 | (ReturnType::Type(Box::new(ty), arrow), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1561 | ExprKind::Block(ExprBlock { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1562 | block: body, |
| 1563 | }).into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1564 | ) |
| 1565 | | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 1566 | map!(ambiguous_expr!(allow_struct), |e| (ReturnType::Default, e)) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1567 | ) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1568 | (ExprClosure { |
| 1569 | capture: capture, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1570 | or1_token: or1, |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 1571 | inputs: inputs, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1572 | or2_token: or2, |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 1573 | output: ret_and_body.0, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1574 | body: Box::new(ret_and_body.1), |
| 1575 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1576 | )); |
| 1577 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1578 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1579 | named!(fn_arg -> FnArg, do_parse!( |
| 1580 | pat: syn!(Pat) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1581 | ty: option!(tuple!(punct!(:), syn!(Type))) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1582 | ({ |
| 1583 | let (colon, ty) = ty.unwrap_or_else(|| { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1584 | (<Token![:]>::default(), TypeInfer { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1585 | underscore_token: <Token![_]>::default(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1586 | }.into()) |
| 1587 | }); |
| 1588 | ArgCaptured { |
| 1589 | pat: pat, |
| 1590 | colon_token: colon, |
| 1591 | ty: ty, |
| 1592 | }.into() |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1593 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 1594 | )); |
| 1595 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1596 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1597 | impl Synom for ExprWhile { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1598 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1599 | lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >> |
| 1600 | while_: keyword!(while) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1601 | cond: expr_no_struct >> |
| 1602 | while_block: syn!(Block) >> |
| 1603 | (ExprWhile { |
| 1604 | while_token: while_, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1605 | colon_token: lbl.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1606 | cond: Box::new(cond), |
| 1607 | body: while_block, |
| 1608 | label: lbl.map(|p| p.0), |
| 1609 | }) |
| 1610 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1613 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1614 | impl Synom for ExprWhileLet { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1615 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1616 | lbl: option!(tuple!(syn!(Lifetime), punct!(:))) >> |
| 1617 | while_: keyword!(while) >> |
| 1618 | let_: keyword!(let) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1619 | pat: syn!(Pat) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1620 | eq: punct!(=) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1621 | value: expr_no_struct >> |
| 1622 | while_block: syn!(Block) >> |
| 1623 | (ExprWhileLet { |
| 1624 | eq_token: eq, |
| 1625 | let_token: let_, |
| 1626 | while_token: while_, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1627 | colon_token: lbl.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1628 | pat: Box::new(pat), |
| 1629 | expr: Box::new(value), |
| 1630 | body: while_block, |
| 1631 | label: lbl.map(|p| p.0), |
| 1632 | }) |
| 1633 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1636 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1637 | impl Synom for ExprContinue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1638 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1639 | cont: keyword!(continue) >> |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1640 | lbl: option!(syn!(Lifetime)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1641 | (ExprContinue { |
| 1642 | continue_token: cont, |
| 1643 | label: lbl, |
| 1644 | }) |
| 1645 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1646 | } |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1647 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1648 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1649 | named!(expr_break(allow_struct: bool) -> ExprKind, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1650 | break_: keyword!(break) >> |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1651 | lbl: option!(syn!(Lifetime)) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1652 | // We can't allow blocks after a `break` expression when we wouldn't |
| 1653 | // allow structs, as this expression is ambiguous. |
| 1654 | val: opt_ambiguous_expr!(allow_struct) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1655 | (ExprBreak { |
| 1656 | label: lbl, |
| 1657 | expr: val.map(Box::new), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1658 | break_token: break_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1659 | }.into()) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1660 | )); |
| 1661 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1662 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1663 | named!(expr_ret(allow_struct: bool) -> ExprKind, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1664 | return_: keyword!(return) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1665 | // NOTE: return is greedy and eats blocks after it even when in a |
| 1666 | // position where structs are not allowed, such as in if statement |
| 1667 | // conditions. For example: |
| 1668 | // |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 1669 | // if return { println!("A") } {} // Prints "A" |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1670 | ret_value: option!(ambiguous_expr!(allow_struct)) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1671 | (ExprRet { |
| 1672 | expr: ret_value.map(Box::new), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1673 | return_token: return_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1674 | }.into()) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1675 | )); |
| 1676 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1677 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1678 | impl Synom for ExprStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1679 | named!(parse -> Self, do_parse!( |
| 1680 | path: syn!(Path) >> |
| 1681 | data: braces!(do_parse!( |
| 1682 | fields: call!(Delimited::parse_terminated) >> |
| 1683 | base: option!( |
| 1684 | cond!(fields.is_empty() || fields.trailing_delim(), |
| 1685 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1686 | dots: punct!(..) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1687 | base: syn!(Expr) >> |
| 1688 | (dots, base) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1689 | ) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1690 | ) |
| 1691 | ) >> |
| 1692 | (fields, base) |
| 1693 | )) >> |
| 1694 | ({ |
| 1695 | let ((fields, base), brace) = data; |
| 1696 | let (dots, rest) = match base.and_then(|b| b) { |
| 1697 | Some((dots, base)) => (Some(dots), Some(base)), |
| 1698 | None => (None, None), |
| 1699 | }; |
| 1700 | ExprStruct { |
| 1701 | brace_token: brace, |
| 1702 | path: path, |
| 1703 | fields: fields, |
| 1704 | dot2_token: dots, |
| 1705 | rest: rest.map(Box::new), |
| 1706 | } |
| 1707 | }) |
| 1708 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1711 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1712 | impl Synom for FieldValue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1713 | named!(parse -> Self, alt!( |
| 1714 | do_parse!( |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1715 | member: syn!(Member) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1716 | colon: punct!(:) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1717 | value: syn!(Expr) >> |
| 1718 | (FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1719 | member: member, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1720 | expr: value, |
| 1721 | is_shorthand: false, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1722 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1723 | colon_token: Some(colon), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1724 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1725 | ) |
| 1726 | | |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 1727 | map!(syn!(Ident), |name| FieldValue { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1728 | member: Member::Named(name), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1729 | expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(), |
| 1730 | is_shorthand: true, |
| 1731 | attrs: Vec::new(), |
| 1732 | colon_token: None, |
| 1733 | }) |
| 1734 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1735 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1736 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1737 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1738 | impl Synom for ExprRepeat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1739 | named!(parse -> Self, do_parse!( |
| 1740 | data: brackets!(do_parse!( |
| 1741 | value: syn!(Expr) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1742 | semi: punct!(;) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1743 | times: syn!(Expr) >> |
| 1744 | (value, semi, times) |
| 1745 | )) >> |
| 1746 | (ExprRepeat { |
| 1747 | expr: Box::new((data.0).0), |
| 1748 | amt: Box::new((data.0).2), |
| 1749 | bracket_token: data.1, |
| 1750 | semi_token: (data.0).1, |
| 1751 | }) |
| 1752 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1753 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1754 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1755 | #[cfg(feature = "full")] |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 1756 | impl Synom for ExprUnsafe { |
| 1757 | named!(parse -> Self, do_parse!( |
| 1758 | unsafe_: keyword!(unsafe) >> |
| 1759 | b: syn!(Block) >> |
| 1760 | (ExprUnsafe { |
| 1761 | unsafe_token: unsafe_, |
| 1762 | block: b, |
| 1763 | }) |
| 1764 | )); |
| 1765 | } |
| 1766 | |
| 1767 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1768 | impl Synom for ExprBlock { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1769 | named!(parse -> Self, do_parse!( |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1770 | b: syn!(Block) >> |
| 1771 | (ExprBlock { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1772 | block: b, |
| 1773 | }) |
| 1774 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1775 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1776 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1777 | #[cfg(feature = "full")] |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1778 | named!(expr_range(allow_struct: bool) -> ExprKind, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1779 | limits: syn!(RangeLimits) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1780 | hi: opt_ambiguous_expr!(allow_struct) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1781 | (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into()) |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1782 | )); |
| 1783 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1784 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1785 | impl Synom for RangeLimits { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1786 | named!(parse -> Self, alt!( |
| 1787 | // Must come before Dot2 |
David Tolnay | be55d7b | 2017-12-17 23:41:20 -0800 | [diff] [blame] | 1788 | punct!(..=) => { RangeLimits::Closed } |
| 1789 | | |
| 1790 | // Must come before Dot2 |
David Tolnay | 995bff2 | 2017-12-17 23:44:43 -0800 | [diff] [blame] | 1791 | punct!(...) => { |dot3| RangeLimits::Closed(Token) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1792 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1793 | punct!(..) => { RangeLimits::HalfOpen } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1794 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1795 | } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1796 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1797 | impl Synom for ExprPath { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1798 | named!(parse -> Self, do_parse!( |
| 1799 | pair: qpath >> |
| 1800 | (ExprPath { |
| 1801 | qself: pair.0, |
| 1802 | path: pair.1, |
| 1803 | }) |
| 1804 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1805 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1806 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1807 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 1808 | named!(and_field -> (Token![.], Member), tuple!(punct!(.), syn!(Member))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1809 | |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 1810 | named!(and_index -> (Expr, token::Bracket), brackets!(syn!(Expr))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1811 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1812 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1813 | impl Synom for Block { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1814 | named!(parse -> Self, do_parse!( |
| 1815 | stmts: braces!(call!(Block::parse_within)) >> |
| 1816 | (Block { |
| 1817 | stmts: stmts.0, |
| 1818 | brace_token: stmts.1, |
| 1819 | }) |
| 1820 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1821 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1822 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1823 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1824 | impl Block { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1825 | named!(pub parse_within -> Vec<Stmt>, do_parse!( |
David Tolnay | 4699a31 | 2017-12-27 14:39:22 -0500 | [diff] [blame] | 1826 | many0!(punct!(;)) >> |
| 1827 | mut standalone: many0!(terminated!(syn!(Stmt), many0!(punct!(;)))) >> |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 1828 | last: option!(do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1829 | attrs: many0!(Attribute::parse_outer) >> |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 1830 | mut e: syn!(Expr) >> |
| 1831 | ({ |
| 1832 | e.attrs = attrs; |
| 1833 | Stmt::Expr(Box::new(e)) |
| 1834 | }) |
| 1835 | )) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1836 | (match last { |
| 1837 | None => standalone, |
| 1838 | Some(last) => { |
Alex Crichton | 70bbd59 | 2017-08-27 10:40:03 -0700 | [diff] [blame] | 1839 | standalone.push(last); |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1840 | standalone |
| 1841 | } |
| 1842 | }) |
| 1843 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1846 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1847 | impl Synom for Stmt { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1848 | named!(parse -> Self, alt!( |
| 1849 | stmt_mac |
| 1850 | | |
| 1851 | stmt_local |
| 1852 | | |
| 1853 | stmt_item |
| 1854 | | |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1855 | stmt_blockexpr |
| 1856 | | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1857 | stmt_expr |
| 1858 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1859 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1860 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1861 | #[cfg(feature = "full")] |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1862 | named!(stmt_mac -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1863 | attrs: many0!(Attribute::parse_outer) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1864 | what: syn!(Path) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1865 | bang: punct!(!) >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1866 | // Only parse braces here; paren and bracket will get parsed as |
| 1867 | // expression statements |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1868 | data: braces!(syn!(TokenStream)) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1869 | semi: option!(punct!(;)) >> |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 1870 | (Stmt::Macro(Box::new(( |
| 1871 | Macro { |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1872 | path: what, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1873 | bang_token: bang, |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 1874 | tokens: proc_macro2::TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 1875 | span: (data.1).0, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 1876 | kind: TokenNode::Group(Delimiter::Brace, data.0), |
David Tolnay | 369f0c5 | 2017-12-27 01:50:45 -0500 | [diff] [blame] | 1877 | }, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1878 | }, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1879 | match semi { |
| 1880 | Some(semi) => MacStmtStyle::Semicolon(semi), |
| 1881 | None => MacStmtStyle::Braces, |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 1882 | }, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1883 | attrs, |
| 1884 | )))) |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1885 | )); |
| 1886 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1887 | #[cfg(feature = "full")] |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1888 | named!(stmt_local -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1889 | attrs: many0!(Attribute::parse_outer) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1890 | let_: keyword!(let) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1891 | pat: syn!(Pat) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1892 | ty: option!(tuple!(punct!(:), syn!(Type))) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1893 | init: option!(tuple!(punct!(=), syn!(Expr))) >> |
| 1894 | semi: punct!(;) >> |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1895 | (Stmt::Local(Box::new(Local { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1896 | let_token: let_, |
| 1897 | semi_token: semi, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1898 | colon_token: ty.as_ref().map(|p| Token.0)), |
| 1899 | eq_token: init.as_ref().map(|p| Token.0)), |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1900 | pat: Box::new(pat), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1901 | ty: ty.map(|p| Box::new(p.1)), |
| 1902 | init: init.map(|p| Box::new(p.1)), |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1903 | attrs: attrs, |
| 1904 | }))) |
| 1905 | )); |
| 1906 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1907 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1908 | named!(stmt_item -> Stmt, map!(syn!(Item), |i| Stmt::Item(Box::new(i)))); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1909 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1910 | #[cfg(feature = "full")] |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1911 | named!(stmt_blockexpr -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1912 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1913 | mut e: expr_nosemi >> |
| 1914 | // If the next token is a `.` or a `?` it is special-cased to parse as |
| 1915 | // an expression instead of a blockexpression. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1916 | not!(punct!(.)) >> |
| 1917 | not!(punct!(?)) >> |
| 1918 | semi: option!(punct!(;)) >> |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1919 | ({ |
| 1920 | e.attrs = attrs; |
| 1921 | if let Some(semi) = semi { |
| 1922 | Stmt::Semi(Box::new(e), semi) |
| 1923 | } else { |
| 1924 | Stmt::Expr(Box::new(e)) |
| 1925 | } |
| 1926 | }) |
| 1927 | )); |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1928 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1929 | #[cfg(feature = "full")] |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1930 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 1931 | attrs: many0!(Attribute::parse_outer) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1932 | mut e: syn!(Expr) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1933 | semi: punct!(;) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1934 | ({ |
| 1935 | e.attrs = attrs; |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1936 | Stmt::Semi(Box::new(e), semi) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1937 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1938 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1939 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1940 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1941 | impl Synom for Pat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1942 | named!(parse -> Self, alt!( |
| 1943 | syn!(PatWild) => { Pat::Wild } // must be before pat_ident |
| 1944 | | |
| 1945 | syn!(PatBox) => { Pat::Box } // must be before pat_ident |
| 1946 | | |
| 1947 | syn!(PatRange) => { Pat::Range } // must be before pat_lit |
| 1948 | | |
| 1949 | syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident |
| 1950 | | |
| 1951 | syn!(PatStruct) => { Pat::Struct } // must be before pat_ident |
| 1952 | | |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 1953 | syn!(Macro) => { Pat::Macro } // must be before pat_ident |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1954 | | |
| 1955 | syn!(PatLit) => { Pat::Lit } // must be before pat_ident |
| 1956 | | |
| 1957 | syn!(PatIdent) => { Pat::Ident } // must be before pat_path |
| 1958 | | |
| 1959 | syn!(PatPath) => { Pat::Path } |
| 1960 | | |
| 1961 | syn!(PatTuple) => { Pat::Tuple } |
| 1962 | | |
| 1963 | syn!(PatRef) => { Pat::Ref } |
| 1964 | | |
| 1965 | syn!(PatSlice) => { Pat::Slice } |
| 1966 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1967 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1968 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1969 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1970 | impl Synom for PatWild { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1971 | named!(parse -> Self, map!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1972 | punct!(_), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1973 | |u| PatWild { underscore_token: u } |
| 1974 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1975 | } |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 1976 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1977 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1978 | impl Synom for PatBox { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1979 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1980 | boxed: keyword!(box) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1981 | pat: syn!(Pat) >> |
| 1982 | (PatBox { |
| 1983 | pat: Box::new(pat), |
| 1984 | box_token: boxed, |
| 1985 | }) |
| 1986 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 1989 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1990 | impl Synom for PatIdent { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1991 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1992 | mode: option!(keyword!(ref)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1993 | mutability: syn!(Mutability) >> |
| 1994 | name: alt!( |
| 1995 | syn!(Ident) |
| 1996 | | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1997 | keyword!(self) => { Into::into } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1998 | ) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1999 | not!(punct!(<)) >> |
| 2000 | not!(punct!(::)) >> |
| 2001 | subpat: option!(tuple!(punct!(@), syn!(Pat))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2002 | (PatIdent { |
| 2003 | mode: match mode { |
| 2004 | Some(mode) => BindingMode::ByRef(mode, mutability), |
| 2005 | None => BindingMode::ByValue(mutability), |
| 2006 | }, |
| 2007 | ident: name, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2008 | at_token: subpat.as_ref().map(|p| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2009 | subpat: subpat.map(|p| Box::new(p.1)), |
| 2010 | }) |
| 2011 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2014 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2015 | impl Synom for PatTupleStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2016 | named!(parse -> Self, do_parse!( |
| 2017 | path: syn!(Path) >> |
| 2018 | tuple: syn!(PatTuple) >> |
| 2019 | (PatTupleStruct { |
| 2020 | path: path, |
| 2021 | pat: tuple, |
| 2022 | }) |
| 2023 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2026 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2027 | impl Synom for PatStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2028 | named!(parse -> Self, do_parse!( |
| 2029 | path: syn!(Path) >> |
| 2030 | data: braces!(do_parse!( |
| 2031 | fields: call!(Delimited::parse_terminated) >> |
| 2032 | base: option!( |
| 2033 | cond!(fields.is_empty() || fields.trailing_delim(), |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2034 | punct!(..)) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2035 | ) >> |
| 2036 | (fields, base) |
| 2037 | )) >> |
| 2038 | (PatStruct { |
| 2039 | path: path, |
| 2040 | fields: (data.0).0, |
| 2041 | brace_token: data.1, |
| 2042 | dot2_token: (data.0).1.and_then(|m| m), |
| 2043 | }) |
| 2044 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2045 | } |
| 2046 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2047 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2048 | impl Synom for FieldPat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2049 | named!(parse -> Self, alt!( |
| 2050 | do_parse!( |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2051 | member: syn!(Member) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2052 | colon: punct!(:) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2053 | pat: syn!(Pat) >> |
| 2054 | (FieldPat { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2055 | member: member, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2056 | pat: Box::new(pat), |
| 2057 | is_shorthand: false, |
| 2058 | attrs: Vec::new(), |
| 2059 | colon_token: Some(colon), |
| 2060 | }) |
| 2061 | ) |
| 2062 | | |
| 2063 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2064 | boxed: option!(keyword!(box)) >> |
| 2065 | mode: option!(keyword!(ref)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2066 | mutability: syn!(Mutability) >> |
| 2067 | ident: syn!(Ident) >> |
| 2068 | ({ |
| 2069 | let mut pat: Pat = PatIdent { |
| 2070 | mode: if let Some(mode) = mode { |
| 2071 | BindingMode::ByRef(mode, mutability) |
| 2072 | } else { |
| 2073 | BindingMode::ByValue(mutability) |
| 2074 | }, |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 2075 | ident: ident, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2076 | subpat: None, |
| 2077 | at_token: None, |
| 2078 | }.into(); |
| 2079 | if let Some(boxed) = boxed { |
| 2080 | pat = PatBox { |
| 2081 | pat: Box::new(pat), |
| 2082 | box_token: boxed, |
| 2083 | }.into(); |
| 2084 | } |
| 2085 | FieldPat { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2086 | member: Member::Named(ident), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2087 | pat: Box::new(pat), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2088 | is_shorthand: true, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2089 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2090 | colon_token: None, |
| 2091 | } |
| 2092 | }) |
| 2093 | ) |
| 2094 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2097 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2098 | impl Synom for Member { |
| 2099 | named!(parse -> Self, alt!( |
| 2100 | syn!(Ident) => { Member::Named } |
| 2101 | | |
| 2102 | syn!(Index) => { Member::Unnamed } |
| 2103 | )); |
| 2104 | } |
| 2105 | |
| 2106 | #[cfg(feature = "full")] |
| 2107 | impl Synom for Index { |
| 2108 | named!(parse -> Self, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2109 | lit: syn!(Lit) >> |
| 2110 | ({ |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2111 | if let Ok(i) = lit.value.to_string().parse() { |
| 2112 | Index { index: i, span: lit.span } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2113 | } else { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2114 | return parse_error(); |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 2115 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2116 | }) |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2117 | )); |
| 2118 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2119 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2120 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2121 | impl Synom for PatPath { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2122 | named!(parse -> Self, map!( |
| 2123 | syn!(ExprPath), |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 2124 | |p| PatPath { qself: p.qself, path: p.path } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2125 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2126 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 2127 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2128 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2129 | impl Synom for PatTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2130 | named!(parse -> Self, do_parse!( |
| 2131 | data: parens!(do_parse!( |
| 2132 | elems: call!(Delimited::parse_terminated) >> |
| 2133 | dotdot: map!(cond!( |
| 2134 | elems.is_empty() || elems.trailing_delim(), |
| 2135 | option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2136 | dots: punct!(..) >> |
| 2137 | trailing: option!(punct!(,)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2138 | (dots, trailing) |
| 2139 | )) |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 2140 | ), |x| x.and_then(|x| x)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2141 | rest: cond!(match dotdot { |
| 2142 | Some((_, Some(_))) => true, |
| 2143 | _ => false, |
| 2144 | }, |
| 2145 | call!(Delimited::parse_terminated)) >> |
| 2146 | (elems, dotdot, rest) |
| 2147 | )) >> |
| 2148 | ({ |
| 2149 | let ((mut elems, dotdot, rest), parens) = data; |
| 2150 | let (dotdot, trailing) = match dotdot { |
| 2151 | Some((a, b)) => (Some(a), Some(b)), |
| 2152 | None => (None, None), |
| 2153 | }; |
| 2154 | PatTuple { |
| 2155 | paren_token: parens, |
| 2156 | dots_pos: dotdot.as_ref().map(|_| elems.len()), |
| 2157 | dot2_token: dotdot, |
| 2158 | comma_token: trailing.and_then(|b| b), |
| 2159 | pats: { |
| 2160 | if let Some(rest) = rest { |
| 2161 | for elem in rest { |
| 2162 | elems.push(elem); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2163 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2164 | } |
| 2165 | elems |
| 2166 | }, |
| 2167 | } |
| 2168 | }) |
| 2169 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2170 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 2171 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2172 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2173 | impl Synom for PatRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2174 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2175 | and: punct!(&) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2176 | mutability: syn!(Mutability) >> |
| 2177 | pat: syn!(Pat) >> |
| 2178 | (PatRef { |
| 2179 | pat: Box::new(pat), |
| 2180 | mutbl: mutability, |
| 2181 | and_token: and, |
| 2182 | }) |
| 2183 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2184 | } |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 2185 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2186 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2187 | impl Synom for PatLit { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2188 | named!(parse -> Self, do_parse!( |
| 2189 | lit: pat_lit_expr >> |
| 2190 | (if let ExprKind::Path(_) = lit.node { |
| 2191 | return parse_error(); // these need to be parsed by pat_path |
| 2192 | } else { |
| 2193 | PatLit { |
| 2194 | expr: Box::new(lit), |
| 2195 | } |
| 2196 | }) |
| 2197 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2198 | } |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 2199 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2200 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2201 | impl Synom for PatRange { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2202 | named!(parse -> Self, do_parse!( |
| 2203 | lo: pat_lit_expr >> |
| 2204 | limits: syn!(RangeLimits) >> |
| 2205 | hi: pat_lit_expr >> |
| 2206 | (PatRange { |
| 2207 | lo: Box::new(lo), |
| 2208 | hi: Box::new(hi), |
| 2209 | limits: limits, |
| 2210 | }) |
| 2211 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2212 | } |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 2213 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2214 | #[cfg(feature = "full")] |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2215 | named!(pat_lit_expr -> Expr, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2216 | neg: option!(punct!(-)) >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2217 | v: alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2218 | syn!(Lit) => { ExprKind::Lit } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2219 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2220 | syn!(ExprPath) => { ExprKind::Path } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2221 | ) >> |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2222 | (if neg.is_some() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2223 | ExprKind::Unary(ExprUnary { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2224 | op: UnOp::Neg(<Token![-]>::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2225 | expr: Box::new(v.into()) |
| 2226 | }).into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2227 | } else { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2228 | v.into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2229 | }) |
| 2230 | )); |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 2231 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2232 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2233 | impl Synom for PatSlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2234 | named!(parse -> Self, map!( |
| 2235 | brackets!(do_parse!( |
| 2236 | before: call!(Delimited::parse_terminated) >> |
| 2237 | middle: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2238 | dots: punct!(..) >> |
| 2239 | trailing: option!(punct!(,)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2240 | (dots, trailing) |
| 2241 | )) >> |
| 2242 | after: cond!( |
| 2243 | match middle { |
| 2244 | Some((_, ref trailing)) => trailing.is_some(), |
| 2245 | _ => false, |
| 2246 | }, |
| 2247 | call!(Delimited::parse_terminated) |
| 2248 | ) >> |
| 2249 | (before, middle, after) |
| 2250 | )), |
| 2251 | |((before, middle, after), brackets)| { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2252 | let mut before: Delimited<Pat, Token![,]> = before; |
| 2253 | let after: Option<Delimited<Pat, Token![,]>> = after; |
| 2254 | let middle: Option<(Token![..], Option<Token![,]>)> = middle; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2255 | PatSlice { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2256 | dot2_token: middle.as_ref().map(|m| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2257 | comma_token: middle.as_ref().and_then(|m| { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2258 | m.1.as_ref().map(|m| Token) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2259 | }), |
| 2260 | bracket_token: brackets, |
| 2261 | middle: middle.and_then(|_| { |
| 2262 | if !before.is_empty() && !before.trailing_delim() { |
| 2263 | Some(Box::new(before.pop().unwrap().into_item())) |
| 2264 | } else { |
| 2265 | None |
| 2266 | } |
| 2267 | }), |
| 2268 | front: before, |
| 2269 | back: after.unwrap_or_default(), |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 2270 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2271 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2272 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2273 | } |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 2274 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2275 | #[cfg(feature = "full")] |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2276 | impl Synom for CaptureBy { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2277 | named!(parse -> Self, alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2278 | keyword!(move) => { CaptureBy::Value } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2279 | | |
| 2280 | epsilon!() => { |_| CaptureBy::Ref } |
| 2281 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2282 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2285 | #[cfg(feature = "printing")] |
| 2286 | mod printing { |
| 2287 | use super::*; |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2288 | #[cfg(feature = "full")] |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2289 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2290 | use quote::{ToTokens, Tokens}; |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2291 | #[cfg(feature = "full")] |
| 2292 | use proc_macro2::{TokenTree, TokenNode, Literal}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2293 | |
David Tolnay | bcf2602 | 2017-12-25 22:10:52 -0500 | [diff] [blame] | 2294 | // If the given expression is a bare `ExprStruct`, wraps it in parenthesis |
| 2295 | // before appending it to `Tokens`. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2296 | #[cfg(feature = "full")] |
| 2297 | fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) { |
| 2298 | if let ExprKind::Struct(_) = e.node { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 2299 | token::Paren::default().surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2300 | e.to_tokens(tokens); |
| 2301 | }); |
| 2302 | } else { |
| 2303 | e.to_tokens(tokens); |
| 2304 | } |
| 2305 | } |
| 2306 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2307 | impl ToTokens for Expr { |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2308 | #[cfg(feature = "full")] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2309 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2310 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2311 | self.node.to_tokens(tokens) |
| 2312 | } |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2313 | |
| 2314 | #[cfg(not(feature = "full"))] |
| 2315 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2316 | self.node.to_tokens(tokens) |
| 2317 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2320 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2321 | impl ToTokens for ExprBox { |
| 2322 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2323 | self.box_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2324 | self.expr.to_tokens(tokens); |
| 2325 | } |
| 2326 | } |
| 2327 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2328 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2329 | impl ToTokens for ExprInPlace { |
| 2330 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 2331 | match self.kind { |
| 2332 | InPlaceKind::Arrow(ref arrow) => { |
| 2333 | self.place.to_tokens(tokens); |
| 2334 | arrow.to_tokens(tokens); |
| 2335 | self.value.to_tokens(tokens); |
| 2336 | } |
| 2337 | InPlaceKind::In(ref _in) => { |
| 2338 | _in.to_tokens(tokens); |
| 2339 | self.place.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2340 | // NOTE: The second operand must be in a block, add one if |
| 2341 | // it is not present. |
| 2342 | if let ExprKind::Block(_) = self.value.node { |
| 2343 | self.value.to_tokens(tokens); |
| 2344 | } else { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 2345 | token::Brace::default().surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2346 | self.value.to_tokens(tokens); |
| 2347 | }) |
| 2348 | } |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 2349 | } |
| 2350 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2354 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2355 | impl ToTokens for ExprArray { |
| 2356 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2357 | self.bracket_token.surround(tokens, |tokens| { |
| 2358 | self.exprs.to_tokens(tokens); |
| 2359 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | impl ToTokens for ExprCall { |
| 2364 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2365 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2366 | self.paren_token.surround(tokens, |tokens| { |
| 2367 | self.args.to_tokens(tokens); |
| 2368 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2372 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2373 | impl ToTokens for ExprMethodCall { |
| 2374 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2375 | self.expr.to_tokens(tokens); |
| 2376 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2377 | self.method.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2378 | if !self.typarams.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2379 | TokensOrDefault(&self.colon2_token).to_tokens(tokens); |
| 2380 | TokensOrDefault(&self.lt_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2381 | self.typarams.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2382 | TokensOrDefault(&self.gt_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2383 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2384 | self.paren_token.surround(tokens, |tokens| { |
| 2385 | self.args.to_tokens(tokens); |
| 2386 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2390 | #[cfg(feature = "full")] |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 2391 | impl ToTokens for ExprTuple { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2392 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2393 | self.paren_token.surround(tokens, |tokens| { |
| 2394 | self.args.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2395 | // If we only have one argument, we need a trailing comma to |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 2396 | // distinguish ExprTuple from ExprParen. |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2397 | if self.args.len() == 1 && !self.args.trailing_delim() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2398 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2399 | } |
| 2400 | // XXX: Not sure how to handle this, but we never parse it yet. |
| 2401 | // Is this for an expression like (0,)? Can't we use the |
| 2402 | // trailing delimiter on Delimited for that? (,) isn't a valid |
| 2403 | // expression as far as I know. |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2404 | self.lone_comma.to_tokens(tokens); |
| 2405 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | impl ToTokens for ExprBinary { |
| 2410 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2411 | self.left.to_tokens(tokens); |
| 2412 | self.op.to_tokens(tokens); |
| 2413 | self.right.to_tokens(tokens); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | impl ToTokens for ExprUnary { |
| 2418 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2419 | self.op.to_tokens(tokens); |
| 2420 | self.expr.to_tokens(tokens); |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | impl ToTokens for ExprCast { |
| 2425 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2426 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2427 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2428 | self.ty.to_tokens(tokens); |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | impl ToTokens for ExprType { |
| 2433 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2434 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2435 | self.colon_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2436 | self.ty.to_tokens(tokens); |
| 2437 | } |
| 2438 | } |
| 2439 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2440 | #[cfg(feature = "full")] |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2441 | fn maybe_wrap_else( |
| 2442 | tokens: &mut Tokens, |
| 2443 | else_token: &Option<Token![else]>, |
| 2444 | if_false: &Option<Box<Expr>>, |
| 2445 | ) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2446 | if let Some(ref if_false) = *if_false { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 2447 | TokensOrDefault(else_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2448 | |
| 2449 | // If we are not one of the valid expressions to exist in an else |
| 2450 | // clause, wrap ourselves in a block. |
| 2451 | match if_false.node { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2452 | ExprKind::If(_) | ExprKind::IfLet(_) | ExprKind::Block(_) => { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2453 | if_false.to_tokens(tokens); |
| 2454 | } |
| 2455 | _ => { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 2456 | token::Brace::default().surround(tokens, |tokens| { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2457 | if_false.to_tokens(tokens); |
| 2458 | }); |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2465 | impl ToTokens for ExprIf { |
| 2466 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2467 | self.if_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2468 | wrap_bare_struct(tokens, &self.cond); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2469 | self.if_true.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2470 | maybe_wrap_else(tokens, &self.else_token, &self.if_false); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2471 | } |
| 2472 | } |
| 2473 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2474 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2475 | impl ToTokens for ExprIfLet { |
| 2476 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2477 | self.if_token.to_tokens(tokens); |
| 2478 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2479 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2480 | self.eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2481 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2482 | self.if_true.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2483 | maybe_wrap_else(tokens, &self.else_token, &self.if_false); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2487 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2488 | impl ToTokens for ExprWhile { |
| 2489 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2490 | if self.label.is_some() { |
| 2491 | self.label.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2492 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2493 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2494 | self.while_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2495 | wrap_bare_struct(tokens, &self.cond); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2496 | self.body.to_tokens(tokens); |
| 2497 | } |
| 2498 | } |
| 2499 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2500 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2501 | impl ToTokens for ExprWhileLet { |
| 2502 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2503 | if self.label.is_some() { |
| 2504 | self.label.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2505 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2506 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2507 | self.while_token.to_tokens(tokens); |
| 2508 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2509 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2510 | self.eq_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2511 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2512 | self.body.to_tokens(tokens); |
| 2513 | } |
| 2514 | } |
| 2515 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2516 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2517 | impl ToTokens for ExprForLoop { |
| 2518 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2519 | if self.label.is_some() { |
| 2520 | self.label.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2521 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2522 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2523 | self.for_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2524 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2525 | self.in_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2526 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2527 | self.body.to_tokens(tokens); |
| 2528 | } |
| 2529 | } |
| 2530 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2531 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2532 | impl ToTokens for ExprLoop { |
| 2533 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2534 | if self.label.is_some() { |
| 2535 | self.label.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2536 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2537 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2538 | self.loop_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2539 | self.body.to_tokens(tokens); |
| 2540 | } |
| 2541 | } |
| 2542 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2543 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2544 | impl ToTokens for ExprMatch { |
| 2545 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2546 | self.match_token.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2547 | wrap_bare_struct(tokens, &self.expr); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2548 | self.brace_token.surround(tokens, |tokens| { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2549 | for (i, arm) in self.arms.iter().enumerate() { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2550 | arm.to_tokens(tokens); |
| 2551 | // Ensure that we have a comma after a non-block arm, except |
| 2552 | // for the last one. |
| 2553 | let is_last = i == self.arms.len() - 1; |
Alex Crichton | 03b3027 | 2017-08-28 09:35:24 -0700 | [diff] [blame] | 2554 | if !is_last && arm_expr_requires_comma(&arm.body) && arm.comma.is_none() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2555 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2556 | } |
| 2557 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2558 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2562 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2563 | impl ToTokens for ExprCatch { |
| 2564 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2565 | self.do_token.to_tokens(tokens); |
| 2566 | self.catch_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2567 | self.block.to_tokens(tokens); |
| 2568 | } |
| 2569 | } |
| 2570 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2571 | #[cfg(feature = "full")] |
Alex Crichton | fe11046 | 2017-06-01 12:49:27 -0700 | [diff] [blame] | 2572 | impl ToTokens for ExprYield { |
| 2573 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2574 | self.yield_token.to_tokens(tokens); |
| 2575 | self.expr.to_tokens(tokens); |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2580 | impl ToTokens for ExprClosure { |
| 2581 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2582 | self.capture.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2583 | self.or1_token.to_tokens(tokens); |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 2584 | for item in self.inputs.iter() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2585 | match **item.item() { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2586 | FnArg::Captured(ArgCaptured { |
| 2587 | ref pat, |
| 2588 | ty: Type::Infer(_), |
| 2589 | .. |
| 2590 | }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2591 | pat.to_tokens(tokens); |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 2592 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2593 | _ => item.item().to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 2594 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2595 | item.delimiter().to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2596 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2597 | self.or2_token.to_tokens(tokens); |
David Tolnay | 7f67574 | 2017-12-27 22:43:21 -0500 | [diff] [blame^] | 2598 | self.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2599 | self.body.to_tokens(tokens); |
| 2600 | } |
| 2601 | } |
| 2602 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2603 | #[cfg(feature = "full")] |
Nika Layzell | 640832a | 2017-12-04 13:37:09 -0500 | [diff] [blame] | 2604 | impl ToTokens for ExprUnsafe { |
| 2605 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2606 | self.unsafe_token.to_tokens(tokens); |
| 2607 | self.block.to_tokens(tokens); |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2612 | impl ToTokens for ExprBlock { |
| 2613 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2614 | self.block.to_tokens(tokens); |
| 2615 | } |
| 2616 | } |
| 2617 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2618 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2619 | impl ToTokens for ExprAssign { |
| 2620 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2621 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2622 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2623 | self.right.to_tokens(tokens); |
| 2624 | } |
| 2625 | } |
| 2626 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2627 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2628 | impl ToTokens for ExprAssignOp { |
| 2629 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2630 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2631 | self.op.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2632 | self.right.to_tokens(tokens); |
| 2633 | } |
| 2634 | } |
| 2635 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2636 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2637 | impl ToTokens for ExprField { |
| 2638 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2639 | self.base.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2640 | self.dot_token.to_tokens(tokens); |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2641 | self.member.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2642 | } |
| 2643 | } |
| 2644 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2645 | #[cfg(feature = "full")] |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2646 | impl ToTokens for Member { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2647 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2648 | match *self { |
| 2649 | Member::Named(ident) => ident.to_tokens(tokens), |
| 2650 | Member::Unnamed(ref index) => index.to_tokens(tokens), |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | #[cfg(feature = "full")] |
| 2656 | impl ToTokens for Index { |
| 2657 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2658 | tokens.append(TokenTree { |
| 2659 | span: self.span, |
David Tolnay | 9bce057 | 2017-12-27 22:24:09 -0500 | [diff] [blame] | 2660 | kind: TokenNode::Literal(Literal::integer(i64::from(self.index))), |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2661 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | impl ToTokens for ExprIndex { |
| 2666 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2667 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2668 | self.bracket_token.surround(tokens, |tokens| { |
| 2669 | self.index.to_tokens(tokens); |
| 2670 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2674 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2675 | impl ToTokens for ExprRange { |
| 2676 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2677 | self.from.to_tokens(tokens); |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 2678 | match self.limits { |
| 2679 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 2680 | RangeLimits::Closed(ref t) => t.to_tokens(tokens), |
| 2681 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2682 | self.to.to_tokens(tokens); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | impl ToTokens for ExprPath { |
| 2687 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2688 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2692 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2693 | impl ToTokens for ExprAddrOf { |
| 2694 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2695 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2696 | self.mutbl.to_tokens(tokens); |
| 2697 | self.expr.to_tokens(tokens); |
| 2698 | } |
| 2699 | } |
| 2700 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2701 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2702 | impl ToTokens for ExprBreak { |
| 2703 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2704 | self.break_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2705 | self.label.to_tokens(tokens); |
| 2706 | self.expr.to_tokens(tokens); |
| 2707 | } |
| 2708 | } |
| 2709 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2710 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2711 | impl ToTokens for ExprContinue { |
| 2712 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2713 | self.continue_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2714 | self.label.to_tokens(tokens); |
| 2715 | } |
| 2716 | } |
| 2717 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2718 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2719 | impl ToTokens for ExprRet { |
| 2720 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2721 | self.return_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2722 | self.expr.to_tokens(tokens); |
| 2723 | } |
| 2724 | } |
| 2725 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2726 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2727 | impl ToTokens for ExprStruct { |
| 2728 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2729 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2730 | self.brace_token.surround(tokens, |tokens| { |
| 2731 | self.fields.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2732 | if self.rest.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2733 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2734 | self.rest.to_tokens(tokens); |
| 2735 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2736 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2740 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2741 | impl ToTokens for ExprRepeat { |
| 2742 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2743 | self.bracket_token.surround(tokens, |tokens| { |
| 2744 | self.expr.to_tokens(tokens); |
| 2745 | self.semi_token.to_tokens(tokens); |
| 2746 | self.amt.to_tokens(tokens); |
| 2747 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 2751 | impl ToTokens for ExprGroup { |
| 2752 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2753 | self.group_token.surround(tokens, |tokens| { |
| 2754 | self.expr.to_tokens(tokens); |
| 2755 | }); |
| 2756 | } |
| 2757 | } |
| 2758 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2759 | impl ToTokens for ExprParen { |
| 2760 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2761 | self.paren_token.surround(tokens, |tokens| { |
| 2762 | self.expr.to_tokens(tokens); |
| 2763 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2767 | #[cfg(feature = "full")] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2768 | impl ToTokens for ExprTry { |
| 2769 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2770 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2771 | self.question_token.to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2772 | } |
| 2773 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2774 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2775 | #[cfg(feature = "full")] |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2776 | impl ToTokens for FieldValue { |
| 2777 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2778 | self.member.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2779 | // XXX: Override self.is_shorthand if expr is not an IdentExpr with |
| 2780 | // the ident self.ident? |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 2781 | if !self.is_shorthand { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2782 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 2783 | self.expr.to_tokens(tokens); |
| 2784 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2788 | #[cfg(feature = "full")] |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2789 | impl ToTokens for Arm { |
| 2790 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2791 | tokens.append_all(&self.attrs); |
| 2792 | self.pats.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2793 | if self.guard.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2794 | TokensOrDefault(&self.if_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2795 | self.guard.to_tokens(tokens); |
| 2796 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2797 | self.rocket_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2798 | self.body.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2799 | self.comma.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2803 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2804 | impl ToTokens for PatWild { |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2805 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2806 | self.underscore_token.to_tokens(tokens); |
| 2807 | } |
| 2808 | } |
| 2809 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2810 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2811 | impl ToTokens for PatIdent { |
| 2812 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2813 | self.mode.to_tokens(tokens); |
| 2814 | self.ident.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2815 | if self.subpat.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2816 | TokensOrDefault(&self.at_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2817 | self.subpat.to_tokens(tokens); |
| 2818 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2822 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2823 | impl ToTokens for PatStruct { |
| 2824 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2825 | self.path.to_tokens(tokens); |
| 2826 | self.brace_token.surround(tokens, |tokens| { |
| 2827 | self.fields.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2828 | // NOTE: We need a comma before the dot2 token if it is present. |
| 2829 | if !self.fields.empty_or_trailing() && self.dot2_token.is_some() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2830 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2831 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2832 | self.dot2_token.to_tokens(tokens); |
| 2833 | }); |
| 2834 | } |
| 2835 | } |
| 2836 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2837 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2838 | impl ToTokens for PatTupleStruct { |
| 2839 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2840 | self.path.to_tokens(tokens); |
| 2841 | self.pat.to_tokens(tokens); |
| 2842 | } |
| 2843 | } |
| 2844 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2845 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2846 | impl ToTokens for PatPath { |
| 2847 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2848 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 2849 | } |
| 2850 | } |
| 2851 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2852 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2853 | impl ToTokens for PatTuple { |
| 2854 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2855 | self.paren_token.surround(tokens, |tokens| { |
| 2856 | for (i, token) in self.pats.iter().enumerate() { |
| 2857 | if Some(i) == self.dots_pos { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2858 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
| 2859 | TokensOrDefault(&self.comma_token).to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2860 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2861 | token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2862 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2863 | |
| 2864 | if Some(self.pats.len()) == self.dots_pos { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2865 | // Ensure there is a comma before the .. token. |
| 2866 | if !self.pats.empty_or_trailing() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2867 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2868 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2869 | self.dot2_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2870 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2871 | }); |
| 2872 | } |
| 2873 | } |
| 2874 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2875 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2876 | impl ToTokens for PatBox { |
| 2877 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2878 | self.box_token.to_tokens(tokens); |
| 2879 | self.pat.to_tokens(tokens); |
| 2880 | } |
| 2881 | } |
| 2882 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2883 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2884 | impl ToTokens for PatRef { |
| 2885 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2886 | self.and_token.to_tokens(tokens); |
| 2887 | self.mutbl.to_tokens(tokens); |
| 2888 | self.pat.to_tokens(tokens); |
| 2889 | } |
| 2890 | } |
| 2891 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2892 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2893 | impl ToTokens for PatLit { |
| 2894 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2895 | self.expr.to_tokens(tokens); |
| 2896 | } |
| 2897 | } |
| 2898 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2899 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2900 | impl ToTokens for PatRange { |
| 2901 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2902 | self.lo.to_tokens(tokens); |
David Tolnay | 475288a | 2017-12-19 22:59:44 -0800 | [diff] [blame] | 2903 | match self.limits { |
| 2904 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 2905 | RangeLimits::Closed(ref t) => Token.to_tokens(tokens), |
| 2906 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2907 | self.hi.to_tokens(tokens); |
| 2908 | } |
| 2909 | } |
| 2910 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2911 | #[cfg(feature = "full")] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2912 | impl ToTokens for PatSlice { |
| 2913 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2914 | // XXX: This is a mess, and it will be so easy to screw it up. How |
| 2915 | // do we make this correct itself better? |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2916 | self.bracket_token.surround(tokens, |tokens| { |
| 2917 | self.front.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2918 | |
| 2919 | // If we need a comma before the middle or standalone .. token, |
| 2920 | // then make sure it's present. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 2921 | if !self.front.empty_or_trailing() |
| 2922 | && (self.middle.is_some() || self.dot2_token.is_some()) |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2923 | { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 2924 | <Token![,]>::default().to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | // If we have an identifier, we always need a .. token. |
| 2928 | if self.middle.is_some() { |
| 2929 | self.middle.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2930 | TokensOrDefault(&self.dot2_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2931 | } else if self.dot2_token.is_some() { |
| 2932 | self.dot2_token.to_tokens(tokens); |
| 2933 | } |
| 2934 | |
| 2935 | // Make sure we have a comma before the back half. |
| 2936 | if !self.back.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2937 | TokensOrDefault(&self.comma_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2938 | self.back.to_tokens(tokens); |
| 2939 | } else { |
| 2940 | self.comma_token.to_tokens(tokens); |
| 2941 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2942 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2946 | #[cfg(feature = "full")] |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2947 | impl ToTokens for FieldPat { |
| 2948 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 2949 | // XXX: Override is_shorthand if it was wrong? |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2950 | if !self.is_shorthand { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 2951 | self.member.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 2952 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2953 | } |
| 2954 | self.pat.to_tokens(tokens); |
| 2955 | } |
| 2956 | } |
| 2957 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2958 | #[cfg(feature = "full")] |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2959 | impl ToTokens for BindingMode { |
| 2960 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2961 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2962 | BindingMode::ByRef(ref t, ref m) => { |
| 2963 | t.to_tokens(tokens); |
| 2964 | m.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2965 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2966 | BindingMode::ByValue(ref m) => { |
| 2967 | m.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | } |
| 2971 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2972 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2973 | #[cfg(feature = "full")] |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2974 | impl ToTokens for CaptureBy { |
| 2975 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2976 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2977 | CaptureBy::Value(ref t) => t.to_tokens(tokens), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 2978 | CaptureBy::Ref => { |
| 2979 | // nothing |
| 2980 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | } |
| 2984 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2985 | #[cfg(feature = "full")] |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2986 | impl ToTokens for Block { |
| 2987 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2988 | self.brace_token.surround(tokens, |tokens| { |
| 2989 | tokens.append_all(&self.stmts); |
| 2990 | }); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
| 2993 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 2994 | #[cfg(feature = "full")] |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2995 | impl ToTokens for Stmt { |
| 2996 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2997 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2998 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2999 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 3000 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3001 | Stmt::Semi(ref expr, ref semi) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3002 | expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3003 | semi.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3004 | } |
David Tolnay | decf28d | 2017-11-11 11:56:45 -0800 | [diff] [blame] | 3005 | Stmt::Macro(ref mac) => { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 3006 | let (ref mac, ref style, ref attrs) = **mac; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 3007 | tokens.append_all(attrs.outer()); |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 3008 | mac.to_tokens(tokens); |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 3009 | match *style { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3010 | MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 3011 | MacStmtStyle::Braces | MacStmtStyle::NoBraces => { |
| 3012 | // no semicolon |
| 3013 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 3014 | } |
| 3015 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 3016 | } |
| 3017 | } |
| 3018 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3019 | |
Michael Layzell | 734adb4 | 2017-06-07 16:58:31 -0400 | [diff] [blame] | 3020 | #[cfg(feature = "full")] |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3021 | impl ToTokens for Local { |
| 3022 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4e3158d | 2016-10-30 00:30:01 -0700 | [diff] [blame] | 3023 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3024 | self.let_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3025 | self.pat.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3026 | if self.ty.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3027 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3028 | self.ty.to_tokens(tokens); |
| 3029 | } |
| 3030 | if self.init.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 3031 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 3032 | self.init.to_tokens(tokens); |
| 3033 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 3034 | self.semi_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 3035 | } |
| 3036 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 3037 | } |