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