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