David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 3 | /// An expression. |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 4 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 5 | pub struct Expr { |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 6 | /// Type of the expression. |
David Tolnay | 12dcd62 | 2016-11-10 13:06:41 -0800 | [diff] [blame] | 7 | pub node: ExprKind, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 8 | |
| 9 | /// Attributes tagged on the expression. |
David Tolnay | 12dcd62 | 2016-11-10 13:06:41 -0800 | [diff] [blame] | 10 | pub attrs: Vec<Attribute>, |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | impl From<ExprKind> for Expr { |
| 14 | fn from(node: ExprKind) -> Expr { |
| 15 | Expr { |
| 16 | node: node, |
| 17 | attrs: Vec::new(), |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 22 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 23 | pub enum ExprKind { |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 24 | /// A `box x` expression. |
| 25 | Box(Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 26 | |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 27 | /// First expr is the place; second expr is the value. |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 28 | /// |
| 29 | /// E.g. 'plae <- val'. |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 30 | InPlace(Box<Expr>, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 31 | |
| 32 | /// An array, e.g. `[a, b, c, d]`. |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 33 | Array(Vec<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 34 | |
| 35 | /// A function call. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 36 | /// |
| 37 | /// The first field resolves to the function itself, |
| 38 | /// and the second field is the list of arguments |
| 39 | Call(Box<Expr>, Vec<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 40 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 41 | /// A method call (`x.foo::<Bar, Baz>(a, b, c, d)`) |
| 42 | /// |
| 43 | /// The `Ident` is the identifier for the method name. |
| 44 | /// The vector of `Ty`s are the ascripted type parameters for the method |
| 45 | /// (within the angle brackets). |
| 46 | /// |
| 47 | /// The first element of the vector of `Expr`s is the expression that evaluates |
| 48 | /// to the object on which the method is being called on (the receiver), |
| 49 | /// and the remaining elements are the rest of the arguments. |
| 50 | /// |
| 51 | /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as |
| 52 | /// `ExprKind::MethodCall(foo, [Bar, Baz], [x, a, b, c, d])`. |
| 53 | MethodCall(Ident, Vec<Ty>, Vec<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 54 | |
| 55 | /// A tuple, e.g. `(a, b, c, d)`. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 56 | Tup(Vec<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 57 | |
| 58 | /// A binary operation, e.g. `a + b`, `a * b`. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 59 | Binary(BinOp, Box<Expr>, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 60 | |
| 61 | /// A unary operation, e.g. `!x`, `*x`. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 62 | Unary(UnOp, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 63 | |
| 64 | /// A literal, e.g. `1`, `"foo"`. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 65 | Lit(Lit), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 66 | |
| 67 | /// A cast, e.g. `foo as f64`. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 68 | Cast(Box<Expr>, Box<Ty>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 69 | |
| 70 | /// A type ascription, e.g. `foo: f64`. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 71 | Type(Box<Expr>, Box<Ty>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 72 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 73 | /// An `if` block, with an optional else block |
| 74 | /// |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 75 | /// E.g., `if expr { block } else { expr }` |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 76 | If(Box<Expr>, Block, Option<Box<Expr>>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 77 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 78 | /// An `if let` expression with an optional else block |
| 79 | /// |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 80 | /// E.g., `if let pat = expr { block } else { expr }` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 81 | /// |
| 82 | /// This is desugared to a `match` expression. |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 83 | IfLet(Box<Pat>, Box<Expr>, Block, Option<Box<Expr>>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 84 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 85 | /// A while loop, with an optional label |
| 86 | /// |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 87 | /// E.g., `'label: while expr { block }` |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 88 | While(Box<Expr>, Block, Option<Ident>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 89 | |
| 90 | /// A while-let loop, with an optional label. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 91 | /// |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 92 | /// E.g., `'label: while let pat = expr { block }` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 93 | /// |
| 94 | /// This is desugared to a combination of `loop` and `match` expressions. |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 95 | WhileLet(Box<Pat>, Box<Expr>, Block, Option<Ident>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 96 | |
| 97 | /// A for loop, with an optional label. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 98 | /// |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 99 | /// E.g., `'label: for pat in expr { block }` |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 100 | /// |
| 101 | /// This is desugared to a combination of `loop` and `match` expressions. |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 102 | ForLoop(Box<Pat>, Box<Expr>, Block, Option<Ident>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 103 | |
| 104 | /// Conditionless loop with an optional label. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 105 | /// |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 106 | /// E.g. `'label: loop { block }` |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 107 | Loop(Block, Option<Ident>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 108 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 109 | /// A `match` block. |
| 110 | Match(Box<Expr>, Vec<Arm>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 111 | |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 112 | /// A closure (for example, `move |a, b, c| a + b + c`) |
| 113 | Closure(CaptureBy, Box<FnDecl>, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 114 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 115 | /// A block (`{ ... }` or `unsafe { ... }`) |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 116 | Block(Unsafety, Block), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 117 | |
| 118 | /// An assignment (`a = foo()`) |
| 119 | Assign(Box<Expr>, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 120 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 121 | /// An assignment with an operator |
| 122 | /// |
| 123 | /// For example, `a += 1`. |
| 124 | AssignOp(BinOp, Box<Expr>, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 125 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 126 | /// Access of a named struct field (`obj.foo`) |
| 127 | Field(Box<Expr>, Ident), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 128 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 129 | /// Access of an unnamed field of a struct or tuple-struct |
| 130 | /// |
| 131 | /// For example, `foo.0`. |
| 132 | TupField(Box<Expr>, usize), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 133 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 134 | /// An indexing operation (`foo[2]`) |
| 135 | Index(Box<Expr>, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 136 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 137 | /// A range (`1..2`, `1..`, `..2`, `1...2`, `1...`, `...2`) |
| 138 | Range(Option<Box<Expr>>, Option<Box<Expr>>, RangeLimits), |
| 139 | |
| 140 | /// Variable reference, possibly containing `::` and/or type |
| 141 | /// parameters, e.g. foo::bar::<baz>. |
| 142 | /// |
| 143 | /// Optionally "qualified", |
| 144 | /// E.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 145 | Path(Option<QSelf>, Path), |
| 146 | |
| 147 | /// A referencing operation (`&a` or `&mut a`) |
| 148 | AddrOf(Mutability, Box<Expr>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 149 | |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 150 | /// A `break`, with an optional label to break, and an optional expression |
| 151 | Break(Option<Ident>, Option<Box<Expr>>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 152 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 153 | /// A `continue`, with an optional label |
| 154 | Continue(Option<Ident>), |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 155 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 156 | /// A `return`, with an optional value to be returned |
| 157 | Ret(Option<Box<Expr>>), |
| 158 | |
| 159 | /// A macro invocation; pre-expansion |
| 160 | Mac(Mac), |
| 161 | |
| 162 | /// A struct literal expression. |
| 163 | /// |
| 164 | /// For example, `Foo {x: 1, y: 2}`, or |
| 165 | /// `Foo {x: 1, .. base}`, where `base` is the `Option<Expr>`. |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 166 | Struct(Path, Vec<FieldValue>, Option<Box<Expr>>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 167 | |
| 168 | /// An array literal constructed from one repeated element. |
| 169 | /// |
| 170 | /// For example, `[1; 5]`. The first expression is the element |
| 171 | /// to be repeated; the second is the number of times to repeat it. |
| 172 | Repeat(Box<Expr>, Box<Expr>), |
| 173 | |
| 174 | /// No-op: used solely so we can pretty-print faithfully |
| 175 | Paren(Box<Expr>), |
| 176 | |
| 177 | /// `expr?` |
| 178 | Try(Box<Expr>), |
| 179 | } |
| 180 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 181 | /// A field-value pair in a struct literal. |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 182 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 183 | pub struct FieldValue { |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 184 | /// Name of the field. |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 185 | pub ident: Ident, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 186 | |
| 187 | /// Value of the field. |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 188 | pub expr: Expr, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 189 | |
| 190 | /// Whether this is a shorthand field, e.g. `Struct { x }` |
| 191 | /// instead of `Struct { x: x }`. |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 192 | pub is_shorthand: bool, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 193 | |
| 194 | /// Attributes tagged on the field. |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 195 | pub attrs: Vec<Attribute>, |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 196 | } |
| 197 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 198 | /// A Block (`{ .. }`). |
| 199 | /// |
| 200 | /// E.g. `{ .. }` as in `fn foo() { .. }` |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 201 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 202 | pub struct Block { |
| 203 | /// Statements in a block |
| 204 | pub stmts: Vec<Stmt>, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 207 | /// A statement, usually ending in a semicolon. |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 208 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 209 | pub enum Stmt { |
| 210 | /// A local (let) binding. |
| 211 | Local(Box<Local>), |
| 212 | |
| 213 | /// An item definition. |
| 214 | Item(Box<Item>), |
| 215 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 216 | /// Expr without trailing semicolon. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 217 | Expr(Box<Expr>), |
| 218 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 219 | /// Expression with trailing semicolon; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 220 | Semi(Box<Expr>), |
| 221 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 222 | /// Macro invocation. |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 223 | Mac(Box<(Mac, MacStmtStyle, Vec<Attribute>)>), |
| 224 | } |
| 225 | |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 226 | /// How a macro was invoked. |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 227 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 228 | pub enum MacStmtStyle { |
| 229 | /// The macro statement had a trailing semicolon, e.g. `foo! { ... };` |
| 230 | /// `foo!(...);`, `foo![...];` |
| 231 | Semicolon, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 232 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 233 | /// The macro statement had braces; e.g. foo! { ... } |
| 234 | Braces, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 235 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 236 | /// The macro statement had parentheses or brackets and no semicolon; e.g. |
| 237 | /// `foo!(...)`. All of these will end up being converted into macro |
| 238 | /// expressions. |
| 239 | NoBraces, |
| 240 | } |
| 241 | |
| 242 | /// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;` |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 243 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 244 | pub struct Local { |
| 245 | pub pat: Box<Pat>, |
| 246 | pub ty: Option<Box<Ty>>, |
Clar Charr | d22b570 | 2017-03-10 15:24:56 -0500 | [diff] [blame^] | 247 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 248 | /// Initializer expression to set the value, if any |
| 249 | pub init: Option<Box<Expr>>, |
| 250 | pub attrs: Vec<Attribute>, |
| 251 | } |
| 252 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 253 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | 58f6f67 | 2016-10-19 08:44:25 -0700 | [diff] [blame] | 254 | // Clippy false positive |
| 255 | // https://github.com/Manishearth/rust-clippy/issues/1241 |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 256 | #[cfg_attr(feature = "cargo-clippy", allow(enum_variant_names))] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 257 | pub enum Pat { |
| 258 | /// Represents a wildcard pattern (`_`) |
| 259 | Wild, |
| 260 | |
David Tolnay | 432afc0 | 2016-09-24 07:37:13 -0700 | [diff] [blame] | 261 | /// A `Pat::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 262 | /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third |
| 263 | /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens |
| 264 | /// during name resolution. |
| 265 | Ident(BindingMode, Ident, Option<Box<Pat>>), |
| 266 | |
| 267 | /// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`. |
| 268 | /// The `bool` is `true` in the presence of a `..`. |
| 269 | Struct(Path, Vec<FieldPat>, bool), |
| 270 | |
| 271 | /// A tuple struct/variant pattern `Variant(x, y, .., z)`. |
| 272 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 273 | /// 0 <= position <= subpats.len() |
| 274 | TupleStruct(Path, Vec<Pat>, Option<usize>), |
| 275 | |
| 276 | /// A possibly qualified path pattern. |
| 277 | /// Unquailfied path patterns `A::B::C` can legally refer to variants, structs, constants |
| 278 | /// or associated constants. Quailfied path patterns `<A>::B::C`/`<A as Trait>::B::C` can |
| 279 | /// only legally refer to associated constants. |
| 280 | Path(Option<QSelf>, Path), |
| 281 | |
| 282 | /// A tuple pattern `(a, b)`. |
| 283 | /// If the `..` pattern fragment is present, then `Option<usize>` denotes its position. |
| 284 | /// 0 <= position <= subpats.len() |
| 285 | Tuple(Vec<Pat>, Option<usize>), |
| 286 | /// A `box` pattern |
| 287 | Box(Box<Pat>), |
| 288 | /// A reference pattern, e.g. `&mut (a, b)` |
| 289 | Ref(Box<Pat>, Mutability), |
| 290 | /// A literal |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 291 | Lit(Box<Expr>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 292 | /// A range pattern, e.g. `1...2` |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 293 | Range(Box<Expr>, Box<Expr>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 294 | /// `[a, b, ..i, y, z]` is represented as: |
David Tolnay | 16709ba | 2016-10-05 23:11:32 -0700 | [diff] [blame] | 295 | /// `Pat::Slice(box [a, b], Some(i), box [y, z])` |
| 296 | Slice(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 297 | /// A macro pattern; pre-expansion |
| 298 | Mac(Mac), |
| 299 | } |
| 300 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 301 | /// An arm of a 'match'. |
| 302 | /// |
| 303 | /// E.g. `0...10 => { println!("match!") }` as in |
| 304 | /// |
| 305 | /// ```rust,ignore |
| 306 | /// match n { |
| 307 | /// 0...10 => { println!("match!") }, |
| 308 | /// // .. |
| 309 | /// } |
| 310 | /// ``` |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 311 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 312 | pub struct Arm { |
| 313 | pub attrs: Vec<Attribute>, |
| 314 | pub pats: Vec<Pat>, |
| 315 | pub guard: Option<Box<Expr>>, |
| 316 | pub body: Box<Expr>, |
| 317 | } |
| 318 | |
| 319 | /// A capture clause |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 320 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 321 | pub enum CaptureBy { |
| 322 | Value, |
| 323 | Ref, |
| 324 | } |
| 325 | |
| 326 | /// Limit types of a range (inclusive or exclusive) |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 327 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 328 | pub enum RangeLimits { |
| 329 | /// Inclusive at the beginning, exclusive at the end |
| 330 | HalfOpen, |
| 331 | /// Inclusive at the beginning and end |
| 332 | Closed, |
| 333 | } |
| 334 | |
| 335 | /// A single field in a struct pattern |
| 336 | /// |
| 337 | /// Patterns like the fields of Foo `{ x, ref y, ref mut z }` |
David Tolnay | 181bac5 | 2016-09-24 00:10:05 -0700 | [diff] [blame] | 338 | /// are treated the same as `x: x, y: ref y, z: ref mut z`, |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 339 | /// except `is_shorthand` is true |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 340 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 341 | pub struct FieldPat { |
| 342 | /// The identifier for the field |
| 343 | pub ident: Ident, |
| 344 | /// The pattern the field is destructured to |
| 345 | pub pat: Box<Pat>, |
| 346 | pub is_shorthand: bool, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 347 | pub attrs: Vec<Attribute>, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 348 | } |
| 349 | |
David Tolnay | 9bf4af8 | 2017-01-07 11:17:46 -0800 | [diff] [blame] | 350 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 351 | pub enum BindingMode { |
| 352 | ByRef(Mutability), |
| 353 | ByValue(Mutability), |
| 354 | } |
| 355 | |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 356 | #[cfg(feature = "parsing")] |
| 357 | pub mod parsing { |
| 358 | use super::*; |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 359 | use {BinOp, Delimited, DelimToken, FnArg, FnDecl, FunctionRetTy, Ident, Lifetime, Mac, |
David Tolnay | 87d4e15 | 2016-12-22 19:06:37 -0500 | [diff] [blame] | 360 | TokenTree, Ty, UnOp, Unsafety}; |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 361 | use attr::parsing::outer_attr; |
Gregory Katz | 1b69f68 | 2016-09-27 21:06:09 -0400 | [diff] [blame] | 362 | use generics::parsing::lifetime; |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 363 | use ident::parsing::{ident, wordlike}; |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 364 | use item::parsing::item; |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 365 | use lit::parsing::{digits, lit}; |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 366 | use mac::parsing::{mac, token_trees}; |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 367 | use synom::IResult::{self, Error}; |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 368 | use op::parsing::{assign_op, binop, unop}; |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 369 | use ty::parsing::{mutability, path, qpath, ty, unsafety}; |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 370 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 371 | // Struct literals are ambiguous in certain positions |
| 372 | // https://github.com/rust-lang/rfcs/pull/92 |
| 373 | macro_rules! named_ambiguous_expr { |
| 374 | ($name:ident -> $o:ty, $allow_struct:ident, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 375 | fn $name(i: &str, $allow_struct: bool) -> $crate::synom::IResult<&str, $o> { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 376 | $submac!(i, $($args)*) |
| 377 | } |
| 378 | }; |
| 379 | } |
| 380 | |
| 381 | macro_rules! ambiguous_expr { |
| 382 | ($i:expr, $allow_struct:ident) => { |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 383 | ambiguous_expr($i, $allow_struct, true) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 384 | }; |
| 385 | } |
| 386 | |
| 387 | named!(pub expr -> Expr, ambiguous_expr!(true)); |
| 388 | |
| 389 | named!(expr_no_struct -> Expr, ambiguous_expr!(false)); |
| 390 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 391 | #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 392 | fn ambiguous_expr(i: &str, allow_struct: bool, allow_block: bool) -> IResult<&str, Expr> { |
| 393 | do_parse!( |
| 394 | i, |
| 395 | mut e: alt!( |
| 396 | expr_lit // must be before expr_struct |
| 397 | | |
| 398 | cond_reduce!(allow_struct, expr_struct) // must be before expr_path |
| 399 | | |
| 400 | expr_paren // must be before expr_tup |
| 401 | | |
| 402 | expr_mac // must be before expr_path |
| 403 | | |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 404 | call!(expr_break, allow_struct) // must be before expr_path |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 405 | | |
| 406 | expr_continue // must be before expr_path |
| 407 | | |
| 408 | call!(expr_ret, allow_struct) // must be before expr_path |
| 409 | | |
| 410 | call!(expr_box, allow_struct) |
| 411 | | |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 412 | expr_in_place |
| 413 | | |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 414 | expr_array |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 415 | | |
| 416 | expr_tup |
| 417 | | |
| 418 | call!(expr_unary, allow_struct) |
| 419 | | |
| 420 | expr_if |
| 421 | | |
| 422 | expr_while |
| 423 | | |
| 424 | expr_for_loop |
| 425 | | |
| 426 | expr_loop |
| 427 | | |
| 428 | expr_match |
| 429 | | |
| 430 | call!(expr_closure, allow_struct) |
| 431 | | |
| 432 | cond_reduce!(allow_block, expr_block) |
| 433 | | |
| 434 | call!(expr_range, allow_struct) |
| 435 | | |
| 436 | expr_path |
| 437 | | |
| 438 | call!(expr_addr_of, allow_struct) |
| 439 | | |
| 440 | expr_repeat |
| 441 | ) >> |
| 442 | many0!(alt!( |
| 443 | tap!(args: and_call => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 444 | e = ExprKind::Call(Box::new(e.into()), args); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 445 | }) |
| 446 | | |
| 447 | tap!(more: and_method_call => { |
| 448 | let (method, ascript, mut args) = more; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 449 | args.insert(0, e.into()); |
| 450 | e = ExprKind::MethodCall(method, ascript, args); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 451 | }) |
| 452 | | |
| 453 | tap!(more: call!(and_binary, allow_struct) => { |
| 454 | let (op, other) = more; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 455 | e = ExprKind::Binary(op, Box::new(e.into()), Box::new(other)); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 456 | }) |
| 457 | | |
| 458 | tap!(ty: and_cast => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 459 | e = ExprKind::Cast(Box::new(e.into()), Box::new(ty)); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 460 | }) |
| 461 | | |
| 462 | tap!(ty: and_ascription => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 463 | e = ExprKind::Type(Box::new(e.into()), Box::new(ty)); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 464 | }) |
| 465 | | |
| 466 | tap!(v: call!(and_assign, allow_struct) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 467 | e = ExprKind::Assign(Box::new(e.into()), Box::new(v)); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 468 | }) |
| 469 | | |
| 470 | tap!(more: call!(and_assign_op, allow_struct) => { |
| 471 | let (op, v) = more; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 472 | e = ExprKind::AssignOp(op, Box::new(e.into()), Box::new(v)); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 473 | }) |
| 474 | | |
| 475 | tap!(field: and_field => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 476 | e = ExprKind::Field(Box::new(e.into()), field); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 477 | }) |
| 478 | | |
| 479 | tap!(field: and_tup_field => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 480 | e = ExprKind::TupField(Box::new(e.into()), field as usize); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 481 | }) |
| 482 | | |
| 483 | tap!(i: and_index => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 484 | e = ExprKind::Index(Box::new(e.into()), Box::new(i)); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 485 | }) |
| 486 | | |
| 487 | tap!(more: call!(and_range, allow_struct) => { |
| 488 | let (limits, hi) = more; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 489 | e = ExprKind::Range(Some(Box::new(e.into())), hi.map(Box::new), limits); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 490 | }) |
| 491 | | |
| 492 | tap!(_try: punct!("?") => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 493 | e = ExprKind::Try(Box::new(e.into())); |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 494 | }) |
| 495 | )) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 496 | (e.into()) |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 497 | ) |
| 498 | } |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 499 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 500 | named!(expr_mac -> ExprKind, map!(mac, ExprKind::Mac)); |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 501 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 502 | named!(expr_paren -> ExprKind, do_parse!( |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 503 | punct!("(") >> |
| 504 | e: expr >> |
| 505 | punct!(")") >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 506 | (ExprKind::Paren(Box::new(e))) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 507 | )); |
| 508 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 509 | named_ambiguous_expr!(expr_box -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 510 | keyword!("box") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 511 | inner: ambiguous_expr!(allow_struct) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 512 | (ExprKind::Box(Box::new(inner))) |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 513 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 514 | |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 515 | named!(expr_in_place -> ExprKind, do_parse!( |
| 516 | keyword!("in") >> |
| 517 | place: expr_no_struct >> |
| 518 | punct!("{") >> |
| 519 | value: within_block >> |
| 520 | punct!("}") >> |
| 521 | (ExprKind::InPlace( |
| 522 | Box::new(place), |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 523 | Box::new(ExprKind::Block(Unsafety::Normal, Block { |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 524 | stmts: value, |
| 525 | }).into()), |
| 526 | )) |
| 527 | )); |
| 528 | |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 529 | named!(expr_array -> ExprKind, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 530 | punct!("[") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 531 | elems: terminated_list!(punct!(","), expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 532 | punct!("]") >> |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 533 | (ExprKind::Array(elems)) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 534 | )); |
| 535 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 536 | named!(and_call -> Vec<Expr>, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 537 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 538 | args: terminated_list!(punct!(","), expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 539 | punct!(")") >> |
| 540 | (args) |
| 541 | )); |
| 542 | |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 543 | named!(and_method_call -> (Ident, Vec<Ty>, Vec<Expr>), do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 544 | punct!(".") >> |
| 545 | method: ident >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 546 | ascript: opt_vec!(preceded!( |
| 547 | punct!("::"), |
| 548 | delimited!( |
| 549 | punct!("<"), |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 550 | terminated_list!(punct!(","), ty), |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 551 | punct!(">") |
| 552 | ) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 553 | )) >> |
| 554 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 555 | args: terminated_list!(punct!(","), expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 556 | punct!(")") >> |
| 557 | (method, ascript, args) |
| 558 | )); |
| 559 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 560 | named!(expr_tup -> ExprKind, do_parse!( |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 561 | punct!("(") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 562 | elems: terminated_list!(punct!(","), expr) >> |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 563 | punct!(")") >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 564 | (ExprKind::Tup(elems)) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 565 | )); |
| 566 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 567 | named_ambiguous_expr!(and_binary -> (BinOp, Expr), allow_struct, tuple!( |
| 568 | binop, |
| 569 | ambiguous_expr!(allow_struct) |
| 570 | )); |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 571 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 572 | named_ambiguous_expr!(expr_unary -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 573 | operator: unop >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 574 | operand: ambiguous_expr!(allow_struct) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 575 | (ExprKind::Unary(operator, Box::new(operand))) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 576 | )); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 577 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 578 | named!(expr_lit -> ExprKind, map!(lit, ExprKind::Lit)); |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 579 | |
| 580 | named!(and_cast -> Ty, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 581 | keyword!("as") >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 582 | ty: ty >> |
| 583 | (ty) |
| 584 | )); |
| 585 | |
| 586 | named!(and_ascription -> Ty, preceded!(punct!(":"), ty)); |
| 587 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 588 | enum Cond { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 589 | Let(Pat, Expr), |
| 590 | Expr(Expr), |
| 591 | } |
| 592 | |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 593 | named!(cond -> Cond, alt!( |
| 594 | do_parse!( |
| 595 | keyword!("let") >> |
| 596 | pat: pat >> |
| 597 | punct!("=") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 598 | value: expr_no_struct >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 599 | (Cond::Let(pat, value)) |
| 600 | ) |
| 601 | | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 602 | map!(expr_no_struct, Cond::Expr) |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 603 | )); |
| 604 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 605 | named!(expr_if -> ExprKind, do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 606 | keyword!("if") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 607 | cond: cond >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 608 | punct!("{") >> |
| 609 | then_block: within_block >> |
| 610 | punct!("}") >> |
| 611 | else_block: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 612 | keyword!("else"), |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 613 | alt!( |
| 614 | expr_if |
| 615 | | |
| 616 | do_parse!( |
| 617 | punct!("{") >> |
| 618 | else_block: within_block >> |
| 619 | punct!("}") >> |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 620 | (ExprKind::Block(Unsafety::Normal, Block { |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 621 | stmts: else_block, |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 622 | }).into()) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 623 | ) |
| 624 | ) |
| 625 | )) >> |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 626 | (match cond { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 627 | Cond::Let(pat, expr) => ExprKind::IfLet( |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 628 | Box::new(pat), |
| 629 | Box::new(expr), |
| 630 | Block { |
| 631 | stmts: then_block, |
| 632 | }, |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 633 | else_block.map(|els| Box::new(els.into())), |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 634 | ), |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 635 | Cond::Expr(cond) => ExprKind::If( |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 636 | Box::new(cond), |
| 637 | Block { |
| 638 | stmts: then_block, |
| 639 | }, |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 640 | else_block.map(|els| Box::new(els.into())), |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 641 | ), |
| 642 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 643 | )); |
| 644 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 645 | named!(expr_for_loop -> ExprKind, do_parse!( |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 646 | lbl: option!(terminated!(label, punct!(":"))) >> |
| 647 | keyword!("for") >> |
| 648 | pat: pat >> |
| 649 | keyword!("in") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 650 | expr: expr_no_struct >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 651 | loop_block: block >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 652 | (ExprKind::ForLoop(Box::new(pat), Box::new(expr), loop_block, lbl)) |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 653 | )); |
| 654 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 655 | named!(expr_loop -> ExprKind, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 656 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 657 | keyword!("loop") >> |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 658 | loop_block: block >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 659 | (ExprKind::Loop(loop_block, lbl)) |
Gregory Katz | e5f3568 | 2016-09-27 14:20:55 -0400 | [diff] [blame] | 660 | )); |
| 661 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 662 | named!(expr_match -> ExprKind, do_parse!( |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 663 | keyword!("match") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 664 | obj: expr_no_struct >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 665 | punct!("{") >> |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 666 | mut arms: many0!(do_parse!( |
| 667 | arm: match_arm >> |
| 668 | cond!(arm_requires_comma(&arm), punct!(",")) >> |
| 669 | cond!(!arm_requires_comma(&arm), option!(punct!(","))) >> |
| 670 | (arm) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 671 | )) >> |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 672 | last_arm: option!(match_arm) >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 673 | punct!("}") >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 674 | (ExprKind::Match(Box::new(obj), { |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 675 | arms.extend(last_arm); |
| 676 | arms |
| 677 | })) |
| 678 | )); |
| 679 | |
| 680 | fn arm_requires_comma(arm: &Arm) -> bool { |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 681 | if let ExprKind::Block(Unsafety::Normal, _) = arm.body.node { |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 682 | false |
| 683 | } else { |
| 684 | true |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | named!(match_arm -> Arm, do_parse!( |
| 689 | attrs: many0!(outer_attr) >> |
| 690 | pats: separated_nonempty_list!(punct!("|"), pat) >> |
| 691 | guard: option!(preceded!(keyword!("if"), expr)) >> |
| 692 | punct!("=>") >> |
| 693 | body: alt!( |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 694 | map!(block, |blk| ExprKind::Block(Unsafety::Normal, blk).into()) |
David Tolnay | 1978c67 | 2016-10-27 22:05:52 -0700 | [diff] [blame] | 695 | | |
| 696 | expr |
| 697 | ) >> |
| 698 | (Arm { |
| 699 | attrs: attrs, |
| 700 | pats: pats, |
| 701 | guard: guard.map(Box::new), |
| 702 | body: Box::new(body), |
| 703 | }) |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 704 | )); |
| 705 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 706 | named_ambiguous_expr!(expr_closure -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 707 | capture: capture_by >> |
| 708 | punct!("|") >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 709 | inputs: terminated_list!(punct!(","), closure_arg) >> |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 710 | punct!("|") >> |
| 711 | ret_and_body: alt!( |
| 712 | do_parse!( |
| 713 | punct!("->") >> |
| 714 | ty: ty >> |
| 715 | body: block >> |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 716 | (FunctionRetTy::Ty(ty), ExprKind::Block(Unsafety::Normal, body).into()) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 717 | ) |
| 718 | | |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 719 | map!(ambiguous_expr!(allow_struct), |e| (FunctionRetTy::Default, e)) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 720 | ) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 721 | (ExprKind::Closure( |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 722 | capture, |
| 723 | Box::new(FnDecl { |
| 724 | inputs: inputs, |
| 725 | output: ret_and_body.0, |
David Tolnay | 292e600 | 2016-10-29 22:03:51 -0700 | [diff] [blame] | 726 | variadic: false, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 727 | }), |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 728 | Box::new(ret_and_body.1), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 729 | )) |
| 730 | )); |
| 731 | |
| 732 | named!(closure_arg -> FnArg, do_parse!( |
| 733 | pat: pat >> |
| 734 | ty: option!(preceded!(punct!(":"), ty)) >> |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 735 | (FnArg::Captured(pat, ty.unwrap_or(Ty::Infer))) |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 736 | )); |
| 737 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 738 | named!(expr_while -> ExprKind, do_parse!( |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 739 | lbl: option!(terminated!(label, punct!(":"))) >> |
David Tolnay | 57ffbde | 2016-09-30 09:38:04 -0700 | [diff] [blame] | 740 | keyword!("while") >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 741 | cond: cond >> |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 742 | while_block: block >> |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 743 | (match cond { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 744 | Cond::Let(pat, expr) => ExprKind::WhileLet( |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 745 | Box::new(pat), |
| 746 | Box::new(expr), |
| 747 | while_block, |
| 748 | lbl, |
| 749 | ), |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 750 | Cond::Expr(cond) => ExprKind::While( |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 751 | Box::new(cond), |
| 752 | while_block, |
| 753 | lbl, |
| 754 | ), |
| 755 | }) |
Gregory Katz | 3e562cc | 2016-09-28 18:33:02 -0400 | [diff] [blame] | 756 | )); |
| 757 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 758 | named!(expr_continue -> ExprKind, do_parse!( |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 759 | keyword!("continue") >> |
| 760 | lbl: option!(label) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 761 | (ExprKind::Continue(lbl)) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 762 | )); |
| 763 | |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 764 | named_ambiguous_expr!(expr_break -> ExprKind, allow_struct, do_parse!( |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 765 | keyword!("break") >> |
| 766 | lbl: option!(label) >> |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 767 | val: option!(call!(ambiguous_expr, allow_struct, false)) >> |
| 768 | (ExprKind::Break(lbl, val.map(Box::new))) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 769 | )); |
| 770 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 771 | named_ambiguous_expr!(expr_ret -> ExprKind, allow_struct, do_parse!( |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 772 | keyword!("return") >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 773 | ret_value: option!(ambiguous_expr!(allow_struct)) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 774 | (ExprKind::Ret(ret_value.map(Box::new))) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 775 | )); |
| 776 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 777 | named!(expr_struct -> ExprKind, do_parse!( |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 778 | path: path >> |
| 779 | punct!("{") >> |
| 780 | fields: separated_list!(punct!(","), field_value) >> |
| 781 | base: option!(do_parse!( |
| 782 | cond!(!fields.is_empty(), punct!(",")) >> |
| 783 | punct!("..") >> |
| 784 | base: expr >> |
| 785 | (base) |
| 786 | )) >> |
David Tolnay | f6c7440 | 2016-10-08 02:31:26 -0700 | [diff] [blame] | 787 | cond!(!fields.is_empty() && base.is_none(), option!(punct!(","))) >> |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 788 | punct!("}") >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 789 | (ExprKind::Struct(path, fields, base.map(Box::new))) |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 790 | )); |
| 791 | |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 792 | named!(field_value -> FieldValue, alt!( |
| 793 | do_parse!( |
| 794 | name: wordlike >> |
| 795 | punct!(":") >> |
| 796 | value: expr >> |
| 797 | (FieldValue { |
| 798 | ident: name, |
| 799 | expr: value, |
| 800 | is_shorthand: false, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 801 | attrs: Vec::new(), |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 802 | }) |
| 803 | ) |
| 804 | | |
| 805 | map!(ident, |name: Ident| FieldValue { |
| 806 | ident: name.clone(), |
| 807 | expr: ExprKind::Path(None, name.into()).into(), |
| 808 | is_shorthand: true, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 809 | attrs: Vec::new(), |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 810 | }) |
| 811 | )); |
| 812 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 813 | named!(expr_repeat -> ExprKind, do_parse!( |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 814 | punct!("[") >> |
| 815 | value: expr >> |
| 816 | punct!(";") >> |
| 817 | times: expr >> |
| 818 | punct!("]") >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 819 | (ExprKind::Repeat(Box::new(value), Box::new(times))) |
Gregory Katz | fd6935d | 2016-09-30 22:51:25 -0400 | [diff] [blame] | 820 | )); |
| 821 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 822 | named!(expr_block -> ExprKind, do_parse!( |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 823 | rules: unsafety >> |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 824 | b: block >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 825 | (ExprKind::Block(rules, Block { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 826 | stmts: b.stmts, |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 827 | })) |
| 828 | )); |
| 829 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 830 | named_ambiguous_expr!(expr_range -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 831 | limits: range_limits >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 832 | hi: option!(ambiguous_expr!(allow_struct)) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 833 | (ExprKind::Range(None, hi.map(Box::new), limits)) |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 834 | )); |
| 835 | |
| 836 | named!(range_limits -> RangeLimits, alt!( |
| 837 | punct!("...") => { |_| RangeLimits::Closed } |
| 838 | | |
| 839 | punct!("..") => { |_| RangeLimits::HalfOpen } |
| 840 | )); |
| 841 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 842 | named!(expr_path -> ExprKind, map!(qpath, |(qself, path)| ExprKind::Path(qself, path))); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 843 | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 844 | named_ambiguous_expr!(expr_addr_of -> ExprKind, allow_struct, do_parse!( |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 845 | punct!("&") >> |
| 846 | mutability: mutability >> |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 847 | expr: ambiguous_expr!(allow_struct) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 848 | (ExprKind::AddrOf(mutability, Box::new(expr))) |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 849 | )); |
| 850 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 851 | named_ambiguous_expr!(and_assign -> Expr, allow_struct, preceded!( |
| 852 | punct!("="), |
| 853 | ambiguous_expr!(allow_struct) |
| 854 | )); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 855 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 856 | named_ambiguous_expr!(and_assign_op -> (BinOp, Expr), allow_struct, tuple!( |
| 857 | assign_op, |
| 858 | ambiguous_expr!(allow_struct) |
| 859 | )); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 860 | |
| 861 | named!(and_field -> Ident, preceded!(punct!("."), ident)); |
| 862 | |
| 863 | named!(and_tup_field -> u64, preceded!(punct!("."), digits)); |
| 864 | |
| 865 | named!(and_index -> Expr, delimited!(punct!("["), expr, punct!("]"))); |
| 866 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 867 | named_ambiguous_expr!(and_range -> (RangeLimits, Option<Expr>), allow_struct, tuple!( |
| 868 | range_limits, |
David Tolnay | 54e854d | 2016-10-24 12:03:30 -0700 | [diff] [blame] | 869 | option!(call!(ambiguous_expr, allow_struct, false)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 870 | )); |
David Tolnay | 438c905 | 2016-10-07 23:24:48 -0700 | [diff] [blame] | 871 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 872 | named!(pub block -> Block, do_parse!( |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 873 | punct!("{") >> |
| 874 | stmts: within_block >> |
| 875 | punct!("}") >> |
| 876 | (Block { |
| 877 | stmts: stmts, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 878 | }) |
| 879 | )); |
| 880 | |
David Tolnay | 3b9783a | 2016-10-29 22:37:09 -0700 | [diff] [blame] | 881 | named!(pub within_block -> Vec<Stmt>, do_parse!( |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 882 | many0!(punct!(";")) >> |
David Tolnay | 1bf1913 | 2017-02-19 22:54:25 -0800 | [diff] [blame] | 883 | mut standalone: many0!(terminated!(stmt, many0!(punct!(";")))) >> |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 884 | last: option!(expr) >> |
| 885 | (match last { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 886 | None => standalone, |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 887 | Some(last) => { |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 888 | standalone.push(Stmt::Expr(Box::new(last))); |
| 889 | standalone |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 890 | } |
| 891 | }) |
| 892 | )); |
| 893 | |
David Tolnay | 1bf1913 | 2017-02-19 22:54:25 -0800 | [diff] [blame] | 894 | named!(pub stmt -> Stmt, alt!( |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 895 | stmt_mac |
| 896 | | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 897 | stmt_local |
| 898 | | |
| 899 | stmt_item |
| 900 | | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 901 | stmt_expr |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 902 | )); |
| 903 | |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 904 | named!(stmt_mac -> Stmt, do_parse!( |
| 905 | attrs: many0!(outer_attr) >> |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 906 | what: path >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 907 | punct!("!") >> |
| 908 | // Only parse braces here; paren and bracket will get parsed as |
| 909 | // expression statements |
| 910 | punct!("{") >> |
| 911 | tts: token_trees >> |
| 912 | punct!("}") >> |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 913 | semi: option!(punct!(";")) >> |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 914 | (Stmt::Mac(Box::new(( |
| 915 | Mac { |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 916 | path: what, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 917 | tts: vec![TokenTree::Delimited(Delimited { |
| 918 | delim: DelimToken::Brace, |
| 919 | tts: tts, |
| 920 | })], |
| 921 | }, |
David Tolnay | 60d4894 | 2016-10-30 14:34:52 -0700 | [diff] [blame] | 922 | if semi.is_some() { |
| 923 | MacStmtStyle::Semicolon |
| 924 | } else { |
| 925 | MacStmtStyle::Braces |
| 926 | }, |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 927 | attrs, |
| 928 | )))) |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 929 | )); |
| 930 | |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 931 | named!(stmt_local -> Stmt, do_parse!( |
| 932 | attrs: many0!(outer_attr) >> |
| 933 | keyword!("let") >> |
| 934 | pat: pat >> |
| 935 | ty: option!(preceded!(punct!(":"), ty)) >> |
| 936 | init: option!(preceded!(punct!("="), expr)) >> |
| 937 | punct!(";") >> |
| 938 | (Stmt::Local(Box::new(Local { |
| 939 | pat: Box::new(pat), |
| 940 | ty: ty.map(Box::new), |
| 941 | init: init.map(Box::new), |
| 942 | attrs: attrs, |
| 943 | }))) |
| 944 | )); |
| 945 | |
| 946 | named!(stmt_item -> Stmt, map!(item, |i| Stmt::Item(Box::new(i)))); |
| 947 | |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 948 | fn requires_semi(e: &Expr) -> bool { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 949 | match e.node { |
| 950 | ExprKind::If(_, _, _) | |
| 951 | ExprKind::IfLet(_, _, _, _) | |
| 952 | ExprKind::While(_, _, _) | |
| 953 | ExprKind::WhileLet(_, _, _, _) | |
| 954 | ExprKind::ForLoop(_, _, _, _) | |
| 955 | ExprKind::Loop(_, _) | |
| 956 | ExprKind::Match(_, _) | |
| 957 | ExprKind::Block(_, _) => false, |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 958 | |
| 959 | _ => true, |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | named!(stmt_expr -> Stmt, do_parse!( |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 964 | attrs: many0!(outer_attr) >> |
| 965 | mut e: expr >> |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 966 | semi: option!(punct!(";")) >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 967 | ({ |
| 968 | e.attrs = attrs; |
| 969 | if semi.is_some() { |
| 970 | Stmt::Semi(Box::new(e)) |
David Tolnay | 092dcb0 | 2016-10-30 10:14:14 -0700 | [diff] [blame] | 971 | } else if requires_semi(&e) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 972 | return Error; |
| 973 | } else { |
| 974 | Stmt::Expr(Box::new(e)) |
| 975 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 976 | }) |
David Tolnay | 939766a | 2016-09-23 23:48:12 -0700 | [diff] [blame] | 977 | )); |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 978 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 979 | named!(pub pat -> Pat, alt!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 980 | pat_wild // must be before pat_ident |
| 981 | | |
| 982 | pat_box // must be before pat_ident |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 983 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 984 | pat_range // must be before pat_lit |
| 985 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 986 | pat_tuple_struct // must be before pat_ident |
| 987 | | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 988 | pat_struct // must be before pat_ident |
| 989 | | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 990 | pat_mac // must be before pat_ident |
| 991 | | |
David Tolnay | bcdbdd0 | 2016-10-24 23:05:02 -0700 | [diff] [blame] | 992 | pat_lit // must be before pat_ident |
| 993 | | |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 994 | pat_ident // must be before pat_path |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 995 | | |
| 996 | pat_path |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 997 | | |
| 998 | pat_tuple |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 999 | | |
| 1000 | pat_ref |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1001 | | |
| 1002 | pat_slice |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1003 | )); |
| 1004 | |
David Tolnay | 84aa075 | 2016-10-02 23:01:13 -0700 | [diff] [blame] | 1005 | named!(pat_mac -> Pat, map!(mac, Pat::Mac)); |
| 1006 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1007 | named!(pat_wild -> Pat, map!(keyword!("_"), |_| Pat::Wild)); |
| 1008 | |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1009 | named!(pat_box -> Pat, do_parse!( |
| 1010 | keyword!("box") >> |
| 1011 | pat: pat >> |
| 1012 | (Pat::Box(Box::new(pat))) |
| 1013 | )); |
| 1014 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1015 | named!(pat_ident -> Pat, do_parse!( |
| 1016 | mode: option!(keyword!("ref")) >> |
| 1017 | mutability: mutability >> |
David Tolnay | 8d4d2fa | 2016-10-25 22:08:07 -0700 | [diff] [blame] | 1018 | name: alt!( |
| 1019 | ident |
| 1020 | | |
| 1021 | keyword!("self") => { Into::into } |
| 1022 | ) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 1023 | not!(punct!("<")) >> |
| 1024 | not!(punct!("::")) >> |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1025 | subpat: option!(preceded!(punct!("@"), pat)) >> |
| 1026 | (Pat::Ident( |
| 1027 | if mode.is_some() { |
| 1028 | BindingMode::ByRef(mutability) |
| 1029 | } else { |
| 1030 | BindingMode::ByValue(mutability) |
| 1031 | }, |
| 1032 | name, |
| 1033 | subpat.map(Box::new), |
| 1034 | )) |
| 1035 | )); |
| 1036 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1037 | named!(pat_tuple_struct -> Pat, do_parse!( |
| 1038 | path: path >> |
| 1039 | tuple: pat_tuple_helper >> |
| 1040 | (Pat::TupleStruct(path, tuple.0, tuple.1)) |
| 1041 | )); |
| 1042 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1043 | named!(pat_struct -> Pat, do_parse!( |
| 1044 | path: path >> |
| 1045 | punct!("{") >> |
| 1046 | fields: separated_list!(punct!(","), field_pat) >> |
| 1047 | more: option!(preceded!( |
| 1048 | cond!(!fields.is_empty(), punct!(",")), |
| 1049 | punct!("..") |
| 1050 | )) >> |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 1051 | cond!(!fields.is_empty() && more.is_none(), option!(punct!(","))) >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1052 | punct!("}") >> |
| 1053 | (Pat::Struct(path, fields, more.is_some())) |
| 1054 | )); |
| 1055 | |
| 1056 | named!(field_pat -> FieldPat, alt!( |
| 1057 | do_parse!( |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 1058 | ident: wordlike >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1059 | punct!(":") >> |
| 1060 | pat: pat >> |
| 1061 | (FieldPat { |
| 1062 | ident: ident, |
| 1063 | pat: Box::new(pat), |
| 1064 | is_shorthand: false, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 1065 | attrs: Vec::new(), |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1066 | }) |
| 1067 | ) |
| 1068 | | |
| 1069 | do_parse!( |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1070 | boxed: option!(keyword!("box")) >> |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1071 | mode: option!(keyword!("ref")) >> |
| 1072 | mutability: mutability >> |
| 1073 | ident: ident >> |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1074 | ({ |
| 1075 | let mut pat = Pat::Ident( |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1076 | if mode.is_some() { |
| 1077 | BindingMode::ByRef(mutability) |
| 1078 | } else { |
| 1079 | BindingMode::ByValue(mutability) |
| 1080 | }, |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1081 | ident.clone(), |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1082 | None, |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1083 | ); |
| 1084 | if boxed.is_some() { |
| 1085 | pat = Pat::Box(Box::new(pat)); |
| 1086 | } |
| 1087 | FieldPat { |
| 1088 | ident: ident, |
| 1089 | pat: Box::new(pat), |
| 1090 | is_shorthand: true, |
David Tolnay | 71d9377 | 2017-01-22 23:58:24 -0800 | [diff] [blame] | 1091 | attrs: Vec::new(), |
David Tolnay | da16738 | 2016-10-30 13:34:09 -0700 | [diff] [blame] | 1092 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1093 | }) |
| 1094 | ) |
| 1095 | )); |
| 1096 | |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1097 | named!(pat_path -> Pat, map!(qpath, |(qself, path)| Pat::Path(qself, path))); |
| 1098 | |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1099 | named!(pat_tuple -> Pat, map!( |
| 1100 | pat_tuple_helper, |
| 1101 | |(pats, dotdot)| Pat::Tuple(pats, dotdot) |
| 1102 | )); |
| 1103 | |
| 1104 | named!(pat_tuple_helper -> (Vec<Pat>, Option<usize>), do_parse!( |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1105 | punct!("(") >> |
| 1106 | mut elems: separated_list!(punct!(","), pat) >> |
| 1107 | dotdot: option!(do_parse!( |
| 1108 | cond!(!elems.is_empty(), punct!(",")) >> |
| 1109 | punct!("..") >> |
| 1110 | rest: many0!(preceded!(punct!(","), pat)) >> |
| 1111 | cond!(!rest.is_empty(), option!(punct!(","))) >> |
| 1112 | (rest) |
| 1113 | )) >> |
| 1114 | cond!(!elems.is_empty() && dotdot.is_none(), option!(punct!(","))) >> |
| 1115 | punct!(")") >> |
| 1116 | (match dotdot { |
| 1117 | Some(rest) => { |
| 1118 | let pos = elems.len(); |
| 1119 | elems.extend(rest); |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1120 | (elems, Some(pos)) |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1121 | } |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1122 | None => (elems, None), |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1123 | }) |
| 1124 | )); |
| 1125 | |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1126 | named!(pat_ref -> Pat, do_parse!( |
| 1127 | punct!("&") >> |
| 1128 | mutability: mutability >> |
| 1129 | pat: pat >> |
| 1130 | (Pat::Ref(Box::new(pat), mutability)) |
| 1131 | )); |
| 1132 | |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1133 | named!(pat_lit -> Pat, do_parse!( |
| 1134 | lit: pat_lit_expr >> |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1135 | (if let ExprKind::Path(_, _) = lit.node { |
David Tolnay | ef40da4 | 2016-10-30 01:19:04 -0700 | [diff] [blame] | 1136 | return IResult::Error; // these need to be parsed by pat_path |
| 1137 | } else { |
| 1138 | Pat::Lit(Box::new(lit)) |
| 1139 | }) |
| 1140 | )); |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1141 | |
| 1142 | named!(pat_range -> Pat, do_parse!( |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1143 | lo: pat_lit_expr >> |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1144 | punct!("...") >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1145 | hi: pat_lit_expr >> |
David Tolnay | e131090 | 2016-10-29 23:40:00 -0700 | [diff] [blame] | 1146 | (Pat::Range(Box::new(lo), Box::new(hi))) |
| 1147 | )); |
| 1148 | |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1149 | named!(pat_lit_expr -> Expr, do_parse!( |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1150 | neg: option!(punct!("-")) >> |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1151 | v: alt!( |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1152 | lit => { ExprKind::Lit } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1153 | | |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1154 | path => { |p| ExprKind::Path(None, p) } |
David Tolnay | 2cfddc6 | 2016-10-30 01:03:27 -0700 | [diff] [blame] | 1155 | ) >> |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1156 | (if neg.is_some() { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1157 | ExprKind::Unary(UnOp::Neg, Box::new(v.into())).into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1158 | } else { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1159 | v.into() |
David Tolnay | 0ad9e9f | 2016-10-29 22:20:02 -0700 | [diff] [blame] | 1160 | }) |
| 1161 | )); |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1162 | |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1163 | named!(pat_slice -> Pat, do_parse!( |
| 1164 | punct!("[") >> |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1165 | mut before: separated_list!(punct!(","), pat) >> |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1166 | after: option!(do_parse!( |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1167 | comma_before_dots: option!(cond_reduce!(!before.is_empty(), punct!(","))) >> |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1168 | punct!("..") >> |
| 1169 | after: many0!(preceded!(punct!(","), pat)) >> |
| 1170 | cond!(!after.is_empty(), option!(punct!(","))) >> |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1171 | (comma_before_dots.is_some(), after) |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1172 | )) >> |
David Tolnay | 6173527 | 2016-10-30 17:34:23 -0700 | [diff] [blame] | 1173 | cond!(after.is_none(), option!(punct!(","))) >> |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1174 | punct!("]") >> |
| 1175 | (match after { |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1176 | None => Pat::Slice(before, None, Vec::new()), |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1177 | Some((true, after)) => { |
| 1178 | if before.is_empty() { |
| 1179 | return IResult::Error; |
| 1180 | } |
| 1181 | Pat::Slice(before, Some(Box::new(Pat::Wild)), after) |
| 1182 | } |
| 1183 | Some((false, after)) => { |
| 1184 | let rest = before.pop().unwrap_or(Pat::Wild); |
| 1185 | Pat::Slice(before, Some(Box::new(rest)), after) |
| 1186 | } |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1187 | }) |
| 1188 | )); |
| 1189 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1190 | named!(capture_by -> CaptureBy, alt!( |
| 1191 | keyword!("move") => { |_| CaptureBy::Value } |
| 1192 | | |
| 1193 | epsilon!() => { |_| CaptureBy::Ref } |
| 1194 | )); |
| 1195 | |
David Tolnay | 8b07f37 | 2016-09-30 10:28:40 -0700 | [diff] [blame] | 1196 | named!(label -> Ident, map!(lifetime, |lt: Lifetime| lt.ident)); |
David Tolnay | b9c8e32 | 2016-09-23 20:48:37 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1199 | #[cfg(feature = "printing")] |
| 1200 | mod printing { |
| 1201 | use super::*; |
David Tolnay | 87d4e15 | 2016-12-22 19:06:37 -0500 | [diff] [blame] | 1202 | use {FnArg, FunctionRetTy, Mutability, Ty, Unsafety}; |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1203 | use attr::FilterAttrs; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1204 | use quote::{Tokens, ToTokens}; |
| 1205 | |
| 1206 | impl ToTokens for Expr { |
| 1207 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1208 | tokens.append_all(self.attrs.outer()); |
| 1209 | match self.node { |
| 1210 | ExprKind::Box(ref inner) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1211 | tokens.append("box"); |
| 1212 | inner.to_tokens(tokens); |
| 1213 | } |
David Tolnay | 6696c3e | 2016-10-30 11:45:10 -0700 | [diff] [blame] | 1214 | ExprKind::InPlace(ref place, ref value) => { |
| 1215 | tokens.append("in"); |
| 1216 | place.to_tokens(tokens); |
| 1217 | value.to_tokens(tokens); |
| 1218 | } |
David Tolnay | 9c7ab13 | 2017-01-23 00:01:22 -0800 | [diff] [blame] | 1219 | ExprKind::Array(ref tys) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1220 | tokens.append("["); |
| 1221 | tokens.append_separated(tys, ","); |
| 1222 | tokens.append("]"); |
| 1223 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1224 | ExprKind::Call(ref func, ref args) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1225 | func.to_tokens(tokens); |
| 1226 | tokens.append("("); |
| 1227 | tokens.append_separated(args, ","); |
| 1228 | tokens.append(")"); |
| 1229 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1230 | ExprKind::MethodCall(ref ident, ref ascript, ref args) => { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1231 | args[0].to_tokens(tokens); |
| 1232 | tokens.append("."); |
| 1233 | ident.to_tokens(tokens); |
David Tolnay | 58f6f67 | 2016-10-19 08:44:25 -0700 | [diff] [blame] | 1234 | if !ascript.is_empty() { |
David Tolnay | 9636c05 | 2016-10-02 17:11:17 -0700 | [diff] [blame] | 1235 | tokens.append("::"); |
| 1236 | tokens.append("<"); |
| 1237 | tokens.append_separated(ascript, ","); |
| 1238 | tokens.append(">"); |
| 1239 | } |
| 1240 | tokens.append("("); |
| 1241 | tokens.append_separated(&args[1..], ","); |
| 1242 | tokens.append(")"); |
| 1243 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1244 | ExprKind::Tup(ref fields) => { |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame] | 1245 | tokens.append("("); |
| 1246 | tokens.append_separated(fields, ","); |
| 1247 | if fields.len() == 1 { |
| 1248 | tokens.append(","); |
| 1249 | } |
| 1250 | tokens.append(")"); |
| 1251 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1252 | ExprKind::Binary(op, ref left, ref right) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1253 | left.to_tokens(tokens); |
| 1254 | op.to_tokens(tokens); |
| 1255 | right.to_tokens(tokens); |
| 1256 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1257 | ExprKind::Unary(op, ref expr) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1258 | op.to_tokens(tokens); |
| 1259 | expr.to_tokens(tokens); |
| 1260 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1261 | ExprKind::Lit(ref lit) => lit.to_tokens(tokens), |
| 1262 | ExprKind::Cast(ref expr, ref ty) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1263 | expr.to_tokens(tokens); |
| 1264 | tokens.append("as"); |
| 1265 | ty.to_tokens(tokens); |
| 1266 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1267 | ExprKind::Type(ref expr, ref ty) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1268 | expr.to_tokens(tokens); |
| 1269 | tokens.append(":"); |
| 1270 | ty.to_tokens(tokens); |
| 1271 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1272 | ExprKind::If(ref cond, ref then_block, ref else_block) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1273 | tokens.append("if"); |
| 1274 | cond.to_tokens(tokens); |
| 1275 | then_block.to_tokens(tokens); |
| 1276 | if let Some(ref else_block) = *else_block { |
| 1277 | tokens.append("else"); |
| 1278 | else_block.to_tokens(tokens); |
| 1279 | } |
| 1280 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1281 | ExprKind::IfLet(ref pat, ref expr, ref then_block, ref else_block) => { |
David Tolnay | 29f9ce1 | 2016-10-02 20:58:40 -0700 | [diff] [blame] | 1282 | tokens.append("if"); |
| 1283 | tokens.append("let"); |
| 1284 | pat.to_tokens(tokens); |
| 1285 | tokens.append("="); |
| 1286 | expr.to_tokens(tokens); |
| 1287 | then_block.to_tokens(tokens); |
| 1288 | if let Some(ref else_block) = *else_block { |
| 1289 | tokens.append("else"); |
| 1290 | else_block.to_tokens(tokens); |
| 1291 | } |
| 1292 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1293 | ExprKind::While(ref cond, ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1294 | if let Some(ref label) = *label { |
| 1295 | label.to_tokens(tokens); |
| 1296 | tokens.append(":"); |
| 1297 | } |
| 1298 | tokens.append("while"); |
| 1299 | cond.to_tokens(tokens); |
| 1300 | body.to_tokens(tokens); |
| 1301 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1302 | ExprKind::WhileLet(ref pat, ref expr, ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1303 | if let Some(ref label) = *label { |
| 1304 | label.to_tokens(tokens); |
| 1305 | tokens.append(":"); |
| 1306 | } |
| 1307 | tokens.append("while"); |
| 1308 | tokens.append("let"); |
| 1309 | pat.to_tokens(tokens); |
| 1310 | tokens.append("="); |
| 1311 | expr.to_tokens(tokens); |
| 1312 | body.to_tokens(tokens); |
| 1313 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1314 | ExprKind::ForLoop(ref pat, ref expr, ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1315 | if let Some(ref label) = *label { |
| 1316 | label.to_tokens(tokens); |
| 1317 | tokens.append(":"); |
| 1318 | } |
| 1319 | tokens.append("for"); |
| 1320 | pat.to_tokens(tokens); |
| 1321 | tokens.append("in"); |
| 1322 | expr.to_tokens(tokens); |
| 1323 | body.to_tokens(tokens); |
| 1324 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1325 | ExprKind::Loop(ref body, ref label) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1326 | if let Some(ref label) = *label { |
| 1327 | label.to_tokens(tokens); |
| 1328 | tokens.append(":"); |
| 1329 | } |
| 1330 | tokens.append("loop"); |
| 1331 | body.to_tokens(tokens); |
| 1332 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1333 | ExprKind::Match(ref expr, ref arms) => { |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1334 | tokens.append("match"); |
| 1335 | expr.to_tokens(tokens); |
| 1336 | tokens.append("{"); |
David Tolnay | 2165b04 | 2016-10-08 00:04:23 -0700 | [diff] [blame] | 1337 | tokens.append_all(arms); |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1338 | tokens.append("}"); |
| 1339 | } |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 1340 | ExprKind::Closure(capture, ref decl, ref expr) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1341 | capture.to_tokens(tokens); |
| 1342 | tokens.append("|"); |
| 1343 | for (i, input) in decl.inputs.iter().enumerate() { |
| 1344 | if i > 0 { |
| 1345 | tokens.append(","); |
| 1346 | } |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1347 | match *input { |
| 1348 | FnArg::Captured(ref pat, Ty::Infer) => { |
| 1349 | pat.to_tokens(tokens); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1350 | } |
David Tolnay | ca08542 | 2016-10-04 00:12:38 -0700 | [diff] [blame] | 1351 | _ => input.to_tokens(tokens), |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | tokens.append("|"); |
| 1355 | match decl.output { |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 1356 | FunctionRetTy::Default => { /* nothing */ } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1357 | FunctionRetTy::Ty(ref ty) => { |
| 1358 | tokens.append("->"); |
| 1359 | ty.to_tokens(tokens); |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
David Tolnay | 58af355 | 2016-12-22 16:58:07 -0500 | [diff] [blame] | 1362 | expr.to_tokens(tokens); |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1363 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1364 | ExprKind::Block(rules, ref block) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1365 | rules.to_tokens(tokens); |
| 1366 | block.to_tokens(tokens); |
| 1367 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1368 | ExprKind::Assign(ref var, ref expr) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1369 | var.to_tokens(tokens); |
| 1370 | tokens.append("="); |
| 1371 | expr.to_tokens(tokens); |
| 1372 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1373 | ExprKind::AssignOp(op, ref var, ref expr) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1374 | var.to_tokens(tokens); |
David Tolnay | 3cb23a9 | 2016-10-07 23:02:21 -0700 | [diff] [blame] | 1375 | tokens.append(op.assign_op().unwrap()); |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1376 | expr.to_tokens(tokens); |
| 1377 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1378 | ExprKind::Field(ref expr, ref field) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1379 | expr.to_tokens(tokens); |
| 1380 | tokens.append("."); |
| 1381 | field.to_tokens(tokens); |
| 1382 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1383 | ExprKind::TupField(ref expr, field) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1384 | expr.to_tokens(tokens); |
| 1385 | tokens.append("."); |
| 1386 | tokens.append(&field.to_string()); |
| 1387 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1388 | ExprKind::Index(ref expr, ref index) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1389 | expr.to_tokens(tokens); |
| 1390 | tokens.append("["); |
| 1391 | index.to_tokens(tokens); |
| 1392 | tokens.append("]"); |
| 1393 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1394 | ExprKind::Range(ref from, ref to, limits) => { |
David Tolnay | bb6feae | 2016-10-02 21:25:20 -0700 | [diff] [blame] | 1395 | from.to_tokens(tokens); |
| 1396 | match limits { |
| 1397 | RangeLimits::HalfOpen => tokens.append(".."), |
| 1398 | RangeLimits::Closed => tokens.append("..."), |
| 1399 | } |
| 1400 | to.to_tokens(tokens); |
| 1401 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1402 | ExprKind::Path(None, ref path) => path.to_tokens(tokens), |
| 1403 | ExprKind::Path(Some(ref qself), ref path) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1404 | tokens.append("<"); |
| 1405 | qself.ty.to_tokens(tokens); |
| 1406 | if qself.position > 0 { |
| 1407 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1408 | for (i, segment) in path.segments |
| 1409 | .iter() |
| 1410 | .take(qself.position) |
| 1411 | .enumerate() { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1412 | if i > 0 || path.global { |
| 1413 | tokens.append("::"); |
| 1414 | } |
| 1415 | segment.to_tokens(tokens); |
| 1416 | } |
| 1417 | } |
| 1418 | tokens.append(">"); |
| 1419 | for segment in path.segments.iter().skip(qself.position) { |
| 1420 | tokens.append("::"); |
| 1421 | segment.to_tokens(tokens); |
| 1422 | } |
| 1423 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1424 | ExprKind::AddrOf(mutability, ref expr) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1425 | tokens.append("&"); |
| 1426 | mutability.to_tokens(tokens); |
| 1427 | expr.to_tokens(tokens); |
| 1428 | } |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1429 | ExprKind::Break(ref opt_label, ref opt_val) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1430 | tokens.append("break"); |
| 1431 | opt_label.to_tokens(tokens); |
David Tolnay | 5d55ef7 | 2016-12-21 20:20:04 -0500 | [diff] [blame] | 1432 | opt_val.to_tokens(tokens); |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1433 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1434 | ExprKind::Continue(ref opt_label) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1435 | tokens.append("continue"); |
| 1436 | opt_label.to_tokens(tokens); |
| 1437 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1438 | ExprKind::Ret(ref opt_expr) => { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1439 | tokens.append("return"); |
| 1440 | opt_expr.to_tokens(tokens); |
| 1441 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1442 | ExprKind::Mac(ref mac) => mac.to_tokens(tokens), |
| 1443 | ExprKind::Struct(ref path, ref fields, ref base) => { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1444 | path.to_tokens(tokens); |
| 1445 | tokens.append("{"); |
| 1446 | tokens.append_separated(fields, ","); |
| 1447 | if let Some(ref base) = *base { |
| 1448 | if !fields.is_empty() { |
| 1449 | tokens.append(","); |
| 1450 | } |
| 1451 | tokens.append(".."); |
| 1452 | base.to_tokens(tokens); |
| 1453 | } |
| 1454 | tokens.append("}"); |
| 1455 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1456 | ExprKind::Repeat(ref expr, ref times) => { |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1457 | tokens.append("["); |
| 1458 | expr.to_tokens(tokens); |
| 1459 | tokens.append(";"); |
| 1460 | times.to_tokens(tokens); |
| 1461 | tokens.append("]"); |
| 1462 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1463 | ExprKind::Paren(ref expr) => { |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1464 | tokens.append("("); |
| 1465 | expr.to_tokens(tokens); |
| 1466 | tokens.append(")"); |
| 1467 | } |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1468 | ExprKind::Try(ref expr) => { |
David Tolnay | 3c2467c | 2016-10-02 17:55:08 -0700 | [diff] [blame] | 1469 | expr.to_tokens(tokens); |
| 1470 | tokens.append("?"); |
| 1471 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1475 | |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1476 | impl ToTokens for FieldValue { |
| 1477 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1478 | self.ident.to_tokens(tokens); |
David Tolnay | 276690f | 2016-10-30 12:06:59 -0700 | [diff] [blame] | 1479 | if !self.is_shorthand { |
| 1480 | tokens.append(":"); |
| 1481 | self.expr.to_tokens(tokens); |
| 1482 | } |
David Tolnay | 055a704 | 2016-10-02 19:23:54 -0700 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1486 | impl ToTokens for Arm { |
| 1487 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1488 | for attr in &self.attrs { |
| 1489 | attr.to_tokens(tokens); |
| 1490 | } |
| 1491 | tokens.append_separated(&self.pats, "|"); |
| 1492 | if let Some(ref guard) = self.guard { |
| 1493 | tokens.append("if"); |
| 1494 | guard.to_tokens(tokens); |
| 1495 | } |
| 1496 | tokens.append("=>"); |
| 1497 | self.body.to_tokens(tokens); |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1498 | match self.body.node { |
David Tolnay | 7b03591 | 2016-12-21 22:42:07 -0500 | [diff] [blame] | 1499 | ExprKind::Block(Unsafety::Normal, _) => { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1500 | // no comma |
| 1501 | } |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1502 | _ => tokens.append(","), |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | impl ToTokens for Pat { |
| 1508 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1509 | match *self { |
| 1510 | Pat::Wild => tokens.append("_"), |
| 1511 | Pat::Ident(mode, ref ident, ref subpat) => { |
| 1512 | mode.to_tokens(tokens); |
| 1513 | ident.to_tokens(tokens); |
| 1514 | if let Some(ref subpat) = *subpat { |
| 1515 | tokens.append("@"); |
| 1516 | subpat.to_tokens(tokens); |
| 1517 | } |
| 1518 | } |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1519 | Pat::Struct(ref path, ref fields, dots) => { |
| 1520 | path.to_tokens(tokens); |
| 1521 | tokens.append("{"); |
| 1522 | tokens.append_separated(fields, ","); |
| 1523 | if dots { |
| 1524 | if !fields.is_empty() { |
| 1525 | tokens.append(","); |
| 1526 | } |
| 1527 | tokens.append(".."); |
| 1528 | } |
| 1529 | tokens.append("}"); |
| 1530 | } |
David Tolnay | aa61094 | 2016-10-03 22:22:49 -0700 | [diff] [blame] | 1531 | Pat::TupleStruct(ref path, ref pats, dotpos) => { |
| 1532 | path.to_tokens(tokens); |
| 1533 | tokens.append("("); |
| 1534 | match dotpos { |
| 1535 | Some(pos) => { |
| 1536 | if pos > 0 { |
| 1537 | tokens.append_separated(&pats[..pos], ","); |
| 1538 | tokens.append(","); |
| 1539 | } |
| 1540 | tokens.append(".."); |
| 1541 | if pos < pats.len() { |
| 1542 | tokens.append(","); |
| 1543 | tokens.append_separated(&pats[pos..], ","); |
| 1544 | } |
| 1545 | } |
| 1546 | None => tokens.append_separated(pats, ","), |
| 1547 | } |
| 1548 | tokens.append(")"); |
| 1549 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1550 | Pat::Path(None, ref path) => path.to_tokens(tokens), |
| 1551 | Pat::Path(Some(ref qself), ref path) => { |
| 1552 | tokens.append("<"); |
| 1553 | qself.ty.to_tokens(tokens); |
| 1554 | if qself.position > 0 { |
| 1555 | tokens.append("as"); |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1556 | for (i, segment) in path.segments |
| 1557 | .iter() |
| 1558 | .take(qself.position) |
| 1559 | .enumerate() { |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1560 | if i > 0 || path.global { |
| 1561 | tokens.append("::"); |
| 1562 | } |
| 1563 | segment.to_tokens(tokens); |
| 1564 | } |
| 1565 | } |
| 1566 | tokens.append(">"); |
| 1567 | for segment in path.segments.iter().skip(qself.position) { |
| 1568 | tokens.append("::"); |
| 1569 | segment.to_tokens(tokens); |
| 1570 | } |
| 1571 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1572 | Pat::Tuple(ref pats, dotpos) => { |
| 1573 | tokens.append("("); |
| 1574 | match dotpos { |
| 1575 | Some(pos) => { |
| 1576 | if pos > 0 { |
| 1577 | tokens.append_separated(&pats[..pos], ","); |
| 1578 | tokens.append(","); |
| 1579 | } |
| 1580 | tokens.append(".."); |
| 1581 | if pos < pats.len() { |
| 1582 | tokens.append(","); |
| 1583 | tokens.append_separated(&pats[pos..], ","); |
| 1584 | } |
| 1585 | } |
David Tolnay | 37aa296 | 2016-10-30 10:11:12 -0700 | [diff] [blame] | 1586 | None => { |
| 1587 | tokens.append_separated(pats, ","); |
| 1588 | if pats.len() == 1 { |
| 1589 | tokens.append(","); |
| 1590 | } |
| 1591 | } |
David Tolnay | fbb7323 | 2016-10-03 01:00:06 -0700 | [diff] [blame] | 1592 | } |
| 1593 | tokens.append(")"); |
| 1594 | } |
| 1595 | Pat::Box(ref inner) => { |
| 1596 | tokens.append("box"); |
| 1597 | inner.to_tokens(tokens); |
| 1598 | } |
David Tolnay | ffdb97f | 2016-10-03 01:28:33 -0700 | [diff] [blame] | 1599 | Pat::Ref(ref target, mutability) => { |
| 1600 | tokens.append("&"); |
| 1601 | mutability.to_tokens(tokens); |
| 1602 | target.to_tokens(tokens); |
| 1603 | } |
David Tolnay | 8b308c2 | 2016-10-03 01:24:10 -0700 | [diff] [blame] | 1604 | Pat::Lit(ref lit) => lit.to_tokens(tokens), |
| 1605 | Pat::Range(ref lo, ref hi) => { |
| 1606 | lo.to_tokens(tokens); |
| 1607 | tokens.append("..."); |
| 1608 | hi.to_tokens(tokens); |
| 1609 | } |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1610 | Pat::Slice(ref before, ref rest, ref after) => { |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1611 | tokens.append("["); |
| 1612 | tokens.append_separated(before, ","); |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1613 | if let Some(ref rest) = *rest { |
| 1614 | if !before.is_empty() { |
| 1615 | tokens.append(","); |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1616 | } |
David Tolnay | e1f13c3 | 2016-10-29 23:34:40 -0700 | [diff] [blame] | 1617 | if **rest != Pat::Wild { |
| 1618 | rest.to_tokens(tokens); |
| 1619 | } |
| 1620 | tokens.append(".."); |
| 1621 | if !after.is_empty() { |
| 1622 | tokens.append(","); |
| 1623 | } |
| 1624 | tokens.append_separated(after, ","); |
David Tolnay | 435a9a8 | 2016-10-29 13:47:20 -0700 | [diff] [blame] | 1625 | } |
| 1626 | tokens.append("]"); |
| 1627 | } |
David Tolnay | cc3d66e | 2016-10-02 23:36:05 -0700 | [diff] [blame] | 1628 | Pat::Mac(ref mac) => mac.to_tokens(tokens), |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | |
David Tolnay | 8d9e81a | 2016-10-03 22:36:32 -0700 | [diff] [blame] | 1633 | impl ToTokens for FieldPat { |
| 1634 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1635 | if !self.is_shorthand { |
| 1636 | self.ident.to_tokens(tokens); |
| 1637 | tokens.append(":"); |
| 1638 | } |
| 1639 | self.pat.to_tokens(tokens); |
| 1640 | } |
| 1641 | } |
| 1642 | |
David Tolnay | b4ad3b5 | 2016-10-01 21:58:13 -0700 | [diff] [blame] | 1643 | impl ToTokens for BindingMode { |
| 1644 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1645 | match *self { |
| 1646 | BindingMode::ByRef(Mutability::Immutable) => { |
| 1647 | tokens.append("ref"); |
| 1648 | } |
| 1649 | BindingMode::ByRef(Mutability::Mutable) => { |
| 1650 | tokens.append("ref"); |
| 1651 | tokens.append("mut"); |
| 1652 | } |
| 1653 | BindingMode::ByValue(Mutability::Immutable) => {} |
| 1654 | BindingMode::ByValue(Mutability::Mutable) => { |
| 1655 | tokens.append("mut"); |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1660 | |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1661 | impl ToTokens for CaptureBy { |
| 1662 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1663 | match *self { |
| 1664 | CaptureBy::Value => tokens.append("move"), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1665 | CaptureBy::Ref => { |
| 1666 | // nothing |
| 1667 | } |
David Tolnay | 89e0567 | 2016-10-02 14:39:42 -0700 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1672 | impl ToTokens for Block { |
| 1673 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1674 | tokens.append("{"); |
David Tolnay | 3b9783a | 2016-10-29 22:37:09 -0700 | [diff] [blame] | 1675 | tokens.append_all(&self.stmts); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1676 | tokens.append("}"); |
| 1677 | } |
| 1678 | } |
| 1679 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1680 | impl ToTokens for Stmt { |
| 1681 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 1682 | match *self { |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1683 | Stmt::Local(ref local) => local.to_tokens(tokens), |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1684 | Stmt::Item(ref item) => item.to_tokens(tokens), |
| 1685 | Stmt::Expr(ref expr) => expr.to_tokens(tokens), |
| 1686 | Stmt::Semi(ref expr) => { |
| 1687 | expr.to_tokens(tokens); |
| 1688 | tokens.append(";"); |
| 1689 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1690 | Stmt::Mac(ref mac) => { |
| 1691 | let (ref mac, style, ref attrs) = **mac; |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 1692 | tokens.append_all(attrs.outer()); |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1693 | mac.to_tokens(tokens); |
| 1694 | match style { |
| 1695 | MacStmtStyle::Semicolon => tokens.append(";"), |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1696 | MacStmtStyle::Braces | MacStmtStyle::NoBraces => { |
| 1697 | // no semicolon |
| 1698 | } |
David Tolnay | 13b3d35 | 2016-10-03 00:31:15 -0700 | [diff] [blame] | 1699 | } |
| 1700 | } |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | } |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1704 | |
| 1705 | impl ToTokens for Local { |
| 1706 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 4e3158d | 2016-10-30 00:30:01 -0700 | [diff] [blame] | 1707 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 191e058 | 2016-10-02 18:31:09 -0700 | [diff] [blame] | 1708 | tokens.append("let"); |
| 1709 | self.pat.to_tokens(tokens); |
| 1710 | if let Some(ref ty) = self.ty { |
| 1711 | tokens.append(":"); |
| 1712 | ty.to_tokens(tokens); |
| 1713 | } |
| 1714 | if let Some(ref init) = self.init { |
| 1715 | tokens.append("="); |
| 1716 | init.to_tokens(tokens); |
| 1717 | } |
| 1718 | tokens.append(";"); |
| 1719 | } |
| 1720 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1721 | } |