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