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