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