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 | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1101 | named!(expr_nosemi -> Expr, map!(alt!( |
| 1102 | syn!(ExprIf) => { ExprKind::If } |
| 1103 | | |
| 1104 | syn!(ExprIfLet) => { ExprKind::IfLet } |
| 1105 | | |
| 1106 | syn!(ExprWhile) => { ExprKind::While } |
| 1107 | | |
| 1108 | syn!(ExprWhileLet) => { ExprKind::WhileLet } |
| 1109 | | |
| 1110 | syn!(ExprForLoop) => { ExprKind::ForLoop } |
| 1111 | | |
| 1112 | syn!(ExprLoop) => { ExprKind::Loop } |
| 1113 | | |
| 1114 | syn!(ExprMatch) => { ExprKind::Match } |
| 1115 | | |
| 1116 | syn!(ExprCatch) => { ExprKind::Catch } |
| 1117 | | |
| 1118 | syn!(ExprBlock) => { ExprKind::Block } |
| 1119 | ), Expr::from)); |
| 1120 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 1121 | impl Synom for ExprGroup { |
| 1122 | named!(parse -> Self, do_parse!( |
| 1123 | e: grouped!(syn!(Expr)) >> |
| 1124 | (ExprGroup { |
| 1125 | expr: Box::new(e.0), |
| 1126 | group_token: e.1, |
| 1127 | }.into()) |
| 1128 | )); |
| 1129 | } |
| 1130 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1131 | impl Synom for ExprParen { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1132 | named!(parse -> Self, do_parse!( |
| 1133 | e: parens!(syn!(Expr)) >> |
| 1134 | (ExprParen { |
| 1135 | expr: Box::new(e.0), |
| 1136 | paren_token: e.1, |
| 1137 | }.into()) |
| 1138 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1139 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1140 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1141 | impl Synom for ExprInPlace { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1142 | named!(parse -> Self, do_parse!( |
| 1143 | in_: syn!(In) >> |
| 1144 | place: expr_no_struct >> |
| 1145 | value: braces!(call!(Block::parse_within)) >> |
| 1146 | (ExprInPlace { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1147 | place: Box::new(place), |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 1148 | kind: InPlaceKind::In(in_), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1149 | value: Box::new(Expr { |
| 1150 | node: ExprBlock { |
| 1151 | unsafety: Unsafety::Normal, |
| 1152 | block: Block { |
| 1153 | stmts: value.0, |
| 1154 | brace_token: value.1, |
| 1155 | }, |
| 1156 | }.into(), |
| 1157 | attrs: Vec::new(), |
| 1158 | }), |
| 1159 | }) |
| 1160 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1161 | } |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 1162 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1163 | impl Synom for ExprArray { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1164 | named!(parse -> Self, do_parse!( |
| 1165 | elems: brackets!(call!(Delimited::parse_terminated)) >> |
| 1166 | (ExprArray { |
| 1167 | exprs: elems.0, |
| 1168 | bracket_token: elems.1, |
| 1169 | }) |
| 1170 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1171 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1172 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1173 | named!(and_call -> (Delimited<Expr, tokens::Comma>, tokens::Paren), |
| 1174 | parens!(call!(Delimited::parse_terminated))); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1175 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1176 | named!(and_method_call -> ExprMethodCall, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1177 | dot: syn!(Dot) >> |
| 1178 | method: syn!(Ident) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1179 | typarams: option!(do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1180 | colon2: syn!(Colon2) >> |
| 1181 | lt: syn!(Lt) >> |
| 1182 | tys: call!(Delimited::parse_terminated) >> |
| 1183 | gt: syn!(Gt) >> |
| 1184 | (colon2, lt, tys, gt) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1185 | )) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1186 | args: parens!(call!(Delimited::parse_terminated)) >> |
| 1187 | ({ |
| 1188 | let (colon2, lt, tys, gt) = match typarams { |
| 1189 | Some((a, b, c, d)) => (Some(a), Some(b), Some(c), Some(d)), |
| 1190 | None => (None, None, None, None), |
| 1191 | }; |
| 1192 | ExprMethodCall { |
| 1193 | // this expr will get overwritten after being returned |
| 1194 | expr: Box::new(ExprKind::Lit(Lit { |
| 1195 | span: Span::default(), |
| 1196 | value: LitKind::Bool(false), |
| 1197 | }).into()), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1198 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1199 | method: method, |
| 1200 | args: args.0, |
| 1201 | paren_token: args.1, |
| 1202 | dot_token: dot, |
| 1203 | lt_token: lt, |
| 1204 | gt_token: gt, |
| 1205 | colon2_token: colon2, |
| 1206 | typarams: tys.unwrap_or_default(), |
| 1207 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1208 | }) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1209 | )); |
| 1210 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1211 | impl Synom for ExprTup { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1212 | named!(parse -> Self, do_parse!( |
| 1213 | elems: parens!(call!(Delimited::parse_terminated)) >> |
| 1214 | (ExprTup { |
| 1215 | args: elems.0, |
| 1216 | paren_token: elems.1, |
| 1217 | lone_comma: None, // TODO: parse this |
| 1218 | }) |
| 1219 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1220 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 1221 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1222 | impl Synom for ExprIfLet { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1223 | named!(parse -> Self, do_parse!( |
| 1224 | if_: syn!(If) >> |
| 1225 | let_: syn!(Let) >> |
| 1226 | pat: syn!(Pat) >> |
| 1227 | eq: syn!(Eq) >> |
| 1228 | cond: expr_no_struct >> |
| 1229 | then_block: braces!(call!(Block::parse_within)) >> |
| 1230 | else_block: option!(else_block) >> |
| 1231 | (ExprIfLet { |
| 1232 | pat: Box::new(pat), |
| 1233 | let_token: let_, |
| 1234 | eq_token: eq, |
| 1235 | expr: Box::new(cond), |
| 1236 | if_true: Block { |
| 1237 | stmts: then_block.0, |
| 1238 | brace_token: then_block.1, |
| 1239 | }, |
| 1240 | if_token: if_, |
| 1241 | else_token: else_block.as_ref().map(|p| Else((p.0).0)), |
| 1242 | if_false: else_block.map(|p| Box::new(p.1.into())), |
| 1243 | }) |
| 1244 | )); |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1247 | impl Synom for ExprIf { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1248 | named!(parse -> Self, do_parse!( |
| 1249 | if_: syn!(If) >> |
| 1250 | cond: expr_no_struct >> |
| 1251 | then_block: braces!(call!(Block::parse_within)) >> |
| 1252 | else_block: option!(else_block) >> |
| 1253 | (ExprIf { |
| 1254 | cond: Box::new(cond), |
| 1255 | if_true: Block { |
| 1256 | stmts: then_block.0, |
| 1257 | brace_token: then_block.1, |
| 1258 | }, |
| 1259 | if_token: if_, |
| 1260 | else_token: else_block.as_ref().map(|p| Else((p.0).0)), |
| 1261 | if_false: else_block.map(|p| Box::new(p.1.into())), |
| 1262 | }) |
| 1263 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1264 | } |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1265 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1266 | named!(else_block -> (Else, ExprKind), do_parse!( |
| 1267 | else_: syn!(Else) >> |
| 1268 | expr: alt!( |
| 1269 | syn!(ExprIf) => { ExprKind::If } |
| 1270 | | |
| 1271 | syn!(ExprIfLet) => { ExprKind::IfLet } |
| 1272 | | |
| 1273 | do_parse!( |
| 1274 | else_block: braces!(call!(Block::parse_within)) >> |
| 1275 | (ExprKind::Block(ExprBlock { |
| 1276 | unsafety: Unsafety::Normal, |
| 1277 | block: Block { |
| 1278 | stmts: else_block.0, |
| 1279 | brace_token: else_block.1, |
| 1280 | }, |
| 1281 | })) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1282 | ) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1283 | ) >> |
| 1284 | (else_, expr) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1285 | )); |
| 1286 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1287 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1288 | impl Synom for ExprForLoop { |
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 | for_: syn!(For) >> |
| 1292 | pat: syn!(Pat) >> |
| 1293 | in_: syn!(In) >> |
| 1294 | expr: expr_no_struct >> |
| 1295 | loop_block: syn!(Block) >> |
| 1296 | (ExprForLoop { |
| 1297 | for_token: for_, |
| 1298 | in_token: in_, |
| 1299 | pat: Box::new(pat), |
| 1300 | expr: Box::new(expr), |
| 1301 | body: loop_block, |
| 1302 | colon_token: lbl.as_ref().map(|p| Colon((p.1).0)), |
| 1303 | label: lbl.map(|p| p.0), |
| 1304 | }) |
| 1305 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1306 | } |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 1307 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1308 | impl Synom for ExprLoop { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1309 | named!(parse -> Self, do_parse!( |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1310 | lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1311 | loop_: syn!(Loop) >> |
| 1312 | loop_block: syn!(Block) >> |
| 1313 | (ExprLoop { |
| 1314 | loop_token: loop_, |
| 1315 | body: loop_block, |
| 1316 | colon_token: lbl.as_ref().map(|p| Colon((p.1).0)), |
| 1317 | label: lbl.map(|p| p.0), |
| 1318 | }) |
| 1319 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | impl Synom for ExprMatch { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1323 | named!(parse -> Self, do_parse!( |
| 1324 | match_: syn!(Match) >> |
| 1325 | obj: expr_no_struct >> |
| 1326 | res: braces!(do_parse!( |
| 1327 | mut arms: many0!(do_parse!( |
| 1328 | arm: syn!(Arm) >> |
| 1329 | cond!(arm_requires_comma(&arm), syn!(Comma)) >> |
| 1330 | cond!(!arm_requires_comma(&arm), option!(syn!(Comma))) >> |
| 1331 | (arm) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1332 | )) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1333 | last_arm: option!(syn!(Arm)) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1334 | ({ |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1335 | arms.extend(last_arm); |
| 1336 | arms |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1337 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1338 | )) >> |
| 1339 | ({ |
| 1340 | let (mut arms, brace) = res; |
| 1341 | ExprMatch { |
| 1342 | expr: Box::new(obj), |
| 1343 | match_token: match_, |
| 1344 | brace_token: brace, |
| 1345 | arms: { |
| 1346 | for arm in &mut arms { |
| 1347 | if arm_requires_comma(arm) { |
| 1348 | arm.comma = Some(tokens::Comma::default()); |
| 1349 | } |
| 1350 | } |
| 1351 | arms |
| 1352 | }, |
| 1353 | } |
| 1354 | }) |
| 1355 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1356 | } |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1357 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1358 | impl Synom for ExprCatch { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1359 | named!(parse -> Self, do_parse!( |
| 1360 | do_: syn!(Do) >> |
| 1361 | catch_: syn!(Catch) >> |
| 1362 | catch_block: syn!(Block) >> |
| 1363 | (ExprCatch { |
| 1364 | block: catch_block, |
| 1365 | do_token: do_, |
| 1366 | catch_token: catch_, |
| 1367 | }.into()) |
| 1368 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1369 | } |
Arnavion | 02ef13f | 2017-04-25 00:54:31 -0700 | [diff] [blame] | 1370 | |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1371 | fn arm_requires_comma(arm: &Arm) -> bool { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1372 | if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node { |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 1373 | false |
| 1374 | } else { |
| 1375 | true |
| 1376 | } |
| 1377 | } |
| 1378 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1379 | impl Synom for Arm { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1380 | named!(parse -> Self, do_parse!( |
| 1381 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 1382 | pats: call!(Delimited::parse_separated_nonempty) >> |
| 1383 | guard: option!(tuple!(syn!(If), syn!(Expr))) >> |
| 1384 | rocket: syn!(Rocket) >> |
| 1385 | body: alt!( |
| 1386 | map!(syn!(Block), |blk| { |
| 1387 | ExprKind::Block(ExprBlock { |
| 1388 | unsafety: Unsafety::Normal, |
| 1389 | block: blk, |
| 1390 | }).into() |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1391 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1392 | | |
| 1393 | syn!(Expr) |
| 1394 | ) >> |
| 1395 | (Arm { |
| 1396 | rocket_token: rocket, |
| 1397 | if_token: guard.as_ref().map(|p| If((p.0).0)), |
| 1398 | attrs: attrs, |
| 1399 | pats: pats, |
| 1400 | guard: guard.map(|p| Box::new(p.1)), |
| 1401 | body: Box::new(body), |
| 1402 | comma: None, |
| 1403 | }) |
| 1404 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1405 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1406 | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1407 | named!(expr_closure(allow_struct: bool) -> ExprKind, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1408 | capture: syn!(CaptureBy) >> |
| 1409 | or1: syn!(Or) >> |
| 1410 | inputs: call!(Delimited::parse_terminated_with, fn_arg) >> |
| 1411 | or2: syn!(Or) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1412 | ret_and_body: alt!( |
| 1413 | do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1414 | arrow: syn!(RArrow) >> |
| 1415 | ty: syn!(Ty) >> |
| 1416 | body: syn!(Block) >> |
| 1417 | (FunctionRetTy::Ty(ty, arrow), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1418 | ExprKind::Block(ExprBlock { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1419 | unsafety: Unsafety::Normal, |
| 1420 | block: body, |
| 1421 | }).into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1422 | ) |
| 1423 | | |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 1424 | map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e)) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1425 | ) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1426 | (ExprClosure { |
| 1427 | capture: capture, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1428 | or1_token: or1, |
| 1429 | or2_token: or2, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1430 | decl: Box::new(FnDecl { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1431 | inputs: inputs, |
| 1432 | output: ret_and_body.0, |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 1433 | variadic: false, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1434 | dot_tokens: None, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1435 | fn_token: tokens::Fn_::default(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1436 | generics: Generics::default(), |
| 1437 | paren_token: tokens::Paren::default(), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1438 | }), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1439 | body: Box::new(ret_and_body.1), |
| 1440 | }.into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1441 | )); |
| 1442 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1443 | named!(fn_arg -> FnArg, do_parse!( |
| 1444 | pat: syn!(Pat) >> |
| 1445 | ty: option!(tuple!(syn!(Colon), syn!(Ty))) >> |
| 1446 | ({ |
| 1447 | let (colon, ty) = ty.unwrap_or_else(|| { |
| 1448 | (Colon::default(), TyInfer { |
| 1449 | underscore_token: Underscore::default(), |
| 1450 | }.into()) |
| 1451 | }); |
| 1452 | ArgCaptured { |
| 1453 | pat: pat, |
| 1454 | colon_token: colon, |
| 1455 | ty: ty, |
| 1456 | }.into() |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1457 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 1458 | )); |
| 1459 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1460 | impl Synom for ExprWhile { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1461 | named!(parse -> Self, do_parse!( |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1462 | lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1463 | while_: syn!(While) >> |
| 1464 | cond: expr_no_struct >> |
| 1465 | while_block: syn!(Block) >> |
| 1466 | (ExprWhile { |
| 1467 | while_token: while_, |
| 1468 | colon_token: lbl.as_ref().map(|p| Colon((p.1).0)), |
| 1469 | cond: Box::new(cond), |
| 1470 | body: while_block, |
| 1471 | label: lbl.map(|p| p.0), |
| 1472 | }) |
| 1473 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | impl Synom for ExprWhileLet { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1477 | named!(parse -> Self, do_parse!( |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1478 | lbl: option!(tuple!(syn!(Lifetime), syn!(Colon))) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1479 | while_: syn!(While) >> |
| 1480 | let_: syn!(Let) >> |
| 1481 | pat: syn!(Pat) >> |
| 1482 | eq: syn!(Eq) >> |
| 1483 | value: expr_no_struct >> |
| 1484 | while_block: syn!(Block) >> |
| 1485 | (ExprWhileLet { |
| 1486 | eq_token: eq, |
| 1487 | let_token: let_, |
| 1488 | while_token: while_, |
| 1489 | colon_token: lbl.as_ref().map(|p| Colon((p.1).0)), |
| 1490 | pat: Box::new(pat), |
| 1491 | expr: Box::new(value), |
| 1492 | body: while_block, |
| 1493 | label: lbl.map(|p| p.0), |
| 1494 | }) |
| 1495 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | impl Synom for ExprContinue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1499 | named!(parse -> Self, do_parse!( |
| 1500 | cont: syn!(Continue) >> |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1501 | lbl: option!(syn!(Lifetime)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1502 | (ExprContinue { |
| 1503 | continue_token: cont, |
| 1504 | label: lbl, |
| 1505 | }) |
| 1506 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1507 | } |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1508 | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1509 | named!(expr_break(allow_struct: bool) -> ExprKind, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1510 | break_: syn!(Break) >> |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 1511 | lbl: option!(syn!(Lifetime)) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1512 | // We can't allow blocks after a `break` expression when we wouldn't |
| 1513 | // allow structs, as this expression is ambiguous. |
| 1514 | val: opt_ambiguous_expr!(allow_struct) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1515 | (ExprBreak { |
| 1516 | label: lbl, |
| 1517 | expr: val.map(Box::new), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1518 | break_token: break_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1519 | }.into()) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 1520 | )); |
| 1521 | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1522 | named!(expr_ret(allow_struct: bool) -> ExprKind, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1523 | return_: syn!(Return) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1524 | // NOTE: return is greedy and eats blocks after it even when in a |
| 1525 | // position where structs are not allowed, such as in if statement |
| 1526 | // conditions. For example: |
| 1527 | // |
| 1528 | // if return { println!("A") } { } // Prints "A" |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 1529 | ret_value: option!(ambiguous_expr!(allow_struct)) >> |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1530 | (ExprRet { |
| 1531 | expr: ret_value.map(Box::new), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1532 | return_token: return_, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1533 | }.into()) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1534 | )); |
| 1535 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1536 | impl Synom for ExprStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1537 | named!(parse -> Self, do_parse!( |
| 1538 | path: syn!(Path) >> |
| 1539 | data: braces!(do_parse!( |
| 1540 | fields: call!(Delimited::parse_terminated) >> |
| 1541 | base: option!( |
| 1542 | cond!(fields.is_empty() || fields.trailing_delim(), |
| 1543 | do_parse!( |
| 1544 | dots: syn!(Dot2) >> |
| 1545 | base: syn!(Expr) >> |
| 1546 | (dots, base) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1547 | ) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1548 | ) |
| 1549 | ) >> |
| 1550 | (fields, base) |
| 1551 | )) >> |
| 1552 | ({ |
| 1553 | let ((fields, base), brace) = data; |
| 1554 | let (dots, rest) = match base.and_then(|b| b) { |
| 1555 | Some((dots, base)) => (Some(dots), Some(base)), |
| 1556 | None => (None, None), |
| 1557 | }; |
| 1558 | ExprStruct { |
| 1559 | brace_token: brace, |
| 1560 | path: path, |
| 1561 | fields: fields, |
| 1562 | dot2_token: dots, |
| 1563 | rest: rest.map(Box::new), |
| 1564 | } |
| 1565 | }) |
| 1566 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | impl Synom for FieldValue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1570 | named!(parse -> Self, alt!( |
| 1571 | do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1572 | ident: field_ident >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1573 | colon: syn!(Colon) >> |
| 1574 | value: syn!(Expr) >> |
| 1575 | (FieldValue { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1576 | ident: ident, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1577 | expr: value, |
| 1578 | is_shorthand: false, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1579 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1580 | colon_token: Some(colon), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1581 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1582 | ) |
| 1583 | | |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 1584 | map!(syn!(Ident), |name| FieldValue { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1585 | ident: name.clone(), |
| 1586 | expr: ExprKind::Path(ExprPath { qself: None, path: name.into() }).into(), |
| 1587 | is_shorthand: true, |
| 1588 | attrs: Vec::new(), |
| 1589 | colon_token: None, |
| 1590 | }) |
| 1591 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1592 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1593 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1594 | impl Synom for ExprRepeat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1595 | named!(parse -> Self, do_parse!( |
| 1596 | data: brackets!(do_parse!( |
| 1597 | value: syn!(Expr) >> |
| 1598 | semi: syn!(Semi) >> |
| 1599 | times: syn!(Expr) >> |
| 1600 | (value, semi, times) |
| 1601 | )) >> |
| 1602 | (ExprRepeat { |
| 1603 | expr: Box::new((data.0).0), |
| 1604 | amt: Box::new((data.0).2), |
| 1605 | bracket_token: data.1, |
| 1606 | semi_token: (data.0).1, |
| 1607 | }) |
| 1608 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1609 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1610 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1611 | impl Synom for ExprBlock { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1612 | named!(parse -> Self, do_parse!( |
| 1613 | rules: syn!(Unsafety) >> |
| 1614 | b: syn!(Block) >> |
| 1615 | (ExprBlock { |
| 1616 | unsafety: rules, |
| 1617 | block: b, |
| 1618 | }) |
| 1619 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1620 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1621 | |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1622 | named!(expr_range(allow_struct: bool) -> ExprKind, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1623 | limits: syn!(RangeLimits) >> |
Michael Layzell | b78f3b5 | 2017-06-04 19:03:03 -0400 | [diff] [blame] | 1624 | hi: opt_ambiguous_expr!(allow_struct) >> |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 1625 | (ExprRange { from: None, to: hi.map(Box::new), limits: limits }.into()) |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1626 | )); |
| 1627 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1628 | impl Synom for RangeLimits { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1629 | named!(parse -> Self, alt!( |
| 1630 | // Must come before Dot2 |
| 1631 | syn!(Dot3) => { RangeLimits::Closed } |
| 1632 | | |
| 1633 | syn!(Dot2) => { RangeLimits::HalfOpen } |
| 1634 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1635 | } |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1636 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1637 | impl Synom for ExprPath { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1638 | named!(parse -> Self, do_parse!( |
| 1639 | pair: qpath >> |
| 1640 | (ExprPath { |
| 1641 | qself: pair.0, |
| 1642 | path: pair.1, |
| 1643 | }) |
| 1644 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1645 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1646 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1647 | named!(and_field -> (Ident, Dot), |
| 1648 | map!(tuple!(syn!(Dot), syn!(Ident)), |(a, b)| (b, a))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1649 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1650 | named!(and_tup_field -> (Lit, Dot), |
| 1651 | map!(tuple!(syn!(Dot), syn!(Lit)), |(a, b)| (b, a))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1652 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1653 | named!(and_index -> (Expr, tokens::Bracket), brackets!(syn!(Expr))); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 1654 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1655 | impl Synom for Block { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1656 | named!(parse -> Self, do_parse!( |
| 1657 | stmts: braces!(call!(Block::parse_within)) >> |
| 1658 | (Block { |
| 1659 | stmts: stmts.0, |
| 1660 | brace_token: stmts.1, |
| 1661 | }) |
| 1662 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1663 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1664 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1665 | impl Block { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1666 | named!(pub parse_within -> Vec<Stmt>, do_parse!( |
| 1667 | many0!(syn!(Semi)) >> |
| 1668 | mut standalone: many0!(terminated!(syn!(Stmt), many0!(syn!(Semi)))) >> |
| 1669 | last: option!(syn!(Expr)) >> |
| 1670 | (match last { |
| 1671 | None => standalone, |
| 1672 | Some(last) => { |
| 1673 | standalone.push(Stmt::Expr(Box::new(last))); |
| 1674 | standalone |
| 1675 | } |
| 1676 | }) |
| 1677 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | impl Synom for Stmt { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1681 | named!(parse -> Self, alt!( |
| 1682 | stmt_mac |
| 1683 | | |
| 1684 | stmt_local |
| 1685 | | |
| 1686 | stmt_item |
| 1687 | | |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1688 | stmt_blockexpr |
| 1689 | | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1690 | stmt_expr |
| 1691 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1692 | } |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1693 | |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1694 | named!(stmt_mac -> Stmt, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1695 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 1696 | what: syn!(Path) >> |
| 1697 | bang: syn!(Bang) >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1698 | // Only parse braces here; paren and bracket will get parsed as |
| 1699 | // expression statements |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1700 | data: braces!(syn!(TokenStream)) >> |
| 1701 | semi: option!(syn!(Semi)) >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1702 | (Stmt::Mac(Box::new(( |
| 1703 | Mac { |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1704 | path: what, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1705 | bang_token: bang, |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1706 | ident: None, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1707 | tokens: vec![TokenTree(proc_macro2::TokenTree { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1708 | span: ((data.1).0).0, |
| 1709 | kind: TokenKind::Sequence(Delimiter::Brace, data.0), |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1710 | })], |
| 1711 | }, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1712 | match semi { |
| 1713 | Some(semi) => MacStmtStyle::Semicolon(semi), |
| 1714 | None => MacStmtStyle::Braces, |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 1715 | }, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 1716 | attrs, |
| 1717 | )))) |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1718 | )); |
| 1719 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1720 | named!(stmt_local -> Stmt, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1721 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 1722 | let_: syn!(Let) >> |
| 1723 | pat: syn!(Pat) >> |
| 1724 | ty: option!(tuple!(syn!(Colon), syn!(Ty))) >> |
| 1725 | init: option!(tuple!(syn!(Eq), syn!(Expr))) >> |
| 1726 | semi: syn!(Semi) >> |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1727 | (Stmt::Local(Box::new(Local { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1728 | let_token: let_, |
| 1729 | semi_token: semi, |
| 1730 | colon_token: ty.as_ref().map(|p| Colon((p.0).0)), |
| 1731 | eq_token: init.as_ref().map(|p| Eq((p.0).0)), |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1732 | pat: Box::new(pat), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1733 | ty: ty.map(|p| Box::new(p.1)), |
| 1734 | init: init.map(|p| Box::new(p.1)), |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1735 | attrs: attrs, |
| 1736 | }))) |
| 1737 | )); |
| 1738 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1739 | 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] | 1740 | |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1741 | named!(stmt_blockexpr -> Stmt, do_parse!( |
| 1742 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 1743 | mut e: expr_nosemi >> |
| 1744 | // If the next token is a `.` or a `?` it is special-cased to parse as |
| 1745 | // an expression instead of a blockexpression. |
| 1746 | not!(syn!(Dot)) >> |
| 1747 | not!(syn!(Question)) >> |
| 1748 | semi: option!(syn!(Semi)) >> |
| 1749 | ({ |
| 1750 | e.attrs = attrs; |
| 1751 | if let Some(semi) = semi { |
| 1752 | Stmt::Semi(Box::new(e), semi) |
| 1753 | } else { |
| 1754 | Stmt::Expr(Box::new(e)) |
| 1755 | } |
| 1756 | }) |
| 1757 | )); |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1758 | |
| 1759 | named!(stmt_expr -> Stmt, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1760 | attrs: many0!(call!(Attribute::parse_outer)) >> |
| 1761 | mut e: syn!(Expr) >> |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1762 | semi: syn!(Semi) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1763 | ({ |
| 1764 | e.attrs = attrs; |
Michael Layzell | 3541878 | 2017-06-07 09:20:25 -0400 | [diff] [blame] | 1765 | Stmt::Semi(Box::new(e), semi) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 1766 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 1767 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1768 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1769 | impl Synom for Pat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1770 | named!(parse -> Self, alt!( |
| 1771 | syn!(PatWild) => { Pat::Wild } // must be before pat_ident |
| 1772 | | |
| 1773 | syn!(PatBox) => { Pat::Box } // must be before pat_ident |
| 1774 | | |
| 1775 | syn!(PatRange) => { Pat::Range } // must be before pat_lit |
| 1776 | | |
| 1777 | syn!(PatTupleStruct) => { Pat::TupleStruct } // must be before pat_ident |
| 1778 | | |
| 1779 | syn!(PatStruct) => { Pat::Struct } // must be before pat_ident |
| 1780 | | |
| 1781 | syn!(Mac) => { Pat::Mac } // must be before pat_ident |
| 1782 | | |
| 1783 | syn!(PatLit) => { Pat::Lit } // must be before pat_ident |
| 1784 | | |
| 1785 | syn!(PatIdent) => { Pat::Ident } // must be before pat_path |
| 1786 | | |
| 1787 | syn!(PatPath) => { Pat::Path } |
| 1788 | | |
| 1789 | syn!(PatTuple) => { Pat::Tuple } |
| 1790 | | |
| 1791 | syn!(PatRef) => { Pat::Ref } |
| 1792 | | |
| 1793 | syn!(PatSlice) => { Pat::Slice } |
| 1794 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1795 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1796 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1797 | impl Synom for PatWild { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1798 | named!(parse -> Self, map!( |
| 1799 | syn!(Underscore), |
| 1800 | |u| PatWild { underscore_token: u } |
| 1801 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1802 | } |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 1803 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1804 | impl Synom for PatBox { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1805 | named!(parse -> Self, do_parse!( |
| 1806 | boxed: syn!(Box_) >> |
| 1807 | pat: syn!(Pat) >> |
| 1808 | (PatBox { |
| 1809 | pat: Box::new(pat), |
| 1810 | box_token: boxed, |
| 1811 | }) |
| 1812 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | impl Synom for PatIdent { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1816 | named!(parse -> Self, do_parse!( |
| 1817 | mode: option!(syn!(Ref)) >> |
| 1818 | mutability: syn!(Mutability) >> |
| 1819 | name: alt!( |
| 1820 | syn!(Ident) |
| 1821 | | |
| 1822 | syn!(Self_) => { Into::into } |
| 1823 | ) >> |
| 1824 | not!(syn!(Lt)) >> |
| 1825 | not!(syn!(Colon2)) >> |
| 1826 | subpat: option!(tuple!(syn!(At), syn!(Pat))) >> |
| 1827 | (PatIdent { |
| 1828 | mode: match mode { |
| 1829 | Some(mode) => BindingMode::ByRef(mode, mutability), |
| 1830 | None => BindingMode::ByValue(mutability), |
| 1831 | }, |
| 1832 | ident: name, |
| 1833 | at_token: subpat.as_ref().map(|p| At((p.0).0)), |
| 1834 | subpat: subpat.map(|p| Box::new(p.1)), |
| 1835 | }) |
| 1836 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | impl Synom for PatTupleStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1840 | named!(parse -> Self, do_parse!( |
| 1841 | path: syn!(Path) >> |
| 1842 | tuple: syn!(PatTuple) >> |
| 1843 | (PatTupleStruct { |
| 1844 | path: path, |
| 1845 | pat: tuple, |
| 1846 | }) |
| 1847 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | impl Synom for PatStruct { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1851 | named!(parse -> Self, do_parse!( |
| 1852 | path: syn!(Path) >> |
| 1853 | data: braces!(do_parse!( |
| 1854 | fields: call!(Delimited::parse_terminated) >> |
| 1855 | base: option!( |
| 1856 | cond!(fields.is_empty() || fields.trailing_delim(), |
| 1857 | syn!(Dot2)) |
| 1858 | ) >> |
| 1859 | (fields, base) |
| 1860 | )) >> |
| 1861 | (PatStruct { |
| 1862 | path: path, |
| 1863 | fields: (data.0).0, |
| 1864 | brace_token: data.1, |
| 1865 | dot2_token: (data.0).1.and_then(|m| m), |
| 1866 | }) |
| 1867 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | impl Synom for FieldPat { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1871 | named!(parse -> Self, alt!( |
| 1872 | do_parse!( |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1873 | ident: field_ident >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1874 | colon: syn!(Colon) >> |
| 1875 | pat: syn!(Pat) >> |
| 1876 | (FieldPat { |
| 1877 | ident: ident, |
| 1878 | pat: Box::new(pat), |
| 1879 | is_shorthand: false, |
| 1880 | attrs: Vec::new(), |
| 1881 | colon_token: Some(colon), |
| 1882 | }) |
| 1883 | ) |
| 1884 | | |
| 1885 | do_parse!( |
| 1886 | boxed: option!(syn!(Box_)) >> |
| 1887 | mode: option!(syn!(Ref)) >> |
| 1888 | mutability: syn!(Mutability) >> |
| 1889 | ident: syn!(Ident) >> |
| 1890 | ({ |
| 1891 | let mut pat: Pat = PatIdent { |
| 1892 | mode: if let Some(mode) = mode { |
| 1893 | BindingMode::ByRef(mode, mutability) |
| 1894 | } else { |
| 1895 | BindingMode::ByValue(mutability) |
| 1896 | }, |
| 1897 | ident: ident.clone(), |
| 1898 | subpat: None, |
| 1899 | at_token: None, |
| 1900 | }.into(); |
| 1901 | if let Some(boxed) = boxed { |
| 1902 | pat = PatBox { |
| 1903 | pat: Box::new(pat), |
| 1904 | box_token: boxed, |
| 1905 | }.into(); |
| 1906 | } |
| 1907 | FieldPat { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1908 | ident: ident, |
| 1909 | pat: Box::new(pat), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1910 | is_shorthand: true, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1911 | attrs: Vec::new(), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1912 | colon_token: None, |
| 1913 | } |
| 1914 | }) |
| 1915 | ) |
| 1916 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1919 | named!(field_ident -> Ident, alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1920 | syn!(Ident) |
| 1921 | | |
| 1922 | do_parse!( |
| 1923 | lit: syn!(Lit) >> |
| 1924 | ({ |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 1925 | let s = lit.to_string(); |
| 1926 | if s.parse::<usize>().is_ok() { |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1927 | Ident::new(s.into(), lit.span) |
| 1928 | } else { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1929 | return parse_error(); |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1930 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1931 | }) |
| 1932 | ) |
| 1933 | )); |
| 1934 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1935 | impl Synom for PatPath { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1936 | named!(parse -> Self, map!( |
| 1937 | syn!(ExprPath), |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 1938 | |p| PatPath { qself: p.qself, path: p.path } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1939 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1940 | } |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1941 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1942 | impl Synom for PatTuple { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1943 | named!(parse -> Self, do_parse!( |
| 1944 | data: parens!(do_parse!( |
| 1945 | elems: call!(Delimited::parse_terminated) >> |
| 1946 | dotdot: map!(cond!( |
| 1947 | elems.is_empty() || elems.trailing_delim(), |
| 1948 | option!(do_parse!( |
| 1949 | dots: syn!(Dot2) >> |
| 1950 | trailing: option!(syn!(Comma)) >> |
| 1951 | (dots, trailing) |
| 1952 | )) |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 1953 | ), |x| x.and_then(|x| x)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1954 | rest: cond!(match dotdot { |
| 1955 | Some((_, Some(_))) => true, |
| 1956 | _ => false, |
| 1957 | }, |
| 1958 | call!(Delimited::parse_terminated)) >> |
| 1959 | (elems, dotdot, rest) |
| 1960 | )) >> |
| 1961 | ({ |
| 1962 | let ((mut elems, dotdot, rest), parens) = data; |
| 1963 | let (dotdot, trailing) = match dotdot { |
| 1964 | Some((a, b)) => (Some(a), Some(b)), |
| 1965 | None => (None, None), |
| 1966 | }; |
| 1967 | PatTuple { |
| 1968 | paren_token: parens, |
| 1969 | dots_pos: dotdot.as_ref().map(|_| elems.len()), |
| 1970 | dot2_token: dotdot, |
| 1971 | comma_token: trailing.and_then(|b| b), |
| 1972 | pats: { |
| 1973 | if let Some(rest) = rest { |
| 1974 | for elem in rest { |
| 1975 | elems.push(elem); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1976 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1977 | } |
| 1978 | elems |
| 1979 | }, |
| 1980 | } |
| 1981 | }) |
| 1982 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1983 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1984 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1985 | impl Synom for PatRef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1986 | named!(parse -> Self, do_parse!( |
| 1987 | and: syn!(And) >> |
| 1988 | mutability: syn!(Mutability) >> |
| 1989 | pat: syn!(Pat) >> |
| 1990 | (PatRef { |
| 1991 | pat: Box::new(pat), |
| 1992 | mutbl: mutability, |
| 1993 | and_token: and, |
| 1994 | }) |
| 1995 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1996 | } |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1997 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 1998 | impl Synom for PatLit { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 1999 | named!(parse -> Self, do_parse!( |
| 2000 | lit: pat_lit_expr >> |
| 2001 | (if let ExprKind::Path(_) = lit.node { |
| 2002 | return parse_error(); // these need to be parsed by pat_path |
| 2003 | } else { |
| 2004 | PatLit { |
| 2005 | expr: Box::new(lit), |
| 2006 | } |
| 2007 | }) |
| 2008 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2009 | } |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 2010 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2011 | impl Synom for PatRange { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2012 | named!(parse -> Self, do_parse!( |
| 2013 | lo: pat_lit_expr >> |
| 2014 | limits: syn!(RangeLimits) >> |
| 2015 | hi: pat_lit_expr >> |
| 2016 | (PatRange { |
| 2017 | lo: Box::new(lo), |
| 2018 | hi: Box::new(hi), |
| 2019 | limits: limits, |
| 2020 | }) |
| 2021 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2022 | } |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 2023 | |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2024 | named!(pat_lit_expr -> Expr, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2025 | neg: option!(syn!(Sub)) >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2026 | v: alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2027 | syn!(Lit) => { ExprKind::Lit } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2028 | | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2029 | syn!(ExprPath) => { ExprKind::Path } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 2030 | ) >> |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2031 | (if neg.is_some() { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2032 | ExprKind::Unary(ExprUnary { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2033 | op: UnOp::Neg(tokens::Sub::default()), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2034 | expr: Box::new(v.into()) |
| 2035 | }).into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2036 | } else { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2037 | v.into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 2038 | }) |
| 2039 | )); |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 2040 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2041 | impl Synom for PatSlice { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2042 | named!(parse -> Self, map!( |
| 2043 | brackets!(do_parse!( |
| 2044 | before: call!(Delimited::parse_terminated) >> |
| 2045 | middle: option!(do_parse!( |
| 2046 | dots: syn!(Dot2) >> |
| 2047 | trailing: option!(syn!(Comma)) >> |
| 2048 | (dots, trailing) |
| 2049 | )) >> |
| 2050 | after: cond!( |
| 2051 | match middle { |
| 2052 | Some((_, ref trailing)) => trailing.is_some(), |
| 2053 | _ => false, |
| 2054 | }, |
| 2055 | call!(Delimited::parse_terminated) |
| 2056 | ) >> |
| 2057 | (before, middle, after) |
| 2058 | )), |
| 2059 | |((before, middle, after), brackets)| { |
| 2060 | let mut before: Delimited<Pat, tokens::Comma> = before; |
| 2061 | let after: Option<Delimited<Pat, tokens::Comma>> = after; |
| 2062 | let middle: Option<(Dot2, Option<Comma>)> = middle; |
| 2063 | PatSlice { |
| 2064 | dot2_token: middle.as_ref().map(|m| Dot2((m.0).0)), |
| 2065 | comma_token: middle.as_ref().and_then(|m| { |
| 2066 | m.1.as_ref().map(|m| Comma(m.0)) |
| 2067 | }), |
| 2068 | bracket_token: brackets, |
| 2069 | middle: middle.and_then(|_| { |
| 2070 | if !before.is_empty() && !before.trailing_delim() { |
| 2071 | Some(Box::new(before.pop().unwrap().into_item())) |
| 2072 | } else { |
| 2073 | None |
| 2074 | } |
| 2075 | }), |
| 2076 | front: before, |
| 2077 | back: after.unwrap_or_default(), |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 2078 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2079 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2080 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2081 | } |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 2082 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2083 | impl Synom for CaptureBy { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 2084 | named!(parse -> Self, alt!( |
| 2085 | syn!(Move) => { CaptureBy::Value } |
| 2086 | | |
| 2087 | epsilon!() => { |_| CaptureBy::Ref } |
| 2088 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2089 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2092 | #[cfg(feature = "printing")] |
| 2093 | mod printing { |
| 2094 | use super::*; |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2095 | use attr::FilterAttrs; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2096 | use quote::{Tokens, ToTokens}; |
| 2097 | |
| 2098 | impl ToTokens for Expr { |
| 2099 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2100 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2101 | self.node.to_tokens(tokens) |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | impl ToTokens for ExprBox { |
| 2106 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2107 | self.box_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2108 | self.expr.to_tokens(tokens); |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | impl ToTokens for ExprInPlace { |
| 2113 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 6a5a164 | 2017-06-04 19:35:15 -0400 | [diff] [blame] | 2114 | match self.kind { |
| 2115 | InPlaceKind::Arrow(ref arrow) => { |
| 2116 | self.place.to_tokens(tokens); |
| 2117 | arrow.to_tokens(tokens); |
| 2118 | self.value.to_tokens(tokens); |
| 2119 | } |
| 2120 | InPlaceKind::In(ref _in) => { |
| 2121 | _in.to_tokens(tokens); |
| 2122 | self.place.to_tokens(tokens); |
| 2123 | self.value.to_tokens(tokens); |
| 2124 | } |
| 2125 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | impl ToTokens for ExprArray { |
| 2130 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2131 | self.bracket_token.surround(tokens, |tokens| { |
| 2132 | self.exprs.to_tokens(tokens); |
| 2133 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | impl ToTokens for ExprCall { |
| 2138 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2139 | self.func.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2140 | self.paren_token.surround(tokens, |tokens| { |
| 2141 | self.args.to_tokens(tokens); |
| 2142 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | impl ToTokens for ExprMethodCall { |
| 2147 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2148 | self.expr.to_tokens(tokens); |
| 2149 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2150 | self.method.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2151 | self.colon2_token.to_tokens(tokens); |
| 2152 | self.lt_token.to_tokens(tokens); |
| 2153 | self.typarams.to_tokens(tokens); |
| 2154 | self.gt_token.to_tokens(tokens); |
| 2155 | self.paren_token.surround(tokens, |tokens| { |
| 2156 | self.args.to_tokens(tokens); |
| 2157 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | impl ToTokens for ExprTup { |
| 2162 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2163 | self.paren_token.surround(tokens, |tokens| { |
| 2164 | self.args.to_tokens(tokens); |
| 2165 | self.lone_comma.to_tokens(tokens); |
| 2166 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | impl ToTokens for ExprBinary { |
| 2171 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2172 | self.left.to_tokens(tokens); |
| 2173 | self.op.to_tokens(tokens); |
| 2174 | self.right.to_tokens(tokens); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | impl ToTokens for ExprUnary { |
| 2179 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2180 | self.op.to_tokens(tokens); |
| 2181 | self.expr.to_tokens(tokens); |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | impl ToTokens for ExprCast { |
| 2186 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2187 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2188 | self.as_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2189 | self.ty.to_tokens(tokens); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | impl ToTokens for ExprType { |
| 2194 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2195 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2196 | self.colon_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2197 | self.ty.to_tokens(tokens); |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | impl ToTokens for ExprIf { |
| 2202 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2203 | self.if_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2204 | self.cond.to_tokens(tokens); |
| 2205 | self.if_true.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2206 | self.else_token.to_tokens(tokens); |
| 2207 | self.if_false.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | impl ToTokens for ExprIfLet { |
| 2212 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2213 | self.if_token.to_tokens(tokens); |
| 2214 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2215 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2216 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2217 | self.expr.to_tokens(tokens); |
| 2218 | self.if_true.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2219 | self.else_token.to_tokens(tokens); |
| 2220 | self.if_false.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | impl ToTokens for ExprWhile { |
| 2225 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2226 | self.label.to_tokens(tokens); |
| 2227 | self.colon_token.to_tokens(tokens); |
| 2228 | self.while_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2229 | self.cond.to_tokens(tokens); |
| 2230 | self.body.to_tokens(tokens); |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | impl ToTokens for ExprWhileLet { |
| 2235 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2236 | self.label.to_tokens(tokens); |
| 2237 | self.colon_token.to_tokens(tokens); |
| 2238 | self.while_token.to_tokens(tokens); |
| 2239 | self.let_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2240 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2241 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2242 | self.expr.to_tokens(tokens); |
| 2243 | self.body.to_tokens(tokens); |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | impl ToTokens for ExprForLoop { |
| 2248 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2249 | self.label.to_tokens(tokens); |
| 2250 | self.colon_token.to_tokens(tokens); |
| 2251 | self.for_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2252 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2253 | self.in_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2254 | self.expr.to_tokens(tokens); |
| 2255 | self.body.to_tokens(tokens); |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | impl ToTokens for ExprLoop { |
| 2260 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2261 | self.label.to_tokens(tokens); |
| 2262 | self.colon_token.to_tokens(tokens); |
| 2263 | self.loop_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2264 | self.body.to_tokens(tokens); |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | impl ToTokens for ExprMatch { |
| 2269 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2270 | self.match_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2271 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2272 | self.brace_token.surround(tokens, |tokens| { |
| 2273 | tokens.append_all(&self.arms); |
| 2274 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | impl ToTokens for ExprCatch { |
| 2279 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2280 | self.do_token.to_tokens(tokens); |
| 2281 | self.catch_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2282 | self.block.to_tokens(tokens); |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | impl ToTokens for ExprClosure { |
| 2287 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2288 | self.capture.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2289 | self.or1_token.to_tokens(tokens); |
| 2290 | for item in self.decl.inputs.iter() { |
| 2291 | match **item.item() { |
| 2292 | FnArg::Captured(ArgCaptured { ref pat, ty: Ty::Infer(_), .. }) => { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2293 | pat.to_tokens(tokens); |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 2294 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2295 | _ => item.item().to_tokens(tokens), |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 2296 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2297 | item.delimiter().to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2298 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2299 | self.or2_token.to_tokens(tokens); |
| 2300 | self.decl.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2301 | self.body.to_tokens(tokens); |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | impl ToTokens for ExprBlock { |
| 2306 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2307 | self.unsafety.to_tokens(tokens); |
| 2308 | self.block.to_tokens(tokens); |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | impl ToTokens for ExprAssign { |
| 2313 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2314 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2315 | self.eq_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2316 | self.right.to_tokens(tokens); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | impl ToTokens for ExprAssignOp { |
| 2321 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2322 | self.left.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2323 | self.op.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2324 | self.right.to_tokens(tokens); |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | impl ToTokens for ExprField { |
| 2329 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2330 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2331 | self.dot_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2332 | self.field.to_tokens(tokens); |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | impl ToTokens for ExprTupField { |
| 2337 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2338 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2339 | self.dot_token.to_tokens(tokens); |
| 2340 | self.field.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | impl ToTokens for ExprIndex { |
| 2345 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2346 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2347 | self.bracket_token.surround(tokens, |tokens| { |
| 2348 | self.index.to_tokens(tokens); |
| 2349 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | impl ToTokens for ExprRange { |
| 2354 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2355 | self.from.to_tokens(tokens); |
| 2356 | self.limits.to_tokens(tokens); |
| 2357 | self.to.to_tokens(tokens); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | impl ToTokens for ExprPath { |
| 2362 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2363 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | impl ToTokens for ExprAddrOf { |
| 2368 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2369 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2370 | self.mutbl.to_tokens(tokens); |
| 2371 | self.expr.to_tokens(tokens); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | impl ToTokens for ExprBreak { |
| 2376 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2377 | self.break_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2378 | self.label.to_tokens(tokens); |
| 2379 | self.expr.to_tokens(tokens); |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | impl ToTokens for ExprContinue { |
| 2384 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2385 | self.continue_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2386 | self.label.to_tokens(tokens); |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | impl ToTokens for ExprRet { |
| 2391 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2392 | self.return_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2393 | self.expr.to_tokens(tokens); |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | impl ToTokens for ExprStruct { |
| 2398 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2399 | self.path.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2400 | self.brace_token.surround(tokens, |tokens| { |
| 2401 | self.fields.to_tokens(tokens); |
| 2402 | self.dot2_token.to_tokens(tokens); |
| 2403 | self.rest.to_tokens(tokens); |
| 2404 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | impl ToTokens for ExprRepeat { |
| 2409 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2410 | self.bracket_token.surround(tokens, |tokens| { |
| 2411 | self.expr.to_tokens(tokens); |
| 2412 | self.semi_token.to_tokens(tokens); |
| 2413 | self.amt.to_tokens(tokens); |
| 2414 | }) |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2415 | } |
| 2416 | } |
| 2417 | |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 2418 | impl ToTokens for ExprGroup { |
| 2419 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2420 | self.group_token.surround(tokens, |tokens| { |
| 2421 | self.expr.to_tokens(tokens); |
| 2422 | }); |
| 2423 | } |
| 2424 | } |
| 2425 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2426 | impl ToTokens for ExprParen { |
| 2427 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2428 | self.paren_token.surround(tokens, |tokens| { |
| 2429 | self.expr.to_tokens(tokens); |
| 2430 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | impl ToTokens for ExprTry { |
| 2435 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2436 | self.expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2437 | self.question_token.to_tokens(tokens); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2438 | } |
| 2439 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2440 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2441 | impl ToTokens for FieldValue { |
| 2442 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2443 | self.ident.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 2444 | if !self.is_shorthand { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2445 | self.colon_token.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 2446 | self.expr.to_tokens(tokens); |
| 2447 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2451 | impl ToTokens for Arm { |
| 2452 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2453 | tokens.append_all(&self.attrs); |
| 2454 | self.pats.to_tokens(tokens); |
| 2455 | self.if_token.to_tokens(tokens); |
| 2456 | self.guard.to_tokens(tokens); |
| 2457 | self.rocket_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2458 | self.body.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2459 | self.comma.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2463 | impl ToTokens for PatWild { |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2464 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2465 | self.underscore_token.to_tokens(tokens); |
| 2466 | } |
| 2467 | } |
| 2468 | |
| 2469 | impl ToTokens for PatIdent { |
| 2470 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2471 | self.mode.to_tokens(tokens); |
| 2472 | self.ident.to_tokens(tokens); |
| 2473 | self.at_token.to_tokens(tokens); |
| 2474 | self.subpat.to_tokens(tokens); |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | impl ToTokens for PatStruct { |
| 2479 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2480 | self.path.to_tokens(tokens); |
| 2481 | self.brace_token.surround(tokens, |tokens| { |
| 2482 | self.fields.to_tokens(tokens); |
| 2483 | self.dot2_token.to_tokens(tokens); |
| 2484 | }); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | impl ToTokens for PatTupleStruct { |
| 2489 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2490 | self.path.to_tokens(tokens); |
| 2491 | self.pat.to_tokens(tokens); |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | impl ToTokens for PatPath { |
| 2496 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2497 | ::PathTokens(&self.qself, &self.path).to_tokens(tokens); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | impl ToTokens for PatTuple { |
| 2502 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2503 | self.paren_token.surround(tokens, |tokens| { |
| 2504 | for (i, token) in self.pats.iter().enumerate() { |
| 2505 | if Some(i) == self.dots_pos { |
| 2506 | self.dot2_token.to_tokens(tokens); |
| 2507 | self.comma_token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2508 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2509 | token.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2510 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2511 | |
| 2512 | if Some(self.pats.len()) == self.dots_pos { |
| 2513 | self.dot2_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2514 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2515 | }); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | impl ToTokens for PatBox { |
| 2520 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2521 | self.box_token.to_tokens(tokens); |
| 2522 | self.pat.to_tokens(tokens); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | impl ToTokens for PatRef { |
| 2527 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2528 | self.and_token.to_tokens(tokens); |
| 2529 | self.mutbl.to_tokens(tokens); |
| 2530 | self.pat.to_tokens(tokens); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | impl ToTokens for PatLit { |
| 2535 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2536 | self.expr.to_tokens(tokens); |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | impl ToTokens for PatRange { |
| 2541 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2542 | self.lo.to_tokens(tokens); |
| 2543 | self.limits.to_tokens(tokens); |
| 2544 | self.hi.to_tokens(tokens); |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | impl ToTokens for PatSlice { |
| 2549 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2550 | self.bracket_token.surround(tokens, |tokens| { |
| 2551 | self.front.to_tokens(tokens); |
| 2552 | self.middle.to_tokens(tokens); |
| 2553 | self.dot2_token.to_tokens(tokens); |
| 2554 | self.comma_token.to_tokens(tokens); |
| 2555 | self.back.to_tokens(tokens); |
| 2556 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
Arnavion | 1992e2f | 2017-04-25 01:47:46 -0700 | [diff] [blame] | 2560 | impl ToTokens for RangeLimits { |
| 2561 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2562 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2563 | RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), |
| 2564 | RangeLimits::Closed(ref t) => t.to_tokens(tokens), |
Arnavion | 1992e2f | 2017-04-25 01:47:46 -0700 | [diff] [blame] | 2565 | } |
| 2566 | } |
| 2567 | } |
| 2568 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2569 | impl ToTokens for FieldPat { |
| 2570 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2571 | if !self.is_shorthand { |
| 2572 | self.ident.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2573 | self.colon_token.to_tokens(tokens); |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 2574 | } |
| 2575 | self.pat.to_tokens(tokens); |
| 2576 | } |
| 2577 | } |
| 2578 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2579 | impl ToTokens for BindingMode { |
| 2580 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2581 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2582 | BindingMode::ByRef(ref t, ref m) => { |
| 2583 | t.to_tokens(tokens); |
| 2584 | m.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2585 | } |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2586 | BindingMode::ByValue(ref m) => { |
| 2587 | m.to_tokens(tokens); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | } |
| 2591 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2592 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2593 | impl ToTokens for CaptureBy { |
| 2594 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2595 | match *self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2596 | CaptureBy::Value(ref t) => t.to_tokens(tokens), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 2597 | CaptureBy::Ref => { |
| 2598 | // nothing |
| 2599 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2604 | impl ToTokens for Block { |
| 2605 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2606 | self.brace_token.surround(tokens, |tokens| { |
| 2607 | tokens.append_all(&self.stmts); |
| 2608 | }); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2609 | } |
| 2610 | } |
| 2611 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2612 | impl ToTokens for Stmt { |
| 2613 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 2614 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2615 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2616 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 2617 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2618 | Stmt::Semi(ref expr, ref semi) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2619 | expr.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2620 | semi.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2621 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2622 | Stmt::Mac(ref mac) => { |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 2623 | let (ref mac, ref style, ref attrs) = **mac; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 2624 | tokens.append_all(attrs.outer()); |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2625 | mac.to_tokens(tokens); |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 2626 | match *style { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2627 | MacStmtStyle::Semicolon(ref s) => s.to_tokens(tokens), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 2628 | MacStmtStyle::Braces | MacStmtStyle::NoBraces => { |
| 2629 | // no semicolon |
| 2630 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 2631 | } |
| 2632 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2636 | |
| 2637 | impl ToTokens for Local { |
| 2638 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4e3158d | 2016-10-30 00:30:01 -0700 | [diff] [blame] | 2639 | tokens.append_all(self.attrs.outer()); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2640 | self.let_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2641 | self.pat.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 2642 | self.colon_token.to_tokens(tokens); |
| 2643 | self.ty.to_tokens(tokens); |
| 2644 | self.eq_token.to_tokens(tokens); |
| 2645 | self.init.to_tokens(tokens); |
| 2646 | self.semi_token.to_tokens(tokens); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 2647 | } |
| 2648 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 2649 | } |