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 | ccbb45d | 2017-05-23 10:58:24 -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 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 32 | /// E.g. 'place <- val'. |
| 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, |
| 142 | pub label: Option<Ident>, |
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, |
| 156 | pub label: Option<Ident>, |
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, |
| 172 | pub label: Option<Ident>, |
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, |
| 183 | pub label: Option<Ident>, |
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 { |
| 276 | pub label: Option<Ident>, |
| 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 { |
| 283 | pub label: Option<Ident>, |
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>, |
| 488 | pub box_token: tokens::Box, |
| 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 | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 590 | use {BinOp, FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Mac, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 591 | TokenTree, Ty, UnOp, Unsafety, ArgCaptured, TyInfer}; |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 592 | use attr::parsing::outer_attr; |
Gregory Katz | 1b69f68 | 2016-09-27 21:06:09 -0400 | [diff] [blame] | 593 | use generics::parsing::lifetime; |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 594 | use ident::parsing::{ident, wordlike}; |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 595 | use item::parsing::item; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 596 | use lit::parsing::lit; |
| 597 | use mac::parsing::{mac, token_stream}; |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 598 | use synom::IResult::{self, Error}; |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 599 | use op::parsing::{assign_op, binop, unop}; |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 600 | use ty::parsing::{mutability, path, qpath, ty, unsafety}; |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 601 | use synom::{self, IResult}; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 602 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 603 | use proc_macro2::{self, TokenKind, Delimiter}; |
| 604 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 605 | // Struct literals are ambiguous in certain positions |
| 606 | // https://github.com/rust-lang/rfcs/pull/92 |
| 607 | macro_rules! named_ambiguous_expr { |
| 608 | ($name:ident -> $o:ty, $allow_struct:ident, $submac:ident!( $($args:tt)* )) => { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 609 | fn $name(i: &[$crate::synom::TokenTree], $allow_struct: bool) |
| 610 | -> $crate::synom::IResult<&[$crate::synom::TokenTree], $o> { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 611 | $submac!(i, $($args)*) |
| 612 | } |
| 613 | }; |
| 614 | } |
| 615 | |
| 616 | macro_rules! ambiguous_expr { |
| 617 | ($i:expr, $allow_struct:ident) => { |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 618 | ambiguous_expr($i, $allow_struct, true) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 619 | }; |
| 620 | } |
| 621 | |
| 622 | named!(pub expr -> Expr, ambiguous_expr!(true)); |
| 623 | |
| 624 | named!(expr_no_struct -> Expr, ambiguous_expr!(false)); |
| 625 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 626 | #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 627 | fn ambiguous_expr(i: &[synom::TokenTree], |
| 628 | allow_struct: bool, |
| 629 | allow_block: bool) |
| 630 | -> IResult<&[synom::TokenTree], Expr> { |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 631 | do_parse!( |
| 632 | i, |
| 633 | mut e: alt!( |
| 634 | expr_lit // must be before expr_struct |
| 635 | | |
| 636 | cond_reduce!(allow_struct, expr_struct) // must be before expr_path |
| 637 | | |
| 638 | expr_paren // must be before expr_tup |
| 639 | | |
| 640 | expr_mac // must be before expr_path |
| 641 | | |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 642 | call!(expr_break, allow_struct) // must be before expr_path |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 643 | | |
| 644 | expr_continue // must be before expr_path |
| 645 | | |
| 646 | call!(expr_ret, allow_struct) // must be before expr_path |
| 647 | | |
| 648 | call!(expr_box, allow_struct) |
| 649 | | |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 650 | expr_in_place |
| 651 | | |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 652 | expr_array |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 653 | | |
| 654 | expr_tup |
| 655 | | |
| 656 | call!(expr_unary, allow_struct) |
| 657 | | |
| 658 | expr_if |
| 659 | | |
| 660 | expr_while |
| 661 | | |
| 662 | expr_for_loop |
| 663 | | |
| 664 | expr_loop |
| 665 | | |
| 666 | expr_match |
| 667 | | |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 668 | expr_catch |
| 669 | | |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 670 | call!(expr_closure, allow_struct) |
| 671 | | |
| 672 | cond_reduce!(allow_block, expr_block) |
| 673 | | |
| 674 | call!(expr_range, allow_struct) |
| 675 | | |
| 676 | expr_path |
| 677 | | |
| 678 | call!(expr_addr_of, allow_struct) |
| 679 | | |
| 680 | expr_repeat |
| 681 | ) >> |
| 682 | many0!(alt!( |
| 683 | tap!(args: and_call => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 684 | let (args, paren) = args; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 685 | e = ExprCall { |
| 686 | func: Box::new(e.into()), |
| 687 | args: args, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 688 | paren_token: paren, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 689 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 690 | }) |
| 691 | | |
| 692 | tap!(more: and_method_call => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 693 | let mut call = more; |
| 694 | call.expr = Box::new(e.into()); |
| 695 | e = call.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 696 | }) |
| 697 | | |
| 698 | tap!(more: call!(and_binary, allow_struct) => { |
| 699 | let (op, other) = more; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 700 | e = ExprBinary { |
| 701 | op: op, |
| 702 | left: Box::new(e.into()), |
| 703 | right: Box::new(other), |
| 704 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 705 | }) |
| 706 | | |
| 707 | tap!(ty: and_cast => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 708 | let (ty, token) = ty; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 709 | e = ExprCast { |
| 710 | expr: Box::new(e.into()), |
| 711 | ty: Box::new(ty), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 712 | as_token: token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 713 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 714 | }) |
| 715 | | |
| 716 | tap!(ty: and_ascription => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 717 | let (ty, token) = ty; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 718 | e = ExprType { |
| 719 | expr: Box::new(e.into()), |
| 720 | ty: Box::new(ty), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 721 | colon_token: token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 722 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 723 | }) |
| 724 | | |
| 725 | tap!(v: call!(and_assign, allow_struct) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 726 | let (v, token) = v; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 727 | e = ExprAssign { |
| 728 | left: Box::new(e.into()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 729 | eq_token: token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 730 | right: Box::new(v), |
| 731 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 732 | }) |
| 733 | | |
| 734 | tap!(more: call!(and_assign_op, allow_struct) => { |
| 735 | let (op, v) = more; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 736 | e = ExprAssignOp { |
| 737 | op: op, |
| 738 | left: Box::new(e.into()), |
| 739 | right: Box::new(v), |
| 740 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 741 | }) |
| 742 | | |
| 743 | tap!(field: and_field => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 744 | let (field, token) = field; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 745 | e = ExprField { |
| 746 | expr: Box::new(e.into()), |
| 747 | field: field, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 748 | dot_token: token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 749 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 750 | }) |
| 751 | | |
| 752 | tap!(field: and_tup_field => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 753 | let (field, token) = field; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 754 | e = ExprTupField { |
| 755 | expr: Box::new(e.into()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 756 | field: field, |
| 757 | dot_token: token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 758 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 759 | }) |
| 760 | | |
| 761 | tap!(i: and_index => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 762 | let (i, token) = i; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 763 | e = ExprIndex { |
| 764 | expr: Box::new(e.into()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 765 | bracket_token: token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 766 | index: Box::new(i), |
| 767 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 768 | }) |
| 769 | | |
| 770 | tap!(more: call!(and_range, allow_struct) => { |
| 771 | let (limits, hi) = more; |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 772 | e = ExprRange { |
| 773 | from: Some(Box::new(e.into())), |
| 774 | to: hi.map(Box::new), |
| 775 | limits: limits, |
| 776 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 777 | }) |
| 778 | | |
| 779 | tap!(_try: punct!("?") => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 780 | e = ExprTry { |
| 781 | expr: Box::new(e.into()), |
| 782 | question_token: tokens::Question::default(), |
| 783 | }.into(); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 784 | }) |
| 785 | )) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 786 | (e.into()) |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 787 | ) |
| 788 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 789 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 790 | named!(expr_mac -> ExprKind, map!(mac, ExprKind::Mac)); |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 791 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 792 | named!(expr_paren -> ExprKind, do_parse!( |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 793 | punct!("(") >> |
| 794 | e: expr >> |
| 795 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 796 | (ExprParen { |
| 797 | expr: Box::new(e), |
| 798 | paren_token: tokens::Paren::default(), |
| 799 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 800 | )); |
| 801 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 802 | named_ambiguous_expr!(expr_box -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 803 | keyword!("box") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 804 | inner: ambiguous_expr!(allow_struct) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 805 | (ExprBox { |
| 806 | expr: Box::new(inner), |
| 807 | box_token: tokens::Box::default(), |
| 808 | }.into()) |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 809 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 810 | |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 811 | named!(expr_in_place -> ExprKind, do_parse!( |
| 812 | keyword!("in") >> |
| 813 | place: expr_no_struct >> |
| 814 | punct!("{") >> |
| 815 | value: within_block >> |
| 816 | punct!("}") >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 817 | (ExprInPlace { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 818 | in_token: tokens::In::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 819 | place: Box::new(place), |
| 820 | value: Box::new(Expr { |
| 821 | node: ExprBlock { |
| 822 | unsafety: Unsafety::Normal, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 823 | block: Block { |
| 824 | stmts: value, |
| 825 | brace_token: tokens::Brace::default(), |
| 826 | }, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 827 | }.into(), |
| 828 | attrs: Vec::new(), |
| 829 | }), |
| 830 | }.into()) |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 831 | )); |
| 832 | |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 833 | named!(expr_array -> ExprKind, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 834 | punct!("[") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 835 | elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 836 | expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 837 | punct!("]") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 838 | (ExprArray { |
| 839 | exprs: elems, |
| 840 | bracket_token: tokens::Bracket::default(), |
| 841 | }.into()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 842 | )); |
| 843 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 844 | named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren), do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 845 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 846 | args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 847 | expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 848 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 849 | (args, tokens::Paren::default()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 850 | )); |
| 851 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 852 | named!(and_method_call -> ExprMethodCall, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 853 | punct!(".") >> |
| 854 | method: ident >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 855 | typarams: option!(do_parse!( |
| 856 | punct!("::") >> |
| 857 | punct!("<") >> |
| 858 | tys: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 859 | ty) >> |
| 860 | punct!(">") >> |
| 861 | (tys) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 862 | )) >> |
| 863 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 864 | args: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 865 | expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 866 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 867 | (ExprMethodCall { |
| 868 | // this expr will get overwritten after being returned |
| 869 | expr: Box::new(ExprKind::Lit(Lit { |
| 870 | span: Span::default(), |
| 871 | value: LitKind::Bool(false), |
| 872 | }).into()), |
| 873 | |
| 874 | method: method, |
| 875 | args: args, |
| 876 | paren_token: tokens::Paren::default(), |
| 877 | dot_token: tokens::Dot::default(), |
| 878 | lt_token: typarams.as_ref().map(|_| tokens::Lt::default()), |
| 879 | gt_token: typarams.as_ref().map(|_| tokens::Gt::default()), |
| 880 | colon2_token: typarams.as_ref().map(|_| tokens::Colon2::default()), |
| 881 | typarams: typarams.unwrap_or_default(), |
| 882 | }) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 883 | )); |
| 884 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 885 | named!(expr_tup -> ExprKind, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 886 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 887 | elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 888 | expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 889 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 890 | (ExprTup { |
| 891 | args: elems, |
| 892 | paren_token: tokens::Paren::default(), |
| 893 | lone_comma: None, // TODO: parse this |
| 894 | }.into()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 895 | )); |
| 896 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 897 | named_ambiguous_expr!(and_binary -> (BinOp, Expr), allow_struct, tuple!( |
| 898 | binop, |
| 899 | ambiguous_expr!(allow_struct) |
| 900 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 901 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 902 | named_ambiguous_expr!(expr_unary -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 903 | operator: unop >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 904 | operand: ambiguous_expr!(allow_struct) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 905 | (ExprUnary { op: operator, expr: Box::new(operand) }.into()) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 906 | )); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 907 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 908 | named!(expr_lit -> ExprKind, map!(lit, ExprKind::Lit)); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 909 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 910 | named!(and_cast -> (Ty, tokens::As), do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 911 | keyword!("as") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 912 | ty: ty >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 913 | (ty, tokens::As::default()) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 914 | )); |
| 915 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 916 | named!(and_ascription -> (Ty, tokens::Colon), |
| 917 | preceded!(punct!(":"), map!(ty, |t| (t, tokens::Colon::default())))); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 918 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 919 | enum Cond { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 920 | Let(Pat, Expr, tokens::Eq, tokens::Let), |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 921 | Expr(Expr), |
| 922 | } |
| 923 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 924 | named!(cond -> Cond, alt!( |
| 925 | do_parse!( |
| 926 | keyword!("let") >> |
| 927 | pat: pat >> |
| 928 | punct!("=") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 929 | value: expr_no_struct >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 930 | (Cond::Let(pat, value, tokens::Eq::default(), tokens::Let::default())) |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 931 | ) |
| 932 | | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 933 | map!(expr_no_struct, Cond::Expr) |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 934 | )); |
| 935 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 936 | named!(expr_if -> ExprKind, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 937 | keyword!("if") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 938 | cond: cond >> |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 939 | then_block: delim!(Brace, within_block) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 940 | else_block: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 941 | keyword!("else"), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 942 | alt!( |
| 943 | expr_if |
| 944 | | |
| 945 | do_parse!( |
| 946 | punct!("{") >> |
| 947 | else_block: within_block >> |
| 948 | punct!("}") >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 949 | (ExprKind::Block(ExprBlock { |
| 950 | unsafety: Unsafety::Normal, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 951 | block: Block { |
| 952 | stmts: else_block, |
| 953 | brace_token: tokens::Brace::default(), |
| 954 | }, |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 955 | }).into()) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 956 | ) |
| 957 | ) |
| 958 | )) >> |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 959 | (match cond { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 960 | Cond::Let(pat, expr, eq_token, let_token) => ExprIfLet { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 961 | pat: Box::new(pat), |
| 962 | expr: Box::new(expr), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 963 | eq_token: eq_token, |
| 964 | let_token: let_token, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 965 | if_true: Block { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 966 | stmts: then_block, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 967 | brace_token: tokens::Brace::default(), |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 968 | }, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 969 | if_token: tokens::If::default(), |
| 970 | else_token: else_block.as_ref().map(|_| tokens::Else::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 971 | if_false: else_block.map(|els| Box::new(els.into())), |
| 972 | }.into(), |
| 973 | Cond::Expr(cond) => ExprIf { |
| 974 | cond: Box::new(cond), |
| 975 | if_true: Block { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 976 | stmts: then_block, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 977 | brace_token: tokens::Brace::default(), |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 978 | }, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 979 | if_token: tokens::If::default(), |
| 980 | else_token: else_block.as_ref().map(|_| tokens::Else::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 981 | if_false: else_block.map(|els| Box::new(els.into())), |
| 982 | }.into(), |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 983 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 984 | )); |
| 985 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 986 | named!(expr_for_loop -> ExprKind, do_parse!( |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 987 | lbl: option!(terminated!(label, punct!(":"))) >> |
| 988 | keyword!("for") >> |
| 989 | pat: pat >> |
| 990 | keyword!("in") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 991 | expr: expr_no_struct >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 992 | loop_block: block >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 993 | (ExprForLoop { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 994 | for_token: tokens::For::default(), |
| 995 | in_token: tokens::In::default(), |
| 996 | colon_token: lbl.as_ref().map(|_| tokens::Colon::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 997 | pat: Box::new(pat), |
| 998 | expr: Box::new(expr), |
| 999 | body: loop_block, |
| 1000 | label: lbl, |
| 1001 | }.into()) |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1002 | )); |
| 1003 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1004 | named!(expr_loop -> ExprKind, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1005 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 1006 | keyword!("loop") >> |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 1007 | loop_block: block >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1008 | (ExprLoop { |
| 1009 | loop_token: tokens::Loop::default(), |
| 1010 | colon_token: lbl.as_ref().map(|_| tokens::Colon::default()), |
| 1011 | body: loop_block, |
| 1012 | label: lbl, |
| 1013 | }.into()) |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 1014 | )); |
| 1015 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1016 | named!(expr_match -> ExprKind, do_parse!( |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1017 | keyword!("match") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1018 | obj: expr_no_struct >> |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1019 | res: delim!(Brace, do_parse!( |
| 1020 | mut arms: many0!(do_parse!( |
| 1021 | arm: match_arm >> |
| 1022 | cond!(arm_requires_comma(&arm), punct!(",")) >> |
| 1023 | cond!(!arm_requires_comma(&arm), option!(punct!(","))) >> |
| 1024 | (arm) |
| 1025 | )) >> |
| 1026 | last_arm: option!(match_arm) >> |
| 1027 | (ExprKind::Match(Box::new(obj), { |
| 1028 | arms.extend(last_arm); |
| 1029 | arms |
| 1030 | })) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1031 | )) >> |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1032 | last_arm: option!(match_arm) >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1033 | punct!("}") >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1034 | (ExprMatch { |
| 1035 | expr: Box::new(obj), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1036 | match_token: tokens::Match::default(), |
| 1037 | brace_token: tokens::Brace::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1038 | arms: { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1039 | for arm in &mut arms { |
| 1040 | if arm_requires_comma(arm) { |
| 1041 | arm.comma = Some(tokens::Comma::default()); |
| 1042 | } |
| 1043 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1044 | arms.extend(last_arm); |
| 1045 | arms |
| 1046 | }, |
| 1047 | }.into()) |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1048 | )); |
| 1049 | |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1050 | named!(expr_catch -> ExprKind, do_parse!( |
| 1051 | keyword!("do") >> |
| 1052 | keyword!("catch") >> |
| 1053 | catch_block: block >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1054 | (ExprCatch { |
| 1055 | block: catch_block, |
| 1056 | do_token: tokens::Do::default(), |
| 1057 | catch_token: tokens::Catch::default(), |
| 1058 | }.into()) |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1059 | )); |
| 1060 | |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1061 | fn arm_requires_comma(arm: &Arm) -> bool { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1062 | if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node { |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1063 | false |
| 1064 | } else { |
| 1065 | true |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | named!(match_arm -> Arm, do_parse!( |
| 1070 | attrs: many0!(outer_attr) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1071 | pats: separated_nonempty_list!(map!(punct!("|"), |_| tokens::Or::default()), |
| 1072 | pat) >> |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1073 | guard: option!(preceded!(keyword!("if"), expr)) >> |
| 1074 | punct!("=>") >> |
| 1075 | body: alt!( |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1076 | map!(block, |blk| { |
| 1077 | ExprKind::Block(ExprBlock { |
| 1078 | unsafety: Unsafety::Normal, |
| 1079 | block: blk, |
| 1080 | }).into() |
| 1081 | }) |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1082 | | |
| 1083 | expr |
| 1084 | ) >> |
| 1085 | (Arm { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1086 | rocket_token: tokens::Rocket::default(), |
| 1087 | if_token: guard.as_ref().map(|_| tokens::If::default()), |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1088 | attrs: attrs, |
| 1089 | pats: pats, |
| 1090 | guard: guard.map(Box::new), |
| 1091 | body: Box::new(body), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1092 | comma: None, |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1093 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1094 | )); |
| 1095 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1096 | named_ambiguous_expr!(expr_closure -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1097 | capture: capture_by >> |
| 1098 | punct!("|") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1099 | inputs: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1100 | closure_arg) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1101 | punct!("|") >> |
| 1102 | ret_and_body: alt!( |
| 1103 | do_parse!( |
| 1104 | punct!("->") >> |
| 1105 | ty: ty >> |
| 1106 | body: block >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1107 | (FunctionRetTy::Ty(ty, tokens::RArrow::default()), |
| 1108 | ExprKind::Block(ExprBlock { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1109 | unsafety: Unsafety::Normal, |
| 1110 | block: body, |
| 1111 | }).into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1112 | ) |
| 1113 | | |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 1114 | map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e)) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1115 | ) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1116 | (ExprClosure { |
| 1117 | capture: capture, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1118 | or1_token: tokens::Or::default(), |
| 1119 | or2_token: tokens::Or::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1120 | decl: Box::new(FnDecl { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1121 | inputs: inputs, |
| 1122 | output: ret_and_body.0, |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 1123 | variadic: false, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1124 | dot_tokens: None, |
| 1125 | fn_token: tokens::Fn::default(), |
| 1126 | generics: Generics::default(), |
| 1127 | paren_token: tokens::Paren::default(), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1128 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1129 | body: Box::new(ret_and_body.1), |
| 1130 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1131 | )); |
| 1132 | |
| 1133 | named!(closure_arg -> FnArg, do_parse!( |
| 1134 | pat: pat >> |
| 1135 | ty: option!(preceded!(punct!(":"), ty)) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1136 | (ArgCaptured { |
| 1137 | pat: pat, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1138 | colon_token: tokens::Colon::default(), |
| 1139 | ty: ty.unwrap_or_else(|| TyInfer { |
| 1140 | underscore_token: tokens::Underscore::default(), |
| 1141 | }.into()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1142 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1143 | )); |
| 1144 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1145 | named!(expr_while -> ExprKind, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1146 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 57ffbde | 2016-09-30 09:38:04 -0700 | [diff] [blame] | 1147 | keyword!("while") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1148 | cond: cond >> |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 1149 | while_block: block >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1150 | (match cond { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1151 | Cond::Let(pat, expr, eq_token, let_token) => ExprWhileLet { |
| 1152 | eq_token: eq_token, |
| 1153 | let_token: let_token, |
| 1154 | while_token: tokens::While::default(), |
| 1155 | colon_token: lbl.as_ref().map(|_| tokens::Colon::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1156 | pat: Box::new(pat), |
| 1157 | expr: Box::new(expr), |
| 1158 | body: while_block, |
| 1159 | label: lbl, |
| 1160 | }.into(), |
| 1161 | Cond::Expr(cond) => ExprWhile { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1162 | while_token: tokens::While::default(), |
| 1163 | colon_token: lbl.as_ref().map(|_| tokens::Colon::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1164 | cond: Box::new(cond), |
| 1165 | body: while_block, |
| 1166 | label: lbl, |
| 1167 | }.into(), |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1168 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 1169 | )); |
| 1170 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1171 | named!(expr_continue -> ExprKind, do_parse!( |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1172 | keyword!("continue") >> |
| 1173 | lbl: option!(label) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1174 | (ExprContinue { |
| 1175 | continue_token: tokens::Continue::default(), |
| 1176 | label: lbl, |
| 1177 | }.into()) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1178 | )); |
| 1179 | |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1180 | named_ambiguous_expr!(expr_break -> ExprKind, allow_struct, do_parse!( |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1181 | keyword!("break") >> |
| 1182 | lbl: option!(label) >> |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1183 | val: option!(call!(ambiguous_expr, allow_struct, false)) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1184 | (ExprBreak { |
| 1185 | label: lbl, |
| 1186 | expr: val.map(Box::new), |
| 1187 | break_token: tokens::Break::default(), |
| 1188 | }.into()) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1189 | )); |
| 1190 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1191 | named_ambiguous_expr!(expr_ret -> ExprKind, allow_struct, do_parse!( |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1192 | keyword!("return") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1193 | ret_value: option!(ambiguous_expr!(allow_struct)) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1194 | (ExprRet { |
| 1195 | expr: ret_value.map(Box::new), |
| 1196 | return_token: tokens::Return::default(), |
| 1197 | }.into()) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1198 | )); |
| 1199 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1200 | named!(expr_struct -> ExprKind, do_parse!( |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1201 | path: path >> |
| 1202 | punct!("{") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1203 | fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1204 | field_value) >> |
| 1205 | base: option!( |
| 1206 | cond!(fields.is_empty() || fields.trailing_delim(), |
| 1207 | do_parse!( |
| 1208 | punct!("..") >> |
| 1209 | base: expr >> |
| 1210 | (base) |
| 1211 | ) |
| 1212 | ) |
| 1213 | ) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1214 | punct!("}") >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1215 | (ExprStruct { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1216 | brace_token: tokens::Brace::default(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1217 | path: path, |
| 1218 | fields: fields, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1219 | dot2_token: base.as_ref().and_then(|b| b.as_ref()) |
| 1220 | .map(|_| tokens::Dot2::default()), |
| 1221 | rest: base.and_then(|b| b.map(Box::new)), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1222 | }.into()) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1223 | )); |
| 1224 | |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 1225 | named!(field_value -> FieldValue, alt!( |
| 1226 | do_parse!( |
| 1227 | name: wordlike >> |
| 1228 | punct!(":") >> |
| 1229 | value: expr >> |
| 1230 | (FieldValue { |
| 1231 | ident: name, |
| 1232 | expr: value, |
| 1233 | is_shorthand: false, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 1234 | attrs: Vec::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1235 | colon_token: Some(tokens::Colon::default()), |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 1236 | }) |
| 1237 | ) |
| 1238 | | |
| 1239 | map!(ident, |name: Ident| FieldValue { |
| 1240 | ident: name.clone(), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1241 | expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(), |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 1242 | is_shorthand: true, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 1243 | attrs: Vec::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1244 | colon_token: None, |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1245 | }) |
| 1246 | )); |
| 1247 | |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1248 | named!(expr_repeat -> ExprKind, delim!(Bracket, do_parse!( |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1249 | value: expr >> |
| 1250 | punct!(";") >> |
| 1251 | times: expr >> |
| 1252 | punct!("]") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1253 | (ExprRepeat { |
| 1254 | expr: Box::new(value), |
| 1255 | amt: Box::new(times), |
| 1256 | bracket_token: tokens::Bracket::default(), |
| 1257 | semi_token: tokens::Semi::default(), |
| 1258 | }.into()) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1259 | )); |
| 1260 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1261 | named!(expr_block -> ExprKind, do_parse!( |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 1262 | rules: unsafety >> |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1263 | b: block >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1264 | (ExprBlock { |
| 1265 | unsafety: rules, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1266 | block: b, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1267 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1268 | )); |
| 1269 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1270 | named_ambiguous_expr!(expr_range -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1271 | limits: range_limits >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1272 | hi: option!(ambiguous_expr!(allow_struct)) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1273 | (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into()) |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1274 | )); |
| 1275 | |
| 1276 | named!(range_limits -> RangeLimits, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1277 | punct!("...") => { |_| RangeLimits::Closed(tokens::Dot3::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1278 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1279 | punct!("..") => { |_| RangeLimits::HalfOpen(tokens::Dot2::default()) } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1280 | )); |
| 1281 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1282 | named!(expr_path -> ExprKind, map!(qpath, |(qself, path)| { |
| 1283 | ExprPath { qself: qself, path: path }.into() |
| 1284 | })); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1285 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1286 | named_ambiguous_expr!(expr_addr_of -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1287 | punct!("&") >> |
| 1288 | mutability: mutability >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1289 | expr: ambiguous_expr!(allow_struct) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1290 | (ExprAddrOf { |
| 1291 | mutbl: mutability, |
| 1292 | expr: Box::new(expr), |
| 1293 | and_token: tokens::And::default(), |
| 1294 | }.into()) |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1295 | )); |
| 1296 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1297 | named_ambiguous_expr!(and_assign -> (Expr, tokens::Eq), allow_struct, preceded!( |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1298 | punct!("="), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1299 | map!(ambiguous_expr!(allow_struct), |t| (t, tokens::Eq::default())) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1300 | )); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1301 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1302 | named_ambiguous_expr!(and_assign_op -> (BinOp, Expr), allow_struct, tuple!( |
| 1303 | assign_op, |
| 1304 | ambiguous_expr!(allow_struct) |
| 1305 | )); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1306 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1307 | named!(and_field -> (Ident, tokens::Dot), |
| 1308 | preceded!(punct!("."), map!(ident, |t| (t, tokens::Dot::default())))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1309 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1310 | named!(and_tup_field -> (Lit, tokens::Dot), |
| 1311 | preceded!(punct!("."), map!(lit, |l| (l, tokens::Dot::default())))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1312 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1313 | named!(and_index -> (Expr, tokens::Bracket), |
| 1314 | map!(delimited!(punct!("["), expr, punct!("]")), |
| 1315 | |t| (t, tokens::Bracket::default()))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1316 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1317 | named_ambiguous_expr!(and_range -> (RangeLimits, Option<Expr>), allow_struct, tuple!( |
| 1318 | range_limits, |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 1319 | option!(call!(ambiguous_expr, allow_struct, false)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1320 | )); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1321 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1322 | named!(pub block -> Block, do_parse!( |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1323 | stmts: delim!(Brace, within_block) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1324 | (Block { |
| 1325 | stmts: stmts, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1326 | brace_token: tokens::Brace::default(), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1327 | }) |
| 1328 | )); |
| 1329 | |
David Tolnay | 3b9783a | 2016-10-29 22:37:09 -0700 | [diff] [blame] | 1330 | named!(pub within_block -> Vec<Stmt>, do_parse!( |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1331 | many0!(punct!(";")) >> |
David Tolnay | 1bf1913 | 2017-02-19 22:54:25 -0800 | [diff] [blame] | 1332 | mut standalone: many0!(terminated!(stmt, many0!(punct!(";")))) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1333 | last: option!(expr) >> |
| 1334 | (match last { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1335 | None => standalone, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1336 | Some(last) => { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1337 | standalone.push(Stmt::Expr(Box::new(last))); |
| 1338 | standalone |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1339 | } |
| 1340 | }) |
| 1341 | )); |
| 1342 | |
David Tolnay | 1bf1913 | 2017-02-19 22:54:25 -0800 | [diff] [blame] | 1343 | named!(pub stmt -> Stmt, alt!( |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1344 | stmt_mac |
| 1345 | | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1346 | stmt_local |
| 1347 | | |
| 1348 | stmt_item |
| 1349 | | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1350 | stmt_expr |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1351 | )); |
| 1352 | |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1353 | named!(stmt_mac -> Stmt, do_parse!( |
| 1354 | attrs: many0!(outer_attr) >> |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1355 | what: path >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1356 | punct!("!") >> |
| 1357 | // Only parse braces here; paren and bracket will get parsed as |
| 1358 | // expression statements |
| 1359 | punct!("{") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1360 | ts: token_stream >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1361 | punct!("}") >> |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 1362 | semi: option!(punct!(";")) >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1363 | (Stmt::Mac(Box::new(( |
| 1364 | Mac { |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1365 | path: what, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1366 | bang_token: tokens::Bang::default(), |
| 1367 | tokens: vec![TokenTree(proc_macro2::TokenTree { |
| 1368 | span: Default::default(), |
| 1369 | kind: TokenKind::Sequence(Delimiter::Brace, ts), |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1370 | })], |
| 1371 | }, |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 1372 | if semi.is_some() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1373 | MacStmtStyle::Semicolon(tokens::Semi::default()) |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 1374 | } else { |
| 1375 | MacStmtStyle::Braces |
| 1376 | }, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1377 | attrs, |
| 1378 | )))) |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1379 | )); |
| 1380 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1381 | named!(stmt_local -> Stmt, do_parse!( |
| 1382 | attrs: many0!(outer_attr) >> |
| 1383 | keyword!("let") >> |
| 1384 | pat: pat >> |
| 1385 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 1386 | init: option!(preceded!(punct!("="), expr)) >> |
| 1387 | punct!(";") >> |
| 1388 | (Stmt::Local(Box::new(Local { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1389 | let_token: tokens::Let::default(), |
| 1390 | semi_token: tokens::Semi::default(), |
| 1391 | colon_token: ty.as_ref().map(|_| tokens::Colon::default()), |
| 1392 | eq_token: init.as_ref().map(|_| tokens::Eq::default()), |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1393 | pat: Box::new(pat), |
| 1394 | ty: ty.map(Box::new), |
| 1395 | init: init.map(Box::new), |
| 1396 | attrs: attrs, |
| 1397 | }))) |
| 1398 | )); |
| 1399 | |
| 1400 | named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i)))); |
| 1401 | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1402 | fn requires_semi(e: &Expr) -> bool { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1403 | match e.node { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1404 | ExprKind::If(_) | |
| 1405 | ExprKind::IfLet(_) | |
| 1406 | ExprKind::While(_) | |
| 1407 | ExprKind::WhileLet(_) | |
| 1408 | ExprKind::ForLoop(_) | |
| 1409 | ExprKind::Loop(_) | |
| 1410 | ExprKind::Match(_) | |
| 1411 | ExprKind::Block(_) => false, |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1412 | |
| 1413 | _ => true, |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1418 | attrs: many0!(outer_attr) >> |
| 1419 | mut e: expr >> |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1420 | semi: option!(punct!(";")) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1421 | ({ |
| 1422 | e.attrs = attrs; |
| 1423 | if semi.is_some() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1424 | Stmt::Semi(Box::new(e), tokens::Semi::default()) |
David Tolnay | 092dcb0 | 2016-10-30 10:14:14 -0700 | [diff] [blame] | 1425 | } else if requires_semi(&e) { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1426 | return IResult::Error; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1427 | } else { |
| 1428 | Stmt::Expr(Box::new(e)) |
| 1429 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1430 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1431 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1432 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1433 | named!(pub pat -> Pat, alt!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1434 | pat_wild // must be before pat_ident |
| 1435 | | |
| 1436 | pat_box // must be before pat_ident |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1437 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1438 | pat_range // must be before pat_lit |
| 1439 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1440 | pat_tuple_struct // must be before pat_ident |
| 1441 | | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1442 | pat_struct // must be before pat_ident |
| 1443 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1444 | pat_mac // must be before pat_ident |
| 1445 | | |
David Tolnay | bcdbdd0 | 2016-10-24 23:05:02 -0700 | [diff] [blame] | 1446 | pat_lit // must be before pat_ident |
| 1447 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1448 | pat_ident // must be before pat_path |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1449 | | |
| 1450 | pat_path |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1451 | | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1452 | map!(pat_tuple, |t: PatTuple| t.into()) |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1453 | | |
| 1454 | pat_ref |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1455 | | |
| 1456 | pat_slice |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1457 | )); |
| 1458 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 1459 | named!(pat_mac -> Pat, map!(mac, Pat::Mac)); |
| 1460 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1461 | named!(pat_wild -> Pat, map!(keyword!("_"), |_| { |
| 1462 | PatWild { underscore_token: tokens::Underscore::default() }.into() |
| 1463 | })); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1464 | |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1465 | named!(pat_box -> Pat, do_parse!( |
| 1466 | keyword!("box") >> |
| 1467 | pat: pat >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1468 | (PatBox { |
| 1469 | pat: Box::new(pat), |
| 1470 | box_token: tokens::Box::default(), |
| 1471 | }.into()) |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1472 | )); |
| 1473 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1474 | named!(pat_ident -> Pat, do_parse!( |
| 1475 | mode: option!(keyword!("ref")) >> |
| 1476 | mutability: mutability >> |
David Tolnay | 8d4d2fa | 2016-10-25 22:08:07 -0700 | [diff] [blame] | 1477 | name: alt!( |
| 1478 | ident |
| 1479 | | |
| 1480 | keyword!("self") => { Into::into } |
| 1481 | ) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1482 | not!(punct!("<")) >> |
| 1483 | not!(punct!("::")) >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1484 | subpat: option!(preceded!(punct!("@"), pat)) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1485 | (PatIdent { |
| 1486 | mode: if mode.is_some() { |
| 1487 | BindingMode::ByRef(tokens::Ref::default(), mutability) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1488 | } else { |
| 1489 | BindingMode::ByValue(mutability) |
| 1490 | }, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1491 | ident: name, |
| 1492 | at_token: subpat.as_ref().map(|_| tokens::At::default()), |
| 1493 | subpat: subpat.map(Box::new), |
| 1494 | }.into()) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1495 | )); |
| 1496 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1497 | named!(pat_tuple_struct -> Pat, do_parse!( |
| 1498 | path: path >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1499 | tuple: pat_tuple >> |
| 1500 | (PatTupleStruct { |
| 1501 | path: path, |
| 1502 | pat: tuple, |
| 1503 | }.into()) |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1504 | )); |
| 1505 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1506 | named!(pat_struct -> Pat, do_parse!( |
| 1507 | path: path >> |
| 1508 | punct!("{") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1509 | fields: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1510 | field_pat) >> |
| 1511 | base: option!( |
| 1512 | cond!(fields.is_empty() || fields.trailing_delim(), |
| 1513 | punct!("..")) |
| 1514 | ) >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1515 | punct!("}") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1516 | (PatStruct { |
| 1517 | path: path, |
| 1518 | fields: fields, |
| 1519 | brace_token: tokens::Brace::default(), |
| 1520 | dot2_token: base.and_then(|m| m).map(|_| tokens::Dot2::default()), |
| 1521 | }.into()) |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1522 | )); |
| 1523 | |
| 1524 | named!(field_pat -> FieldPat, alt!( |
| 1525 | do_parse!( |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 1526 | ident: wordlike >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1527 | punct!(":") >> |
| 1528 | pat: pat >> |
| 1529 | (FieldPat { |
| 1530 | ident: ident, |
| 1531 | pat: Box::new(pat), |
| 1532 | is_shorthand: false, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 1533 | attrs: Vec::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1534 | colon_token: Some(tokens::Colon::default()), |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1535 | }) |
| 1536 | ) |
| 1537 | | |
| 1538 | do_parse!( |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1539 | boxed: option!(keyword!("box")) >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1540 | mode: option!(keyword!("ref")) >> |
| 1541 | mutability: mutability >> |
| 1542 | ident: ident >> |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1543 | ({ |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1544 | let mut pat: Pat = PatIdent { |
| 1545 | mode: if mode.is_some() { |
| 1546 | BindingMode::ByRef(tokens::Ref::default(), mutability) |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1547 | } else { |
| 1548 | BindingMode::ByValue(mutability) |
| 1549 | }, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1550 | ident: ident.clone(), |
| 1551 | subpat: None, |
| 1552 | at_token: None, |
| 1553 | }.into(); |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1554 | if boxed.is_some() { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1555 | pat = PatBox { |
| 1556 | pat: Box::new(pat), |
| 1557 | box_token: tokens::Box::default(), |
| 1558 | }.into(); |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1559 | } |
| 1560 | FieldPat { |
| 1561 | ident: ident, |
| 1562 | pat: Box::new(pat), |
| 1563 | is_shorthand: true, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 1564 | attrs: Vec::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1565 | colon_token: None, |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1566 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1567 | }) |
| 1568 | ) |
| 1569 | )); |
| 1570 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1571 | named!(pat_path -> Pat, map!(qpath, |(qself, path)| { |
| 1572 | PatPath { qself: qself, path: path }.into() |
| 1573 | })); |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1574 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1575 | named!(pat_tuple -> PatTuple, do_parse!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1576 | punct!("(") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1577 | mut elems: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1578 | pat) >> |
| 1579 | dotdot: map!(cond!( |
| 1580 | elems.is_empty() || elems.trailing_delim(), |
| 1581 | option!(do_parse!( |
| 1582 | punct!("..") >> |
| 1583 | trailing: option!(punct!(",")) >> |
| 1584 | (trailing.is_some()) |
| 1585 | )) |
| 1586 | ), |x: Option<_>| x.and_then(|x| x)) >> |
| 1587 | rest: cond!(dotdot == Some(true), |
| 1588 | terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1589 | pat)) >> |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1590 | punct!(")") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1591 | (PatTuple { |
| 1592 | paren_token: tokens::Paren::default(), |
| 1593 | dots_pos: dotdot.map(|_| elems.len()), |
| 1594 | dot2_token: dotdot.map(|_| tokens::Dot2::default()), |
| 1595 | comma_token: dotdot.and_then(|b| { |
| 1596 | if b { |
| 1597 | Some(tokens::Comma::default()) |
| 1598 | } else { |
| 1599 | None |
| 1600 | } |
| 1601 | }), |
| 1602 | pats: { |
| 1603 | if let Some(rest) = rest { |
| 1604 | for elem in rest.into_iter() { |
| 1605 | elems.push(elem); |
| 1606 | } |
| 1607 | } |
| 1608 | elems |
| 1609 | }, |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1610 | }) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1611 | ))); |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1612 | |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1613 | named!(pat_ref -> Pat, do_parse!( |
| 1614 | punct!("&") >> |
| 1615 | mutability: mutability >> |
| 1616 | pat: pat >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1617 | (PatRef { |
| 1618 | pat: Box::new(pat), |
| 1619 | mutbl: mutability, |
| 1620 | and_token: tokens::And::default(), |
| 1621 | }.into()) |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1622 | )); |
| 1623 | |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1624 | named!(pat_lit -> Pat, do_parse!( |
| 1625 | lit: pat_lit_expr >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1626 | (if let ExprKind::Path(_) = lit.node { |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1627 | return IResult::Error; // these need to be parsed by pat_path |
| 1628 | } else { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1629 | PatLit { |
| 1630 | expr: Box::new(lit), |
| 1631 | }.into() |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1632 | }) |
| 1633 | )); |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1634 | |
| 1635 | named!(pat_range -> Pat, do_parse!( |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1636 | lo: pat_lit_expr >> |
Arnavion | 1992e2f | 2017-04-25 01:47:46 -0700 | [diff] [blame] | 1637 | limits: range_limits >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1638 | hi: pat_lit_expr >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1639 | (PatRange { |
| 1640 | lo: Box::new(lo), |
| 1641 | hi: Box::new(hi), |
| 1642 | limits: limits, |
| 1643 | }.into()) |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1644 | )); |
| 1645 | |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1646 | named!(pat_lit_expr -> Expr, do_parse!( |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1647 | neg: option!(punct!("-")) >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1648 | v: alt!( |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1649 | lit => { ExprKind::Lit } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1650 | | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1651 | path => { |p| ExprPath { qself: None, path: p }.into() } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1652 | ) >> |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1653 | (if neg.is_some() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1654 | ExprKind::Unary(ExprUnary { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1655 | op: UnOp::Neg(tokens::Sub::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1656 | expr: Box::new(v.into()) |
| 1657 | }).into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1658 | } else { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1659 | v.into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1660 | }) |
| 1661 | )); |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1662 | |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1663 | named!(pat_slice -> Pat, do_parse!( |
| 1664 | punct!("[") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1665 | mut before: terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1666 | pat) >> |
| 1667 | middle: option!(do_parse!( |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1668 | punct!("..") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1669 | trailing: option!(punct!(",")) >> |
| 1670 | (trailing.is_some()) |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1671 | )) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1672 | after: cond!( |
| 1673 | match middle { |
| 1674 | Some(trailing) => trailing, |
| 1675 | _ => false, |
| 1676 | }, |
| 1677 | terminated_list!(map!(punct!(","), |_| tokens::Comma::default()), |
| 1678 | pat) |
| 1679 | ) >> |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1680 | punct!("]") >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1681 | (PatSlice { |
| 1682 | dot2_token: middle.as_ref().map(|_| tokens::Dot2::default()), |
| 1683 | comma_token: { |
| 1684 | let trailing = middle.unwrap_or(false); |
| 1685 | if trailing {Some(tokens::Comma::default())} else {None} |
| 1686 | }, |
| 1687 | bracket_token: tokens::Bracket::default(), |
| 1688 | middle: middle.and_then(|_| { |
| 1689 | if !before.is_empty() && !before.trailing_delim() { |
| 1690 | Some(Box::new(before.pop().unwrap().into_item())) |
| 1691 | } else { |
| 1692 | None |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1693 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1694 | }), |
| 1695 | front: before, |
| 1696 | back: after.unwrap_or_default(), |
| 1697 | }.into()) |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1698 | )); |
| 1699 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1700 | named!(capture_by -> CaptureBy, alt!( |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1701 | keyword!("move") => { |_| CaptureBy::Value(tokens::Move::default()) } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1702 | | |
| 1703 | epsilon!() => { |_| CaptureBy::Ref } |
| 1704 | )); |
| 1705 | |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1706 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1709 | #[cfg(feature = "printing")] |
| 1710 | mod printing { |
| 1711 | use super::*; |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1712 | use attr::FilterAttrs; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1713 | use quote::{Tokens, ToTokens}; |
| 1714 | |
| 1715 | impl ToTokens for Expr { |
| 1716 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1717 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1718 | self.node.to_tokens(tokens) |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | impl ToTokens for ExprBox { |
| 1723 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1724 | self.box_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1725 | self.expr.to_tokens(tokens); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | impl ToTokens for ExprInPlace { |
| 1730 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1731 | self.in_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1732 | self.place.to_tokens(tokens); |
| 1733 | self.value.to_tokens(tokens); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | impl ToTokens for ExprArray { |
| 1738 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1739 | self.bracket_token.surround(tokens, |tokens| { |
| 1740 | self.exprs.to_tokens(tokens); |
| 1741 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | impl ToTokens for ExprCall { |
| 1746 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1747 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1748 | self.paren_token.surround(tokens, |tokens| { |
| 1749 | self.args.to_tokens(tokens); |
| 1750 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | impl ToTokens for ExprMethodCall { |
| 1755 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1756 | self.expr.to_tokens(tokens); |
| 1757 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1758 | self.method.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1759 | self.colon2_token.to_tokens(tokens); |
| 1760 | self.lt_token.to_tokens(tokens); |
| 1761 | self.typarams.to_tokens(tokens); |
| 1762 | self.gt_token.to_tokens(tokens); |
| 1763 | self.paren_token.surround(tokens, |tokens| { |
| 1764 | self.args.to_tokens(tokens); |
| 1765 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | impl ToTokens for ExprTup { |
| 1770 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1771 | self.paren_token.surround(tokens, |tokens| { |
| 1772 | self.args.to_tokens(tokens); |
| 1773 | self.lone_comma.to_tokens(tokens); |
| 1774 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | impl ToTokens for ExprBinary { |
| 1779 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1780 | self.left.to_tokens(tokens); |
| 1781 | self.op.to_tokens(tokens); |
| 1782 | self.right.to_tokens(tokens); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | impl ToTokens for ExprUnary { |
| 1787 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1788 | self.op.to_tokens(tokens); |
| 1789 | self.expr.to_tokens(tokens); |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | impl ToTokens for ExprCast { |
| 1794 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1795 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1796 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1797 | self.ty.to_tokens(tokens); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | impl ToTokens for ExprType { |
| 1802 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1803 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1804 | self.colon_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1805 | self.ty.to_tokens(tokens); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | impl ToTokens for ExprIf { |
| 1810 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1811 | self.if_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1812 | self.cond.to_tokens(tokens); |
| 1813 | self.if_true.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1814 | self.else_token.to_tokens(tokens); |
| 1815 | self.if_false.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | impl ToTokens for ExprIfLet { |
| 1820 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1821 | self.if_token.to_tokens(tokens); |
| 1822 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1823 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1824 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1825 | self.expr.to_tokens(tokens); |
| 1826 | self.if_true.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1827 | self.else_token.to_tokens(tokens); |
| 1828 | self.if_false.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | impl ToTokens for ExprWhile { |
| 1833 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1834 | self.label.to_tokens(tokens); |
| 1835 | self.colon_token.to_tokens(tokens); |
| 1836 | self.while_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1837 | self.cond.to_tokens(tokens); |
| 1838 | self.body.to_tokens(tokens); |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | impl ToTokens for ExprWhileLet { |
| 1843 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1844 | self.label.to_tokens(tokens); |
| 1845 | self.colon_token.to_tokens(tokens); |
| 1846 | self.while_token.to_tokens(tokens); |
| 1847 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1848 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1849 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1850 | self.expr.to_tokens(tokens); |
| 1851 | self.body.to_tokens(tokens); |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | impl ToTokens for ExprForLoop { |
| 1856 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1857 | self.label.to_tokens(tokens); |
| 1858 | self.colon_token.to_tokens(tokens); |
| 1859 | self.for_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1860 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1861 | self.in_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1862 | self.expr.to_tokens(tokens); |
| 1863 | self.body.to_tokens(tokens); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | impl ToTokens for ExprLoop { |
| 1868 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1869 | self.label.to_tokens(tokens); |
| 1870 | self.colon_token.to_tokens(tokens); |
| 1871 | self.loop_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1872 | self.body.to_tokens(tokens); |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | impl ToTokens for ExprMatch { |
| 1877 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1878 | self.match_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1879 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1880 | self.brace_token.surround(tokens, |tokens| { |
| 1881 | tokens.append_all(&self.arms); |
| 1882 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | impl ToTokens for ExprCatch { |
| 1887 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1888 | self.do_token.to_tokens(tokens); |
| 1889 | self.catch_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1890 | self.block.to_tokens(tokens); |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | impl ToTokens for ExprClosure { |
| 1895 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1896 | self.capture.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1897 | self.or1_token.to_tokens(tokens); |
| 1898 | for item in self.decl.inputs.iter() { |
| 1899 | match **item.item() { |
| 1900 | FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1901 | pat.to_tokens(tokens); |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1902 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1903 | _ => item.item().to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1904 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1905 | item.delimiter().to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1906 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1907 | self.or2_token.to_tokens(tokens); |
| 1908 | self.decl.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1909 | self.body.to_tokens(tokens); |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | impl ToTokens for ExprBlock { |
| 1914 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1915 | self.unsafety.to_tokens(tokens); |
| 1916 | self.block.to_tokens(tokens); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | impl ToTokens for ExprAssign { |
| 1921 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1922 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1923 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1924 | self.right.to_tokens(tokens); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | impl ToTokens for ExprAssignOp { |
| 1929 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1930 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1931 | self.op.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1932 | self.right.to_tokens(tokens); |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | impl ToTokens for ExprField { |
| 1937 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1938 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1939 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1940 | self.field.to_tokens(tokens); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | impl ToTokens for ExprTupField { |
| 1945 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1946 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1947 | self.dot_token.to_tokens(tokens); |
| 1948 | self.field.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | impl ToTokens for ExprIndex { |
| 1953 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1954 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1955 | self.bracket_token.surround(tokens, |tokens| { |
| 1956 | self.index.to_tokens(tokens); |
| 1957 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | impl ToTokens for ExprRange { |
| 1962 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1963 | self.from.to_tokens(tokens); |
| 1964 | self.limits.to_tokens(tokens); |
| 1965 | self.to.to_tokens(tokens); |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | impl ToTokens for ExprPath { |
| 1970 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1971 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | impl ToTokens for ExprAddrOf { |
| 1976 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1977 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1978 | self.mutbl.to_tokens(tokens); |
| 1979 | self.expr.to_tokens(tokens); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | impl ToTokens for ExprBreak { |
| 1984 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1985 | self.break_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1986 | self.label.to_tokens(tokens); |
| 1987 | self.expr.to_tokens(tokens); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | impl ToTokens for ExprContinue { |
| 1992 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1993 | self.continue_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1994 | self.label.to_tokens(tokens); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | impl ToTokens for ExprRet { |
| 1999 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2000 | self.return_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2001 | self.expr.to_tokens(tokens); |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | impl ToTokens for ExprStruct { |
| 2006 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2007 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2008 | self.brace_token.surround(tokens, |tokens| { |
| 2009 | self.fields.to_tokens(tokens); |
| 2010 | self.dot2_token.to_tokens(tokens); |
| 2011 | self.rest.to_tokens(tokens); |
| 2012 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | impl ToTokens for ExprRepeat { |
| 2017 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2018 | self.bracket_token.surround(tokens, |tokens| { |
| 2019 | self.expr.to_tokens(tokens); |
| 2020 | self.semi_token.to_tokens(tokens); |
| 2021 | self.amt.to_tokens(tokens); |
| 2022 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | impl ToTokens for ExprParen { |
| 2027 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2028 | self.paren_token.surround(tokens, |tokens| { |
| 2029 | self.expr.to_tokens(tokens); |
| 2030 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | impl ToTokens for ExprTry { |
| 2035 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2036 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2037 | self.question_token.to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2038 | } |
| 2039 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2040 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2041 | impl ToTokens for FieldValue { |
| 2042 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2043 | self.ident.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 2044 | if !self.is_shorthand { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2045 | self.colon_token.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 2046 | self.expr.to_tokens(tokens); |
| 2047 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2048 | } |
| 2049 | } |
| 2050 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2051 | impl ToTokens for Arm { |
| 2052 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2053 | tokens.append_all(&self.attrs); |
| 2054 | self.pats.to_tokens(tokens); |
| 2055 | self.if_token.to_tokens(tokens); |
| 2056 | self.guard.to_tokens(tokens); |
| 2057 | self.rocket_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2058 | self.body.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2059 | self.comma.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2060 | } |
| 2061 | } |
| 2062 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2063 | impl ToTokens for PatWild { |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2064 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2065 | self.underscore_token.to_tokens(tokens); |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | impl ToTokens for PatIdent { |
| 2070 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2071 | self.mode.to_tokens(tokens); |
| 2072 | self.ident.to_tokens(tokens); |
| 2073 | self.at_token.to_tokens(tokens); |
| 2074 | self.subpat.to_tokens(tokens); |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | impl ToTokens for PatStruct { |
| 2079 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2080 | self.path.to_tokens(tokens); |
| 2081 | self.brace_token.surround(tokens, |tokens| { |
| 2082 | self.fields.to_tokens(tokens); |
| 2083 | self.dot2_token.to_tokens(tokens); |
| 2084 | }); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | impl ToTokens for PatTupleStruct { |
| 2089 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2090 | self.path.to_tokens(tokens); |
| 2091 | self.pat.to_tokens(tokens); |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | impl ToTokens for PatPath { |
| 2096 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2097 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | impl ToTokens for PatTuple { |
| 2102 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2103 | self.paren_token.surround(tokens, |tokens| { |
| 2104 | for (i, token) in self.pats.iter().enumerate() { |
| 2105 | if Some(i) == self.dots_pos { |
| 2106 | self.dot2_token.to_tokens(tokens); |
| 2107 | self.comma_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2108 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2109 | token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2110 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2111 | |
| 2112 | if Some(self.pats.len()) == self.dots_pos { |
| 2113 | self.dot2_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2114 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2115 | }); |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | impl ToTokens for PatBox { |
| 2120 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2121 | self.box_token.to_tokens(tokens); |
| 2122 | self.pat.to_tokens(tokens); |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | impl ToTokens for PatRef { |
| 2127 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2128 | self.and_token.to_tokens(tokens); |
| 2129 | self.mutbl.to_tokens(tokens); |
| 2130 | self.pat.to_tokens(tokens); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | impl ToTokens for PatLit { |
| 2135 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2136 | self.expr.to_tokens(tokens); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | impl ToTokens for PatRange { |
| 2141 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2142 | self.lo.to_tokens(tokens); |
| 2143 | self.limits.to_tokens(tokens); |
| 2144 | self.hi.to_tokens(tokens); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | impl ToTokens for PatSlice { |
| 2149 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2150 | self.bracket_token.surround(tokens, |tokens| { |
| 2151 | self.front.to_tokens(tokens); |
| 2152 | self.middle.to_tokens(tokens); |
| 2153 | self.dot2_token.to_tokens(tokens); |
| 2154 | self.comma_token.to_tokens(tokens); |
| 2155 | self.back.to_tokens(tokens); |
| 2156 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | |
Arnavion | 1992e2f | 2017-04-25 01:47:46 -0700 | [diff] [blame] | 2160 | impl ToTokens for RangeLimits { |
| 2161 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2162 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2163 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 2164 | RangeLimits::Closed(ref t) => t.to_tokens(tokens), |
Arnavion | 1992e2f | 2017-04-25 01:47:46 -0700 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2169 | impl ToTokens for FieldPat { |
| 2170 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2171 | if !self.is_shorthand { |
| 2172 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2173 | self.colon_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2174 | } |
| 2175 | self.pat.to_tokens(tokens); |
| 2176 | } |
| 2177 | } |
| 2178 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2179 | impl ToTokens for BindingMode { |
| 2180 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2181 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2182 | BindingMode::ByRef(ref t, ref m) => { |
| 2183 | t.to_tokens(tokens); |
| 2184 | m.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2185 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2186 | BindingMode::ByValue(ref m) => { |
| 2187 | m.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | } |
| 2191 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2192 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2193 | impl ToTokens for CaptureBy { |
| 2194 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2195 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2196 | CaptureBy::Value(ref t) => t.to_tokens(tokens), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 2197 | CaptureBy::Ref => { |
| 2198 | // nothing |
| 2199 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2204 | impl ToTokens for Block { |
| 2205 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2206 | self.brace_token.surround(tokens, |tokens| { |
| 2207 | tokens.append_all(&self.stmts); |
| 2208 | }); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2209 | } |
| 2210 | } |
| 2211 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2212 | impl ToTokens for Stmt { |
| 2213 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2214 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2215 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2216 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 2217 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2218 | Stmt::Semi(ref expr, ref semi) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2219 | expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2220 | semi.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2221 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2222 | Stmt::Mac(ref mac) => { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 2223 | let (ref mac, ref style, ref attrs) = **mac; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2224 | tokens.append_all(attrs.outer()); |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2225 | mac.to_tokens(tokens); |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 2226 | match *style { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2227 | MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 2228 | MacStmtStyle::Braces | MacStmtStyle::NoBraces => { |
| 2229 | // no semicolon |
| 2230 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2233 | } |
| 2234 | } |
| 2235 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2236 | |
| 2237 | impl ToTokens for Local { |
| 2238 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4e3158d | 2016-10-30 00:30:01 -0700 | [diff] [blame] | 2239 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2240 | self.let_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2241 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2242 | self.colon_token.to_tokens(tokens); |
| 2243 | self.ty.to_tokens(tokens); |
| 2244 | self.eq_token.to_tokens(tokens); |
| 2245 | self.init.to_tokens(tokens); |
| 2246 | self.semi_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2247 | } |
| 2248 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2249 | } |