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